| 1 | // Copyright 2019 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 | "crypto/rand" |
| 9 | "crypto/sha256" |
| 10 | "errors" |
| 11 | "fmt" |
| 12 | "io" |
| 13 | "strings" |
| 14 | |
| 15 | "filippo.io/age/internal/bech32" |
| 16 | "filippo.io/age/internal/format" |
| 17 | "golang.org/x/crypto/chacha20poly1305" |
| 18 | "golang.org/x/crypto/curve25519" |
| 19 | "golang.org/x/crypto/hkdf" |
| 20 | ) |
| 21 | |
| 22 | const x25519Label = "age-encryption.org/v1/X25519" |
| 23 | |
| 24 | // X25519Recipient is the standard age pre-quantum public key. Messages |
| 25 | // encrypted to this recipient can be decrypted with the corresponding |
| 26 | // [X25519Identity]. For post-quantum resistance, use [HybridRecipient]. |
| 27 | // |
| 28 | // This recipient is anonymous, in the sense that an attacker can't tell from |
| 29 | // the message alone if it is encrypted to a certain recipient. |
| 30 | type X25519Recipient struct { |
| 31 | theirPublicKey []byte |
| 32 | } |
| 33 | |
| 34 | var _ Recipient = &X25519Recipient{} |
| 35 | |
| 36 | // newX25519RecipientFromPoint returns a new X25519Recipient from a raw Curve25519 point. |
| 37 | func newX25519RecipientFromPoint(publicKey []byte) (*X25519Recipient, error) { |
| 38 | if len(publicKey) != curve25519.PointSize { |
| 39 | return nil, errors.New("invalid X25519 public key") |
| 40 | } |
| 41 | r := &X25519Recipient{ |
| 42 | theirPublicKey: make([]byte, curve25519.PointSize), |
| 43 | } |
| 44 | copy(r.theirPublicKey, publicKey) |
| 45 | return r, nil |
| 46 | } |
| 47 | |
| 48 | // ParseX25519Recipient returns a new X25519Recipient from a Bech32 public key |
| 49 | // encoding with the "age1" prefix. |
| 50 | func ParseX25519Recipient(s string) (*X25519Recipient, error) { |
| 51 | t, k, err := bech32.Decode(s) |
| 52 | if err != nil { |
| 53 | return nil, fmt.Errorf("malformed recipient %q: %v", s, err) |
| 54 | } |
| 55 | if t != "age" { |
| 56 | return nil, fmt.Errorf("malformed recipient %q: invalid type %q", s, t) |
| 57 | } |
| 58 | r, err := newX25519RecipientFromPoint(k) |
| 59 | if err != nil { |
| 60 | return nil, fmt.Errorf("malformed recipient %q: %v", s, err) |
| 61 | } |
| 62 | return r, nil |
| 63 | } |
| 64 | |
| 65 | func (r *X25519Recipient) Wrap(fileKey []byte) ([]*Stanza, error) { |
| 66 | ephemeral := make([]byte, curve25519.ScalarSize) |
| 67 | if _, err := rand.Read(ephemeral); err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | ourPublicKey, err := curve25519.X25519(ephemeral, curve25519.Basepoint) |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | |
| 75 | sharedSecret, err := curve25519.X25519(ephemeral, r.theirPublicKey) |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | |
| 80 | l := &Stanza{ |
| 81 | Type: "X25519", |
| 82 | Args: []string{format.EncodeToString(ourPublicKey)}, |
| 83 | } |
| 84 | |
| 85 | salt := make([]byte, 0, len(ourPublicKey)+len(r.theirPublicKey)) |
| 86 | salt = append(salt, ourPublicKey...) |
| 87 | salt = append(salt, r.theirPublicKey...) |
| 88 | h := hkdf.New(sha256.New, sharedSecret, salt, []byte(x25519Label)) |
| 89 | wrappingKey := make([]byte, chacha20poly1305.KeySize) |
| 90 | if _, err := io.ReadFull(h, wrappingKey); err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | |
| 94 | wrappedKey, err := aeadEncrypt(wrappingKey, fileKey) |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | l.Body = wrappedKey |
| 99 | |
| 100 | return []*Stanza{l}, nil |
| 101 | } |
| 102 | |
| 103 | // String returns the Bech32 public key encoding of r. |
| 104 | func (r *X25519Recipient) String() string { |
| 105 | s, _ := bech32.Encode("age", r.theirPublicKey) |
| 106 | return s |
| 107 | } |
| 108 | |
| 109 | // X25519Identity is the standard pre-quantum age private key, which can decrypt |
| 110 | // messages encrypted to the corresponding [X25519Recipient]. For post-quantum |
| 111 | // resistance, use [HybridIdentity]. |
| 112 | type X25519Identity struct { |
| 113 | secretKey, ourPublicKey []byte |
| 114 | } |
| 115 | |
| 116 | var _ Identity = &X25519Identity{} |
| 117 | |
| 118 | // newX25519IdentityFromScalar returns a new X25519Identity from a raw Curve25519 scalar. |
| 119 | func newX25519IdentityFromScalar(secretKey []byte) (*X25519Identity, error) { |
| 120 | if len(secretKey) != curve25519.ScalarSize { |
| 121 | return nil, errors.New("invalid X25519 secret key") |
| 122 | } |
| 123 | i := &X25519Identity{ |
| 124 | secretKey: make([]byte, curve25519.ScalarSize), |
| 125 | } |
| 126 | copy(i.secretKey, secretKey) |
| 127 | i.ourPublicKey, _ = curve25519.X25519(i.secretKey, curve25519.Basepoint) |
| 128 | return i, nil |
| 129 | } |
| 130 | |
| 131 | // GenerateX25519Identity randomly generates a new X25519Identity. |
| 132 | func GenerateX25519Identity() (*X25519Identity, error) { |
| 133 | secretKey := make([]byte, curve25519.ScalarSize) |
| 134 | if _, err := rand.Read(secretKey); err != nil { |
| 135 | return nil, fmt.Errorf("internal error: %v", err) |
| 136 | } |
| 137 | return newX25519IdentityFromScalar(secretKey) |
| 138 | } |
| 139 | |
| 140 | // ParseX25519Identity returns a new X25519Identity from a Bech32 private key |
| 141 | // encoding with the "AGE-SECRET-KEY-1" prefix. |
| 142 | func ParseX25519Identity(s string) (*X25519Identity, error) { |
| 143 | t, k, err := bech32.Decode(s) |
| 144 | if err != nil { |
| 145 | return nil, fmt.Errorf("malformed secret key: %v", err) |
| 146 | } |
| 147 | if t != "AGE-SECRET-KEY-" { |
| 148 | return nil, fmt.Errorf("malformed secret key: unknown type %q", t) |
| 149 | } |
| 150 | r, err := newX25519IdentityFromScalar(k) |
| 151 | if err != nil { |
| 152 | return nil, fmt.Errorf("malformed secret key: %v", err) |
| 153 | } |
| 154 | return r, nil |
| 155 | } |
| 156 | |
| 157 | func (i *X25519Identity) Unwrap(stanzas []*Stanza) ([]byte, error) { |
| 158 | return multiUnwrap(i.unwrap, stanzas) |
| 159 | } |
| 160 | |
| 161 | func (i *X25519Identity) unwrap(block *Stanza) ([]byte, error) { |
| 162 | if block.Type != "X25519" { |
| 163 | return nil, ErrIncorrectIdentity |
| 164 | } |
| 165 | if len(block.Args) != 1 { |
| 166 | return nil, errors.New("invalid X25519 recipient block") |
| 167 | } |
| 168 | publicKey, err := format.DecodeString(block.Args[0]) |
| 169 | if err != nil { |
| 170 | return nil, fmt.Errorf("failed to parse X25519 recipient: %v", err) |
| 171 | } |
| 172 | if len(publicKey) != curve25519.PointSize { |
| 173 | return nil, errors.New("invalid X25519 recipient block") |
| 174 | } |
| 175 | |
| 176 | sharedSecret, err := curve25519.X25519(i.secretKey, publicKey) |
| 177 | if err != nil { |
| 178 | return nil, fmt.Errorf("invalid X25519 recipient: %v", err) |
| 179 | } |
| 180 | |
| 181 | salt := make([]byte, 0, len(publicKey)+len(i.ourPublicKey)) |
| 182 | salt = append(salt, publicKey...) |
| 183 | salt = append(salt, i.ourPublicKey...) |
| 184 | h := hkdf.New(sha256.New, sharedSecret, salt, []byte(x25519Label)) |
| 185 | wrappingKey := make([]byte, chacha20poly1305.KeySize) |
| 186 | if _, err := io.ReadFull(h, wrappingKey); err != nil { |
| 187 | return nil, err |
| 188 | } |
| 189 | |
| 190 | fileKey, err := aeadDecrypt(wrappingKey, fileKeySize, block.Body) |
| 191 | if err == errIncorrectCiphertextSize { |
| 192 | return nil, errors.New("invalid X25519 recipient block: incorrect file key size") |
| 193 | } else if err != nil { |
| 194 | return nil, ErrIncorrectIdentity |
| 195 | } |
| 196 | return fileKey, nil |
| 197 | } |
| 198 | |
| 199 | // Recipient returns the public X25519Recipient value corresponding to i. |
| 200 | func (i *X25519Identity) Recipient() *X25519Recipient { |
| 201 | r := &X25519Recipient{} |
| 202 | r.theirPublicKey = i.ourPublicKey |
| 203 | return r |
| 204 | } |
| 205 | |
| 206 | // String returns the Bech32 private key encoding of i. |
| 207 | func (i *X25519Identity) String() string { |
| 208 | s, _ := bech32.Encode("AGE-SECRET-KEY-", i.secretKey) |
| 209 | return strings.ToUpper(s) |
| 210 | } |