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ů.

aes_wrap.c 2.9KB

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