croc/src/crypt/crypt.go

126 lines
3.4 KiB
Go
Raw Normal View History

2018-09-22 05:17:57 +02:00
package crypt
import (
2023-10-05 17:30:56 +02:00
"crypto/aes"
2018-09-22 05:17:57 +02:00
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
2019-11-18 16:50:43 +01:00
"fmt"
2020-08-23 01:05:00 +02:00
"log"
2018-09-22 05:17:57 +02:00
2021-04-22 01:53:29 +02:00
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/chacha20poly1305"
2018-09-22 05:17:57 +02:00
"golang.org/x/crypto/pbkdf2"
)
2019-11-18 16:50:43 +01:00
// New generates a new key based on a passphrase and salt
func New(passphrase []byte, usersalt []byte) (key []byte, salt []byte, err error) {
if len(passphrase) < 1 {
err = fmt.Errorf("need more than that for passphrase")
2019-04-30 04:19:25 +02:00
return
}
2019-11-18 16:50:43 +01:00
if usersalt == nil {
salt = make([]byte, 8)
// http://www.ietf.org/rfc/rfc2898.txt
// Salt.
2020-08-23 01:05:00 +02:00
if _, err := rand.Read(salt); err != nil {
log.Fatalf("can't get random salt: %v", err)
}
} else {
2019-11-18 16:50:43 +01:00
salt = usersalt
}
2020-08-23 01:05:00 +02:00
key = pbkdf2.Key(passphrase, salt, 100, 32, sha256.New)
return
}
2019-11-18 16:50:43 +01:00
// Encrypt will encrypt using the pre-generated key
func Encrypt(plaintext []byte, key []byte) (encrypted []byte, err error) {
2023-10-05 17:30:56 +02:00
// generate a random iv each time
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
// Section 8.2
ivBytes := make([]byte, 12)
if _, err = rand.Read(ivBytes); err != nil {
log.Fatalf("can't initialize crypto: %v", err)
}
b, err := aes.NewCipher(key)
if err != nil {
return
}
aesgcm, err := cipher.NewGCM(b)
if err != nil {
return
}
encrypted = aesgcm.Seal(nil, ivBytes, plaintext, nil)
encrypted = append(ivBytes, encrypted...)
2019-04-28 02:03:05 +02:00
return
2018-09-22 05:17:57 +02:00
}
2019-11-18 16:50:43 +01:00
// Decrypt using the pre-generated key
func Decrypt(encrypted []byte, key []byte) (plaintext []byte, err error) {
2023-10-05 17:30:56 +02:00
if len(encrypted) < 13 {
err = fmt.Errorf("incorrect passphrase")
return
}
b, err := aes.NewCipher(key)
if err != nil {
return
}
aesgcm, err := cipher.NewGCM(b)
if err != nil {
return
}
plaintext, err = aesgcm.Open(nil, encrypted[:12], encrypted[12:], nil)
2018-09-22 05:17:57 +02:00
return
}
2021-04-22 01:53:29 +02:00
// NewArgon2 generates a new key based on a passphrase and salt
// using argon2
// https://pkg.go.dev/golang.org/x/crypto/argon2
2021-04-22 01:57:18 +02:00
func NewArgon2(passphrase []byte, usersalt []byte) (aead cipher.AEAD, salt []byte, err error) {
2021-04-22 01:53:29 +02:00
if len(passphrase) < 1 {
err = fmt.Errorf("need more than that for passphrase")
return
}
if usersalt == nil {
salt = make([]byte, 8)
// http://www.ietf.org/rfc/rfc2898.txt
// Salt.
2022-12-05 20:21:04 +01:00
if _, err = rand.Read(salt); err != nil {
2021-04-22 01:53:29 +02:00
log.Fatalf("can't get random salt: %v", err)
}
} else {
salt = usersalt
}
2021-04-22 01:57:18 +02:00
aead, err = chacha20poly1305.NewX(argon2.IDKey(passphrase, salt, 1, 64*1024, 4, 32))
2021-04-22 01:53:29 +02:00
return
}
// EncryptChaCha will encrypt ChaCha20-Poly1305 using the pre-generated key
// https://pkg.go.dev/golang.org/x/crypto/chacha20poly1305
2021-04-22 01:57:18 +02:00
func EncryptChaCha(plaintext []byte, aead cipher.AEAD) (encrypted []byte, err error) {
2021-04-22 01:53:29 +02:00
nonce := make([]byte, aead.NonceSize(), aead.NonceSize()+len(plaintext)+aead.Overhead())
if _, err := rand.Read(nonce); err != nil {
panic(err)
}
// Encrypt the message and append the ciphertext to the nonce.
encrypted = aead.Seal(nonce, nonce, plaintext, nil)
return
}
// DecryptChaCha will encrypt ChaCha20-Poly1305 using the pre-generated key
// https://pkg.go.dev/golang.org/x/crypto/chacha20poly1305
2021-04-22 01:57:18 +02:00
func DecryptChaCha(encryptedMsg []byte, aead cipher.AEAD) (encrypted []byte, err error) {
2021-04-22 01:53:29 +02:00
if len(encryptedMsg) < aead.NonceSize() {
err = fmt.Errorf("ciphertext too short")
return
}
// Split nonce and ciphertext.
nonce, ciphertext := encryptedMsg[:aead.NonceSize()], encryptedMsg[aead.NonceSize():]
// Decrypt the message and check it wasn't tampered with.
encrypted, err = aead.Open(nil, nonce, ciphertext, nil)
return
}