forked from kryptco/kr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrypto.go
135 lines (115 loc) · 2.68 KB
/
krypto.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
package kr
import (
"crypto/rand"
"fmt"
"github.com/kryptco/go-crypto/blake2b"
"golang.org/x/crypto/nacl/box"
)
const (
HEADER_CIPHERTEXT = iota
HEADER_WRAPPED_KEY
HEADER_WRAPPED_PUBLIC_KEY
)
func sodiumBox(m, pk, sk []byte) (c []byte, err error) {
var n [24]byte
_, err = rand.Read(n[:])
if err != nil {
return
}
if len(pk) != 32 || len(sk) != 32 {
err = fmt.Errorf("incorrect key length")
return
}
var pkArr [32]byte
copy(pkArr[:], pk)
var skArr [32]byte
copy(skArr[:], sk)
c = box.Seal(c, m, &n, &pkArr, &skArr)
c = append(n[:], c...)
return
}
func sodiumBoxOpen(nonceAndCiphertext, pk, sk []byte) (m []byte, err error) {
if len(nonceAndCiphertext) < 24 {
err = fmt.Errorf("CryptoBox nonce too small")
return
}
var n [24]byte
copy(n[:], nonceAndCiphertext[:24])
if len(pk) != 32 || len(sk) != 32 {
err = fmt.Errorf("incorrect key length")
return
}
var pkArr [32]byte
copy(pkArr[:], pk)
var skArr [32]byte
copy(skArr[:], sk)
var ret bool
c := nonceAndCiphertext[24:]
m, ret = box.Open(m, c, &n, &pkArr, &skArr)
if !ret {
err = fmt.Errorf("Open failed")
return
}
return
}
func sodiumBoxSealOpen(c, pk, sk []byte) (m []byte, err error) {
if len(c) < 32 || len(pk) != 32 || len(sk) != 32 {
err = fmt.Errorf("invalid argument passed to sodium")
return
}
var ephemeralPk [32]byte
copy(ephemeralPk[:], c[:32])
var skArr [32]byte
copy(skArr[:], sk)
noncePreimage := append(ephemeralPk[:], pk...)
n := blake2b.Sum192(noncePreimage)
m, ok := box.Open(m, c[32:], &n, &ephemeralPk, &skArr)
if !ok {
err = fmt.Errorf("verify failed")
return
}
return
}
// https://download.libsodium.org/doc/public-key_cryptography/sealed_boxes.html
func sodiumBoxSeal(m, pk []byte) (c []byte, err error) {
// protect against bindings panicking
if len(m) == 0 || len(pk) == 0 {
err = fmt.Errorf("empty argument passed to sodium")
return
}
ephemeralPk, ephemeralSk, err := box.GenerateKey(rand.Reader)
if err != nil {
return
}
noncePreimage := append(ephemeralPk[:], pk...)
n := blake2b.Sum192(noncePreimage)
var pkArr [32]byte
copy(pkArr[:], pk)
c = box.Seal(c, m, &n, &pkArr, ephemeralSk)
c = append(ephemeralPk[:], c...)
return
}
func UnwrapKey(c, pk, sk []byte) (key []byte, err error) {
key, err = sodiumBoxSealOpen(c, pk, sk)
if err != nil {
return
}
return
}
func WrapKey(pkToWrap, pk []byte) (c []byte, err error) {
encryptedKey, err := sodiumBoxSeal(pkToWrap, pk)
if err != nil {
return
}
c = append([]byte{HEADER_WRAPPED_PUBLIC_KEY}, encryptedKey...)
return
}
func GenKeyPair() (pk []byte, sk []byte, err error) {
pkArr, skArr, err := box.GenerateKey(rand.Reader)
if err != nil {
return
}
pk = pkArr[:]
sk = skArr[:]
return
}