3232import com .google .cose .utils .CborUtils ;
3333import com .google .cose .utils .CoseUtils ;
3434import com .google .cose .utils .Headers ;
35+ import com .google .crypto .tink .subtle .EcdsaSignJce ;
36+ import com .google .crypto .tink .subtle .EcdsaVerifyJce ;
3537import com .google .crypto .tink .subtle .EllipticCurves .CurveType ;
38+ import com .google .crypto .tink .subtle .EllipticCurves .EcdsaEncoding ;
39+ import com .google .crypto .tink .subtle .Enums .HashType ;
3640import java .security .GeneralSecurityException ;
37- import java .security .InvalidKeyException ;
3841import java .security .KeyPair ;
39- import java .security .NoSuchAlgorithmException ;
40- import java .security .NoSuchProviderException ;
4142import java .security .PublicKey ;
4243import java .security .Signature ;
43- import java .security .SignatureException ;
4444import java .security .interfaces .ECPrivateKey ;
4545import java .security .interfaces .ECPublicKey ;
4646import java .security .spec .ECPoint ;
@@ -257,18 +257,17 @@ public byte[] sign(Algorithm algorithm, byte[] message, String provider)
257257 verifyAlgorithmAllowedByKey (algorithm );
258258 verifyOperationAllowedByKey (Headers .KEY_OPERATIONS_SIGN );
259259
260+ ECPrivateKey key = (ECPrivateKey ) keyPair .getPrivate ();
260261 try {
261- Signature signature ;
262262 if (provider == null ) {
263- signature = Signature .getInstance (algorithm .getJavaAlgorithmId ());
264- } else {
265- signature = Signature .getInstance (algorithm .getJavaAlgorithmId (), provider );
263+ return new EcdsaSignJce (key , getHashType (algorithm ), EcdsaEncoding .DER ).sign (message );
266264 }
267- signature .initSign (keyPair .getPrivate ());
265+
266+ Signature signature = Signature .getInstance (algorithm .getJavaAlgorithmId (), provider );
267+ signature .initSign (key );
268268 signature .update (message );
269269 return signature .sign ();
270- } catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException
271- | NoSuchProviderException e ) {
270+ } catch (GeneralSecurityException e ) {
272271 throw new CoseException ("Error while signing message." , e );
273272 }
274273 }
@@ -279,21 +278,35 @@ public void verify(Algorithm algorithm, byte[] message, byte[] signature, String
279278 verifyAlgorithmAllowedByKey (algorithm );
280279 verifyOperationAllowedByKey (Headers .KEY_OPERATIONS_VERIFY );
281280
281+ ECPublicKey key = (ECPublicKey ) keyPair .getPublic ();
282282 try {
283- Signature signer ;
284283 if (provider == null ) {
285- signer = Signature . getInstance (algorithm . getJavaAlgorithmId ());
286- } else {
287- signer = Signature . getInstance ( algorithm . getJavaAlgorithmId (), provider ) ;
284+ new EcdsaVerifyJce ( key , getHashType (algorithm ), EcdsaEncoding . DER )
285+ . verify ( signature , message );
286+ return ;
288287 }
289- signer .initVerify (keyPair .getPublic ());
288+
289+ Signature signer = Signature .getInstance (algorithm .getJavaAlgorithmId (), provider );
290+ signer .initVerify (key );
290291 signer .update (message );
291292 if (!signer .verify (signature )) {
292293 throw new CoseException ("Failed verification." );
293294 }
294- } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeyException
295- | SignatureException e ) {
295+ } catch (GeneralSecurityException e ) {
296296 throw new CoseException ("Error while verifying " , e );
297297 }
298298 }
299+
300+ private static HashType getHashType (Algorithm algorithm ) {
301+ switch (algorithm ) {
302+ case SIGNING_ALGORITHM_ECDSA_SHA_256 :
303+ return HashType .SHA256 ;
304+ case SIGNING_ALGORITHM_ECDSA_SHA_384 :
305+ return HashType .SHA384 ;
306+ case SIGNING_ALGORITHM_ECDSA_SHA_512 :
307+ return HashType .SHA512 ;
308+ default :
309+ throw new IllegalArgumentException ("Unsupported algorithm " + algorithm );
310+ }
311+ }
299312}
0 commit comments