Repository navigation
Expand file tree
/
Copy pathaead_chacha20.go
More file actions
105 lines (89 loc) · 3.23 KB
/
Copy pathaead_chacha20.go
File metadata and controls
105 lines (89 loc) · 3.23 KB
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
package crypt
import (
"crypto/cipher"
"encoding/base64"
"fmt"
"io"
"golang.org/x/crypto/chacha20poly1305"
)
// ChaCha20-Poly1305 AEAD — alternative to AES-256-GCM for hardware
// without AES-NI (older ARM, embedded devices). Same wire format
// shape as v1, with version byte 0x02:
//
// [0x02][nonce:12][ciphertext:N][tag:16]
//
// Use it when:
// - Target hardware lacks AES-NI (you'll see worse AES-GCM
// performance vs ChaCha20).
// - You want defense-in-depth diversity (different cipher family).
//
// Otherwise, prefer Seal (AES-256-GCM) — it's the default because
// AES-NI is ubiquitous on modern x86_64 and ARMv8.
// SealChaCha20 encrypts plaintext under a 32-byte ChaCha20-Poly1305
// key with optional AAD and returns base64url-no-pad ciphertext
// tagged with version byte 0x02.
func SealChaCha20(key, plaintext, aad []byte) (string, error) {
return sealChaCha20WithNonce(key, plaintext, aad, nil)
}
// OpenChaCha20 decrypts a v2 ciphertext (ChaCha20-Poly1305).
//
// Errors:
// - ErrInvalidKey if key length != 32.
// - ErrInvalidCiphertext on bad base64url.
// - ErrCiphertextTooShort if too small.
// - ErrUnsupportedVersion if version byte != 0x02.
// - ErrTampered on auth failure.
func OpenChaCha20(key []byte, ciphertext string, aad []byte) ([]byte, error) {
if len(key) != AEADKeySize {
return nil, fmt.Errorf("%w: ChaCha20 requires %d bytes; got %d", ErrInvalidKey, AEADKeySize, len(key))
}
raw, err := base64.RawURLEncoding.DecodeString(ciphertext)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInvalidCiphertext, err)
}
if len(raw) < aeadMinSize {
return nil, ErrCiphertextTooShort
}
if raw[0] != VersionAEADv2 {
return nil, fmt.Errorf("%w: 0x%02x", ErrUnsupportedVersion, raw[0])
}
c, err := chacha20poly1305.New(key)
if err != nil {
return nil, fmt.Errorf("crypt: chacha20poly1305.New: %w", err)
}
nonce := raw[1:aeadHeaderSize]
body := raw[aeadHeaderSize:]
plaintext, err := c.Open(nil, nonce, body, aad)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrTampered, err)
}
return plaintext, nil
}
// sealChaCha20WithNonce is the test-injectable form. nil nonce →
// random; non-nil must be 12 bytes.
func sealChaCha20WithNonce(key, plaintext, aad, nonce []byte) (string, error) {
if len(key) != AEADKeySize {
return "", fmt.Errorf("%w: ChaCha20 requires %d bytes; got %d", ErrInvalidKey, AEADKeySize, len(key))
}
c, err := chacha20poly1305.New(key)
if err != nil {
return "", fmt.Errorf("crypt: chacha20poly1305.New: %w", err)
}
if nonce == nil {
nonce = make([]byte, aeadNonceSize)
if _, err := io.ReadFull(randReader, nonce); err != nil {
return "", fmt.Errorf("crypt: random nonce: %w", err)
}
} else if len(nonce) != aeadNonceSize {
return "", fmt.Errorf("crypt: nonce must be %d bytes; got %d", aeadNonceSize, len(nonce))
}
out := make([]byte, 0, aeadHeaderSize+len(plaintext)+aeadTagSize)
out = append(out, VersionAEADv2)
out = append(out, nonce...)
out = c.Seal(out, nonce, plaintext, aad)
return base64.RawURLEncoding.EncodeToString(out), nil
}
// cipher.AEAD interface check — ensures std lib drift is caught at
// compile time if x/crypto changes.
var _ cipher.AEAD = (*chacha20poly1305Wrapper)(nil)
type chacha20poly1305Wrapper struct{ cipher.AEAD }