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.

aes_wrap.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
  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 <stdlib.h>
  21. #include <string.h>
  22. #include <ipxe/crypto.h>
  23. #include <ipxe/aes.h>
  24. /**
  25. * Wrap a key or other data using AES Key Wrap (RFC 3394)
  26. *
  27. * @v kek Key Encryption Key, 16 bytes
  28. * @v src Data to encrypt
  29. * @v nblk Number of 8-byte blocks in @a data
  30. * @ret dest Encrypted data (8 bytes longer than input)
  31. *
  32. * The algorithm is implemented such that @a src and @a dest may point
  33. * to the same buffer.
  34. */
  35. int aes_wrap ( const void *kek, const void *src, void *dest, int nblk )
  36. {
  37. u8 *A = dest;
  38. u8 B[16];
  39. u8 *R;
  40. int i, j;
  41. void *aes_ctx = malloc ( AES_CTX_SIZE );
  42. if ( ! aes_ctx )
  43. return -1;
  44. cipher_setkey ( &aes_algorithm, aes_ctx, kek, 16 );
  45. /* Set up */
  46. memset ( A, 0xA6, 8 );
  47. memmove ( dest + 8, src, nblk * 8 );
  48. /* Wrap */
  49. for ( j = 0; j < 6; j++ ) {
  50. R = dest + 8;
  51. for ( i = 1; i <= nblk; i++ ) {
  52. memcpy ( B, A, 8 );
  53. memcpy ( B + 8, R, 8 );
  54. cipher_encrypt ( &aes_algorithm, aes_ctx, B, B, 16 );
  55. memcpy ( A, B, 8 );
  56. A[7] ^= ( nblk * j ) + i;
  57. memcpy ( R, B + 8, 8 );
  58. R += 8;
  59. }
  60. }
  61. free ( aes_ctx );
  62. return 0;
  63. }
  64. /**
  65. * Unwrap a key or other data using AES Key Wrap (RFC 3394)
  66. *
  67. * @v kek Key Encryption Key, 16 bytes
  68. * @v src Data to decrypt
  69. * @v nblk Number of 8-byte blocks in @e plaintext key
  70. * @ret dest Decrypted data (8 bytes shorter than input)
  71. * @ret rc Zero on success, nonzero on IV mismatch
  72. *
  73. * The algorithm is implemented such that @a src and @a dest may point
  74. * to the same buffer.
  75. */
  76. int aes_unwrap ( const void *kek, const void *src, void *dest, int nblk )
  77. {
  78. u8 A[8], B[16];
  79. u8 *R;
  80. int i, j;
  81. void *aes_ctx = malloc ( AES_CTX_SIZE );
  82. if ( ! aes_ctx )
  83. return -1;
  84. cipher_setkey ( &aes_algorithm, aes_ctx, kek, 16 );
  85. /* Set up */
  86. memcpy ( A, src, 8 );
  87. memmove ( dest, src + 8, nblk * 8 );
  88. /* Unwrap */
  89. for ( j = 5; j >= 0; j-- ) {
  90. R = dest + ( nblk - 1 ) * 8;
  91. for ( i = nblk; i >= 1; i-- ) {
  92. memcpy ( B, A, 8 );
  93. memcpy ( B + 8, R, 8 );
  94. B[7] ^= ( nblk * j ) + i;
  95. cipher_decrypt ( &aes_algorithm, aes_ctx, B, B, 16 );
  96. memcpy ( A, B, 8 );
  97. memcpy ( R, B + 8, 8 );
  98. R -= 8;
  99. }
  100. }
  101. free ( aes_ctx );
  102. /* Check IV */
  103. for ( i = 0; i < 8; i++ ) {
  104. if ( A[i] != 0xA6 )
  105. return -1;
  106. }
  107. return 0;
  108. }