| 1 | // Copyright 2025 The age Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package tag implements tagged P-256 or hybrid P-256 + ML-KEM-768 recipients, |
| 6 | // which can be used with identities stored on hardware keys, usually supported |
| 7 | // by dedicated plugins. |
| 8 | // |
| 9 | // The tag reduces privacy, by allowing an observer to correlate files with a |
| 10 | // recipient (but not files amongst them without knowledge of the recipient), |
| 11 | // but this is also a desirable property for hardware keys that require user |
| 12 | // interaction for each decryption operation. |
| 13 | package tag |
| 14 | |
| 15 | import ( |
| 16 | "crypto/ecdh" |
| 17 | "crypto/hkdf" |
| 18 | "crypto/mlkem" |
| 19 | "crypto/sha256" |
| 20 | "fmt" |
| 21 | "slices" |
| 22 | |
| 23 | "filippo.io/age" |
| 24 | "filippo.io/age/internal/format" |
| 25 | "filippo.io/age/plugin" |
| 26 | "filippo.io/hpke" |
| 27 | "filippo.io/nistec" |
| 28 | ) |
| 29 | |
| 30 | // Recipient is a tagged P-256 or hybrid P-256 + ML-KEM-768 recipient. |
| 31 | // |
| 32 | // The latter recipient is safe against future cryptographically-relevant |
| 33 | // quantum computers, and can only be used along with other post-quantum |
| 34 | // recipients. |
| 35 | type Recipient struct { |
| 36 | pk hpke.PublicKey |
| 37 | } |
| 38 | |
| 39 | var _ age.Recipient = &Recipient{} |
| 40 | |
| 41 | // ParseRecipient returns a new [Recipient] from a Bech32 public key |
| 42 | // encoding with the "age1tag1" or "age1tagpq1" prefix. |
| 43 | func ParseRecipient(s string) (*Recipient, error) { |
| 44 | t, k, err := plugin.ParseRecipient(s) |
| 45 | if err != nil { |
| 46 | return nil, fmt.Errorf("malformed recipient %q: %v", s, err) |
| 47 | } |
| 48 | switch t { |
| 49 | case "tag": |
| 50 | r, err := NewClassicRecipient(k) |
| 51 | if err != nil { |
| 52 | return nil, fmt.Errorf("malformed recipient %q: %v", s, err) |
| 53 | } |
| 54 | return r, nil |
| 55 | case "tagpq": |
| 56 | r, err := NewHybridRecipient(k) |
| 57 | if err != nil { |
| 58 | return nil, fmt.Errorf("malformed recipient %q: %v", s, err) |
| 59 | } |
| 60 | return r, nil |
| 61 | default: |
| 62 | return nil, fmt.Errorf("malformed recipient %q: invalid type %q", s, t) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | const compressedPointSize = 1 + 32 |
| 67 | const uncompressedPointSize = 1 + 32 + 32 |
| 68 | |
| 69 | // NewClassicRecipient returns a new P-256 [Recipient] from a raw public key. |
| 70 | func NewClassicRecipient(publicKey []byte) (*Recipient, error) { |
| 71 | if len(publicKey) != compressedPointSize { |
| 72 | return nil, fmt.Errorf("invalid tag recipient public key size %d", len(publicKey)) |
| 73 | } |
| 74 | p, err := nistec.NewP256Point().SetBytes(publicKey) |
| 75 | if err != nil { |
| 76 | return nil, fmt.Errorf("invalid tag recipient public key: %v", err) |
| 77 | } |
| 78 | k, err := hpke.DHKEM(ecdh.P256()).NewPublicKey(p.Bytes()) |
| 79 | if err != nil { |
| 80 | return nil, fmt.Errorf("invalid tag recipient public key: %v", err) |
| 81 | } |
| 82 | return &Recipient{k}, nil |
| 83 | } |
| 84 | |
| 85 | // NewHybridRecipient returns a new hybrid P-256 + ML-KEM-768 [Recipient] from |
| 86 | // raw concatenated public keys. |
| 87 | func NewHybridRecipient(publicKey []byte) (*Recipient, error) { |
| 88 | k, err := hpke.MLKEM768P256().NewPublicKey(publicKey) |
| 89 | if err != nil { |
| 90 | return nil, fmt.Errorf("invalid tagpq recipient public key: %v", err) |
| 91 | } |
| 92 | return &Recipient{k}, nil |
| 93 | } |
| 94 | |
| 95 | // Hybrid reports whether r is a hybrid P-256 + ML-KEM-768 recipient. |
| 96 | func (r *Recipient) Hybrid() bool { |
| 97 | return r.pk.KEM().ID() == hpke.MLKEM768P256().ID() |
| 98 | } |
| 99 | |
| 100 | func (r *Recipient) Wrap(fileKey []byte) ([]*age.Stanza, error) { |
| 101 | s, _, err := r.WrapWithLabels(fileKey) |
| 102 | return s, err |
| 103 | } |
| 104 | |
| 105 | // Tag computes the 4-byte tag for the given ciphertext enc. |
| 106 | // |
| 107 | // This is a low-level method exposed for use by plugins that implement |
| 108 | // identities compatible with tagged recipients. |
| 109 | func (r *Recipient) Tag(enc []byte) ([]byte, error) { |
| 110 | label, tagRecipient := "age-encryption.org/p256tag", r.Bytes() |
| 111 | if r.Hybrid() { |
| 112 | label = "age-encryption.org/mlkem768p256tag" |
| 113 | // In hybrid mode, the tag is computed over just the P-256 part. |
| 114 | tagRecipient = tagRecipient[mlkem.EncapsulationKeySize768:] |
| 115 | if len(enc) != mlkem.CiphertextSize768+uncompressedPointSize { |
| 116 | return nil, fmt.Errorf("invalid ciphertext size") |
| 117 | } |
| 118 | } else if len(enc) != uncompressedPointSize { |
| 119 | return nil, fmt.Errorf("invalid ciphertext size") |
| 120 | } |
| 121 | rh := sha256.Sum256(tagRecipient) |
| 122 | tag, err := hkdf.Extract(sha256.New, append(slices.Clip(enc), rh[:4]...), []byte(label)) |
| 123 | if err != nil { |
| 124 | return nil, fmt.Errorf("failed to compute tag: %v", err) |
| 125 | } |
| 126 | return tag[:4], nil |
| 127 | } |
| 128 | |
| 129 | // WrapWithLabels implements [age.RecipientWithLabels], returning a single |
| 130 | // "postquantum" label if r is a hybrid P-256 + ML-KEM-768 recipient. This |
| 131 | // ensures a hybrid Recipient can't be mixed with other recipients that would |
| 132 | // defeat its post-quantum security. |
| 133 | // |
| 134 | // To unsafely bypass this restriction, wrap Recipient in an [age.Recipient] |
| 135 | // type that doesn't expose WrapWithLabels. |
| 136 | func (r *Recipient) WrapWithLabels(fileKey []byte) ([]*age.Stanza, []string, error) { |
| 137 | label, arg := "age-encryption.org/p256tag", "p256tag" |
| 138 | if r.Hybrid() { |
| 139 | label, arg = "age-encryption.org/mlkem768p256tag", "mlkem768p256tag" |
| 140 | } |
| 141 | |
| 142 | enc, s, err := hpke.NewSender(r.pk, hpke.HKDFSHA256(), hpke.ChaCha20Poly1305(), []byte(label)) |
| 143 | if err != nil { |
| 144 | return nil, nil, fmt.Errorf("failed to set up HPKE sender: %v", err) |
| 145 | } |
| 146 | ct, err := s.Seal(nil, fileKey) |
| 147 | if err != nil { |
| 148 | return nil, nil, fmt.Errorf("failed to encrypt file key: %v", err) |
| 149 | } |
| 150 | |
| 151 | tag, err := r.Tag(enc) |
| 152 | if err != nil { |
| 153 | return nil, nil, fmt.Errorf("failed to compute tag: %v", err) |
| 154 | } |
| 155 | |
| 156 | l := &age.Stanza{ |
| 157 | Type: arg, |
| 158 | Args: []string{ |
| 159 | format.EncodeToString(tag[:4]), |
| 160 | format.EncodeToString(enc), |
| 161 | }, |
| 162 | Body: ct, |
| 163 | } |
| 164 | |
| 165 | if r.Hybrid() { |
| 166 | return []*age.Stanza{l}, []string{"postquantum"}, nil |
| 167 | } |
| 168 | return []*age.Stanza{l}, nil, nil |
| 169 | } |
| 170 | |
| 171 | // Bytes returns the raw recipient encoding. |
| 172 | func (r *Recipient) Bytes() []byte { |
| 173 | if r.Hybrid() { |
| 174 | return r.pk.Bytes() |
| 175 | } |
| 176 | p, err := nistec.NewP256Point().SetBytes(r.pk.Bytes()) |
| 177 | if err != nil { |
| 178 | panic("internal error: invalid P-256 public key") |
| 179 | } |
| 180 | return p.BytesCompressed() |
| 181 | } |
| 182 | |
| 183 | // String returns the Bech32 public key encoding of r. |
| 184 | func (r *Recipient) String() string { |
| 185 | if r.Hybrid() { |
| 186 | return plugin.EncodeRecipient("tagpq", r.Bytes()) |
| 187 | } |
| 188 | return plugin.EncodeRecipient("tag", r.Bytes()) |
| 189 | } |