f0f37a36e600fce33169ebdaec7075a11944c637
[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);
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 INLINE size_t cipher_key_len(BlockCipher *c)
60 {
61         return c->key_len;
62 }
63
64 /**
65  * Return the block length (in bytes)
66  */
67 INLINE size_t cipher_block_len(BlockCipher *c)
68 {
69         return c->block_len;
70 }
71
72 /**
73  * Set the current key used by the cipher.
74  *
75  * \note the buffer pointed by \a key is not modified and it is
76  * not needed anymore after this call returns.
77  */
78 INLINE void cipher_set_key(BlockCipher *c, const void *key)
79 {
80         ASSERT(c->set_key);
81         c->set_key(c, key);
82 }
83
84 /*********************************************************************************/
85 /* ECB mode                                                                      */
86 /*********************************************************************************/
87
88 /**
89  * Encrypt a block (in-place) using the current key in ECB mode.
90  */
91 INLINE void cipher_ecb_encrypt(BlockCipher *c, void *block)
92 {
93         ASSERT(c->enc_block);
94         c->enc_block(c, block);
95 }
96
97 /**
98  * Decrypt a block (in-place) using the current key in ECB mode.
99  */
100 INLINE void cipher_ecb_decrypt(BlockCipher *c, void *block)
101 {
102         ASSERT(c->dec_block);
103         c->dec_block(c, block);
104 }
105
106
107 /*********************************************************************************/
108 /* CBC mode                                                                      */
109 /*********************************************************************************/
110
111 /**
112  * Initialize CBC by setting the IV.
113  *
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.
117  */
118 INLINE void cipher_cbc_begin(BlockCipher *c, void *iv)
119 {
120         c->buf = iv;
121 }
122
123 /**
124  * Encrypt a block (in-place) using the current key in CBC mode.
125  */
126 void cipher_cbc_encrypt(BlockCipher *c, void *block);
127
128 /**
129  * Decrypt a block (in-place) using the current key in CBC mode.
130  */
131 void cipher_cbc_decrypt(BlockCipher *c, void *block);
132
133
134
135 /*********************************************************************************/
136 /* CTR mode                                                                      */
137 /*********************************************************************************/
138
139 /**
140  * Initialize CTR by setting the counter.
141  *
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.
145  */
146 INLINE void cipher_ctr_begin(BlockCipher *c, void *counter)
147 {
148         c->buf = counter;
149 }
150
151 /**
152  * Encrypt a block (in-place) using the current key in CBC mode.
153  */
154 void cipher_ctr_encrypt(BlockCipher *c, void *block);
155
156 /**
157  * Decrypt a block (in-place) using the current key in CBC mode.
158  */
159 void cipher_ctr_decrypt(BlockCipher *c, void *block);
160
161 #endif /* SEC_CIPHER_H */