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