Search in sources :

Example 1 with ParametersWithRandom

use of org.spongycastle.crypto.params.ParametersWithRandom in project universa by UniversaBlockchain.

the class OAEPEncodingTest method encodeBlock.

/**
 * Make sure the SpongyCastle OAEPEncoding encodes the block according
 * to the test vectors from RSA OAEP specification.
 *
 * @throws Exception
 */
@Test
public void encodeBlock() throws Exception {
    AsymmetricBlockCipher encoder = new OAEPEncoding(RSAEngineFactory.make());
    encoder.init(true, new ParametersWithRandom(oaepSpec.pubParameters, oaepSpec.getRandSeed()));
    byte[] encoded = encoder.processBlock(oaepSpec.M, 0, oaepSpec.M.length);
    assertArrayEquals(encoded, oaepSpec.C);
}
Also used : ParametersWithRandom(org.spongycastle.crypto.params.ParametersWithRandom) OAEPEncoding(org.spongycastle.crypto.encodings.OAEPEncoding) AsymmetricBlockCipher(org.spongycastle.crypto.AsymmetricBlockCipher) Test(org.junit.Test)

Example 2 with ParametersWithRandom

use of org.spongycastle.crypto.params.ParametersWithRandom in project universa by UniversaBlockchain.

the class RSAEngineFactory method whetherShouldUseNative.

/**
 * Perform a test to check whether we should use an optimized native implementation or default Java one.
 */
static boolean whetherShouldUseNative() {
    // Sometimes NativeRSAEngine does not work; e.g. when GMP native binary is not available.
    // Test once if it will work; if it doesn't, fallback to use default RSAEngine.
    final RSAOAEPTestVectors oaepSpec = new RSAOAEPTestVectors();
    final ParametersWithRandom param = new ParametersWithRandom(oaepSpec.pubParameters, oaepSpec.getRandSeed());
    boolean errorOccured;
    try {
        final NativeRSAEngine nativeRSAEngine = new NativeRSAEngine();
        nativeRSAEngine.init(true, param);
        errorOccured = false;
    } catch (Throwable e) {
        errorOccured = true;
    }
    return !errorOccured;
}
Also used : ParametersWithRandom(org.spongycastle.crypto.params.ParametersWithRandom) NativeRSAEngine(com.icodici.crypto.rsaoaep.scrsa.NativeRSAEngine)

Example 3 with ParametersWithRandom

use of org.spongycastle.crypto.params.ParametersWithRandom in project universa by UniversaBlockchain.

the class RSAOAEPPublicKey method checkSignature.

/**
 * {@inheritDoc}
 */
@NonNull
@Override
public boolean checkSignature(InputStream input, byte[] signature, HashType hashType, int saltLength) throws IllegalStateException, IOException {
    if (state == null) {
        throw new IllegalStateException();
    } else {
        final Digest primaryDigest = hashType.makeDigest();
        if (saltLength == MAX_SALT_LENGTH) {
            saltLength = getMaxSaltLength(getBitStrength(), primaryDigest.getDigestSize());
        }
        if (saltLength < 0) {
            throw new RuntimeException(String.format("Incorrect salt length %s", saltLength));
        }
        final Signer signatureChecker = new PSSSigner(RSAEngineFactory.make(), primaryDigest, state.mgf1HashType.makeDigest(), saltLength);
        signatureChecker.init(false, new ParametersWithRandom(state.keyParameters, state.rng));
        boolean done = false;
        while (!done) {
            int availableBytes = input.available();
            if (availableBytes <= 0) {
                done = true;
            } else {
                byte[] buffer = new byte[availableBytes];
                int howManyBytesRead = input.read(buffer);
                if (howManyBytesRead <= 0) {
                    done = true;
                } else {
                    signatureChecker.update(buffer, 0, howManyBytesRead);
                }
            }
        }
        return signatureChecker.verifySignature(signature);
    }
}
Also used : PSSSigner(org.spongycastle.crypto.signers.PSSSigner) Signer(org.spongycastle.crypto.Signer) Digest(org.spongycastle.crypto.Digest) SHA1Digest(org.spongycastle.crypto.digests.SHA1Digest) PSSSigner(org.spongycastle.crypto.signers.PSSSigner) ParametersWithRandom(org.spongycastle.crypto.params.ParametersWithRandom) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 4 with ParametersWithRandom

use of org.spongycastle.crypto.params.ParametersWithRandom in project universa by UniversaBlockchain.

the class RSAOAEPPrivateKey method sign.

