@@ -86,19 +86,29 @@ func NewRSAPSSSigner(keyID string, key rsa.PrivateKey, config *SignConfig, field
8686// NewP256Signer returns a new Signer structure. Key is an elliptic curve P-256 private key.
8787// Config may be nil for a default configuration.
8888func NewP256Signer (keyID string , key ecdsa.PrivateKey , config * SignConfig , fields Fields ) (* Signer , error ) {
89+ return newECCSigner (keyID , key , config , fields , elliptic .P256 (), "P-256" , "ecdsa-p256-sha256" )
90+ }
91+
92+ // NewP384Signer returns a new Signer structure. Key is an elliptic curve P-384 private key.
93+ // Config may be nil for a default configuration.
94+ func NewP384Signer (keyID string , key ecdsa.PrivateKey , config * SignConfig , fields Fields ) (* Signer , error ) {
95+ return newECCSigner (keyID , key , config , fields , elliptic .P384 (), "P-384" , "ecdsa-p384-sha384" )
96+ }
97+
98+ func newECCSigner (keyID string , key ecdsa.PrivateKey , config * SignConfig , fields Fields , curve elliptic.Curve , curveName , alg string ) (* Signer , error ) {
8999 if keyID == "" {
90100 return nil , fmt .Errorf ("keyID must not be empty" )
91101 }
92- if key .Curve != elliptic . P256 () {
93- return nil , fmt .Errorf ("key curve must be P-256" )
102+ if key .Curve != curve {
103+ return nil , fmt .Errorf ("key curve must be %s" , curveName )
94104 }
95105 if config == nil {
96106 config = NewSignConfig ()
97107 }
98108 return & Signer {
99109 keyID : keyID ,
100110 key : key ,
101- alg : "ecdsa-p256-sha256" ,
111+ alg : alg ,
102112 config : config ,
103113 fields : fields ,
104114 }, nil
@@ -196,6 +206,10 @@ func (s Signer) sign(buff []byte) ([]byte, error) {
196206 hashed := sha256 .Sum256 (buff )
197207 key := s .key .(ecdsa.PrivateKey )
198208 return ecdsaSignRaw (rand .Reader , & key , hashed [:])
209+ case "ecdsa-p384-sha384" :
210+ hashed := sha512 .Sum384 (buff )
211+ key := s .key .(ecdsa.PrivateKey )
212+ return ecdsaSignRaw (rand .Reader , & key , hashed [:])
199213 case "ed25519" :
200214 key := s .key .(ed25519.PrivateKey )
201215 return ed25519 .Sign (key , buff ), nil
@@ -277,19 +291,29 @@ func NewRSAPSSVerifier(keyID string, key rsa.PublicKey, config *VerifyConfig, fi
277291// NewP256Verifier generates a new Verifier for ECDSA (P-256) signatures. Set config to nil for a default configuration.
278292// Fields is the list of required headers and fields, which may be empty (but this is typically insecure).
279293func NewP256Verifier (keyID string , key ecdsa.PublicKey , config * VerifyConfig , fields Fields ) (* Verifier , error ) {
294+ return newECCVerifier (keyID , key , config , fields , elliptic .P256 (), "P-256" , "ecdsa-p256-sha256" )
295+ }
296+
297+ // NewP384Verifier generates a new Verifier for ECDSA (P-384) signatures. Set config to nil for a default configuration.
298+ // Fields is the list of required headers and fields, which may be empty (but this is typically insecure).
299+ func NewP384Verifier (keyID string , key ecdsa.PublicKey , config * VerifyConfig , fields Fields ) (* Verifier , error ) {
300+ return newECCVerifier (keyID , key , config , fields , elliptic .P384 (), "P-384" , "ecdsa-p384-sha384" )
301+ }
302+
303+ func newECCVerifier (keyID string , key ecdsa.PublicKey , config * VerifyConfig , fields Fields , curve elliptic.Curve , curveName , alg string ) (* Verifier , error ) {
280304 if config == nil {
281305 config = NewVerifyConfig ()
282306 }
283307 if config .verifyKeyID && keyID == "" {
284308 return nil , fmt .Errorf ("keyID should not be empty" )
285309 }
286- if key .Curve != elliptic . P256 () {
287- return nil , fmt .Errorf ("key curve must be P-256" )
310+ if key .Curve != curve {
311+ return nil , fmt .Errorf ("key curve must be %s" , curveName )
288312 }
289313 return & Verifier {
290314 keyID : keyID ,
291315 key : key ,
292- alg : "ecdsa-p256-sha256" ,
316+ alg : alg ,
293317 config : config ,
294318 fields : fields ,
295319 }, nil
@@ -385,6 +409,10 @@ func (v Verifier) verify(buff []byte, sig []byte) (bool, error) {
385409 hashed := sha256 .Sum256 (buff )
386410 key := v .key .(ecdsa.PublicKey )
387411 return ecdsaVerifyRaw (& key , hashed [:], sig )
412+ case "ecdsa-p384-sha384" :
413+ hashed := sha512 .Sum384 (buff )
414+ key := v .key .(ecdsa.PublicKey )
415+ return ecdsaVerifyRaw (& key , hashed [:], sig )
388416 case "ed25519" :
389417 key := v .key .(ed25519.PublicKey )
390418 verified := ed25519 .Verify (key , buff , sig )
0 commit comments