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.3k Golang : Turn string or text file into slice example
+20.5k Golang : Clean up null characters from input data
+21.5k Golang : Convert seconds to minutes and remainder seconds
+16k Golang : Send email and SMTP configuration example
+21.4k Golang : How to reverse slice or array elements order
+4.1k Java : Generate multiplication table example
+4k Javascript : How to show different content with noscript?
+6.7k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+13.9k Golang : Recombine chunked files example
+24k Golang : Time slice or date sort and reverse sort example
+12.4k Golang : Get terminal width and height example
+16.4k Golang : Get own process identifier