Example #1
0
  protected void engineTest() throws Exception {
    setExpectedPasses(7);

    String[][] data = {
      //    data, md
      //  ......................
      {"", "8350E5A3E24C153DF2275C9F80692773"}, // A.5 1
      {"a", "32EC01EC4A6DAC72C0AB96FB34C0B5D1"}, // A.5 2
      {"abc", "DA853B0D3F88D99B30283A69E6DED6BB"}, // A.5 3
      {"message digest", "AB4F496BFB2A530B219FF33031FE06B0"}, // A.5 4
      {"abcdefghijklmnopqrstuvwxyz", "4E8DDFF3650292AB5A4108C3AA47940B"}, // A.5 5
      {
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
        "DA33DEF2A42DF13975352846C30338CD"
      }, // A.5 6
      {
        "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
        "D5976F79D83D3A0DC9806C3C66F3EFD8"
      } // A.5 7
    };

    MessageDigest alg = MessageDigest.getInstance("MD2", "Cryptix");

    for (int i = 0; i < data.length; i++) {
      String a = Hex.toString(alg.digest(data[i][0].getBytes()));
      out.println("  test vector: " + data[i][0]);
      out.println("  computed md: " + a);
      out.println(" certified md: " + data[i][1]);
      passIf(a.equals(data[i][1]), "MD2 #" + (i + 1));
    }
  }
Example #2
0
  private static void test(
      PrintWriter out, int rounds, String keyStr, String plainStr, String cipherStr)
      throws Exception {
    // Note that the paper uses certification data that is indexed
    // from RIGHT to LEFT, i.e., 7, 6, 5, 4, 3, 2, 1, 0.
    byte keyBytes[] = Hex.fromReversedString(keyStr);
    byte plain[] = Hex.fromReversedString(plainStr);
    byte cipher[] = Hex.fromReversedString(cipherStr);

    SPEED speed = new SPEED();
    speed.setBlockSize(plain.length);
    speed.setRounds(rounds);
    Key key = new RawSecretKey("SPEED", keyBytes);

    speed.initEncrypt(key);
    byte encP[] = speed.crypt(plain);
    String a, b;
    out.println("    key:" + Hex.toString(keyBytes));
    out.println("  plain:" + Hex.toString(plain));
    out.println("    enc:" + (a = Hex.toString(encP)));
    b = Hex.toString(cipher);
    if (a.equals(b)) out.print("encryption good; ");
    else {
      out.println("   calc:" + b);
      out.println(" ********* SPEED ENCRYPTION FAILED ********* ");
      speed.dump();
    }

    speed.initDecrypt(key);
    byte[] decC = speed.crypt(encP);
    a = Hex.toString(decC);
    b = Hex.toString(plain);
    if (a.equals(b)) out.println("decryption good");
    else {
      out.println();
      out.println("    enc:" + Hex.toString(encP));
      out.println("    dec:" + a);
      out.println("   calc:" + b);
      out.println(" ********* SPEED DECRYPTION FAILED ********* ");
      speed.dump();
    }
  }
Example #3
0
  private void test1() throws Exception {
    byte[] ect, dct;
    String a, b;
    int i;

    for (i = 0; i < testData1.length; i++) {
      Cipher alg = Cipher.getInstance("SPEED", "Cryptix");
      ((Parameterized) alg).setParameter("rounds", new Integer(testData1[i][0]));
      ((Parameterized) alg).setParameter("blockSize", new Integer(testData1[i][2].length() / 2));
      RawSecretKey key = new RawSecretKey("SPEED", Hex.fromString(testData1[i][1]));

      alg.initEncrypt(key);
      ect = alg.crypt(Hex.fromString(testData1[i][2]));
      a = Hex.toString(ect);
      alg.initDecrypt(key);
      dct = alg.crypt(ect);
      b = Hex.toString(dct);

      out.println("     plain:  " + testData1[i][2]);
      out.println("     cipher: " + a);
      out.println("     cert:   " + testData1[i][3]);

      passIf(a.equals(testData1[i][3]), " *** SPEED encrypt");

      out.println("     cipher: " + Hex.toString(ect));
      out.println("     plain:  " + b);
      out.println("     cert:   " + testData1[i][2]);

      passIf(b.equals(testData1[i][2]), " *** SPEED decrypt");

      //          // print reversed data for replacing/comparing in the data above
      //          out.println("\n  \"" +
      //            Hex.toString(Hex.fromReversedString(testData1[i][1])) + "\"");
      //          out.println("  \"" +
      //            Hex.toString(Hex.fromReversedString(testData1[i][2])) + "\"");
      //          out.println("  \"" +
      //            Hex.toString(Hex.fromReversedString(testData1[i][3])) + "\"\n");
    }
  }
