4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief Generic interface for symmetric block ciphers.
34 * \author Giovanni Bajo <rasky@develer.com>
41 #include <cfg/compiler.h>
42 #include <cfg/debug.h>
44 typedef struct BlockCipher
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);
57 * Return the key length (in bytes).
59 * In case of ciphers that allow a variabile key size with a fixed state
60 * (eg: Blowfish), this returns the preferred key length.
62 INLINE size_t cipher_key_len(BlockCipher *c)
68 * Return the block length (in bytes)
70 INLINE size_t cipher_block_len(BlockCipher *c)
76 * Set the current key used by the cipher.
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().
82 INLINE void cipher_set_key(BlockCipher *c, const void *key)
85 c->set_key(c, key, c->key_len);
89 * Set the current key (of variable size) used by the cipher.
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().
95 * \note the buffer pointed by \a key is not modified and it is
96 * not needed anymore after this call returns.
98 INLINE void cipher_set_vkey(BlockCipher *c, const void *key, size_t len)
101 c->set_key(c, key, len);
104 /*********************************************************************************/
106 /*********************************************************************************/
109 * Encrypt a block (in-place) using the current key in ECB mode.
111 INLINE void cipher_ecb_encrypt(BlockCipher *c, void *block)
113 ASSERT(c->enc_block);
114 c->enc_block(c, block);
118 * Decrypt a block (in-place) using the current key in ECB mode.
120 INLINE void cipher_ecb_decrypt(BlockCipher *c, void *block)
122 ASSERT(c->dec_block);
123 c->dec_block(c, block);
127 /*********************************************************************************/
129 /*********************************************************************************/
132 * Initialize CBC by setting the IV.
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.
138 INLINE void cipher_cbc_begin(BlockCipher *c, void *iv)
144 * Encrypt a block (in-place) using the current key in CBC mode.
146 void cipher_cbc_encrypt(BlockCipher *c, void *block);
149 * Decrypt a block (in-place) using the current key in CBC mode.
151 void cipher_cbc_decrypt(BlockCipher *c, void *block);
155 /*********************************************************************************/
157 /*********************************************************************************/
160 * Initialize CTR by setting the counter.
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.
166 INLINE void cipher_ctr_begin(BlockCipher *c, void *counter)
172 * Encrypt a block (in-place) using the current key in CTR mode.
174 void cipher_ctr_encrypt(BlockCipher *c, void *block);
177 * Decrypt a block (in-place) using the current key in CTR mode.
179 void cipher_ctr_decrypt(BlockCipher *c, void *block);
182 * Generate the crypted stream block in CTR mode for the current
183 * counter, and then bump it.
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.
189 void cipher_ctr_step(BlockCipher *c, void *block);
192 /*********************************************************************************/
194 /*********************************************************************************/
197 * Initialize OFB by setting the IV.
199 * \note the memory pointed by \a iv will be used and modified by the CBC
200 * functions. It is caller's responsibility to keep it available until there is
201 * no more OFB work to do.
203 INLINE void cipher_ofb_begin(BlockCipher *c, void *iv)
209 * Encrypt a block (in-place) using the current key in OFB mode.
211 void cipher_ofb_encrypt(BlockCipher *c, void *block);
214 * Decrypt a block (in-place) using the current key in OFB mode.
216 void cipher_ofb_decrypt(BlockCipher *c, void *block);
219 #endif /* SEC_CIPHER_H */