Skip to content

Commit 1da9aa9

Browse files
committed
[dev.boringcrypto.go1.8] crypto/rsa: use BoringCrypto
Change-Id: Ibb92f0f8cb487f4d179b069e588e1cb266599384 Reviewed-on: https://go-review.googlesource.com/55479 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Adam Langley <[email protected]> Reviewed-on: https://go-review.googlesource.com/57942
1 parent 775b238 commit 1da9aa9

12 files changed

Lines changed: 755 additions & 31 deletions

File tree

src/crypto/internal/boring/boring.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,20 @@ func bnToBig(bn *C.GO_BIGNUM) *big.Int {
5353
n := C._goboringcrypto_BN_bn2bin(bn, base(raw))
5454
return new(big.Int).SetBytes(raw[:n])
5555
}
56+
57+
func bigToBn(bnp **C.GO_BIGNUM, b *big.Int) bool {
58+
if *bnp != nil {
59+
C._goboringcrypto_BN_free(*bnp)
60+
*bnp = nil
61+
}
62+
if b == nil {
63+
return true
64+
}
65+
raw := b.Bytes()
66+
bn := C._goboringcrypto_BN_bin2bn(base(raw), C.size_t(len(raw)), nil)
67+
if bn == nil {
68+
return false
69+
}
70+
*bnp = bn
71+
return true
72+
}

src/crypto/internal/boring/goboringcrypto.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ size_t _goboringcrypto_ECDSA_size(const GO_EC_KEY*);
177177
int _goboringcrypto_ECDSA_verify(int, const uint8_t*, size_t, const uint8_t*, size_t, const GO_EC_KEY*);
178178

179179
// #include <openssl/rsa.h>
180-
/*unchecked (opaque)*/ typedef struct GO_RSA { char data[1]; } GO_RSA;
180+
181+
// Note: order of struct fields here is unchecked.
182+
typedef struct GO_RSA { void *meth; GO_BIGNUM *n, *e, *d, *p, *q, *dmp1, *dmq1, *iqmp; char data[120]; } GO_RSA;
181183
/*unchecked (opaque)*/ typedef struct GO_BN_GENCB { char data[1]; } GO_BN_GENCB;
182184
GO_RSA* _goboringcrypto_RSA_new(void);
183185
void _goboringcrypto_RSA_free(GO_RSA*);

src/crypto/internal/boring/hmac.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package boring
1010
// #include "goboringcrypto.h"
1111
import "C"
1212
import (
13+
"crypto"
1314
"hash"
1415
"runtime"
1516
"unsafe"
@@ -33,6 +34,28 @@ func hashToMD(h hash.Hash) *C.GO_EVP_MD {
3334
return nil
3435
}
3536

37+
// cryptoHashToMD converts a crypto.Hash
38+
// to a BoringCrypto *C.GO_EVP_MD.
39+
func cryptoHashToMD(ch crypto.Hash) *C.GO_EVP_MD {
40+
switch ch {
41+
case crypto.MD5:
42+
return C._goboringcrypto_EVP_md5()
43+
case crypto.MD5SHA1:
44+
return C._goboringcrypto_EVP_md5_sha1()
45+
case crypto.SHA1:
46+
return C._goboringcrypto_EVP_sha1()
47+
case crypto.SHA224:
48+
return C._goboringcrypto_EVP_sha224()
49+
case crypto.SHA256:
50+
return C._goboringcrypto_EVP_sha256()
51+
case crypto.SHA384:
52+
return C._goboringcrypto_EVP_sha384()
53+
case crypto.SHA512:
54+
return C._goboringcrypto_EVP_sha512()
55+
}
56+
return nil
57+
}
58+
3659
// NewHMAC returns a new HMAC using BoringCrypto.
3760
// The function h must return a hash implemented by
3861
// BoringCrypto (for example, h could be boring.NewSHA256).

src/crypto/internal/boring/notboring.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package boring
88

99
import (
10+
"crypto"
1011
"crypto/cipher"
1112
"hash"
1213
"math/big"
@@ -59,3 +60,44 @@ func SignMarshalECDSA(priv *PrivateKeyECDSA, hash []byte) ([]byte, error) {
5960
func VerifyECDSA(pub *PublicKeyECDSA, hash []byte, r, s *big.Int) bool {
6061
panic("boringcrypto: not available")
6162
}
63+
64+
type PublicKeyRSA struct{ _ int }
65+
type PrivateKeyRSA struct{ _ int }
66+
67+
func DecryptRSAOAEP(h hash.Hash, priv *PrivateKeyRSA, ciphertext, label []byte) ([]byte, error) {
68+
panic("boringcrypto: not available")
69+
}
70+
func DecryptRSAPKCS1(priv *PrivateKeyRSA, ciphertext []byte) ([]byte, error) {
71+
panic("boringcrypto: not available")
72+
}
73+
func DecryptRSANoPadding(priv *PrivateKeyRSA, ciphertext []byte) ([]byte, error) {
74+
panic("boringcrypto: not available")
75+
}
76+
func EncryptRSAOAEP(h hash.Hash, pub *PublicKeyRSA, msg, label []byte) ([]byte, error) {
77+
panic("boringcrypto: not available")
78+
}
79+
func EncryptRSAPKCS1(pub *PublicKeyRSA, msg []byte) ([]byte, error) {
80+
panic("boringcrypto: not available")
81+
}
82+
func EncryptRSANoPadding(pub *PublicKeyRSA, msg []byte) ([]byte, error) {
83+
panic("boringcrypto: not available")
84+
}
85+
func GenerateKeyRSA(bits int) (N, E, D, P, Q, Dp, Dq, Qinv *big.Int, err error) {
86+
panic("boringcrypto: not available")
87+
}
88+
func NewPrivateKeyRSA(N, E, D, P, Q, Dp, Dq, Qinv *big.Int) (*PrivateKeyRSA, error) {
89+
panic("boringcrypto: not available")
90+
}
91+
func NewPublicKeyRSA(N, E *big.Int) (*PublicKeyRSA, error) { panic("boringcrypto: not available") }
92+
func SignRSAPKCS1v15(priv *PrivateKeyRSA, h crypto.Hash, hashed []byte) ([]byte, error) {
93+
panic("boringcrypto: not available")
94+
}
95+
func SignRSAPSS(priv *PrivateKeyRSA, h crypto.Hash, hashed []byte, saltLen int) ([]byte, error) {
96+
panic("boringcrypto: not available")
97+
}
98+
func VerifyRSAPKCS1v15(pub *PublicKeyRSA, h crypto.Hash, hashed, sig []byte) error {
99+
panic("boringcrypto: not available")
100+
}
101+
func VerifyRSAPSS(pub *PublicKeyRSA, h crypto.Hash, hashed, sig []byte, saltLen int) error {
102+
panic("boringcrypto: not available")
103+
}

0 commit comments

Comments
 (0)