ECB.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* ECB.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 implementation of the Electronic Codebook mode.
  36. * <p>
  37. * The Electronic Codebook (ECB) mode is a confidentiality mode that is defined
  38. * as follows:
  39. * <ul>
  40. * <li>ECB Encryption: C<sub>j</sub> = CIPH<sub>K</sub>(P<sub>j</sub>)
  41. * for j = 1...n</li>
  42. * <li>ECB Decryption: P<sub>j</sub> = CIPH<sup>-1</sup><sub>K</sub>(C<sub>j</sub>)
  43. * for j = 1...n</li>
  44. * </ul>
  45. * <p>
  46. * In ECB encryption, the forward cipher function is applied directly, and
  47. * independently, to each block of the plaintext. The resulting sequence of
  48. * output blocks is the ciphertext.
  49. * <p>
  50. * In ECB decryption, the inverse cipher function is applied directly, and
  51. * independently, to each block of the ciphertext. The resulting sequence of
  52. * output blocks is the plaintext.
  53. * <p>
  54. * References:
  55. * <ol>
  56. * <li><a
  57. * href="http://csrc.nist.gov/encryption/modes/Recommendation/Modes01.pdf">
  58. * Recommendation for Block Cipher Modes of Operation Methods and Techniques</a>,
  59. * Morris Dworkin.</li>
  60. * </ol>
  61. */
  62. public class ECB
  63. extends BaseMode
  64. implements Cloneable
  65. {
  66. /**
  67. * Trivial package-private constructor for use by the Factory class.
  68. *
  69. * @param underlyingCipher the underlying cipher implementation.
  70. * @param cipherBlockSize the underlying cipher block size to use.
  71. */
  72. ECB(IBlockCipher underlyingCipher, int cipherBlockSize)
  73. {
  74. super(Registry.ECB_MODE, underlyingCipher, cipherBlockSize);
  75. }
  76. /**
  77. * Private constructor for cloning purposes.
  78. *
  79. * @param that the mode to clone.
  80. */
  81. private ECB(ECB that)
  82. {
  83. this((IBlockCipher) that.cipher.clone(), that.cipherBlockSize);
  84. }
  85. public Object clone()
  86. {
  87. return new ECB(this);
  88. }
  89. public void setup()
  90. {
  91. if (modeBlockSize != cipherBlockSize)
  92. throw new IllegalArgumentException(IMode.MODE_BLOCK_SIZE);
  93. }
  94. public void teardown()
  95. {
  96. }
  97. public void encryptBlock(byte[] in, int i, byte[] out, int o)
  98. {
  99. cipher.encryptBlock(in, i, out, o);
  100. }
  101. public void decryptBlock(byte[] in, int i, byte[] out, int o)
  102. {
  103. cipher.decryptBlock(in, i, out, o);
  104. }
  105. }