Example #4
0
  void dump() {
    if (0 == data_bits) {
      err.println("no data set yet");
      return;
    }

    err.println("KEY SCHEDULE");
    err.println(" data_bits " + data_bits);
    err.println(" kb_bits " + kb_bits);
    err.println(" kb.length " + kb.length);
    err.println(" f_wd_mask " + Hex.intToString(f_wd_mask));
    err.println(" h_wd_mask " + Hex.intToString(h_wd_mask));
    err.println(" v_shift " + v_shift);

    err.println(" double byte buffer");
    for (int i = 0; i < key_len_dbyte; i++) {
      err.print(" " + Hex.intToString(kb[i]));
    }
    err.println();

    switch (data_bits) {
      case 256:
        for (int i = 0; i < kb_bits / 2; i++) err.print(" " + Hex.intToString(round_key[i]));
        break;

      case 128:
        for (int i = 0; i < kb_bits; i++) err.print(" " + Hex.shortToString(round_key[i]));
        break;

      case 64:
        for (int i = 0; i < kb_bits * 2; i++) {
          err.print(" " + Hex.byteToString(round_key[i]));
        }
        break;

      default:
        throw new CryptixException("SPEED: data_bits=" + data_bits + " illegal in key_schedule?");
    }
    err.println();
  }
