Search in sources :

Example 1 with DSASigner

use of org.spongycastle.crypto.signers.DSASigner in project Zom-Android by zom.

the class OtrCryptoEngineImpl method sign.

public byte[] sign(byte[] b, PrivateKey privatekey) throws OtrCryptoException {
    if (!(privatekey instanceof DSAPrivateKey))
        throw new IllegalArgumentException();
    DSAParams dsaParams = ((DSAPrivateKey) privatekey).getParams();
    DSAParameters bcDSAParameters = new DSAParameters(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG());
    DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privatekey;
    DSAPrivateKeyParameters bcDSAPrivateKeyParms = new DSAPrivateKeyParameters(dsaPrivateKey.getX(), bcDSAParameters);
    DSASigner dsaSigner = new DSASigner();
    dsaSigner.init(true, bcDSAPrivateKeyParms);
    BigInteger q = dsaParams.getQ();
    // Ian: Note that if you can get the standard DSA implementation you're
    // using to not hash its input, you should be able to pass it ((256-bit
    // value) mod q), (rather than truncating the 256-bit value) and all
    // should be well.
    // ref: Interop problems with libotr - DSA signature
    BigInteger bmpi = new BigInteger(1, b);
    BigInteger[] rs = dsaSigner.generateSignature(BigIntegers.asUnsignedByteArray(bmpi.mod(q)));
    int siglen = q.bitLength() / 4;
    int rslen = siglen / 2;
    byte[] rb = BigIntegers.asUnsignedByteArray(rs[0]);
    byte[] sb = BigIntegers.asUnsignedByteArray(rs[1]);
    // Create the final signature array, padded with zeros if necessary.
    byte[] sig = new byte[siglen];
    Boolean writeR = false;
    Boolean writeS = false;
    int shiftR = rslen - rb.length;
    int shiftS = rslen - sb.length;
    for (int i = 0; i < siglen; i++) {
        if (i < rslen) {
            if (!writeR)
                writeR = rb.length >= rslen - i;
            sig[i] = (writeR) ? rb[i - shiftR] : (byte) 0x0;
        } else {
            // Rebase.
            int j = i - rslen;
            if (!writeS)
                writeS = sb.length >= rslen - j;
            sig[i] = (writeS) ? sb[j - shiftS] : (byte) 0x0;
        }
    }
    return sig;
}
Also used : DSASigner(org.spongycastle.crypto.signers.DSASigner) DSAPrivateKeyParameters(org.spongycastle.crypto.params.DSAPrivateKeyParameters) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) DSAParams(java.security.interfaces.DSAParams) DSAParameters(org.spongycastle.crypto.params.DSAParameters)

Example 2 with DSASigner

use of org.spongycastle.crypto.signers.DSASigner in project Zom-Android by zom.

the class OtrCryptoEngineImpl method verify.

private Boolean verify(byte[] b, PublicKey pubKey, BigInteger r, BigInteger s) throws OtrCryptoException {
    if (!(pubKey instanceof DSAPublicKey))
        throw new IllegalArgumentException();
    DSAParams dsaParams = ((DSAPublicKey) pubKey).getParams();
    BigInteger q = dsaParams.getQ();
    DSAParameters bcDSAParams = new DSAParameters(dsaParams.getP(), q, dsaParams.getG());
    DSAPublicKey dsaPrivateKey = (DSAPublicKey) pubKey;
    DSAPublicKeyParameters dsaPrivParms = new DSAPublicKeyParameters(dsaPrivateKey.getY(), bcDSAParams);
    // Ian: Note that if you can get the standard DSA implementation you're
    // using to not hash its input, you should be able to pass it ((256-bit
    // value) mod q), (rather than truncating the 256-bit value) and all
    // should be well.
    // ref: Interop problems with libotr - DSA signature
    DSASigner dsaSigner = new DSASigner();
    dsaSigner.init(false, dsaPrivParms);
    BigInteger bmpi = new BigInteger(1, b);
    Boolean result = dsaSigner.verifySignature(BigIntegers.asUnsignedByteArray(bmpi.mod(q)), r, s);
    return result;
}
Also used : DSAPublicKeyParameters(org.spongycastle.crypto.params.DSAPublicKeyParameters) DSASigner(org.spongycastle.crypto.signers.DSASigner) BigInteger(java.math.BigInteger) DSAParams(java.security.interfaces.DSAParams) DSAParameters(org.spongycastle.crypto.params.DSAParameters) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Aggregations

BigInteger (java.math.BigInteger)2 DSAParams (java.security.interfaces.DSAParams)2 DSAParameters (org.spongycastle.crypto.params.DSAParameters)2 DSASigner (org.spongycastle.crypto.signers.DSASigner)2 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)1 DSAPublicKey (java.security.interfaces.DSAPublicKey)1 DSAPrivateKeyParameters (org.spongycastle.crypto.params.DSAPrivateKeyParameters)1 DSAPublicKeyParameters (org.spongycastle.crypto.params.DSAPublicKeyParameters)1