Golang : How to get ECDSA curve and parameters data?
Problem :
"ECDSA cents have a parameters block which has the parameters for the curve. Any info regarding how those would be created and saved in Go?"
Solution :
In the previous tutorial on how to use ECDSA Elliptic curve algorithm functions. We learn how to generate a private key and extract the public key from the private key.
From the public key data structure, we can observe that the Curve data is embedded inside the public key.
type PublicKey struct {
elliptic.Curve
X, Y *big.Int
}
and from https://golang.org/pkg/crypto/elliptic/#Curve interface, we can use the Params() function to returns the parameters for the curve and retrieve other type of data as well.
type Curve interface {
// Params returns the parameters for the curve.
Params() *CurveParams
// IsOnCurve returns true if the given (x,y) lies on the curve.
IsOnCurve(x, y *big.Int) bool
// Add returns the sum of (x1,y1) and (x2,y2)
Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int)
// Double returns 2*(x,y)
Double(x1, y1 *big.Int) (x, y *big.Int)
// ScalarMult returns k*(Bx,By) where k is a number in big-endian form.
ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int)
// ScalarBaseMult returns k*G, where G is the base point of the group
// and k is an integer in big-endian form.
ScalarBaseMult(k []byte) (x, y *big.Int)
}
The following code example demonstrates how to extract the curve and parameters data.
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"fmt"
"os"
)
func main() {
pubkeyCurve := elliptic.P256() //see http://golang.org/pkg/crypto/elliptic/#P256
privatekey := new(ecdsa.PrivateKey)
privatekey, err := ecdsa.GenerateKey(pubkeyCurve, rand.Reader) // this generates a public & private key pair
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var pubkey ecdsa.PublicKey
pubkey = privatekey.PublicKey
fmt.Println("Public Key : ")
// PublicKey represents and ECDSA public key
// type PublicKey struct {
// elliptic.Curve
// X, Y *big.Int
//}
fmt.Println("Curve : ", pubkey.Curve)
// Access curve data type and parameters example
// ---- https://golang.org/pkg/crypto/elliptic/#Curve
curveParams := pubkey.Curve.Params()
fmt.Println("P : ", curveParams.P)
fmt.Println("N : ", curveParams.N)
fmt.Println("B : ", curveParams.B)
fmt.Printf("Gx, Gy : %v, %v\n", curveParams.Gx, curveParams.Gy)
fmt.Println("BitSize : ", curveParams.BitSize)
fmt.Println("Is On Curve ? ", pubkey.Curve.IsOnCurve(pubkey.X, pubkey.Y))
fmt.Println("X : ", pubkey.X)
fmt.Println("Y : ", pubkey.Y)
}
Sample output :
Public Key :
Curve : {0x20824e450}
P : 115792089210356248762697446949407573530086143415290314195533631308867097853951
N : 115792089210356248762697446949407573529996955224135760342422259061068512044369
B : 41058363725152142129326129780047268409114441015993725554835256314039467401291
Gx, Gy : 48439561293906451759052585252797914202762949526041747995844080717082404635286, 36134250956749795798585127919587881956611106672985015071877198253568414405109
BitSize : 256
Is On Curve ? true
X : 81954825413260908377823288112466052223174308460634959356324006972238863306292
Y : 104045399658056244122317769635250651827793337497571819907947708147764927071641
References :
https://golang.org/pkg/crypto/ecdsa/#PublicKey
See also : Golang : Saving private and public key to files
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.4k Golang : How to extract video or image files from html source code
+16.9k Golang : Covert map/slice/array to JSON or XML format
+16.3k Golang : How to implement two-factor authentication?
+6k Golang : Convert Chinese UTF8 characters to Pin Yin
+10.2k Golang : cannot assign type int to value (type uint8) in range error
+5.8k Golang : Denco multiplexer example
+19.1k Golang : Delete item from slice based on index/key position
+14.4k Golang : Reset buffer example
+36.1k Golang : How to split or chunking a file to smaller pieces?
+14.2k Golang : Get uploaded file name or access uploaded files
+8.9k Golang : automatically figure out array length(size) with three dots