Search in sources :

Example 31 with Signature

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

the class DSAEngine method signIt.

/**
 *  Sign using DSA-SHA1 or Syndie DSA-SHA256 ONLY.
 *
 *  @param hash either a Hash or a SHA1Hash
 *  @return null on error
 *  @since 0.8.3
 */
private Signature signIt(SimpleDataStructure hash, SigningPrivateKey signingKey) {
    if ((signingKey == null) || (hash == null))
        return null;
    if (signingKey.getType() != SigType.DSA_SHA1)
        throw new IllegalArgumentException("Bad key type " + signingKey.getType());
    long start = _context.clock().now();
    BigInteger k;
    boolean ok;
    do {
        k = new NativeBigInteger(160, _context.random());
        ok = k.compareTo(CryptoConstants.dsaq) != 1;
        ok = ok && !k.equals(BigInteger.ZERO);
    // System.out.println("K picked (ok? " + ok + "): " + k.bitLength() + ": " + k.toString());
    } while (!ok);
    BigInteger r = CryptoConstants.dsag.modPowCT(k, CryptoConstants.dsap).mod(CryptoConstants.dsaq);
    BigInteger kinv = k.modInverse(CryptoConstants.dsaq);
    BigInteger M = new NativeBigInteger(1, hash.getData());
    BigInteger x = new NativeBigInteger(1, signingKey.getData());
    BigInteger s = (kinv.multiply(M.add(x.multiply(r)))).mod(CryptoConstants.dsaq);
    byte[] rbytes = r.toByteArray();
    byte[] sbytes = s.toByteArray();
    byte[] out = new byte[40];
    // (q^random)%p is computationally random
    _context.random().harvester().feedEntropy("DSA.sign", rbytes, 0, rbytes.length);
    if (rbytes.length == 20) {
        // System.arraycopy(rbytes, 0, out, 0, 20);
        for (int i = 0; i < 20; i++) {
            out[i] = rbytes[i];
        }
    } else if (rbytes.length == 21) {
        // System.arraycopy(rbytes, 1, out, 0, 20);
        for (int i = 0; i < 20; i++) {
            out[i] = rbytes[i + 1];
        }
    } else if (rbytes.length > 21) {
        _log.error("Bad R length " + rbytes.length);
        return null;
    } else {
        // System.arraycopy(rbytes, 0, out, 20 - rbytes.length, rbytes.length);
        for (int i = 0; i < rbytes.length; i++) out[i + 20 - rbytes.length] = rbytes[i];
    }
    if (sbytes.length == 20) {
        // System.arraycopy(sbytes, 0, out, 20, 20);
        for (int i = 0; i < 20; i++) {
            out[i + 20] = sbytes[i];
        }
    } else if (sbytes.length == 21) {
        // System.arraycopy(sbytes, 1, out, 20, 20);
        for (int i = 0; i < 20; i++) {
            out[i + 20] = sbytes[i + 1];
        }
    } else if (sbytes.length > 21) {
        _log.error("Bad S length " + sbytes.length);
        return null;
    } else {
        // System.arraycopy(sbytes, 0, out, 40 - sbytes.length, sbytes.length);
        for (int i = 0; i < sbytes.length; i++) out[i + 20 + 20 - sbytes.length] = sbytes[i];
    }
    long diff = _context.clock().now() - start;
    if (diff > 1000) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Took too long to sign (" + diff + "ms)");
    }
    return new Signature(out);
}
Also used : NativeBigInteger(net.i2p.util.NativeBigInteger) Signature(net.i2p.data.Signature) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger)

Example 32 with Signature

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

the class HostTxtEntry method hasValidSig.

/**
 * Verify with the dest public key using the "sig" property
 */
public boolean hasValidSig() {
    if (props == null || name == null || dest == null)
        return false;
    if (!isValidated) {
        isValidated = true;
        StringWriter buf = new StringWriter(1024);
        String sig = props.getProperty(PROP_SIG);
        if (sig == null)
            return false;
        buf.append(name);
        buf.append(KV_SEPARATOR);
        buf.append(dest);
        try {
            writeProps(buf, true, false);
        } catch (IOException ioe) {
            // won't happen
            return false;
        }
        byte[] sdata = Base64.decode(sig);
        if (sdata == null)
            return false;
        Destination d;
        try {
            d = new Destination(dest);
        } 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;
        }
        isValid = DSAEngine.getInstance().verifySignature(s, DataHelper.getUTF8(buf.toString()), spk);
    }
    return isValid;
}
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 33 with Signature

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

