您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

axtls_aes.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <assert.h>
  23. #include <byteswap.h>
  24. #include <ipxe/crypto.h>
  25. #include <ipxe/ecb.h>
  26. #include <ipxe/cbc.h>
  27. #include <ipxe/aes.h>
  28. #include "crypto/axtls/crypto.h"
  29. /** @file
  30. *
  31. * AES algorithm
  32. *
  33. */
  34. /**
  35. * Set key
  36. *
  37. * @v ctx Context
  38. * @v key Key
  39. * @v keylen Key length
  40. * @ret rc Return status code
  41. */
  42. static int aes_setkey ( void *ctx, const void *key, size_t keylen ) {
  43. struct aes_context *aes_ctx = ctx;
  44. AES_MODE mode;
  45. void *iv;
  46. switch ( keylen ) {
  47. case ( 128 / 8 ):
  48. mode = AES_MODE_128;
  49. break;
  50. case ( 256 / 8 ):
  51. mode = AES_MODE_256;
  52. break;
  53. default:
  54. return -EINVAL;
  55. }
  56. /* IV is not a relevant concept at this stage; use a dummy
  57. * value that will have no side-effects.
  58. */
  59. iv = &aes_ctx->axtls_ctx.iv;
  60. AES_set_key ( &aes_ctx->axtls_ctx, key, iv, mode );
  61. aes_ctx->decrypting = 0;
  62. return 0;
  63. }
  64. /**
  65. * Set initialisation vector
  66. *
  67. * @v ctx Context
  68. * @v iv Initialisation vector
  69. */
  70. static void aes_setiv ( void *ctx __unused, const void *iv __unused ) {
  71. /* Nothing to do */
  72. }
  73. /**
  74. * Call AXTLS' AES_encrypt() or AES_decrypt() functions
  75. *
  76. * @v axtls_ctx AXTLS AES context
  77. * @v src Data to process
  78. * @v dst Buffer for output
  79. * @v func AXTLS AES function to call
  80. */
  81. static void aes_call_axtls ( AES_CTX *axtls_ctx, const void *src, void *dst,
  82. void ( * func ) ( const AES_CTX *axtls_ctx,
  83. uint32_t *data ) ){
  84. const uint32_t *srcl = src;
  85. uint32_t *dstl = dst;
  86. unsigned int i;
  87. /* AXTLS' AES_encrypt() and AES_decrypt() functions both
  88. * expect to deal with an array of four dwords in host-endian
  89. * order.
  90. */
  91. for ( i = 0 ; i < 4 ; i++ )
  92. dstl[i] = ntohl ( srcl[i] );
  93. func ( axtls_ctx, dstl );
  94. for ( i = 0 ; i < 4 ; i++ )
  95. dstl[i] = htonl ( dstl[i] );
  96. }
  97. /**
  98. * Encrypt data
  99. *
  100. * @v ctx Context
  101. * @v src Data to encrypt
  102. * @v dst Buffer for encrypted data
  103. * @v len Length of data
  104. */
  105. static void aes_encrypt ( void *ctx, const void *src, void *dst,
  106. size_t len ) {
  107. struct aes_context *aes_ctx = ctx;
  108. assert ( len == AES_BLOCKSIZE );
  109. if ( aes_ctx->decrypting )
  110. assert ( 0 );
  111. aes_call_axtls ( &aes_ctx->axtls_ctx, src, dst, axtls_aes_encrypt );
  112. }
  113. /**
  114. * Decrypt data
  115. *
  116. * @v ctx Context
  117. * @v src Data to decrypt
  118. * @v dst Buffer for decrypted data
  119. * @v len Length of data
  120. */
  121. static void aes_decrypt ( void *ctx, const void *src, void *dst,
  122. size_t len ) {
  123. struct aes_context *aes_ctx = ctx;
  124. assert ( len == AES_BLOCKSIZE );
  125. if ( ! aes_ctx->decrypting ) {
  126. AES_convert_key ( &aes_ctx->axtls_ctx );
  127. aes_ctx->decrypting = 1;
  128. }
  129. aes_call_axtls ( &aes_ctx->axtls_ctx, src, dst, axtls_aes_decrypt );
  130. }
  131. /** Basic AES algorithm */
  132. struct cipher_algorithm aes_algorithm = {
  133. .name = "aes",
  134. .ctxsize = sizeof ( struct aes_context ),
  135. .blocksize = AES_BLOCKSIZE,
  136. .setkey = aes_setkey,
  137. .setiv = aes_setiv,
  138. .encrypt = aes_encrypt,
  139. .decrypt = aes_decrypt,
  140. };
  141. /* AES in Electronic Codebook mode */
  142. ECB_CIPHER ( aes_ecb, aes_ecb_algorithm,
  143. aes_algorithm, struct aes_context, AES_BLOCKSIZE );
  144. /* AES in Cipher Block Chaining mode */
  145. CBC_CIPHER ( aes_cbc, aes_cbc_algorithm,
  146. aes_algorithm, struct aes_context, AES_BLOCKSIZE );