Skip to content

Commit

Permalink
Test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo committed Jun 28, 2019
1 parent 97a1c5d commit 1c0e79c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 60 deletions.
69 changes: 13 additions & 56 deletions key_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package tcrsa
package tcrsa_test

import (
"crypto"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/niclabs/tcrsa"
"math/big"
"testing"
)

const keyTestBitLen = 512
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 := NewKey(keyTestBitLen, keyTestK, keyTestL, &KeyMetaArgs{})
keyShares, _, err := tcrsa.NewKey(keyTestSize, keyTestK, keyTestL, &tcrsa.KeyMetaArgs{})
if err != nil {
t.Errorf("couldn't create keys")
}
Expand All @@ -43,20 +45,20 @@ func TestGenerateKeys_validRandom(t *testing.T) {
k := uint16(keyTestK)
l := uint16(keyTestL)

keyMetaArgs := &KeyMetaArgs{}
keyMetaArgs := &tcrsa.KeyMetaArgs{}

keyShares, keyMeta, err := NewKey(keyTestSize, uint16(k), uint16(l), keyMetaArgs)
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 := PrepareDocumentHash(keyMeta.PublicKey.Size(), keyTestHashType, docHash[:])
docPKCS1, err := tcrsa.PrepareDocumentHash(keyMeta.PublicKey.Size(), keyTestHashType, docHash[:])
if err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}

sigShares := make(SigShareList, l)
sigShares := make(tcrsa.SigShareList, l)

