going somewhere
This commit is contained in:
1 parent
6b3c99664c
commit
209af0d820
16 files changed
+549
-131
No files matched your search
@@ -1,7 +1,11 @@
|
||||
package asymetric
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
|
||||
"github.com/didiercrunch/filou/contract"
|
||||
"github.com/didiercrunch/filou/helper"
|
||||
)
|
||||
|
||||
type PublicKeyEncryptor interface {
|
||||
@@ -12,10 +16,37 @@ type Signer interface {
|
||||
Sign(data []byte) ([]byte, error)
|
||||
}
|
||||
|
||||
func GetSigner(contract_ contract.AcceptedContract) (Signer, error) {
|
||||
return nil, nil
|
||||
func GetSigner(contract_ *contract.AcceptedContract) (Signer, error) {
|
||||
if contract_.SignatureScheme == "rsa_pss" {
|
||||
return getRsaPssSignerFromContract(contract_)
|
||||
}
|
||||
return nil, errors.New("signature scheme " + contract_.SignatureScheme + " not supported")
|
||||
}
|
||||
|
||||
func GetPublicKeyEncryptor(contract_ contract.AcceptedContract) (Signer, error) {
|
||||
return nil, nil
|
||||
func GetPublicKeyEncryptor(contract_ *contract.AcceptedContract) (PublicKeyEncryptor, error) {
|
||||
if contract_.AsynchronousEncryptionScheme == "rsa_oaep" {
|
||||
return getRsaOaepEncryptorFromContract(contract_)
|
||||
} else {
|
||||
return nil, errors.New("asynchronous encryption scheme " + contract_.AsynchronousEncryptionScheme + " not supported")
|
||||
}
|
||||
}
|
||||
|
||||
func getRsaPssSignerFromContract(con *contract.AcceptedContract) (*RsaPssSigner, error) {
|
||||
if privKey, err := con.RsaPrivateKey(); err != nil {
|
||||
return nil, err
|
||||
} else if hash, err := helper.GetHashFunctionByLowerCaseName(con.Hash); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return &RsaPssSigner{privKey, con.RsaPublicKey, hash, rand.Reader}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getRsaOaepEncryptorFromContract(con *contract.AcceptedContract) (*RsaOaepEncrypter, error) {
|
||||
if privKey, err := con.RsaPrivateKey(); err != nil {
|
||||
return nil, err
|
||||
} else if hash, err := helper.GetHashFunctionByLowerCaseName(con.Hash); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return &RsaOaepEncrypter{privKey, con.RsaPublicKey, hash, rand.Reader}, nil
|
||||
}
|
||||
}
|
||||
@@ -1 +1,63 @@
|
||||
package asymetric
|
||||
package asymetric
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"testing"
|
||||
|
||||
"github.com/didiercrunch/filou/contract"
|
||||
)
|
||||
|
||||
func TestGetSignerEmptyContract(t *testing.T) {
|
||||
c := new(contract.AcceptedContract)
|
||||
if _, err := GetSigner(c); err == nil {
|
||||
t.Error("exceptec an error here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSignerBadName(t *testing.T) {
|
||||
c := new(contract.AcceptedContract)
|
||||
c.SignatureScheme = "bigtits.com"
|
||||
if _, err := GetSigner(c); err == nil {
|
||||
t.Error("exceptec an error here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSigner(t *testing.T) {
|
||||
c := new(contract.AcceptedContract)
|
||||
priv, pub := generateKeyPair()
|
||||
|
||||
c.Hash = "sha256"
|
||||
c.SignatureScheme = "rsa_pss"
|
||||
c.RsaPublicKey = pub
|
||||
|
||||
c.RsaPrivateKey = func() (*rsa.PrivateKey, error) {
|
||||
return priv, nil
|
||||
}
|
||||
if _, err := GetSigner(c); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPublicKeyEncryptor(t *testing.T) {
|
||||
c := new(contract.AcceptedContract)
|
||||
priv, pub := generateKeyPair()
|
||||
|
||||
c.Hash = "sha256"
|
||||
c.AsynchronousEncryptionScheme = "rsa_oaep"
|
||||
c.RsaPublicKey = pub
|
||||
|
||||
c.RsaPrivateKey = func() (*rsa.PrivateKey, error) {
|
||||
return priv, nil
|
||||
}
|
||||
if _, err := GetPublicKeyEncryptor(c); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPublicKeyEncryptorBadName(t *testing.T) {
|
||||
c := new(contract.AcceptedContract)
|
||||
c.AsynchronousEncryptionScheme = "bigtits.com"
|
||||
if _, err := GetPublicKeyEncryptor(c); err == nil {
|
||||
t.Error("exceptec an error here")
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
type RsaOaepPss struct {
|
||||
type RsaOaepEncrypter struct {
|
||||
PrivateKey *rsa.PrivateKey
|
||||
PublicKey *rsa.PublicKey
|
||||
Hash crypto.Hash
|
||||
@@ -19,7 +19,7 @@ type RsaOaepPss struct {
|
||||
Random io.Reader
|
||||
}
|
||||
|
||||
func (this *RsaOaepPss) getHash() (crypto.Hash, error) {
|
||||
func (this *RsaOaepEncrypter) getHash() (crypto.Hash, error) {
|
||||
if !this.Hash.Available() {
|
||||
return 0, errors.New(fmt.Sprintf("hash function %v is unabailable", this.Hash))
|
||||
} else {
|
||||
@@ -27,7 +27,7 @@ func (this *RsaOaepPss) getHash() (crypto.Hash, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (this *RsaOaepPss) Encrypt(data []byte) ([]byte, error) {
|
||||
func (this *RsaOaepEncrypter) Encrypt(data []byte) ([]byte, error) {
|
||||
label := make([]byte, 0)
|
||||
if hash, err := this.getHash(); err != nil {
|
||||
return nil, err
|
||||
@@ -37,7 +37,7 @@ func (this *RsaOaepPss) Encrypt(data []byte) ([]byte, error) {
|
||||
|
||||
}
|
||||
|
||||
func (this *RsaOaepPss) Decrypt(ciphertext []byte) ([]byte, error) {
|
||||
func (this *RsaOaepEncrypter) Decrypt(ciphertext []byte) ([]byte, error) {
|
||||
label := make([]byte, 0)
|
||||
if hash, err := this.getHash(); err != nil {
|
||||
return nil, err
|
||||
@@ -45,12 +45,3 @@ func (this *RsaOaepPss) Decrypt(ciphertext []byte) ([]byte, error) {
|
||||
return rsa.DecryptOAEP(hash.New(), this.Random, this.PrivateKey, ciphertext, label)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *RsaOaepPss) Sign(hashedData []byte) ([]byte, error) {
|
||||
ret, err := rsa.SignPSS(this.Random, this.PrivateKey, this.Hash, hashedData, nil)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (this *RsaOaepPss) VerifySignature(hashedData []byte, signature []byte) error {
|
||||
return rsa.VerifyPSS(this.PublicKey, this.Hash, hashedData, signature, nil)
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package asymetric
|
||||
|
||||
import (
|
||||
_ "crypto/sha512"
|
||||
|
||||
"crypto"
|
||||
"crypto/rsa"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRsaOaepEncrypt(t *testing.T) {
|
||||
bobPrivKey, bobPubKey := generateKeyPair()
|
||||
|
||||
r := &RsaOaepEncrypter{nil, bobPubKey, crypto.SHA384, random}
|
||||
|
||||
ciphertext, err := r.Encrypt([]byte("this is a secret message"))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
orgTest, err := rsa.DecryptOAEP(crypto.SHA384.New(), random, bobPrivKey, ciphertext, []byte{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if string(orgTest) != "this is a secret message" {
|
||||
t.Error("the decryption of the message does not fit the original message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRsaOaepEncryptDecrypt(t *testing.T) {
|
||||
bobPrivKey, bobPubKey := generateKeyPair()
|
||||
|
||||
message := "this is a secret message"
|
||||
|
||||
rAlice := &RsaOaepEncrypter{nil, bobPubKey, crypto.SHA384, random}
|
||||
rBob := &RsaOaepEncrypter{bobPrivKey, nil, crypto.SHA384, random}
|
||||
|
||||
if ciphertext, err := rAlice.Encrypt([]byte(message)); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
} else if decryptedText, err := rBob.Decrypt(ciphertext); err != nil {
|
||||
t.Error(err)
|
||||
} else if string(decryptedText) != message {
|
||||
t.Error("the decryption of the message does not fit the original message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRsaOaepAvailableHashes(t *testing.T) {
|
||||
hashes := map[crypto.Hash]bool{crypto.SHA224: true, crypto.SHA256: true, crypto.SHA384: true, crypto.SHA512: true}
|
||||
var i crypto.Hash
|
||||
for i = 0; i < 22; i++ {
|
||||
r := &RsaOaepEncrypter{Hash: i}
|
||||
if _, err := r.getHash(); err != nil && hashes[i] {
|
||||
t.Error("should support", i, "hash type but does not")
|
||||
} else if err == nil && !hashes[i] {
|
||||
t.Error("should not support", i, "hash type but does")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package asymetric
|
||||
|
||||
import (
|
||||
_ "crypto/sha256"
|
||||
_ "crypto/sha512"
|
||||
|
||||
"crypto"
|
||||
"crypto/rsa"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type RsaPssSigner struct {
|
||||
PrivateKey *rsa.PrivateKey
|
||||
PublicKey *rsa.PublicKey
|
||||
Hash crypto.Hash
|
||||
|
||||
Random io.Reader
|
||||
}
|
||||
|
||||
func (this *RsaPssSigner) getHash() (crypto.Hash, error) {
|
||||
if !this.Hash.Available() {
|
||||
return 0, errors.New(fmt.Sprintf("hash function %v is unabailable", this.Hash))
|
||||
} else {
|
||||
return this.Hash, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (this *RsaPssSigner) Sign(hashedData []byte) ([]byte, error) {
|
||||
ret, err := rsa.SignPSS(this.Random, this.PrivateKey, this.Hash, hashedData, nil)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (this *RsaPssSigner) VerifySignature(hashedData []byte, signature []byte) error {
|
||||
return rsa.VerifyPSS(this.PublicKey, this.Hash, hashedData, signature, nil)
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package asymetric
|
||||
|
||||
import (
|
||||
_ "crypto/sha512"
|
||||
|
||||
"crypto"
|
||||
"crypto/rsa"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//var random = helper.NewMockRandomReader()
|
||||
|
||||
//func generateKeyPair() (*rsa.PrivateKey, *rsa.PublicKey) {
|
||||
// if key, err := rsa.GenerateKey(random, 1024); err != nil {
|
||||
// panic(err)
|
||||
// } else {
|
||||
// return key, &key.PublicKey
|
||||
// }
|
||||
//}
|
||||
|
||||
func TestRsaPssSign(t *testing.T) {
|
||||
alicePrivKey, alicePubKey := generateKeyPair()
|
||||
h := crypto.SHA384.New()
|
||||
h.Write([]byte("this is a secret message"))
|
||||
hashedMessage := h.Sum(nil)
|
||||
|
||||
rAlice := &RsaPssSigner{alicePrivKey, nil, crypto.SHA384, random}
|
||||
signature, err := rAlice.Sign(hashedMessage)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if err := rsa.VerifyPSS(alicePubKey, crypto.SHA384, hashedMessage, signature, nil); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRsaPssSignAndVerify(t *testing.T) {
|
||||
alicePrivKey, alicePubKey := generateKeyPair()
|
||||
h := crypto.SHA384.New()
|
||||
h.Write([]byte("this is a secret message"))
|
||||
hashedMessage := h.Sum(nil)
|
||||
|
||||
rAlice := &RsaPssSigner{alicePrivKey, nil, crypto.SHA384, random}
|
||||
rBob := &RsaPssSigner{nil, alicePubKey, crypto.SHA384, random}
|
||||
if signature, err := rAlice.Sign(hashedMessage); err != nil {
|
||||
t.Error(err)
|
||||
} else if err := rBob.VerifySignature(hashedMessage, signature); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRsaPssAvailableHashes(t *testing.T) {
|
||||
hashes := map[crypto.Hash]bool{crypto.SHA224: true, crypto.SHA256: true, crypto.SHA384: true, crypto.SHA512: true}
|
||||
var i crypto.Hash
|
||||
for i = 0; i < 22; i++ {
|
||||
r := &RsaPssSigner{Hash: i}
|
||||
if _, err := r.getHash(); err != nil && hashes[i] {
|
||||
t.Error("should support", i, "hash type but does not")
|
||||
} else if err == nil && !hashes[i] {
|
||||
t.Error("should not support", i, "hash type but does")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package asymetric
|
||||
|
||||
import (
|
||||
_ "crypto/sha512"
|
||||
|
||||
"crypto/rsa"
|
||||
|
||||
"github.com/didiercrunch/filou/helper"
|
||||
)
|
||||
|
||||
var random = helper.NewMockRandomReader()
|
||||
|
||||
func generateKeyPair() (*rsa.PrivateKey, *rsa.PublicKey) {
|
||||
if key, err := rsa.GenerateKey(random, 1024); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
return key, &key.PublicKey
|
||||
}
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
package asymetric
|
||||
|
||||
import (
|
||||
_ "crypto/sha512"
|
||||
|
||||
"crypto"
|
||||
"crypto/rsa"
|
||||
"testing"
|
||||
|
||||
"github.com/didiercrunch/filou/helper"
|
||||
)
|
||||
|
||||
var random = helper.NewMockRandomReader()
|
||||
|
||||
func generateKeyPair() (*rsa.PrivateKey, *rsa.PublicKey) {
|
||||
if key, err := rsa.GenerateKey(random, 1024); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
return key, &key.PublicKey
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncrypt(t *testing.T) {
|
||||
bobPrivKey, bobPubKey := generateKeyPair()
|
||||
|
||||
r := &RsaOaepPss{nil, bobPubKey, crypto.SHA384, random}
|
||||
|
||||
ciphertext, err := r.Encrypt([]byte("this is a secret message"))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
orgTest, err := rsa.DecryptOAEP(crypto.SHA384.New(), random, bobPrivKey, ciphertext, []byte{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if string(orgTest) != "this is a secret message" {
|
||||
t.Error("the decryption of the message does not fit the original message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptDecrypt(t *testing.T) {
|
||||
bobPrivKey, bobPubKey := generateKeyPair()
|
||||
|
||||
message := "this is a secret message"
|
||||
|
||||
rAlice := &RsaOaepPss{nil, bobPubKey, crypto.SHA384, random}
|
||||
rBob := &RsaOaepPss{bobPrivKey, nil, crypto.SHA384, random}
|
||||
|
||||
if ciphertext, err := rAlice.Encrypt([]byte(message)); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
} else if decryptedText, err := rBob.Decrypt(ciphertext); err != nil {
|
||||
t.Error(err)
|
||||
} else if string(decryptedText) != message {
|
||||
t.Error("the decryption of the message does not fit the original message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSign(t *testing.T) {
|
||||
alicePrivKey, alicePubKey := generateKeyPair()
|
||||
h := crypto.SHA384.New()
|
||||
h.Write([]byte("this is a secret message"))
|
||||
hashedMessage := h.Sum(nil)
|
||||
|
||||
rAlice := &RsaOaepPss{alicePrivKey, nil, crypto.SHA384, random}
|
||||
signature, err := rAlice.Sign(hashedMessage)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if err := rsa.VerifyPSS(alicePubKey, crypto.SHA384, hashedMessage, signature, nil); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignAndVerify(t *testing.T) {
|
||||
alicePrivKey, alicePubKey := generateKeyPair()
|
||||
h := crypto.SHA384.New()
|
||||
h.Write([]byte("this is a secret message"))
|
||||
hashedMessage := h.Sum(nil)
|
||||
|
||||
rAlice := &RsaOaepPss{alicePrivKey, nil, crypto.SHA384, random}
|
||||
rBob := &RsaOaepPss{nil, alicePubKey, crypto.SHA384, random}
|
||||
if signature, err := rAlice.Sign(hashedMessage); err != nil {
|
||||
t.Error(err)
|
||||
} else if err := rBob.VerifySignature(hashedMessage, signature); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAvailableHashes(t *testing.T) {
|
||||
hashes := map[crypto.Hash]bool{crypto.SHA224: true, crypto.SHA256: true, crypto.SHA384: true, crypto.SHA512: true}
|
||||
var i crypto.Hash
|
||||
for i = 0; i < 22; i++ {
|
||||
r := &RsaOaepPss{Hash: i}
|
||||
if _, err := r.getHash(); err != nil && hashes[i] {
|
||||
t.Error("should support", i, "hash type but does not")
|
||||
} else if err == nil && !hashes[i] {
|
||||
t.Error("should not support", i, "hash type but does")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user