Skip to content

Commit 8827807

Browse files
authored
Implementation of NHP-KGC
1. Setup KGC 2. Generate user private key and declared public key 3. Use user private key to sign the message 4. Use user declared public key, user identifier to verify the signature of message
1 parent ce103e5 commit 8827807

File tree

11 files changed

+1137
-160
lines changed

11 files changed

+1137
-160
lines changed

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export GO111MODULE := on
2-
CUSTOM_LD_FLAGS ?=
2+
CUSTOM_LD_FLAGS ?=
33

44
all: generate-version-and-build
55

@@ -61,6 +61,7 @@ generate-version-and-build:
6161
@$(MAKE) acd
6262
@$(MAKE) serverd
6363
@$(MAKE) db
64+
@$(MAKE) kgc
6465
@$(MAKE) agentsdk
6566
@$(MAKE) devicesdk
6667
@$(MAKE) plugins
@@ -99,6 +100,13 @@ db:
99100
mkdir -p ../release/nhp-db/etc; \
100101
cp ./db/main/etc/*.toml ../release/nhp-db/etc/
101102

103+
kgc:
104+
@echo "$(COLOUR_BLUE)[OpenNHP] Building nhp-kgc... $(END_COLOUR)"
105+
cd endpoints && \
106+
go build -trimpath -ldflags ${LD_FLAGS} -v -o ../release/nhp-kgc/nhp-kgc ./kgc/main/main.go && \
107+
mkdir -p ../release/nhp-kgc/etc; \
108+
cp ./kgc/main/etc/*.toml ../release/nhp-kgc/etc/
109+
102110
agentsdk:
103111
@echo "$(COLOUR_BLUE)[OpenNHP] Building agent SDK... $(END_COLOUR)"
104112
ifeq ($(OS_NAME), linux)

build.bat

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ IF %ERRORLEVEL% NEQ 0 goto :exit
4646
if not exist ..\release\nhp-db\etc mkdir ..\release\nhp-db\etc
4747
copy db\main\etc\*.* ..\release\nhp-db\etc
4848

49+
:kgc
50+
go build -trimpath -ldflags %LD_FLAGS% -v -o ..\release\nhp-kgc\nhp-kgc.exe kgc\main\main.go
51+
IF %ERRORLEVEL% NEQ 0 goto :exit
52+
if not exist ..\release\nhp-kgc\etc mkdir ..\release\nhp-kgc\etc
53+
copy kgc\main\etc\*.* ..\release\nhp-kgc\etc
54+
4955
:agentsdk
5056
go build -trimpath -buildmode=c-shared -ldflags %LD_FLAGS% -v -o ..\release\nhp-agent\nhp-agent.dll agent\main\main.go agent\main\export.go
5157
IF %ERRORLEVEL% NEQ 0 goto :exit

docs/build.md

Lines changed: 160 additions & 159 deletions
Large diffs are not rendered by default.

docs/zh-cn/build.zh-cn.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ permalink: /zh-cn/build/
162162
- **NHP-AC**的可执行文件和配置文件: `release\nhp-ac` 子目录下
163163
- **NHP-Agent**的可执行文件和配置文件: `release\nhp-agent` 子目录下
164164
- **NHP-DB**的可执行文件和配置文件: `release\nhp-db` 子目录下
165+
- **NHP-KGC**的可执行文件和配置文件: `release\nhp-kgc` 子目录下
165166
- 所有二进制文件打包成一个`tar`文件: `release\archive` 子目录下
166167
167168
---

endpoints/kgc/curve/curve.go

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
// This package provides the interface and custom implementation of elliptic curve operations.
2+
// It defines interfaces and structures to perform standard elliptic curve cryptographic operations
3+
// such as point addition, scalar multiplication, and checking whether a point lies on the curve.
4+
// we can create own custom curve or wrap official curve to follow the interface of this package.
5+
6+
package curve
7+
8+
import (
9+
"crypto/elliptic"
10+
"math/big"
11+
12+
"github.com/emmansun/gmsm/sm2"
13+
)
14+
15+
type CurveParams struct {
16+
P *big.Int // the order of the underlying field
17+
N *big.Int // the order of the base point
18+
A *big.Int // the constant of the curve equation
19+
B *big.Int // the constant of the curve equation
20+
Gx, Gy *big.Int // (x,y) of the base point
21+
BitSize int // the size of the underlying field
22+
Name string // the canonical name of the curve
23+
}
24+
25+
type Curve interface {
26+
Params() *CurveParams
27+
IsOnCurve(x, y *big.Int) bool
28+
Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int)
29+
ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int)
30+
ScalarBaseMult(k []byte) (x, y *big.Int)
31+
}
32+
33+
type CustomStandardCurve struct {
34+
*CurveParams
35+
}
36+
37+
func (c *CustomStandardCurve) Params() *CurveParams {
38+
return c.CurveParams
39+
}
40+
41+
func (c *CustomStandardCurve) IsOnCurve(x, y *big.Int) bool {
42+
if x.Sign() == 0 && y.Sign() == 0 {
43+
return true // infinite point
44+
}
45+
46+
// calculate y² mod p
47+
ySquare := new(big.Int).Exp(y, big.NewInt(2), c.P)
48+
49+
// calculate x³ + ax + b mod p
50+
x3 := new(big.Int).Exp(x, big.NewInt(3), c.P)
51+
ax := new(big.Int).Mul(c.A, x)
52+
ax.Mod(ax, c.P)
53+
54+
rhs := new(big.Int).Add(x3, ax)
55+
rhs.Add(rhs, c.B)
56+
rhs.Mod(rhs, c.P)
57+
58+
return ySquare.Cmp(rhs) == 0
59+
}
60+
61+
func (c *CustomStandardCurve) Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int) {
62+
if x1.Sign() == 0 && y1.Sign() == 0 {
63+
return x2, y2
64+
}
65+
66+
if x2.Sign() == 0 && y2.Sign() == 0 {
67+
return x1, y1
68+
}
69+
70+
// handle inverse point (P + (-P) = infinity point)
71+
if x1.Cmp(x2) == 0 && y1.Cmp(new(big.Int).Sub(c.P, y2)) == 0 {
72+
return new(big.Int), new(big.Int)
73+
}
74+
75+
var lambda *big.Int
76+
77+
// point doubling (P == Q)
78+
if x1.Cmp(x2) == 0 && y1.Cmp(y2) == 0 {
79+
// λ = (3x² + a) / (2y) mod p
80+
num := new(big.Int).Mul(big.NewInt(3), new(big.Int).Exp(x1, big.NewInt(2), nil))
81+
num.Add(num, c.A)
82+
num.Mod(num, c.P)
83+
84+
den := new(big.Int).Mul(big.NewInt(2), y1)
85+
den.Mod(den, c.P)
86+
87+
// calculate modular inverse
88+
denInv := new(big.Int).ModInverse(den, c.P)
89+
lambda = new(big.Int).Mul(num, denInv)
90+
lambda.Mod(lambda, c.P)
91+
} else {
92+
// normal addition (P ≠ Q)
93+
// λ = (y₂ - y₁) / (x₂ - x₁) mod p
94+
num := new(big.Int).Sub(y2, y1)
95+
num.Mod(num, c.P)
96+
97+
den := new(big.Int).Sub(x2, x1)
98+
den.Mod(den, c.P)
99+
100+
denInv := new(big.Int).ModInverse(den, c.P)
101+
lambda = new(big.Int).Mul(num, denInv)
102+
lambda.Mod(lambda, c.P)
103+
}
104+
105+
// calculate x₃ = λ² - x₁ - x₂ mod p
106+
x3 := new(big.Int).Exp(lambda, big.NewInt(2), nil)
107+
x3.Sub(x3, x1)
108+
x3.Sub(x3, x2)
109+
x3.Mod(x3, c.P)
110+
111+
// calculate y₃ = λ(x₁ - x₃) - y₁ mod p
112+
y3 := new(big.Int).Sub(x1, x3)
113+
y3.Mul(y3, lambda)
114+
y3.Sub(y3, y1)
115+
y3.Mod(y3, c.P)
116+
117+
118+
return x3, y3
119+
}
120+
121+
func (c *CustomStandardCurve) ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int) {
122+
x = new(big.Int)
123+
y = new(big.Int)
124+
125+
kCopy := new(big.Int).SetBytes(k)
126+
127+
// binary expansion (Montgomery ladder algorithm)
128+
for kCopy.Sign() > 0 {
129+
if kCopy.Bit(0) == 1 {
130+
x, y = c.Add(x, y, x1, y1)
131+
}
132+
133+
x1, y1 = c.Add(x1, y1, x1, y1) // point doubling
134+
kCopy.Rsh(kCopy, 1) // right shift by one bit
135+
}
136+
137+
return x, y
138+
}
139+
140+
func (c *CustomStandardCurve) ScalarBaseMult(k []byte) (x, y *big.Int) {
141+
return c.ScalarMult(c.Gx, c.Gy, k)
142+
}
143+
144+
func NewCustomSM2Curve() *CustomStandardCurve {
145+
p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16)
146+
a, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", 16)
147+
b, _ := new(big.Int).SetString("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", 16)
148+
n, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16)
149+
150+
gx, _ := new(big.Int).SetString("32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", 16)
151+
gy, _ := new(big.Int).SetString("BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", 16)
152+
153+
return &CustomStandardCurve{
154+
&CurveParams{
155+
P: p,
156+
N: n,
157+
A: a,
158+
B: b,
159+
Gx: gx,
160+
Gy: gy,
161+
BitSize: 256,
162+
Name: "Custom SM2",
163+
},
164+
}
165+
}
166+
167+
func NewCustomSecp256k1Curve() *CustomStandardCurve {
168+
p, _ := new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16)
169+
a, _ := new(big.Int).SetString("000000000000000000000000000000000000000000000000000000000000", 16)
170+
b, _ := new(big.Int).SetString("000000000000000000000000000000000000000000000000000000000007", 16)
171+
n, _ := new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16)
172+
173+
gx, _ := new(big.Int).SetString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16)
174+
gy, _ := new(big.Int).SetString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16)
175+
176+
return &CustomStandardCurve{
177+
&CurveParams{
178+
P: p,
179+
N: n,
180+
A: a,
181+
B: b,
182+
Gx: gx,
183+
Gy: gy,
184+
BitSize: 256,
185+
Name: "Custom Secp256k1",
186+
},
187+
}
188+
}
189+
190+
type OfficialSM2Curve struct {
191+
curve elliptic.Curve
192+
}
193+
194+
func (c *OfficialSM2Curve) Params() *CurveParams {
195+
A := new(big.Int).Mod(big.NewInt(-3), c.curve.Params().P)
196+
197+
return &CurveParams{
198+
P: c.curve.Params().P,
199+
N: c.curve.Params().N,
200+
A: A,
201+
B: c.curve.Params().B,
202+
Gx: c.curve.Params().Gx,
203+
Gy: c.curve.Params().Gy,
204+
BitSize: c.curve.Params().BitSize,
205+
Name: c.curve.Params().Name,
206+
}
207+
}
208+
209+
func (c *OfficialSM2Curve) IsOnCurve(x, y *big.Int) bool {
210+
return c.curve.IsOnCurve(x, y)
211+
}
212+
213+
func (c *OfficialSM2Curve) Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int) {
214+
return c.curve.Add(x1, y1, x2, y2)
215+
}
216+
217+
func (c *OfficialSM2Curve) ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int) {
218+
return c.curve.ScalarMult(x1, y1, k)
219+
}
220+
221+
func (c *OfficialSM2Curve) ScalarBaseMult(k []byte) (x, y *big.Int) {
222+
return c.curve.ScalarBaseMult(k)
223+
}
224+
225+
func NewOfficialSM2Curve() *OfficialSM2Curve {
226+
return &OfficialSM2Curve{
227+
curve: sm2.P256(),
228+
}
229+
}

0 commit comments

Comments
 (0)