/
ed25519.go
195 lines (172 loc) · 5.04 KB
/
ed25519.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// This file is part of BOSSWAVE.
//
// BOSSWAVE 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 3 of the License, or
// (at your option) any later version.
//
// BOSSWAVE 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 BOSSWAVE. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright © 2015 Michael Andersen <m.andersen@cs.berkeley.edu>
package crypto
// #cgo CFLAGS: -O3
// #cgo linux LDFLAGS: -lssl -lcrypto
// #cgo !linux CFLAGS: -DWINSUPPORT
// #include "ed25519.h"
// #include <string.h>
// #include <stdint.h>
import "C"
import (
"crypto/rand"
"crypto/sha512"
"encoding/base64"
"errors"
"hash"
"sync"
"unsafe"
)
//These functions are used on windows by the C so we don't have to link to openSSL
var hashCtxLock sync.Mutex
var hashCtxMap map[uint32]hash.Hash
var hashCtxIdx uint32
func init() {
hashCtxMap = make(map[uint32]hash.Hash)
}
//export HashInit
func HashInit(ctx *C.uint32_t) {
hashCtxLock.Lock()
idx := hashCtxIdx
hashCtxIdx++
hashCtxMap[idx] = sha512.New()
hashCtxLock.Unlock()
*ctx = C.uint32_t(idx)
}
//export HashUpdate
func HashUpdate(ctx *C.uint32_t, in *C.uint8_t, inlen C.size_t) {
hashCtxLock.Lock()
h := hashCtxMap[uint32(*ctx)]
hashCtxLock.Unlock()
//Not that I care about windows performance, but this is an
//unecessary copy
h.Write(C.GoBytes(unsafe.Pointer(in), C.int(inlen)))
}
//export HashFinal
func HashFinal(ctx *C.uint32_t, hash *C.uint8_t) {
hashCtxLock.Lock()
h := hashCtxMap[uint32(*ctx)]
delete(hashCtxMap, uint32(*ctx))
hashCtxLock.Unlock()
rv := h.Sum(nil)
C.memcpy(unsafe.Pointer(hash), unsafe.Pointer(&rv[0]), 64)
}
//export Hash
func Hash(hash *C.uint8_t, in *C.uint8_t, inlen C.size_t) {
rv := sha512.Sum512(C.GoBytes(unsafe.Pointer(in), C.int(inlen)))
C.memcpy(unsafe.Pointer(hash), unsafe.Pointer(&rv[0]), 64)
}
//export RandomBytes
func RandomBytes(dest *C.uint8_t, ln C.size_t) {
rv := make([]byte, ln)
rand.Read(rv)
C.memcpy(unsafe.Pointer(dest), unsafe.Pointer(&rv[0]), ln)
}
//SignVector will generate a signature on the arguments, in order
//and return it
func SignVector(sk []byte, vk []byte, into []byte, vec ...[]byte) {
if len(into) != 64 {
panic("Into must be exactly 64 bytes long")
}
lens := make([]C.size_t, len(vec))
for i, v := range vec {
lens[i] = C.size_t(len(v))
}
//From SO user jimt
var b *C.char
ptrSize := unsafe.Sizeof(b)
// Allocate the char** list.
ptr := C.malloc(C.size_t(len(vec)) * C.size_t(ptrSize))
defer C.free(ptr)
// Assign each byte slice to its appropriate offset.
for i := 0; i < len(vec); i++ {
element := (**C.char)(unsafe.Pointer(uintptr(ptr) + uintptr(i)*ptrSize))
*element = (*C.char)(unsafe.Pointer(&vec[i][0]))
}
C.ed25519_sign_vector((**C.uchar)(ptr),
(*C.size_t)(unsafe.Pointer(&lens[0])),
(C.size_t)(len(vec)),
(*C.uchar)(unsafe.Pointer(&sk[0])),
(*C.uchar)(unsafe.Pointer(&vk[0])),
(*C.uchar)(unsafe.Pointer(&into[0])))
}
func SignBlob(sk []byte, vk []byte, into []byte, blob []byte) {
if len(into) != 64 {
panic("into must be exactly 64 bytes long")
}
C.ed25519_sign((*C.uchar)(unsafe.Pointer(&blob[0])),
(C.size_t)(len(blob)),
(*C.uchar)(unsafe.Pointer(&sk[0])),
(*C.uchar)(unsafe.Pointer(&vk[0])),
(*C.uchar)(unsafe.Pointer(&into[0])))
}
//VerifyBlob returns true if the sig is ok, false otherwise
func VerifyBlob(vk []byte, sig []byte, blob []byte) bool {
rv := C.ed25519_sign_open((*C.uchar)(unsafe.Pointer(&blob[0])),
(C.size_t)(len(blob)),
(*C.uchar)(unsafe.Pointer(&vk[0])),
(*C.uchar)(unsafe.Pointer(&sig[0])))
return rv == 0
}
func GenerateKeypair() (sk []byte, vk []byte) {
sk = make([]byte, 32)
vk = make([]byte, 32)
for {
C.bw_generate_keypair((*C.uchar)(unsafe.Pointer(&sk[0])),
(*C.uchar)(unsafe.Pointer(&vk[0])))
if FmtKey(vk)[0] != '-' {
return
}
}
}
func CheckKeypair(sk []byte, vk []byte) bool {
blob := make([]byte, 128)
rand.Read(blob)
sig := make([]byte, 64)
SignBlob(sk, vk, sig, blob)
return VerifyBlob(vk, sig, blob)
}
func FmtKey(key []byte) string {
return base64.URLEncoding.EncodeToString(key)
}
func UnFmtKey(key string) ([]byte, error) {
rv, err := base64.URLEncoding.DecodeString(key)
if len(rv) != 32 {
return nil, errors.New("Invalid length")
}
return rv, err
}
func FmtSig(sig []byte) string {
return base64.URLEncoding.EncodeToString(sig)
}
func UnFmtSig(sig string) ([]byte, error) {
rv, err := base64.URLEncoding.DecodeString(sig)
if len(rv) != 64 {
return nil, errors.New("Invalid length")
}
return rv, err
}
func FmtHash(hash []byte) string {
return base64.URLEncoding.EncodeToString(hash)
}
func UnFmtHash(hash string) ([]byte, error) {
rv, err := base64.URLEncoding.DecodeString(hash)
if len(rv) != 32 {
return nil, errors.New("Invalid length")
}
return rv, err
}