AESKeyWrap.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* AESWrap.java -- An implementation of RFC-3394 AES Key Wrap Algorithm
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.kwa;
  32. import gnu.java.security.Registry;
  33. import gnu.javax.crypto.cipher.IBlockCipher;
  34. import gnu.javax.crypto.cipher.Rijndael;
  35. import java.security.InvalidKeyException;
  36. import java.util.Arrays;
  37. import java.util.HashMap;
  38. import java.util.Map;
  39. /**
  40. * The GNU implementation of the AES Key Wrap Algorithm as described in [1].
  41. * <p>
  42. * References:
  43. * <ol>
  44. * <li><a href="http://csrc.nist.gov/encryption/kms/key-wrap.pdf"></a>.</li>
  45. * <li><a href="http://www.rfc-archive.org/getrfc.php?rfc=3394">Advanced
  46. * Encryption Standard (AES) Key Wrap Algorithm</a>.</li>
  47. * <li><a href="http://www.w3.org/TR/xmlenc-core/">XML Encryption Syntax and
  48. * Processing</a>.</li>
  49. * </ol>
  50. */
  51. public class AESKeyWrap
  52. extends BaseKeyWrappingAlgorithm
  53. {
  54. private static final byte[] DEFAULT_IV = new byte[] {
  55. (byte) 0xA6, (byte) 0xA6, (byte) 0xA6, (byte) 0xA6,
  56. (byte) 0xA6, (byte) 0xA6, (byte) 0xA6, (byte) 0xA6 };
  57. private Rijndael aes;
  58. private byte[] iv;
  59. public AESKeyWrap()
  60. {
  61. super(Registry.AES_KWA);
  62. aes = new Rijndael();
  63. }
  64. protected void engineInit(Map attributes) throws InvalidKeyException
  65. {
  66. Map cipherAttributes = new HashMap();
  67. cipherAttributes.put(IBlockCipher.CIPHER_BLOCK_SIZE, Integer.valueOf(16));
  68. cipherAttributes.put(IBlockCipher.KEY_MATERIAL,
  69. attributes.get(KEY_ENCRYPTION_KEY_MATERIAL));
  70. aes.reset();
  71. aes.init(cipherAttributes);
  72. byte[] initialValue = (byte[]) attributes.get(INITIAL_VALUE);
  73. iv = initialValue == null ? DEFAULT_IV : (byte[]) initialValue.clone();
  74. }
  75. protected byte[] engineWrap(byte[] in, int inOffset, int length)
  76. {
  77. // TODO: handle input length which is not a multiple of 8 as suggested by
  78. // section 2.2.3.2 of RFC-3394
  79. if (length % 8 != 0)
  80. throw new IllegalArgumentException("Input length MUST be a multiple of 8");
  81. int n = length / 8;
  82. // output is always one block larger than input
  83. byte[] result = new byte[length + 8];
  84. // 1. init variables: we'll use out buffer for our work buffer;
  85. // A will be the first block in out, while R will be the rest
  86. System.arraycopy(iv, 0, result, 0, 8);
  87. System.arraycopy(in, inOffset, result, 8, length);
  88. byte[] B = new byte[2 * 8];
  89. // 2. compute intermediate values
  90. long t;
  91. for (int j = 0; j < 6; j++)
  92. for (int i = 1; i <= n; i++)
  93. {
  94. System.arraycopy(result, 0, B, 0, 8);
  95. System.arraycopy(result, i * 8, B, 8, 8);
  96. aes.encryptBlock(B, 0, B, 0);
  97. t = (n * j) + i;
  98. result[0] = (byte)(B[0] ^ (t >>> 56));
  99. result[1] = (byte)(B[1] ^ (t >>> 48));
  100. result[2] = (byte)(B[2] ^ (t >>> 40));
  101. result[3] = (byte)(B[3] ^ (t >>> 32));
  102. result[4] = (byte)(B[4] ^ (t >>> 24));
  103. result[5] = (byte)(B[5] ^ (t >>> 16));
  104. result[6] = (byte)(B[6] ^ (t >>> 8));
  105. result[7] = (byte)(B[7] ^ t );
  106. System.arraycopy(B, 8, result, i * 8, 8);
  107. }
  108. return result;
  109. }
  110. protected byte[] engineUnwrap(byte[] in, int inOffset, int length)
  111. throws KeyUnwrappingException
  112. {
  113. // TODO: handle input length which is not a multiple of 8 as suggested by
  114. // section 2.2.3.2 of RFC-3394
  115. if (length % 8 != 0)
  116. throw new IllegalArgumentException("Input length MUST be a multiple of 8");
  117. // output is always one block shorter than input
  118. byte[] result = new byte[length - 8];
  119. // 1. init variables: we'll use out buffer for our R work buffer
  120. byte[] A = new byte[8];
  121. System.arraycopy(in, inOffset, A, 0, 8);
  122. System.arraycopy(in, inOffset + 8, result, 0, result.length);
  123. byte[] B = new byte[2 * 8];
  124. // 2. compute intermediate values
  125. int n = length / 8 - 1;
  126. long t;
  127. for (int j = 5; j >= 0; j--)
  128. for (int i = n; i >= 1; i--)
  129. {
  130. t = (n * j) + i;
  131. B[0] = (byte)(A[0] ^ (t >>> 56));
  132. B[1] = (byte)(A[1] ^ (t >>> 48));
  133. B[2] = (byte)(A[2] ^ (t >>> 40));
  134. B[3] = (byte)(A[3] ^ (t >>> 32));
  135. B[4] = (byte)(A[4] ^ (t >>> 24));
  136. B[5] = (byte)(A[5] ^ (t >>> 16));
  137. B[6] = (byte)(A[6] ^ (t >>> 8));
  138. B[7] = (byte)(A[7] ^ t );
  139. System.arraycopy(result, (i - 1) * 8, B, 8, 8);
  140. aes.decryptBlock(B, 0, B, 0);
  141. System.arraycopy(B, 0, A, 0, 8);
  142. System.arraycopy(B, 8, result, (i - 1) * 8, 8);
  143. }
  144. if (! Arrays.equals(A, iv))
  145. throw new KeyUnwrappingException();
  146. return result;
  147. }
  148. }