-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.go
53 lines (42 loc) · 1.44 KB
/
aes.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
// SPDX-FileCopyrightText: 2023-2024 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package openpgp
import (
"crypto/aes"
"cunicu.li/go-iso7816"
)
// ICV is the Initial Chaining Value used by OpenPGP cards for symmetric encryption using AES-CBC
//
//nolint:gochecknoglobals
var ICV = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
type BlockCipher struct {
card *Card
}
// BlockSize returns the cipher's block size.
func (k BlockCipher) BlockSize() int {
return aes.BlockSize
}
// Encrypt encrypts the provided plaintext using AES in Cipher Block Chaining (CBC) mode
// using an Initial Chaining Value (ICV) of zero bytes ([IV]).
//
// See: OpenPGP Smart Card Application - Section 7.2.12 PSO: ENCIPHER
func (k *BlockCipher) Encrypt(pt []byte) ([]byte, error) {
if len(pt)%aes.BlockSize != 0 {
return nil, ErrInvalidLength
}
resp, err := send(k.card.tx, iso7816.InsPerformSecurityOperation, 0x86, 0x80, pt)
if err != nil {
return nil, err
}
return resp[1:], err
}
// Decrypt decrypts the provided ciphertext using AES in Cipher Block Chaining (CBC) mode
// using an Initial Chaining Value (ICV) of zero bytes.
//
// See: OpenPGP Smart Card Application - Section 7.2.11 PSO: DECIPHER
func (k *BlockCipher) Decrypt(ct []byte) ([]byte, error) {
if len(ct)%aes.BlockSize != 0 {
return nil, ErrInvalidLength
}
return send(k.card.tx, iso7816.InsPerformSecurityOperation, 0x80, 0x86, append([]byte{0x02}, ct...))
}