Documentation updates.
[bertos.git] / bertos / sec / cipher.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Generic interface for symmetric block ciphers.
34  * \author Giovanni Bajo <rasky@develer.com>
35  *
36  */
37
38 #ifndef SEC_CIPHER_H
39 #define SEC_CIPHER_H
40
41 #include <cfg/compiler.h>
42 #include <cfg/debug.h>
43
44 typedef struct BlockCipher
45 {
46         void (*set_key)(struct BlockCipher *c, const void *key, size_t len);
47         void (*enc_block)(struct BlockCipher *c, void *block);
48         void (*dec_block)(struct BlockCipher *c, void *block);
49
50         void *buf;
51         uint8_t key_len;
52         uint8_t block_len;
53 } BlockCipher;
54
55
56 /**
57  * Return the key length (in bytes).
58  *
59  * In case of ciphers that allow a variabile key size with a fixed state
60  * (eg: Blowfish), this returns the preferred key length.
61  */
62 INLINE size_t cipher_key_len(BlockCipher *c)
63 {
64         return c->key_len;
65 }
66
67 /**
68  * Return the block length (in bytes)
69  */
70 INLINE size_t cipher_block_len(BlockCipher *c)
71 {
72         return c->block_len;
73 }
74
75 /**
76  * Set the current key used by the cipher.
77  *
78  * \note the buffer pointed by \a key is not modified and it is
79  * not needed anymore after this call returns. Its lenght must match
80  * the value returned by \a cipher_key_len().
81  */
82 INLINE void cipher_set_key(BlockCipher *c, const void *key)
83 {
84         ASSERT(c->set_key);
85         c->set_key(c, key, c->key_len);
86 }
87
88 /**
89  * Set the current key (of variable size) used by the cipher.
90  *
91  * This function is useful for ciphers that allow a variable size for the key
92  * (even with a fixed state). For all the other ciphers, the length must
93  * match the value returned by \a cipher_key_len().
94  *
95  * \note the buffer pointed by \a key is not modified and it is
96  * not needed anymore after this call returns.
97  */
98 INLINE void cipher_set_vkey(BlockCipher *c, const void *key, size_t len)
99 {
100         ASSERT(c->set_key);
101         c->set_key(c, key, len);
102 }
103
104 /*********************************************************************************/
105 /* ECB mode                                                                      */
106 /*********************************************************************************/
107
108 /**
109  * Encrypt a block (in-place) using the current key in ECB mode.
110  */
111 INLINE void cipher_ecb_encrypt(BlockCipher *c, void *block)
112 {
113         ASSERT(c->enc_block);
114         c->enc_block(c, block);
115 }
116
117 /**
118  * Decrypt a block (in-place) using the current key in ECB mode.
119  */
120 INLINE void cipher_ecb_decrypt(BlockCipher *c, void *block)
121 {
122         ASSERT(c->dec_block);
123         c->dec_block(c, block);
124 }
125
126
127 /*********************************************************************************/
128 /* CBC mode                                                                      */
129 /*********************************************************************************/
130
131 /**
132  * Initialize CBC by setting the IV.
133  *
134  * \note the memory pointed by \a iv will be used and modified by the CBC
135  * functions. It is caller's responsibility to keep it available until there is
136  * no more CBC work to do.
137  */
138 INLINE void cipher_cbc_begin(BlockCipher *c, void *iv)
139 {
140         c->buf = iv;
141 }
142
143 /**
144  * Encrypt a block (in-place) using the current key in CBC mode.
145  */
146 void cipher_cbc_encrypt(BlockCipher *c, void *block);
147
148 /**
149  * Decrypt a block (in-place) using the current key in CBC mode.
150  */
151 void cipher_cbc_decrypt(BlockCipher *c, void *block);
152
153
154
155 /*********************************************************************************/
156 /* CTR mode                                                                      */
157 /*********************************************************************************/
158
159 /**
160  * Initialize CTR by setting the counter.
161  *
162  * \note the memory pointed by \a counter will be used and modified (incremented)
163  * by the CTR functions. It is caller's responsibility to keep it available until
164  * there is no more CTR work to do.
165  */
166 INLINE void cipher_ctr_begin(BlockCipher *c, void *counter)
167 {
168         c->buf = counter;
169 }
170
171 /**
172  * Encrypt a block (in-place) using the current key in CTR mode.
173  */
174 void cipher_ctr_encrypt(BlockCipher *c, void *block);
175
176 /**
177  * Decrypt a block (in-place) using the current key in CTR mode.
178  */
179 void cipher_ctr_decrypt(BlockCipher *c, void *block);
180
181 /**
182  * Generate the crypted stream block in CTR mode for the current
183  * counter, and then bump it.
184  *
185  * This function is basically the core CTR operation, without the final
186  * XOR pass with the plaintext or ciphertext. For normal CTR usage,
187  * you never need to call it.
188  */
189 void cipher_ctr_step(BlockCipher *c, void *block);
190
191
192 /*********************************************************************************/
193 /* OFB mode                                                                      */
194 /*********************************************************************************/
195
196 /**
197  * Initialize OFB by setting the IV.
198  *
199  * \note the memory pointed by \a iv will be used and modified by the OFB
200  * functions. It is caller's responsibility to keep it available until there is
201  * no more OFB work to do.
202  */
203 INLINE void cipher_ofb_begin(BlockCipher *c, void *iv)
204 {
205         c->buf = iv;
206 }
207
208 /**
209  * Encrypt a block (in-place) using the current key in OFB mode.
210  */
211 void cipher_ofb_encrypt(BlockCipher *c, void *block);
212
213 /**
214  * Decrypt a block (in-place) using the current key in OFB mode.
215  */
216 void cipher_ofb_decrypt(BlockCipher *c, void *block);
217
218
219 #endif /* SEC_CIPHER_H */