Search in sources :

Example 1 with MacOutputStream

use of com.github.zhenwei.provider.jcajce.io.MacOutputStream in project LinLong-Java by zhenwei1108.

the class JceKEKAuthenticatedRecipient method getRecipientOperator.

public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentMacAlgorithm, byte[] encryptedContentEncryptionKey) throws CMSException {
    final Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentMacAlgorithm, encryptedContentEncryptionKey);
    final Mac dataMac = contentHelper.createContentMac(secretKey, contentMacAlgorithm);
    return new RecipientOperator(new MacCalculator() {

        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return contentMacAlgorithm;
        }

        public GenericKey getKey() {
            return new JceGenericKey(contentMacAlgorithm, secretKey);
        }

        public OutputStream getOutputStream() {
            return new MacOutputStream(dataMac);
        }

        public byte[] getMac() {
            return dataMac.doFinal();
        }
    });
}
Also used : JceGenericKey(com.github.zhenwei.pkix.operator.jcajce.JceGenericKey) OutputStream(java.io.OutputStream) MacOutputStream(com.github.zhenwei.provider.jcajce.io.MacOutputStream) RecipientOperator(com.github.zhenwei.pkix.cms.RecipientOperator) MacOutputStream(com.github.zhenwei.provider.jcajce.io.MacOutputStream) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) JceGenericKey(com.github.zhenwei.pkix.operator.jcajce.JceGenericKey) Key(java.security.Key) SecretKey(javax.crypto.SecretKey) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) JceGenericKey(com.github.zhenwei.pkix.operator.jcajce.JceGenericKey) Mac(javax.crypto.Mac) MacCalculator(com.github.zhenwei.pkix.operator.MacCalculator) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 2 with MacOutputStream

use of com.github.zhenwei.provider.jcajce.io.MacOutputStream in project LinLong-Java by zhenwei1108.

the class JcePKCS12MacCalculatorBuilder method build.

public MacCalculator build(final char[] password) throws OperatorCreationException {
    if (random == null) {
        random = new SecureRandom();
    }
    try {
        final Mac mac = helper.createMac(algorithm.getId());
        saltLength = mac.getMacLength();
        final byte[] salt = new byte[saltLength];
        random.nextBytes(salt);
        PBEParameterSpec defParams = new PBEParameterSpec(salt, iterationCount);
        final SecretKey key = new PKCS12Key(password);
        mac.init(key, defParams);
        return new MacCalculator() {

            public AlgorithmIdentifier getAlgorithmIdentifier() {
                return new AlgorithmIdentifier(algorithm, new PKCS12PBEParams(salt, iterationCount));
            }

            public OutputStream getOutputStream() {
                return new MacOutputStream(mac);
            }

            public byte[] getMac() {
                return mac.doFinal();
            }

            public GenericKey getKey() {
                return new GenericKey(getAlgorithmIdentifier(), key.getEncoded());
            }
        };
    } catch (Exception e) {
        throw new OperatorCreationException("unable to create MAC calculator: " + e.getMessage(), e);
    }
}
Also used : SecureRandom(java.security.SecureRandom) MacOutputStream(com.github.zhenwei.provider.jcajce.io.MacOutputStream) Mac(javax.crypto.Mac) MacCalculator(com.github.zhenwei.pkix.operator.MacCalculator) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) SecretKey(javax.crypto.SecretKey) PKCS12PBEParams(com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams) PKCS12Key(com.github.zhenwei.provider.jcajce.PKCS12Key) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 3 with MacOutputStream

use of com.github.zhenwei.provider.jcajce.io.MacOutputStream in project LinLong-Java by zhenwei1108.

the class JcePKCS12MacCalculatorBuilderProvider method get.

public PKCS12MacCalculatorBuilder get(final AlgorithmIdentifier algorithmIdentifier) {
    return new PKCS12MacCalculatorBuilder() {

        public MacCalculator build(final char[] password) throws OperatorCreationException {
            final PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algorithmIdentifier.getParameters());
            try {
                final ASN1ObjectIdentifier algorithm = algorithmIdentifier.getAlgorithm();
                final Mac mac = helper.createMac(algorithm.getId());
                PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
                final SecretKey key = new PKCS12Key(password);
                mac.init(key, defParams);
                return new MacCalculator() {

                    public AlgorithmIdentifier getAlgorithmIdentifier() {
                        return new AlgorithmIdentifier(algorithm, pbeParams);
                    }

                    public OutputStream getOutputStream() {
                        return new MacOutputStream(mac);
                    }

                    public byte[] getMac() {
                        return mac.doFinal();
                    }

                    public GenericKey getKey() {
                        return new GenericKey(getAlgorithmIdentifier(), key.getEncoded());
                    }
                };
            } catch (Exception e) {
                throw new OperatorCreationException("unable to create MAC calculator: " + e.getMessage(), e);
            }
        }

        public AlgorithmIdentifier getDigestAlgorithmIdentifier() {
            return new AlgorithmIdentifier(algorithmIdentifier.getAlgorithm(), DERNull.INSTANCE);
        }
    };
}
Also used : MacOutputStream(com.github.zhenwei.provider.jcajce.io.MacOutputStream) Mac(javax.crypto.Mac) MacCalculator(com.github.zhenwei.pkix.operator.MacCalculator) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) SecretKey(javax.crypto.SecretKey) PKCS12PBEParams(com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams) PKCS12MacCalculatorBuilder(com.github.zhenwei.pkix.pkcs.PKCS12MacCalculatorBuilder) PKCS12Key(com.github.zhenwei.provider.jcajce.PKCS12Key) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 4 with MacOutputStream

