protected void engineInitSign( PrivateKey privateKey) throws InvalidKeyException { CipherParameters param = null; if (privateKey instanceof ECKey) { param = ECUtil.generatePrivateKeyParameter(privateKey); } digest = new GOST3411Digest(DEFAULT_SBOX); if (appRandom != null) { signer.init(true, new ParametersWithRandom(param, appRandom)); } else { signer.init(true, param); } }
public Object clone() throws CloneNotSupportedException { Digest d = (Digest)super.clone(); d.digest = new GOST3411Digest((GOST3411Digest)digest); return d; }
protected void engineInitVerify( PublicKey publicKey) throws InvalidKeyException { CipherParameters param; if (publicKey instanceof ECPublicKey) { param = ECUtil.generatePublicKeyParameter(publicKey); } else { try { byte[] bytes = publicKey.getEncoded(); publicKey = BouncyCastleProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(bytes)); if (publicKey instanceof ECPublicKey) { param = ECUtil.generatePublicKeyParameter(publicKey); } else { throw new InvalidKeyException("can't recognise key type in DSA based signer"); } } catch (Exception e) { throw new InvalidKeyException("can't recognise key type in DSA based signer"); } } digest = new GOST3411Digest(expandSbox(((BCDSTU4145PublicKey)publicKey).getSbox())); signer.init(false, param); }
private byte[] generateKey(byte[] startkey) { byte[] newKey = new byte[GOST28147_KEY_LENGTH]; GOST3411Digest digest = new GOST3411Digest(); digest.update(startkey, 0, startkey.length); digest.doFinal(newKey, 0); return newKey; }
private static void initDigesters() { digesters.put("Blake2b", Blake2bDigest.class); digesters.put("GOST3411", GOST3411Digest.class); digesters.put("Keccak", KeccakDigest.class); digesters.put("MD2", MD2Digest.class); digesters.put("MD4", MD4Digest.class); digesters.put("MD5", MD5Digest.class); digesters.put("RIPEMD128", RIPEMD128Digest.class); digesters.put("RIPEMD160", RIPEMD160Digest.class); digesters.put("RIPEMD256", RIPEMD256Digest.class); digesters.put("RIPEMD320", RIPEMD320Digest.class); digesters.put("SHA1", SHA1Digest.class); digesters.put("SHA224", SHA224Digest.class); digesters.put("SHA256", SHA256Digest.class); digesters.put("SHA384", SHA384Digest.class); digesters.put("SHA3-512", SHA3Digest.class); digesters.put("SHA3-256", SHA3Digest.class); digesters.put("SHA3-224", SHA3Digest.class); digesters.put("SHA3-384", SHA3Digest.class); digesters.put("SHA512", SHA512Digest.class); digesters.put("SHAKE-128", SHAKEDigest.class); digesters.put("SHAKE-256", SHAKEDigest.class); digesters.put("Skein256", SkeinDigest.class); digesters.put("Skein512", SkeinDigest.class); digesters.put("Skein1024", SkeinDigest.class); digesters.put("SM3", SM3Digest.class); digesters.put("Tiger", TigerDigest.class); digesters.put("Whirlpool", WhirlpoolDigest.class); }
public static void main( String[] args) { HMac gMac = new HMac(new GOST3411Digest(GOST28147Engine.getSBox("D-Test"))); gMac.init(new KeyParameter(PKCS5S1ParametersGenerator.PKCS5PasswordToUTF8Bytes("Boss".toCharArray()))); byte[] iBuf = new byte[4]; byte[] data = Hex.decode("b5d78fa546ba645c"); gMac.update(data, 0, data.length); byte[] mac = new byte[gMac.getMacSize()]; int pos = 3; while (++iBuf[pos] == 0) { --pos; } gMac.update(iBuf, 0, iBuf.length); gMac.doFinal(mac, 0); System.err.println(mac.length + " " + new String(Hex.encode(mac))); PKCS5S2ParametersGenerator pGen = new PKCS5S2ParametersGenerator(new GOST3411Digest()); pGen.init(PKCS5S1ParametersGenerator.PKCS5PasswordToUTF8Bytes("1".toCharArray()), data, 2048); KeyParameter kp = (KeyParameter)pGen.generateDerivedMacParameters(256); System.err.println(kp.getKey().length + " " + new String(Hex.encode(kp.getKey()))); runTest(new GOST3411DigestTest()); }
public Digest() { super(new GOST3411Digest()); }
public HashMac() { super(new HMac(new GOST3411Digest())); }
public SignatureSpi() { this.digest = new GOST3411Digest(); this.signer = new GOST3410Signer(); }
public SignatureSpi() { this.digest = new GOST3411Digest(); this.signer = new ECGOST3410Signer(); }
/** * Test Sign & Verify with test parameters * see: http://www.ietf.org/internet-drafts/draft-popov-cryptopro-cpalgs-01.txt * gostR3410-2001-TestParamSet P.46 */ private void ecGOST3410_TestParam() { SecureRandom random = new SecureRandom(); BigInteger mod_p = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564821041"); //p BigInteger mod_q = new BigInteger("57896044618658097711785492504343953927082934583725450622380973592137631069619"); ECCurve.Fp curve = new ECCurve.Fp( mod_p, // p new BigInteger("7"), // a new BigInteger("43308876546767276905765904595650931995942111794451039583252968842033849580414"), // b mod_q, ECConstants.ONE); ECDomainParameters params = new ECDomainParameters( curve, curve.createPoint( new BigInteger("2"), // x new BigInteger("4018974056539037503335449422937059775635739389905545080690979365213431566280")), // y mod_q); ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters( params, random); pGen.init(genParam); AsymmetricCipherKeyPair pair = pGen.generateKeyPair(); ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), random); ECGOST3410Signer ecgost3410 = new ECGOST3410Signer(); ecgost3410.init(true, param); //get hash message using the digest GOST3411. byte[] message = "Message for sign".getBytes(); GOST3411Digest gost3411 = new GOST3411Digest(); gost3411.update(message, 0, message.length); byte[] hashmessage = new byte[gost3411.getDigestSize()]; gost3411.doFinal(hashmessage, 0); BigInteger[] sig = ecgost3410.generateSignature(hashmessage); ecgost3410.init(false, pair.getPublic()); if (!ecgost3410.verifySignature(hashmessage, sig[0], sig[1])) { fail("signature fails"); } }
GOST3411DigestTest() { super(new GOST3411Digest(), messages, digests); }
public void performTest() { super.performTest(); millionATest(million_a_digest); HMac gMac = new HMac(new GOST3411Digest()); gMac.init(new KeyParameter(PKCS5S1ParametersGenerator.PKCS5PasswordToUTF8Bytes("1".toCharArray()))); byte[] data = "fred".getBytes(); gMac.update(data, 0, data.length); byte[] mac = new byte[gMac.getMacSize()]; gMac.doFinal(mac, 0); if (!Arrays.areEqual(Hex.decode("e9f98610cfc80084462b175a15d2b4ec10b2ab892eae5a6179d572d9b1db6b72"), mac)) { fail("mac calculation failed."); } }
protected Digest cloneDigest(Digest digest) { return new GOST3411Digest((GOST3411Digest)digest); }
public static final void register(final Map<String, Hasher> hashers) { hashers.put(BouncyCastleHasher.GOST, new BouncyCastleHasher(new GOST3411Digest())); hashers.put(BouncyCastleHasher.MD2, new BouncyCastleHasher(new MD2Digest())); hashers.put(BouncyCastleHasher.MD4, new BouncyCastleHasher(new MD4Digest())); hashers.put(BouncyCastleHasher.MD5, new BouncyCastleHasher(new MD5Digest())); hashers.put(BouncyCastleHasher.RIPEMD128, new BouncyCastleHasher(new RIPEMD128Digest())); hashers.put(BouncyCastleHasher.RIPEMD160, new BouncyCastleHasher(new RIPEMD160Digest())); hashers.put(BouncyCastleHasher.RIPEMD256, new BouncyCastleHasher(new RIPEMD256Digest())); hashers.put(BouncyCastleHasher.RIPEMD320, new BouncyCastleHasher(new RIPEMD320Digest())); hashers.put(BouncyCastleHasher.SHA1, new BouncyCastleHasher(new SHA1Digest())); hashers.put(BouncyCastleHasher.SHA224, new BouncyCastleHasher(new SHA224Digest())); hashers.put(BouncyCastleHasher.SHA256, new BouncyCastleHasher(new SHA256Digest())); hashers.put(BouncyCastleHasher.SHA3, new BouncyCastleHasher(new SHA3Digest())); hashers.put(BouncyCastleHasher.SHA384, new BouncyCastleHasher(new SHA384Digest())); hashers.put(BouncyCastleHasher.SHA512, new BouncyCastleHasher(new SHA512Digest())); hashers.put(BouncyCastleHasher.SHA512_T, new BouncyCastleHasher(new SHA512tDigest(7 * 8))); hashers.put(BouncyCastleHasher.SKEIN1024, new BouncyCastleHasher( new SkeinDigest(SkeinDigest.SKEIN_1024, Long.BYTES * 8))); hashers.put(BouncyCastleHasher.SKEIN256, new BouncyCastleHasher( new SkeinDigest(SkeinDigest.SKEIN_256, Long.BYTES * 8))); hashers.put(BouncyCastleHasher.SKEIN512, new BouncyCastleHasher( new SkeinDigest(SkeinDigest.SKEIN_512, Long.BYTES * 8))); hashers.put(BouncyCastleHasher.SM3, new BouncyCastleHasher(new SM3Digest())); hashers.put(BouncyCastleHasher.TIGER, new BouncyCastleHasher(new TigerDigest())); hashers.put(BouncyCastleHasher.WHIRLPOOL2, new BouncyCastleHasher(new WhirlpoolDigest())); }
/** * Test Sign & Verify with test parameters * see: http://www.ietf.org/internet-drafts/draft-popov-cryptopro-cpalgs-01.txt * gostR3410-2001-TestParamSet P.46 */ private void ecGOST3410_TestParam() { SecureRandom random = new SecureRandom(); BigInteger mod_p = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564821041"); //p ECCurve.Fp curve = new ECCurve.Fp( mod_p, // p new BigInteger("7"), // a new BigInteger("43308876546767276905765904595650931995942111794451039583252968842033849580414")); // b ECDomainParameters params = new ECDomainParameters( curve, new ECPoint.Fp(curve, new ECFieldElement.Fp(mod_p,new BigInteger("2")), // x new ECFieldElement.Fp(mod_p,new BigInteger("4018974056539037503335449422937059775635739389905545080690979365213431566280"))), // y new BigInteger("57896044618658097711785492504343953927082934583725450622380973592137631069619")); // q ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters( params, random); pGen.init(genParam); AsymmetricCipherKeyPair pair = pGen.generateKeyPair(); ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), random); ECGOST3410Signer ecgost3410 = new ECGOST3410Signer(); ecgost3410.init(true, param); //get hash message using the digest GOST3411. byte[] message = "Message for sign".getBytes(); GOST3411Digest gost3411 = new GOST3411Digest(); gost3411.update(message, 0, message.length); byte[] hashmessage = new byte[gost3411.getDigestSize()]; gost3411.doFinal(hashmessage, 0); BigInteger[] sig = ecgost3410.generateSignature(hashmessage); ecgost3410.init(false, pair.getPublic()); if (!ecgost3410.verifySignature(hashmessage, sig[0], sig[1])) { fail("signature fails"); } }