Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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