...

Source file src/github.com/ProtonMail/go-crypto/openpgp/internal/ecc/curve_info.go

Documentation: github.com/ProtonMail/go-crypto/openpgp/internal/ecc

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

View as plain text