mirror of
https://github.com/muun/recovery.git
synced 2025-11-12 14:51:37 -05:00
Update project structure and build process
This commit is contained in:
83
libwallet/aescbc/aescbc.go
Executable file
83
libwallet/aescbc/aescbc.go
Executable file
@@ -0,0 +1,83 @@
|
||||
package aescbc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const KeySize = 32
|
||||
|
||||
func EncryptPkcs7(key []byte, iv []byte, plaintext []byte) ([]byte, error) {
|
||||
plaintext = pkcs7Padding(plaintext)
|
||||
return EncryptNoPadding(key, iv, plaintext)
|
||||
}
|
||||
|
||||
func EncryptNoPadding(key []byte, iv []byte, plaintext []byte) ([]byte, error) {
|
||||
if len(key) != KeySize {
|
||||
return nil, fmt.Errorf("invalid key size, expected %v, got %v", KeySize, len(key))
|
||||
}
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ciphertext := make([]byte, len(plaintext))
|
||||
|
||||
mode := cipher.NewCBCEncrypter(block, iv)
|
||||
mode.CryptBlocks(ciphertext, plaintext)
|
||||
|
||||
return ciphertext, nil
|
||||
}
|
||||
|
||||
func DecryptPkcs7(key []byte, iv []byte, cypertext []byte) ([]byte, error) {
|
||||
paddedPlaintext, err := DecryptNoPadding(key, iv, cypertext)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pkcs7UnPadding(paddedPlaintext)
|
||||
}
|
||||
|
||||
func DecryptNoPadding(key []byte, iv []byte, cypertext []byte) ([]byte, error) {
|
||||
if len(key) != KeySize {
|
||||
return nil, fmt.Errorf("invalid key size, expected %v, got %v", KeySize, len(key))
|
||||
}
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
plaintext := make([]byte, len(cypertext))
|
||||
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
mode.CryptBlocks(plaintext, cypertext)
|
||||
|
||||
return plaintext, nil
|
||||
}
|
||||
|
||||
func pkcs7Padding(src []byte) []byte {
|
||||
padding := aes.BlockSize - len(src)%aes.BlockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(src, padtext...)
|
||||
}
|
||||
|
||||
func pkcs7UnPadding(src []byte) ([]byte, error) {
|
||||
length := len(src)
|
||||
unpadding := int(src[length-1])
|
||||
|
||||
if unpadding > aes.BlockSize || unpadding == 0 {
|
||||
return nil, errors.New("invalid pkcs7 padding (unpadding > aes.BlockSize || unpadding == 0)")
|
||||
}
|
||||
|
||||
pad := src[len(src)-unpadding:]
|
||||
for i := 0; i < unpadding; i++ {
|
||||
if pad[i] != byte(unpadding) {
|
||||
return nil, errors.New("invalid pkcs7 padding (pad[i] != unpadding)")
|
||||
}
|
||||
}
|
||||
|
||||
return src[:(length - unpadding)], nil
|
||||
}
|
||||
36
libwallet/aescbc/aescbc_test.go
Normal file
36
libwallet/aescbc/aescbc_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package aescbc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEncryptionWithPkcs7Padding(t *testing.T) {
|
||||
key := randomBytes(32)
|
||||
iv := randomBytes(16)
|
||||
|
||||
plaintext := []byte("foobar")
|
||||
ciphertext, err := EncryptPkcs7(key, iv, plaintext)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
decrypted, err := DecryptPkcs7(key, iv, ciphertext)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(decrypted, plaintext) {
|
||||
t.Fatalf("expected decrypted text to match plaintext")
|
||||
}
|
||||
}
|
||||
|
||||
func randomBytes(count int) []byte {
|
||||
buf := make([]byte, count)
|
||||
_, err := rand.Read(buf)
|
||||
if err != nil {
|
||||
panic("couldn't read random bytes")
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
||||
Reference in New Issue
Block a user