Search in sources :

Example 1 with SHA256Digest

use of org.spongycastle.crypto.digests.SHA256Digest in project rskj by rsksmart.

the class CryptoTest method test14.

// ECIES_AES128_SHA256 + No Ephemeral Key + IV(all zeroes)
@Test
public void test14() throws Throwable {
    AESFastEngine aesFastEngine = new AESFastEngine();
    IESEngine iesEngine = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new BufferedBlockCipher(new SICBlockCipher(aesFastEngine)));
    byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
    IESParameters p = new IESWithCipherParameters(d, e, 64, 128);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, new byte[16]);
    ECKeyPairGenerator eGen = new ECKeyPairGenerator();
    KeyGenerationParameters gParam = new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom());
    eGen.init(gParam);
    AsymmetricCipherKeyPair p1 = eGen.generateKeyPair();
    AsymmetricCipherKeyPair p2 = eGen.generateKeyPair();
    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom());
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keygenParams);
    ECKeyPairGenerator gen = new ECKeyPairGenerator();
    gen.init(new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom()));
    iesEngine.init(true, p1.getPrivate(), p2.getPublic(), parametersWithIV);
    byte[] message = Hex.decode("010101");
    log.info("payload: {}", Hex.toHexString(message));
    byte[] cipher = iesEngine.processBlock(message, 0, message.length);
    log.info("cipher: {}", Hex.toHexString(cipher));
    IESEngine decryptorIES_Engine = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new BufferedBlockCipher(new SICBlockCipher(aesFastEngine)));
    decryptorIES_Engine.init(false, p2.getPrivate(), p1.getPublic(), parametersWithIV);
    byte[] orig = decryptorIES_Engine.processBlock(cipher, 0, cipher.length);
    log.info("orig: " + Hex.toHexString(orig));
}
Also used : ECKeyPairGenerator(org.spongycastle.crypto.generators.ECKeyPairGenerator) HMac(org.spongycastle.crypto.macs.HMac) SecureRandom(java.security.SecureRandom) SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) KeyGenerationParameters(org.spongycastle.crypto.KeyGenerationParameters) IESEngine(org.spongycastle.crypto.engines.IESEngine) AsymmetricCipherKeyPair(org.spongycastle.crypto.AsymmetricCipherKeyPair) ECDHBasicAgreement(org.spongycastle.crypto.agreement.ECDHBasicAgreement) KDF2BytesGenerator(org.spongycastle.crypto.generators.KDF2BytesGenerator) SHA256Digest(org.spongycastle.crypto.digests.SHA256Digest) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) Test(org.junit.Test)

Example 2 with SHA256Digest

use of org.spongycastle.crypto.digests.SHA256Digest in project rskj by rsksmart.

the class ECIESTest method testKDF.

@Test
public void testKDF() {
    ConcatKDFBytesGenerator kdf = new ConcatKDFBytesGenerator(new SHA256Digest());
    kdf.init(new KDFParameters("Hello".getBytes(), new byte[0]));
    byte[] bytes = new byte[2];
    kdf.generateBytes(bytes, 0, bytes.length);
    assertArrayEquals(new byte[] { -66, -89 }, bytes);
}
Also used : ConcatKDFBytesGenerator(org.ethereum.ConcatKDFBytesGenerator) SHA256Digest(org.spongycastle.crypto.digests.SHA256Digest) Test(org.junit.Test)

Example 3 with SHA256Digest

use of org.spongycastle.crypto.digests.SHA256Digest in project rskj by rsksmart.

the class ECIESCoder method decrypt.

public static byte[] decrypt(ECPoint ephem, BigInteger prv, byte[] iv, byte[] cipher, byte[] macData) throws InvalidCipherTextException {
    AESFastEngine aesFastEngine = new AESFastEngine();
    EthereumIESEngine iesEngine = new EthereumIESEngine(new ECDHBasicAgreement(), new ConcatKDFBytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new SHA256Digest(), new BufferedBlockCipher(new SICBlockCipher(aesFastEngine)));
    byte[] d = new byte[] {};
    byte[] e = new byte[] {};
    IESParameters p = new IESWithCipherParameters(d, e, KEY_SIZE, KEY_SIZE);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, iv);
    iesEngine.init(false, new ECPrivateKeyParameters(prv, CURVE), new ECPublicKeyParameters(ephem, CURVE), parametersWithIV);
    return iesEngine.processBlock(cipher, 0, cipher.length, macData);
}
Also used : HMac(org.spongycastle.crypto.macs.HMac) SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) ECDHBasicAgreement(org.spongycastle.crypto.agreement.ECDHBasicAgreement) ConcatKDFBytesGenerator(org.ethereum.ConcatKDFBytesGenerator) SHA256Digest(org.spongycastle.crypto.digests.SHA256Digest) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher)

