3 * Additional SNMPv3 functionality RFC3414 and RFC3826.
7 * Copyright (c) 2016 Elias Oenal.
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * Author: Elias Oenal <lwip@eliasoenal.com>
35 #include "snmpv3_priv.h"
36 #include "lwip/apps/snmpv3.h"
40 #if LWIP_SNMP && LWIP_SNMP_V3
42 #ifdef LWIP_SNMPV3_INCLUDE_ENGINE
43 #include LWIP_SNMPV3_INCLUDE_ENGINE
46 #define SNMP_MAX_TIME_BOOT 2147483647UL
48 /** Call this if engine has been changed. Has to reset boots, see below */
50 snmpv3_engine_id_changed(void)
52 snmpv3_set_engine_boots(0);
55 /** According to RFC3414 2.2.2.
57 * The number of times that the SNMP engine has
58 * (re-)initialized itself since snmpEngineID
59 * was last configured.
62 snmpv3_get_engine_boots_internal(void)
64 if (snmpv3_get_engine_boots() == 0 ||
65 snmpv3_get_engine_boots() < SNMP_MAX_TIME_BOOT) {
66 return snmpv3_get_engine_boots();
69 snmpv3_set_engine_boots(SNMP_MAX_TIME_BOOT);
70 return snmpv3_get_engine_boots();
75 * Once the timer reaches 2147483647 it gets reset to zero and the
76 * engine boot ups get incremented.
79 snmpv3_get_engine_time_internal(void)
81 if (snmpv3_get_engine_time() >= SNMP_MAX_TIME_BOOT) {
82 snmpv3_reset_engine_time();
84 if (snmpv3_get_engine_boots() < SNMP_MAX_TIME_BOOT - 1) {
85 snmpv3_set_engine_boots(snmpv3_get_engine_boots() + 1);
87 snmpv3_set_engine_boots(SNMP_MAX_TIME_BOOT);
91 return snmpv3_get_engine_time();
94 #if LWIP_SNMP_V3_CRYPTO
96 /* This function ignores the byte order suggestion in RFC3414
97 * since it simply doesn't influence the effectiveness of an IV.
99 * Implementing RFC3826 priv param algorithm if LWIP_RAND is available.
101 * @todo: This is a potential thread safety issue.
104 snmpv3_build_priv_param(u8_t* priv_param)
106 #ifdef LWIP_RAND /* Based on RFC3826 */
108 static u32_t priv1, priv2;
110 /* Lazy initialisation */
117 SMEMCPY(&priv_param[0], &priv1, sizeof(priv1));
118 SMEMCPY(&priv_param[4], &priv2, sizeof(priv2));
120 /* Emulate 64bit increment */
122 if (!priv1) { /* Overflow */
125 #else /* Based on RFC3414 */
127 u32_t boots = LWIP_SNMPV3_GET_ENGINE_BOOTS();
128 SMEMCPY(&priv_param[0], &boots, 4);
129 SMEMCPY(&priv_param[4], &ctr, 4);
134 #endif /* LWIP_SNMP_V3_CRYPTO */