1515 */
1616package dev .sigstore ;
1717
18- import static com .google .common .hash .Hashing .sha256 ;
19-
20- import com .google .common .hash .HashFunction ;
18+ import java .math .BigInteger ;
2119import java .security .PublicKey ;
2220import java .security .interfaces .ECPublicKey ;
2321import java .security .interfaces .RSAPublicKey ;
22+ import java .util .Map ;
2423
2524public class AlgorithmRegistry {
2625
26+ // order to differentiate between the various ec curves
27+ private static final Map <BigInteger , SigningAlgorithm > ECDSA_ORDERS =
28+ Map .of (
29+ new BigInteger (
30+ "115792089210356248762697446949407573529996955224135760342422259061068512044369" ),
31+ SigningAlgorithm .PKIX_ECDSA_P256_SHA_256 ,
32+ new BigInteger (
33+ "39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643" ),
34+ SigningAlgorithm .PKIX_ECDSA_P384_SHA_384 ,
35+ new BigInteger (
36+ "6864797660130609714981900799081393217269435300143305409394463459185543183397655394245057746333217197532963996371363321113864768612440380340372808892707005449" ),
37+ SigningAlgorithm .PKIX_ECDSA_P521_SHA_512 );
38+
2739 /**
2840 * Determine the signing algorithm based on the public key.
2941 *
@@ -53,11 +65,12 @@ public static SigningAlgorithm getSigningAlgorithm(PublicKey publicKey)
5365 if ("EC" .equals (algorithm ) || "ECDSA" .equals (algorithm )) {
5466 if (publicKey instanceof ECPublicKey ) {
5567 var ecKey = (ECPublicKey ) publicKey ;
56- int fieldSize = ecKey .getParams ().getCurve ().getField ().getFieldSize ();
57- if (fieldSize == 256 ) {
58- return SigningAlgorithm .PKIX_ECDSA_P256_SHA_256 ;
68+ var order = ecKey .getParams ().getOrder ();
69+ var signingAlgorithm = ECDSA_ORDERS .get (order );
70+ if (signingAlgorithm == null ) {
71+ throw new UnsupportedAlgorithmException ("Unsupported EC key with order: " + order );
5972 }
60- throw new UnsupportedAlgorithmException ( "Unsupported EC field size: " + fieldSize ) ;
73+ return signingAlgorithm ;
6174 }
6275 throw new IllegalStateException ("EC/ECDSA key must be an instance of ECPublicKey" );
6376 }
@@ -70,9 +83,9 @@ public enum SigningAlgorithm {
7083 PKIX_RSA_PKCS1V15_4096_SHA256 (HashAlgorithm .SHA2_256 ),
7184
7285 // ECDSA
73- PKIX_ECDSA_P256_SHA_256 (HashAlgorithm .SHA2_256 );
74- // TODO: PKIX_ECDSA_P384_SHA_384(HashAlgorithm.SHA2_384),
75- // TODO: PKIX_ECDSA_P521_SHA_512(HashAlgorithm.SHA2_512);
86+ PKIX_ECDSA_P256_SHA_256 (HashAlgorithm .SHA2_256 ),
87+ PKIX_ECDSA_P384_SHA_384 (HashAlgorithm .SHA2_384 ),
88+ PKIX_ECDSA_P521_SHA_512 (HashAlgorithm .SHA2_512 );
7689
7790 private final HashAlgorithm hashAlgorithm ;
7891
@@ -86,30 +99,30 @@ public HashAlgorithm getHashAlgorithm() {
8699 }
87100
88101 public enum HashAlgorithm {
89- SHA2_256 ("SHA256" , 32 , sha256 ());
90- // TODO: SHA2_384("SHA384", 48, sha384() ),
91- // TODO: SHA2_512("SHA512", 64, sha512() );
102+ SHA2_256 ("SHA256" , "sha256" , 32 ),
103+ SHA2_384 ("SHA384" , "sha384" , 48 ),
104+ SHA2_512 ("SHA512" , "sha512" , 64 );
92105
93106 private final String name ;
107+ private final String lowercase ;
94108 private final int length ;
95- private final HashFunction hashFunction ;
96109
97- HashAlgorithm (String name , int length , HashFunction hashFunction ) {
110+ HashAlgorithm (String name , String lowercase , int length ) {
98111 this .name = name ;
112+ this .lowercase = lowercase ;
99113 this .length = length ;
100- this .hashFunction = hashFunction ;
101114 }
102115
103116 public String toString () {
104117 return name ;
105118 }
106119
107- public int getLength () {
108- return length ;
120+ public String toLowercaseString () {
121+ return lowercase ;
109122 }
110123
111- HashFunction getHashFunction () {
112- return hashFunction ;
124+ public int getLength () {
125+ return length ;
113126 }
114127 }
115128}
0 commit comments