aes.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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_SELF_TEST
  40. #define polarssl_printf printf
  41. ///////add end
  42. #include <string.h>
  43. #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
  44. #include <basetsd.h>
  45. typedef UINT32 uint32_t;
  46. #else
  47. #include <inttypes.h>
  48. #endif
  49. /* padlock.c and aesni.c rely on these values! */
  50. #define AES_ENCRYPT 1
  51. #define AES_DECRYPT 0
  52. #define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
  53. #define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
  54. #if !defined(POLARSSL_AES_ALT)
  55. // Regular implementation
  56. //
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. /**
  61. * \brief AES context structure
  62. *
  63. * \note buf is able to hold 32 extra bytes, which can be used:
  64. * - for alignment purposes if VIA padlock is used, and/or
  65. * - to simplify key expansion in the 256-bit case by
  66. * generating an extra round key
  67. */
  68. typedef struct
  69. {
  70. int nr; /*!< number of rounds */
  71. uint32_t *rk; /*!< AES round keys */
  72. uint32_t buf[68]; /*!< unaligned data */
  73. }
  74. aes_context;
  75. /**
  76. * \brief Initialize AES context
  77. *
  78. * \param ctx AES context to be initialized
  79. */
  80. void aes_init( aes_context *ctx );
  81. /**
  82. * \brief Clear AES context
  83. *
  84. * \param ctx AES context to be cleared
  85. */
  86. void aes_free( aes_context *ctx );
  87. /**
  88. * \brief AES key schedule (encryption)
  89. *
  90. * \param ctx AES context to be initialized
  91. * \param key encryption key
  92. * \param keysize must be 128, 192 or 256
  93. *
  94. * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
  95. */
  96. int aes_setkey_enc( aes_context *ctx, const unsigned char *key,
  97. unsigned int keysize );
  98. /**
  99. * \brief AES key schedule (decryption)
  100. *
  101. * \param ctx AES context to be initialized
  102. * \param key decryption key
  103. * \param keysize must be 128, 192 or 256
  104. *
  105. * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
  106. */
  107. int aes_setkey_dec( aes_context *ctx, const unsigned char *key,
  108. unsigned int keysize );
  109. /**
  110. * \brief AES-ECB block encryption/decryption
  111. *
  112. * \param ctx AES context
  113. * \param mode AES_ENCRYPT or AES_DECRYPT
  114. * \param input 16-byte input block
  115. * \param output 16-byte output block
  116. *
  117. * \return 0 if successful
  118. */
  119. int aes_crypt_ecb( aes_context *ctx,
  120. int mode,
  121. const unsigned char input[16],
  122. unsigned char output[16] );
  123. #if defined(POLARSSL_CIPHER_MODE_CBC)
  124. /**
  125. * \brief AES-CBC buffer encryption/decryption
  126. * Length should be a multiple of the block
  127. * size (16 bytes)
  128. *
  129. * \param ctx AES context
  130. * \param mode AES_ENCRYPT or AES_DECRYPT
  131. * \param length length of the input data
  132. * \param iv initialization vector (updated after use)
  133. * \param input buffer holding the input data
  134. * \param output buffer holding the output data
  135. *
  136. * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
  137. */
  138. int aes_crypt_cbc( aes_context *ctx,
  139. int mode,
  140. size_t length,
  141. unsigned char iv[16],
  142. const unsigned char *input,
  143. unsigned char *output );
  144. #endif /* POLARSSL_CIPHER_MODE_CBC */
  145. #if defined(POLARSSL_CIPHER_MODE_CFB)
  146. /**
  147. * \brief AES-CFB128 buffer encryption/decryption.
  148. *
  149. * Note: Due to the nature of CFB you should use the same key schedule for
  150. * both encryption and decryption. So a context initialized with
  151. * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  152. *
  153. * \param ctx AES context
  154. * \param mode AES_ENCRYPT or AES_DECRYPT
  155. * \param length length of the input data
  156. * \param iv_off offset in IV (updated after use)
  157. * \param iv initialization vector (updated after use)
  158. * \param input buffer holding the input data
  159. * \param output buffer holding the output data
  160. *
  161. * \return 0 if successful
  162. */
  163. int aes_crypt_cfb128( aes_context *ctx,
  164. int mode,
  165. size_t length,
  166. size_t *iv_off,
  167. unsigned char iv[16],
  168. const unsigned char *input,
  169. unsigned char *output );
  170. /**
  171. * \brief AES-CFB8 buffer encryption/decryption.
  172. *
  173. * Note: Due to the nature of CFB you should use the same key schedule for
  174. * both encryption and decryption. So a context initialized with
  175. * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  176. *
  177. * \param ctx AES context
  178. * \param mode AES_ENCRYPT or AES_DECRYPT
  179. * \param length length of the input data
  180. * \param iv initialization vector (updated after use)
  181. * \param input buffer holding the input data
  182. * \param output buffer holding the output data
  183. *
  184. * \return 0 if successful
  185. */
  186. int aes_crypt_cfb8( aes_context *ctx,
  187. int mode,
  188. size_t length,
  189. unsigned char iv[16],
  190. const unsigned char *input,
  191. unsigned char *output );
  192. #endif /*POLARSSL_CIPHER_MODE_CFB */
  193. #if defined(POLARSSL_CIPHER_MODE_CTR)
  194. /**
  195. * \brief AES-CTR buffer encryption/decryption
  196. *
  197. * Warning: You have to keep the maximum use of your counter in mind!
  198. *
  199. * Note: Due to the nature of CTR you should use the same key schedule for
  200. * both encryption and decryption. So a context initialized with
  201. * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  202. *
  203. * \param ctx AES context
  204. * \param length The length of the data
  205. * \param nc_off The offset in the current stream_block (for resuming
  206. * within current cipher stream). The offset pointer to
  207. * should be 0 at the start of a stream.
  208. * \param nonce_counter The 128-bit nonce and counter.
  209. * \param stream_block The saved stream-block for resuming. Is overwritten
  210. * by the function.
  211. * \param input The input data stream
  212. * \param output The output data stream
  213. *
  214. * \return 0 if successful
  215. */
  216. int aes_crypt_ctr( aes_context *ctx,
  217. size_t length,
  218. size_t *nc_off,
  219. unsigned char nonce_counter[16],
  220. unsigned char stream_block[16],
  221. const unsigned char *input,
  222. unsigned char *output );
  223. #endif /* POLARSSL_CIPHER_MODE_CTR */
  224. #ifdef __cplusplus
  225. }
  226. #endif
  227. #else /* POLARSSL_AES_ALT */
  228. #include "aes_alt.h"
  229. #endif /* POLARSSL_AES_ALT */
  230. #ifdef __cplusplus
  231. extern "C" {
  232. #endif
  233. /**
  234. * \brief Checkup routine
  235. *
  236. * \return 0 if successful, or 1 if the test failed
  237. */
  238. int aes_self_test( int verbose );
  239. #ifdef __cplusplus
  240. }
  241. #endif
  242. #endif /* aes.h */