OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package rsa | 5 package rsa |
6 | 6 |
7 import ( | 7 import ( |
8 "big"; | 8 "big"; |
9 "bytes"; | 9 "bytes"; |
| 10 "crypto/sha1"; |
10 "encoding/base64"; | 11 "encoding/base64"; |
| 12 "encoding/hex"; |
11 "os"; | 13 "os"; |
12 "io"; | 14 "io"; |
13 "strings"; | 15 "strings"; |
14 "testing"; | 16 "testing"; |
15 "testing/quick"; | 17 "testing/quick"; |
16 ) | 18 ) |
17 | 19 |
18 func decodeBase64(in string) []byte { | 20 func decodeBase64(in string) []byte { |
19 out := make([]byte, base64.StdEncoding.DecodedLen(len(in))); | 21 out := make([]byte, base64.StdEncoding.DecodedLen(len(in))); |
20 n, err := base64.StdEncoding.Decode(out, strings.Bytes(in)); | 22 n, err := base64.StdEncoding.Decode(out, strings.Bytes(in)); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 t.Errorf("returned error: %s", err) | 149 t.Errorf("returned error: %s", err) |
148 } | 150 } |
149 for _, b := range b { | 151 for _, b := range b { |
150 if b == 0 { | 152 if b == 0 { |
151 t.Errorf("Zero octet found"); | 153 t.Errorf("Zero octet found"); |
152 return; | 154 return; |
153 } | 155 } |
154 } | 156 } |
155 } | 157 } |
156 | 158 |
| 159 type signPKCS1v15Test struct { |
| 160 in, out string; |
| 161 } |
| 162 |
| 163 // These vectors have been tested with |
| 164 // `openssl rsautl -verify -inkey pk -in signature | hexdump -C` |
| 165 var signPKCS1v15Tests = []signPKCS1v15Test{ |
| 166 signPKCS1v15Test{"Test.\n", "a4f3fa6ea93bcdd0c57be020c1193ecbfd6f200a3d9
5c409769b029578fa0e336ad9a347600e40d3ae823b8c7e6bad88cc07c1d54c3a1523cbbb6d58efc
362ae"}, |
| 167 } |
| 168 |
| 169 func TestSignPKCS1v15(t *testing.T) { |
| 170 for i, test := range signPKCS1v15Tests { |
| 171 h := sha1.New(); |
| 172 h.Write(strings.Bytes(test.in)); |
| 173 digest := h.Sum(); |
| 174 |
| 175 s, err := SignPKCS1v15(nil, rsaPrivateKey, HashSHA1, digest); |
| 176 if err != nil { |
| 177 t.Errorf("#%d %s", i, err) |
| 178 } |
| 179 |
| 180 expected, _ := hex.DecodeString(test.out); |
| 181 if bytes.Compare(s, expected) != 0 { |
| 182 t.Errorf("#%d got: %x want: %x", i, s, expected) |
| 183 } |
| 184 } |
| 185 } |
| 186 |
| 187 func TestVerifyPKCS1v15(t *testing.T) { |
| 188 for i, test := range signPKCS1v15Tests { |
| 189 h := sha1.New(); |
| 190 h.Write(strings.Bytes(test.in)); |
| 191 digest := h.Sum(); |
| 192 |
| 193 sig, _ := hex.DecodeString(test.out); |
| 194 |
| 195 err := VerifyPKCS1v15(&rsaPrivateKey.PublicKey, HashSHA1, digest
, sig); |
| 196 if err != nil { |
| 197 t.Errorf("#%d %s", i, err) |
| 198 } |
| 199 } |
| 200 } |
| 201 |
157 func bigFromString(s string) *big.Int { | 202 func bigFromString(s string) *big.Int { |
158 ret := new(big.Int); | 203 ret := new(big.Int); |
159 ret.SetString(s, 10); | 204 ret.SetString(s, 10); |
160 return ret; | 205 return ret; |
161 } | 206 } |
162 | 207 |
163 // In order to generate new test vectors you'll need the PEM form of this key: | 208 // In order to generate new test vectors you'll need the PEM form of this key: |
164 // -----BEGIN RSA PRIVATE KEY----- | 209 // -----BEGIN RSA PRIVATE KEY----- |
165 // MIIBOgIBAAJBALKZD0nEffqM1ACuak0bijtqE2QrI/KLADv7l3kK3ppMyCuLKoF0 | 210 // MIIBOgIBAAJBALKZD0nEffqM1ACuak0bijtqE2QrI/KLADv7l3kK3ppMyCuLKoF0 |
166 // fd7Ai2KW5ToIwzFofvJcS/STa6HA5gQenRUCAwEAAQJBAIq9amn00aS0h/CrjXqu | 211 // fd7Ai2KW5ToIwzFofvJcS/STa6HA5gQenRUCAwEAAQJBAIq9amn00aS0h/CrjXqu |
167 // /ThglAXJmZhOMPVn4eiu7/ROixi9sex436MaVeMqSNf7Ex9a8fRNfWss7Sqd9eWu | 212 // /ThglAXJmZhOMPVn4eiu7/ROixi9sex436MaVeMqSNf7Ex9a8fRNfWss7Sqd9eWu |
168 // RTUCIQDasvGASLqmjeffBNLTXV2A5g4t+kLVCpsEIZAycV5GswIhANEPLmax0ME/ | 213 // RTUCIQDasvGASLqmjeffBNLTXV2A5g4t+kLVCpsEIZAycV5GswIhANEPLmax0ME/ |
169 // EO+ZJ79TJKN5yiGBRsv5yvx5UiHxajEXAiAhAol5N4EUyq6I9w1rYdhPMGpLfk7A | 214 // EO+ZJ79TJKN5yiGBRsv5yvx5UiHxajEXAiAhAol5N4EUyq6I9w1rYdhPMGpLfk7A |
170 // IU2snfRJ6Nq2CQIgFrPsWRCkV+gOYcajD17rEqmuLrdIRexpg8N1DOSXoJ8CIGlS | 215 // IU2snfRJ6Nq2CQIgFrPsWRCkV+gOYcajD17rEqmuLrdIRexpg8N1DOSXoJ8CIGlS |
171 // tAboUGBxTDq3ZroNism3DaMIbKPyYrAqhKov1h5V | 216 // tAboUGBxTDq3ZroNism3DaMIbKPyYrAqhKov1h5V |
172 // -----END RSA PRIVATE KEY----- | 217 // -----END RSA PRIVATE KEY----- |
173 | 218 |
174 var rsaPrivateKey = &PrivateKey{ | 219 var rsaPrivateKey = &PrivateKey{ |
175 PublicKey: PublicKey{ | 220 PublicKey: PublicKey{ |
176 N: bigFromString("9353930466774385905609975137998169297361893554
14998671685329502257853572497967725295852446635047121036783518748074826886427746
4700638583474144061408845077"), | 221 N: bigFromString("9353930466774385905609975137998169297361893554
14998671685329502257853572497967725295852446635047121036783518748074826886427746
4700638583474144061408845077"), |
177 E: 65537, | 222 E: 65537, |
178 }, | 223 }, |
179 D: bigFromString("726639843132811634405769937974922253227934392381906363
94970490393898993285385430876577337665541558398345195294398516730148002612857577
59040931985506583861"), | 224 D: bigFromString("726639843132811634405769937974922253227934392381906363
94970490393898993285385430876577337665541558398345195294398516730148002612857577
59040931985506583861"), |
180 P: bigFromString("989203665480846436017288690555926508355729509322669674
61790948584315647051443"), | 225 P: bigFromString("989203665480846436017288690555926508355729509322669674
61790948584315647051443"), |
181 Q: bigFromString("945602083088470157474985238840633946716066719049446663
60068158221458669711639"), | 226 Q: bigFromString("945602083088470157474985238840633946716066719049446663
60068158221458669711639"), |
182 } | 227 } |
OLD | NEW |