CFB.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* CFB.java --
  2. Copyright (C) 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.javax.crypto.cipher.IBlockCipher;
  34. /**
  35. * The cipher feedback mode. CFB mode is a stream mode that operates on <i>s</i>
  36. * bit blocks, where 1 &lt;= <i>s</i> &lt;= <i>b</i>, if <i>b</i> is the
  37. * underlying cipher's block size. Encryption is:
  38. * <pre>
  39. * I[1] = IV
  40. * I[j] = LSB(b-s, I[j-1]) | C[j-1] for j = 2...n
  41. * O[j] = CIPH(K, I[j]) for j = 1,2...n
  42. * C[j] = P[j] &circ; MSB(s, O[j]) for j = 1,2...n
  43. * </pre>
  44. * <p>
  45. * And decryption is:
  46. * <pre>
  47. * I[1] = IV
  48. * I[j] = LSB(b-s, I[j-1]) | C[j-1] for j = 2...n
  49. * O[j] = CIPH(K, I[j]) for j = 1,2...n
  50. * P[j] = C[j] &circ; MSB(s, O[j]) for j = 1,2...n
  51. * </pre>
  52. * <p>
  53. * CFB mode requires an initialization vector, which need not be kept secret.
  54. * <p>
  55. * References:
  56. * <ol>
  57. * <li>Bruce Schneier, <i>Applied Cryptography: Protocols, Algorithms, and
  58. * Source Code in C, Second Edition</i>. (1996 John Wiley and Sons) ISBN
  59. * 0-471-11709-9.</li>
  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 CFB
  67. extends BaseMode
  68. {
  69. /** The shift register, the input block to the block cipher. */
  70. private byte[] shiftRegister;
  71. /** The output block from the block cipher. */
  72. private byte[] scratch;
  73. /**
  74. * Package-private constructor for the factory class.
  75. *
  76. * @param underlyingCipher The cipher implementation.
  77. * @param cipherBlockSize The cipher's block size.
  78. */
  79. CFB(IBlockCipher underlyingCipher, int cipherBlockSize)
  80. {
  81. super(Registry.CFB_MODE, underlyingCipher, cipherBlockSize);
  82. }
  83. /**
  84. * Cloneing constructor.
  85. *
  86. * @param that The instance being cloned.
  87. */
  88. private CFB(CFB that)
  89. {
  90. this((IBlockCipher) that.cipher.clone(), that.cipherBlockSize);
  91. }
  92. public Object clone()
  93. {
  94. return new CFB(this);
  95. }
  96. public void setup()
  97. {
  98. if (modeBlockSize > cipherBlockSize)
  99. throw new IllegalArgumentException(
  100. "CFB block size cannot be larger than the cipher block size");
  101. shiftRegister = new byte[cipherBlockSize];
  102. scratch = new byte[cipherBlockSize];
  103. System.arraycopy(iv, 0,
  104. shiftRegister, 0,
  105. Math.min(iv.length, cipherBlockSize));
  106. }
  107. public void teardown()
  108. {
  109. if (shiftRegister != null)
  110. for (int i = 0; i < shiftRegister.length; i++)
  111. shiftRegister[i] = 0;
  112. shiftRegister = null;
  113. }
  114. public void encryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
  115. {
  116. cipher.encryptBlock(shiftRegister, 0, scratch, 0);
  117. for (int i = 0; i < modeBlockSize; i++)
  118. out[outOffset + i] = (byte)(in[inOffset + i] ^ scratch[i]);
  119. System.arraycopy(shiftRegister, modeBlockSize,
  120. shiftRegister, 0,
  121. cipherBlockSize - modeBlockSize);
  122. System.arraycopy(out, outOffset,
  123. shiftRegister, cipherBlockSize - modeBlockSize,
  124. modeBlockSize);
  125. }
  126. public void decryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
  127. {
  128. cipher.encryptBlock(shiftRegister, 0, scratch, 0);
  129. for (int i = 0; i < modeBlockSize; i++)
  130. out[outOffset + i] = (byte)(in[inOffset + i] ^ scratch[i]);
  131. System.arraycopy(shiftRegister, modeBlockSize,
  132. shiftRegister, 0,
  133. cipherBlockSize - modeBlockSize);
  134. System.arraycopy(in, inOffset,
  135. shiftRegister, cipherBlockSize - modeBlockSize,
  136. modeBlockSize);
  137. }
  138. }