-
Notifications
You must be signed in to change notification settings - Fork 2
/
multikey_test.go
45 lines (42 loc) · 1.14 KB
/
multikey_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
package multikey
import (
"crypto/rsa"
"testing"
"github.com/adrianosela/multikey/keys"
"github.com/stretchr/testify/assert"
)
// We test the following statement:
// For a secret encrypted with n required keys {n:n ∈ ℕ}, n keys are
// necessary and sufficient to decrypt the secret
func TestEncryptDecrypt(t *testing.T) {
n := 10
testSecret := []byte("test secret value")
privs, pubs := []*rsa.PrivateKey{}, []*rsa.PublicKey{}
// generate n keys
for k := 0; k < n; k++ {
pri, pub, err := keys.GenerateRSAKeyPair(2048)
if err != nil {
assert.Fail(t, "could not generate test keys")
}
privs = append(privs, pri)
pubs = append(pubs, pub)
}
// encrypt with 1 to n keys
for e := 1; e <= n; e++ {
s, err := Encrypt(testSecret, pubs, e)
if err != nil {
assert.Fail(t, "could not encrypt test secret")
}
// decrypt unsuccessfully with 1 to e keys
for d := 1; d < e; d++ {
plain, _ := Decrypt(s, privs[:d])
assert.NotEqual(t, plain, testSecret)
}
// decrypt successfully with e to n keys
for d := e; d <= n; d++ {
plain, err := Decrypt(s, privs[:d])
assert.Nil(t, err)
assert.Equal(t, plain, testSecret)
}
}
}