| 1 |
// Copyright 2011 The Go 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 packet |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"crypto" |
| 10 |
"crypto/rsa" |
| 11 |
"encoding/binary" |
| 12 |
"encoding/hex" |
| 13 |
"io" |
| 14 |
"math/big" |
| 15 |
"strconv" |
| 16 |
|
| 17 |
"github.com/ProtonMail/go-crypto/openpgp/ecdh" |
| 18 |
"github.com/ProtonMail/go-crypto/openpgp/elgamal" |
| 19 |
"github.com/ProtonMail/go-crypto/openpgp/errors" |
| 20 |
"github.com/ProtonMail/go-crypto/openpgp/internal/encoding" |
| 21 |
"github.com/ProtonMail/go-crypto/openpgp/mlkem_ecdh" |
| 22 |
"github.com/ProtonMail/go-crypto/openpgp/x25519" |
| 23 |
"github.com/ProtonMail/go-crypto/openpgp/x448" |
| 24 |
) |
| 25 |
|
| 26 |
// EncryptedKey represents a public-key encrypted session key. See RFC 4880, |
| 27 |
// section 5.1. |
| 28 |
type EncryptedKey struct { |
| 29 |
Version int |
| 30 |
KeyId uint64 |
| 31 |
KeyVersion int // v6 |
| 32 |
KeyFingerprint []byte // v6 |
| 33 |
Algo PublicKeyAlgorithm |
| 34 |
CipherFunc CipherFunction // only valid after a successful Decrypt for a v3 packet |
| 35 |
Key []byte // only valid after a successful Decrypt |
| 36 |
|
| 37 |
encryptedMPI1 encoding.Field // used for RSA, Elgamal and ECDH |
| 38 |
encryptedMPI2 encoding.Field // used for Elgamal and ECDH |
| 39 |
ephemeralPublicEcc []byte // used for X25519, X448 and ML-KEM |
| 40 |
ephemeralPublicMlKem []byte // used for ML-KEM |
| 41 |
encryptedSession []byte // used for X25519, X448 and ML-KEM |
| 42 |
} |
| 43 |
|
| 44 |
func (e *EncryptedKey) parse(r io.Reader) (err error) { |
| 45 |
var buf [8]byte |
| 46 |
_, err = readFull(r, buf[:versionSize]) |
| 47 |
if err != nil { |
| 48 |
return |
| 49 |
} |
| 50 |
e.Version = int(buf[0]) |
| 51 |
if e.Version != 3 && e.Version != 6 { |
| 52 |
return errors.UnsupportedError("unknown EncryptedKey version " + strconv.Itoa(int(buf[0]))) |
| 53 |
} |
| 54 |
if e.Version == 6 { |
| 55 |
//Read a one-octet size of the following two fields. |
| 56 |
if _, err = readFull(r, buf[:1]); err != nil { |
| 57 |
return |
| 58 |
} |
| 59 |
// The size may also be zero, and the key version and |
| 60 |
// fingerprint omitted for an "anonymous recipient" |
| 61 |
if buf[0] != 0 { |
| 62 |
// non-anonymous case |
| 63 |
_, err = readFull(r, buf[:versionSize]) |
| 64 |
if err != nil { |
| 65 |
return |
| 66 |
} |
| 67 |
e.KeyVersion = int(buf[0]) |
| 68 |
if e.KeyVersion != 4 && e.KeyVersion != 6 { |
| 69 |
return errors.UnsupportedError("unknown public key version " + strconv.Itoa(e.KeyVersion)) |
| 70 |
} |
| 71 |
var fingerprint []byte |
| 72 |
if e.KeyVersion == 6 { |
| 73 |
fingerprint = make([]byte, fingerprintSizeV6) |
| 74 |
} else if e.KeyVersion == 4 { |
| 75 |
fingerprint = make([]byte, fingerprintSize) |
| 76 |
} |
| 77 |
_, err = readFull(r, fingerprint) |
| 78 |
if err != nil { |
| 79 |
return |
| 80 |
} |
| 81 |
e.KeyFingerprint = fingerprint |
| 82 |
if e.KeyVersion == 6 { |
| 83 |
e.KeyId = binary.BigEndian.Uint64(e.KeyFingerprint[:keyIdSize]) |
| 84 |
} else if e.KeyVersion == 4 { |
| 85 |
e.KeyId = binary.BigEndian.Uint64(e.KeyFingerprint[fingerprintSize-keyIdSize : fingerprintSize]) |
| 86 |
} |
| 87 |
} |
| 88 |
} else { |
| 89 |
_, err = readFull(r, buf[:8]) |
| 90 |
if err != nil { |
| 91 |
return |
| 92 |
} |
| 93 |
e.KeyId = binary.BigEndian.Uint64(buf[:keyIdSize]) |
| 94 |
} |
| 95 |
|
| 96 |
_, err = readFull(r, buf[:1]) |
| 97 |
if err != nil { |
| 98 |
return |
| 99 |
} |
| 100 |
e.Algo = PublicKeyAlgorithm(buf[0]) |
| 101 |
var cipherFunction byte |
| 102 |
switch e.Algo { |
| 103 |
case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly: |
| 104 |
e.encryptedMPI1 = new(encoding.MPI) |
| 105 |
if _, err = e.encryptedMPI1.ReadFrom(r); err != nil { |
| 106 |
return |
| 107 |
} |
| 108 |
case PubKeyAlgoElGamal: |
| 109 |
e.encryptedMPI1 = new(encoding.MPI) |
| 110 |
if _, err = e.encryptedMPI1.ReadFrom(r); err != nil { |
| 111 |
return |
| 112 |
} |
| 113 |
|
| 114 |
e.encryptedMPI2 = new(encoding.MPI) |
| 115 |
if _, err = e.encryptedMPI2.ReadFrom(r); err != nil { |
| 116 |
return |
| 117 |
} |
| 118 |
case PubKeyAlgoECDH: |
| 119 |
e.encryptedMPI1 = new(encoding.MPI) |
| 120 |
if _, err = e.encryptedMPI1.ReadFrom(r); err != nil { |
| 121 |
return |
| 122 |
} |
| 123 |
|
| 124 |
e.encryptedMPI2 = new(encoding.OID) |
| 125 |
if _, err = e.encryptedMPI2.ReadFrom(r); err != nil { |
| 126 |
return |
| 127 |
} |
| 128 |
case PubKeyAlgoX25519: |
| 129 |
e.ephemeralPublicEcc, e.encryptedSession, cipherFunction, err = x25519.DecodeFields(r, e.Version == 6) |
| 130 |
if err != nil { |
| 131 |
return |
| 132 |
} |
| 133 |
case PubKeyAlgoX448: |
| 134 |
e.ephemeralPublicEcc, e.encryptedSession, cipherFunction, err = x448.DecodeFields(r, e.Version == 6) |
| 135 |
if err != nil { |
| 136 |
return |
| 137 |
} |
| 138 |
case PubKeyAlgoMlkem768X25519: |
| 139 |
if e.ephemeralPublicEcc, e.ephemeralPublicMlKem, e.encryptedSession, cipherFunction, err = mlkem_ecdh.DecodeFields(r, 32, 1088, e.Version == 6); err != nil { |
| 140 |
return err |
| 141 |
} |
| 142 |
case PubKeyAlgoMlkem1024X448: |
| 143 |
if e.ephemeralPublicEcc, e.ephemeralPublicMlKem, e.encryptedSession, cipherFunction, err = mlkem_ecdh.DecodeFields(r, 56, 1568, e.Version == 6); err != nil { |
| 144 |
return err |
| 145 |
} |
| 146 |
} |
| 147 |
if e.Version < 6 { |
| 148 |
switch e.Algo { |
| 149 |
case PubKeyAlgoX25519, PubKeyAlgoX448, PubKeyAlgoMlkem768X25519, PubKeyAlgoMlkem1024X448: |
| 150 |
e.CipherFunc = CipherFunction(cipherFunction) |
| 151 |
// Check for validity is in the Decrypt method |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
_, err = consumeAll(r) |
| 156 |
return |
| 157 |
} |
| 158 |
|
| 159 |
// Decrypt decrypts an encrypted session key with the given private key. The |
| 160 |
// private key must have been decrypted first. |
| 161 |
// If config is nil, sensible defaults will be used. |
| 162 |
func (e *EncryptedKey) Decrypt(priv *PrivateKey, config *Config) error { |
| 163 |
if e.Version < 6 && e.KeyId != 0 && e.KeyId != priv.KeyId { |
| 164 |
return errors.InvalidArgumentError("cannot decrypt encrypted session key for key id " + strconv.FormatUint(e.KeyId, 16) + " with private key id " + strconv.FormatUint(priv.KeyId, 16)) |
| 165 |
} |
| 166 |
if e.Version == 6 && e.KeyVersion != 0 && !bytes.Equal(e.KeyFingerprint, priv.Fingerprint) { |
| 167 |
return errors.InvalidArgumentError("cannot decrypt encrypted session key for key fingerprint " + hex.EncodeToString(e.KeyFingerprint) + " with private key fingerprint " + hex.EncodeToString(priv.Fingerprint)) |
| 168 |
} |
| 169 |
if e.Algo != priv.PubKeyAlgo { |
| 170 |
return errors.InvalidArgumentError("cannot decrypt encrypted session key of type " + strconv.Itoa(int(e.Algo)) + " with private key of type " + strconv.Itoa(int(priv.PubKeyAlgo))) |
| 171 |
} |
| 172 |
if priv.Dummy() { |
| 173 |
return errors.ErrDummyPrivateKey("dummy key found") |
| 174 |
} |
| 175 |
|
| 176 |
var err error |
| 177 |
var b []byte |
| 178 |
|
| 179 |
// TODO(agl): use session key decryption routines here to avoid |
| 180 |
// padding oracle attacks. |
| 181 |
switch priv.PubKeyAlgo { |
| 182 |
case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly: |
| 183 |
// Supports both *rsa.PrivateKey and crypto.Decrypter |
| 184 |
k := priv.PrivateKey.(crypto.Decrypter) |
| 185 |
b, err = k.Decrypt(config.Random(), padToKeySize(k.Public().(*rsa.PublicKey), e.encryptedMPI1.Bytes()), nil) |
| 186 |
case PubKeyAlgoElGamal: |
| 187 |
c1 := new(big.Int).SetBytes(e.encryptedMPI1.Bytes()) |
| 188 |
c2 := new(big.Int).SetBytes(e.encryptedMPI2.Bytes()) |
| 189 |
b, err = elgamal.Decrypt(priv.PrivateKey.(*elgamal.PrivateKey), c1, c2) |
| 190 |
case PubKeyAlgoECDH: |
| 191 |
vsG := e.encryptedMPI1.Bytes() |
| 192 |
m := e.encryptedMPI2.Bytes() |
| 193 |
oid := priv.PublicKey.oid.EncodedBytes() |
| 194 |
fp := priv.PublicKey.Fingerprint[:] |
| 195 |
if priv.PublicKey.Version == 5 { |
| 196 |
// For v5 the, the fingerprint must be restricted to 20 bytes |
| 197 |
fp = fp[:20] |
| 198 |
} |
| 199 |
b, err = ecdh.Decrypt(priv.PrivateKey.(*ecdh.PrivateKey), vsG, m, oid, fp) |
| 200 |
case PubKeyAlgoX25519: |
| 201 |
b, err = x25519.Decrypt(priv.PrivateKey.(*x25519.PrivateKey), e.ephemeralPublicEcc, e.encryptedSession) |
| 202 |
case PubKeyAlgoX448: |
| 203 |
b, err = x448.Decrypt(priv.PrivateKey.(*x448.PrivateKey), e.ephemeralPublicEcc, e.encryptedSession) |
| 204 |
case PubKeyAlgoMlkem768X25519, PubKeyAlgoMlkem1024X448: |
| 205 |
b, err = mlkem_ecdh.Decrypt(priv.PrivateKey.(*mlkem_ecdh.PrivateKey), e.ephemeralPublicMlKem, e.ephemeralPublicEcc, e.encryptedSession) |
| 206 |
default: |
| 207 |
err = errors.InvalidArgumentError("cannot decrypt encrypted session key with private key of type " + strconv.Itoa(int(priv.PubKeyAlgo))) |
| 208 |
} |
| 209 |
if err != nil { |
| 210 |
return err |
| 211 |
} |
| 212 |
|
| 213 |
var key []byte |
| 214 |
switch priv.PubKeyAlgo { |
| 215 |
case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly, PubKeyAlgoElGamal, PubKeyAlgoECDH: |
| 216 |
keyOffset := 0 |
| 217 |
if e.Version < 6 { |
| 218 |
if len(b) == 0 { |
| 219 |
return errors.StructuralError("truncated session key") |
| 220 |
} |
| 221 |
e.CipherFunc = CipherFunction(b[0]) |
| 222 |
keyOffset = 1 |
| 223 |
if !e.CipherFunc.IsSupported() { |
| 224 |
return errors.UnsupportedError("unsupported encryption function") |
| 225 |
} |
| 226 |
} |
| 227 |
key, err = decodeChecksumKey(b[keyOffset:]) |
| 228 |
case PubKeyAlgoX25519, PubKeyAlgoX448, PubKeyAlgoMlkem768X25519, PubKeyAlgoMlkem1024X448: |
| 229 |
if e.Version < 6 { |
| 230 |
switch e.CipherFunc { |
| 231 |
case CipherAES128, CipherAES192, CipherAES256: |
| 232 |
break |
| 233 |
default: |
| 234 |
return errors.StructuralError("v3 PKESK mandates AES as cipher function for x25519, x448, and PQC") |
| 235 |
} |
| 236 |
} |
| 237 |
key = b[:] |
| 238 |
default: |
| 239 |
return errors.UnsupportedError("unsupported algorithm for decryption") |
| 240 |
} |
| 241 |
if err != nil { |
| 242 |
return err |
| 243 |
} |
| 244 |
e.Key = key |
| 245 |
return nil |
| 246 |
} |
| 247 |
|
| 248 |
// Serialize writes the encrypted key packet, e, to w. |
| 249 |
func (e *EncryptedKey) Serialize(w io.Writer) error { |
| 250 |
var encodedLength int |
| 251 |
switch e.Algo { |
| 252 |
case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly: |
| 253 |
encodedLength = int(e.encryptedMPI1.EncodedLength()) |
| 254 |
case PubKeyAlgoElGamal: |
| 255 |
encodedLength = int(e.encryptedMPI1.EncodedLength()) + int(e.encryptedMPI2.EncodedLength()) |
| 256 |
case PubKeyAlgoECDH: |
| 257 |
encodedLength = int(e.encryptedMPI1.EncodedLength()) + int(e.encryptedMPI2.EncodedLength()) |
| 258 |
case PubKeyAlgoX25519: |
| 259 |
encodedLength = x25519.EncodedFieldsLength(e.encryptedSession, e.Version == 6) |
| 260 |
case PubKeyAlgoX448: |
| 261 |
encodedLength = x448.EncodedFieldsLength(e.encryptedSession, e.Version == 6) |
| 262 |
case PubKeyAlgoMlkem768X25519, PubKeyAlgoMlkem1024X448: |
| 263 |
encodedLength = len(e.ephemeralPublicEcc) + len(e.ephemeralPublicMlKem) + len(e.encryptedSession) + 1 |
| 264 |
if e.Version < 6 { |
| 265 |
encodedLength += 1 |
| 266 |
} |
| 267 |
default: |
| 268 |
return errors.InvalidArgumentError("don't know how to serialize encrypted key type " + strconv.Itoa(int(e.Algo))) |
| 269 |
} |
| 270 |
|
| 271 |
packetLen := versionSize /* version */ + keyIdSize /* key id */ + algorithmSize /* algo */ + encodedLength |
| 272 |
if e.Version == 6 { |
| 273 |
packetLen = versionSize /* version */ + algorithmSize /* algo */ + encodedLength + keyVersionSize /* key version */ |
| 274 |
if e.KeyVersion == 6 { |
| 275 |
packetLen += fingerprintSizeV6 |
| 276 |
} else if e.KeyVersion == 4 { |
| 277 |
packetLen += fingerprintSize |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
err := serializeHeader(w, packetTypeEncryptedKey, packetLen) |
| 282 |
if err != nil { |
| 283 |
return err |
| 284 |
} |
| 285 |
|
| 286 |
_, err = w.Write([]byte{byte(e.Version)}) |
| 287 |
if err != nil { |
| 288 |
return err |
| 289 |
} |
| 290 |
if e.Version == 6 { |
| 291 |
_, err = w.Write([]byte{byte(e.KeyVersion)}) |
| 292 |
if err != nil { |
| 293 |
return err |
| 294 |
} |
| 295 |
// The key version number may also be zero, |
| 296 |
// and the fingerprint omitted |
| 297 |
if e.KeyVersion != 0 { |
| 298 |
_, err = w.Write(e.KeyFingerprint) |
| 299 |
if err != nil { |
| 300 |
return err |
| 301 |
} |
| 302 |
} |
| 303 |
} else { |
| 304 |
// Write KeyID |
| 305 |
err = binary.Write(w, binary.BigEndian, e.KeyId) |
| 306 |
if err != nil { |
| 307 |
return err |
| 308 |
} |
| 309 |
} |
| 310 |
_, err = w.Write([]byte{byte(e.Algo)}) |
| 311 |
if err != nil { |
| 312 |
return err |
| 313 |
} |
| 314 |
|
| 315 |
switch e.Algo { |
| 316 |
case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly: |
| 317 |
_, err := w.Write(e.encryptedMPI1.EncodedBytes()) |
| 318 |
return err |
| 319 |
case PubKeyAlgoElGamal: |
| 320 |
if _, err := w.Write(e.encryptedMPI1.EncodedBytes()); err != nil { |
| 321 |
return err |
| 322 |
} |
| 323 |
_, err := w.Write(e.encryptedMPI2.EncodedBytes()) |
| 324 |
return err |
| 325 |
case PubKeyAlgoECDH: |
| 326 |
if _, err := w.Write(e.encryptedMPI1.EncodedBytes()); err != nil { |
| 327 |
return err |
| 328 |
} |
| 329 |
_, err := w.Write(e.encryptedMPI2.EncodedBytes()) |
| 330 |
return err |
| 331 |
case PubKeyAlgoX25519: |
| 332 |
err := x25519.EncodeFields(w, e.ephemeralPublicEcc, e.encryptedSession, byte(e.CipherFunc), e.Version == 6) |
| 333 |
return err |
| 334 |
case PubKeyAlgoX448: |
| 335 |
err := x448.EncodeFields(w, e.ephemeralPublicEcc, e.encryptedSession, byte(e.CipherFunc), e.Version == 6) |
| 336 |
return err |
| 337 |
case PubKeyAlgoMlkem768X25519, PubKeyAlgoMlkem1024X448: |
| 338 |
err := mlkem_ecdh.EncodeFields(w, e.ephemeralPublicEcc, e.ephemeralPublicMlKem, e.encryptedSession, byte(e.CipherFunc), e.Version == 6) |
| 339 |
return err |
| 340 |
default: |
| 341 |
panic("internal error") |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
// SerializeEncryptedKeyAEAD serializes an encrypted key packet to w that contains |
| 346 |
// key, encrypted to pub. |
| 347 |
// If aeadSupported is set, PKESK v6 is used, otherwise v3. |
| 348 |
// Note: aeadSupported MUST match the value passed to SerializeSymmetricallyEncrypted. |
| 349 |
// If config is nil, sensible defaults will be used. |
| 350 |
func SerializeEncryptedKeyAEAD(w io.Writer, pub *PublicKey, cipherFunc CipherFunction, aeadSupported bool, key []byte, config *Config) error { |
| 351 |
return SerializeEncryptedKeyAEADwithHiddenOption(w, pub, cipherFunc, aeadSupported, key, false, config) |
| 352 |
} |
| 353 |
|
| 354 |
// SerializeEncryptedKeyAEADwithHiddenOption serializes an encrypted key packet to w that contains |
| 355 |
// key, encrypted to pub. |
| 356 |
// Offers the hidden flag option to indicated if the PKESK packet should include a wildcard KeyID. |
| 357 |
// If aeadSupported is set, PKESK v6 is used, otherwise v3. |
| 358 |
// Note: aeadSupported MUST match the value passed to SerializeSymmetricallyEncrypted. |
| 359 |
// If config is nil, sensible defaults will be used. |
| 360 |
func SerializeEncryptedKeyAEADwithHiddenOption(w io.Writer, pub *PublicKey, cipherFunc CipherFunction, aeadSupported bool, key []byte, hidden bool, config *Config) error { |
| 361 |
var buf [36]byte // max possible header size is v6 |
| 362 |
lenHeaderWritten := versionSize |
| 363 |
version := 3 |
| 364 |
|
| 365 |
if aeadSupported { |
| 366 |
version = 6 |
| 367 |
} |
| 368 |
// An implementation MUST NOT generate ElGamal v6 PKESKs. |
| 369 |
if version == 6 && pub.PubKeyAlgo == PubKeyAlgoElGamal { |
| 370 |
return errors.InvalidArgumentError("ElGamal v6 PKESK are not allowed") |
| 371 |
} |
| 372 |
// In v3 PKESKs, for X25519 and X448, mandate using AES |
| 373 |
if version == 3 && cipherFunc != CipherAES128 && cipherFunc != CipherAES192 && cipherFunc != CipherAES256 { |
| 374 |
switch pub.PubKeyAlgo { |
| 375 |
case PubKeyAlgoX25519, PubKeyAlgoX448, PubKeyAlgoMlkem768X25519, PubKeyAlgoMlkem1024X448: |
| 376 |
return errors.InvalidArgumentError("v3 PKESK mandates AES for x25519, x448, and PQC") |
| 377 |
default: |
| 378 |
break |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
buf[0] = byte(version) |
| 383 |
|
| 384 |
// If hidden is set, the key should be hidden |
| 385 |
// An implementation MAY accept or use a Key ID of all zeros, |
| 386 |
// or a key version of zero and no key fingerprint, to hide the intended decryption key. |
| 387 |
// See Section 5.1.8. in the open pgp crypto refresh |
| 388 |
if version == 6 { |
| 389 |
if !hidden { |
| 390 |
// A one-octet size of the following two fields. |
| 391 |
buf[1] = byte(keyVersionSize + len(pub.Fingerprint)) |
| 392 |
// A one octet key version number. |
| 393 |
buf[2] = byte(pub.Version) |
| 394 |
lenHeaderWritten += keyVersionSize + 1 |
| 395 |
// The fingerprint of the public key |
| 396 |
copy(buf[lenHeaderWritten:lenHeaderWritten+len(pub.Fingerprint)], pub.Fingerprint) |
| 397 |
lenHeaderWritten += len(pub.Fingerprint) |
| 398 |
} else { |
| 399 |
// The size may also be zero, and the key version |
| 400 |
// and fingerprint omitted for an "anonymous recipient" |
| 401 |
buf[1] = 0 |
| 402 |
lenHeaderWritten += 1 |
| 403 |
} |
| 404 |
} else { |
| 405 |
if !hidden { |
| 406 |
binary.BigEndian.PutUint64(buf[versionSize:(versionSize+keyIdSize)], pub.KeyId) |
| 407 |
} |
| 408 |
lenHeaderWritten += keyIdSize |
| 409 |
} |
| 410 |
buf[lenHeaderWritten] = byte(pub.PubKeyAlgo) |
| 411 |
lenHeaderWritten += algorithmSize |
| 412 |
|
| 413 |
var keyBlock []byte |
| 414 |
switch pub.PubKeyAlgo { |
| 415 |
case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly, PubKeyAlgoElGamal, PubKeyAlgoECDH: |
| 416 |
lenKeyBlock := len(key) + 2 |
| 417 |
if version < 6 { |
| 418 |
lenKeyBlock += 1 // cipher type included |
| 419 |
} |
| 420 |
keyBlock = make([]byte, lenKeyBlock) |
| 421 |
keyOffset := 0 |
| 422 |
if version < 6 { |
| 423 |
keyBlock[0] = byte(cipherFunc) |
| 424 |
keyOffset = 1 |
| 425 |
} |
| 426 |
encodeChecksumKey(keyBlock[keyOffset:], key) |
| 427 |
case PubKeyAlgoX25519, PubKeyAlgoX448, PubKeyAlgoMlkem768X25519, PubKeyAlgoMlkem1024X448: |
| 428 |
// algorithm is added in plaintext below |
| 429 |
keyBlock = key |
| 430 |
} |
| 431 |
|
| 432 |
switch pub.PubKeyAlgo { |
| 433 |
case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly: |
| 434 |
return serializeEncryptedKeyRSA(w, config.Random(), buf[:lenHeaderWritten], pub.PublicKey.(*rsa.PublicKey), keyBlock) |
| 435 |
case PubKeyAlgoElGamal: |
| 436 |
return serializeEncryptedKeyElGamal(w, config.Random(), buf[:lenHeaderWritten], pub.PublicKey.(*elgamal.PublicKey), keyBlock) |
| 437 |
case PubKeyAlgoECDH: |
| 438 |
return serializeEncryptedKeyECDH(w, config.Random(), buf[:lenHeaderWritten], pub.PublicKey.(*ecdh.PublicKey), keyBlock, pub.oid, pub.Fingerprint) |
| 439 |
case PubKeyAlgoX25519: |
| 440 |
return serializeEncryptedKeyX25519(w, config.Random(), buf[:lenHeaderWritten], pub.PublicKey.(*x25519.PublicKey), keyBlock, byte(cipherFunc), version) |
| 441 |
case PubKeyAlgoX448: |
| 442 |
return serializeEncryptedKeyX448(w, config.Random(), buf[:lenHeaderWritten], pub.PublicKey.(*x448.PublicKey), keyBlock, byte(cipherFunc), version) |
| 443 |
case PubKeyAlgoMlkem768X25519, PubKeyAlgoMlkem1024X448: |
| 444 |
return serializeEncryptedKeyMlkem(w, config.Random(), buf[:lenHeaderWritten], pub.PublicKey.(*mlkem_ecdh.PublicKey), keyBlock, byte(cipherFunc), version) |
| 445 |
case PubKeyAlgoDSA, PubKeyAlgoRSASignOnly: |
| 446 |
return errors.InvalidArgumentError("cannot encrypt to public key of type " + strconv.Itoa(int(pub.PubKeyAlgo))) |
| 447 |
} |
| 448 |
|
| 449 |
return errors.UnsupportedError("encrypting a key to public key of type " + strconv.Itoa(int(pub.PubKeyAlgo))) |
| 450 |
} |
| 451 |
|
| 452 |
// SerializeEncryptedKey serializes an encrypted key packet to w that contains |
| 453 |
// key, encrypted to pub. |
| 454 |
// PKESKv6 is used if config.AEAD() is not nil. |
| 455 |
// If config is nil, sensible defaults will be used. |
| 456 |
// Deprecated: Use SerializeEncryptedKeyAEAD instead. |
| 457 |
func SerializeEncryptedKey(w io.Writer, pub *PublicKey, cipherFunc CipherFunction, key []byte, config *Config) error { |
| 458 |
return SerializeEncryptedKeyAEAD(w, pub, cipherFunc, config.AEAD() != nil, key, config) |
| 459 |
} |
| 460 |
|
| 461 |
// SerializeEncryptedKeyWithHiddenOption serializes an encrypted key packet to w that contains |
| 462 |
// key, encrypted to pub. PKESKv6 is used if config.AEAD() is not nil. |
| 463 |
// The hidden option controls if the packet should be anonymous, i.e., omit key metadata. |
| 464 |
// If config is nil, sensible defaults will be used. |
| 465 |
// Deprecated: Use SerializeEncryptedKeyAEADwithHiddenOption instead. |
| 466 |
func SerializeEncryptedKeyWithHiddenOption(w io.Writer, pub *PublicKey, cipherFunc CipherFunction, key []byte, hidden bool, config *Config) error { |
| 467 |
return SerializeEncryptedKeyAEADwithHiddenOption(w, pub, cipherFunc, config.AEAD() != nil, key, hidden, config) |
| 468 |
} |
| 469 |
|
| 470 |
func serializeEncryptedKeyRSA(w io.Writer, rand io.Reader, header []byte, pub *rsa.PublicKey, keyBlock []byte) error { |
| 471 |
cipherText, err := rsa.EncryptPKCS1v15(rand, pub, keyBlock) |
| 472 |
if err != nil { |
| 473 |
return errors.InvalidArgumentError("RSA encryption failed: " + err.Error()) |
| 474 |
} |
| 475 |
|
| 476 |
cipherMPI := encoding.NewMPI(cipherText) |
| 477 |
packetLen := len(header) /* header length */ + int(cipherMPI.EncodedLength()) |
| 478 |
|
| 479 |
err = serializeHeader(w, packetTypeEncryptedKey, packetLen) |
| 480 |
if err != nil { |
| 481 |
return err |
| 482 |
} |
| 483 |
_, err = w.Write(header[:]) |
| 484 |
if err != nil { |
| 485 |
return err |
| 486 |
} |
| 487 |
_, err = w.Write(cipherMPI.EncodedBytes()) |
| 488 |
return err |
| 489 |
} |
| 490 |
|
| 491 |
func serializeEncryptedKeyElGamal(w io.Writer, rand io.Reader, header []byte, pub *elgamal.PublicKey, keyBlock []byte) error { |
| 492 |
c1, c2, err := elgamal.Encrypt(rand, pub, keyBlock) |
| 493 |
if err != nil { |
| 494 |
return errors.InvalidArgumentError("ElGamal encryption failed: " + err.Error()) |
| 495 |
} |
| 496 |
|
| 497 |
packetLen := len(header) /* header length */ |
| 498 |
packetLen += 2 /* mpi size */ + (c1.BitLen()+7)/8 |
| 499 |
packetLen += 2 /* mpi size */ + (c2.BitLen()+7)/8 |
| 500 |
|
| 501 |
err = serializeHeader(w, packetTypeEncryptedKey, packetLen) |
| 502 |
if err != nil { |
| 503 |
return err |
| 504 |
} |
| 505 |
_, err = w.Write(header[:]) |
| 506 |
if err != nil { |
| 507 |
return err |
| 508 |
} |
| 509 |
if _, err = w.Write(new(encoding.MPI).SetBig(c1).EncodedBytes()); err != nil { |
| 510 |
return err |
| 511 |
} |
| 512 |
_, err = w.Write(new(encoding.MPI).SetBig(c2).EncodedBytes()) |
| 513 |
return err |
| 514 |
} |
| 515 |
|
| 516 |
func serializeEncryptedKeyECDH(w io.Writer, rand io.Reader, header []byte, pub *ecdh.PublicKey, keyBlock []byte, oid encoding.Field, fingerprint []byte) error { |
| 517 |
vsG, c, err := ecdh.Encrypt(rand, pub, keyBlock, oid.EncodedBytes(), fingerprint) |
| 518 |
if err != nil { |
| 519 |
return errors.InvalidArgumentError("ECDH encryption failed: " + err.Error()) |
| 520 |
} |
| 521 |
|
| 522 |
g := encoding.NewMPI(vsG) |
| 523 |
m := encoding.NewOID(c) |
| 524 |
|
| 525 |
packetLen := len(header) /* header length */ |
| 526 |
packetLen += int(g.EncodedLength()) + int(m.EncodedLength()) |
| 527 |
|
| 528 |
err = serializeHeader(w, packetTypeEncryptedKey, packetLen) |
| 529 |
if err != nil { |
| 530 |
return err |
| 531 |
} |
| 532 |
|
| 533 |
_, err = w.Write(header[:]) |
| 534 |
if err != nil { |
| 535 |
return err |
| 536 |
} |
| 537 |
if _, err = w.Write(g.EncodedBytes()); err != nil { |
| 538 |
return err |
| 539 |
} |
| 540 |
_, err = w.Write(m.EncodedBytes()) |
| 541 |
return err |
| 542 |
} |
| 543 |
|
| 544 |
func serializeEncryptedKeyX25519(w io.Writer, rand io.Reader, header []byte, pub *x25519.PublicKey, keyBlock []byte, cipherFunc byte, version int) error { |
| 545 |
ephemeralPublicX25519, ciphertext, err := x25519.Encrypt(rand, pub, keyBlock) |
| 546 |
if err != nil { |
| 547 |
return errors.InvalidArgumentError("x25519 encryption failed: " + err.Error()) |
| 548 |
} |
| 549 |
|
| 550 |
packetLen := len(header) /* header length */ |
| 551 |
packetLen += x25519.EncodedFieldsLength(ciphertext, version == 6) |
| 552 |
|
| 553 |
err = serializeHeader(w, packetTypeEncryptedKey, packetLen) |
| 554 |
if err != nil { |
| 555 |
return err |
| 556 |
} |
| 557 |
|
| 558 |
_, err = w.Write(header[:]) |
| 559 |
if err != nil { |
| 560 |
return err |
| 561 |
} |
| 562 |
return x25519.EncodeFields(w, ephemeralPublicX25519, ciphertext, cipherFunc, version == 6) |
| 563 |
} |
| 564 |
|
| 565 |
func serializeEncryptedKeyX448(w io.Writer, rand io.Reader, header []byte, pub *x448.PublicKey, keyBlock []byte, cipherFunc byte, version int) error { |
| 566 |
ephemeralPublicX448, ciphertext, err := x448.Encrypt(rand, pub, keyBlock) |
| 567 |
if err != nil { |
| 568 |
return errors.InvalidArgumentError("x448 encryption failed: " + err.Error()) |
| 569 |
} |
| 570 |
|
| 571 |
packetLen := len(header) /* header length */ |
| 572 |
packetLen += x448.EncodedFieldsLength(ciphertext, version == 6) |
| 573 |
|
| 574 |
err = serializeHeader(w, packetTypeEncryptedKey, packetLen) |
| 575 |
if err != nil { |
| 576 |
return err |
| 577 |
} |
| 578 |
|
| 579 |
_, err = w.Write(header[:]) |
| 580 |
if err != nil { |
| 581 |
return err |
| 582 |
} |
| 583 |
return x448.EncodeFields(w, ephemeralPublicX448, ciphertext, cipherFunc, version == 6) |
| 584 |
} |
| 585 |
|
| 586 |
func checksumKeyMaterial(key []byte) uint16 { |
| 587 |
var checksum uint16 |
| 588 |
for _, v := range key { |
| 589 |
checksum += uint16(v) |
| 590 |
} |
| 591 |
return checksum |
| 592 |
} |
| 593 |
|
| 594 |
func decodeChecksumKey(msg []byte) (key []byte, err error) { |
| 595 |
if len(msg) < 2 { |
| 596 |
return nil, errors.StructuralError("truncated session key") |
| 597 |
} |
| 598 |
key = msg[:len(msg)-2] |
| 599 |
expectedChecksum := uint16(msg[len(msg)-2])<<8 | uint16(msg[len(msg)-1]) |
| 600 |
checksum := checksumKeyMaterial(key) |
| 601 |
if checksum != expectedChecksum { |
| 602 |
err = errors.StructuralError("session key checksum is incorrect") |
| 603 |
} |
| 604 |
return |
| 605 |
} |
| 606 |
|
| 607 |
func encodeChecksumKey(buffer []byte, key []byte) { |
| 608 |
copy(buffer, key) |
| 609 |
checksum := checksumKeyMaterial(key) |
| 610 |
buffer[len(key)] = byte(checksum >> 8) |
| 611 |
buffer[len(key)+1] = byte(checksum) |
| 612 |
} |
| 613 |
|
| 614 |
func serializeEncryptedKeyMlkem(w io.Writer, rand io.Reader, header []byte, pub *mlkem_ecdh.PublicKey, keyBlock []byte, cipherFunc byte, version int) error { |
| 615 |
mlE, ecE, c, err := mlkem_ecdh.Encrypt(rand, pub, keyBlock) |
| 616 |
if err != nil { |
| 617 |
return errors.InvalidArgumentError("ML-KEM + ECDH encryption failed: " + err.Error()) |
| 618 |
} |
| 619 |
|
| 620 |
packetLen := len(header) /* header length */ |
| 621 |
packetLen += len(ecE) + len(mlE) + len(c) + 1 |
| 622 |
if version < 6 { |
| 623 |
packetLen += 1 |
| 624 |
} |
| 625 |
|
| 626 |
err = serializeHeader(w, packetTypeEncryptedKey, packetLen) |
| 627 |
if err != nil { |
| 628 |
return err |
| 629 |
} |
| 630 |
|
| 631 |
_, err = w.Write(header) |
| 632 |
if err != nil { |
| 633 |
return err |
| 634 |
} |
| 635 |
|
| 636 |
return mlkem_ecdh.EncodeFields(w, ecE, mlE, c, cipherFunc, version == 6) |
| 637 |
} |