the class HostTxtEntry method signIt.

/**
 * @param sigprop The signature property to set
 */
private void signIt(SigningPrivateKey spk, String sigprop) {
    if (props == null)
        throw new IllegalStateException();
    if (props.containsKey(sigprop))
        throw new IllegalStateException();
    if (!props.containsKey(PROP_DATE))
        props.setProperty(PROP_DATE, Long.toString(System.currentTimeMillis() / 1000));
    StringWriter buf = new StringWriter(1024);
    buf.append(name);
    buf.append(KV_SEPARATOR);
    buf.append(dest);
    try {
        writeProps(buf);
    } catch (IOException ioe) {
        throw new IllegalStateException(ioe);
    }
    Signature s = DSAEngine.getInstance().sign(DataHelper.getUTF8(buf.toString()), spk);
    if (s == null)
        throw new IllegalArgumentException("sig failed");
    props.setProperty(sigprop, s.toBase64());
}
Also used : StringWriter(java.io.StringWriter) Signature(net.i2p.data.Signature) IOException(java.io.IOException)

Example 34 with Signature

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

the class HostTxtEntry method hasValidInnerSig.

/**
 * Verify with the "olddest" property's public key using the "oldsig" property
 */
public boolean hasValidInnerSig() {
    if (props == null || name == null || dest == null)
        return false;
    boolean rv = false;
    // don't cache result
    if (true) {
        StringWriter buf = new StringWriter(1024);
        String sig = props.getProperty(PROP_OLDSIG);
        String olddest = props.getProperty(PROP_OLDDEST);
        if (sig == null || olddest == null)
            return false;
        buf.append(name);
        buf.append(KV_SEPARATOR);
        buf.append(dest);
        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 35 with Signature

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

the class HostTxtEntry method signRemove.

/**
 * Sign as a "remove" line #!dest=dest#name=name#k1=v1#sig=sig...]
 * Must have been constructed with non-null properties.
 */
public void signRemove(SigningPrivateKey spk) {
    if (props == null)
        throw new IllegalStateException();
    if (props.containsKey(PROP_SIG))
        throw new IllegalStateException();
    props.setProperty(PROP_NAME, name);
    props.setProperty(PROP_DEST, dest);
    if (!props.containsKey(PROP_DATE))
        props.setProperty(PROP_DATE, Long.toString(System.currentTimeMillis() / 1000));
    StringWriter buf = new StringWriter(1024);
    try {
        writeProps(buf);
    } catch (IOException ioe) {
        throw new IllegalStateException(ioe);
    }
    props.remove(PROP_NAME);
    props.remove(PROP_DEST);
    Signature s = DSAEngine.getInstance().sign(DataHelper.getUTF8(buf.toString()), spk);
    if (s == null)
        throw new IllegalArgumentException("sig failed");
    props.setProperty(PROP_SIG, s.toBase64());
}
Also used : StringWriter(java.io.StringWriter) Signature(net.i2p.data.Signature) IOException(java.io.IOException)

Aggregations

Signature (net.i2p.data.Signature)36 IOException (java.io.IOException)16 SigType (net.i2p.crypto.SigType)14 DataFormatException (net.i2p.data.DataFormatException)11 SigningPublicKey (net.i2p.data.SigningPublicKey)11 ByteArrayInputStream (java.io.ByteArrayInputStream)7 GeneralSecurityException (java.security.GeneralSecurityException)6 FileInputStream (java.io.FileInputStream)5 StringWriter (java.io.StringWriter)5 SigningPrivateKey (net.i2p.data.SigningPrivateKey)5 InetAddress (java.net.InetAddress)4 MessageDigest (java.security.MessageDigest)4 Destination (net.i2p.data.Destination)4 SimpleDataStructure (net.i2p.data.SimpleDataStructure)4 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)3 DigestInputStream (java.security.DigestInputStream)3 Hash (net.i2p.data.Hash)3 Log (net.i2p.util.Log)3 SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)3