use of com.github.zhenwei.provider.jcajce.io.MacOutputStream in project LinLong-Java by zhenwei1108.

the class JceKeyTransAuthenticatedRecipient method getRecipientOperator.

public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentMacAlgorithm, byte[] encryptedContentEncryptionKey) throws CMSException {
    final Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentMacAlgorithm, encryptedContentEncryptionKey);
    final Mac dataMac = contentHelper.createContentMac(secretKey, contentMacAlgorithm);
    return new RecipientOperator(new MacCalculator() {

        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return contentMacAlgorithm;
        }

        public GenericKey getKey() {
            return new JceGenericKey(contentMacAlgorithm, secretKey);
        }

        public OutputStream getOutputStream() {
            return new MacOutputStream(dataMac);
        }

        public byte[] getMac() {
            return dataMac.doFinal();
        }
    });
}
Also used : JceGenericKey(com.github.zhenwei.pkix.operator.jcajce.JceGenericKey) OutputStream(java.io.OutputStream) MacOutputStream(com.github.zhenwei.provider.jcajce.io.MacOutputStream) RecipientOperator(com.github.zhenwei.pkix.cms.RecipientOperator) MacOutputStream(com.github.zhenwei.provider.jcajce.io.MacOutputStream) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) JceGenericKey(com.github.zhenwei.pkix.operator.jcajce.JceGenericKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) JceGenericKey(com.github.zhenwei.pkix.operator.jcajce.JceGenericKey) Mac(javax.crypto.Mac) MacCalculator(com.github.zhenwei.pkix.operator.MacCalculator) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 5 with MacOutputStream

use of com.github.zhenwei.provider.jcajce.io.MacOutputStream in project LinLong-Java by zhenwei1108.

the class JcePasswordAuthenticatedRecipient method getRecipientOperator.

public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentMacAlgorithm, byte[] derivedKey, byte[] encryptedContentEncryptionKey) throws CMSException {
    final Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentMacAlgorithm, derivedKey, encryptedContentEncryptionKey);
    final Mac dataMac = helper.createContentMac(secretKey, contentMacAlgorithm);
    return new RecipientOperator(new MacCalculator() {

        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return contentMacAlgorithm;
        }

        public GenericKey getKey() {
            return new JceGenericKey(contentMacAlgorithm, secretKey);
        }

        public OutputStream getOutputStream() {
            return new MacOutputStream(dataMac);
        }

        public byte[] getMac() {
            return dataMac.doFinal();
        }
    });
}
Also used : JceGenericKey(com.github.zhenwei.pkix.operator.jcajce.JceGenericKey) OutputStream(java.io.OutputStream) MacOutputStream(com.github.zhenwei.provider.jcajce.io.MacOutputStream) RecipientOperator(com.github.zhenwei.pkix.cms.RecipientOperator) MacOutputStream(com.github.zhenwei.provider.jcajce.io.MacOutputStream) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) JceGenericKey(com.github.zhenwei.pkix.operator.jcajce.JceGenericKey) Key(java.security.Key) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) JceGenericKey(com.github.zhenwei.pkix.operator.jcajce.JceGenericKey) Mac(javax.crypto.Mac) MacCalculator(com.github.zhenwei.pkix.operator.MacCalculator) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Aggregations

AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)7 GenericKey (com.github.zhenwei.pkix.operator.GenericKey)7 MacCalculator (com.github.zhenwei.pkix.operator.MacCalculator)7 MacOutputStream (com.github.zhenwei.provider.jcajce.io.MacOutputStream)7 Mac (javax.crypto.Mac)7 RecipientOperator (com.github.zhenwei.pkix.cms.RecipientOperator)5 JceGenericKey (com.github.zhenwei.pkix.operator.jcajce.JceGenericKey)5 OutputStream (java.io.OutputStream)5 Key (java.security.Key)5 PrivateKey (java.security.PrivateKey)3 SecretKey (javax.crypto.SecretKey)3 PKCS12PBEParams (com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams)2 OperatorCreationException (com.github.zhenwei.pkix.operator.OperatorCreationException)2 PKCS12Key (com.github.zhenwei.provider.jcajce.PKCS12Key)2 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)2 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 PKCS12MacCalculatorBuilder (com.github.zhenwei.pkix.pkcs.PKCS12MacCalculatorBuilder)1 SecureRandom (java.security.SecureRandom)1