IPad.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* IPad.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.pad;
  32. import java.util.Map;
  33. /**
  34. * The basic visible methods, and attribute names, of every padding algorithm.
  35. * <p>
  36. * Padding algorithms serve to <i>pad</i> and <i>unpad</i> byte arrays usually
  37. * as the last step in an <i>encryption</i> or respectively a <i>decryption</i>
  38. * operation. Their input buffers are usually those processed by instances of
  39. * {@link gnu.javax.crypto.mode.IMode} and/or
  40. * {@link gnu.javax.crypto.cipher.IBlockCipher}.
  41. */
  42. public interface IPad
  43. {
  44. /**
  45. * Property name of the block size in which to operate the padding algorithm.
  46. * The value associated with this property name is taken to be a positive
  47. * {@link Integer} greater than zero.
  48. */
  49. String PADDING_BLOCK_SIZE = "gnu.crypto.pad.block.size";
  50. /** @return the canonical name of this instance. */
  51. String name();
  52. /**
  53. * Initialises the padding scheme with a designated block size.
  54. *
  55. * @param bs the designated block size.
  56. * @exception IllegalStateException if the instance is already initialised.
  57. * @exception IllegalArgumentException if the block size value is invalid.
  58. */
  59. void init(int bs) throws IllegalStateException;
  60. /**
  61. * Initialises the algorithm with designated attributes. Names, valid and/or
  62. * recognisable by all concrete implementations are described in the class
  63. * documentation above. Other algorithm-specific attributes MUST be documented
  64. * in the implementation class of that padding algorithm.
  65. *
  66. * @param attributes a set of name-value pairs that describes the desired
  67. * future behaviour of this instance.
  68. * @exception IllegalStateException if the instance is already initialised.
  69. * @exception IllegalArgumentException if the block size value is invalid.
  70. */
  71. void init(Map attributes) throws IllegalStateException;
  72. /**
  73. * Returns the byte sequence that should be appended to the designated input.
  74. *
  75. * @param in the input buffer containing the bytes to pad.
  76. * @param offset the starting index of meaningful data in <i>in</i>.
  77. * @param length the number of meaningful bytes in <i>in</i>.
  78. * @return the possibly 0-byte long sequence to be appended to the designated
  79. * input.
  80. */
  81. byte[] pad(byte[] in, int offset, int length);
  82. /**
  83. * Returns the number of bytes to discard from a designated input buffer.
  84. *
  85. * @param in the input buffer containing the bytes to unpad.
  86. * @param offset the starting index of meaningful data in <i>in</i>.
  87. * @param length the number of meaningful bytes in <i>in</i>.
  88. * @return the number of bytes to discard, to the left of index position
  89. * <code>offset + length</code> in <i>in</i>. In other words, if
  90. * the return value of a successful invocation of this method is
  91. * <code>result</code>, then the unpadded byte sequence will be
  92. * <code>offset + length - result</code> bytes in <i>in</i>,
  93. * starting from index position <code>offset</code>.
  94. * @exception WrongPaddingException if the data is not terminated with the
  95. * expected padding bytes.
  96. */
  97. int unpad(byte[] in, int offset, int length) throws WrongPaddingException;
  98. /**
  99. * Resets the scheme instance for re-initialisation and use with other
  100. * characteristics. This method always succeeds.
  101. */
  102. void reset();
  103. /**
  104. * A basic symmetric pad/unpad test.
  105. *
  106. * @return <code>true</code> if the implementation passes a basic symmetric
  107. * self-test. Returns <code>false</code> otherwise.
  108. */
  109. boolean selfTest();
  110. }