Search in sources :

Example 1 with DHKEKGenerator

use of com.github.zhenwei.core.crypto.agreement.kdf.DHKEKGenerator in project LinLong-Java by zhenwei1108.

the class BaseAgreementSpi method getSharedSecretBytes.

private byte[] getSharedSecretBytes(byte[] secret, String oidAlgorithm, int keySize) throws NoSuchAlgorithmException {
    if (kdf != null) {
        if (keySize < 0) {
            throw new NoSuchAlgorithmException("unknown algorithm encountered: " + oidAlgorithm);
        }
        byte[] keyBytes = new byte[keySize / 8];
        if (kdf instanceof DHKEKGenerator) {
            if (oidAlgorithm == null) {
                throw new NoSuchAlgorithmException("algorithm OID is null");
            }
            ASN1ObjectIdentifier oid;
            try {
                oid = new ASN1ObjectIdentifier(oidAlgorithm);
            } catch (IllegalArgumentException e) {
                throw new NoSuchAlgorithmException("no OID for algorithm: " + oidAlgorithm);
            }
            DHKDFParameters params = new DHKDFParameters(oid, keySize, secret, ukmParameters);
            kdf.init(params);
        } else {
            KDFParameters params = new KDFParameters(secret, ukmParameters);
            kdf.init(params);
        }
        kdf.generateBytes(keyBytes, 0, keyBytes.length);
        Arrays.clear(secret);
        return keyBytes;
    } else {
        if (keySize > 0) {
            byte[] keyBytes = new byte[keySize / 8];
            System.arraycopy(secret, 0, keyBytes, 0, keyBytes.length);
            Arrays.clear(secret);
            return keyBytes;
        }
        return secret;
    }
}
Also used : DHKDFParameters(com.github.zhenwei.core.crypto.agreement.kdf.DHKDFParameters) KDFParameters(com.github.zhenwei.core.crypto.params.KDFParameters) DHKEKGenerator(com.github.zhenwei.core.crypto.agreement.kdf.DHKEKGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DHKDFParameters(com.github.zhenwei.core.crypto.agreement.kdf.DHKDFParameters) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Aggregations

ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 DHKDFParameters (com.github.zhenwei.core.crypto.agreement.kdf.DHKDFParameters)1 DHKEKGenerator (com.github.zhenwei.core.crypto.agreement.kdf.DHKEKGenerator)1 KDFParameters (com.github.zhenwei.core.crypto.params.KDFParameters)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1