var i uint16
for i = 0; i < l; i++ {
Expand All @@ -83,7 +85,7 @@ func TestGenerateKeys_validFixed(t *testing.T) {

k := uint16(keyTestK)
l := uint16(keyTestL)
keyMetaArgs := &KeyMetaArgs{}
keyMetaArgs := &tcrsa.KeyMetaArgs{}

pBig, err := base64.StdEncoding.DecodeString(keyTestFixedP)
if err != nil {
Expand All @@ -106,18 +108,18 @@ func TestGenerateKeys_validFixed(t *testing.T) {
keyMetaArgs.R = new(big.Int).SetBytes(rBig)
keyMetaArgs.U = new(big.Int).SetBytes(vkuBig)

keyShares, keyMeta, err := NewKey(keyTestSize, uint16(k), uint16(l), 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 := PrepareDocumentHash(keyMeta.PublicKey.Size(), keyTestHashType, docHash[:])
docPKCS1, err := tcrsa.PrepareDocumentHash(keyMeta.PublicKey.Size(), keyTestHashType, docHash[:])
if err != nil {
t.Errorf(fmt.Sprintf("%v", err))
}

sigShares := make(SigShareList, l)
sigShares := make(tcrsa.SigShareList, l)

var i uint16
for i = 0; i < l; i++ {
Expand Down Expand Up @@ -146,48 +148,3 @@ func TestGenerateKeys_validFixed(t *testing.T) {
}

}

func Example() {

// First we need to get the values of K and L from somewhere.
k := uint16(3)
l := uint16(5)

// Generate keys provides to us with a list of keyShares and the key metainformation.
keyShares, keyMeta, err := NewKey(keyTestSize, uint16(k), uint16(l), nil)
if err != nil {
panic(fmt.Sprintf("%v", err))
}

// Then we need to prepare the document we want to sign, so we hash it and pad it using PKCS v1.15.
docHash := sha256.Sum256([]byte(keyTestMessage))
docPKCS1, err := PrepareDocumentHash(keyMeta.PublicKey.Size(), crypto.SHA256, docHash[:])
if err != nil {
panic(fmt.Sprintf("%v", err))
}

sigShares := make(SigShareList, l)
var i uint16

// Now we sign with at least k nodes and check immediately the signature share for consistency.
for i = 0; i < k; i++ {
sigShares[i], err = keyShares[i].Sign(docPKCS1, crypto.SHA256, keyMeta)
if err != nil {
panic(fmt.Sprintf("%v", err))
}
if err := sigShares[i].Verify(docPKCS1, keyMeta); err != nil {
panic(fmt.Sprintf("%v", err))
}
}

// Having all the signature shares we needed, we join them to create a real signature.
signature, err := sigShares.Join(docPKCS1, keyMeta)
if err != nil {
panic(fmt.Sprintf("%v", err))
}

// Finally we check the signature with Golang's crypto/rsa PKCSv1.15 verification routine.
if err := rsa.VerifyPKCS1v15(keyMeta.PublicKey, crypto.SHA256, docHash[:], signature); err != nil {
panic(fmt.Sprintf("%v", err))
}
}
1 change: 0 additions & 1 deletion pkcs1_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

// This section is copied almost literally from the golang crypto/rsa source code
// https://golang.org/src/crypto/rsa/pkcs1v15.go

// These are ASN1 DER structures:
// DigestInfo ::= SEQUENCE {
// digestAlgorithm AlgorithmIdentifier,
Expand Down
5 changes: 3 additions & 2 deletions pkcs1_encoding_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package tcrsa
package tcrsa_test

import (
"crypto"
"crypto/sha256"
"github.com/niclabs/tcrsa"
"testing"
)

Expand All @@ -11,7 +12,7 @@ const pkcs1EncodingLeyLength = 64

func TestPrepareDocumentHash(t *testing.T) {
docHash := sha256.Sum256([]byte(pkcs1EncodingTestMessage))
docPKCS1, err := PrepareDocumentHash(pkcs1EncodingLeyLength, crypto.SHA256, docHash[:])
docPKCS1, err := tcrsa.PrepareDocumentHash(pkcs1EncodingLeyLength, crypto.SHA256, docHash[:])
if err != nil {
t.Errorf("couldn't prepare document hash: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion polynomial.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (p polynomial) eval(x *big.Int) *big.Int {
}

// string returns the polynomial formatted as a string.
func (p polynomial) string() string {
func (p polynomial) String() string {
s := make([]string, len(p))
for i := 0; i < len(p); i++ {
s[i] = fmt.Sprintf("%dx^%d", p[i], i)
Expand Down
62 changes: 62 additions & 0 deletions tcrsa_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package tcrsa_test

import (
"crypto"
"crypto/rsa"
"crypto/sha256"
"fmt"
"github.com/niclabs/tcrsa"
)

const exampleK = 3
const exampleL = 5

const exampleHashType = crypto.SHA256
const exampleSize = 2048
const exampleMessage = "Hello world"

func Example() {
// First we need to get the values of K and L from somewhere.
k := uint16(exampleK)
l := uint16(exampleL)

// Generate keys provides to us with a list of keyShares and the key metainformation.
keyShares, keyMeta, err := tcrsa.NewKey(exampleSize, uint16(k), uint16(l), nil)
if err != nil {
panic(fmt.Sprintf("%v", err))
}

// Then we need to prepare the document we want to sign, so we hash it and pad it using PKCS v1.15.
docHash := sha256.Sum256([]byte(exampleMessage))
docPKCS1, err := tcrsa.PrepareDocumentHash(keyMeta.PublicKey.Size(), crypto.SHA256, docHash[:])
if err != nil {
panic(fmt.Sprintf("%v", err))
}

sigShares := make(tcrsa.SigShareList, l)
var i uint16

// Now we sign with at least k nodes and check immediately the signature share for consistency.
for i = 0; i < l; i++ {
sigShares[i], err = keyShares[i].Sign(docPKCS1, exampleHashType, keyMeta)
if err != nil {
panic(fmt.Sprintf("%v", err))
}
if err := sigShares[i].Verify(docPKCS1, keyMeta); err != nil {
panic(fmt.Sprintf("%v", err))
}
}

// Having all the signature shares we needed, we join them to create a real signature.
signature, err := sigShares.Join(docPKCS1, keyMeta)
if err != nil {
panic(fmt.Sprintf("%v", err))
}

// Finally we check the signature with Golang's crypto/rsa PKCSv1.15 verification routine.
if err := rsa.VerifyPKCS1v15(keyMeta.PublicKey, crypto.SHA256, docHash[:], signature); err != nil {
panic(fmt.Sprintf("%v", err))
}
fmt.Println("ok")
// Output: ok
}

0 comments on commit 1c0e79c

Please sign in to comment.