Changeset 468871f for core


Ignore:
Timestamp:
Dec 13, 2018 2:39:08 PM (18 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
5d06de8
Parents:
6c3c227c
Message:

Crypto: Add HKDF class for LS2 and NTCP2 (proposal 123)
Minor speedup in HMAC256

Location:
core/java/src/net/i2p/crypto
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/crypto/HMAC256Generator.java

    r6c3c227c r468871f  
    22
    33import java.security.GeneralSecurityException;
    4 import java.security.Key;
    54import java.security.MessageDigest;
    65import java.security.NoSuchAlgorithmException;
     6import java.util.Arrays;
    77import javax.crypto.Mac;
     8import javax.crypto.SecretKey;
    89import javax.crypto.spec.SecretKeySpec;
    910
     
    7172     *  Outputs 32 bytes to target starting at targetOffset.
    7273     *
    73      *  @param key 32 bytes
     74     *  @param key first 32 bytes used as the key
    7475     *  @throws UnsupportedOperationException if the JVM does not support it
    7576     *  @throws IllegalArgumentException for bad key or target too small
     
    7980        try {
    8081            Mac mac = Mac.getInstance("HmacSHA256");
    81             Key keyObj = new SecretKeySpec(key, "HmacSHA256");
     82            SecretKey keyObj = new HMACKey(key);
    8283            mac.init(keyObj);
    8384            mac.update(data, offset, length);
     
    110111        releaseTmp(calc);
    111112        return eq;
     113    }
     114
     115    /**
     116     *  Like SecretKeySpec but doesn't copy the key in the construtor, for speed.
     117     *  It still returns a copy in getEncoded(), because Mac relies
     118     *  on that, doesn't work otherwise.
     119     *  First 32 bytes are returned in getEncoded(), data may be longer.
     120     *
     121     *  @since 0.9.38
     122     */
     123    static final class HMACKey implements SecretKey {
     124        private final byte[] _data;
     125
     126        public HMACKey(byte[] data) { _data = data; }
     127
     128        public String getAlgorithm() { return "HmacSHA256"; }
     129        public byte[] getEncoded() { return Arrays.copyOf(_data, 32); }
     130        public String getFormat() { return "RAW"; }
    112131    }
    113132
     
    151170        byte[] rand = new byte[32];
    152171        byte[] data = new byte[LENGTH];
    153         Key keyObj = new SecretKeySpec(rand, "HmacSHA256");
     172        SecretKey keyObj = null;
    154173        SessionKey key = new SessionKey(rand);
    155174
    156175        HMAC256Generator gen = new HMAC256Generator(I2PAppContext.getGlobalContext());
    157176        byte[] result = new byte[32];
    158         javax.crypto.Mac mac;
     177        Mac mac;
    159178        try {
    160             mac = javax.crypto.Mac.getInstance("HmacSHA256");
     179            mac = Mac.getInstance("HmacSHA256");
    161180        } catch (NoSuchAlgorithmException e) {
    162181            System.err.println("Fatal: " + e);
     
    173192            if (!DataHelper.eq(rand, keyBytes))
    174193                System.out.println("secret key in != out");
    175             key = new SessionKey(rand);
    176             gen.calculate(key, data, 0, data.length, result, 0);
     194            gen.calculate(rand, data, 0, data.length, result, 0);
    177195            try {
    178196                mac.init(keyObj);
     
    182200            }
    183201            byte[] result2 = mac.doFinal(data);
    184             if (!DataHelper.eq(result, result2))
    185                 throw new IllegalStateException();
     202            if (!DataHelper.eq(result, result2)) {
     203                throw new IllegalStateException("Mismatch on run " + i + ": result1:\n" +
     204                                                net.i2p.util.HexDump.dump(result) +
     205                                                "result1:\n" +
     206                                                net.i2p.util.HexDump.dump(result2));
     207            }
    186208        }
    187209
     
    189211        System.out.println("Passed");
    190212        System.out.println("BC Test:");
    191         RUNS = 500000;
     213        RUNS = 1000000;
    192214        long start = System.currentTimeMillis();
    193215        for (int i = 0; i < RUNS; i++) {
    194             gen.calculate(key, data, 0, data.length, result, 0);
     216            gen.calculate(rand, data, 0, data.length, result, 0);
    195217        }
    196218        long time = System.currentTimeMillis() - start;
Note: See TracChangeset for help on using the changeset viewer.