Search in sources :

Example 1 with ConcurrentBagEntry

use of org.xipki.util.concurrent.ConcurrentBagEntry in project xipki by xipki.

the class HashCalculator method hash.

public static byte[] hash(HashAlgo hashAlgo, byte[]... datas) {
    notNull(hashAlgo, "hashAlgo");
    notNull(datas, "datas");
    if (!MDS_MAP.containsKey(hashAlgo)) {
        throw new IllegalArgumentException("unknown hash algo " + hashAlgo);
    }
    ConcurrentBag<ConcurrentBagEntry<Digest>> mds = MDS_MAP.get(hashAlgo);
    ConcurrentBagEntry<Digest> md0 = null;
    for (int i = 0; i < 3; i++) {
        try {
            md0 = mds.borrow(10, TimeUnit.SECONDS);
            break;
        } catch (InterruptedException ex) {
        }
    }
    if (md0 == null) {
        throw new RuntimeOperatorException("could not get idle MessageDigest");
    }
    try {
        Digest md = md0.value();
        md.reset();
        for (byte[] data : datas) {
            if (data != null && data.length > 0) {
                md.update(data, 0, data.length);
            }
        }
        byte[] bytes = new byte[md.getDigestSize()];
        md.doFinal(bytes, 0);
        return bytes;
    } finally {
        mds.requite(md0);
    }
}
Also used : RuntimeOperatorException(org.bouncycastle.operator.RuntimeOperatorException) Digest(org.bouncycastle.crypto.Digest) ConcurrentBagEntry(org.xipki.util.concurrent.ConcurrentBagEntry)

Example 2 with ConcurrentBagEntry

use of org.xipki.util.concurrent.ConcurrentBagEntry in project xipki by xipki.

the class HashCalculator method hash.

// method hash
public static byte[] hash(HashAlgo hashAlgo, byte[] data, int offset, int len) {
    notNull(hashAlgo, "hashAlgo");
    notNull(data, "data");
    if (data.length - offset < len) {
        throw new IndexOutOfBoundsException("data.length - offset < len");
    }
    if (!MDS_MAP.containsKey(hashAlgo)) {
        throw new IllegalArgumentException("unknown hash algo " + hashAlgo);
    }
    ConcurrentBag<ConcurrentBagEntry<Digest>> mds = MDS_MAP.get(hashAlgo);
    ConcurrentBagEntry<Digest> md0 = null;
    for (int i = 0; i < 3; i++) {
        try {
            md0 = mds.borrow(10, TimeUnit.SECONDS);
            break;
        } catch (InterruptedException ex) {
        }
    }
    if (md0 == null) {
        throw new RuntimeOperatorException("could not get idle MessageDigest");
    }
    try {
        Digest md = md0.value();
        md.reset();
        md.update(data, offset, len);
        byte[] bytes = new byte[md.getDigestSize()];
        md.doFinal(bytes, 0);
        return bytes;
    } finally {
        mds.requite(md0);
    }
}
Also used : RuntimeOperatorException(org.bouncycastle.operator.RuntimeOperatorException) Digest(org.bouncycastle.crypto.Digest) ConcurrentBagEntry(org.xipki.util.concurrent.ConcurrentBagEntry)

Example 3 with ConcurrentBagEntry

use of org.xipki.util.concurrent.ConcurrentBagEntry in project xipki by xipki.

the class EmulatorP11Identity method init.

private synchronized void init() throws P11TokenException {
    if (initialized) {
        return;
    }
    try {
        if (this.publicKey instanceof RSAPublicKey) {
            String providerName = "BC";
            LOG.info("use provider {}", providerName);
            for (int i = 0; i < maxSessions; i++) {
                Cipher rsaCipher;
                try {
                    final String algo = "RSA/ECB/NoPadding";
                    rsaCipher = Cipher.getInstance(algo, providerName);
                    LOG.info("use cipher algorithm {}", algo);
                } catch (NoSuchPaddingException ex) {
                    throw new P11TokenException("NoSuchPadding", ex);
                } catch (NoSuchAlgorithmException ex) {
                    final String algo = "RSA/NONE/NoPadding";
                    try {
                        rsaCipher = Cipher.getInstance(algo, providerName);
                        LOG.info("use cipher algorithm {}", algo);
                    } catch (NoSuchPaddingException e1) {
                        throw new P11TokenException("NoSuchPadding", ex);
                    }
                }
                rsaCipher.init(Cipher.ENCRYPT_MODE, signingKey);
                rsaCiphers.add(new ConcurrentBagEntry<>(rsaCipher));
            }
        } else {
            String algorithm;
            if (this.publicKey instanceof ECPublicKey) {
                boolean sm2curve = GMUtil.isSm2primev2Curve(((ECPublicKey) this.publicKey).getParams().getCurve());
                algorithm = sm2curve ? null : "NONEwithECDSA";
            } else if (this.publicKey instanceof DSAPublicKey) {
                algorithm = "NONEwithDSA";
            } else if (this.publicKey instanceof EdDSAKey) {
                algorithm = null;
            } else if (this.publicKey instanceof XDHKey) {
                algorithm = null;
            } else {
                throw new P11TokenException("Currently only RSA, DSA, EC, EC Edwards and EC " + "Montgomery public key are supported, but not " + this.publicKey.getAlgorithm() + " (class: " + this.publicKey.getClass().getName() + ")");
            }
            if (algorithm != null) {
                for (int i = 0; i < maxSessions; i++) {
                    Signature dsaSignature = Signature.getInstance(algorithm, "BC");
                    dsaSignature.initSign((PrivateKey) signingKey, random);
                    dsaSignatures.add(new ConcurrentBagEntry<>(dsaSignature));
                }
            } else if (this.publicKey instanceof EdDSAKey) {
                algorithm = this.publicKey.getAlgorithm();
                for (int i = 0; i < maxSessions; i++) {
                    Signature signature = Signature.getInstance(algorithm, "BC");
                    signature.initSign((PrivateKey) signingKey);
                    eddsaSignatures.add(new ConcurrentBagEntry<>(signature));
                }
            } else if (this.publicKey instanceof XDHKey) {
            // do nothing. not suitable for sign.
            } else {
                for (int i = 0; i < maxSessions; i++) {
                    SM2Signer sm2signer = new SM2Signer(ECUtil.generatePrivateKeyParameter((PrivateKey) signingKey));
                    sm2Signers.add(new ConcurrentBagEntry<>(sm2signer));
                }
            }
        }
    } catch (GeneralSecurityException ex) {
        throw new P11TokenException(ex);
    } finally {
        initialized = true;
    }
}
Also used : ConcurrentBagEntry(org.xipki.util.concurrent.ConcurrentBagEntry) XDHKey(org.bouncycastle.jcajce.interfaces.XDHKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) EdDSAKey(org.bouncycastle.jcajce.interfaces.EdDSAKey) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher)

Aggregations

ConcurrentBagEntry (org.xipki.util.concurrent.ConcurrentBagEntry)3 Digest (org.bouncycastle.crypto.Digest)2 RuntimeOperatorException (org.bouncycastle.operator.RuntimeOperatorException)2 DSAPublicKey (java.security.interfaces.DSAPublicKey)1 ECPublicKey (java.security.interfaces.ECPublicKey)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)1 EdDSAKey (org.bouncycastle.jcajce.interfaces.EdDSAKey)1 XDHKey (org.bouncycastle.jcajce.interfaces.XDHKey)1