public static final void main(String[] args) {
    try {
      IBlockCipher cipher = CipherFactory.getInstance(args[0]);

      int keySize = cipher.defaultKeySize() * 8;
      if (args.length > 1) {
        keySize = Integer.parseInt(args[1]);
      }

      long time = -System.currentTimeMillis();

      set1(cipher, keySize);
      set2(cipher, keySize);
      set3(cipher, keySize);
      set4(cipher, keySize);

      time += System.currentTimeMillis();
      System.out.println();
      System.out.println("End of test vectors");
      System.out.println();
      System.out.println("*** Tests lasted " + time + " ms...");

    } catch (Exception x) {
      x.printStackTrace(System.err);
    }
  }
  private static void set4(IBlockCipher cipher, int keySize) throws InvalidKeyException {
    // this is torture for low-speed CPUs. only execute if global env var
    // TORTURE is set!
    String dummy = System.getProperty("TORTURE");
    if (dummy == null) {
      return;
    }

    int kl = keySize / 8;
    int bl = cipher.defaultBlockSize();
    byte[] k = new byte[kl];
    byte[] p = new byte[bl];
    int j, t, x;
    System.out.println("Test vectors -- set 4");
    System.out.println("=====================");
    System.out.println();
    HashMap map = new HashMap();
    for (int i = 0; i < 4; i++) {
      System.out.println("Set 4, vector#  " + String.valueOf(i) + ":");
      for (j = 0; j < bl; j++) {
        k[j] = (byte) i;
        p[j] = (byte) i;
      }
      for (; j < kl; j++) {
        k[j] = (byte) i;
      }
      System.out.println("                      key=" + Util.toString(k));
      System.out.println("                    plain=" + Util.toString(p));
      x = i;
      for (j = 0; j < 100000000; j++) {
        for (t = 0; t < kl; t++) {
          k[t] = (byte) x;
        }
        map.put(IBlockCipher.KEY_MATERIAL, k);
        cipher.init(map);
        cipher.encryptBlock(p, 0, p, 0);
        cipher.reset();
        x = p[bl - 1] & 0xFF;
      }
      System.out.println("      Iterated 10^8 times=" + Util.toString(p));
      System.out.println();
    }
  }
Ejemplo n.º 3
0
  /** Test cloneability. */
  protected boolean cloneabilityTest() throws Exception {
    int blockSize = cipher.defaultBlockSize();
    int keySize = cipher.defaultKeySize();

    byte[] pt = new byte[blockSize];
    byte[] ct1 = new byte[blockSize];
    byte[] ct2 = new byte[blockSize];
    byte[] kb = new byte[keySize];
    HashMap attributes = new HashMap();
    attributes.put(IBlockCipher.KEY_MATERIAL, kb);

    cipher.reset();
    cipher.init(attributes);

    cipher.encryptBlock(pt, 0, pt, 0);
    IBlockCipher thomas = (IBlockCipher) cipher.clone();
    thomas.init(attributes);
    cipher.encryptBlock(pt, 0, ct1, 0);
    thomas.encryptBlock(pt, 0, ct2, 0);

    return Arrays.equals(ct1, ct2);
  }
 private static void set3(IBlockCipher cipher, int keySize) throws InvalidKeyException {
   String s;
   int kl = keySize / 8;
   int bl = cipher.defaultBlockSize();
   byte[] k = new byte[kl];
   byte[] p = new byte[bl];
   byte[] c = new byte[bl];
   byte[] d = new byte[bl];
   int j;
   System.out.println("Test vectors -- set 3");
   System.out.println("=====================");
   System.out.println();
   cipher.reset();
   HashMap map = new HashMap();
   for (int i = 0; i < 256; i++) {
     s = "   " + String.valueOf(i);
     s = s.substring(s.length() - 3);
     System.out.println("Set 3, vector#" + s + ":");
     for (j = 0; j < bl; j++) {
       k[j] = (byte) i;
       p[j] = (byte) i;
     }
     for (; j < kl; j++) {
       k[j] = (byte) i;
     }
     map.put(IBlockCipher.KEY_MATERIAL, k);
     cipher.init(map);
     System.out.println("                      key=" + Util.toString(k));
     System.out.println("                    plain=" + Util.toString(p));
     cipher.encryptBlock(p, 0, c, 0);
     System.out.println("                   cipher=" + Util.toString(c));
     cipher.decryptBlock(c, 0, d, 0);
     System.out.println("                decrypted=" + Util.toString(d));
     if (!Arrays.equals(p, d)) {
       throw new RuntimeException("Symmetric operation failure...");
     }
     for (j = 1; j < 100; j++) {
       cipher.encryptBlock(c, 0, c, 0);
     }
     System.out.println("       Iterated 100 times=" + Util.toString(c));
     for (j = 100; j < 1000; j++) {
       cipher.encryptBlock(c, 0, c, 0);
     }
     System.out.println("      Iterated 1000 times=" + Util.toString(c));
     System.out.println();
     cipher.reset();
   }
 }