OFB.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* OFB.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.javax.crypto.cipher.IBlockCipher;
  34. /**
  35. * The Output Feedback (OFB) mode is a confidentiality mode that requires a
  36. * unique <code>IV</code> for every message that is ever encrypted under the
  37. * given key. The OFB mode is defined as follows:
  38. * <ul>
  39. * <li>OFB Encryption:
  40. * <ul>
  41. * <li>I<sub>1</sub> = IV;</li>
  42. * <li>I<sub>j</sub> = O<sub>j -1</sub> for j = 2...n;</li>
  43. * <li>O<sub>j</sub> = CIPH<sub>K</sub>(I<sub>j</sub>) for j = 1, 2...n;</li>
  44. * <li>C<sub>j</sub> = P<sub>j</sub> XOR O<sub>j</sub> for j = 1, 2...n.</li>
  45. * </ul>
  46. * </li>
  47. * <li>OFB Decryption:
  48. * <ul>
  49. * <li>I<sub>1</sub> = IV;</li>
  50. * <li>I<sub>j</sub> = O<sub>j -1</sub> for j = 2...n;</li>
  51. * <li>O<sub>j</sub> = CIPH<sub>K</sub>(I<sub>j</sub>) for j = 1, 2...n;</li>
  52. * <li>P<sub>j</sub> = C<sub>j</sub> XOR O<sub>j</sub> for j = 1, 2...n.</li>
  53. * </ul>
  54. * </li>
  55. * </ul>
  56. * <p>
  57. * In OFB encryption, the <code>IV</code> is transformed by the forward cipher
  58. * function to produce the first output block. The first output block is
  59. * exclusive-ORed with the first plaintext block to produce the first ciphertext
  60. * block. The first output block is then transformed by the forward cipher
  61. * function to produce the second output block. The second output block is
  62. * exclusive-ORed with the second plaintext block to produce the second
  63. * ciphertext block, and the second output block is transformed by the forward
  64. * cipher function to produce the third output block. Thus, the successive
  65. * output blocks are produced from enciphering the previous output blocks, and
  66. * the output blocks are exclusive-ORed with the corresponding plaintext blocks
  67. * to produce the ciphertext blocks.
  68. * <p>
  69. * In OFB decryption, the <code>IV</code> is transformed by the forward cipher
  70. * function to produce the first output block. The first output block is
  71. * exclusive-ORed with the first ciphertext block to recover the first plaintext
  72. * block. The first output block is then transformed by the forward cipher
  73. * function to produce the second output block. The second output block is
  74. * exclusive-ORed with the second ciphertext block to produce the second
  75. * plaintext block, and the second output block is also transformed by the
  76. * forward cipher function to produce the third output block. Thus, the
  77. * successive output blocks are produced from enciphering the previous output
  78. * blocks, and the output blocks are exclusive-ORed with the corresponding
  79. * ciphertext blocks to recover the plaintext blocks.
  80. * <p>
  81. * In both OFB encryption and OFB decryption, each forward cipher function
  82. * (except the first) depends on the results of the previous forward cipher
  83. * function; therefore, multiple forward cipher functions cannot be performed in
  84. * parallel. However, if the <code>IV</code> is known, the output blocks can
  85. * be generated prior to the availability of the plaintext or ciphertext data.
  86. * <p>
  87. * The OFB mode requires a unique <code>IV</code> for every message that is
  88. * ever encrypted under the given key. If, contrary to this requirement, the
  89. * same <code>IV</code> is used for the encryption of more than one message,
  90. * then the confidentiality of those messages may be compromised. In particular,
  91. * if a plaintext block of any of these messages is known, say, the j<sup>th</sup>
  92. * plaintext block, then the j<sup>th</sup> output of the forward cipher
  93. * function can be determined easily from the j<sup>th</sup> ciphertext block
  94. * of the message. This information allows the j<sup>th</sup> plaintext block
  95. * of any other message that is encrypted using the same <code>IV</code> to be
  96. * easily recovered from the jth ciphertext block of that message.
  97. * <p>
  98. * Confidentiality may similarly be compromised if any of the input blocks to
  99. * the forward cipher function for the encryption of a message is used as the
  100. * <code>IV</code> for the encryption of another message under the given key.
  101. * <p>
  102. * References:
  103. * <ol>
  104. * <li><a
  105. * href="http://csrc.nist.gov/encryption/modes/Recommendation/Modes01.pdf">
  106. * Recommendation for Block Cipher Modes of Operation Methods and Techniques</a>,
  107. * Morris Dworkin.</li>
  108. * </ol>
  109. */
  110. public class OFB
  111. extends BaseMode
  112. implements Cloneable
  113. {
  114. private byte[] outputBlock;
  115. /**
  116. * Trivial package-private constructor for use by the Factory class.
  117. *
  118. * @param underlyingCipher the underlying cipher implementation.
  119. * @param cipherBlockSize the underlying cipher block size to use.
  120. */
  121. OFB(IBlockCipher underlyingCipher, int cipherBlockSize)
  122. {
  123. super(Registry.OFB_MODE, underlyingCipher, cipherBlockSize);
  124. }
  125. /**
  126. * Private constructor for cloning purposes.
  127. *
  128. * @param that the mode to clone.
  129. */
  130. private OFB(OFB that)
  131. {
  132. this((IBlockCipher) that.cipher.clone(), that.cipherBlockSize);
  133. }
  134. public Object clone()
  135. {
  136. return new OFB(this);
  137. }
  138. public void setup()
  139. {
  140. if (modeBlockSize != cipherBlockSize)
  141. throw new IllegalArgumentException(IMode.MODE_BLOCK_SIZE);
  142. outputBlock = (byte[]) iv.clone();
  143. }
  144. public void teardown()
  145. {
  146. }
  147. public void encryptBlock(byte[] in, int i, byte[] out, int o)
  148. {
  149. cipher.encryptBlock(outputBlock, 0, outputBlock, 0);
  150. for (int j = 0; j < cipherBlockSize;)
  151. out[o++] = (byte)(in[i++] ^ outputBlock[j++]);
  152. }
  153. public void decryptBlock(byte[] in, int i, byte[] out, int o)
  154. {
  155. this.encryptBlock(in, i, out, o);
  156. }
  157. }