SEC: add some documentation to the interface.
[bertos.git] / bertos / sec / util.h
1 #ifndef SEC_UTIL_H
2 #define SEC_UTIL_H
3
4 #include <cfg/compiler.h>
5 #include <cfg/debug.h>
6
7 /**
8  * Purge local variables, by zeroing them.
9  * 
10  * This can be used to clear stack from intermediate results in crypto
11  * calculations that might somehow be leaked.
12  */
13 #define PURGE(x) \
14         memset(&x, 0, sizeof(x))
15
16 /**
17  * Convert a generic "password" (low-diffused) to a generic "key"
18  * (high-diffused).
19  * 
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.
24  * 
25  * This function derives a strong key from the password using a one-way
26  * process.
27  * 
28  * \note Uses PBKDF2 as key-derivation function, with a fixed salt that
29  * changes for each Bertos project.
30  */
31 void password2key(const char *pwd, size_t pwd_len,
32                                   uint8_t *key, size_t key_len);
33                                   
34 /**
35  * Perform a bitwise xor between \a in and \a inout, and store
36  * the result into \a inout.
37  */
38 INLINE void xor_block(uint8_t *out, const uint8_t *in1, const uint8_t* in2, size_t len);
39
40 /**
41  * Perform a bitwise xor over \a inout with constant \a k.
42  */
43 INLINE void xor_block_const(uint8_t *out, const uint8_t *in, uint8_t k, size_t len);
44
45
46 // FIXME: provide non-32bit fallback
47 // FIXME: proper ifdef conditional
48 #if 1 // 32-bit optimized versions
49
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)
52 {
53         ASSERT(((size_t)in1 % 4) == 0);
54         ASSERT(((size_t)in2 % 4) == 0);
55         ASSERT(((size_t)out % 4) == 0);
56
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);
61         
62         len /= 4;
63         while (len--)
64                 *obuf++ = *ibuf1++ ^ *ibuf2++;
65
66         in1 = (const uint8_t*)ibuf1;
67         in2 = (const uint8_t*)ibuf2;
68         out = (uint8_t*)obuf;
69         while (rem--)   
70                 *out++ = *in1++ ^ *in2++;
71 }
72
73 INLINE void xor_block_const(uint8_t *out, const uint8_t *in, uint8_t k, size_t len)
74 {
75         ASSERT(((size_t)in % 4) == 0);
76         ASSERT(((size_t)out % 4) == 0);
77
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);
82         
83         len /= 4;
84         while (len--)
85                 *obuf++ = *ibuf++ ^ k32;
86
87         in = (const uint8_t*)ibuf;
88         out = (uint8_t*)obuf;
89         while (rem--)   
90                 *out++ = *in++ ^ k;
91 }
92
93 #endif
94
95 #endif /* SEC_UTIL_H */