Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

axtls_aes.c 3.7KB

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