axtls_aes.c 3.7KB

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