Search in sources :

Example 6 with SigningPublicKey

use of net.i2p.data.SigningPublicKey in project i2p.i2p by i2p.

the class HostTxtEntry method hasValidRemoveSig.

/**
 * Verify with the "dest" property's public key using the "sig" property
 */
public boolean hasValidRemoveSig() {
    if (props == null)
        return false;
    boolean rv = false;
    // don't cache result
    if (true) {
        StringWriter buf = new StringWriter(1024);
        String sig = props.getProperty(PROP_SIG);
        String olddest = props.getProperty(PROP_DEST);
        if (sig == null || olddest == null)
            return false;
        try {
            writeProps(buf, true, true);
        } catch (IOException ioe) {
            // won't happen
            return false;
        }
        byte[] sdata = Base64.decode(sig);
        if (sdata == null)
            return false;
        Destination d;
        try {
            d = new Destination(olddest);
        } catch (DataFormatException dfe) {
            return false;
        }
        SigningPublicKey spk = d.getSigningPublicKey();
        SigType type = spk.getType();
        if (type == null)
            return false;
        Signature s;
        try {
            s = new Signature(type, sdata);
        } catch (IllegalArgumentException iae) {
            return false;
        }
        rv = DSAEngine.getInstance().verifySignature(s, DataHelper.getUTF8(buf.toString()), spk);
    }
    return rv;
}
Also used : Destination(net.i2p.data.Destination) SigningPublicKey(net.i2p.data.SigningPublicKey) DataFormatException(net.i2p.data.DataFormatException) StringWriter(java.io.StringWriter) Signature(net.i2p.data.Signature) IOException(java.io.IOException) SigType(net.i2p.crypto.SigType)

Example 7 with SigningPublicKey

use of net.i2p.data.SigningPublicKey in project i2p.i2p by i2p.

the class I2PDatagramDissector method verifySignature.

/**
 * Verify the signature of this datagram (previously loaded with the
 * loadI2PDatagram() method)
 * @throws I2PInvalidDatagramException if the signature is invalid
 */
public void verifySignature() throws I2PInvalidDatagramException {
    // first check if it already got validated
    if (this.valid)
        return;
    if (rxSign == null || rxSign.getData() == null || rxDest == null)
        throw new I2PInvalidDatagramException("Datagram not yet read");
    // now validate
    SigningPublicKey spk = rxDest.getSigningPublicKey();
    SigType type = spk.getType();
    if (type == null)
        throw new I2PInvalidDatagramException("unsupported sig type");
    if (type == SigType.DSA_SHA1) {
        if (!this.dsaEng.verifySignature(rxSign, rxHash, spk))
            throw new I2PInvalidDatagramException("Incorrect I2P repliable datagram signature");
    } else {
        if (!this.dsaEng.verifySignature(rxSign, rxPayload, 0, rxPayloadLen, spk))
            throw new I2PInvalidDatagramException("Incorrect I2P repliable datagram signature");
    }
    // set validated
    this.valid = true;
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) SigType(net.i2p.crypto.SigType)

Example 8 with SigningPublicKey

use of net.i2p.data.SigningPublicKey in project i2p.i2p by i2p.

the class KeyGenerator method generateSigningKeys.

/**
 *  DSA-SHA1 only.
 *
 *  Same as above but different return type
 *  @since 0.8.7
 */
public SimpleDataStructure[] generateSigningKeys() {
    SimpleDataStructure[] keys = new SimpleDataStructure[2];
    BigInteger x = null;
    // make sure the random key is less than the DSA q and greater than zero
    do {
        x = new NativeBigInteger(160, _context.random());
    } while (x.compareTo(CryptoConstants.dsaq) >= 0 || x.equals(BigInteger.ZERO));
    BigInteger y = CryptoConstants.dsag.modPow(x, CryptoConstants.dsap);
    keys[0] = new SigningPublicKey();
    keys[1] = new SigningPrivateKey();
    try {
        keys[0].setData(SigUtil.rectify(y, SigningPublicKey.KEYSIZE_BYTES));
        keys[1].setData(SigUtil.rectify(x, SigningPrivateKey.KEYSIZE_BYTES));
    } catch (InvalidKeyException ike) {
        throw new IllegalStateException(ike);
    }
    return keys;
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) SigningPublicKey(net.i2p.data.SigningPublicKey) NativeBigInteger(net.i2p.util.NativeBigInteger) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) InvalidKeyException(java.security.InvalidKeyException) SimpleDataStructure(net.i2p.data.SimpleDataStructure)

Example 9 with SigningPublicKey

use of net.i2p.data.SigningPublicKey in project i2p.i2p by i2p.

the class KeyGenerator method testSig.