/**
 * {@inheritDoc}
 * <p>
 * Signature is created using RSA-PSS as described in PKCS# 1 v 2.1.
 */
@Override
public byte[] sign(InputStream input, HashType hashType, @Nullable byte[] salt) throws IllegalStateException, IOException {
    if (state == null) {
        throw new IllegalStateException();
    } else {
        final Digest primaryDigest = hashType.makeDigest();
        final PSSSigner signer;
        if (salt == null) {
            // Use maximum possible salt
            signer = new PSSSigner(RSAEngineFactory.make(), primaryDigest, state.mgf1HashType.makeDigest(), getMaxSaltLength(getBitStrength(), primaryDigest.getDigestSize()));
        } else {
            // Use some specific salt
            signer = new PSSSigner(RSAEngineFactory.make(), primaryDigest, state.mgf1HashType.makeDigest(), salt);
        }
        signer.init(true, new ParametersWithRandom(state.keyParameters, state.rng));
        boolean done = false;
        while (!done) {
            int availableBytes = input.available();
            if (availableBytes <= 0) {
                done = true;
            } else {
                byte[] buffer = new byte[availableBytes];
                int howManyBytesRead = input.read(buffer);
                if (howManyBytesRead <= 0) {
                    done = true;
                } else {
                    signer.update(buffer, 0, howManyBytesRead);
                }
            }
        }
        try {
            return signer.generateSignature();
        } catch (CryptoException e) {
            throw new IOException(String.format("Cannot sign data: %s", e.toString()));
        }
    }
}
Also used : SHA1Digest(org.spongycastle.crypto.digests.SHA1Digest) PSSSigner(org.spongycastle.crypto.signers.PSSSigner) ParametersWithRandom(org.spongycastle.crypto.params.ParametersWithRandom) IOException(java.io.IOException)

Example 5 with ParametersWithRandom

use of org.spongycastle.crypto.params.ParametersWithRandom in project universa by UniversaBlockchain.

the class NativeRSACoreEngine method init.

/**
 * initialise the RSA engine.
 *
 * @param forEncryption true if we are encrypting, false otherwise.
 * @param param         the necessary RSA key parameters.
 */
public void init(boolean forEncryption, CipherParameters param) {
    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom rParam = (ParametersWithRandom) param;
        key = (RSAKeyParameters) rParam.getParameters();
    } else {
        key = (RSAKeyParameters) param;
    }
    this.forEncryption = forEncryption;
    if (key instanceof RSAPrivateCrtKeyParameters) {
        isPrivate = true;
        // 
        // we have the extra factors, use the Chinese Remainder Theorem - the author
        // wishes to express his thanks to Dirk Bonekaemper at rtsffm.com for
        // advice regarding the expression of this.
        // 
        RSAPrivateCrtKeyParameters crtKey = (RSAPrivateCrtKeyParameters) key;
        p = new GmpInteger(crtKey.getP());
        q = new GmpInteger(crtKey.getQ());
        dP = new GmpInteger(crtKey.getDP());
        dQ = new GmpInteger(crtKey.getDQ());
        qInv = crtKey.getQInv();
        exponent = modulus = null;
    } else {
        isPrivate = false;
        exponent = new GmpInteger(key.getExponent());
        modulus = new GmpInteger(key.getModulus());
        isSmallExponent = exponent.bitLength() < 64;
        p = q = dP = dQ = null;
        qInv = null;
    }
}
Also used : GmpInteger(com.squareup.jnagmp.GmpInteger) ParametersWithRandom(org.spongycastle.crypto.params.ParametersWithRandom) RSAPrivateCrtKeyParameters(org.spongycastle.crypto.params.RSAPrivateCrtKeyParameters)

Aggregations

ParametersWithRandom (org.spongycastle.crypto.params.ParametersWithRandom)5 SHA1Digest (org.spongycastle.crypto.digests.SHA1Digest)2 PSSSigner (org.spongycastle.crypto.signers.PSSSigner)2 NativeRSAEngine (com.icodici.crypto.rsaoaep.scrsa.NativeRSAEngine)1 GmpInteger (com.squareup.jnagmp.GmpInteger)1 IOException (java.io.IOException)1 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1 Test (org.junit.Test)1 AsymmetricBlockCipher (org.spongycastle.crypto.AsymmetricBlockCipher)1 Digest (org.spongycastle.crypto.Digest)1 Signer (org.spongycastle.crypto.Signer)1 OAEPEncoding (org.spongycastle.crypto.encodings.OAEPEncoding)1 RSAPrivateCrtKeyParameters (org.spongycastle.crypto.params.RSAPrivateCrtKeyParameters)1