| 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 age |
| 6 | |
| 7 | import ( |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "strings" |
| 11 | |
| 12 | "filippo.io/age/internal/bech32" |
| 13 | "filippo.io/age/internal/format" |
| 14 | "filippo.io/hpke" |
| 15 | "golang.org/x/crypto/chacha20poly1305" |
| 16 | ) |
| 17 | |
| 18 | const pqLabel = "age-encryption.org/mlkem768x25519" |
| 19 | |
| 20 | // HybridRecipient is the standard age public key. Messages encrypted to |
| 21 | // this recipient can be decrypted with the corresponding [HybridIdentity]. |
| 22 | // |
| 23 | // This recipient is safe against future cryptographically-relevant quantum |
| 24 | // computers, and can only be used along with other post-quantum recipients. |
| 25 | // |
| 26 | // This recipient is anonymous, in the sense that an attacker can't tell from |
| 27 | // the message alone if it is encrypted to a certain recipient. |
| 28 | type HybridRecipient struct { |
| 29 | pk hpke.PublicKey |
| 30 | } |
| 31 | |
| 32 | var _ Recipient = &HybridRecipient{} |
| 33 | |
| 34 | // newHybridRecipient returns a new [HybridRecipient] from a raw HPKE public key. |
| 35 | func newHybridRecipient(publicKey []byte) (*HybridRecipient, error) { |
| 36 | pk, err := hpke.MLKEM768X25519().NewPublicKey(publicKey) |
| 37 | if err != nil { |
| 38 | return nil, errors.New("invalid MLKEM768-X25519 public key") |
| 39 | } |
| 40 | return &HybridRecipient{pk: pk}, nil |
| 41 | } |
| 42 | |
| 43 | // ParseHybridRecipient returns a new [HybridRecipient] from a Bech32 public key |
| 44 | // encoding with the "age1pq1" prefix. |
| 45 | func ParseHybridRecipient(s string) (*HybridRecipient, error) { |
| 46 | t, k, err := bech32.Decode(s) |
| 47 | if err != nil { |
| 48 | return nil, fmt.Errorf("malformed recipient %q: %v", s, err) |
| 49 | } |
| 50 | if t != "age1pq" { |
| 51 | return nil, fmt.Errorf("malformed recipient %q: invalid type %q", s, t) |
| 52 | } |
| 53 | r, err := newHybridRecipient(k) |
| 54 | if err != nil { |
| 55 | return nil, fmt.Errorf("malformed recipient %q: %v", s, err) |
| 56 | } |
| 57 | return r, nil |
| 58 | } |
| 59 | |
| 60 | func (r *HybridRecipient) Wrap(fileKey []byte) ([]*Stanza, error) { |
| 61 | s, _, err := r.WrapWithLabels(fileKey) |
| 62 | return s, err |
| 63 | } |
| 64 | |
| 65 | // WrapWithLabels implements [RecipientWithLabels], returning a single |
| 66 | // "postquantum" label. This ensures a HybridRecipient can't be mixed with other |
| 67 | // recipients that would defeat its post-quantum security. |
| 68 | // |
| 69 | // To unsafely bypass this restriction, wrap HybridRecipient in a [Recipient] |
| 70 | // type that doesn't expose WrapWithLabels. |
| 71 | func (r *HybridRecipient) WrapWithLabels(fileKey []byte) ([]*Stanza, []string, error) { |
| 72 | enc, s, err := hpke.NewSender(r.pk, hpke.HKDFSHA256(), hpke.ChaCha20Poly1305(), []byte(pqLabel)) |
| 73 | if err != nil { |
| 74 | return nil, nil, fmt.Errorf("failed to set up HPKE sender: %v", err) |
| 75 | } |
| 76 | ct, err := s.Seal(nil, fileKey) |
| 77 | if err != nil { |
| 78 | return nil, nil, fmt.Errorf("failed to encrypt file key: %v", err) |
| 79 | } |
| 80 | |
| 81 | l := &Stanza{ |
| 82 | Type: "mlkem768x25519", |
| 83 | Args: []string{format.EncodeToString(enc)}, |
| 84 | Body: ct, |
| 85 | } |
| 86 | |
| 87 | return []*Stanza{l}, []string{"postquantum"}, nil |
| 88 | } |
| 89 | |
| 90 | // String returns the Bech32 public key encoding of r. |
| 91 | func (r *HybridRecipient) String() string { |
| 92 | s, _ := bech32.Encode("age1pq", r.pk.Bytes()) |
| 93 | return s |
| 94 | } |
| 95 | |
| 96 | // HybridIdentity is the standard age private key, which can decrypt messages |
| 97 | // encrypted to the corresponding [HybridRecipient]. |
| 98 | type HybridIdentity struct { |
| 99 | k hpke.PrivateKey |
| 100 | } |
| 101 | |
| 102 | var _ Identity = &HybridIdentity{} |
| 103 | |
| 104 | // newHybridIdentity returns a new [HybridIdentity] from a raw HPKE private key. |
| 105 | func newHybridIdentity(secretKey []byte) (*HybridIdentity, error) { |
| 106 | k, err := hpke.MLKEM768X25519().NewPrivateKey(secretKey) |
| 107 | if err != nil { |
| 108 | return nil, errors.New("invalid MLKEM768-X25519 secret key") |
| 109 | } |
| 110 | return &HybridIdentity{k: k}, nil |
| 111 | } |
| 112 | |
| 113 | // GenerateHybridIdentity randomly generates a new [HybridIdentity]. |
| 114 | func GenerateHybridIdentity() (*HybridIdentity, error) { |
| 115 | k, err := hpke.MLKEM768X25519().GenerateKey() |
| 116 | if err != nil { |
| 117 | return nil, fmt.Errorf("failed to generate post-quantum identity: %v", err) |
| 118 | } |
| 119 | return &HybridIdentity{k: k}, nil |
| 120 | } |
| 121 | |
| 122 | // ParseHybridIdentity returns a new [HybridIdentity] from a Bech32 private key |
| 123 | // encoding with the "AGE-SECRET-KEY-PQ-1" prefix. |
| 124 | func ParseHybridIdentity(s string) (*HybridIdentity, error) { |
| 125 | t, k, err := bech32.Decode(s) |
| 126 | if err != nil { |
| 127 | return nil, fmt.Errorf("malformed secret key: %v", err) |
| 128 | } |
| 129 | if t != "AGE-SECRET-KEY-PQ-" { |
| 130 | return nil, fmt.Errorf("malformed secret key: unknown type %q", t) |
| 131 | } |
| 132 | r, err := newHybridIdentity(k) |
| 133 | if err != nil { |
| 134 | return nil, fmt.Errorf("malformed secret key: %v", err) |
| 135 | } |
| 136 | return r, nil |
| 137 | } |
| 138 | |
| 139 | func (i *HybridIdentity) Unwrap(stanzas []*Stanza) ([]byte, error) { |
| 140 | return multiUnwrap(i.unwrap, stanzas) |
| 141 | } |
| 142 | |
| 143 | func (i *HybridIdentity) unwrap(block *Stanza) ([]byte, error) { |
| 144 | if block.Type != "mlkem768x25519" { |
| 145 | return nil, ErrIncorrectIdentity |
| 146 | } |
| 147 | if len(block.Args) != 1 { |
| 148 | return nil, errors.New("invalid mlkem768x25519 recipient block") |
| 149 | } |
| 150 | enc, err := format.DecodeString(block.Args[0]) |
| 151 | if err != nil { |
| 152 | return nil, fmt.Errorf("failed to parse mlkem768x25519 recipient: %v", err) |
| 153 | } |
| 154 | if len(block.Body) != fileKeySize+chacha20poly1305.Overhead { |
| 155 | return nil, errIncorrectCiphertextSize |
| 156 | } |
| 157 | |
| 158 | r, err := hpke.NewRecipient(enc, i.k, hpke.HKDFSHA256(), hpke.ChaCha20Poly1305(), []byte(pqLabel)) |
| 159 | if err != nil { |
| 160 | // MLKEM768-X25519 does implicit rejection, so a mismatched key does not |
| 161 | // hit this error path, but is only detected later when trying to open. |
| 162 | return nil, fmt.Errorf("invalid mlkem768x25519 recipient: %v", err) |
| 163 | } |
| 164 | fileKey, err := r.Open(nil, block.Body) |
| 165 | if err != nil { |
| 166 | return nil, ErrIncorrectIdentity |
| 167 | } |
| 168 | return fileKey, nil |
| 169 | } |
| 170 | |
| 171 | // Recipient returns the public [HybridRecipient] value corresponding to i. |
| 172 | func (i *HybridIdentity) Recipient() *HybridRecipient { |
| 173 | return &HybridRecipient{pk: i.k.PublicKey()} |
| 174 | } |
| 175 | |
| 176 | // String returns the Bech32 private key encoding of i. |
| 177 | func (i *HybridIdentity) String() string { |
| 178 | b, _ := i.k.Bytes() |
| 179 | s, _ := bech32.Encode("AGE-SECRET-KEY-PQ-", b) |
| 180 | return strings.ToUpper(s) |
| 181 | } |