IMac.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* IMac.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.mac;
  32. import java.security.InvalidKeyException;
  33. import java.util.Map;
  34. /**
  35. * The basic visible methods of any MAC (Message Authentication Code) algorithm.
  36. * <p>
  37. * A <i>MAC</i> provides a way to check the integrity of information
  38. * transmitted over, or stored in, an unreliable medium, based on a secret key.
  39. * Typically, <i>MAC</i>s are used between two parties, that share a common
  40. * secret key, in order to validate information transmitted between them.
  41. * <p>
  42. * When a <i>MAC</i> algorithm is based on a cryptographic hash function, it is
  43. * then called to a <i>HMAC</i> (Hashed Message Authentication Code) --see <a
  44. * href="http://www.ietf.org/rfc/rfc-2104.txt">RFC-2104</a>.
  45. * <p>
  46. * Another type of <i>MAC</i> algorithms exist: UMAC or <i>Universal Message
  47. * Authentication Code</i>, described in <a
  48. * href="http://www.ietf.org/internet-drafts/draft-krovetz-umac-01.txt">
  49. * draft-krovetz-umac-01.txt</a>.
  50. * <p>
  51. * With <i>UMAC</i>s, the sender and receiver share a common secret key (the
  52. * <i>MAC</i> key) which determines:
  53. * <ul>
  54. * <li>The key for a <i>universal hash function</i>. This hash function is
  55. * <i>non-cryptographic</i>, in the sense that it does not need to have any
  56. * cryptographic <i>hardness</i> property. Rather, it needs to satisfy some
  57. * combinatorial property, which can be proven to hold without relying on
  58. * unproven hardness assumptions.</li>
  59. * <li>The key for a <i>pseudorandom function</i>. This is where one needs a
  60. * cryptographic hardness assumption. The pseudorandom function may be obtained
  61. * from a <i>block cipher</i> or a <i>cryptographic hash function</i>. </li>
  62. * </ul>
  63. * <p>
  64. * References:
  65. * <ol>
  66. * <li><a href="http://www.ietf.org/rfc/rfc-2104.txt">RFC 2104</a>HMAC:
  67. * Keyed-Hashing for Message Authentication.<br>
  68. * H. Krawczyk, M. Bellare, and R. Canetti.</li>
  69. * <li><a href="http://www.ietf.org/internet-drafts/draft-krovetz-umac-01.txt">
  70. * UMAC</a>: Message Authentication Code using Universal Hashing.<br>
  71. * T. Krovetz, J. Black, S. Halevi, A. Hevia, H. Krawczyk, and P. Rogaway.</li>
  72. * </ol>
  73. */
  74. public interface IMac
  75. {
  76. /**
  77. * Property name of the user-supplied key material. The value associated to
  78. * this property name is taken to be a byte array.
  79. */
  80. String MAC_KEY_MATERIAL = "gnu.crypto.mac.key.material";
  81. /**
  82. * Property name of the desired truncated output size in bytes. The value
  83. * associated to this property name is taken to be an integer. If no value is
  84. * specified in the attributes map at initialisation time, then all bytes of
  85. * the underlying hash algorithm's output are emitted.
  86. * <p>
  87. * This implementation, follows the recommendation of the <i>RFC 2104</i>
  88. * authors; specifically:
  89. * <pre>
  90. * We recommend that the output length t be not less than half the
  91. * length of the hash output (to match the birthday attack bound)
  92. * and not less than 80 bits (a suitable lower bound on the number
  93. * of bits that need to be predicted by an attacker).
  94. * </pre>
  95. */
  96. String TRUNCATED_SIZE = "gnu.crypto.mac.truncated.size";
  97. /**
  98. * Returns the canonical name of this algorithm.
  99. *
  100. * @return the canonical name of this algorithm.
  101. */
  102. String name();
  103. /**
  104. * Returns the output length in bytes of this <i>MAC</i> algorithm.
  105. *
  106. * @return the output length in bytes of this <i>MAC</i> algorithm.
  107. */
  108. int macSize();
  109. /**
  110. * Initialises the algorithm with designated attributes. Permissible names and
  111. * values are described in the class documentation above.
  112. *
  113. * @param attributes a set of name-value pairs that describe the desired
  114. * future instance behaviour.
  115. * @exception InvalidKeyException if the key data is invalid.
  116. * @exception IllegalStateException if the instance is already initialised.
  117. * @see #MAC_KEY_MATERIAL
  118. */
  119. void init(Map attributes) throws InvalidKeyException, IllegalStateException;
  120. /**
  121. * Continues a <i>MAC</i> operation using the input byte.
  122. *
  123. * @param b the input byte to digest.
  124. */
  125. void update(byte b);
  126. /**
  127. * Continues a <i>MAC</i> operation, by filling the buffer, processing data
  128. * in the algorithm's MAC_SIZE-bit block(s), updating the context and count,
  129. * and buffering the remaining bytes in buffer for the next operation.
  130. *
  131. * @param in the input block.
  132. * @param offset start of meaningful bytes in input block.
  133. * @param length number of bytes, in input block, to consider.
  134. */
  135. void update(byte[] in, int offset, int length);
  136. /**
  137. * Completes the <i>MAC</i> by performing final operations such as padding
  138. * and resetting the instance.
  139. *
  140. * @return the array of bytes representing the <i>MAC</i> value.
  141. */
  142. byte[] digest();
  143. /**
  144. * Resets the algorithm instance for re-initialisation and use with other
  145. * characteristics. This method always succeeds.
  146. */
  147. void reset();
  148. /**
  149. * A basic test. Ensures that the MAC of a pre-determined message is equal to
  150. * a known pre-computed value.
  151. *
  152. * @return <code>true</code> if the implementation passes a basic self-test.
  153. * Returns <code>false</code> otherwise.
  154. */
  155. boolean selfTest();
  156. /**
  157. * Returns a clone copy of this instance.
  158. *
  159. * @return a clone copy of this instance.
  160. */
  161. Object clone() throws CloneNotSupportedException;
  162. }