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);
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 INLINE size_t cipher_key_len(BlockCipher *c)
65 * Return the block length (in bytes)
67 INLINE size_t cipher_block_len(BlockCipher *c)
73 * Set the current key used by the cipher.
75 * \note the buffer pointed by \a key is not modified and it is
76 * not needed anymore after this call returns.
78 INLINE void cipher_set_key(BlockCipher *c, const void *key)
84 /*********************************************************************************/
86 /*********************************************************************************/
89 * Encrypt a block (in-place) using the current key in ECB mode.
91 INLINE void cipher_ecb_encrypt(BlockCipher *c, void *block)
94 c->enc_block(c, block);
98 * Decrypt a block (in-place) using the current key in ECB mode.
100 INLINE void cipher_ecb_decrypt(BlockCipher *c, void *block)
102 ASSERT(c->dec_block);
103 c->dec_block(c, block);
107 /*********************************************************************************/
109 /*********************************************************************************/
112 * Initialize CBC by setting the IV.
114 * \note the memory pointed by \a iv will be used and modified by the CBC
115 * functions. It is caller's responsibility to keep it available until there is
116 * no more CBC work to do.
118 INLINE void cipher_cbc_begin(BlockCipher *c, void *iv)
124 * Encrypt a block (in-place) using the current key in CBC mode.
126 void cipher_cbc_encrypt(BlockCipher *c, void *block);
129 * Decrypt a block (in-place) using the current key in CBC mode.
131 void cipher_cbc_decrypt(BlockCipher *c, void *block);
135 /*********************************************************************************/
137 /*********************************************************************************/
140 * Initialize CTR by setting the counter.
142 * \note the memory pointed by \a counter will be used and modified (incremented)
143 * by the CTR functions. It is caller's responsibility to keep it available until
144 * there is no more CTR work to do.
146 INLINE void cipher_ctr_begin(BlockCipher *c, void *counter)
152 * Encrypt a block (in-place) using the current key in CBC mode.
154 void cipher_ctr_encrypt(BlockCipher *c, void *block);
157 * Decrypt a block (in-place) using the current key in CBC mode.
159 void cipher_ctr_decrypt(BlockCipher *c, void *block);
161 #endif /* SEC_CIPHER_H */