-
Notifications
You must be signed in to change notification settings - Fork 8
/
ecc.go
124 lines (105 loc) · 4.28 KB
/
ecc.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* ecc.go
*
* Copyright (C) 2006-2022 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
package wolfSSL
// #include <wolfssl/options.h>
// #include <wolfssl/wolfcrypt/ecc.h>
// #include <wolfssl/wolfcrypt/asn_public.h>
// #include <wolfssl/wolfcrypt/random.h>
// #ifndef HAVE_ECC
// #define ECC_MAX_SIG_SIZE 1
// typedef struct ecc_key {} ecc_key;
// int wc_ecc_init(ecc_key *key) {
// return -174;
// }
// int wc_ecc_free(ecc_key *key) {
// return -174;
// }
// int wc_ecc_make_key(WC_RNG* rng, int keysize, ecc_key* key) {
// return -174;
// }
// int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
// WC_RNG* rng, ecc_key* key) {
// return -174;
// }
// int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
// word32 hashlen, int* res, ecc_key* key) {
// return -174;
// }
// #endif
import "C"
import (
"unsafe"
)
const ECC_MAX_SIG_SIZE = int(C.ECC_MAX_SIG_SIZE)
type Ecc_key = C.struct_ecc_key
const ECC_SECP256R1 = int(C.ECC_SECP256R1)
func Wc_ecc_init(key *C.struct_ecc_key) int {
return int(C.wc_ecc_init(key))
}
func Wc_ecc_free(key *C.struct_ecc_key) int {
return int(C.wc_ecc_free(key))
}
func Wc_ecc_make_key(rng *C.struct_WC_RNG, keySize int, key *C.struct_ecc_key) int {
return int(C.wc_ecc_make_key(rng, C.int(keySize), key))
}
func Wc_ecc_make_pub_in_priv(key *C.struct_ecc_key) int {
return int(C.wc_ecc_make_pub(key, nil))
}
func Wc_ecc_set_rng(key *C.struct_ecc_key, rng *C.struct_WC_RNG) int {
return int(C.wc_ecc_set_rng(key, rng))
}
func Wc_ecc_export_private_only(key *C.struct_ecc_key, out []byte, outLen *int) int {
cOutLen := C.word32(*outLen)
ret := int(C.wc_ecc_export_private_only(key, (*C.byte)(unsafe.Pointer(&out[0])), &cOutLen))
*outLen = int(cOutLen)
return ret
}
func Wc_ecc_export_x963_ex(key *C.struct_ecc_key, out []byte, outLen *int, compressed int) int {
cOutLen := C.word32(*outLen)
ret := int(C.wc_ecc_export_x963_ex(key, (*C.byte)(unsafe.Pointer(&out[0])), &cOutLen, C.int(compressed)))
*outLen = int(cOutLen)
return ret
}
func Wc_ecc_import_private_key_ex(priv []byte, privSz int, pub []byte, pubSz int, key *C.struct_ecc_key, curveId int) int {
privPtr := (*C.byte)(unsafe.Pointer(&priv[0]))
var pubPtr *C.byte
if pubSz > 0 {
pubPtr = (*C.byte)(unsafe.Pointer(&pub[0]))
}
return int(C.wc_ecc_import_private_key_ex(privPtr, C.word32(privSz), pubPtr, C.word32(pubSz), key, C.int(curveId)))
}
func Wc_ecc_import_x963_ex(pubKey []byte, pubSz int, key *C.struct_ecc_key, curveID int) int {
return int(C.wc_ecc_import_x963_ex((*C.uchar)(unsafe.Pointer(&pubKey[0])), C.word32(pubSz), key, C.int(curveID)))
}
func Wc_ecc_sign_hash(in []byte, inLen int, out []byte, outLen *int, rng *C.struct_WC_RNG, key *C.struct_ecc_key) int {
return int(C.wc_ecc_sign_hash((*C.uchar)(unsafe.Pointer(&in[0])), C.word32(inLen),
(*C.uchar)(unsafe.Pointer(&out[0])), (*C.word32)(unsafe.Pointer(outLen)), rng, key))
}
func Wc_ecc_verify_hash(sig []byte, sigLen int, hash []byte, hashLen int, res *int, key *C.struct_ecc_key) int {
return int(C.wc_ecc_verify_hash((*C.uchar)(unsafe.Pointer(&sig[0])), C.word32(sigLen),
(*C.uchar)(unsafe.Pointer(&hash[0])), C.word32(sigLen), (*C.int)(unsafe.Pointer(res)), key))
}
func Wc_ecc_shared_secret(privKey, pubKey *C.struct_ecc_key, out []byte, outLen *int) int {
cOutLen := C.word32(*outLen)
ret := int(C.wc_ecc_shared_secret(privKey, pubKey, (*C.uchar)(unsafe.Pointer(&out[0])), &cOutLen))
*outLen = int(cOutLen)
return ret
}