-
Notifications
You must be signed in to change notification settings - Fork 0
/
curve.go
77 lines (71 loc) · 1.49 KB
/
curve.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package ecc
import (
"crypto/elliptic"
"sync"
)
type Curve struct {
Id uint8
Name string
Aliases []string
Description string
Curve func() elliptic.Curve
}
var Curves = []*Curve {
{
Id: 1,
Name: "P-224",
Aliases: []string{"secp224r1"},
Description: "P-224 (FIPS 186-3, section D.2.2)",
Curve: elliptic.P224,
},
{
Id: 2,
Name: "P-256",
Aliases: []string{"secp256r1", "prime256v1"},
Description: "NIST P-256 (FIPS 186-3, section D.2.3)",
Curve: elliptic.P256,
},
{
Id: 3,
Name: "P-384",
Aliases: []string{"secp384r1"},
Description: "NIST P-384 (FIPS 186-3, section D.2.4)",
Curve: elliptic.P384,
},
{
Id: 4,
Name: "P-521",
Aliases: []string{"secp521r1"},
Description: "NIST P-521 (FIPS 186-3, section D.2.5)",
Curve: elliptic.P521,
},
}
var curvesIndexMutex sync.Mutex
var curvesIndex map[string]*Curve
// LookupCurve finds Curve by specified name.
func LookupCurve(name string) *Curve {
curvesIndexMutex.Lock()
if curvesIndex == nil {
curvesIndex = make(map[string]*Curve)
for _, curve := range Curves {
curvesIndex[curve.Name] = curve
for _, alias := range curve.Aliases {
curvesIndex[alias] = curve
}
}
}
curvesIndexMutex.Unlock()
if curve, ok := curvesIndex[name]; ok {
return curve
}
return nil
}
func IdentifyCurve(c elliptic.Curve) *Curve {
curveName := c.Params().Name
for _, curve := range Curves {
if curve.Name == curveName {
return curve
}
}
return nil
}