-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto.go
98 lines (88 loc) · 2.69 KB
/
crypto.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
/* ****************************************************************************
* Copyright 2020 51 Degrees Mobile Experts Limited (51degrees.com)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
* ***************************************************************************/
package swift
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
)
// crypto structure containing AES ciphers.
type crypto struct {
gcm cipher.AEAD
}
// newCrypto creates a new instance of the security structure used to encrypt
// and decrypt data using rotating shared secret keys.
func newCrypto(key []byte) (*crypto, error) {
var x crypto
i, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
x.gcm, err = cipher.NewGCM(i)
if err != nil {
return nil, err
}
return &x, nil
}
// decrypt the byte array b returning the decrypted byte array.
//
// b the byte array previous generated via the encrypt method.
func (x *crypto) decrypt(b []byte) ([]byte, error) {
nonceSize := x.gcm.NonceSize()
if len(b) < nonceSize {
return nil, fmt.Errorf(
"data length '%d' shorter than nonce '%d'",
len(b),
nonceSize)
}
nonce, c := b[:nonceSize], b[nonceSize:]
d, err := x.gcm.Open(nil, nonce, c, nil)
if err != nil {
return nil, err
}
return d, err
}
// encryptWithNonce encrypts the byte array b with the nounce provided n.
//
// b the byte array to be encrypted.
//
// n the nonce to use for encryption.
func (x *crypto) encryptWithNonce(b []byte, n []byte) []byte {
// Seal encrypts and authenticates plaintext, authenticates the
// additional data and appends the result to dst, returning the updated
// slice. The nonce must be NonceSize() bytes long and unique for all
// time, for a given key.
return x.gcm.Seal(n, n, b, nil)
}
// encrypt the byte array b with random nonce.
//
// b the byte array to be encrypted.
func (x *crypto) encrypt(b []byte) ([]byte, error) {
// Create nonce with a cryptographically secure random sequence. Nonce
// should never be repeated.
n, err := randomBytes(x.gcm.NonceSize())
if err != nil {
return nil, err
}
return x.encryptWithNonce(b, n), nil
}
func randomBytes(l int) ([]byte, error) {
r := make([]byte, l)
_, err := io.ReadFull(rand.Reader, r)
return r, err
}