2021-07-15 19:14:14 +01:00
|
|
|
/*
|
|
|
|
|
Copyright © 2021 Sniptt <support@sniptt.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
|
|
|
|
|
|
2025-02-07 22:36:53 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2021-07-15 19:14:14 +01:00
|
|
|
|
|
|
|
|
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 encrypt
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/aes"
|
|
|
|
|
"crypto/cipher"
|
|
|
|
|
"crypto/rand"
|
2025-02-07 22:36:53 +00:00
|
|
|
"fmt"
|
2021-07-15 19:14:14 +01:00
|
|
|
)
|
|
|
|
|
|
2021-07-22 23:42:43 +01:00
|
|
|
type EncryptedBytes struct {
|
|
|
|
|
Ciphertext []byte
|
|
|
|
|
Key []byte
|
|
|
|
|
Nonce []byte
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 22:36:53 +00:00
|
|
|
// Bytes encrypts the provided data using AES-256-GCM with a randomly generated
|
|
|
|
|
// key and nonce. The nonce is prepended to the returned ciphertext.
|
2021-07-31 19:28:38 +01:00
|
|
|
func Bytes(bytes []byte) (EncryptedBytes, error) {
|
2025-02-07 22:36:53 +00:00
|
|
|
if len(bytes) == 0 {
|
|
|
|
|
return EncryptedBytes{}, fmt.Errorf("input bytes cannot be empty")
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-22 23:42:43 +01:00
|
|
|
// Key should be 16 bytes (AES-128), 24 bytes (AES-192) or 32 bytes (AES-256)
|
2021-07-31 19:28:38 +01:00
|
|
|
key := make([]byte, 32)
|
|
|
|
|
_, err := rand.Read(key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return EncryptedBytes{}, err
|
2021-07-15 19:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate a new aes cipher using the key above
|
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
|
if err != nil {
|
2021-07-22 23:42:43 +01:00
|
|
|
return EncryptedBytes{}, err
|
2021-07-15 19:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// gcm or Galois/Counter Mode, is a mode of operation
|
|
|
|
|
// for symmetric key cryptographic block ciphers
|
|
|
|
|
// - https://en.wikipedia.org/wiki/Galois/Counter_Mode
|
2021-07-22 23:42:43 +01:00
|
|
|
aesGCM, err := cipher.NewGCM(block)
|
2021-07-15 19:14:14 +01:00
|
|
|
if err != nil {
|
2021-07-22 23:42:43 +01:00
|
|
|
return EncryptedBytes{}, err
|
2021-07-15 19:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new byte array the size of the nonce,
|
|
|
|
|
// populate with a cryptographically secure random sequence
|
2021-07-22 23:42:43 +01:00
|
|
|
nonce := make([]byte, aesGCM.NonceSize())
|
2021-07-15 19:14:14 +01:00
|
|
|
_, err = rand.Read(nonce)
|
|
|
|
|
if err != nil {
|
2021-07-22 23:42:43 +01:00
|
|
|
return EncryptedBytes{}, err
|
2021-07-15 19:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Encrypt and authenticate plaintext
|
2025-02-07 22:36:53 +00:00
|
|
|
// Note: Seal() prepends the nonce to the ciphertext
|
2021-07-22 23:42:43 +01:00
|
|
|
ciphertext := aesGCM.Seal(nonce, nonce, bytes, nil)
|
2021-07-15 19:14:14 +01:00
|
|
|
|
2021-07-22 23:42:43 +01:00
|
|
|
return EncryptedBytes{ciphertext, key, nonce}, nil
|
2021-07-15 19:14:14 +01:00
|
|
|
}
|
2025-02-07 22:36:53 +00:00
|
|
|
|
|
|
|
|
// ExtractNonce returns the nonce from the ciphertext where it was prepended
|
|
|
|
|
func (e EncryptedBytes) ExtractNonce() []byte {
|
|
|
|
|
return e.Ciphertext[:len(e.Nonce)]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CiphertextWithoutNonce returns the ciphertext without the prepended nonce
|
|
|
|
|
func (e EncryptedBytes) CiphertextWithoutNonce() []byte {
|
|
|
|
|
return e.Ciphertext[len(e.Nonce):]
|
|
|
|
|
}
|