Ejemplo n.º 1
0
  public void init(Map attributes) {
    HashMap sha_attr = new HashMap();
    HashMap md5_attr = new HashMap();
    byte[] secret = (byte[]) attributes.get(SECRET);
    if (secret != null) {
      int l = (secret.length >>> 1) + (secret.length & 1);
      byte[] s1 = Util.trim(secret, 0, l);
      byte[] s2 = Util.trim(secret, secret.length - l, l);
      md5_attr.put(IMac.MAC_KEY_MATERIAL, s1);
      sha_attr.put(IMac.MAC_KEY_MATERIAL, s2);
      try {
        hmac_md5.init(md5_attr);
        hmac_sha.init(sha_attr);
      } catch (InvalidKeyException ike) {
        throw new Error(ike.toString());
      }
    } else if (!init) {
      throw new IllegalArgumentException("no secret supplied");
    }
    // else re-use

    byte[] seeed = (byte[]) attributes.get(SEED);
    if (seeed != null) {
      seed = (byte[]) seeed.clone();
    } else if (!init) {
      throw new IllegalArgumentException("no seed supplied");
    }
    // else re-use

    // A(0) is the seed, A(1) = HMAC_hash(secret, A(0)).
    hmac_md5.update(seed, 0, seed.length);
    md5_a = hmac_md5.digest();
    hmac_md5.reset();
    hmac_sha.update(seed, 0, seed.length);
    sha_a = hmac_sha.digest();
    hmac_sha.reset();
    fillBuffer();
    init = true;
  }
  private IMac getMac(char[] password) throws MalformedKeyringException {
    if (!properties.containsKey("salt")) {
      throw new MalformedKeyringException("no salt");
    }
    byte[] salt = Util.toBytesFromString(properties.get("salt"));
    IMac mac = MacFactory.getInstance(properties.get("mac"));
    if (mac == null) {
      throw new MalformedKeyringException("no such mac: " + properties.get("mac"));
    }
    int keylen = mac.macSize();
    int maclen = 0;
    if (!properties.containsKey("maclen")) {
      throw new MalformedKeyringException("no MAC length");
    }
    try {
      maclen = Integer.parseInt(properties.get("maclen"));
    } catch (NumberFormatException nfe) {
      throw new MalformedKeyringException("bad MAC length");
    }

    HashMap pbAttr = new HashMap();
    pbAttr.put(IPBE.PASSWORD, password);
    pbAttr.put(IPBE.SALT, salt);
    pbAttr.put(IPBE.ITERATION_COUNT, ITERATION_COUNT);
    IRandom kdf = PRNGFactory.getInstance("PBKDF2-HMAC-SHA");
    kdf.init(pbAttr);

    byte[] dk = new byte[keylen];
    try {
      kdf.nextBytes(dk, 0, keylen);
    } catch (LimitReachedException shouldNotHappen) {
      throw new Error(shouldNotHappen.toString());
    }

    HashMap macAttr = new HashMap();
    macAttr.put(IMac.MAC_KEY_MATERIAL, dk);
    macAttr.put(IMac.TRUNCATED_SIZE, new Integer(maclen));
    try {
      mac.init(macAttr);
    } catch (InvalidKeyException shouldNotHappen) {
      throw new Error(shouldNotHappen.toString());
    }
    return mac;
  }
Ejemplo n.º 3
0
  public void test(TestHarness harness) {
    harness.checkPoint("TestOfTMMH16");

    /*
    KEY_LENGTH: 10
    TAG_LENGTH: 2
    key: { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc }
    message: { 0xca, 0xfe, 0xba, 0xbe, 0xba, 0xde }
    output: { 0x9d, 0x6a }
    */
    try {
      attributes.clear();
      keystream = new DummyKeystream();
      keystream.init(null);

      output = new byte[] {(byte) 0x9d, (byte) 0x6a};
      mac = new TMMH16();
      attributes.put(TMMH16.KEYSTREAM, keystream);
      attributes.put(TMMH16.TAG_LENGTH, new Integer(2));
      mac.init(attributes);
      message =
          new byte[] {(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe, (byte) 0xba, (byte) 0xde};
      for (int i = 0; i < message.length; i++) {
        mac.update(message[i]);
      }
      result = mac.digest();
      harness.check(Arrays.equals(result, output), "testVector1");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfTMMH16.testVector1");
    }

    /*
    KEY_LENGTH: 10
    TAG_LENGTH: 2
    key: { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc }
    message: { 0xca, 0xfe, 0xba }
    output: { 0xc8, 0x8e }
    */
    try {
      attributes.clear();
      keystream = new DummyKeystream();
      keystream.init(null);

      output = new byte[] {(byte) 0xc8, (byte) 0x8e};
      mac = new TMMH16();
      attributes.put(TMMH16.KEYSTREAM, keystream);
      attributes.put(TMMH16.TAG_LENGTH, new Integer(2));
      mac.init(attributes);
      message = new byte[] {(byte) 0xca, (byte) 0xfe, (byte) 0xba};
      for (int i = 0; i < message.length; i++) {
        mac.update(message[i]);
      }
      result = mac.digest();
      harness.check(Arrays.equals(result, output), "testVector2");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfTMMH16.testVector2");
    }

    /*
    KEY_LENGTH: 10
    TAG_LENGTH: 4
    key: { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc }
    message: { 0xca, 0xfe, 0xba, 0xbe, 0xba, 0xde }
    output: { 0x9d, 0x6a, 0xc0, 0xd3 }
    */
    try {
      attributes.clear();
      keystream = new DummyKeystream();
      keystream.init(null);

      output = new byte[] {(byte) 0x9d, (byte) 0x6a, (byte) 0xc0, (byte) 0xd3};
      mac = new TMMH16();
      attributes.put(TMMH16.KEYSTREAM, keystream);
      attributes.put(TMMH16.TAG_LENGTH, new Integer(4));
      mac.init(attributes);
      message =
          new byte[] {(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe, (byte) 0xba, (byte) 0xde};
      for (int i = 0; i < message.length; i++) {
        mac.update(message[i]);
      }
      result = mac.digest();
      harness.check(Arrays.equals(result, output), "testVector3");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfTMMH16.testVector3");
    }
  }