storj/pkg/pkcrypto/signing_test.go
paul cannon c35b93766d
Unite all cryptographic signing and verifying (#1244)
this change removes the cryptopasta dependency.

a couple possible sources of problem with this change:

 * the encoding used for ECDSA signatures on SignedMessage has changed.
   the encoding employed by cryptopasta was workable, but not the same
   as the encoding used for such signatures in the rest of the world
   (most particularly, on ECDSA signatures in X.509 certificates). I
   think we'll be best served by using one ECDSA signature encoding from
   here on, but if we need to use the old encoding for backwards
   compatibility with existing nodes, that can be arranged.

 * since there's already a breaking change in SignedMessage, I changed
   it to send and receive public keys in raw PKIX format, instead of
   PEM. PEM just adds unhelpful overhead for this case.
2019-02-07 14:39:20 -06:00

68 lines
1.7 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package pkcrypto
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSigningAndVerifyingECDSA(t *testing.T) {
tests := []struct {
name string
data string
}{
{"empty", ""},
{"single byte", "C"},
{"longnulls", string(make([]byte, 2000))},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
privKey, err := GeneratePrivateECDSAKey(authECCurve)
assert.NoError(t, err)
pubKey := PublicKeyFromPrivate(privKey)
// test signing and verifying a hash of the data
sig, err := HashAndSign(privKey, []byte(tt.data))
assert.NoError(t, err)
err = HashAndVerifySignature(pubKey, []byte(tt.data), sig)
assert.NoError(t, err)
// test signing and verifying the data directly
sig, err = SignWithoutHashing(privKey, []byte(tt.data))
assert.NoError(t, err)
err = VerifySignatureWithoutHashing(pubKey, []byte(tt.data), sig)
assert.NoError(t, err)
})
}
}
func TestSigningAndVerifyingRSA(t *testing.T) {
privKey, err := GeneratePrivateRSAKey(StorjRSAKeyBits)
assert.NoError(t, err)
pubKey := PublicKeyFromPrivate(privKey)
tests := []struct {
name string
data string
}{
{"empty", ""},
{"single byte", "C"},
{"longnulls", string(make([]byte, 2000))},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// test signing and verifying a hash of the data
sig, err := HashAndSign(privKey, []byte(tt.data))
assert.NoError(t, err)
err = HashAndVerifySignature(pubKey, []byte(tt.data), sig)
assert.NoError(t, err)
// don't test signing and verifying the data directly, as RSA can't
// handle messages of arbitrary size
})
}
}