mirror of
https://github.com/FiloSottile/age.git
synced 2025-12-23 05:25:14 +00:00
It's possible to craft ChaCha20Poly1305 ciphertexts that decrypt under multiple keys. (I know, it's wild.) The impact is different for different recipients, but in general only applies to Chosen Ciphertext Attacks against online decryption oracles: * With the scrypt recipient, it lets the attacker make a recipient stanza that decrypts with multiple passwords, speeding up a bruteforce in terms of oracle queries (but not scrypt work, which can be precomputed) to logN by binary search. Limiting the ciphertext size limits the keys to two, which makes this acceptable: it's a loss of only one bit of security in a scenario (online decryption oracles) that is not recommended. * With the X25519 recipient, it lets the attacker search for accepted public keys without using multiple recipient stanzas in the message. That lets the attacker bypass the 20 recipients limit (which was not actually intended to defend against deanonymization attacks). This is not really in the threat model for age: we make no attempt to provide anonymity in an online CCA scenario. Anyway, limiting the keys to two by enforcing short ciphertexts mitigates the attack: it only lets the attacker test 40 keys per message instead of 20. * With the ssh-ed25519 recipient, the attack should be irrelevant, since the recipient stanza includes a 32-bit hash of the public key, making it decidedly not anonymous. Also to avoid breaking the abstraction in the agessh package, we don't mitigate the attack for this recipient, but we document the lack of anonymity. This was reported by Paul Grubbs in the context of the upcoming paper "Partitioning Oracle Attacks", USENIX Security 2021 (to appear), by Julia Len, Paul Grubbs, and Thomas Ristenpart.
172 lines
5.0 KiB
Go
172 lines
5.0 KiB
Go
// Copyright 2019 Google LLC
|
|
//
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file or at
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
package age
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"filippo.io/age/internal/format"
|
|
"golang.org/x/crypto/chacha20poly1305"
|
|
"golang.org/x/crypto/scrypt"
|
|
)
|
|
|
|
const scryptLabel = "age-encryption.org/v1/scrypt"
|
|
|
|
// ScryptRecipient is a password-based recipient. Anyone with the password can
|
|
// decrypt the message.
|
|
//
|
|
// If a ScryptRecipient is used, it must be the only recipient for the file: it
|
|
// can't be mixed with other recipient types and can't be used multiple times
|
|
// for the same file.
|
|
//
|
|
// Its use is not recommended for automated systems, which should prefer
|
|
// X25519Recipient.
|
|
type ScryptRecipient struct {
|
|
password []byte
|
|
workFactor int
|
|
}
|
|
|
|
var _ Recipient = &ScryptRecipient{}
|
|
|
|
func (*ScryptRecipient) Type() string { return "scrypt" }
|
|
|
|
// NewScryptRecipient returns a new ScryptRecipient with the provided password.
|
|
func NewScryptRecipient(password string) (*ScryptRecipient, error) {
|
|
if len(password) == 0 {
|
|
return nil, errors.New("passphrase can't be empty")
|
|
}
|
|
r := &ScryptRecipient{
|
|
password: []byte(password),
|
|
// TODO: automatically scale this to 1s (with a min) in the CLI.
|
|
workFactor: 18, // 1s on a modern machine
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
// SetWorkFactor sets the scrypt work factor to 2^logN.
|
|
// It must be called before Wrap.
|
|
//
|
|
// If SetWorkFactor is not called, a reasonable default is used.
|
|
func (r *ScryptRecipient) SetWorkFactor(logN int) {
|
|
if logN > 30 || logN < 1 {
|
|
panic("age: SetWorkFactor called with illegal value")
|
|
}
|
|
r.workFactor = logN
|
|
}
|
|
|
|
const scryptSaltSize = 16
|
|
|
|
func (r *ScryptRecipient) Wrap(fileKey []byte) (*Stanza, error) {
|
|
salt := make([]byte, scryptSaltSize)
|
|
if _, err := rand.Read(salt[:]); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
logN := r.workFactor
|
|
l := &Stanza{
|
|
Type: "scrypt",
|
|
Args: []string{format.EncodeToString(salt), strconv.Itoa(logN)},
|
|
}
|
|
|
|
salt = append([]byte(scryptLabel), salt...)
|
|
k, err := scrypt.Key(r.password, salt, 1<<logN, 8, 1, chacha20poly1305.KeySize)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to generate scrypt hash: %v", err)
|
|
}
|
|
|
|
wrappedKey, err := aeadEncrypt(k, fileKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
l.Body = wrappedKey
|
|
|
|
return l, nil
|
|
}
|
|
|
|
// ScryptIdentity is a password-based identity.
|
|
type ScryptIdentity struct {
|
|
password []byte
|
|
maxWorkFactor int
|
|
}
|
|
|
|
var _ Identity = &ScryptIdentity{}
|
|
|
|
func (*ScryptIdentity) Type() string { return "scrypt" }
|
|
|
|
// NewScryptIdentity returns a new ScryptIdentity with the provided password.
|
|
func NewScryptIdentity(password string) (*ScryptIdentity, error) {
|
|
if len(password) == 0 {
|
|
return nil, errors.New("passphrase can't be empty")
|
|
}
|
|
i := &ScryptIdentity{
|
|
password: []byte(password),
|
|
maxWorkFactor: 22, // 15s on a modern machine
|
|
}
|
|
return i, nil
|
|
}
|
|
|
|
// SetMaxWorkFactor sets the maximum accepted scrypt work factor to 2^logN.
|
|
// It must be called before Unwrap.
|
|
//
|
|
// This caps the amount of work that Decrypt might have to do to process
|
|
// received files. If SetMaxWorkFactor is not called, a fairly high default is
|
|
// used, which might not be suitable for systems processing untrusted files.
|
|
func (i *ScryptIdentity) SetMaxWorkFactor(logN int) {
|
|
if logN > 30 || logN < 1 {
|
|
panic("age: SetMaxWorkFactor called with illegal value")
|
|
}
|
|
i.maxWorkFactor = logN
|
|
}
|
|
|
|
func (i *ScryptIdentity) Unwrap(block *Stanza) ([]byte, error) {
|
|
if block.Type != "scrypt" {
|
|
return nil, ErrIncorrectIdentity
|
|
}
|
|
if len(block.Args) != 2 {
|
|
return nil, errors.New("invalid scrypt recipient block")
|
|
}
|
|
salt, err := format.DecodeString(block.Args[0])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse scrypt salt: %v", err)
|
|
}
|
|
if len(salt) != scryptSaltSize {
|
|
return nil, errors.New("invalid scrypt recipient block")
|
|
}
|
|
logN, err := strconv.Atoi(block.Args[1])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse scrypt work factor: %v", err)
|
|
}
|
|
if logN > i.maxWorkFactor {
|
|
return nil, fmt.Errorf("scrypt work factor too large: %v", logN)
|
|
}
|
|
if logN <= 0 {
|
|
return nil, fmt.Errorf("invalid scrypt work factor: %v", logN)
|
|
}
|
|
|
|
salt = append([]byte(scryptLabel), salt...)
|
|
k, err := scrypt.Key(i.password, salt, 1<<logN, 8, 1, chacha20poly1305.KeySize)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to generate scrypt hash: %v", err)
|
|
}
|
|
|
|
// This AEAD is not robust, so an attacker could craft a message that
|
|
// decrypts under two different keys (meaning two different passphrases) and
|
|
// then use an error side-channel in an online decryption oracle to learn if
|
|
// either key is correct. This is deemed acceptable because the usa case (an
|
|
// online decryption oracle) is not recommended, and the security loss is
|
|
// only one bit. This also does not bypass any scrypt work, but that work
|
|
// can be precomputed in an online oracle scenario.
|
|
fileKey, err := aeadDecrypt(k, fileKeySize, block.Body)
|
|
if err != nil {
|
|
return nil, ErrIncorrectIdentity
|
|
}
|
|
return fileKey, nil
|
|
}
|