4 #include <cfg/compiler.h>
9 * Purge local variables, by zeroing them.
11 * This can be used to clear stack from intermediate results in crypto
12 * calculations that might somehow be leaked.
15 memset(&x, 0, sizeof(x))
18 * Convert a generic "password" (low-diffused) to a generic "key"
21 * In common terminology, a "password" is a key with weak cryptographic
22 * characteristics, such as commonly used password input by an user,
23 * which are usually short and use only a few different characters from
24 * the 0-255 byte range.
26 * This function derives a strong key from the password using a one-way
29 * \note Uses PBKDF2 as key-derivation function, with a fixed salt that
30 * changes for each Bertos project.
32 void password2key(const char *pwd, size_t pwd_len,
33 uint8_t *key, size_t key_len);
36 * Perform a bitwise xor between \a in and \a inout, and store
37 * the result into \a inout.
39 INLINE void xor_block(uint8_t *out, const uint8_t *in1, const uint8_t* in2, size_t len);
42 * Perform a bitwise xor over \a inout with constant \a k.
44 INLINE void xor_block_const(uint8_t *out, const uint8_t *in, uint8_t k, size_t len);
47 // FIXME: provide non-32bit fallback
48 // FIXME: proper ifdef conditional
49 #if 1 // 32-bit optimized versions
51 // FIXME: this code is currently buggy because it ignores alignment issues.
52 INLINE void xor_block(uint8_t *out, const uint8_t *in1, const uint8_t* in2, size_t len)
54 ASSERT(((size_t)in1 % 4) == 0);
55 ASSERT(((size_t)in2 % 4) == 0);
56 ASSERT(((size_t)out % 4) == 0);
58 const uint32_t *ibuf1 = (const uint32_t *)in1;
59 const uint32_t *ibuf2 = (const uint32_t *)in2;
60 uint32_t *obuf = (uint32_t *)out;
61 size_t rem = (len & 3);
65 *obuf++ = *ibuf1++ ^ *ibuf2++;
67 in1 = (const uint8_t*)ibuf1;
68 in2 = (const uint8_t*)ibuf2;
71 *out++ = *in1++ ^ *in2++;
74 INLINE void xor_block_const(uint8_t *out, const uint8_t *in, uint8_t k, size_t len)
76 ASSERT(((size_t)in % 4) == 0);
77 ASSERT(((size_t)out % 4) == 0);
79 uint32_t k32 = k | ((uint32_t)k<<8) | ((uint32_t)k<<16) | ((uint32_t)k<<24);
80 const uint32_t *ibuf = (const uint32_t *)in;
81 uint32_t *obuf = (uint32_t *)out;
82 size_t rem = (len & 3);
86 *obuf++ = *ibuf++ ^ k32;
88 in = (const uint8_t*)ibuf;
96 #endif /* SEC_UTIL_H */