Search in sources :

Example 6 with HMac

use of org.spongycastle.crypto.macs.HMac in project rskj by rsksmart.

the class CryptoTest method test15.

// ECIES_AES128_SHA256 + Ephemeral Key + IV(all zeroes)
@Test
public void test15() throws Throwable {
    byte[] privKey = Hex.decode("a4627abc2a3c25315bff732cb22bc128f203912dd2a840f31e66efb27a47d2b1");
    ECKey ecKey = ECKey.fromPrivate(privKey);
    ECPrivateKeyParameters ecPrivKey = new ECPrivateKeyParameters(ecKey.getPrivKey(), ECKey.CURVE);
    ECPublicKeyParameters ecPubKey = new ECPublicKeyParameters(ecKey.getPubKeyPoint(), ECKey.CURVE);
    AsymmetricCipherKeyPair myKey = new AsymmetricCipherKeyPair(ecPubKey, ecPrivKey);
    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);
    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom());
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keygenParams);
    EphemeralKeyPairGenerator kGen = new EphemeralKeyPairGenerator(generator, new KeyEncoder() {

        public byte[] getEncoded(AsymmetricKeyParameter keyParameter) {
            return ((ECPublicKeyParameters) keyParameter).getQ().getEncoded();
        }
    });
    ECKeyPairGenerator gen = new ECKeyPairGenerator();
    gen.init(new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom()));
    iesEngine.init(myKey.getPublic(), parametersWithIV, kGen);
    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(myKey.getPrivate(), parametersWithIV, new ECIESPublicKeyParser(ECKey.CURVE));
    byte[] orig = decryptorIES_Engine.processBlock(cipher, 0, cipher.length);
    log.info("orig: " + Hex.toHexString(orig));
}
Also used : ECKeyPairGenerator(org.spongycastle.crypto.generators.ECKeyPairGenerator) EphemeralKeyPairGenerator(org.spongycastle.crypto.generators.EphemeralKeyPairGenerator) KeyEncoder(org.spongycastle.crypto.KeyEncoder) 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) AsymmetricCipherKeyPair(org.spongycastle.crypto.AsymmetricCipherKeyPair) IESEngine(org.spongycastle.crypto.engines.IESEngine) ECDHBasicAgreement(org.spongycastle.crypto.agreement.ECDHBasicAgreement) KDF2BytesGenerator(org.spongycastle.crypto.generators.KDF2BytesGenerator) SHA256Digest(org.spongycastle.crypto.digests.SHA256Digest) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) ECIESPublicKeyParser(org.spongycastle.crypto.parsers.ECIESPublicKeyParser) Test(org.junit.Test)

Example 7 with HMac

use of org.spongycastle.crypto.macs.HMac in project rskj by rsksmart.

the class ECIESTest method makeIESEngine.

private static EthereumIESEngine makeIESEngine(boolean isEncrypt, ECPoint pub, BigInteger prv, byte[] IV) {
    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(isEncrypt, new ECPrivateKeyParameters(prv, curve), new ECPublicKeyParameters(pub, curve), parametersWithIV);
    return iesEngine;
}
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)

Example 8 with HMac

use of org.spongycastle.crypto.macs.HMac in project SightRemote by TebbeUbben.

the class Cryptograph method getHmac.

private static byte[] getHmac(byte[] secret, byte[] data, Digest algorithm) {
    HMac hmac = new HMac(algorithm);
    hmac.init(new KeyParameter(secret));
    byte[] result = new byte[hmac.getMacSize()];
    hmac.update(data, 0, data.length);
    hmac.doFinal(result, 0);
    return result;
}
Also used : HMac(org.spongycastle.crypto.macs.HMac) AsymmetricKeyParameter(org.spongycastle.crypto.params.AsymmetricKeyParameter) KeyParameter(org.spongycastle.crypto.params.KeyParameter)

Aggregations

HMac (org.spongycastle.crypto.macs.HMac)8 ECDHBasicAgreement (org.spongycastle.crypto.agreement.ECDHBasicAgreement)7 SHA256Digest (org.spongycastle.crypto.digests.SHA256Digest)5 AESFastEngine (org.spongycastle.crypto.engines.AESFastEngine)5 SICBlockCipher (org.spongycastle.crypto.modes.SICBlockCipher)5 BufferedBlockCipher (org.spongycastle.crypto.BufferedBlockCipher)4 SecureRandom (java.security.SecureRandom)3 ConcatKDFBytesGenerator (org.ethereum.ConcatKDFBytesGenerator)3 KeyGenerationParameters (org.spongycastle.crypto.KeyGenerationParameters)3 ECKeyPairGenerator (org.spongycastle.crypto.generators.ECKeyPairGenerator)3 Test (org.junit.Test)2 AsymmetricCipherKeyPair (org.spongycastle.crypto.AsymmetricCipherKeyPair)2 SHA1Digest (org.spongycastle.crypto.digests.SHA1Digest)2 IESEngine (org.spongycastle.crypto.engines.IESEngine)2 EphemeralKeyPairGenerator (org.spongycastle.crypto.generators.EphemeralKeyPairGenerator)2 KDF2BytesGenerator (org.spongycastle.crypto.generators.KDF2BytesGenerator)2 ECIESPublicKeyParser (org.spongycastle.crypto.parsers.ECIESPublicKeyParser)2 KeyEncoder (org.spongycastle.crypto.KeyEncoder)1 AsymmetricKeyParameter (org.spongycastle.crypto.params.AsymmetricKeyParameter)1 KeyParameter (org.spongycastle.crypto.params.KeyParameter)1