summaryrefslogtreecommitdiff
path: root/pkg/scrypto/aes256.go
blob: 27a39d6ae0e607b97ebbcc6174496350d04701d3 (plain)
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
package scrypto

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"fmt"
)

type AES256Key [32]byte

const (
	AES256IVSize int = aes.BlockSize
)

var Zero = AES256Key{}

func (key *AES256Key) Bytes() []byte {
	return key[:]
}

func (key *AES256Key) IsZero() bool {
	return bytes.Equal(key.Bytes(), Zero.Bytes())
}

func (key *AES256Key) Hex() string {
	return hex.EncodeToString(key[:])
}

func (key *AES256Key) FromHex(s string) error {
	data, err := hex.DecodeString(s)
	if err != nil {
		return err
	}

	if len(data) != 32 {
		return fmt.Errorf("invalid key size")
	}

	copy((*key)[:], data[:32])

	return nil
}

func (key *AES256Key) FromBase64(s string) error {
	data, err := base64.StdEncoding.DecodeString(s)
	if err != nil {
		return err
	}

	if len(data) != 32 {
		return fmt.Errorf("invalid key size")
	}

	copy((*key)[:], data[:32])

	return nil
}

func (key *AES256Key) MarshalJSON() ([]byte, error) {
	s := base64.StdEncoding.EncodeToString(key[:])
	return json.Marshal(s)
}

func (pkey *AES256Key) UnmarshalJSON(data []byte) error {
	var s string
	if err := json.Unmarshal(data, &s); err != nil {
		return err
	}

	key, err := base64.StdEncoding.DecodeString(s)
	if err != nil {
		return fmt.Errorf("cannot decode base64 value: %w", err)
	}

	if len(key) != 32 {
		return fmt.Errorf("invalid key size (must be 32 bytes long)")
	}

	copy(pkey[:], key)

	return nil
}

func (key *AES256Key) EncryptAES256(inputData []byte) ([]byte, error) {
	blockCipher, err := aes.NewCipher(key[:])
	if err != nil {
		return nil, fmt.Errorf("cannot create cipher: %w", err)
	}

	paddedData := PadPKCS5(inputData, aes.BlockSize)

	outputData := make([]byte, AES256IVSize+len(paddedData))

	iv := outputData[:AES256IVSize]
	encryptedData := outputData[AES256IVSize:]

	if _, err := rand.Read(iv); err != nil {
		return nil, fmt.Errorf("cannot generate iv: %w", err)
	}

	encrypter := cipher.NewCBCEncrypter(blockCipher, iv)
	encrypter.CryptBlocks(encryptedData, paddedData)

	return outputData, nil
}

func (key *AES256Key) DecryptAES256(inputData []byte) ([]byte, error) {
	blockCipher, err := aes.NewCipher(key[:])
	if err != nil {
		return nil, fmt.Errorf("cannot create cipher: %w", err)
	}

	if len(inputData) < AES256IVSize {
		return nil, fmt.Errorf("truncated data")
	}

	iv := inputData[:AES256IVSize]
	paddedData := inputData[AES256IVSize:]

	if len(paddedData)%aes.BlockSize != 0 {
		return nil, fmt.Errorf("invalid padded data length")
	}

	decrypter := cipher.NewCBCDecrypter(blockCipher, iv)
	decrypter.CryptBlocks(paddedData, paddedData)

	outputData, err := UnpadPKCS5(paddedData, aes.BlockSize)
	if err != nil {
		return nil, fmt.Errorf("invalid padded data: %w", err)
	}

	return outputData, nil
}