public static GOST28147ParameterSpec getGostSpec(CipherSuite cipherSuite) { return new GOST28147ParameterSpec(getGostSBox(cipherSuite)); }
protected AlgorithmParameterSpec localEngineGetParameterSpec( Class paramSpec) throws InvalidParameterSpecException { if (paramSpec == IvParameterSpec.class) { return new IvParameterSpec(iv); } if (paramSpec == GOST28147ParameterSpec.class || paramSpec == AlgorithmParameterSpec.class) { return new GOST28147ParameterSpec(sBox, iv); } throw new InvalidParameterSpecException("AlgorithmParameterSpec not recognized: " + paramSpec.getName()); }
protected AlgorithmParameterSpec localEngineGetParameterSpec( Class paramSpec) throws InvalidParameterSpecException { if (paramSpec == IvParameterSpec.class) { return new IvParameterSpec(iv); } if (paramSpec == GOST28147ParameterSpec.class || paramSpec == AlgorithmParameterSpec.class) { return new GOST28147ParameterSpec(sBox, iv); } throw new InvalidParameterSpecException("AlgorithmParameterSpec not recognized: " + paramSpec.getName()); }
protected AlgorithmParameters engineGenerateParameters() { if (random == null) { random = CryptoServicesRegistrar.getSecureRandom(); } random.nextBytes(iv); AlgorithmParameters params; try { params = createParametersInstance("GOST28147"); params.init(new GOST28147ParameterSpec(sBox, iv)); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return params; } }
private void gost28147MacFunc(byte[] in, byte[] out) { if (processedBytes == 1024) { processedBytes = 0; try { SecretKeySpec spec = new SecretKeySpec(key, meshCipher.getAlgorithm()); meshCipher.init(Cipher.DECRYPT_MODE, spec, new GOST28147ParameterSpec(S)); key = meshCipher.doFinal(GOST28147Cipher.C); workingKey = generateWorkingKey(key); } catch (GeneralSecurityException e) { throw new IllegalStateException("Could not mesh key!"); } } processedBytes += 8; int N1, N2, tmp; // tmp -> for saving N1 N1 = bytesToint(in, 0); N2 = bytesToint(in, 4); for (int k = 0; k < 2; k++) // 1-16 steps { for (int j = 0; j < 8; j++) { tmp = N1; N1 = N2 ^ gost28147_mainStep(N1, workingKey[j]); // CM2 N2 = tmp; } } intTobytes(N1, out, 0); intTobytes(N2, out, 4); }
private Cipher createCipher(int mode, char[] password, AlgorithmIdentifier algId) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchProviderException { PBES2Parameters alg = PBES2Parameters.getInstance(algId.getParameters()); PBKDF2Params func = PBKDF2Params.getInstance(alg.getKeyDerivationFunc().getParameters()); AlgorithmIdentifier encScheme = AlgorithmIdentifier.getInstance(alg.getEncryptionScheme()); SecretKeyFactory keyFact = helper.createSecretKeyFactory(alg.getKeyDerivationFunc().getAlgorithm().getId()); SecretKey key; if (func.isDefaultPrf()) { key = keyFact.generateSecret(new PBEKeySpec(password, func.getSalt(), validateIterationCount(func.getIterationCount()), keySizeProvider.getKeySize(encScheme))); } else { key = keyFact.generateSecret(new PBKDF2KeySpec(password, func.getSalt(), validateIterationCount(func.getIterationCount()), keySizeProvider.getKeySize(encScheme), func.getPrf())); } Cipher cipher = Cipher.getInstance(alg.getEncryptionScheme().getAlgorithm().getId()); ASN1Encodable encParams = alg.getEncryptionScheme().getParameters(); if (encParams instanceof ASN1OctetString) { cipher.init(mode, key, new IvParameterSpec(ASN1OctetString.getInstance(encParams).getOctets())); } else { // TODO: at the moment it's just GOST, but... GOST28147Parameters gParams = GOST28147Parameters.getInstance(encParams); cipher.init(mode, key, new GOST28147ParameterSpec(gParams.getEncryptionParamSet(), gParams.getIV())); } return cipher; }