@@ -4818,9 +4818,8 @@ index 1c5e4c742d..2fa4a38e44 100644
48184818 }
48194819- func SignMarshalECDSA(priv *PrivateKeyECDSA, hash []byte) ([]byte, error) {
48204820+ func SignECDSA(priv *PrivateKeyECDSA, hash []byte, h crypto.Hash) (r, s BigInt, err error) {
4821- panic("boringcrypto: not available")
4822- }
4823- - func VerifyECDSA(pub *PublicKeyECDSA, hash []byte, sig []byte) bool {
4821+ + panic("boringcrypto: not available")
4822+ + }
48244823+ func SignMarshalECDSA(priv *PrivateKeyECDSA, hash []byte, h crypto.Hash) ([]byte, error) {
48254824+ panic("boringcrypto: not available")
48264825+ }
@@ -4841,8 +4840,9 @@ index 1c5e4c742d..2fa4a38e44 100644
48414840+ panic("boringcrypto: not available")
48424841+ }
48434842+ func ECDH(priv *PrivateKeyECDH, pub *PublicKeyECDH) ([]byte, error) {
4844- + panic("boringcrypto: not available")
4845- + }
4843+ panic("boringcrypto: not available")
4844+ }
4845+ - func VerifyECDSA(pub *PublicKeyECDSA, hash []byte, sig []byte) bool {
48464846+ func GenerateKeyECDH(curve string) (*PrivateKeyECDH, []byte, error) {
48474847 panic("boringcrypto: not available")
48484848 }
@@ -6480,8 +6480,7 @@ index fa693ea319..75ba7a8a59 100644
64806480 key := C._goboringcrypto_RSA_new()
64816481 if key == nil {
64826482- return nil, fail("RSA_new")
6483- + return nil, NewOpenSSLError("RSA_new failed")
6484- }
6483+ - }
64856484- if !bigToBn(&key.n, N) ||
64866485- !bigToBn(&key.e, E) ||
64876486- !bigToBn(&key.d, D) ||
@@ -6491,6 +6490,8 @@ index fa693ea319..75ba7a8a59 100644
64916490- !bigToBn(&key.dmq1, Dq) ||
64926491- !bigToBn(&key.iqmp, Qinv) {
64936492- return nil, fail("BN_bin2bn")
6493+ + return nil, NewOpenSSLError("RSA_new failed")
6494+ + }
64946495+ var n, e, d, p, q, dp, dq, qinv *C.GO_BIGNUM
64956496+ n = bigToBN(N)
64966497+ e = bigToBN(E)
@@ -6686,6 +6687,14 @@ index fa693ea319..75ba7a8a59 100644
66866687- // it, and lengths < -2, before we convert to the BoringSSL sentinel values.
66876688- if saltLen <= -2 {
66886689- return nil, invalidSaltLenErr
6690+ - }
6691+ -
6692+ - // BoringSSL uses sentinel salt length values like we do, but the values don't
6693+ - // fully match what we use. We both use -1 for salt length equal to hash length,
6694+ - // but BoringSSL uses -2 to mean maximal size where we use 0. In the latter
6695+ - // case convert to the BoringSSL version.
6696+ - if saltLen == 0 {
6697+ - saltLen = -2
66896698+ switch saltLen {
66906699+ case saltLengthAuto:
66916700+ saltLen = C.GO_RSA_PSS_SALTLEN_AUTO
@@ -6698,14 +6707,6 @@ index fa693ea319..75ba7a8a59 100644
66986707+ return nil, invalidSaltLenErr
66996708+ }
67006709 }
6701- -
6702- - // BoringSSL uses sentinel salt length values like we do, but the values don't
6703- - // fully match what we use. We both use -1 for salt length equal to hash length,
6704- - // but BoringSSL uses -2 to mean maximal size where we use 0. In the latter
6705- - // case convert to the BoringSSL version.
6706- - if saltLen == 0 {
6707- - saltLen = -2
6708- - }
67096710-
67106711 var out []byte
67116712- var outLen C.size_t
@@ -6728,6 +6729,14 @@ index fa693ea319..75ba7a8a59 100644
67286729- // it, and lengths < -2, before we convert to the BoringSSL sentinel values.
67296730- if saltLen <= -2 {
67306731- return invalidSaltLenErr
6732+ - }
6733+ -
6734+ - // BoringSSL uses sentinel salt length values like we do, but the values don't
6735+ - // fully match what we use. We both use -1 for salt length equal to hash length,
6736+ - // but BoringSSL uses -2 to mean maximal size where we use 0. In the latter
6737+ - // case convert to the BoringSSL version.
6738+ - if saltLen == 0 {
6739+ - saltLen = -2
67316740+ switch saltLen {
67326741+ case saltLengthAuto:
67336742+ saltLen = C.GO_RSA_PSS_SALTLEN_AUTO
@@ -6740,14 +6749,6 @@ index fa693ea319..75ba7a8a59 100644
67406749+ return invalidSaltLenErr
67416750+ }
67426751 }
6743- -
6744- - // BoringSSL uses sentinel salt length values like we do, but the values don't
6745- - // fully match what we use. We both use -1 for salt length equal to hash length,
6746- - // but BoringSSL uses -2 to mean maximal size where we use 0. In the latter
6747- - // case convert to the BoringSSL version.
6748- - if saltLen == 0 {
6749- - saltLen = -2
6750- - }
67516752-
67526753 if pub.withKey(func(key *C.GO_RSA) C.int {
67536754- return C._goboringcrypto_RSA_verify_pss_mgf1(key, base(hashed), C.size_t(len(hashed)),
@@ -6792,69 +6793,66 @@ index fa693ea319..75ba7a8a59 100644
67926793 return out[:outLen], nil
67936794 }
67946795
6796+ - md := cryptoHashToMD(h)
6797+ - if md == nil {
6798+ - return nil, errors.New("crypto/rsa: unsupported hash function: " + strconv.Itoa(int(h)))
67956799+ var out []byte
67966800+ var outLen C.size_t
67976801+
67986802+ if priv.withKey(func(key *C.GO_RSA) C.int {
67996803+ return C._goboringcrypto_EVP_RSA_sign(md, base(msg), C.uint(len(msg)), base(out), &outLen, key)
68006804+ }) == 0 {
68016805+ return nil, NewOpenSSLError("RSA_sign")
6802- + }
6806+ }
6807+ - nid := C._goboringcrypto_EVP_MD_type(md)
68036808+ return out[:outLen], nil
68046809+ }
68056810+
68066811+ func signRSAPKCS1v15Raw(priv *PrivateKeyRSA, msg []byte, md *C.GO_EVP_MD) ([]byte, error) {
6807- + var out []byte
6812+ var out []byte
6813+ - var outLen C.uint
68086814+ var outLen C.size_t
68096815+ PanicIfStrictFIPS("You must provide a raw unhashed message for PKCS1v15 signing and use HashSignPKCS1v15 instead of SignPKCS1v15")
68106816+
6811- + if priv.withKey(func(key *C.GO_RSA) C.int {
6812- + out = make([]byte, C._goboringcrypto_RSA_size(key))
6817+ if priv.withKey(func(key *C.GO_RSA) C.int {
6818+ out = make([]byte, C._goboringcrypto_RSA_size(key))
6819+ - return C._goboringcrypto_RSA_sign(nid, base(hashed), C.uint(len(hashed)),
6820+ - base(out), &outLen, key)
68136821+ outLen = C.size_t(len(out))
68146822+ return C._goboringcrypto_EVP_sign_raw(md, nil, base(msg),
68156823+ C.size_t(len(msg)), base(out), &outLen, key)
6816- + }) == 0 {
6824+ }) == 0 {
6825+ - return nil, fail("RSA_sign")
68176826+ return nil, NewOpenSSLError("RSA_sign")
6818- + }
6827+ }
68196828+ runtime.KeepAlive(priv)
6820- + return out[:outLen], nil
6821- + }
6822- +
6829+ return out[:outLen], nil
6830+ }
6831+
6832+ - func VerifyRSAPKCS1v15(pub *PublicKeyRSA, h crypto.Hash, hashed, sig []byte) error {
6833+ - if h == 0 {
6834+ - var out []byte
6835+ - var outLen C.size_t
68236836+ func VerifyRSAPKCS1v15(pub *PublicKeyRSA, h crypto.Hash, msg, sig []byte, msgIsHashed bool) error {
68246837+ if h == 0 && ExecutingTest() {
68256838+ return verifyRSAPKCS1v15Raw(pub, msg, sig)
68266839+ }
68276840+
6828- md := cryptoHashToMD(h)
6829- if md == nil {
6830- - return nil, errors.New("crypto/rsa: unsupported hash function: " + strconv.Itoa(int(h)))
6841+ + md := cryptoHashToMD(h)
6842+ + if md == nil {
68316843+ return errors.New("crypto/rsa: unsupported hash function")
6832- }
6833- - nid := C._goboringcrypto_EVP_MD_type(md)
6834- - var out []byte
6835- - var outLen C.uint
6836- - if priv.withKey(func(key *C.GO_RSA) C.int {
6837- - out = make([]byte, C._goboringcrypto_RSA_size(key))
6838- - return C._goboringcrypto_RSA_sign(nid, base(hashed), C.uint(len(hashed)),
6839- - base(out), &outLen, key)
6844+ + }
68406845+
68416846+ if pub.withKey(func(key *C.GO_RSA) C.int {
68426847+ size := int(C._goboringcrypto_RSA_size(key))
68436848+ if len(sig) < size {
68446849+ return 0
68456850+ }
68466851+ return 1
6847- }) == 0 {
6848- - return nil, fail("RSA_sign")
6852+ + }) == 0 {
68496853+ return errors.New("crypto/rsa: verification error")
6850- }
6851- - return out[:outLen], nil
6852- - }
6853-
6854- - func VerifyRSAPKCS1v15(pub *PublicKeyRSA, h crypto.Hash, hashed, sig []byte) error {
6855- - if h == 0 {
6856- - var out []byte
6857- - var outLen C.size_t
6854+ + }
6855+ +
68586856+ if msgIsHashed {
68596857+ PanicIfStrictFIPS("You must provide a raw unhashed message for PKCS1v15 verification and use HashVerifyPKCS1v15 instead of VerifyPKCS1v15")
68606858+ nid := C._goboringcrypto_EVP_MD_type(md)
0 commit comments