Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

axtls_aes.c 3.9KB

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