aes.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * \file aes.h
  3. *
  4. * \brief AES block cipher
  5. *
  6. * Copyright (C) 2006-2010, 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. /* Note: This file has been modified by ST's MCD Application Team, to support
  28. the hardware crypto engine embedded in STM32F417xx */
  29. #ifndef POLARSSL_AES_H
  30. #define POLARSSL_AES_H
  31. #include <string.h>
  32. #define AES_ENCRYPT 1
  33. #define AES_DECRYPT 0
  34. #define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
  35. #define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
  36. /**
  37. * \brief AES context structure
  38. */
  39. typedef struct
  40. {
  41. int nr; /*!< number of rounds */
  42. unsigned long *rk; /*!< AES round keys */
  43. unsigned long buf[68]; /*!< unaligned data */
  44. #ifdef USE_STM32F4XX_HW_CRYPTO
  45. unsigned char aes_enc_key[32]; /* Encryption key */
  46. unsigned char aes_dec_key[32]; /* Decryption key */
  47. #endif /* USE_STM32F4XX_HW_CRYPTO */
  48. }aes_context;
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. /**
  53. * \brief AES key schedule (encryption)
  54. *
  55. * \param ctx AES context to be initialized
  56. * \param key encryption key
  57. * \param keysize must be 128, 192 or 256
  58. *
  59. * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
  60. */
  61. int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int keysize );
  62. /**
  63. * \brief AES key schedule (decryption)
  64. *
  65. * \param ctx AES context to be initialized
  66. * \param key decryption key
  67. * \param keysize must be 128, 192 or 256
  68. *
  69. * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
  70. */
  71. int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int keysize );
  72. /**
  73. * \brief AES-ECB block encryption/decryption
  74. *
  75. * \param ctx AES context
  76. * \param mode AES_ENCRYPT or AES_DECRYPT
  77. * \param input 16-byte input block
  78. * \param output 16-byte output block
  79. *
  80. * \return 0 if successful
  81. */
  82. int aes_crypt_ecb( aes_context *ctx,
  83. int mode,
  84. const unsigned char input[16],
  85. unsigned char output[16] );
  86. /**
  87. * \brief AES-CBC buffer encryption/decryption
  88. * Length should be a multiple of the block
  89. * size (16 bytes)
  90. *
  91. * \param ctx AES context
  92. * \param mode AES_ENCRYPT or AES_DECRYPT
  93. * \param length length of the input data
  94. * \param iv initialization vector (updated after use)
  95. * \param input buffer holding the input data
  96. * \param output buffer holding the output data
  97. *
  98. * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
  99. */
  100. int aes_crypt_cbc( aes_context *ctx,
  101. int mode,
  102. size_t length,
  103. unsigned char iv[16],
  104. const unsigned char *input,
  105. unsigned char *output );
  106. /**
  107. * \brief AES-CFB128 buffer encryption/decryption.
  108. *
  109. * \param ctx AES context
  110. * \param mode AES_ENCRYPT or AES_DECRYPT
  111. * \param length length of the input data
  112. * \param iv_off offset in IV (updated after use)
  113. * \param iv initialization vector (updated after use)
  114. * \param input buffer holding the input data
  115. * \param output buffer holding the output data
  116. *
  117. * \return 0 if successful
  118. */
  119. int aes_crypt_cfb128( aes_context *ctx,
  120. int mode,
  121. size_t length,
  122. size_t *iv_off,
  123. unsigned char iv[16],
  124. const unsigned char *input,
  125. unsigned char *output );
  126. /*
  127. * \brief AES-CTR buffer encryption/decryption
  128. *
  129. * Warning: You have to keep the maximum use of your counter in mind!
  130. *
  131. * \param length The length of the data
  132. * \param nc_off The offset in the current stream_block (for resuming
  133. * within current cipher stream). The offset pointer to
  134. * should be 0 at the start of a stream.
  135. * \param nonce_counter The 128-bit nonce and counter.
  136. * \param stream_block The saved stream-block for resuming. Is overwritten
  137. * by the function.
  138. * \param input The input data stream
  139. * \param output The output data stream
  140. *
  141. * \return 0 if successful
  142. */
  143. int aes_crypt_ctr( aes_context *ctx,
  144. size_t length,
  145. size_t *nc_off,
  146. unsigned char nonce_counter[16],
  147. unsigned char stream_block[16],
  148. const unsigned char *input,
  149. unsigned char *output );
  150. /**
  151. * \brief Checkup routine
  152. *
  153. * \return 0 if successful, or 1 if the test failed
  154. */
  155. int aes_self_test( int verbose );
  156. #ifdef __cplusplus
  157. }
  158. #endif
  159. #endif /* aes.h */