EAX.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* EAX.java --
  2. Copyright (C) 2004, 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. import gnu.javax.crypto.mac.IMac;
  35. import gnu.javax.crypto.mac.MacFactory;
  36. import java.security.InvalidKeyException;
  37. import java.util.Arrays;
  38. import java.util.Collections;
  39. import java.util.HashMap;
  40. import java.util.Iterator;
  41. import java.util.Map;
  42. /**
  43. * A conventional two-pass authenticated-encrypted mode, EAX. EAX is a
  44. * <i>Authenticated Encryption with Additional Data</i> (<b>AEAD</b>) scheme,
  45. * which provides protection and authentication for the message, and provides
  46. * authentication of an (optional) header. EAX is composed of the counter mode
  47. * (CTR) and the one-key CBC MAC (OMAC).
  48. * <p>
  49. * This class makes full use of the {@link IAuthenticatedMode} interface, that
  50. * is, all methods of both {@link IMode} and {@link IMac} can be used as
  51. * specified in the {@link IAuthenticatedMode} interface.
  52. * <p>
  53. * References:
  54. * <ol>
  55. * <li>M. Bellare, P. Rogaway, and D. Wagner; <a
  56. * href="http://www.cs.berkeley.edu/~daw/papers/eprint-short-ae.pdf">A
  57. * Conventional Authenticated-Encryption Mode</a>.</li>
  58. * </ol>
  59. */
  60. public class EAX
  61. implements IAuthenticatedMode
  62. {
  63. /** The tag size, in bytes. */
  64. private int tagSize;
  65. /** The nonce OMAC instance. */
  66. private IMac nonceOmac;
  67. /** The header OMAC instance. */
  68. private IMac headerOmac;
  69. /** The message OMAC instance. */
  70. private IMac msgOmac;
  71. /** The CTR instance. */
  72. private IMode ctr;
  73. /** The direction state (encrypting or decrypting). */
  74. private int state;
  75. /** Whether we're initialized or not. */
  76. private boolean init;
  77. /** The cipher block size. */
  78. private int cipherBlockSize;
  79. /** The cipher. */
  80. private IBlockCipher cipher;
  81. /** The [t]_n array. */
  82. private byte[] t_n;
  83. private static boolean valid = false;
  84. public EAX(IBlockCipher cipher, int cipherBlockSize)
  85. {
  86. this.cipher = cipher;
  87. this.cipherBlockSize = cipherBlockSize;
  88. String name = cipher.name();
  89. int i = name.indexOf('-');
  90. if (i >= 0)
  91. name = name.substring(0, i);
  92. String omacname = Registry.OMAC_PREFIX + name;
  93. nonceOmac = MacFactory.getInstance(omacname);
  94. headerOmac = MacFactory.getInstance(omacname);
  95. msgOmac = MacFactory.getInstance(omacname);
  96. ctr = ModeFactory.getInstance(Registry.CTR_MODE, cipher, cipherBlockSize);
  97. t_n = new byte[cipherBlockSize];
  98. init = false;
  99. }
  100. public Object clone()
  101. {
  102. return new EAX((IBlockCipher) cipher.clone(), cipherBlockSize);
  103. }
  104. public String name()
  105. {
  106. return Registry.EAX_MODE + "(" + cipher.name() + ")";
  107. }
  108. public int defaultBlockSize()
  109. {
  110. return ctr.defaultBlockSize();
  111. }
  112. public int defaultKeySize()
  113. {
  114. return ctr.defaultKeySize();
  115. }
  116. public Iterator blockSizes()
  117. {
  118. return ctr.blockSizes();
  119. }
  120. public Iterator keySizes()
  121. {
  122. return ctr.keySizes();
  123. }
  124. public void init(Map attrib) throws InvalidKeyException
  125. {
  126. byte[] nonce = (byte[]) attrib.get(IV);
  127. if (nonce == null)
  128. throw new IllegalArgumentException("no nonce provided");
  129. byte[] key = (byte[]) attrib.get(KEY_MATERIAL);
  130. if (key == null)
  131. throw new IllegalArgumentException("no key provided");
  132. Arrays.fill(t_n, (byte) 0);
  133. nonceOmac.reset();
  134. nonceOmac.init(Collections.singletonMap(MAC_KEY_MATERIAL, key));
  135. nonceOmac.update(t_n, 0, t_n.length);
  136. nonceOmac.update(nonce, 0, nonce.length);
  137. byte[] N = nonceOmac.digest();
  138. nonceOmac.reset();
  139. nonceOmac.update(t_n, 0, t_n.length);
  140. nonceOmac.update(nonce, 0, nonce.length);
  141. t_n[t_n.length - 1] = 1;
  142. headerOmac.reset();
  143. headerOmac.init(Collections.singletonMap(MAC_KEY_MATERIAL, key));
  144. headerOmac.update(t_n, 0, t_n.length);
  145. t_n[t_n.length - 1] = 2;
  146. msgOmac.reset();
  147. msgOmac.init(Collections.singletonMap(MAC_KEY_MATERIAL, key));
  148. msgOmac.update(t_n, 0, t_n.length);
  149. Integer modeSize = (Integer) attrib.get(MODE_BLOCK_SIZE);
  150. if (modeSize == null)
  151. modeSize = Integer.valueOf(cipherBlockSize);
  152. HashMap ctrAttr = new HashMap();
  153. ctrAttr.put(KEY_MATERIAL, key);
  154. ctrAttr.put(IV, N);
  155. ctrAttr.put(STATE, Integer.valueOf(ENCRYPTION));
  156. ctrAttr.put(MODE_BLOCK_SIZE, modeSize);
  157. ctr.reset();
  158. ctr.init(ctrAttr);
  159. Integer st = (Integer) attrib.get(STATE);
  160. if (st != null)
  161. {
  162. state = st.intValue();
  163. if (state != ENCRYPTION && state != DECRYPTION)
  164. throw new IllegalArgumentException("invalid state");
  165. }
  166. else
  167. state = ENCRYPTION;
  168. Integer ts = (Integer) attrib.get(TRUNCATED_SIZE);
  169. if (ts != null)
  170. tagSize = ts.intValue();
  171. else
  172. tagSize = cipherBlockSize;
  173. if (tagSize < 0 || tagSize > cipherBlockSize)
  174. throw new IllegalArgumentException("tag size out of range");
  175. init = true;
  176. }
  177. public int currentBlockSize()
  178. {
  179. return ctr.currentBlockSize();
  180. }
  181. public void encryptBlock(byte[] in, int inOff, byte[] out, int outOff)
  182. {
  183. if (! init)
  184. throw new IllegalStateException("not initialized");
  185. if (state != ENCRYPTION)
  186. throw new IllegalStateException("not encrypting");
  187. ctr.update(in, inOff, out, outOff);
  188. msgOmac.update(out, outOff, ctr.currentBlockSize());
  189. }
  190. public void decryptBlock(byte[] in, int inOff, byte[] out, int outOff)
  191. {
  192. if (! init)
  193. throw new IllegalStateException("not initialized");
  194. if (state != DECRYPTION)
  195. throw new IllegalStateException("not decrypting");
  196. msgOmac.update(in, inOff, ctr.currentBlockSize());
  197. ctr.update(in, inOff, out, outOff);
  198. }
  199. public void update(byte[] in, int inOff, byte[] out, int outOff)
  200. {
  201. switch (state)
  202. {
  203. case ENCRYPTION:
  204. encryptBlock(in, inOff, out, outOff);
  205. break;
  206. case DECRYPTION:
  207. decryptBlock(in, inOff, out, outOff);
  208. break;
  209. default:
  210. throw new IllegalStateException("impossible state " + state);
  211. }
  212. }
  213. public void reset()
  214. {
  215. nonceOmac.reset();
  216. headerOmac.reset();
  217. msgOmac.reset();
  218. ctr.reset();
  219. }
  220. public boolean selfTest()
  221. {
  222. return true; // XXX
  223. }
  224. public int macSize()
  225. {
  226. return tagSize;
  227. }
  228. public byte[] digest()
  229. {
  230. byte[] tag = new byte[tagSize];
  231. digest(tag, 0);
  232. return tag;
  233. }
  234. public void digest(byte[] out, int outOffset)
  235. {
  236. if (outOffset < 0 || outOffset + tagSize > out.length)
  237. throw new IndexOutOfBoundsException();
  238. byte[] N = nonceOmac.digest();
  239. byte[] H = headerOmac.digest();
  240. byte[] M = msgOmac.digest();
  241. for (int i = 0; i < tagSize; i++)
  242. out[outOffset + i] = (byte)(N[i] ^ H[i] ^ M[i]);
  243. reset();
  244. }
  245. public void update(byte b)
  246. {
  247. if (! init)
  248. throw new IllegalStateException("not initialized");
  249. headerOmac.update(b);
  250. }
  251. public void update(byte[] buf, int off, int len)
  252. {
  253. if (! init)
  254. throw new IllegalStateException("not initialized");
  255. headerOmac.update(buf, off, len);
  256. }
  257. }