4 #include <cfg/compiler.h>
8 * Purge local variables, by zeroing them.
10 * This can be used to clear stack from intermediate results in crypto
11 * calculations that might somehow be leaked.
14 memset(&x, 0, sizeof(x))
17 * Convert a generic "password" (low-diffused) to a generic "key"
20 * In common terminology, a "password" is a key with weak cryptographic
21 * characteristics, such as commonly used password input by an user,
22 * which are usually short and use only a few different characters from
23 * the 0-255 byte range.
25 * This function derives a strong key from the password using a one-way
28 * \note Uses PBKDF2 as key-derivation function, with a fixed salt that
29 * changes for each Bertos project.
31 void password2key(const char *pwd, size_t pwd_len,
32 uint8_t *key, size_t key_len);
35 * Perform a bitwise xor between \a in and \a inout, and store
36 * the result into \a inout.
38 INLINE void xor_block(uint8_t *out, const uint8_t *in1, const uint8_t* in2, size_t len);
41 * Perform a bitwise xor over \a inout with constant \a k.
43 INLINE void xor_block_const(uint8_t *out, const uint8_t *in, uint8_t k, size_t len);
46 // FIXME: provide non-32bit fallback
47 // FIXME: proper ifdef conditional
48 #if 1 // 32-bit optimized versions
50 // FIXME: this code is currently buggy because it ignores alignment issues.
51 INLINE void xor_block(uint8_t *out, const uint8_t *in1, const uint8_t* in2, size_t len)
53 ASSERT(((size_t)in1 % 4) == 0);
54 ASSERT(((size_t)in2 % 4) == 0);
55 ASSERT(((size_t)out % 4) == 0);
57 const uint32_t *ibuf1 = (const uint32_t *)in1;
58 const uint32_t *ibuf2 = (const uint32_t *)in2;
59 uint32_t *obuf = (uint32_t *)out;
60 size_t rem = (len & 3);
64 *obuf++ = *ibuf1++ ^ *ibuf2++;
66 in1 = (const uint8_t*)ibuf1;
67 in2 = (const uint8_t*)ibuf2;
70 *out++ = *in1++ ^ *in2++;
73 INLINE void xor_block_const(uint8_t *out, const uint8_t *in, uint8_t k, size_t len)
75 ASSERT(((size_t)in % 4) == 0);
76 ASSERT(((size_t)out % 4) == 0);
78 uint32_t k32 = k | ((uint32_t)k<<8) | ((uint32_t)k<<16) | ((uint32_t)k<<24);
79 const uint32_t *ibuf = (const uint32_t *)in;
80 uint32_t *obuf = (uint32_t *)out;
81 size_t rem = (len & 3);
85 *obuf++ = *ibuf++ ^ k32;
87 in = (const uint8_t*)ibuf;
95 #endif /* SEC_UTIL_H */