aes.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #ifndef _AES_H_
  2. #define _AES_H_
  3. /*
  4. * Copyright (c) 1998-2008, Brian Gladman, Worcester, UK.
  5. *
  6. * Copyright 2012, Ole Reinhardt <ole.reinhardt@embedded-it.de>
  7. *
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the copyright holders nor the names of
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  33. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * For additional information see http://www.ethernut.de/
  37. */
  38. /*
  39. * \file include/gorp/aes.h
  40. *
  41. * \brief Brian Gladmans byte oriented implementation of the AES crypto algorithm
  42. *
  43. * Issue 09/09/2006
  44. *
  45. * This is an AES implementation that uses only 8-bit byte operations on the
  46. * cipher state (there are options to use 32-bit types if available).
  47. *
  48. * \verbatim
  49. * $Id$
  50. * \endverbatim
  51. */
  52. #include <cfg/aes.h>
  53. #define N_ROW 4
  54. #define N_COL 4
  55. #define N_BLOCK (N_ROW * N_COL)
  56. #define N_MAX_ROUNDS 14
  57. typedef uint8_t uint_8t;
  58. typedef uint_8t return_type;
  59. /* Warning: The key length for 256 bit keys overflows a byte
  60. (see comment below)
  61. */
  62. typedef struct
  63. { uint_8t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK];
  64. uint_8t rnd;
  65. } aes_context;
  66. /* The following calls are for a precomputed key schedule
  67. NOTE: If the length_type used for the key length is an
  68. unsigned 8-bit character, a key length of 256 bits must
  69. be entered as a length in bytes (valid inputs are hence
  70. 128, 192, 16, 24 and 32).
  71. */
  72. #if defined( AES_ENC_PREKEYED ) || defined( AES_DEC_PREKEYED )
  73. return_type aes_set_key( const uint8_t key[],
  74. int keylen,
  75. aes_context ctx[1] );
  76. #endif
  77. #if defined( AES_ENC_PREKEYED )
  78. return_type aes_encrypt( const uint8_t in[N_BLOCK],
  79. uint8_t out[N_BLOCK],
  80. const aes_context ctx[1] );
  81. return_type aes_cbc_encrypt( const uint8_t *in,
  82. uint8_t *out,
  83. int n_block,
  84. uint8_t iv[N_BLOCK],
  85. const aes_context ctx[1] );
  86. #endif
  87. #if defined( AES_DEC_PREKEYED )
  88. return_type aes_decrypt( const uint8_t in[N_BLOCK],
  89. uint8_t out[N_BLOCK],
  90. const aes_context ctx[1] );
  91. return_type aes_cbc_decrypt( const uint8_t *in,
  92. uint8_t *out,
  93. int n_block,
  94. uint8_t iv[N_BLOCK],
  95. const aes_context ctx[1] );
  96. #endif
  97. /* The following calls are for 'on the fly' keying. In this case the
  98. encryption and decryption keys are different.
  99. The encryption subroutines take a key in an array of bytes in
  100. key[L] where L is 16, 24 or 32 bytes for key lengths of 128,
  101. 192, and 256 bits respectively. They then encrypts the input
  102. data, in[] with this key and put the reult in the output array
  103. out[]. In addition, the second key array, o_key[L], is used
  104. to output the key that is needed by the decryption subroutine
  105. to reverse the encryption operation. The two key arrays can
  106. be the same array but in this case the original key will be
  107. overwritten.
  108. In the same way, the decryption subroutines output keys that
  109. can be used to reverse their effect when used for encryption.
  110. Only 128 and 256 bit keys are supported in these 'on the fly'
  111. modes.
  112. */
  113. #if defined( AES_ENC_128_OTFK )
  114. void aes_encrypt_128( const uint8_t in[N_BLOCK],
  115. uint8_t out[N_BLOCK],
  116. const uint8_t key[N_BLOCK],
  117. uint_8t o_key[N_BLOCK] );
  118. #endif
  119. #if defined( AES_DEC_128_OTFK )
  120. void aes_decrypt_128( const uint8_t in[N_BLOCK],
  121. uint8_t out[N_BLOCK],
  122. const uint8_t key[N_BLOCK],
  123. uint8_t o_key[N_BLOCK] );
  124. #endif
  125. #if defined( AES_ENC_256_OTFK )
  126. void aes_encrypt_256( const uint8_t in[N_BLOCK],
  127. uint8_t out[N_BLOCK],
  128. const uint8_t key[2 * N_BLOCK],
  129. uint8_t o_key[2 * N_BLOCK] );
  130. #endif
  131. #if defined( AES_DEC_256_OTFK )
  132. void aes_decrypt_256( const uint8_t in[N_BLOCK],
  133. uint8_t out[N_BLOCK],
  134. const uint8_t key[2 * N_BLOCK],
  135. uint8_t o_key[2 * N_BLOCK] );
  136. #endif
  137. #endif