Example 4 with SHA256Digest

use of org.spongycastle.crypto.digests.SHA256Digest in project rskj by rsksmart.

the class ECKey method doSign.

/**
 * Signs the given hash and returns the R and S components as BigIntegers
 * and put them in ECDSASignature
 *
 * @param input to sign
 * @return ECDSASignature signature that contains the R and S components
 */
public ECDSASignature doSign(byte[] input) {
    // No decryption of private key required.
    if (priv == null) {
        throw new MissingPrivateKeyException();
    }
    ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
    ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(priv, CURVE);
    signer.init(true, privKey);
    BigInteger[] components = signer.generateSignature(input);
    return new ECDSASignature(components[0], components[1]).toCanonicalised();
}
Also used : HMacDSAKCalculator(org.spongycastle.crypto.signers.HMacDSAKCalculator) ECDSASigner(org.spongycastle.crypto.signers.ECDSASigner) SHA256Digest(org.spongycastle.crypto.digests.SHA256Digest) BigInteger(java.math.BigInteger)

Example 5 with SHA256Digest

use of org.spongycastle.crypto.digests.SHA256Digest in project java-tron by tronprotocol.

the class ECKey method doSign.

/**
 * Signs the given hash and returns the R and S components as BigIntegers and putData them in
 * ECDSASignature
 *
 * @param input to sign
 * @return ECDSASignature signature that contains the R and S components
 */
public ECDSASignature doSign(byte[] input) {
    if (input.length != 32) {
        throw new IllegalArgumentException("Expected 32 byte input to " + "ECDSA signature, not " + input.length);
    }
    // No decryption of private key required.
    if (privKey == null) {
        throw new MissingPrivateKeyException();
    }
    if (privKey instanceof BCECPrivateKey) {
        ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
        ECPrivateKeyParameters privKeyParams = new ECPrivateKeyParameters(((BCECPrivateKey) privKey).getD(), CURVE);
        signer.init(true, privKeyParams);
        BigInteger[] components = signer.generateSignature(input);
        return new ECDSASignature(components[0], components[1]).toCanonicalised();
    } else {
        try {
            final Signature ecSig = ECSignatureFactory.getRawInstance(provider);
            ecSig.initSign(privKey);
            ecSig.update(input);
            final byte[] derSignature = ecSig.sign();
            return ECDSASignature.decodeFromDER(derSignature).toCanonicalised();
        } catch (SignatureException | InvalidKeyException ex) {
            throw new RuntimeException("ECKey signing error", ex);
        }
    }
}
Also used : ECDSASigner(org.spongycastle.crypto.signers.ECDSASigner) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) BCECPrivateKey(org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey) HMacDSAKCalculator(org.spongycastle.crypto.signers.HMacDSAKCalculator) ECPrivateKeyParameters(org.spongycastle.crypto.params.ECPrivateKeyParameters) SHA256Digest(org.spongycastle.crypto.digests.SHA256Digest) Signature(java.security.Signature) BigInteger(java.math.BigInteger)

Aggregations

SHA256Digest (org.spongycastle.crypto.digests.SHA256Digest)15 BigInteger (java.math.BigInteger)8 ECDSASigner (org.spongycastle.crypto.signers.ECDSASigner)7 HMacDSAKCalculator (org.spongycastle.crypto.signers.HMacDSAKCalculator)7 ECDHBasicAgreement (org.spongycastle.crypto.agreement.ECDHBasicAgreement)5 AESFastEngine (org.spongycastle.crypto.engines.AESFastEngine)5 HMac (org.spongycastle.crypto.macs.HMac)5 SICBlockCipher (org.spongycastle.crypto.modes.SICBlockCipher)5 ECPrivateKeyParameters (org.spongycastle.crypto.params.ECPrivateKeyParameters)5 InvalidKeyException (java.security.InvalidKeyException)4 Signature (java.security.Signature)4 SignatureException (java.security.SignatureException)4 ConcatKDFBytesGenerator (org.ethereum.ConcatKDFBytesGenerator)4 BufferedBlockCipher (org.spongycastle.crypto.BufferedBlockCipher)4 BCECPrivateKey (org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)4 Test (org.junit.Test)3 SecureRandom (java.security.SecureRandom)2 AsymmetricCipherKeyPair (org.spongycastle.crypto.AsymmetricCipherKeyPair)2 KeyGenerationParameters (org.spongycastle.crypto.KeyGenerationParameters)2 IESEngine (org.spongycastle.crypto.engines.IESEngine)2