Changeset 2487bca


Ignore:
Timestamp:
Nov 30, 2018 3:15:31 PM (16 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
f168918
Parents:
cc4da1b
Message:

Crypto: Change X25519 key classes from Java keys to I2P keys,
in prep for new crypto (Proposal 144)
Add EncType?
Fix PrivateKey? constructor w/ EncType?
Add support to KeyGenerator?

Files:
3 added
2 deleted
7 edited

Legend:

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

    rcc4da1b r2487bca  
    1111
    1212    ELGAMAL("ElGamal"),
    13     EC("EC");
     13    EC("EC"),
     14
     15    /** @since 0.9.38 */
     16    ECIES("ECIES");
    1417
    1518    private final String name;
  • core/java/src/net/i2p/crypto/EncType.java

    rcc4da1b r2487bca  
    77import java.util.Map;
    88
     9import static net.i2p.crypto.x25519.spec.X25519Spec.X25519_SPEC;
    910import net.i2p.data.Hash;
    1011import net.i2p.data.SimpleDataStructure;
     
    3738
    3839    /**  Pubkey 132 bytes; privkey 66 bytes; */
    39     EC_P521(3, 132, 66, EncAlgo.EC, "EC/None/NoPadding", ECConstants.P521_SPEC, "0.9.20");
     40    EC_P521(3, 132, 66, EncAlgo.EC, "EC/None/NoPadding", ECConstants.P521_SPEC, "0.9.20"),
     41
     42    /**
     43     *  Pubkey 32 bytes; privkey 32 bytes
     44     *  @since 0.9.38
     45     */
     46    ECIES_X25519(4, 32, 32, EncAlgo.ECIES, "EC/None/NoPadding", X25519_SPEC, "0.9.38");
    4047
    4148
  • core/java/src/net/i2p/crypto/KeyGenerator.java

    rcc4da1b r2487bca  
    3232import java.util.Collection;
    3333
     34import com.southernstorm.noise.crypto.x25519.Curve25519;
     35
    3436import net.i2p.I2PAppContext;
    3537import net.i2p.crypto.eddsa.EdDSAPrivateKey;
     
    174176    }
    175177
    176     /** Convert a PrivateKey to its corresponding PublicKey
     178    /**
     179     * Convert a PrivateKey to its corresponding PublicKey.
     180     * As of 0.9.38, supports EncTypes
     181     *
    177182     * @param priv PrivateKey object
    178183     * @return the corresponding PublicKey object
     
    180185     */
    181186    public static PublicKey getPublicKey(PrivateKey priv) {
    182         BigInteger a = new NativeBigInteger(1, priv.toByteArray());
    183         BigInteger aalpha = CryptoConstants.elgg.modPow(a, CryptoConstants.elgp);
    184         PublicKey pub = new PublicKey();
    185         try {
    186             pub.setData(SigUtil.rectify(aalpha, PublicKey.KEYSIZE_BYTES));
    187         } catch (InvalidKeyException ike) {
    188             throw new IllegalArgumentException(ike);
    189         }
     187        EncType type = priv.getType();
     188        byte[] data;
     189        switch (type) {
     190          case ELGAMAL_2048:
     191            BigInteger a = new NativeBigInteger(1, priv.toByteArray());
     192            BigInteger aalpha = CryptoConstants.elgg.modPow(a, CryptoConstants.elgp);
     193            try {
     194                data = SigUtil.rectify(aalpha, PublicKey.KEYSIZE_BYTES);
     195            } catch (InvalidKeyException ike) {
     196                throw new IllegalArgumentException(ike);
     197            }
     198            break;
     199
     200          case ECIES_X25519:
     201            data = new byte[32];
     202            Curve25519.eval(data, 0, priv.getData(), null);
     203            break;
     204
     205          default:
     206            throw new IllegalArgumentException("Unsupported algorithm");
     207
     208        }
     209        PublicKey pub = new PublicKey(type, data);
    190210        return pub;
    191211    }
  • core/java/src/net/i2p/data/PrivateKey.java

    rcc4da1b r2487bca  
    5454     */
    5555    public PrivateKey(EncType type, byte data[]) {
    56         super(data);
     56        super();
    5757        _type = type;
     58        if (data == null)
     59            throw new IllegalArgumentException("Data must be specified");
     60        _data = data;
    5861    }
    5962
  • router/java/src/com/southernstorm/noise/protocol/Curve25519DHState.java

    rcc4da1b r2487bca  
    2323package com.southernstorm.noise.protocol;
    2424
    25 import java.security.KeyPair;
    2625import java.util.Arrays;
    2726
    2827import com.southernstorm.noise.crypto.x25519.Curve25519;
    2928
     29import net.i2p.crypto.KeyPair;
    3030import net.i2p.router.transport.crypto.X25519KeyFactory;
    3131
     
    7979        public void generateKeyPair() {
    8080                KeyPair kp = _xdh.getKeys();
    81                 System.arraycopy(kp.getPrivate().getEncoded(), 0, privateKey, 0, 32);
    82                 System.arraycopy(kp.getPublic().getEncoded(), 0, publicKey, 0, 32);
     81                System.arraycopy(kp.getPrivate().getData(), 0, privateKey, 0, 32);
     82                System.arraycopy(kp.getPublic().getData(), 0, publicKey, 0, 32);
    8383                mode = 0x03;
    8484        }
  • router/java/src/net/i2p/router/transport/crypto/X25519KeyFactory.java

    rcc4da1b r2487bca  
    11package net.i2p.router.transport.crypto;
    22
    3 import java.security.KeyPair;
    43import java.util.concurrent.LinkedBlockingQueue;
    54
     
    76
    87import net.i2p.I2PAppContext;
     8import net.i2p.crypto.EncType;
     9import net.i2p.crypto.KeyPair;
     10import net.i2p.data.PrivateKey;
     11import net.i2p.data.PublicKey;
    912import net.i2p.util.I2PThread;
    1013import net.i2p.util.Log;
     
    134137        byte[] pub = new byte[32];
    135138        Curve25519.eval(pub, 0, priv, null);
    136         KeyPair rv = new KeyPair(new X25519PublicKey(pub), new X25519PrivateKey(priv));
     139        KeyPair rv = new KeyPair(new PublicKey(EncType.ECIES_X25519, pub), new PrivateKey(EncType.ECIES_X25519, priv));
    137140        long end = System.currentTimeMillis();
    138141        long diff = end - start;
  • router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java

    rcc4da1b r2487bca  
    99import java.nio.channels.ServerSocketChannel;
    1010import java.nio.channels.SocketChannel;
    11 import java.security.KeyPair;
    1211import java.text.DecimalFormat;
    1312import java.text.NumberFormat;
     
    2827import java.util.concurrent.ConcurrentHashMap;
    2928
     29import net.i2p.crypto.EncType;
     30import net.i2p.crypto.KeyPair;
    3031import net.i2p.crypto.SigType;
    3132import net.i2p.data.Base64;
     
    3334import net.i2p.data.DataHelper;
    3435import net.i2p.data.Hash;
     36import net.i2p.data.PublicKey;
     37import net.i2p.data.PrivateKey;
    3538import net.i2p.data.router.RouterAddress;
    3639import net.i2p.data.router.RouterIdentity;
     
    4952import net.i2p.router.transport.crypto.DHSessionKeyBuilder;
    5053import net.i2p.router.transport.crypto.X25519KeyFactory;
    51 import net.i2p.router.transport.crypto.X25519PublicKey;
    52 import net.i2p.router.transport.crypto.X25519PrivateKey;
    5354import net.i2p.router.util.DecayingHashSet;
    5455import net.i2p.router.util.DecayingBloomFilter;
     
    258259            if (priv == null || priv.length != NTCP2_KEY_LEN) {
    259260                KeyPair keys = xdh.getKeys();
    260                 _ntcp2StaticPrivkey = keys.getPrivate().getEncoded();
    261                 _ntcp2StaticPubkey = keys.getPublic().getEncoded();
     261                _ntcp2StaticPrivkey = keys.getPrivate().getData();
     262                _ntcp2StaticPubkey = keys.getPublic().getData();
    262263                shouldSave = true;
    263264            } else {
    264265                _ntcp2StaticPrivkey = priv;
    265                 _ntcp2StaticPubkey = (new X25519PrivateKey(priv)).toPublic().getEncoded();
     266                _ntcp2StaticPubkey = (new PrivateKey(EncType.ECIES_X25519, priv)).toPublic().getData();
    266267            }
    267268            if (!shouldSave) {
Note: See TracChangeset for help on using the changeset viewer.