Example #5
0
  /** Encrypts a block. */
  private void encrypt(int in[], int out[]) {
    int t0 = in[0],
        t1 = in[1],
        t2 = in[2],
        t3 = in[3],
        t4 = in[4],
        t5 = in[5],
        t6 = in[6],
        t7 = in[7];

    int k = 0; /* points to the first round key */
    int quarter_rounds = rounds / 4;

    //
    // In the following 4 passes, only the first assignment
    // changes, which is the nonlinear function.
    //
    /* Pass 1 uses FF1 */
    for (int i = 0; i < quarter_rounds; i++) {
      int temp = ((t6) & (t3)) ^ ((t5) & (t1)) ^ ((t4) & (t2)) ^ ((t1) & (t0)) ^ (t0);

      int vv = (((temp >>> h_wd_len) + temp) & h_wd_mask) >>> v_shift;
      //
      //    rotate_data_right(t7, h_wd_len - 1)
      //    + rotate_data_right(temp, vv)
      //    + round_key[k++];
      //
      t7 &= f_wd_mask; // not re-used
      int rot1 = (t7 >>> (h_wd_len - 1)) | (t7 << (f_wd_len - (h_wd_len - 1)));

      temp &= f_wd_mask; // not re-used
      int rot2 = (temp >>> vv) | (temp << (f_wd_len - vv));

      temp = rot1 + rot2 + round_key[k++];

      t7 = t6;
      t6 = t5;
      t5 = t4;
      t4 = t3;
      t3 = t2;
      t2 = t1;
      t1 = t0;
      t0 = temp & f_wd_mask;
    }

    if (DEBUG_SLOW && debuglevel >= 5) {
      debug(
          "PASS 1: "
              + Hex.intToString(t7)
              + " "
              + Hex.intToString(t6)
              + " "
              + Hex.intToString(t5)
              + " "
              + Hex.intToString(t4)
              + " "
              + Hex.intToString(t3)
              + " "
              + Hex.intToString(t2)
              + " "
              + Hex.intToString(t1)
              + " "
              + Hex.intToString(t0)
              + " ");
    }

    /* Pass 2 uses FF2 */
    for (int i = 0; i < quarter_rounds; i++) {
      int temp =
          ((t6) & (t4) & (t0))
              ^ ((t4) & (t3) & (t0))
              ^ ((t5) & (t2))
              ^ ((t4) & (t3))
              ^ ((t4) & (t1))
              ^ ((t3) & (t0))
              ^ (t1);

      int vv = (((temp >>> h_wd_len) + temp) & h_wd_mask) >>> v_shift;
      //
      //    rotate_data_right(t7, h_wd_len - 1)
      //    + rotate_data_right(temp, vv)
      //    + round_key[k++];
      //
      t7 &= f_wd_mask; // not re-used
      int rot1 = (t7 >>> (h_wd_len - 1)) | (t7 << (f_wd_len - (h_wd_len - 1)));

      temp &= f_wd_mask; // not re-used
      int rot2 = (temp >>> vv) | (temp << (f_wd_len - vv));

      temp = rot1 + rot2 + round_key[k++];

      t7 = t6;
      t6 = t5;
      t5 = t4;
      t4 = t3;
      t3 = t2;
      t2 = t1;
      t1 = t0;
      t0 = temp & f_wd_mask;
    }

    if (DEBUG_SLOW && debuglevel >= 5) {
      debug(
          "PASS 2: "
              + Hex.intToString(t7)
              + " "
              + Hex.intToString(t6)
              + " "
              + Hex.intToString(t5)
              + " "
              + Hex.intToString(t4)
              + " "
              + Hex.intToString(t3)
              + " "
              + Hex.intToString(t2)
              + " "
              + Hex.intToString(t1)
              + " "
              + Hex.intToString(t0)
              + " ");
    }

    /* Pass 3 uses FF3 */
    for (int i = 0; i < quarter_rounds; i++) {
      int temp =
          ((t5) & (t4) & (t0))
              ^ ((t6) & (t4))
              ^ ((t5) & (t2))
              ^ ((t3) & (t0))
              ^ ((t1) & (t0))
              ^ (t3);

      int vv = (((temp >>> h_wd_len) + temp) & h_wd_mask) >>> v_shift;
      //
      //    rotate_data_right(t7, h_wd_len - 1)
      //    + rotate_data_right(temp, vv)
      //    + round_key[k++];
      //
      t7 &= f_wd_mask; // not re-used
      int rot1 = (t7 >>> (h_wd_len - 1)) | (t7 << (f_wd_len - (h_wd_len - 1)));

      temp &= f_wd_mask; // not re-used
      int rot2 = (temp >>> vv) | (temp << (f_wd_len - vv));

      temp = rot1 + rot2 + round_key[k++];

      t7 = t6;
      t6 = t5;
      t5 = t4;
      t4 = t3;
      t3 = t2;
      t2 = t1;
      t1 = t0;
      t0 = temp & f_wd_mask;
    }

    if (DEBUG_SLOW && debuglevel >= 5) {
      debug(
          "PASS 3: "
              + Hex.intToString(t7)
              + " "
              + Hex.intToString(t6)
              + " "
              + Hex.intToString(t5)
              + " "
              + Hex.intToString(t4)
              + " "
              + Hex.intToString(t3)
              + " "
              + Hex.intToString(t2)
              + " "
              + Hex.intToString(t1)
              + " "
              + Hex.intToString(t0)
              + " ");
    }

    /* Pass 4 uses FF4  */
    for (int i = 0; i < quarter_rounds; i++) {
      int temp =
          ((t6) & (t4) & (t2) & (t0))
              ^ ((t6) & (t5))
              ^ ((t4) & (t3))
              ^ ((t3) & (t2))
              ^ ((t1) & (t0))
              ^ (t2);

      int vv = (((temp >>> h_wd_len) + temp) & h_wd_mask) >>> v_shift;
      //
      //    rotate_data_right(t7, h_wd_len - 1)
      //    + rotate_data_right(temp, vv)
      //    + round_key[k++];
      //
      t7 &= f_wd_mask; // not re-used
      int rot1 = (t7 >>> (h_wd_len - 1)) | (t7 << (f_wd_len - (h_wd_len - 1)));

      temp &= f_wd_mask; // not re-used
      int rot2 = (temp >>> vv) | (temp << (f_wd_len - vv));

      temp = rot1 + rot2 + round_key[k++];

      t7 = t6;
      t6 = t5;
      t5 = t4;
      t4 = t3;
      t3 = t2;
      t2 = t1;
      t1 = t0;
      t0 = temp & f_wd_mask;
    }

    if (DEBUG_SLOW && debuglevel >= 5) {
      debug(
          "PASS 4: "
              + Hex.intToString(t7)
              + " "
              + Hex.intToString(t6)
              + " "
              + Hex.intToString(t5)
              + " "
              + Hex.intToString(t4)
              + " "
              + Hex.intToString(t3)
              + " "
              + Hex.intToString(t2)
              + " "
              + Hex.intToString(t1)
              + " "
              + Hex.intToString(t0)
              + " ");
    }

    out[0] = t0;
    out[1] = t1;
    out[2] = t2;
    out[3] = t3;
    out[4] = t4;
    out[5] = t5;
    out[6] = t6;
    out[7] = t7;
  }