Commit 10ce8b0e authored by Egon Elbre's avatar Egon Elbre Committed by Péter Szilágyi

crypto: fix megacheck warnings (#14917)

* crypto: fix megacheck warnings

* crypto/ecies: remove ASN.1 support
parent 9a7e99f7
This diff is collapsed.
...@@ -151,14 +151,16 @@ var ( ...@@ -151,14 +151,16 @@ var (
func incCounter(ctr []byte) { func incCounter(ctr []byte) {
if ctr[3]++; ctr[3] != 0 { if ctr[3]++; ctr[3] != 0 {
return return
} else if ctr[2]++; ctr[2] != 0 { }
if ctr[2]++; ctr[2] != 0 {
return return
} else if ctr[1]++; ctr[1] != 0 { }
if ctr[1]++; ctr[1] != 0 {
return return
} else if ctr[0]++; ctr[0] != 0 { }
if ctr[0]++; ctr[0] != 0 {
return return
} }
return
} }
// NIST SP 800-56 Concatenation Key Derivation Function (see section 5.8.1). // NIST SP 800-56 Concatenation Key Derivation Function (see section 5.8.1).
......
...@@ -37,7 +37,6 @@ import ( ...@@ -37,7 +37,6 @@ import (
"encoding/hex" "encoding/hex"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"testing" "testing"
...@@ -63,8 +62,7 @@ func TestKDF(t *testing.T) { ...@@ -63,8 +62,7 @@ func TestKDF(t *testing.T) {
t.FailNow() t.FailNow()
} }
if len(k) != 64 { if len(k) != 64 {
fmt.Printf("KDF: generated key is the wrong size (%d instead of 64\n", fmt.Printf("KDF: generated key is the wrong size (%d instead of 64\n", len(k))
len(k))
t.FailNow() t.FailNow()
} }
} }
...@@ -74,14 +72,9 @@ var ErrBadSharedKeys = fmt.Errorf("ecies: shared keys don't match") ...@@ -74,14 +72,9 @@ var ErrBadSharedKeys = fmt.Errorf("ecies: shared keys don't match")
// cmpParams compares a set of ECIES parameters. We assume, as per the // cmpParams compares a set of ECIES parameters. We assume, as per the
// docs, that AES is the only supported symmetric encryption algorithm. // docs, that AES is the only supported symmetric encryption algorithm.
func cmpParams(p1, p2 *ECIESParams) bool { func cmpParams(p1, p2 *ECIESParams) bool {
if p1.hashAlgo != p2.hashAlgo { return p1.hashAlgo == p2.hashAlgo &&
return false p1.KeyLen == p2.KeyLen &&
} else if p1.KeyLen != p2.KeyLen { p1.BlockSize == p2.BlockSize
return false
} else if p1.BlockSize != p2.BlockSize {
return false
}
return true
} }
// cmpPublic returns true if the two public keys represent the same pojnt. // cmpPublic returns true if the two public keys represent the same pojnt.
...@@ -212,118 +205,6 @@ func TestTooBigSharedKey(t *testing.T) { ...@@ -212,118 +205,6 @@ func TestTooBigSharedKey(t *testing.T) {
} }
} }
// Ensure a public key can be successfully marshalled and unmarshalled, and
// that the decoded key is the same as the original.
func TestMarshalPublic(t *testing.T) {
prv, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil {
t.Fatalf("GenerateKey error: %s", err)
}
out, err := MarshalPublic(&prv.PublicKey)
if err != nil {
t.Fatalf("MarshalPublic error: %s", err)
}
pub, err := UnmarshalPublic(out)
if err != nil {
t.Fatalf("UnmarshalPublic error: %s", err)
}
if !cmpPublic(prv.PublicKey, *pub) {
t.Fatal("ecies: failed to unmarshal public key")
}
}
// Ensure that a private key can be encoded into DER format, and that
// the resulting key is properly parsed back into a public key.
func TestMarshalPrivate(t *testing.T) {
prv, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
out, err := MarshalPrivate(prv)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
if dumpEnc {
ioutil.WriteFile("test.out", out, 0644)
}
prv2, err := UnmarshalPrivate(out)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
if !cmpPrivate(prv, prv2) {
fmt.Println("ecdh: private key import failed")
t.FailNow()
}
}
// Ensure that a private key can be successfully encoded to PEM format, and
// the resulting key is properly parsed back in.
func TestPrivatePEM(t *testing.T) {
prv, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
out, err := ExportPrivatePEM(prv)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
if dumpEnc {
ioutil.WriteFile("test.key", out, 0644)
}
prv2, err := ImportPrivatePEM(out)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
} else if !cmpPrivate(prv, prv2) {
fmt.Println("ecdh: import from PEM failed")
t.FailNow()
}
}
// Ensure that a public key can be successfully encoded to PEM format, and
// the resulting key is properly parsed back in.
func TestPublicPEM(t *testing.T) {
prv, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
out, err := ExportPublicPEM(&prv.PublicKey)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
if dumpEnc {
ioutil.WriteFile("test.pem", out, 0644)
}
pub2, err := ImportPublicPEM(out)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
} else if !cmpPublic(prv.PublicKey, *pub2) {
fmt.Println("ecdh: import from PEM failed")
t.FailNow()
}
}
// Benchmark the generation of P256 keys. // Benchmark the generation of P256 keys.
func BenchmarkGenerateKeyP256(b *testing.B) { func BenchmarkGenerateKeyP256(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
...@@ -437,74 +318,27 @@ func TestDecryptShared2(t *testing.T) { ...@@ -437,74 +318,27 @@ func TestDecryptShared2(t *testing.T) {
} }
} }
// TestMarshalEncryption validates the encode/decode produces a valid
// ECIES encryption key.
func TestMarshalEncryption(t *testing.T) {
prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
out, err := MarshalPrivate(prv1)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
prv2, err := UnmarshalPrivate(out)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
message := []byte("Hello, world.")
ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
pt, err := prv2.Decrypt(rand.Reader, ct, nil, nil)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
if !bytes.Equal(pt, message) {
fmt.Println("ecies: plaintext doesn't match message")
t.FailNow()
}
_, err = prv1.Decrypt(rand.Reader, ct, nil, nil)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
}
type testCase struct { type testCase struct {
Curve elliptic.Curve Curve elliptic.Curve
Name string Name string
Expected bool Expected *ECIESParams
} }
var testCases = []testCase{ var testCases = []testCase{
{ {
Curve: elliptic.P256(), Curve: elliptic.P256(),
Name: "P256", Name: "P256",
Expected: true, Expected: ECIES_AES128_SHA256,
}, },
{ {
Curve: elliptic.P384(), Curve: elliptic.P384(),
Name: "P384", Name: "P384",
Expected: true, Expected: ECIES_AES256_SHA384,
}, },
{ {
Curve: elliptic.P521(), Curve: elliptic.P521(),
Name: "P521", Name: "P521",
Expected: true, Expected: ECIES_AES256_SHA512,
}, },
} }
...@@ -519,10 +353,10 @@ func TestParamSelection(t *testing.T) { ...@@ -519,10 +353,10 @@ func TestParamSelection(t *testing.T) {
func testParamSelection(t *testing.T, c testCase) { func testParamSelection(t *testing.T, c testCase) {
params := ParamsFromCurve(c.Curve) params := ParamsFromCurve(c.Curve)
if params == nil && c.Expected { if params == nil && c.Expected != nil {
fmt.Printf("%s (%s)\n", ErrInvalidParams.Error(), c.Name) fmt.Printf("%s (%s)\n", ErrInvalidParams.Error(), c.Name)
t.FailNow() t.FailNow()
} else if params != nil && !c.Expected { } else if params != nil && !cmpParams(params, c.Expected) {
fmt.Printf("ecies: parameters should be invalid (%s)\n", fmt.Printf("ecies: parameters should be invalid (%s)\n",
c.Name) c.Name)
t.FailNow() t.FailNow()
......
...@@ -114,97 +114,4 @@ func AddParamsForCurve(curve elliptic.Curve, params *ECIESParams) { ...@@ -114,97 +114,4 @@ func AddParamsForCurve(curve elliptic.Curve, params *ECIESParams) {
// Only the curves P256, P384, and P512 are supported. // Only the curves P256, P384, and P512 are supported.
func ParamsFromCurve(curve elliptic.Curve) (params *ECIESParams) { func ParamsFromCurve(curve elliptic.Curve) (params *ECIESParams) {
return paramsFromCurve[curve] return paramsFromCurve[curve]
/*
switch curve {
case elliptic.P256():
return ECIES_AES128_SHA256
case elliptic.P384():
return ECIES_AES256_SHA384
case elliptic.P521():
return ECIES_AES256_SHA512
default:
return nil
}
*/
}
// ASN.1 encode the ECIES parameters relevant to the encryption operations.
func paramsToASNECIES(params *ECIESParams) (asnParams asnECIESParameters) {
if nil == params {
return
}
asnParams.KDF = asnNISTConcatenationKDF
asnParams.MAC = hmacFull
switch params.KeyLen {
case 16:
asnParams.Sym = aes128CTRinECIES
case 24:
asnParams.Sym = aes192CTRinECIES
case 32:
asnParams.Sym = aes256CTRinECIES
}
return
}
// ASN.1 encode the ECIES parameters relevant to ECDH.
func paramsToASNECDH(params *ECIESParams) (algo asnECDHAlgorithm) {
switch params.hashAlgo {
case crypto.SHA224:
algo = dhSinglePass_stdDH_sha224kdf
case crypto.SHA256:
algo = dhSinglePass_stdDH_sha256kdf
case crypto.SHA384:
algo = dhSinglePass_stdDH_sha384kdf
case crypto.SHA512:
algo = dhSinglePass_stdDH_sha512kdf
}
return
}
// ASN.1 decode the ECIES parameters relevant to the encryption stage.
func asnECIEStoParams(asnParams asnECIESParameters, params *ECIESParams) {
if !asnParams.KDF.Cmp(asnNISTConcatenationKDF) {
params = nil
return
} else if !asnParams.MAC.Cmp(hmacFull) {
params = nil
return
}
switch {
case asnParams.Sym.Cmp(aes128CTRinECIES):
params.KeyLen = 16
params.BlockSize = 16
params.Cipher = aes.NewCipher
case asnParams.Sym.Cmp(aes192CTRinECIES):
params.KeyLen = 24
params.BlockSize = 16
params.Cipher = aes.NewCipher
case asnParams.Sym.Cmp(aes256CTRinECIES):
params.KeyLen = 32
params.BlockSize = 16
params.Cipher = aes.NewCipher
default:
params = nil
}
}
// ASN.1 decode the ECIES parameters relevant to ECDH.
func asnECDHtoParams(asnParams asnECDHAlgorithm, params *ECIESParams) {
if asnParams.Cmp(dhSinglePass_stdDH_sha224kdf) {
params.hashAlgo = crypto.SHA224
params.Hash = sha256.New224
} else if asnParams.Cmp(dhSinglePass_stdDH_sha256kdf) {
params.hashAlgo = crypto.SHA256
params.Hash = sha256.New
} else if asnParams.Cmp(dhSinglePass_stdDH_sha384kdf) {
params.hashAlgo = crypto.SHA384
params.Hash = sha512.New384
} else if asnParams.Cmp(dhSinglePass_stdDH_sha512kdf) {
params.hashAlgo = crypto.SHA512
params.Hash = sha512.New
} else {
params = nil
}
} }
...@@ -42,9 +42,8 @@ type state struct { ...@@ -42,9 +42,8 @@ type state struct {
storage [maxRate]byte storage [maxRate]byte
// Specific to SHA-3 and SHAKE. // Specific to SHA-3 and SHAKE.
fixedOutput bool // whether this is a fixed-output-length instance outputLen int // the default output size in bytes
outputLen int // the default output size in bytes state spongeDirection // whether the sponge is absorbing or squeezing
state spongeDirection // whether the sponge is absorbing or squeezing
} }
// BlockSize returns the rate of sponge underlying this hash function. // BlockSize returns the rate of sponge underlying this hash function.
......
...@@ -53,15 +53,6 @@ var testShakes = map[string]func() ShakeHash{ ...@@ -53,15 +53,6 @@ var testShakes = map[string]func() ShakeHash{
"SHAKE256": NewShake256, "SHAKE256": NewShake256,
} }
// decodeHex converts a hex-encoded string into a raw byte string.
func decodeHex(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return b
}
// structs used to marshal JSON test-cases. // structs used to marshal JSON test-cases.
type KeccakKats struct { type KeccakKats struct {
Kats map[string][]struct { Kats map[string][]struct {
...@@ -125,7 +116,7 @@ func TestKeccakKats(t *testing.T) { ...@@ -125,7 +116,7 @@ func TestKeccakKats(t *testing.T) {
// TestUnalignedWrite tests that writing data in an arbitrary pattern with // TestUnalignedWrite tests that writing data in an arbitrary pattern with
// small input buffers. // small input buffers.
func testUnalignedWrite(t *testing.T) { func TestUnalignedWrite(t *testing.T) {
testUnalignedAndGeneric(t, func(impl string) { testUnalignedAndGeneric(t, func(impl string) {
buf := sequentialBytes(0x10000) buf := sequentialBytes(0x10000)
for alg, df := range testDigests { for alg, df := range testDigests {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment