CTR.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* CTR.java --
  2. Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
  3. This file is a 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 of the License, or (at
  7. your option) 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; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. 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.mode;
  32. import gnu.java.security.Registry;
  33. import gnu.java.security.util.Sequence;
  34. import gnu.javax.crypto.cipher.IBlockCipher;
  35. import java.util.Arrays;
  36. import java.util.Iterator;
  37. /**
  38. * The implementation of the Counter Mode.
  39. * <p>
  40. * The algorithm steps are formally described as follows:
  41. *
  42. * <pre>
  43. * CTR Encryption: O[j] = E(K)(T[j]); for j = 1, 2...n;
  44. * C[j] = P[j] &circ; O[j]; for j = 1, 2...n.
  45. * CTR Decryption: O[j] = E(K)(T[j]); for j = 1, 2...n;
  46. * P[j] = C[j] &circ; O[j]; for j = 1, 2...n.
  47. * </pre>
  48. *
  49. * <p>
  50. * where <code>P</code> is the plaintext, <code>C</code> is the ciphertext,
  51. * <code>E(K)</code> is the underlying block cipher encryption function
  52. * parametrised with the session key <code>K</code>, and <code>T</code> is
  53. * the <i>Counter</i>.
  54. * <p>
  55. * This implementation, uses a standard incrementing function with a step of 1,
  56. * and an initial value similar to that described in the NIST document.
  57. * <p>
  58. * References:
  59. * <ol>
  60. * <li><a
  61. * href="http://csrc.nist.gov/encryption/modes/Recommendation/Modes01.pdf">
  62. * Recommendation for Block Cipher Modes of Operation Methods and Techniques</a>,
  63. * Morris Dworkin.</li>
  64. * </ol>
  65. */
  66. public class CTR
  67. extends BaseMode
  68. implements Cloneable
  69. {
  70. private int off;
  71. private byte[] counter, enc;
  72. /**
  73. * Trivial package-private constructor for use by the Factory class.
  74. *
  75. * @param underlyingCipher the underlying cipher implementation.
  76. * @param cipherBlockSize the underlying cipher block size to use.
  77. */
  78. CTR(IBlockCipher underlyingCipher, int cipherBlockSize)
  79. {
  80. super(Registry.CTR_MODE, underlyingCipher, cipherBlockSize);
  81. }
  82. /**
  83. * Private constructor for cloning purposes.
  84. *
  85. * @param that the instance to clone.
  86. */
  87. private CTR(CTR that)
  88. {
  89. this((IBlockCipher) that.cipher.clone(), that.cipherBlockSize);
  90. }
  91. public Object clone()
  92. {
  93. return new CTR(this);
  94. }
  95. public void setup()
  96. {
  97. if (modeBlockSize > cipherBlockSize)
  98. throw new IllegalArgumentException("mode size exceeds cipher block size");
  99. off = 0;
  100. counter = new byte[cipherBlockSize];
  101. int i = cipherBlockSize - 1;
  102. int j = iv.length - 1;
  103. while (i >= 0 && j >= 0)
  104. counter[i--] = iv[j--];
  105. enc = new byte[cipherBlockSize];
  106. cipher.encryptBlock(counter, 0, enc, 0);
  107. }
  108. public void teardown()
  109. {
  110. if (counter != null)
  111. Arrays.fill(counter, (byte) 0);
  112. if (enc != null)
  113. Arrays.fill(enc, (byte) 0);
  114. }
  115. public void encryptBlock(byte[] in, int i, byte[] out, int o)
  116. {
  117. ctr(in, i, out, o);
  118. }
  119. public void decryptBlock(byte[] in, int i, byte[] out, int o)
  120. {
  121. ctr(in, i, out, o);
  122. }
  123. public Iterator blockSizes()
  124. {
  125. return new Sequence(1, cipherBlockSize).iterator();
  126. }
  127. private void ctr(byte[] in, int inOffset, byte[] out, int outOffset)
  128. {
  129. for (int i = 0; i < modeBlockSize; i++)
  130. {
  131. out[outOffset++] = (byte)(in[inOffset++] ^ enc[off++]);
  132. if (off == cipherBlockSize)
  133. {
  134. int j;
  135. for (j = cipherBlockSize - 1; j >= 0; j--)
  136. {
  137. counter[j]++;
  138. if ((counter[j] & 0xFF) != 0)
  139. break;
  140. }
  141. if (j == 0)
  142. counter[cipherBlockSize - 1]++;
  143. off = 0;
  144. cipher.encryptBlock(counter, 0, enc, 0);
  145. }
  146. }
  147. }
  148. }