NoekeonVects.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. NoekeonVects.java - Generate Noekeon test vectors using BouncyCastle.
  3. Written in 2011 by Patrick Pelletier <code@funwithsoftware.org>
  4. To the extent possible under law, the author(s) have dedicated all
  5. copyright and related and neighboring rights to this software to
  6. the public domain worldwide. This software is distributed without
  7. any warranty.
  8. This file is dedicated to the public domain with the CC0 Public Domain
  9. Dedication: http://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
  10. You may also consider this file to be covered by the WTFPL, as contained
  11. in the LibTomCrypt LICENSE file, if that makes you happier for some reason.
  12. ----------------------------------------------------------------------
  13. This program was inspired by the comment in Botan 1.10.1's
  14. doc/examples/eax_test.cpp:
  15. // Noekeon: unknown cause, though LTC's lone test vector does not
  16. // match Botan
  17. So, I investigated the discrepancy by comparing them with a third
  18. implementation, BouncyCastle: http://www.bouncycastle.org/java.html
  19. I determined that there are two reasons why LibTomCrypt's Noekeon does
  20. not match Botan:
  21. 1) Botan uses "indirect Noekeon" (with a key schedule), while
  22. LibTomCrypt and BouncyCastle both use "direct Noekeon" (without
  23. a key schedule). See slide 14 of
  24. http://gro.noekeon.org/Noekeon-slides.pdf
  25. 2) However, LibTomCrypt's direct Noekeon still does not match
  26. BouncyCastle's direct Noekeon. This is because of a bug in
  27. LibTomCrypt's PI1 and PI2 functions:
  28. https://github.com/libtom/libtomcrypt/issues/5
  29. This program uses BouncyCastle to produce test vectors which are
  30. suitable for Botan (by explicitly scheduling the key, thus
  31. building indirect Noekeon out of BouncyCastle's direct Noekeon),
  32. and also produces test vectors which would be suitable for
  33. LibTomCrypt (direct Noekeon) once its PI1 and PI2 functions are
  34. fixed to match the Noekeon specification.
  35. Although this program uses a PRNG from BouncyCastle to generate
  36. data for the test vectors, it uses a fixed seed and thus will
  37. produce the same output every time it is run.
  38. */
  39. import java.io.ByteArrayOutputStream;
  40. import java.io.IOException;
  41. import java.util.Locale;
  42. import org.bouncycastle.crypto.digests.RIPEMD128Digest;
  43. import org.bouncycastle.crypto.engines.NoekeonEngine;
  44. import org.bouncycastle.crypto.modes.EAXBlockCipher;
  45. import org.bouncycastle.crypto.params.AEADParameters;
  46. import org.bouncycastle.crypto.params.KeyParameter;
  47. import org.bouncycastle.crypto.prng.DigestRandomGenerator;
  48. import org.bouncycastle.util.encoders.HexEncoder;
  49. public class NoekeonVects
  50. {
  51. private final DigestRandomGenerator r =
  52. new DigestRandomGenerator(new RIPEMD128Digest());
  53. private final HexEncoder h = new HexEncoder();
  54. private final NoekeonEngine noekeon = new NoekeonEngine();
  55. private final KeyParameter null_key = new KeyParameter(new byte[16]);
  56. private final boolean schedule_key;
  57. private final boolean botan_format;
  58. private byte[] randomBytes(int n)
  59. {
  60. byte[] b = new byte[n];
  61. r.nextBytes(b);
  62. return b;
  63. }
  64. private void hexOut(byte[] b) throws IOException
  65. {
  66. // HexEncoder uses lowercase, and Botan's test vectors must
  67. // be in uppercase, so...
  68. ByteArrayOutputStream os = new ByteArrayOutputStream();
  69. h.encode(b, 0, b.length, os);
  70. String s = os.toString("US-ASCII");
  71. System.out.print(s.toUpperCase(Locale.US));
  72. }
  73. private void printCArray(byte[] a) throws IOException
  74. {
  75. byte[] b = new byte[1];
  76. for (int i = 0; i < a.length; i++)
  77. {
  78. if (i > 0)
  79. System.out.print(", ");
  80. System.out.print("0x");
  81. b[0] = a[i];
  82. hexOut(b);
  83. }
  84. }
  85. private void printVector(byte[] key, byte[] plaintext, byte[] ciphertext)
  86. throws IOException
  87. {
  88. if (botan_format)
  89. {
  90. hexOut(plaintext);
  91. System.out.print(":");
  92. hexOut(ciphertext);
  93. System.out.println(":\\");
  94. hexOut(key);
  95. System.out.println();
  96. }
  97. else
  98. {
  99. System.out.println(" {");
  100. System.out.println(" 16,");
  101. System.out.print(" { ");
  102. printCArray (key);
  103. System.out.println(" },");
  104. System.out.print(" { ");
  105. printCArray (plaintext);
  106. System.out.println(" },");
  107. System.out.print(" { ");
  108. printCArray (ciphertext);
  109. System.out.println(" }");
  110. System.out.println(" },");
  111. }
  112. }
  113. private KeyParameter maybe_schedule_key(byte[] key)
  114. {
  115. if (schedule_key)
  116. {
  117. noekeon.init(true, null_key);
  118. byte[] scheduled = new byte[16];
  119. noekeon.processBlock(key, 0, scheduled, 0);
  120. return new KeyParameter(scheduled);
  121. }
  122. else
  123. return new KeyParameter(key);
  124. }
  125. private byte[] encrypt(byte[] plaintext, byte[] key)
  126. {
  127. KeyParameter kp = maybe_schedule_key(key);
  128. noekeon.init(true, kp);
  129. byte[] ciphertext = new byte[16];
  130. noekeon.processBlock(plaintext, 0, ciphertext, 0);
  131. return ciphertext;
  132. }
  133. public NoekeonVects(long seed, boolean schedule_key, boolean botan_format)
  134. {
  135. this.schedule_key = schedule_key;
  136. this.botan_format = botan_format;
  137. r.addSeedMaterial(seed);
  138. }
  139. public void ecb_vectors() throws IOException
  140. {
  141. for (int i = 0; i < 8; i++)
  142. {
  143. byte[] key = randomBytes(16);
  144. byte[] plaintext = randomBytes(16);
  145. byte[] ciphertext = encrypt(plaintext, key);
  146. printVector(key, plaintext, ciphertext);
  147. }
  148. }
  149. public void eax_vectors() throws Exception
  150. {
  151. System.out.println("EAX-noekeon (16 byte key)");
  152. EAXBlockCipher eax = new EAXBlockCipher(new NoekeonEngine());
  153. byte[] output = new byte[48];
  154. byte[] tag = new byte[16];
  155. for (int j = 0; j < 16; j++)
  156. tag[j] = (byte) j;
  157. for (int i = 0; i <= 32; i++)
  158. {
  159. byte[] header_nonce_plaintext = new byte[i];
  160. for (int j = 0; j < i; j++)
  161. header_nonce_plaintext[j] = (byte) j;
  162. AEADParameters params =
  163. new AEADParameters(maybe_schedule_key(tag),
  164. 128,
  165. header_nonce_plaintext,
  166. header_nonce_plaintext);
  167. eax.init(true, params);
  168. int off = eax.processBytes(header_nonce_plaintext, 0, i,
  169. output, 0);
  170. off += eax.doFinal(output, off);
  171. if (off != i + 16)
  172. throw new RuntimeException("didn't expect that");
  173. byte[] ciphertext = new byte[i];
  174. for (int j = 0; j < i; j++)
  175. ciphertext[j] = output[j];
  176. for (int j = 0; j < 16; j++)
  177. tag[j] = output[i + j];
  178. System.out.print(i < 10 ? " " : " ");
  179. System.out.print(i);
  180. System.out.print(": ");
  181. hexOut(ciphertext);
  182. System.out.print(", ");
  183. hexOut(tag);
  184. System.out.println();
  185. }
  186. }
  187. public static void main(String[] argv) throws Exception
  188. {
  189. NoekeonVects bot = new NoekeonVects(0xdefacedbadfacadeL, true, true);
  190. NoekeonVects tom = new NoekeonVects(0xdefacedbadfacadeL, false, false);
  191. System.out.println("# ECB vectors for indirect Noekeon, in Botan's");
  192. System.out.println("# test vector format, suitable for insertion");
  193. System.out.println("# into Botan's file checks/validate.dat");
  194. System.out.println("# Block cipher format is plaintext:ciphertext:key");
  195. bot.ecb_vectors();
  196. System.out.println();
  197. System.out.println("/* ECB vectors for direct Noekeon, as C arrays");
  198. System.out.println(" * suitable for insertion into LibTomCrypt's");
  199. System.out.println(" * noekeon_test() in src/ciphers/noekeon.c,");
  200. System.out.println(" * once LTC's PI1/PI2 bug is fixed. */");
  201. tom.ecb_vectors();
  202. System.out.println();
  203. System.out.println("# EAX vectors for indirect Noekeon, in the format");
  204. System.out.println("# generated by LTC's demos/tv_gen.c and consumed");
  205. System.out.println("# by Botan's doc/examples/eax_test.cpp, suitable");
  206. System.out.println("# for insertion in Botan's doc/examples/eax.vec");
  207. bot.eax_vectors();
  208. System.out.println();
  209. System.out.println("# EAX vectors for direct Noekeon, in the format");
  210. System.out.println("# generated by LTC's demos/tv_gen.c and consumed");
  211. System.out.println("# by Botan's doc/examples/eax_test.cpp, which");
  212. System.out.println("# should match LTC's notes/eax_tv.txt, once");
  213. System.out.println("# LTC's PI1/PI2 bug is fixed.");
  214. tom.eax_vectors();
  215. System.out.flush();
  216. }
  217. }