Search in sources :

Example 6 with DigestOutputStream

use of org.xipki.security.pkcs11.DigestOutputStream in project xipki by xipki.

the class AbstractP11ECDSASignatureSpi method engineInitSign.

@Override
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    if (!(privateKey instanceof P11PrivateKey)) {
        throw new InvalidKeyException("privateKey is not instanceof " + P11PrivateKey.class.getName());
    }
    String algo = privateKey.getAlgorithm();
    if (!("EC".equals(algo) || "ECDSA".equals(algo))) {
        throw new InvalidKeyException("privateKey is not an EC private key: " + algo);
    }
    this.signingKey = (P11PrivateKey) privateKey;
    if (signingKey.supportsMechanism(PKCS11Constants.CKM_ECDSA)) {
        mechanism = PKCS11Constants.CKM_ECDSA;
        if (hashAlgo == null) {
            outputStream = new ByteArrayOutputStream();
        } else {
            outputStream = new DigestOutputStream(hashAlgo.createDigest());
        }
    } else {
        if (hashAlgo == HashAlgo.SHA1 && signingKey.supportsMechanism(PKCS11Constants.CKM_ECDSA_SHA1)) {
            mechanism = PKCS11Constants.CKM_ECDSA_SHA1;
        } else if (hashAlgo == HashAlgo.SHA224 && signingKey.supportsMechanism(PKCS11Constants.CKM_ECDSA_SHA224)) {
            mechanism = PKCS11Constants.CKM_ECDSA_SHA224;
        } else if (hashAlgo == HashAlgo.SHA256 && signingKey.supportsMechanism(PKCS11Constants.CKM_ECDSA_SHA256)) {
            mechanism = PKCS11Constants.CKM_ECDSA_SHA256;
        } else if (hashAlgo == HashAlgo.SHA384 && signingKey.supportsMechanism(PKCS11Constants.CKM_ECDSA_SHA384)) {
            mechanism = PKCS11Constants.CKM_ECDSA_SHA384;
        } else if (hashAlgo == HashAlgo.SHA512 && signingKey.supportsMechanism(PKCS11Constants.CKM_ECDSA_SHA512)) {
            mechanism = PKCS11Constants.CKM_ECDSA_SHA512;
        } else if (hashAlgo == HashAlgo.SHA3_224 && signingKey.supportsMechanism(PKCS11Constants.CKM_ECDSA_SHA3_224)) {
            mechanism = PKCS11Constants.CKM_ECDSA_SHA3_224;
        } else if (hashAlgo == HashAlgo.SHA3_256 && signingKey.supportsMechanism(PKCS11Constants.CKM_ECDSA_SHA3_256)) {
            mechanism = PKCS11Constants.CKM_ECDSA_SHA3_256;
        } else if (hashAlgo == HashAlgo.SHA3_384 && signingKey.supportsMechanism(PKCS11Constants.CKM_ECDSA_SHA3_384)) {
            mechanism = PKCS11Constants.CKM_ECDSA_SHA3_384;
        } else if (hashAlgo == HashAlgo.SHA3_512 && signingKey.supportsMechanism(PKCS11Constants.CKM_ECDSA_SHA3_512)) {
            mechanism = PKCS11Constants.CKM_ECDSA_SHA3_512;
        } else {
            throw new InvalidKeyException("privateKey and algorithm does not match");
        }
        outputStream = new ByteArrayOutputStream();
    }
    this.signingKey = (P11PrivateKey) privateKey;
}
Also used : DigestOutputStream(org.xipki.security.pkcs11.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DigestOutputStream (org.xipki.security.pkcs11.DigestOutputStream)6 InvalidKeyException (java.security.InvalidKeyException)3 SignatureException (java.security.SignatureException)3 P11TokenException (org.xipki.security.exception.P11TokenException)3 XiSecurityException (org.xipki.security.exception.XiSecurityException)3 IOException (java.io.IOException)2 ECPublicKey (java.security.interfaces.ECPublicKey)1 ECPoint (java.security.spec.ECPoint)1 P11ByteArrayParams (org.xipki.security.pkcs11.P11ByteArrayParams)1