OMAC.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* OMAC.java --
  2. Copyright (C) 2004, 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.mac;
  32. import gnu.java.security.Configuration;
  33. import gnu.java.security.Registry;
  34. import gnu.java.security.util.Util;
  35. import gnu.javax.crypto.cipher.CipherFactory;
  36. import gnu.javax.crypto.cipher.IBlockCipher;
  37. import gnu.javax.crypto.mode.IMode;
  38. import java.security.InvalidKeyException;
  39. import java.util.Arrays;
  40. import java.util.HashMap;
  41. import java.util.Map;
  42. import java.util.logging.Logger;
  43. /**
  44. * The One-Key CBC MAC, OMAC. This message authentication code is based on a
  45. * block cipher in CBC mode.
  46. * <p>
  47. * References:
  48. * <ol>
  49. * <li>Tetsu Iwata and Kaoru Kurosawa, <i><a
  50. * href="http://crypt.cis.ibaraki.ac.jp/omac/docs/omac.pdf">OMAC: One-Key CBC
  51. * MAC</a></i>.</li>
  52. * </ol>
  53. */
  54. public class OMAC
  55. implements IMac
  56. {
  57. private static final Logger log = Configuration.DEBUG ?
  58. Logger.getLogger(OMAC.class.getName()) : null;
  59. private static final byte C1 = (byte) 0x87;
  60. private static final byte C2 = 0x1b;
  61. // Test key for OMAC-AES-128
  62. private static final byte[] KEY0 =
  63. Util.toBytesFromString("2b7e151628aed2a6abf7158809cf4f3c");
  64. // Test MAC for zero-length input.
  65. private static final byte[] DIGEST0 =
  66. Util.toBytesFromString("bb1d6929e95937287fa37d129b756746");
  67. private static Boolean valid;
  68. private final IBlockCipher cipher;
  69. private final String name;
  70. private IMode mode;
  71. private int blockSize;
  72. private int outputSize;
  73. private byte[] Lu, Lu2;
  74. private byte[] M;
  75. private byte[] Y;
  76. private boolean init;
  77. private int index;
  78. public OMAC(IBlockCipher cipher)
  79. {
  80. this.cipher = cipher;
  81. this.name = "OMAC-" + cipher.name();
  82. }
  83. public Object clone()
  84. {
  85. return new OMAC(cipher);
  86. }
  87. public String name()
  88. {
  89. return name;
  90. }
  91. public int macSize()
  92. {
  93. return outputSize;
  94. }
  95. public void init(Map attrib) throws InvalidKeyException
  96. {
  97. HashMap attrib2 = new HashMap();
  98. attrib2.put(IBlockCipher.KEY_MATERIAL, attrib.get(MAC_KEY_MATERIAL));
  99. cipher.reset();
  100. cipher.init(attrib2);
  101. blockSize = cipher.currentBlockSize();
  102. Integer os = (Integer) attrib.get(TRUNCATED_SIZE);
  103. if (os != null)
  104. {
  105. outputSize = os.intValue();
  106. if (outputSize < 0 || outputSize > blockSize)
  107. throw new IllegalArgumentException("truncated size out of range");
  108. }
  109. else
  110. outputSize = blockSize;
  111. byte[] L = new byte[blockSize];
  112. cipher.encryptBlock(L, 0, L, 0);
  113. if (Configuration.DEBUG)
  114. log.fine("L = " + Util.toString(L).toLowerCase());
  115. if (Lu != null)
  116. {
  117. Arrays.fill(Lu, (byte) 0);
  118. if (Lu.length != blockSize)
  119. Lu = new byte[blockSize];
  120. }
  121. else
  122. Lu = new byte[blockSize];
  123. if (Lu2 != null)
  124. {
  125. Arrays.fill(Lu2, (byte) 0);
  126. if (Lu2.length != blockSize)
  127. Lu2 = new byte[blockSize];
  128. }
  129. else
  130. Lu2 = new byte[blockSize];
  131. boolean msb = (L[0] & 0x80) != 0;
  132. for (int i = 0; i < blockSize; i++)
  133. {
  134. Lu[i] = (byte)(L[i] << 1 & 0xFF);
  135. if (i + 1 < blockSize)
  136. Lu[i] |= (byte)((L[i + 1] & 0x80) >> 7);
  137. }
  138. if (msb)
  139. {
  140. if (blockSize == 16)
  141. Lu[Lu.length - 1] ^= C1;
  142. else if (blockSize == 8)
  143. Lu[Lu.length - 1] ^= C2;
  144. else
  145. throw new IllegalArgumentException("unsupported cipher block size: "
  146. + blockSize);
  147. }
  148. if (Configuration.DEBUG)
  149. log.fine("Lu = " + Util.toString(Lu).toLowerCase());
  150. msb = (Lu[0] & 0x80) != 0;
  151. for (int i = 0; i < blockSize; i++)
  152. {
  153. Lu2[i] = (byte)(Lu[i] << 1 & 0xFF);
  154. if (i + 1 < blockSize)
  155. Lu2[i] |= (byte)((Lu[i + 1] & 0x80) >> 7);
  156. }
  157. if (msb)
  158. {
  159. if (blockSize == 16)
  160. Lu2[Lu2.length - 1] ^= C1;
  161. else
  162. Lu2[Lu2.length - 1] ^= C2;
  163. }
  164. if (Configuration.DEBUG)
  165. log.fine("Lu2 = " + Util.toString(Lu2).toLowerCase());
  166. if (M != null)
  167. {
  168. Arrays.fill(M, (byte) 0);
  169. if (M.length != blockSize)
  170. M = new byte[blockSize];
  171. }
  172. else
  173. M = new byte[blockSize];
  174. if (Y != null)
  175. {
  176. Arrays.fill(Y, (byte) 0);
  177. if (Y.length != blockSize)
  178. Y = new byte[blockSize];
  179. }
  180. else
  181. Y = new byte[blockSize];
  182. index = 0;
  183. init = true;
  184. }
  185. public void update(byte b)
  186. {
  187. if (! init)
  188. throw new IllegalStateException("not initialized");
  189. if (index == M.length)
  190. {
  191. process();
  192. index = 0;
  193. }
  194. M[index++] = b;
  195. }
  196. public void update(byte[] buf, int off, int len)
  197. {
  198. if (! init)
  199. throw new IllegalStateException("not initialized");
  200. if (off < 0 || len < 0 || off + len > buf.length)
  201. throw new IndexOutOfBoundsException("size=" + buf.length + "; off=" + off
  202. + "; len=" + len);
  203. for (int i = 0; i < len;)
  204. {
  205. if (index == blockSize)
  206. {
  207. process();
  208. index = 0;
  209. }
  210. int count = Math.min(blockSize - index, len - i);
  211. System.arraycopy(buf, off + i, M, index, count);
  212. index += count;
  213. i += count;
  214. }
  215. }
  216. public byte[] digest()
  217. {
  218. byte[] b = new byte[outputSize];
  219. digest(b, 0);
  220. return b;
  221. }
  222. public void digest(byte[] out, int off)
  223. {
  224. if (! init)
  225. throw new IllegalStateException("not initialized");
  226. if (off < 0 || off + outputSize > out.length)
  227. throw new IndexOutOfBoundsException("size=" + out.length + "; off=" + off
  228. + "; len=" + outputSize);
  229. byte[] T = new byte[blockSize];
  230. byte[] L = Lu;
  231. if (index < blockSize)
  232. {
  233. M[index++] = (byte) 0x80;
  234. while (index < blockSize)
  235. M[index++] = 0;
  236. L = Lu2;
  237. }
  238. for (int i = 0; i < blockSize; i++)
  239. T[i] = (byte)(M[i] ^ Y[i] ^ L[i]);
  240. cipher.encryptBlock(T, 0, T, 0);
  241. System.arraycopy(T, 0, out, off, outputSize);
  242. reset();
  243. }
  244. public void reset()
  245. {
  246. index = 0;
  247. if (Y != null)
  248. Arrays.fill(Y, (byte) 0);
  249. if (M != null)
  250. Arrays.fill(M, (byte) 0);
  251. }
  252. public boolean selfTest()
  253. {
  254. OMAC mac = new OMAC(CipherFactory.getInstance(Registry.AES_CIPHER));
  255. mac.reset();
  256. Map attr = new HashMap();
  257. attr.put(MAC_KEY_MATERIAL, KEY0);
  258. byte[] digest = null;
  259. try
  260. {
  261. mac.init(attr);
  262. digest = mac.digest();
  263. }
  264. catch (Exception x)
  265. {
  266. return false;
  267. }
  268. if (digest == null)
  269. return false;
  270. return Arrays.equals(DIGEST0, digest);
  271. }
  272. private void process()
  273. {
  274. for (int i = 0; i < blockSize; i++)
  275. M[i] = (byte)(M[i] ^ Y[i]);
  276. cipher.encryptBlock(M, 0, Y, 0);
  277. }
  278. }