Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crypto: delete internal crypto #118

Merged
merged 30 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
45052d2
update secp256k1 implementation
Jun 10, 2020
1302a57
delete the inernal crypto folder and use flow-go instead
Nov 24, 2020
49d1ca8
update private and public key
Nov 24, 2020
c66b3aa
fix a test bug
Nov 24, 2020
11cddef
Merge branch 'master' into tarak/use-flow-go-crypto
Nov 24, 2020
5f811f5
fix kms files issues
Nov 24, 2020
d3ec9ac
minor update
Dec 5, 2020
a5bd321
revert a change and update the crypto version
Dec 5, 2020
0a050f3
Merge branch 'master' into tarak/use-flow-go-crypto
Dec 5, 2020
243061e
Merge branch 'master' into tarak/use-flow-go-crypto
Kay-Zee Dec 30, 2020
c060033
Merge branch 'master' into tarak/use-flow-go-crypto
Jan 8, 2021
e72c1d2
fix an issue with go.sum
Jan 8, 2021
dcce723
Merge branch 'master' into tarak/use-flow-go-crypto
Jan 14, 2021
cb4d8ea
delete the inernal crypto folder and use flow-go instead
Nov 24, 2020
bf3ec84
update private and public key
Nov 24, 2020
6b12fd2
fix a test bug
Nov 24, 2020
5d3ae93
fix kms files issues
Nov 24, 2020
08e0667
minor update
Dec 5, 2020
57aa229
revert a change and update the crypto version
Dec 5, 2020
3db72a9
fix an issue after rebasing
Jan 14, 2021
23050f5
use type equality to simplify the code
Jan 15, 2021
31f930d
Merge branch 'tarak/use-flow-go-crypto' of github.com:onflow/flow-go-…
Jan 15, 2021
e2de02d
fix an issue with go.sum
Jan 15, 2021
d1cdc8b
import flow-go-sdk/crypto in flow.go
Jan 22, 2021
fb945ff
update go.sum
Jan 22, 2021
028dd80
go mod tidy
turbolent Jan 23, 2021
134e13b
Merge branch 'master' into tarak/use-flow-go-crypto
Jan 23, 2021
e8a867d
Merge branch 'tarak/use-flow-go-crypto' of github.com:onflow/flow-go-…
Jan 23, 2021
443aecb
Merge branch 'master' into tarak/use-flow-go-crypto
psiemens Feb 17, 2021
ce6aa5c
Update go.sum
psiemens Feb 17, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 27 additions & 56 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,29 @@ import (
"encoding/pem"
"fmt"

"github.com/onflow/flow-go-sdk/crypto/internal/crypto"
"github.com/onflow/flow-go-sdk/crypto/internal/crypto/hash"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/crypto/hash"
)

// SignatureAlgorithm is an identifier for a signature algorithm (and parameters if applicable).
type SignatureAlgorithm int
type SignatureAlgorithm crypto.SigningAlgorithm
tarakby marked this conversation as resolved.
Show resolved Hide resolved

const (
UnknownSignatureAlgorithm SignatureAlgorithm = iota
// BLS_BLS12381 is BLS on BLS 12-381 curve
BLS_BLS12381
tarakby marked this conversation as resolved.
Show resolved Hide resolved
UnknownSignatureAlgorithm SignatureAlgorithm = SignatureAlgorithm(crypto.UnknownSigningAlgorithm)
// ECDSA_P256 is ECDSA on NIST P-256 curve
ECDSA_P256
ECDSA_P256 = SignatureAlgorithm(crypto.ECDSAP256)
// ECDSA_secp256k1 is ECDSA on secp256k1 curve
ECDSA_secp256k1
ECDSA_secp256k1 = SignatureAlgorithm(crypto.ECDSASecp256k1)
)

// String returns the string representation of this signature algorithm.
func (f SignatureAlgorithm) String() string {
return [...]string{"UNKNOWN", "BLS_BLS12381", "ECDSA_P256", "ECDSA_secp256k1"}[f]
return crypto.SigningAlgorithm(f).String()
}

