1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
20 func decodeBase64(in string) []byte {
21 out := make([]byte, base64.StdEncoding.DecodedLen(len(in)))
22 n, err := base64.StdEncoding.Decode(out, []byte(in))
29 type DecryptPKCS1v15Test struct {
33 // These test vectors were generated with `openssl rsautl -pkcs -encrypt`
34 var decryptPKCS1v15Tests = []DecryptPKCS1v15Test{
36 "gIcUIoVkD6ATMBk/u/nlCZCCWRKdkfjCgFdo35VpRXLduiKXhNz1XupLLzTXAybEq15juc+EgY5o0DHv/nt3yg==",
40 "Y7TOCSqofGhkRb+jaVRLzK8xw2cSo1IVES19utzv6hwvx+M8kFsoWQm5DzBeJCZTCVDPkTpavUuEbgp8hnUGDw==",
44 "arReP9DJtEVyV2Dg3dDp4c/PSk1O6lxkoJ8HcFupoRorBZG+7+1fDAwT1olNddFnQMjmkb8vxwmNMoTAT/BFjQ==",
48 "WtaBXIoGC54+vH0NH0CHHE+dRDOsMc/6BrfFu2lEqcKL9+uDuWaf+Xj9mrbQCjjZcpQuX733zyok/jsnqe/Ftw==",
49 "01234567890123456789012345678901234567890123456789012",
53 func TestDecryptPKCS1v15(t *testing.T) {
54 for i, test := range decryptPKCS1v15Tests {
55 out, err := DecryptPKCS1v15(nil, rsaPrivateKey, decodeBase64(test.in))
57 t.Errorf("#%d error decrypting", i)
59 want := []byte(test.out)
60 if !bytes.Equal(out, want) {
61 t.Errorf("#%d got:%#v want:%#v", i, out, want)
66 func TestEncryptPKCS1v15(t *testing.T) {
68 k := (rsaPrivateKey.N.BitLen() + 7) / 8
70 tryEncryptDecrypt := func(in []byte, blind bool) bool {
75 ciphertext, err := EncryptPKCS1v15(random, &rsaPrivateKey.PublicKey, in)
77 t.Errorf("error encrypting: %s", err)
87 plaintext, err := DecryptPKCS1v15(rand, rsaPrivateKey, ciphertext)
89 t.Errorf("error decrypting: %s", err)
93 if !bytes.Equal(plaintext, in) {
94 t.Errorf("output mismatch: %#v %#v", plaintext, in)
100 config := new(quick.Config)
104 quick.Check(tryEncryptDecrypt, config)
107 // These test vectors were generated with `openssl rsautl -pkcs -encrypt`
108 var decryptPKCS1v15SessionKeyTests = []DecryptPKCS1v15Test{
110 "e6ukkae6Gykq0fKzYwULpZehX+UPXYzMoB5mHQUDEiclRbOTqas4Y0E6nwns1BBpdvEJcilhl5zsox/6DtGsYg==",
114 "Dtis4uk/q/LQGGqGk97P59K03hkCIVFMEFZRgVWOAAhxgYpCRG0MX2adptt92l67IqMki6iVQyyt0TtX3IdtEw==",
118 "LIyFyCYCptPxrvTxpol8F3M7ZivlMsf53zs0vHRAv+rDIh2YsHS69ePMoPMe3TkOMZ3NupiL3takPxIs1sK+dw==",
122 "bafnobel46bKy76JzqU/RIVOH0uAYvzUtauKmIidKgM0sMlvobYVAVQPeUQ/oTGjbIZ1v/6Gyi5AO4DtHruGdw==",
127 func TestEncryptPKCS1v15SessionKey(t *testing.T) {
128 for i, test := range decryptPKCS1v15SessionKeyTests {
129 key := []byte("FAIL")
130 err := DecryptPKCS1v15SessionKey(nil, rsaPrivateKey, decodeBase64(test.in), key)
132 t.Errorf("#%d error decrypting", i)
134 want := []byte(test.out)
135 if !bytes.Equal(key, want) {
136 t.Errorf("#%d got:%#v want:%#v", i, key, want)
141 func TestNonZeroRandomBytes(t *testing.T) {
142 random := rand.Reader
144 b := make([]byte, 512)
145 err := nonZeroRandomBytes(b, random)
147 t.Errorf("returned error: %s", err)
149 for _, b := range b {
151 t.Errorf("Zero octet found")
157 type signPKCS1v15Test struct {
161 // These vectors have been tested with
162 // `openssl rsautl -verify -inkey pk -in signature | hexdump -C`
163 var signPKCS1v15Tests = []signPKCS1v15Test{
164 {"Test.\n", "a4f3fa6ea93bcdd0c57be020c1193ecbfd6f200a3d95c409769b029578fa0e336ad9a347600e40d3ae823b8c7e6bad88cc07c1d54c3a1523cbbb6d58efc362ae"},
167 func TestSignPKCS1v15(t *testing.T) {
168 for i, test := range signPKCS1v15Tests {
170 h.Write([]byte(test.in))
173 s, err := SignPKCS1v15(nil, rsaPrivateKey, crypto.SHA1, digest)
175 t.Errorf("#%d %s", i, err)
178 expected, _ := hex.DecodeString(test.out)
179 if !bytes.Equal(s, expected) {
180 t.Errorf("#%d got: %x want: %x", i, s, expected)
185 func TestVerifyPKCS1v15(t *testing.T) {
186 for i, test := range signPKCS1v15Tests {
188 h.Write([]byte(test.in))
191 sig, _ := hex.DecodeString(test.out)
193 err := VerifyPKCS1v15(&rsaPrivateKey.PublicKey, crypto.SHA1, digest, sig)
195 t.Errorf("#%d %s", i, err)
200 // In order to generate new test vectors you'll need the PEM form of this key:
201 // -----BEGIN RSA PRIVATE KEY-----
202 // MIIBOgIBAAJBALKZD0nEffqM1ACuak0bijtqE2QrI/KLADv7l3kK3ppMyCuLKoF0
203 // fd7Ai2KW5ToIwzFofvJcS/STa6HA5gQenRUCAwEAAQJBAIq9amn00aS0h/CrjXqu
204 // /ThglAXJmZhOMPVn4eiu7/ROixi9sex436MaVeMqSNf7Ex9a8fRNfWss7Sqd9eWu
205 // RTUCIQDasvGASLqmjeffBNLTXV2A5g4t+kLVCpsEIZAycV5GswIhANEPLmax0ME/
206 // EO+ZJ79TJKN5yiGBRsv5yvx5UiHxajEXAiAhAol5N4EUyq6I9w1rYdhPMGpLfk7A
207 // IU2snfRJ6Nq2CQIgFrPsWRCkV+gOYcajD17rEqmuLrdIRexpg8N1DOSXoJ8CIGlS
208 // tAboUGBxTDq3ZroNism3DaMIbKPyYrAqhKov1h5V
209 // -----END RSA PRIVATE KEY-----
211 var rsaPrivateKey = &PrivateKey{
212 PublicKey: PublicKey{
213 N: fromBase10("9353930466774385905609975137998169297361893554149986716853295022578535724979677252958524466350471210367835187480748268864277464700638583474144061408845077"),
216 D: fromBase10("7266398431328116344057699379749222532279343923819063639497049039389899328538543087657733766554155839834519529439851673014800261285757759040931985506583861"),
218 fromBase10("98920366548084643601728869055592650835572950932266967461790948584315647051443"),
219 fromBase10("94560208308847015747498523884063394671606671904944666360068158221458669711639"),