OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / crypto / openpgp / packet / encrypted_key_test.go
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/rsa"
10         "encoding/hex"
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("115804063926007623305902631768113868327816898845124614648849934718568541074358183759250136204762053879858102352159854352727097033322663029387610959884180306668628526686121021235757016368038585212410610742029286439607686208110250133174279811431933746643015923132833417396844716207301518956640020862630546868823"),
27 }
28
29 var encryptedKeyRSAPriv = &rsa.PrivateKey{
30         PublicKey: encryptedKeyPub,
31         D:         bigFromBase10("32355588668219869544751561565313228297765464314098552250409557267371233892496951383426602439009993875125222579159850054973310859166139474359774543943714622292329487391199285040721944491839695981199720170366763547754915493640685849961780092241140181198779299712578774460837139360803883139311171713302987058393"),
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 = "c18c032a67d68660df41c70104005789d0de26b6a50c985a02a13131ca829c413a35d0e6fa8d6842599252162808ac7439c72151c8c6183e76923fe3299301414d0c25a2f06a2257db3839e7df0ec964773f6e4c4ac7ff3b48c444237166dd46ba8ff443a5410dc670cb486672fdbe7c9dfafb75b4fea83af3a204fe2a7dfa86bd20122b4f3d2646cbeecb8f7be8"
43         const expectedKeyHex = "d930363f7e0308c333b9618617ea728963d8df993665ae7be1092d4926fd864b"
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, nil)
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, pub, CipherAES128, key, nil)
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, nil)
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 }
127
128 func TestSerializingEncryptedKey(t *testing.T) {
129         const encryptedKeyHex = "c18c032a67d68660df41c70104005789d0de26b6a50c985a02a13131ca829c413a35d0e6fa8d6842599252162808ac7439c72151c8c6183e76923fe3299301414d0c25a2f06a2257db3839e7df0ec964773f6e4c4ac7ff3b48c444237166dd46ba8ff443a5410dc670cb486672fdbe7c9dfafb75b4fea83af3a204fe2a7dfa86bd20122b4f3d2646cbeecb8f7be8"
130
131         p, err := Read(readerFromHex(encryptedKeyHex))
132         if err != nil {
133                 t.Fatalf("error from Read: %s", err)
134         }
135         ek, ok := p.(*EncryptedKey)
136         if !ok {
137                 t.Fatalf("didn't parse an EncryptedKey, got %#v", p)
138         }
139
140         var buf bytes.Buffer
141         ek.Serialize(&buf)
142
143         if bufHex := hex.EncodeToString(buf.Bytes()); bufHex != encryptedKeyHex {
144                 t.Fatalf("serialization of encrypted key differed from original. Original was %s, but reserialized as %s", encryptedKeyHex, bufHex)
145         }
146 }