private static void testSig(SigType type, int runs) throws GeneralSecurityException {
    byte[] src = new byte[512];
    double gtime = 0;
    long stime = 0;
    long vtime = 0;
    SimpleDataStructure[] keys = null;
    long st = System.nanoTime();
    // RSA super slow, limit to 5
    int genruns = (type.getBaseAlgorithm() == SigAlgo.RSA) ? Math.min(runs, 5) : runs;
    for (int i = 0; i < genruns; i++) {
        keys = KeyGenerator.getInstance().generateSigningKeys(type);
    }
    long en = System.nanoTime();
    gtime = ((en - st) / (1000 * 1000d)) / genruns;
    System.out.println(type + " key gen " + genruns + " times: " + gtime + " ms each");
    SigningPublicKey pubkey = (SigningPublicKey) keys[0];
    SigningPrivateKey privkey = (SigningPrivateKey) keys[1];
    SigningPublicKey pubkey2 = getSigningPublicKey(privkey);
    if (pubkey.equals(pubkey2))
        System.out.println(type + " private-to-public test PASSED");
    else
        System.out.println(type + " private-to-public test FAILED");
    // System.out.println("privkey " + keys[1]);
    MessageDigest md = type.getDigestInstance();
    for (int i = 0; i < runs; i++) {
        RandomSource.getInstance().nextBytes(src);
        md.update(src);
        byte[] sha = md.digest();
        SimpleDataStructure hash = type.getHashInstance();
        hash.setData(sha);
        long start = System.nanoTime();
        Signature sig = DSAEngine.getInstance().sign(src, privkey);
        Signature sig2 = DSAEngine.getInstance().sign(hash, privkey);
        if (sig == null)
            throw new GeneralSecurityException("signature generation failed");
        if (sig2 == null)
            throw new GeneralSecurityException("signature generation (H) failed");
        long mid = System.nanoTime();
        boolean ok = DSAEngine.getInstance().verifySignature(sig, src, pubkey);
        boolean ok2 = DSAEngine.getInstance().verifySignature(sig2, hash, pubkey);
        long end = System.nanoTime();
        stime += mid - start;
        vtime += end - mid;
        if (!ok)
            throw new GeneralSecurityException(type + " V(S(data)) fail");
        if (!ok2)
            throw new GeneralSecurityException(type + " V(S(H(data))) fail");
    }
    stime /= 1000 * 1000;
    vtime /= 1000 * 1000;
    System.out.println(type + " sign/verify " + runs + " times: " + (vtime + stime) + " ms = " + (((double) stime) / runs) + " each sign, " + (((double) vtime) / runs) + " each verify, " + (((double) (stime + vtime)) / runs) + " s+v");
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) SigningPublicKey(net.i2p.data.SigningPublicKey) Signature(net.i2p.data.Signature) GeneralSecurityException(java.security.GeneralSecurityException) MessageDigest(java.security.MessageDigest) SimpleDataStructure(net.i2p.data.SimpleDataStructure) ECPoint(java.security.spec.ECPoint)

Example 10 with SigningPublicKey

use of net.i2p.data.SigningPublicKey in project i2p.i2p by i2p.

the class DSATest method testMultiple.

public void testMultiple() {
    for (int i = 0; i < 25; i++) {
        byte[] message = new byte[256];
        _context.random().nextBytes(message);
        Object[] keys = KeyGenerator.getInstance().generateSigningKeypair();
        SigningPublicKey pubkey = (SigningPublicKey) keys[0];
        SigningPrivateKey privkey = (SigningPrivateKey) keys[1];
        Signature s = DSAEngine.getInstance().sign(message, privkey);
        Signature s1 = DSAEngine.getInstance().sign(new ByteArrayInputStream(message), privkey);
        assertTrue(DSAEngine.getInstance().verifySignature(s, message, pubkey));
        assertTrue(DSAEngine.getInstance().verifySignature(s1, new ByteArrayInputStream(message), pubkey));
        assertTrue(DSAEngine.getInstance().verifySignature(s1, message, pubkey));
        assertTrue(DSAEngine.getInstance().verifySignature(s, new ByteArrayInputStream(message), pubkey));
    }
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) SigningPublicKey(net.i2p.data.SigningPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Signature(net.i2p.data.Signature)

Aggregations

SigningPublicKey (net.i2p.data.SigningPublicKey)36 SigningPrivateKey (net.i2p.data.SigningPrivateKey)13 IOException (java.io.IOException)12 DataFormatException (net.i2p.data.DataFormatException)11 SigType (net.i2p.crypto.SigType)10 Signature (net.i2p.data.Signature)10 PublicKey (net.i2p.data.PublicKey)9 File (java.io.File)8 GeneralSecurityException (java.security.GeneralSecurityException)8 PublicKey (java.security.PublicKey)7 PrivateKey (net.i2p.data.PrivateKey)6 SimpleDataStructure (net.i2p.data.SimpleDataStructure)6 BigInteger (java.math.BigInteger)5 ECPoint (java.security.spec.ECPoint)5 Certificate (net.i2p.data.Certificate)5 Destination (net.i2p.data.Destination)5 NativeBigInteger (net.i2p.util.NativeBigInteger)5 FileInputStream (java.io.FileInputStream)3 StringWriter (java.io.StringWriter)3 CertificateTest (net.i2p.data.CertificateTest)3