-
Notifications
You must be signed in to change notification settings - Fork 6
/
key_test.go
150 lines (124 loc) · 4.08 KB
/
key_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package tcrsa_test
import (
"crypto"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/niclabs/tcrsa"
"math/big"
"testing"
)
const keyTestK = 3
const keyTestL = 5
const keyTestFixedSize = 512
const keyTestFixedP = "132TWiSEqNNnfiF5AZjS2R8SwUszMGnHSKTYAtWckuc="
const keyTestFixedQ = "f8PooDmAlOUFf3BdAxPCOy8p5ArfLHs6ODFWTFnpUxM="
const keyTestFixedR = "UfF0MWqXf+K4GjmcWhxdK3CH/XVsDxm8r+CqBenL7TfdWNAD4rpUMIHzhqb0WV6KAAJfGEBlHyj1JH2rr9LiUA=="
const keyTestFixedU = "CpJe+VzsAI3FcPioeMXklkxFFb+M9MaN1VzuScOs+7bwvczarYABZhyjPFC8McXCFAJIvaKTZwTlpylwJPumZw=="
const keyTestHashType = crypto.SHA256
const keyTestSize = 512
const keyTestMessage = "Hello world"
func TestGenerateKeys_differentKeys(t *testing.T) {
keyShares, _, err := tcrsa.NewKey(keyTestSize, keyTestK, keyTestL, &tcrsa.KeyMetaArgs{})
if err != nil {
t.Errorf("couldn't create keys")
}
for i := 0; i < len(keyShares); i++ {
key1 := keyShares[i]
for j := i + 1; j < len(keyShares); j++ {
key2 := keyShares[j]
if key1.EqualsSi(key2) {
t.Errorf("key shares are equal: k%d=%s, k%d=%s", i, key1.ToBase64(), j, key2.ToBase64())
}
}
}
}
func TestGenerateKeys_validRandom(t *testing.T) {
k := uint16(keyTestK)
l := uint16(keyTestL)
keyMetaArgs := &tcrsa.KeyMetaArgs{}
keyShares, keyMeta, err := tcrsa.NewKey(keyTestSize, uint16(k), uint16(l), keyMetaArgs)
if err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}
docHash := sha256.Sum256([]byte(keyTestMessage))
docPKCS1, err := tcrsa.PrepareDocumentHash(keyMeta.PublicKey.Size(), keyTestHashType, docHash[:])
if err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}
sigShares := make(tcrsa.SigShareList, l)
var i uint16
for i = 0; i < l; i++ {
sigShares[i], err = keyShares[i].Sign(docPKCS1, keyTestHashType, keyMeta)
if err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}
if err := sigShares[i].Verify(docPKCS1, keyMeta); err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}
}
signature, err := sigShares.Join(docPKCS1, keyMeta)
if err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}
if err := rsa.VerifyPKCS1v15(keyMeta.PublicKey, keyTestHashType, docHash[:], signature); err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}
}
func TestGenerateKeys_validFixed(t *testing.T) {
k := uint16(keyTestK)
l := uint16(keyTestL)
keyMetaArgs := &tcrsa.KeyMetaArgs{}
pBig, err := base64.StdEncoding.DecodeString(keyTestFixedP)
if err != nil {
t.Errorf("could not decode b64 key for p")
}
qBig, err := base64.StdEncoding.DecodeString(keyTestFixedQ)
if err != nil {
t.Errorf("could not decode b64 key for q")
}
rBig, err := base64.StdEncoding.DecodeString(keyTestFixedR)
if err != nil {
t.Errorf("could not decode b64 key for R")
}
vkuBig, err := base64.StdEncoding.DecodeString(keyTestFixedU)
if err != nil {
t.Errorf("could not decode b64 key for vk_u")
}
keyMetaArgs.P = new(big.Int).SetBytes(pBig)
keyMetaArgs.Q = new(big.Int).SetBytes(qBig)
keyMetaArgs.R = new(big.Int).SetBytes(rBig)
keyMetaArgs.U = new(big.Int).SetBytes(vkuBig)
keyShares, keyMeta, err := tcrsa.NewKey(keyTestFixedSize, uint16(k), uint16(l), keyMetaArgs)
if err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}
docHash := sha256.Sum256([]byte(keyTestMessage))
docPKCS1, err := tcrsa.PrepareDocumentHash(keyMeta.PublicKey.Size(), keyTestHashType, docHash[:])
if err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}
sigShares := make(tcrsa.SigShareList, l)
var i uint16
for i = 0; i < l; i++ {
sigShares[i], err = keyShares[i].Sign(docPKCS1, keyTestHashType, keyMeta)
if err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}
if err := sigShares[i].Verify(docPKCS1, keyMeta); err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}
}
signature, err := sigShares.Join(docPKCS1, keyMeta)
if err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}
sigB64 := base64.StdEncoding.EncodeToString(signature)
if sigB64 != "BUNv4j1NkVFNwx6v0GVG6CfN1Y7yhOBG2Tyy7ci7VK+AVYukZdiajnaYPALHLsEwngDLgNPK40o6HhbWT+ikXQ==" {
t.Errorf("signature is not as expected.")
}
if err := rsa.VerifyPKCS1v15(keyMeta.PublicKey, keyTestHashType, docHash[:], signature); err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}
}