BasePad.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* BasePad.java --
  2. Copyright (C) 2001, 2002, 2003, 2006, 2010 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.pad;
  32. import gnu.java.lang.CPStringBuilder;
  33. import gnu.java.security.Configuration;
  34. import java.util.Map;
  35. import java.util.logging.Level;
  36. import java.util.logging.Logger;
  37. /**
  38. * An abstract class to facilitate implementing padding algorithms.
  39. */
  40. public abstract class BasePad
  41. implements IPad
  42. {
  43. private static final Logger log = Configuration.DEBUG ?
  44. Logger.getLogger(BasePad.class.getName()) : null;
  45. /** The canonical name prefix of the padding algorithm. */
  46. protected String name;
  47. /** The block size, in bytes, for this instance. */
  48. protected int blockSize;
  49. /** Trivial constructor for use by concrete subclasses. */
  50. protected BasePad(final String name)
  51. {
  52. super();
  53. this.name = name;
  54. blockSize = -1;
  55. }
  56. public String name()
  57. {
  58. final CPStringBuilder sb = new CPStringBuilder(name);
  59. if (blockSize != -1)
  60. sb.append('-').append(String.valueOf(8 * blockSize));
  61. return sb.toString();
  62. }
  63. public void init(final int bs) throws IllegalStateException
  64. {
  65. if (blockSize != -1)
  66. throw new IllegalStateException();
  67. blockSize = bs;
  68. setup();
  69. }
  70. /**
  71. * Initialises the algorithm with designated attributes. Names, valid and/or
  72. * recognisable by all concrete implementations are described in {@link IPad}
  73. * class documentation. Other algorithm-specific attributes MUST be documented
  74. * in the implementation class of that padding algorithm.
  75. * <p>
  76. * For compatibility reasons, this method is not declared <i>abstract</i>.
  77. * Furthermore, and unless overridden, the default implementation will throw
  78. * an {@link UnsupportedOperationException}. Concrete padding algorithms MUST
  79. * override this method if they wish to offer an initialisation method that
  80. * allows for other than the padding block size parameter to be specified.
  81. *
  82. * @param attributes a set of name-value pairs that describes the desired
  83. * future behaviour of this instance.
  84. * @exception IllegalStateException if the instance is already initialised.
  85. * @exception IllegalArgumentException if the block size value is invalid.
  86. */
  87. public void init(Map attributes) throws IllegalStateException
  88. {
  89. throw new UnsupportedOperationException();
  90. }
  91. public void reset()
  92. {
  93. blockSize = -1;
  94. }
  95. /**
  96. * A default implementation of a correctness test that exercises the padder
  97. * implementation, using block sizes varying from 2 to 256 bytes.
  98. *
  99. * @return <code>true</code> if the concrete implementation correctly unpads
  100. * what it pads for all tested block sizes. Returns <code>false</code>
  101. * if the test fails for any block size.
  102. */
  103. public boolean selfTest()
  104. {
  105. final byte[] in = new byte[1024];
  106. for (int bs = 2; bs < 256; bs++)
  107. if (! test1BlockSize(bs, in))
  108. return false;
  109. return true;
  110. }
  111. /**
  112. * The basic symmetric test for a padder given a specific block size.
  113. * <p>
  114. * The code ensures that the implementation is capable of unpadding what it
  115. * pads.
  116. *
  117. * @param size the block size to test.
  118. * @param buffer a work buffer. It is exposed as an argument for this method
  119. * to reduce un-necessary object allocations.
  120. * @return <code>true</code> if the test passes; <code>false</code>
  121. * otherwise.
  122. */
  123. protected boolean test1BlockSize(int size, byte[] buffer)
  124. {
  125. byte[] padBytes;
  126. final int offset = 5;
  127. final int limit = buffer.length;
  128. this.init(size);
  129. for (int i = 0; i < limit - offset - blockSize; i++)
  130. {
  131. padBytes = pad(buffer, offset, i);
  132. if (((i + padBytes.length) % blockSize) != 0)
  133. {
  134. if (Configuration.DEBUG)
  135. log.log(Level.SEVERE,
  136. "Length of padded text MUST be a multiple of "
  137. + blockSize, new RuntimeException(name()));
  138. return false;
  139. }
  140. System.arraycopy(padBytes, 0, buffer, offset + i, padBytes.length);
  141. try
  142. {
  143. if (padBytes.length != unpad(buffer, offset, i + padBytes.length))
  144. {
  145. if (Configuration.DEBUG)
  146. log.log(Level.SEVERE,
  147. "IPad [" + name() + "] failed symmetric operation",
  148. new RuntimeException(name()));
  149. return false;
  150. }
  151. }
  152. catch (WrongPaddingException x)
  153. {
  154. if (Configuration.DEBUG)
  155. log.throwing(this.getClass().getName(), "test1BlockSize", x);
  156. return false;
  157. }
  158. }
  159. this.reset();
  160. return true;
  161. }
  162. /**
  163. * If any additional checks or resource setup must be done by the subclass,
  164. * then this is the hook for it. This method will be called before the
  165. * {@link #init(int)} method returns.
  166. */
  167. public abstract void setup();
  168. public abstract byte[] pad(byte[] in, int off, int len);
  169. public abstract int unpad(byte[] in, int off, int len)
  170. throws WrongPaddingException;
  171. }