forked from aws/aws-lc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for secp256k1 elliptic curve (aws#457)
The change adds support for secp256k1 elliptic curve (required by ACCP). We use the generic |EC_GFp_mont_method| for the new curve. The largest part of the change is adding tests to cover as much as possible the potential use of the curve. We have added EC, ECDH, and ECDSA tests. In all three cases we generate the test vectors by newly added Go scripts. The standard Go library doesn't have support for secp256k1, and moreover, we can't instantiate the curve on our own because Go's implementation assumes a curve given by y^2 = x^3 + ax + b has a = -3, which is not the case for secp256k1. We work around this issue by using the most widely used secp256k1 Go implementation, Ethereum's go-ethereum/crypto/secp256k1 module. Also, EVP Wycheproof tests specific to secp256k1 were added. A small issue with FIPS service indicator test for ECDH is also fixed. The test assumed in one case that the indicator should always return APPROVED, when in reality the indicator status should depened on which elliptic curve is used in the protocol. For example, when a non FIPS approved curve like secp256k1 is used, we expect the indicator to return NOT_APPROVED. Co-authored-by: Dusan Kostic <[email protected]>
- Loading branch information
Showing
20 changed files
with
9,567 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// This script is used to generate the ECDH test vectors for secp256k1 curve. | ||
|
||
package main | ||
|
||
import ( | ||
"crypto/sha256" | ||
"math/big" | ||
"strconv" | ||
"fmt" | ||
) | ||
|
||
import "github.com/ethereum/go-ethereum/crypto/secp256k1" | ||
|
||
// Number of test vectors to be generated | ||
const numOfTests = 25 | ||
|
||
// Initialize the counter used for generating pseudo-random numbers | ||
// using the SHA-256 hash function. | ||
var prng_ctr = 1 | ||
|
||
func printPadded(key string, n, max *big.Int) { | ||
padded := make([]byte, len(max.Bytes())) | ||
b := n.Bytes() | ||
copy(padded[len(padded)-len(b):], b) | ||
fmt.Printf("%s = %x\n", key, padded) | ||
} | ||
|
||
func genRandModN(N *big.Int) *big.Int { | ||
res := new(big.Int) | ||
for { | ||
dgst := sha256.Sum256([]byte("Dummy string" + strconv.Itoa(prng_ctr))) | ||
prng_ctr++ | ||
res.SetBytes(dgst[:]) | ||
if res.Cmp(N) == -1 { | ||
break | ||
} | ||
} | ||
return res | ||
} | ||
|
||
func main() { | ||
|
||
curve := secp256k1.S256() | ||
|
||
fmt.Printf("\n# Test vectors for secp256k1 curve were produced by") | ||
fmt.Printf("\n# the |make_secp256k1_test_vectors.go| script.\n\n") | ||
|
||
for i := 0; i < numOfTests; i++ { | ||
// Generate a private key for Alice and for Bob | ||
sA := genRandModN(curve.Params().P) | ||
sB := genRandModN(curve.Params().P) | ||
|
||
// Compute the corresponding public key | ||
xA, yA := curve.ScalarBaseMult(sA.Bytes()) | ||
xB, yB := curve.ScalarBaseMult(sB.Bytes()) | ||
|
||
// Compute shared keys zA and zB for Alice and Bob | ||
zA, _ := curve.ScalarMult(xB, yB, sA.Bytes()) | ||
zB, _ := curve.ScalarMult(xA, yA, sB.Bytes()) | ||
|
||
if zA.Cmp(zB) != 0 { | ||
fmt.Printf("Error, shared secret keys for Alice and Bob are different!") | ||
return | ||
} | ||
|
||
// Print all the required values | ||
fmt.Printf("Curve = secp256k1\n") | ||
printPadded("Private", sA, curve.Params().P) | ||
printPadded("X", xA, curve.Params().P) | ||
printPadded("Y", yA, curve.Params().P) | ||
printPadded("PeerX", xB, curve.Params().P) | ||
printPadded("PeerY", yB, curve.Params().P) | ||
printPadded("Z", zA, curve.Params().P) | ||
fmt.Printf("\n") | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.