-
Notifications
You must be signed in to change notification settings - Fork 21
/
secretbox.go
128 lines (109 loc) · 2.95 KB
/
secretbox.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
package sodium
// #cgo pkg-config: libsodium
// #include <stdlib.h>
// #include <sodium.h>
import "C"
var (
cryptoSecretBoxKeyBytes = int(C.crypto_secretbox_keybytes())
cryptoSecretBoxNonceBytes = int(C.crypto_secretbox_noncebytes())
cryptoSecretBoxMacBytes = int(C.crypto_secretbox_macbytes())
)
type SecretBoxKey struct {
Bytes
}
func (s SecretBoxKey) Size() int {
return cryptoSecretBoxKeyBytes
}
type SecretBoxNonce struct {
Bytes
}
func (n SecretBoxNonce) Size() int {
return cryptoSecretBoxNonceBytes
}
func (n *SecretBoxNonce) Next() {
C.sodium_increment((*C.uchar)(&n.Bytes[0]), (C.size_t)(cryptoSecretBoxNonceBytes))
}
type SecretBoxMAC struct {
Bytes
}
func (s SecretBoxMAC) Size() int {
return cryptoSecretBoxMacBytes
}
//SecretBox use a SecretBoxNonce and a SecretBoxKey to encrypt a message.
func (b Bytes) SecretBox(n SecretBoxNonce, k SecretBoxKey) (c Bytes) {
checkTypedSize(&n, "nonce")
checkTypedSize(&k, "secret key")
bp, bl := plen(b)
c = make([]byte, bl+cryptoSecretBoxMacBytes)
if int(C.crypto_secretbox_easy(
(*C.uchar)(&c[0]),
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(&n.Bytes[0]),
(*C.uchar)(&k.Bytes[0]))) != 0 {
panic("see libsodium")
}
return
}
//SecretBoxOpen opens a SecretBox using SecretBoxKey and SecretBoxNonce.
//
//It returns an error if opening failed.
func (b Bytes) SecretBoxOpen(n SecretBoxNonce, k SecretBoxKey) (m Bytes, err error) {
checkTypedSize(&n, "nonce")
checkTypedSize(&k, "secret key")
bp, bl := plen(b)
m = make([]byte, bl-cryptoSecretBoxMacBytes)
mp, _ := plen(m)
if int(C.crypto_secretbox_open_easy(
(*C.uchar)(mp),
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(&n.Bytes[0]),
(*C.uchar)(&k.Bytes[0]))) != 0 {
err = ErrOpenBox
}
return
}
//SecretBoxDetached use a SecretBoxNonce and a SecretBoxKey to encrypt a message.
//A separate MAC is returned.
func (b Bytes) SecretBoxDetached(n SecretBoxNonce, k SecretBoxKey) (c Bytes, mac SecretBoxMAC) {
checkTypedSize(&n, "nonce")
checkTypedSize(&k, "secret key")
bp, bl := plen(b)
c = make([]byte, bl)
cp, _ := plen(c)
macb := make([]byte, cryptoSecretBoxMacBytes)
if int(C.crypto_secretbox_detached(
(*C.uchar)(cp),
(*C.uchar)(&macb[0]),
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(&n.Bytes[0]),
(*C.uchar)(&k.Bytes[0]))) != 0 {
panic("see libsodium")
}
mac = SecretBoxMAC{macb}
return
}
//SecretBoxOpenDetached opens a SecretBox using SecretBoxKey and SecretBoxNonce.
//with a separate MAC.
//
//It returns an error if opening failed.
func (b Bytes) SecretBoxOpenDetached(mac SecretBoxMAC, n SecretBoxNonce, k SecretBoxKey) (m Bytes, err error) {
checkTypedSize(&mac, "mac")
checkTypedSize(&n, "nonce")
checkTypedSize(&k, "key")
bp, bl := plen(b)
m = make([]byte, bl)
mp, _ := plen(m)
if int(C.crypto_secretbox_open_detached(
(*C.uchar)(mp),
(*C.uchar)(bp),
(*C.uchar)(&mac.Bytes[0]),
(C.ulonglong)(bl),
(*C.uchar)(&n.Bytes[0]),
(*C.uchar)(&k.Bytes[0]))) != 0 {
err = ErrOpenBox
}
return
}