main
Raw Download raw file
  1// Package ecc implements a generic interface for ECDH, ECDSA, and EdDSA.
  2package ecc
  3
  4import (
  5	"bytes"
  6	"crypto/elliptic"
  7
  8	"github.com/ProtonMail/go-crypto/bitcurves"
  9	"github.com/ProtonMail/go-crypto/brainpool"
 10	"github.com/ProtonMail/go-crypto/openpgp/internal/encoding"
 11)
 12
 13const Curve25519GenName = "Curve25519"
 14
 15type CurveInfo struct {
 16	GenName string
 17	Oid     *encoding.OID
 18	Curve   Curve
 19}
 20
 21var Curves = []CurveInfo{
 22	{
 23		// NIST P-256
 24		GenName: "P256",
 25		Oid:     encoding.NewOID([]byte{0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07}),
 26		Curve:   NewGenericCurve(elliptic.P256()),
 27	},
 28	{
 29		// NIST P-384
 30		GenName: "P384",
 31		Oid:     encoding.NewOID([]byte{0x2B, 0x81, 0x04, 0x00, 0x22}),
 32		Curve:   NewGenericCurve(elliptic.P384()),
 33	},
 34	{
 35		// NIST P-521
 36		GenName: "P521",
 37		Oid:     encoding.NewOID([]byte{0x2B, 0x81, 0x04, 0x00, 0x23}),
 38		Curve:   NewGenericCurve(elliptic.P521()),
 39	},
 40	{
 41		// SecP256k1
 42		GenName: "SecP256k1",
 43		Oid:     encoding.NewOID([]byte{0x2B, 0x81, 0x04, 0x00, 0x0A}),
 44		Curve:   NewGenericCurve(bitcurves.S256()),
 45	},
 46	{
 47		// Curve25519
 48		GenName: Curve25519GenName,
 49		Oid:     encoding.NewOID([]byte{0x2B, 0x06, 0x01, 0x04, 0x01, 0x97, 0x55, 0x01, 0x05, 0x01}),
 50		Curve:   NewCurve25519(),
 51	},
 52	{
 53		// x448
 54		GenName: "Curve448",
 55		Oid:     encoding.NewOID([]byte{0x2B, 0x65, 0x6F}),
 56		Curve:   NewX448(),
 57	},
 58	{
 59		// Ed25519
 60		GenName: Curve25519GenName,
 61		Oid:     encoding.NewOID([]byte{0x2B, 0x06, 0x01, 0x04, 0x01, 0xDA, 0x47, 0x0F, 0x01}),
 62		Curve:   NewEd25519(),
 63	},
 64	{
 65		// Ed448
 66		GenName: "Curve448",
 67		Oid:     encoding.NewOID([]byte{0x2B, 0x65, 0x71}),
 68		Curve:   NewEd448(),
 69	},
 70	{
 71		// BrainpoolP256r1
 72		GenName: "BrainpoolP256",
 73		Oid:     encoding.NewOID([]byte{0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x07}),
 74		Curve:   NewGenericCurve(brainpool.P256r1()),
 75	},
 76	{
 77		// BrainpoolP384r1
 78		GenName: "BrainpoolP384",
 79		Oid:     encoding.NewOID([]byte{0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0B}),
 80		Curve:   NewGenericCurve(brainpool.P384r1()),
 81	},
 82	{
 83		// BrainpoolP512r1
 84		GenName: "BrainpoolP512",
 85		Oid:     encoding.NewOID([]byte{0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0D}),
 86		Curve:   NewGenericCurve(brainpool.P512r1()),
 87	},
 88}
 89
 90func FindByCurve(curve Curve) *CurveInfo {
 91	for _, curveInfo := range Curves {
 92		if curveInfo.Curve.GetCurveName() == curve.GetCurveName() {
 93			return &curveInfo
 94		}
 95	}
 96	return nil
 97}
 98
 99func FindByOid(oid encoding.Field) *CurveInfo {
100	var rawBytes = oid.Bytes()
101	for _, curveInfo := range Curves {
102		if bytes.Equal(curveInfo.Oid.Bytes(), rawBytes) {
103			return &curveInfo
104		}
105	}
106	return nil
107}
108
109func FindEdDSAByGenName(curveGenName string) EdDSACurve {
110	for _, curveInfo := range Curves {
111		if curveInfo.GenName == curveGenName {
112			curve, ok := curveInfo.Curve.(EdDSACurve)
113			if ok {
114				return curve
115			}
116		}
117	}
118	return nil
119}
120
121func FindECDSAByGenName(curveGenName string) ECDSACurve {
122	for _, curveInfo := range Curves {
123		if curveInfo.GenName == curveGenName {
124			curve, ok := curveInfo.Curve.(ECDSACurve)
125			if ok {
126				return curve
127			}
128		}
129	}
130	return nil
131}
132
133func FindECDHByGenName(curveGenName string) ECDHCurve {
134	for _, curveInfo := range Curves {
135		if curveInfo.GenName == curveGenName {
136			curve, ok := curveInfo.Curve.(ECDHCurve)
137			if ok {
138				return curve
139			}
140		}
141	}
142	return nil
143}