tiny_aes.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * \file aes.h
  3. *
  4. * Based on TropicSSL: Copyright (C) 2017 Shanghai Real-Thread Technology Co., Ltd
  5. *
  6. * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
  7. *
  8. * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * * Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * * Neither the names of PolarSSL or XySSL nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  31. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #ifndef TINY_CRYPT_AES_H__
  38. #define TINY_CRYPT_AES_H__
  39. #define AES_ENCRYPT 1
  40. #define AES_DECRYPT 0
  41. /**
  42. * \brief AES context structure
  43. */
  44. typedef struct {
  45. int nr; /*!< number of rounds */
  46. unsigned long *rk; /*!< AES round keys */
  47. unsigned long buf[68]; /*!< unaligned data */
  48. } tiny_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. void tiny_aes_setkey_enc(tiny_aes_context * ctx, unsigned char *key, int keysize);
  60. /**
  61. * \brief AES key schedule (decryption)
  62. *
  63. * \param ctx AES context to be initialized
  64. * \param key decryption key
  65. * \param keysize must be 128, 192 or 256
  66. */
  67. void tiny_aes_setkey_dec(tiny_aes_context * ctx, unsigned char *key, int keysize);
  68. /**
  69. * \brief AES-ECB block encryption/decryption
  70. *
  71. * \param ctx AES context
  72. * \param mode AES_ENCRYPT or AES_DECRYPT
  73. * \param input 16-byte input block
  74. * \param output 16-byte output block
  75. */
  76. void tiny_aes_crypt_ecb(tiny_aes_context * ctx,
  77. int mode,
  78. unsigned char input[16], unsigned char output[16]);
  79. /**
  80. * \brief AES-CBC buffer encryption/decryption
  81. *
  82. * \param ctx AES context
  83. * \param mode AES_ENCRYPT or AES_DECRYPT
  84. * \param length length of the input data
  85. * \param iv initialization vector (updated after use)
  86. * \param input buffer holding the input data
  87. * \param output buffer holding the output data
  88. */
  89. void tiny_aes_crypt_cbc(tiny_aes_context * ctx,
  90. int mode,
  91. int length,
  92. unsigned char iv[16],
  93. unsigned char *input, unsigned char *output);
  94. /**
  95. * \brief AES-CFB128 buffer encryption/decryption
  96. *
  97. * \param ctx AES context
  98. * \param mode AES_ENCRYPT or AES_DECRYPT
  99. * \param length length of the input data
  100. * \param iv_off offset in IV (updated after use)
  101. * \param iv initialization vector (updated after use)
  102. * \param input buffer holding the input data
  103. * \param output buffer holding the output data
  104. */
  105. void tiny_aes_crypt_cfb128(tiny_aes_context * ctx,
  106. int mode,
  107. int length,
  108. int *iv_off,
  109. unsigned char iv[16],
  110. unsigned char *input, unsigned char *output);
  111. #ifdef __cplusplus
  112. }
  113. #endif
  114. #endif /* aes.h */