OLD | NEW |
(Empty) | |
| 1 // Copyright 2011 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. |
| 4 |
| 5 package packet |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "crypto/rand" |
| 10 "crypto/rsa" |
| 11 "fmt" |
| 12 "math/big" |
| 13 "testing" |
| 14 ) |
| 15 |
| 16 func bigFromBase10(s string) *big.Int { |
| 17 b, ok := new(big.Int).SetString(s, 10) |
| 18 if !ok { |
| 19 panic("bigFromBase10 failed") |
| 20 } |
| 21 return b |
| 22 } |
| 23 |
| 24 var encryptedKeyPub = rsa.PublicKey{ |
| 25 E: 65537, |
| 26 N: bigFromBase10("115804063926007623305902631768113868327816898845124614
64884993471856854107435818375925013620476205387985810235215985435272709703332266
30293876109598841803066686285266861210212357570163680385852124106107420292864396
07686208110250133174279811431933746643015923132833417396844716207301518956640020
862630546868823"), |
| 27 } |
| 28 |
| 29 var encryptedKeyRSAPriv = &rsa.PrivateKey{ |
| 30 PublicKey: encryptedKeyPub, |
| 31 D: bigFromBase10("3235558866821986954475156156531322829776546431
40985522504095572673712338924969513834266024390099938751252225791598500549733108
59166139474359774543943714622292329487391199285040721944491839695981199720170366
76354775491549364068584996178009224114018119877929971257877446083713936080388313
9311171713302987058393"), |
| 32 } |
| 33 |
| 34 var encryptedKeyPriv = &PrivateKey{ |
| 35 PublicKey: PublicKey{ |
| 36 PubKeyAlgo: PubKeyAlgoRSA, |
| 37 }, |
| 38 PrivateKey: encryptedKeyRSAPriv, |
| 39 } |
| 40 |
| 41 func TestDecryptingEncryptedKey(t *testing.T) { |
| 42 const encryptedKeyHex = "c18c032a67d68660df41c70104005789d0de26b6a50c985
a02a13131ca829c413a35d0e6fa8d6842599252162808ac7439c72151c8c6183e76923fe32993014
14d0c25a2f06a2257db3839e7df0ec964773f6e4c4ac7ff3b48c444237166dd46ba8ff443a5410dc
670cb486672fdbe7c9dfafb75b4fea83af3a204fe2a7dfa86bd20122b4f3d2646cbeecb8f7be8" |
| 43 const expectedKeyHex = "d930363f7e0308c333b9618617ea728963d8df993665ae7b
e1092d4926fd864b" |
| 44 |
| 45 p, err := Read(readerFromHex(encryptedKeyHex)) |
| 46 if err != nil { |
| 47 t.Errorf("error from Read: %s", err) |
| 48 return |
| 49 } |
| 50 ek, ok := p.(*EncryptedKey) |
| 51 if !ok { |
| 52 t.Errorf("didn't parse an EncryptedKey, got %#v", p) |
| 53 return |
| 54 } |
| 55 |
| 56 if ek.KeyId != 0x2a67d68660df41c7 || ek.Algo != PubKeyAlgoRSA { |
| 57 t.Errorf("unexpected EncryptedKey contents: %#v", ek) |
| 58 return |
| 59 } |
| 60 |
| 61 err = ek.Decrypt(encryptedKeyPriv) |
| 62 if err != nil { |
| 63 t.Errorf("error from Decrypt: %s", err) |
| 64 return |
| 65 } |
| 66 |
| 67 if ek.CipherFunc != CipherAES256 { |
| 68 t.Errorf("unexpected EncryptedKey contents: %#v", ek) |
| 69 return |
| 70 } |
| 71 |
| 72 keyHex := fmt.Sprintf("%x", ek.Key) |
| 73 if keyHex != expectedKeyHex { |
| 74 t.Errorf("bad key, got %s want %x", keyHex, expectedKeyHex) |
| 75 } |
| 76 } |
| 77 |
| 78 func TestEncryptingEncryptedKey(t *testing.T) { |
| 79 key := []byte{1, 2, 3, 4} |
| 80 const expectedKeyHex = "01020304" |
| 81 const keyId = 42 |
| 82 |
| 83 pub := &PublicKey{ |
| 84 PublicKey: &encryptedKeyPub, |
| 85 KeyId: keyId, |
| 86 PubKeyAlgo: PubKeyAlgoRSAEncryptOnly, |
| 87 } |
| 88 |
| 89 buf := new(bytes.Buffer) |
| 90 err := SerializeEncryptedKey(buf, rand.Reader, pub, CipherAES128, key) |
| 91 if err != nil { |
| 92 t.Errorf("error writing encrypted key packet: %s", err) |
| 93 } |
| 94 |
| 95 p, err := Read(buf) |
| 96 if err != nil { |
| 97 t.Errorf("error from Read: %s", err) |
| 98 return |
| 99 } |
| 100 ek, ok := p.(*EncryptedKey) |
| 101 if !ok { |
| 102 t.Errorf("didn't parse an EncryptedKey, got %#v", p) |
| 103 return |
| 104 } |
| 105 |
| 106 if ek.KeyId != keyId || ek.Algo != PubKeyAlgoRSAEncryptOnly { |
| 107 t.Errorf("unexpected EncryptedKey contents: %#v", ek) |
| 108 return |
| 109 } |
| 110 |
| 111 err = ek.Decrypt(encryptedKeyPriv) |
| 112 if err != nil { |
| 113 t.Errorf("error from Decrypt: %s", err) |
| 114 return |
| 115 } |
| 116 |
| 117 if ek.CipherFunc != CipherAES128 { |
| 118 t.Errorf("unexpected EncryptedKey contents: %#v", ek) |
| 119 return |
| 120 } |
| 121 |
| 122 keyHex := fmt.Sprintf("%x", ek.Key) |
| 123 if keyHex != expectedKeyHex { |
| 124 t.Errorf("bad key, got %s want %x", keyHex, expectedKeyHex) |
| 125 } |
| 126 } |
OLD | NEW |