aes.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * \file aes.h
  3. *
  4. * \brief AES block cipher
  5. *
  6. * Copyright (C) 2006-2014, Brainspark B.V.
  7. *
  8. * This file is part of PolarSSL (http://www.polarssl.org)
  9. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  10. *
  11. * All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26. */
  27. #ifndef POLARSSL_AES_H
  28. #define POLARSSL_AES_H
  29. /*
  30. #if !defined(POLARSSL_CONFIG_FILE)
  31. #include "config.h"
  32. #else
  33. #include POLARSSL_CONFIG_FILE
  34. #endif
  35. */
  36. ////////modification begin
  37. #define POLARSSL_AES_ROM_TABLES
  38. #define POLARSSL_CIPHER_MODE_CBC
  39. #define POLARSSL_CIPHER_MODE_CFB
  40. //#define POLARSSL_SELF_TEST
  41. #define polarssl_printf printf
  42. ///////add end
  43. #include <string.h>
  44. #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
  45. #include <basetsd.h>
  46. typedef UINT32 uint32_t;
  47. #else
  48. #include <inttypes.h>
  49. #endif
  50. /* padlock.c and aesni.c rely on these values! */
  51. #define AES_ENCRYPT 1
  52. #define AES_DECRYPT 0
  53. #define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
  54. #define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
  55. #if !defined(POLARSSL_AES_ALT)
  56. // Regular implementation
  57. //
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. /**
  62. * \brief AES context structure
  63. *
  64. * \note buf is able to hold 32 extra bytes, which can be used:
  65. * - for alignment purposes if VIA padlock is used, and/or
  66. * - to simplify key expansion in the 256-bit case by
  67. * generating an extra round key
  68. */
  69. typedef struct
  70. {
  71. int nr; /*!< number of rounds */
  72. uint32_t *rk; /*!< AES round keys */
  73. uint32_t buf[68]; /*!< unaligned data */
  74. }
  75. aes_context;
  76. /**
  77. * \brief Initialize AES context
  78. *
  79. * \param ctx AES context to be initialized
  80. */
  81. void aes_init( aes_context *ctx );
  82. /**
  83. * \brief Clear AES context
  84. *
  85. * \param ctx AES context to be cleared
  86. */
  87. void aes_free( aes_context *ctx );
  88. /**
  89. * \brief AES key schedule (encryption)
  90. *
  91. * \param ctx AES context to be initialized
  92. * \param key encryption key
  93. * \param keysize must be 128, 192 or 256
  94. *
  95. * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
  96. */
  97. int aes_setkey_enc( aes_context *ctx, const unsigned char *key,
  98. unsigned int keysize );
  99. /**
  100. * \brief AES key schedule (decryption)
  101. *
  102. * \param ctx AES context to be initialized
  103. * \param key decryption key
  104. * \param keysize must be 128, 192 or 256
  105. *
  106. * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
  107. */
  108. int aes_setkey_dec( aes_context *ctx, const unsigned char *key,
  109. unsigned int keysize );
  110. /**
  111. * \brief AES-ECB block encryption/decryption
  112. *
  113. * \param ctx AES context
  114. * \param mode AES_ENCRYPT or AES_DECRYPT
  115. * \param input 16-byte input block
  116. * \param output 16-byte output block
  117. *
  118. * \return 0 if successful
  119. */
  120. int aes_crypt_ecb( aes_context *ctx,
  121. int mode,
  122. const unsigned char input[16],
  123. unsigned char output[16] );
  124. #if defined(POLARSSL_CIPHER_MODE_CBC)
  125. /**
  126. * \brief AES-CBC buffer encryption/decryption
  127. * Length should be a multiple of the block
  128. * size (16 bytes)
  129. *
  130. * \param ctx AES context
  131. * \param mode AES_ENCRYPT or AES_DECRYPT
  132. * \param length length of the input data
  133. * \param iv initialization vector (updated after use)
  134. * \param input buffer holding the input data
  135. * \param output buffer holding the output data
  136. *
  137. * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
  138. */
  139. int aes_crypt_cbc( aes_context *ctx,
  140. int mode,
  141. size_t length,
  142. unsigned char iv[16],
  143. const unsigned char *input,
  144. unsigned char *output );
  145. #endif /* POLARSSL_CIPHER_MODE_CBC */
  146. #if defined(POLARSSL_CIPHER_MODE_CFB)
  147. /**
  148. * \brief AES-CFB128 buffer encryption/decryption.
  149. *
  150. * Note: Due to the nature of CFB you should use the same key schedule for
  151. * both encryption and decryption. So a context initialized with
  152. * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  153. *
  154. * \param ctx AES context
  155. * \param mode AES_ENCRYPT or AES_DECRYPT
  156. * \param length length of the input data
  157. * \param iv_off offset in IV (updated after use)
  158. * \param iv initialization vector (updated after use)
  159. * \param input buffer holding the input data
  160. * \param output buffer holding the output data
  161. *
  162. * \return 0 if successful
  163. */
  164. int aes_crypt_cfb128( aes_context *ctx,
  165. int mode,
  166. size_t length,
  167. size_t *iv_off,
  168. unsigned char iv[16],
  169. const unsigned char *input,
  170. unsigned char *output );
  171. /**
  172. * \brief AES-CFB8 buffer encryption/decryption.
  173. *
  174. * Note: Due to the nature of CFB you should use the same key schedule for
  175. * both encryption and decryption. So a context initialized with
  176. * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  177. *
  178. * \param ctx AES context
  179. * \param mode AES_ENCRYPT or AES_DECRYPT
  180. * \param length length of the input data
  181. * \param iv initialization vector (updated after use)
  182. * \param input buffer holding the input data
  183. * \param output buffer holding the output data
  184. *
  185. * \return 0 if successful
  186. */
  187. int aes_crypt_cfb8( aes_context *ctx,
  188. int mode,
  189. size_t length,
  190. unsigned char iv[16],
  191. const unsigned char *input,
  192. unsigned char *output );
  193. #endif /*POLARSSL_CIPHER_MODE_CFB */
  194. #if defined(POLARSSL_CIPHER_MODE_CTR)
  195. /**
  196. * \brief AES-CTR buffer encryption/decryption
  197. *
  198. * Warning: You have to keep the maximum use of your counter in mind!
  199. *
  200. * Note: Due to the nature of CTR you should use the same key schedule for
  201. * both encryption and decryption. So a context initialized with
  202. * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  203. *
  204. * \param ctx AES context
  205. * \param length The length of the data
  206. * \param nc_off The offset in the current stream_block (for resuming
  207. * within current cipher stream). The offset pointer to
  208. * should be 0 at the start of a stream.
  209. * \param nonce_counter The 128-bit nonce and counter.
  210. * \param stream_block The saved stream-block for resuming. Is overwritten
  211. * by the function.
  212. * \param input The input data stream
  213. * \param output The output data stream
  214. *
  215. * \return 0 if successful
  216. */
  217. int aes_crypt_ctr( aes_context *ctx,
  218. size_t length,
  219. size_t *nc_off,
  220. unsigned char nonce_counter[16],
  221. unsigned char stream_block[16],
  222. const unsigned char *input,
  223. unsigned char *output );
  224. #endif /* POLARSSL_CIPHER_MODE_CTR */
  225. #ifdef __cplusplus
  226. }
  227. #endif
  228. #else /* POLARSSL_AES_ALT */
  229. #include "aes_alt.h"
  230. #endif /* POLARSSL_AES_ALT */
  231. #ifdef __cplusplus
  232. extern "C" {
  233. #endif
  234. /**
  235. * \brief Checkup routine
  236. *
  237. * \return 0 if successful, or 1 if the test failed
  238. */
  239. int aes_self_test( int verbose );
  240. #ifdef __cplusplus
  241. }
  242. #endif
  243. #endif /* aes.h */