You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

axtls_aes.c 3.9KB

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