/
curves.go
74 lines (63 loc) · 2.29 KB
/
curves.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
/*
* Copyright (C) 2019 The OnyxChain Authors
* This file is part of The OnyxChain library.
*
* The OnyxChain is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The OnyxChain is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The OnyxChain. If not, see <http://www.gnu.org/licenses/>.
*/
package sm2
import (
"crypto/elliptic"
"math/big"
"sync"
)
var initonce sync.Once
// SM2Curve is the curve interface used in sm2 algorithm.
// It extends elliptic.Curve by adding a function ABytes().
type SM2Curve interface {
elliptic.Curve
// ABytes returns the little endian byte sequence of parameter A.
ABytes() []byte
}
type p256Curve struct {
*elliptic.CurveParams
a []byte
}
var (
p256 p256Curve
)
func initP256() {
// See FIPS 186-3, section D.2.3
p256.CurveParams = &elliptic.CurveParams{Name: "sm2p256v1"}
p256.P, _ = new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16)
p256.N, _ = new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16)
p256.B, _ = new(big.Int).SetString("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", 16)
p256.Gx, _ = new(big.Int).SetString("32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", 16)
p256.Gy, _ = new(big.Int).SetString("BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", 16)
p256.BitSize = 256
p256.a = []byte{0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC}
}
func (curve p256Curve) Params() *elliptic.CurveParams {
return curve.CurveParams
}
func (curve p256Curve) ABytes() []byte {
return curve.a
}
// SM2P256V1 returns the sm2p256v1 curve.
func SM2P256V1() elliptic.Curve {
initonce.Do(initP256)
return p256
}