選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

axtls_aes.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <byteswap.h>
  21. #include <gpxe/crypto.h>
  22. #include <gpxe/cbc.h>
  23. #include <gpxe/aes.h>
  24. #include "crypto/axtls/crypto.h"
  25. /** @file
  26. *
  27. * AES algorithm
  28. *
  29. */
  30. /** Basic AES blocksize */
  31. #define AES_BLOCKSIZE 16
  32. /****************************************************************************
  33. *
  34. * Basic AES algorithm (independent of mode of operation)
  35. *
  36. ****************************************************************************
  37. */
  38. /** AES context */
  39. struct aes_context {
  40. /** AES context for AXTLS */
  41. AES_CTX axtls_ctx;
  42. /** Cipher is being used for decrypting */
  43. int decrypting;
  44. };
  45. /**
  46. * Set key
  47. *
  48. * @v ctx Context
  49. * @v key Key
  50. * @v keylen Key length
  51. * @ret rc Return status code
  52. */
  53. static int aes_setkey ( void *ctx, const void *key, size_t keylen ) {
  54. struct aes_context *aes_ctx = ctx;
  55. AES_MODE mode;
  56. void *iv;
  57. switch ( keylen ) {
  58. case ( 128 / 8 ):
  59. mode = AES_MODE_128;
  60. break;
  61. case ( 256 / 8 ):
  62. mode = AES_MODE_256;
  63. break;
  64. default:
  65. return -EINVAL;
  66. }
  67. /* IV is not a relevant concept at this stage; use a dummy
  68. * value that will have no side-effects.
  69. */
  70. iv = &aes_ctx->axtls_ctx.iv;
  71. AES_set_key ( &aes_ctx->axtls_ctx, key, iv, mode );
  72. aes_ctx->decrypting = 0;
  73. return 0;
  74. }
  75. /**
  76. * Set initialisation vector
  77. *
  78. * @v ctx Context
  79. * @v iv Initialisation vector
  80. */
  81. static void aes_setiv ( void *ctx __unused, const void *iv __unused ) {
  82. /* Nothing to do */
  83. }
  84. /**
  85. * Call AXTLS' AES_encrypt() or AES_decrypt() functions
  86. *
  87. * @v axtls_ctx AXTLS AES context
  88. * @v src Data to process
  89. * @v dst Buffer for output
  90. * @v func AXTLS AES function to call
  91. */
  92. static void aes_call_axtls ( AES_CTX *axtls_ctx, const void *src, void *dst,
  93. void ( * func ) ( const AES_CTX *axtls_ctx,
  94. uint32_t *data ) ){
  95. const uint32_t *srcl = src;
  96. uint32_t *dstl = dst;
  97. unsigned int i;
  98. /* AXTLS' AES_encrypt() and AES_decrypt() functions both
  99. * expect to deal with an array of four dwords in host-endian
  100. * order.
  101. */
  102. for ( i = 0 ; i < 4 ; i++ )
  103. dstl[i] = ntohl ( srcl[i] );
  104. func ( axtls_ctx, dstl );
  105. for ( i = 0 ; i < 4 ; i++ )
  106. dstl[i] = htonl ( dstl[i] );
  107. }
  108. /**
  109. * Encrypt data
  110. *
  111. * @v ctx Context
  112. * @v src Data to encrypt
  113. * @v dst Buffer for encrypted data
  114. * @v len Length of data
  115. */
  116. static void aes_encrypt ( void *ctx, const void *src, void *dst,
  117. size_t len ) {
  118. struct aes_context *aes_ctx = ctx;
  119. assert ( len == AES_BLOCKSIZE );
  120. if ( aes_ctx->decrypting )
  121. assert ( 0 );
  122. aes_call_axtls ( &aes_ctx->axtls_ctx, src, dst, AES_encrypt );
  123. }
  124. /**
  125. * Decrypt data
  126. *
  127. * @v ctx Context
  128. * @v src Data to decrypt
  129. * @v dst Buffer for decrypted data
  130. * @v len Length of data
  131. */
  132. static void aes_decrypt ( void *ctx, const void *src, void *dst,
  133. size_t len ) {
  134. struct aes_context *aes_ctx = ctx;
  135. assert ( len == AES_BLOCKSIZE );
  136. if ( ! aes_ctx->decrypting ) {
  137. AES_convert_key ( &aes_ctx->axtls_ctx );
  138. aes_ctx->decrypting = 1;
  139. }
  140. aes_call_axtls ( &aes_ctx->axtls_ctx, src, dst, AES_decrypt );
  141. }
  142. /** Basic AES algorithm */
  143. static struct cipher_algorithm aes_algorithm = {
  144. .name = "aes",
  145. .ctxsize = sizeof ( struct aes_context ),
  146. .blocksize = AES_BLOCKSIZE,
  147. .setkey = aes_setkey,
  148. .setiv = aes_setiv,
  149. .encrypt = aes_encrypt,
  150. .decrypt = aes_decrypt,
  151. };
  152. /****************************************************************************
  153. *
  154. * AES with cipher-block chaining (CBC)
  155. *
  156. ****************************************************************************
  157. */
  158. /** AES with CBC context */
  159. struct aes_cbc_context {
  160. /** AES context */
  161. struct aes_context aes_ctx;
  162. /** CBC context */
  163. uint8_t cbc_ctx[AES_BLOCKSIZE];
  164. };
  165. /**
  166. * Set key
  167. *
  168. * @v ctx Context
  169. * @v key Key
  170. * @v keylen Key length
  171. * @ret rc Return status code
  172. */
  173. static int aes_cbc_setkey ( void *ctx, const void *key, size_t keylen ) {
  174. struct aes_cbc_context *aes_cbc_ctx = ctx;
  175. return cbc_setkey ( ctx, key, keylen, &aes_algorithm,
  176. &aes_cbc_ctx->cbc_ctx );
  177. }
  178. /**
  179. * Set initialisation vector
  180. *
  181. * @v ctx Context
  182. * @v iv Initialisation vector
  183. */
  184. static void aes_cbc_setiv ( void *ctx, const void *iv ) {
  185. struct aes_cbc_context *aes_cbc_ctx = ctx;
  186. cbc_setiv ( ctx, iv, &aes_algorithm, &aes_cbc_ctx->cbc_ctx );
  187. }
  188. /**
  189. * Encrypt data
  190. *
  191. * @v ctx Context
  192. * @v src Data to encrypt
  193. * @v dst Buffer for encrypted data
  194. * @v len Length of data
  195. */
  196. static void aes_cbc_encrypt ( void *ctx, const void *src, void *dst,
  197. size_t len ) {
  198. struct aes_cbc_context *aes_cbc_ctx = ctx;
  199. cbc_encrypt ( &aes_cbc_ctx->aes_ctx, src, dst, len,
  200. &aes_algorithm, &aes_cbc_ctx->cbc_ctx );
  201. }
  202. /**
  203. * Decrypt data
  204. *
  205. * @v ctx Context
  206. * @v src Data to decrypt
  207. * @v dst Buffer for decrypted data
  208. * @v len Length of data
  209. */
  210. static void aes_cbc_decrypt ( void *ctx, const void *src, void *dst,
  211. size_t len ) {
  212. struct aes_cbc_context *aes_cbc_ctx = ctx;
  213. cbc_decrypt ( &aes_cbc_ctx->aes_ctx, src, dst, len,
  214. &aes_algorithm, &aes_cbc_ctx->cbc_ctx );
  215. }
  216. /* AES with cipher-block chaining */
  217. struct cipher_algorithm aes_cbc_algorithm = {
  218. .name = "aes_cbc",
  219. .ctxsize = sizeof ( struct aes_cbc_context ),
  220. .blocksize = AES_BLOCKSIZE,
  221. .setkey = aes_cbc_setkey,
  222. .setiv = aes_cbc_setiv,
  223. .encrypt = aes_cbc_encrypt,
  224. .decrypt = aes_cbc_decrypt,
  225. };