| Left: | ||
| Right: |
| 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/rand" | |
| 10 "crypto/sha1" | 11 "crypto/sha1" |
| 11 "os" | |
| 12 "testing" | 12 "testing" |
| 13 ) | 13 ) |
| 14 | 14 |
| 15 func TestKeyGeneration(t *testing.T) { | 15 func TestKeyGeneration(t *testing.T) { |
| 16 » urandom, err := os.Open("/dev/urandom", os.O_RDONLY, 0) | 16 » random := rand.Reader |
| 17 » if err != nil { | 17 » if random == nil { |
| rsc1 2010/07/12 20:15:30 delete peterGo 2010/07/12 21:06:33 Done. | |
| 18 » » t.Errorf("failed to open /dev/urandom") | 18 » » t.Errorf("Failed to find a random reader") |
| 19 } | 19 } |
| 20 | 20 |
| 21 » priv, err := GenerateKey(urandom, 1024) | 21 » priv, err := GenerateKey(random, 1024) |
| 22 if err != nil { | 22 if err != nil { |
| 23 t.Errorf("failed to generate key") | 23 t.Errorf("failed to generate key") |
| 24 } | 24 } |
| 25 pub := &priv.PublicKey | 25 pub := &priv.PublicKey |
| 26 m := big.NewInt(42) | 26 m := big.NewInt(42) |
| 27 c := encrypt(new(big.Int), pub, m) | 27 c := encrypt(new(big.Int), pub, m) |
| 28 m2, err := decrypt(nil, priv, c) | 28 m2, err := decrypt(nil, priv, c) |
| 29 if err != nil { | 29 if err != nil { |
| 30 t.Errorf("error while decrypting: %s", err) | 30 t.Errorf("error while decrypting: %s", err) |
| 31 } | 31 } |
| 32 if m.Cmp(m2) != 0 { | 32 if m.Cmp(m2) != 0 { |
| 33 t.Errorf("got:%v, want:%v (%s)", m2, m, priv) | 33 t.Errorf("got:%v, want:%v (%s)", m2, m, priv) |
| 34 } | 34 } |
| 35 | 35 |
| 36 » m3, err := decrypt(urandom, priv, c) | 36 » m3, err := decrypt(random, priv, c) |
| 37 if err != nil { | 37 if err != nil { |
| 38 t.Errorf("error while decrypting (blind): %s", err) | 38 t.Errorf("error while decrypting (blind): %s", err) |
| 39 } | 39 } |
| 40 if m.Cmp(m3) != 0 { | 40 if m.Cmp(m3) != 0 { |
| 41 t.Errorf("(blind) got:%v, want:%v", m3, m) | 41 t.Errorf("(blind) got:%v, want:%v", m3, m) |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 | 44 |
| 45 type testEncryptOAEPMessage struct { | 45 type testEncryptOAEPMessage struct { |
| 46 in []byte | 46 in []byte |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 69 t.Errorf("#%d,%d error: %s", i, j, err) | 69 t.Errorf("#%d,%d error: %s", i, j, err) |
| 70 } | 70 } |
| 71 if bytes.Compare(out, message.out) != 0 { | 71 if bytes.Compare(out, message.out) != 0 { |
| 72 t.Errorf("#%d,%d bad result: %s (want %s)", i, j , out, message.out) | 72 t.Errorf("#%d,%d bad result: %s (want %s)", i, j , out, message.out) |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 func TestDecryptOAEP(t *testing.T) { | 78 func TestDecryptOAEP(t *testing.T) { |
| 79 » urandom, err := os.Open("/dev/urandom", os.O_RDONLY, 0) | 79 » random := rand.Reader |
| 80 » if err != nil { | 80 » if random == nil { |
| rsc1 2010/07/12 20:15:30 delete peterGo 2010/07/12 21:06:33 Done. | |
| 81 » » t.Errorf("Failed to open /dev/urandom") | 81 » » t.Errorf("Failed to find a random reader") |
| 82 } | 82 } |
| 83 | 83 |
| 84 sha1 := sha1.New() | 84 sha1 := sha1.New() |
| 85 n := new(big.Int) | 85 n := new(big.Int) |
| 86 d := new(big.Int) | 86 d := new(big.Int) |
| 87 for i, test := range testEncryptOAEPData { | 87 for i, test := range testEncryptOAEPData { |
| 88 n.SetString(test.modulus, 16) | 88 n.SetString(test.modulus, 16) |
| 89 d.SetString(test.d, 16) | 89 d.SetString(test.d, 16) |
| 90 private := PrivateKey{PublicKey{n, test.e}, d, nil, nil} | 90 private := PrivateKey{PublicKey{n, test.e}, d, nil, nil} |
| 91 | 91 |
| 92 for j, message := range test.msgs { | 92 for j, message := range test.msgs { |
| 93 out, err := DecryptOAEP(sha1, nil, &private, message.out , nil) | 93 out, err := DecryptOAEP(sha1, nil, &private, message.out , nil) |
| 94 if err != nil { | 94 if err != nil { |
| 95 t.Errorf("#%d,%d error: %s", i, j, err) | 95 t.Errorf("#%d,%d error: %s", i, j, err) |
| 96 } else if bytes.Compare(out, message.in) != 0 { | 96 } else if bytes.Compare(out, message.in) != 0 { |
| 97 t.Errorf("#%d,%d bad result: %#v (want %#v)", i, j, out, message.in) | 97 t.Errorf("#%d,%d bad result: %#v (want %#v)", i, j, out, message.in) |
| 98 } | 98 } |
| 99 | 99 |
| 100 // Decrypt with blinding. | 100 // Decrypt with blinding. |
| 101 » » » out, err = DecryptOAEP(sha1, urandom, &private, message. out, nil) | 101 » » » out, err = DecryptOAEP(sha1, random, &private, message.o ut, nil) |
| 102 if err != nil { | 102 if err != nil { |
| 103 t.Errorf("#%d,%d (blind) error: %s", i, j, err) | 103 t.Errorf("#%d,%d (blind) error: %s", i, j, err) |
| 104 } else if bytes.Compare(out, message.in) != 0 { | 104 } else if bytes.Compare(out, message.in) != 0 { |
| 105 t.Errorf("#%d,%d (blind) bad result: %#v (want % #v)", i, j, out, message.in) | 105 t.Errorf("#%d,%d (blind) bad result: %#v (want % #v)", i, j, out, message.in) |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 } | 109 } |
| 110 | 110 |
| 111 // testEncryptOAEPData contains a subset of the vectors from RSA's "Test vectors for RSA-OAEP". | 111 // testEncryptOAEPData contains a subset of the vectors from RSA's "Test vectors for RSA-OAEP". |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 0x5e, 0x7f, 0x16, 0x64, 0x61, 0x82, 0xfd , 0xb4, 0x64, | 247 0x5e, 0x7f, 0x16, 0x64, 0x61, 0x82, 0xfd , 0xb4, 0x64, |
| 248 0x73, 0x9b, 0x68, 0xab, 0x5d, 0xaf, 0xf0 , 0xe6, 0x3e, | 248 0x73, 0x9b, 0x68, 0xab, 0x5d, 0xaf, 0xf0 , 0xe6, 0x3e, |
| 249 0x95, 0x52, 0x01, 0x68, 0x24, 0xf0, 0x54 , 0xbf, 0x4d, | 249 0x95, 0x52, 0x01, 0x68, 0x24, 0xf0, 0x54 , 0xbf, 0x4d, |
| 250 0x3c, 0x8c, 0x90, 0xa9, 0x7b, 0xb6, 0xb6 , 0x55, 0x32, | 250 0x3c, 0x8c, 0x90, 0xa9, 0x7b, 0xb6, 0xb6 , 0x55, 0x32, |
| 251 0x84, 0xeb, 0x42, 0x9f, 0xcc, | 251 0x84, 0xeb, 0x42, 0x9f, 0xcc, |
| 252 }, | 252 }, |
| 253 }, | 253 }, |
| 254 }, | 254 }, |
| 255 }, | 255 }, |
| 256 } | 256 } |
| OLD | NEW |