// StringToSignatureAlgorithm converts a string to a SignatureAlgorithm.
func StringToSignatureAlgorithm(s string) SignatureAlgorithm {
switch s {
case BLS_BLS12381.String():
return BLS_BLS12381
case ECDSA_P256.String():
return ECDSA_P256
case ECDSA_secp256k1.String():
Expand All @@ -62,32 +58,29 @@ func StringToSignatureAlgorithm(s string) SignatureAlgorithm {
}

// HashAlgorithm is an identifier for a hash algorithm.
type HashAlgorithm int
type HashAlgorithm hash.HashingAlgorithm

const (
UnknownHashAlgorithm HashAlgorithm = iota
SHA2_256
SHA2_384
SHA3_256
SHA3_384
UnknownHashAlgorithm HashAlgorithm = HashAlgorithm(hash.UnknownHashingAlgorithm)
SHA2_256 = HashAlgorithm(hash.SHA2_256)
SHA2_384 = HashAlgorithm(hash.SHA2_384)
SHA3_256 = HashAlgorithm(hash.SHA3_256)
SHA3_384 = HashAlgorithm(hash.SHA3_384)
tarakby marked this conversation as resolved.
Show resolved Hide resolved
)

// String returns the string representation of this hash algorithm.
func (f HashAlgorithm) String() string {
return [...]string{"UNKNOWN", "SHA2_256", "SHA2_384", "SHA3_256", "SHA3_384"}[f]
return hash.HashingAlgorithm(f).String()
}

// StringToHashAlgorithm converts a string to a HashAlgorithm.
func StringToHashAlgorithm(s string) HashAlgorithm {
switch s {
case SHA2_256.String():
return SHA2_256
case SHA2_384.String():
return SHA2_384
case SHA3_256.String():
return SHA3_256
case SHA3_384.String():
return SHA3_384

default:
return UnknownHashAlgorithm
}
Expand All @@ -111,52 +104,27 @@ func CompatibleAlgorithms(sigAlgo SignatureAlgorithm, hashAlgo HashAlgorithm) bo

// A PrivateKey is a cryptographic private key that can be used for in-memory signing.
type PrivateKey struct {
privateKey crypto.PrivateKey
}

// Sign signs the given message with this private key and the provided hasher.
//
// This function returns an error if a signature cannot be generated.
func (sk PrivateKey) Sign(message []byte, hasher Hasher) ([]byte, error) {
return sk.privateKey.Sign(message, hasher)
crypto.PrivateKey
}

// Algorithm returns the signature algorithm for this private key.
func (sk PrivateKey) Algorithm() SignatureAlgorithm {
return SignatureAlgorithm(sk.privateKey.Algorithm())
return SignatureAlgorithm(sk.PrivateKey.Algorithm())
}
tarakby marked this conversation as resolved.
Show resolved Hide resolved

// PublicKey returns the public key for this private key.
func (sk PrivateKey) PublicKey() PublicKey {
return PublicKey{publicKey: sk.privateKey.PublicKey()}
}

// Encode returns the raw byte encoding of this private key.
func (sk PrivateKey) Encode() []byte {
return sk.privateKey.Encode()
return PublicKey{PublicKey: sk.PrivateKey.PublicKey()}
}

// A PublicKey is a cryptographic public key that can be used to verify signatures.
type PublicKey struct {
publicKey crypto.PublicKey
}

// Verify verifies the given signature against a message with this public key and the provided hasher.
//
// This function returns true if the signature is valid for the message, and false otherwise. An error
// is returned if the signature cannot be verified.
func (pk PublicKey) Verify(sig, message []byte, hasher Hasher) (bool, error) {
return pk.publicKey.Verify(sig, message, hasher)
crypto.PublicKey
}

// Algorithm returns the signature algorithm for this public key.
func (pk PublicKey) Algorithm() SignatureAlgorithm {
return SignatureAlgorithm(pk.publicKey.Algorithm())
}

// Encode returns the raw byte encoding of this public key.
func (pk PublicKey) Encode() []byte {
return pk.publicKey.Encode()
return SignatureAlgorithm(pk.PublicKey.Algorithm())
}

// A Signer is capable of generating cryptographic signatures.
Expand Down Expand Up @@ -197,12 +165,15 @@ func NewNaiveSigner(privateKey PrivateKey, hashAlgo HashAlgorithm) NaiveSigner {
return NewInMemorySigner(privateKey, hashAlgo)
}

// targeted security bits of the cryptographic algorithms
const securityBits = 128

// MinSeedLength is the generic minimum seed length required to guarantee sufficient
// entropy when generating keys.
//
// This minimum is used when the seed source is not necessarily a CSPRG and the seed
// should be expanded before being passed to the key generation process.
const MinSeedLength = crypto.MinSeedLen
const MinSeedLength = 2 * (securityBits / 8)
tarakby marked this conversation as resolved.
Show resolved Hide resolved

func keyGenerationKMACTag(sigAlgo SignatureAlgorithm) []byte {
return []byte(fmt.Sprintf("%s Key Generation", sigAlgo))
Expand Down Expand Up @@ -250,7 +221,7 @@ func GeneratePrivateKey(sigAlgo SignatureAlgorithm, seed []byte) (PrivateKey, er
}

return PrivateKey{
privateKey: privKey,
PrivateKey: privKey,
}, nil
}

Expand All @@ -262,7 +233,7 @@ func DecodePrivateKey(sigAlgo SignatureAlgorithm, b []byte) (PrivateKey, error)
}

return PrivateKey{
privateKey: privKey,
PrivateKey: privKey,
}, nil
}

Expand All @@ -284,7 +255,7 @@ func DecodePublicKey(sigAlgo SignatureAlgorithm, b []byte) (PublicKey, error) {
}

return PublicKey{
publicKey: pubKey,
PublicKey: pubKey,
}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go-sdk/crypto"
fgcrypto "github.com/onflow/flow-go/crypto"
)

func TestGeneratePrivateKey(t *testing.T) {
Expand All @@ -36,7 +37,7 @@ func TestGeneratePrivateKey(t *testing.T) {

// key algorithms not currently supported by the SDK
unsupportedAlgos := []crypto.SignatureAlgorithm{
crypto.BLS_BLS12381,
crypto.SignatureAlgorithm(fgcrypto.BLSBLS12381),
}

// key algorithm that does not represent any valid algorithm
Expand Down
6 changes: 3 additions & 3 deletions crypto/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ package crypto
import (
"fmt"

"github.com/onflow/flow-go-sdk/crypto/internal/crypto/hash"
"github.com/onflow/flow-go/crypto/hash"
)

type Hasher = hash.Hasher
type Hash = hash.Hash
type Hasher hash.Hasher
type Hash hash.Hash

// NewHasher initializes and returns a new hasher with the given hash algorithm.
//
Expand Down
Loading