-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnigori.go
183 lines (149 loc) · 4.45 KB
/
nigori.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
// Package nigori ...
package nigori
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
)
type Type uint32
var initialVector = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
const (
NigoriKeyName = "nigori-key"
IvSize = 16
Password Type = 1
)
type Nigori interface {
Derivate(params *keyDerivationParams, password string) (err error) //nolint: misspell
Permute(t Type, name string) (string, error)
Encrypt(value string) (string, error)
Decrypt(value string) (string, error)
ExportKeys() *keys
}
// A (partial) implementation of nigori, a protocol to securely store secrets in
// the cloud. This implementation does not support server authentication or
// assisted key derivation.
//
// To store secrets securely, use the |Permute| method to derive a lookup name
// for your secret (basically a map key), and |Encrypt| and |Decrypt| to store
// and retrieve the secret.
//
// https://www.cl.cam.ac.uk/~drt24/nigori/nigori-overview.pdf
type nigori struct {
Keys *keys
}
func NewNigori() *nigori {
return &nigori{}
}
func (n *nigori) Derivate(params *keyDerivationParams, password string) (err error) { //nolint: misspell
n.Keys, err = NewKeys(params, password)
return err
}
// Derives a secure lookup name from |type| and |name|. If |hostname|,
// |username| and |password| are kept constant, a given |type| and |name| pair
// always yields the same |permuted| value. Note that |permuted| will be
// Base64 encoded.
func (n *nigori) Permute(t Type, name string) (string, error) {
encoder := base64.StdEncoding
key := n.Keys.EncryptionKey
mkey := n.Keys.MacKey
ns := NewNigoriStream(Password, NigoriKeyName)
plaintext := ns.Stream
// AES encrypt
c, err := aes.NewCipher(key)
if err != nil {
return "", err
}
encrypter := cipher.NewCBCEncrypter(c, initialVector)
ciphertext := pad(plaintext, c.BlockSize())
encrypter.CryptBlocks(ciphertext, ciphertext)
hasher := hmac.New(sha256.New, mkey)
hasher.Write(ciphertext)
hash := hasher.Sum(nil)
ciphertext = append(ciphertext, hash...)
permuted := encoder.EncodeToString(ciphertext)
return permuted, nil
}
// Encrypts |value|. Note that on success, |encrypted| will be Base64
// encoded.
func (n *nigori) Encrypt(value string) (string, error) {
key := n.Keys.EncryptionKey
mackey := n.Keys.MacKey
c, err := aes.NewCipher(key)
if err != nil {
return "", err
}
iv := make([]byte, IvSize)
if _, err := rand.Read(iv); err != nil {
return "", nil
}
encrypter := cipher.NewCBCEncrypter(c, iv)
ciphertext := pad([]byte(value), c.BlockSize())
encrypter.CryptBlocks(ciphertext, ciphertext)
hasher := hmac.New(sha256.New, mackey)
hasher.Write(ciphertext)
hash := hasher.Sum(nil)
result := make([]byte, 0, len(ciphertext)+len(iv)+len(hash))
result = append(result, iv...)
result = append(result, ciphertext...)
result = append(result, hash...)
encoder := base64.StdEncoding
encrypted := encoder.EncodeToString(result)
return encrypted, nil
}
// Decrypts |value| into |decrypted|. It is assumed that |value| is Base64
// encoded.
func (n *nigori) Decrypt(value string) (string, error) {
encoder := base64.StdEncoding
key := n.Keys.EncryptionKey
mackey := n.Keys.MacKey
input, err := encoder.DecodeString(value)
if err != nil {
return "", err
}
if len(input) < IvSize*2+hashSize {
return "", errors.New("invalid value")
}
// The input is:
// * iv (16 bytes)
// * ciphertext (multiple of 16 bytes)
// * hash (32 bytes)
iv := input[0:IvSize]
l := len(input)
ciphertext := input[IvSize : l-hashSize]
hash := input[l-hashSize:]
// hmac verify
hasher := hmac.New(sha256.New, mackey)
hasher.Write(ciphertext)
verified := hasher.Sum(nil)
if !hmac.Equal(hash, verified) {
return "", errors.New("verify failed")
}
// decrypt
c, err := aes.NewCipher(key)
if err != nil {
return "", err
}
decrypter := cipher.NewCBCDecrypter(c, iv)
plaintext := make([]byte, len(ciphertext))
decrypter.CryptBlocks(plaintext, ciphertext)
decrypted := encoder.EncodeToString(unpad(plaintext))
return decrypted, nil
}
// Exports the raw derived keys.
func (n *nigori) ExportKeys() *keys {
return n.Keys
}
func pad(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func unpad(encrypt []byte) []byte {
padding := encrypt[len(encrypt)-1]
return encrypt[:len(encrypt)-int(padding)]
}