/bertos/sec/cipher.h

https://github.com/batt/StratoSpera · C Header · 219 lines · 61 code · 31 blank · 127 comment · 0 complexity · 6b9253db8e6cf91f9a9c0a720e2e8ee3 MD5 · raw file

  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. #ifndef SEC_CIPHER_H
  38. #define SEC_CIPHER_H
  39. #include <cfg/compiler.h>
  40. #include <cfg/debug.h>
  41. typedef struct BlockCipher
  42. {
  43. void (*set_key)(struct BlockCipher *c, const void *key, size_t len);
  44. void (*enc_block)(struct BlockCipher *c, void *block);
  45. void (*dec_block)(struct BlockCipher *c, void *block);
  46. void *buf;
  47. uint8_t key_len;
  48. uint8_t block_len;
  49. } BlockCipher;
  50. /**
  51. * Return the key length (in bytes).
  52. *
  53. * In case of ciphers that allow a variabile key size with a fixed state
  54. * (eg: Blowfish), this returns the preferred key length.
  55. */
  56. INLINE size_t cipher_key_len(BlockCipher *c)
  57. {
  58. return c->key_len;
  59. }
  60. /**
  61. * Return the block length (in bytes)
  62. */
  63. INLINE size_t cipher_block_len(BlockCipher *c)
  64. {
  65. return c->block_len;
  66. }
  67. /**
  68. * Set the current key used by the cipher.
  69. *
  70. * \note the buffer pointed by \a key is not modified and it is
  71. * not needed anymore after this call returns. Its lenght must match
  72. * the value returned by \a cipher_key_len().
  73. */
  74. INLINE void cipher_set_key(BlockCipher *c, const void *key)
  75. {
  76. ASSERT(c->set_key);
  77. c->set_key(c, key, c->key_len);
  78. }
  79. /**
  80. * Set the current key (of variable size) used by the cipher.
  81. *
  82. * This function is useful for ciphers that allow a variable size for the key
  83. * (even with a fixed state). For all the other ciphers, the length must
  84. * match the value returned by \a cipher_key_len().
  85. *
  86. * \note the buffer pointed by \a key is not modified and it is
  87. * not needed anymore after this call returns.
  88. */
  89. INLINE void cipher_set_vkey(BlockCipher *c, const void *key, size_t len)
  90. {
  91. ASSERT(c->set_key);
  92. c->set_key(c, key, len);
  93. }
  94. /*********************************************************************************/
  95. /* ECB mode */
  96. /*********************************************************************************/
  97. /**
  98. * Encrypt a block (in-place) using the current key in ECB mode.
  99. */
  100. INLINE void cipher_ecb_encrypt(BlockCipher *c, void *block)
  101. {
  102. ASSERT(c->enc_block);
  103. c->enc_block(c, block);
  104. }
  105. /**
  106. * Decrypt a block (in-place) using the current key in ECB mode.
  107. */
  108. INLINE void cipher_ecb_decrypt(BlockCipher *c, void *block)
  109. {
  110. ASSERT(c->dec_block);
  111. c->dec_block(c, block);
  112. }
  113. /*********************************************************************************/
  114. /* CBC mode */
  115. /*********************************************************************************/
  116. /**
  117. * Initialize CBC by setting the IV.
  118. *
  119. * \note the memory pointed by \a iv will be used and modified by the CBC
  120. * functions. It is caller's responsibility to keep it available until there is
  121. * no more CBC work to do.
  122. */
  123. INLINE void cipher_cbc_begin(BlockCipher *c, void *iv)
  124. {
  125. c->buf = iv;
  126. }
  127. /**
  128. * Encrypt a block (in-place) using the current key in CBC mode.
  129. */
  130. void cipher_cbc_encrypt(BlockCipher *c, void *block);
  131. /**
  132. * Decrypt a block (in-place) using the current key in CBC mode.
  133. */
  134. void cipher_cbc_decrypt(BlockCipher *c, void *block);
  135. /*********************************************************************************/
  136. /* CTR mode */
  137. /*********************************************************************************/
  138. /**
  139. * Initialize CTR by setting the counter.
  140. *
  141. * \note the memory pointed by \a counter will be used and modified (incremented)
  142. * by the CTR functions. It is caller's responsibility to keep it available until
  143. * there is no more CTR work to do.
  144. */
  145. INLINE void cipher_ctr_begin(BlockCipher *c, void *counter)
  146. {
  147. c->buf = counter;
  148. }
  149. /**
  150. * Encrypt a block (in-place) using the current key in CTR mode.
  151. */
  152. void cipher_ctr_encrypt(BlockCipher *c, void *block);
  153. /**
  154. * Decrypt a block (in-place) using the current key in CTR mode.
  155. */
  156. void cipher_ctr_decrypt(BlockCipher *c, void *block);
  157. /**
  158. * Generate the crypted stream block in CTR mode for the current
  159. * counter, and then bump it.
  160. *
  161. * This function is basically the core CTR operation, without the final
  162. * XOR pass with the plaintext or ciphertext. For normal CTR usage,
  163. * you never need to call it.
  164. */
  165. void cipher_ctr_step(BlockCipher *c, void *block);
  166. /*********************************************************************************/
  167. /* OFB mode */
  168. /*********************************************************************************/
  169. /**
  170. * Initialize OFB by setting the IV.
  171. *
  172. * \note the memory pointed by \a iv will be used and modified by the CBC
  173. * functions. It is caller's responsibility to keep it available until there is
  174. * no more OFB work to do.
  175. */
  176. INLINE void cipher_ofb_begin(BlockCipher *c, void *iv)
  177. {
  178. c->buf = iv;
  179. }
  180. /**
  181. * Encrypt a block (in-place) using the current key in OFB mode.
  182. */
  183. void cipher_ofb_encrypt(BlockCipher *c, void *block);
  184. /**
  185. * Decrypt a block (in-place) using the current key in OFB mode.
  186. */
  187. void cipher_ofb_decrypt(BlockCipher *c, void *block);
  188. #endif /* SEC_CIPHER_H */