| 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/cipher" |
| 10 |
"crypto/sha256" |
| 11 |
"io" |
| 12 |
"strconv" |
| 13 |
|
| 14 |
"github.com/ProtonMail/go-crypto/openpgp/errors" |
| 15 |
"github.com/ProtonMail/go-crypto/openpgp/s2k" |
| 16 |
"golang.org/x/crypto/hkdf" |
| 17 |
) |
| 18 |
|
| 19 |
// This is the largest session key that we'll support. Since at most 256-bit cipher |
| 20 |
// is supported in OpenPGP, this is large enough to contain also the auth tag. |
| 21 |
const maxSessionKeySizeInBytes = 64 |
| 22 |
|
| 23 |
// SymmetricKeyEncrypted represents a passphrase protected session key. See RFC |
| 24 |
// 4880, section 5.3. |
| 25 |
type SymmetricKeyEncrypted struct { |
| 26 |
Version int |
| 27 |
CipherFunc CipherFunction |
| 28 |
Mode AEADMode |
| 29 |
s2k func(out, in []byte) |
| 30 |
iv []byte |
| 31 |
encryptedKey []byte // Contains also the authentication tag for AEAD |
| 32 |
} |
| 33 |
|
| 34 |
// parse parses an SymmetricKeyEncrypted packet as specified in |
| 35 |
// https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-07.html#name-symmetric-key-encrypted-ses |
| 36 |
func (ske *SymmetricKeyEncrypted) parse(r io.Reader) error { |
| 37 |
var buf [1]byte |
| 38 |
|
| 39 |
// Version |
| 40 |
if _, err := readFull(r, buf[:]); err != nil { |
| 41 |
return err |
| 42 |
} |
| 43 |
ske.Version = int(buf[0]) |
| 44 |
if ske.Version != 4 && ske.Version != 5 && ske.Version != 6 { |
| 45 |
return errors.UnsupportedError("unknown SymmetricKeyEncrypted version") |
| 46 |
} |
| 47 |
|
| 48 |
if V5Disabled && ske.Version == 5 { |
| 49 |
return errors.UnsupportedError("support for parsing v5 entities is disabled; build with `-tags v5` if needed") |
| 50 |
} |
| 51 |
|
| 52 |
if ske.Version > 5 { |
| 53 |
// Scalar octet count |
| 54 |
if _, err := readFull(r, buf[:]); err != nil { |
| 55 |
return err |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// Cipher function |
| 60 |
if _, err := readFull(r, buf[:]); err != nil { |
| 61 |
return err |
| 62 |
} |
| 63 |
ske.CipherFunc = CipherFunction(buf[0]) |
| 64 |
if !ske.CipherFunc.IsSupported() { |
| 65 |
return errors.UnsupportedError("unknown cipher: " + strconv.Itoa(int(buf[0]))) |
| 66 |
} |
| 67 |
|
| 68 |
if ske.Version >= 5 { |
| 69 |
// AEAD mode |
| 70 |
if _, err := readFull(r, buf[:]); err != nil { |
| 71 |
return errors.StructuralError("cannot read AEAD octet from packet") |
| 72 |
} |
| 73 |
ske.Mode = AEADMode(buf[0]) |
| 74 |
} |
| 75 |
|
| 76 |
if ske.Version > 5 { |
| 77 |
// Scalar octet count |
| 78 |
if _, err := readFull(r, buf[:]); err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
var err error |
| 84 |
if ske.s2k, err = s2k.Parse(r); err != nil { |
| 85 |
if _, ok := err.(errors.ErrDummyPrivateKey); ok { |
| 86 |
return errors.UnsupportedError("missing key GNU extension in session key") |
| 87 |
} |
| 88 |
return err |
| 89 |
} |
| 90 |
|
| 91 |
if ske.Version >= 5 { |
| 92 |
// AEAD IV |
| 93 |
iv := make([]byte, ske.Mode.IvLength()) |
| 94 |
_, err := readFull(r, iv) |
| 95 |
if err != nil { |
| 96 |
return errors.StructuralError("cannot read AEAD IV") |
| 97 |
} |
| 98 |
|
| 99 |
ske.iv = iv |
| 100 |
} |
| 101 |
|
| 102 |
encryptedKey := make([]byte, maxSessionKeySizeInBytes) |
| 103 |
// The session key may follow. We just have to try and read to find |
| 104 |
// out. If it exists then we limit it to maxSessionKeySizeInBytes. |
| 105 |
n, err := readFull(r, encryptedKey) |
| 106 |
if err != nil && err != io.ErrUnexpectedEOF { |
| 107 |
return err |
| 108 |
} |
| 109 |
|
| 110 |
if n != 0 { |
| 111 |
if n == maxSessionKeySizeInBytes { |
| 112 |
return errors.UnsupportedError("oversized encrypted session key") |
| 113 |
} |
| 114 |
ske.encryptedKey = encryptedKey[:n] |
| 115 |
} |
| 116 |
return nil |
| 117 |
} |
| 118 |
|
| 119 |
// Decrypt attempts to decrypt an encrypted session key and returns the key and |
| 120 |
// the cipher to use when decrypting a subsequent Symmetrically Encrypted Data |
| 121 |
// packet. |
| 122 |
func (ske *SymmetricKeyEncrypted) Decrypt(passphrase []byte) ([]byte, CipherFunction, error) { |
| 123 |
key := make([]byte, ske.CipherFunc.KeySize()) |
| 124 |
ske.s2k(key, passphrase) |
| 125 |
if len(ske.encryptedKey) == 0 { |
| 126 |
return key, ske.CipherFunc, nil |
| 127 |
} |
| 128 |
switch ske.Version { |
| 129 |
case 4: |
| 130 |
plaintextKey, cipherFunc, err := ske.decryptV4(key) |
| 131 |
return plaintextKey, cipherFunc, err |
| 132 |
case 5, 6: |
| 133 |
plaintextKey, err := ske.aeadDecrypt(ske.Version, key) |
| 134 |
return plaintextKey, CipherFunction(0), err |
| 135 |
} |
| 136 |
err := errors.UnsupportedError("unknown SymmetricKeyEncrypted version") |
| 137 |
return nil, CipherFunction(0), err |
| 138 |
} |
| 139 |
|
| 140 |
func (ske *SymmetricKeyEncrypted) decryptV4(key []byte) ([]byte, CipherFunction, error) { |
| 141 |
// the IV is all zeros |
| 142 |
iv := make([]byte, ske.CipherFunc.blockSize()) |
| 143 |
c := cipher.NewCFBDecrypter(ske.CipherFunc.new(key), iv) |
| 144 |
plaintextKey := make([]byte, len(ske.encryptedKey)) |
| 145 |
c.XORKeyStream(plaintextKey, ske.encryptedKey) |
| 146 |
cipherFunc := CipherFunction(plaintextKey[0]) |
| 147 |
if cipherFunc.blockSize() == 0 { |
| 148 |
return nil, ske.CipherFunc, errors.UnsupportedError( |
| 149 |
"unknown cipher: " + strconv.Itoa(int(cipherFunc))) |
| 150 |
} |
| 151 |
plaintextKey = plaintextKey[1:] |
| 152 |
if len(plaintextKey) != cipherFunc.KeySize() { |
| 153 |
return nil, cipherFunc, errors.StructuralError( |
| 154 |
"length of decrypted key not equal to cipher keysize") |
| 155 |
} |
| 156 |
return plaintextKey, cipherFunc, nil |
| 157 |
} |
| 158 |
|
| 159 |
func (ske *SymmetricKeyEncrypted) aeadDecrypt(version int, key []byte) ([]byte, error) { |
| 160 |
adata := []byte{0xc3, byte(version), byte(ske.CipherFunc), byte(ske.Mode)} |
| 161 |
aead, err := getEncryptedKeyAeadInstance(ske.CipherFunc, ske.Mode, key, adata, version) |
| 162 |
if err != nil { |
| 163 |
return nil, err |
| 164 |
} |
| 165 |
|
| 166 |
plaintextKey, err := aead.Open(nil, ske.iv, ske.encryptedKey, adata) |
| 167 |
if err != nil { |
| 168 |
return nil, err |
| 169 |
} |
| 170 |
return plaintextKey, nil |
| 171 |
} |
| 172 |
|
| 173 |
// SerializeSymmetricKeyEncrypted serializes a symmetric key packet to w. |
| 174 |
// The packet contains a random session key, encrypted by a key derived from |
| 175 |
// the given passphrase. The session key is returned and must be passed to |
| 176 |
// SerializeSymmetricallyEncrypted. |
| 177 |
// If config is nil, sensible defaults will be used. |
| 178 |
func SerializeSymmetricKeyEncrypted(w io.Writer, passphrase []byte, config *Config) (key []byte, err error) { |
| 179 |
cipherFunc := config.Cipher() |
| 180 |
|
| 181 |
sessionKey := make([]byte, cipherFunc.KeySize()) |
| 182 |
_, err = io.ReadFull(config.Random(), sessionKey) |
| 183 |
if err != nil { |
| 184 |
return |
| 185 |
} |
| 186 |
|
| 187 |
err = SerializeSymmetricKeyEncryptedReuseKey(w, sessionKey, passphrase, config) |
| 188 |
if err != nil { |
| 189 |
return |
| 190 |
} |
| 191 |
|
| 192 |
key = sessionKey |
| 193 |
return |
| 194 |
} |
| 195 |
|
| 196 |
// SerializeSymmetricKeyEncryptedReuseKey serializes a symmetric key packet to w. |
| 197 |
// The packet contains the given session key, encrypted by a key derived from |
| 198 |
// the given passphrase. The returned session key must be passed to |
| 199 |
// SerializeSymmetricallyEncrypted. |
| 200 |
// If config is nil, sensible defaults will be used. |
| 201 |
// Deprecated: Use SerializeSymmetricKeyEncryptedAEADReuseKey instead. |
| 202 |
func SerializeSymmetricKeyEncryptedReuseKey(w io.Writer, sessionKey []byte, passphrase []byte, config *Config) (err error) { |
| 203 |
return SerializeSymmetricKeyEncryptedAEADReuseKey(w, sessionKey, passphrase, config.AEAD() != nil, config) |
| 204 |
} |
| 205 |
|
| 206 |
// SerializeSymmetricKeyEncryptedAEADReuseKey serializes a symmetric key packet to w. |
| 207 |
// The packet contains the given session key, encrypted by a key derived from |
| 208 |
// the given passphrase. The returned session key must be passed to |
| 209 |
// SerializeSymmetricallyEncrypted. |
| 210 |
// If aeadSupported is set, SKESK v6 is used, otherwise v4. |
| 211 |
// Note: aeadSupported MUST match the value passed to SerializeSymmetricallyEncrypted. |
| 212 |
// If config is nil, sensible defaults will be used. |
| 213 |
func SerializeSymmetricKeyEncryptedAEADReuseKey(w io.Writer, sessionKey []byte, passphrase []byte, aeadSupported bool, config *Config) (err error) { |
| 214 |
var version int |
| 215 |
if aeadSupported { |
| 216 |
version = 6 |
| 217 |
} else { |
| 218 |
version = 4 |
| 219 |
} |
| 220 |
cipherFunc := config.Cipher() |
| 221 |
// cipherFunc must be AES |
| 222 |
if !cipherFunc.IsSupported() || cipherFunc < CipherAES128 || cipherFunc > CipherAES256 { |
| 223 |
return errors.UnsupportedError("unsupported cipher: " + strconv.Itoa(int(cipherFunc))) |
| 224 |
} |
| 225 |
|
| 226 |
keySize := cipherFunc.KeySize() |
| 227 |
s2kBuf := new(bytes.Buffer) |
| 228 |
keyEncryptingKey := make([]byte, keySize) |
| 229 |
// s2k.Serialize salts and stretches the passphrase, and writes the |
| 230 |
// resulting key to keyEncryptingKey and the s2k descriptor to s2kBuf. |
| 231 |
err = s2k.Serialize(s2kBuf, keyEncryptingKey, config.Random(), passphrase, config.S2K()) |
| 232 |
if err != nil { |
| 233 |
return |
| 234 |
} |
| 235 |
s2kBytes := s2kBuf.Bytes() |
| 236 |
|
| 237 |
var packetLength int |
| 238 |
switch version { |
| 239 |
case 4: |
| 240 |
packetLength = 2 /* header */ + len(s2kBytes) + 1 /* cipher type */ + keySize |
| 241 |
case 5, 6: |
| 242 |
ivLen := config.AEAD().Mode().IvLength() |
| 243 |
tagLen := config.AEAD().Mode().TagLength() |
| 244 |
packetLength = 3 + len(s2kBytes) + ivLen + keySize + tagLen |
| 245 |
} |
| 246 |
if version > 5 { |
| 247 |
packetLength += 2 // additional octet count fields |
| 248 |
} |
| 249 |
|
| 250 |
err = serializeHeader(w, packetTypeSymmetricKeyEncrypted, packetLength) |
| 251 |
if err != nil { |
| 252 |
return |
| 253 |
} |
| 254 |
|
| 255 |
// Symmetric Key Encrypted Version |
| 256 |
buf := []byte{byte(version)} |
| 257 |
|
| 258 |
if version > 5 { |
| 259 |
// Scalar octet count |
| 260 |
buf = append(buf, byte(3+len(s2kBytes)+config.AEAD().Mode().IvLength())) |
| 261 |
} |
| 262 |
|
| 263 |
// Cipher function |
| 264 |
buf = append(buf, byte(cipherFunc)) |
| 265 |
|
| 266 |
if version >= 5 { |
| 267 |
// AEAD mode |
| 268 |
buf = append(buf, byte(config.AEAD().Mode())) |
| 269 |
} |
| 270 |
if version > 5 { |
| 271 |
// Scalar octet count |
| 272 |
buf = append(buf, byte(len(s2kBytes))) |
| 273 |
} |
| 274 |
_, err = w.Write(buf) |
| 275 |
if err != nil { |
| 276 |
return |
| 277 |
} |
| 278 |
_, err = w.Write(s2kBytes) |
| 279 |
if err != nil { |
| 280 |
return |
| 281 |
} |
| 282 |
|
| 283 |
switch version { |
| 284 |
case 4: |
| 285 |
iv := make([]byte, cipherFunc.blockSize()) |
| 286 |
c := cipher.NewCFBEncrypter(cipherFunc.new(keyEncryptingKey), iv) |
| 287 |
encryptedCipherAndKey := make([]byte, keySize+1) |
| 288 |
c.XORKeyStream(encryptedCipherAndKey, buf[1:]) |
| 289 |
c.XORKeyStream(encryptedCipherAndKey[1:], sessionKey) |
| 290 |
_, err = w.Write(encryptedCipherAndKey) |
| 291 |
if err != nil { |
| 292 |
return |
| 293 |
} |
| 294 |
case 5, 6: |
| 295 |
mode := config.AEAD().Mode() |
| 296 |
adata := []byte{0xc3, byte(version), byte(cipherFunc), byte(mode)} |
| 297 |
var aead cipher.AEAD |
| 298 |
aead, err = getEncryptedKeyAeadInstance(cipherFunc, mode, keyEncryptingKey, adata, version) |
| 299 |
if err != nil { |
| 300 |
return |
| 301 |
} |
| 302 |
|
| 303 |
// Sample iv using random reader |
| 304 |
iv := make([]byte, config.AEAD().Mode().IvLength()) |
| 305 |
_, err = io.ReadFull(config.Random(), iv) |
| 306 |
if err != nil { |
| 307 |
return |
| 308 |
} |
| 309 |
// Seal and write (encryptedData includes auth. tag) |
| 310 |
|
| 311 |
encryptedData := aead.Seal(nil, iv, sessionKey, adata) |
| 312 |
_, err = w.Write(iv) |
| 313 |
if err != nil { |
| 314 |
return |
| 315 |
} |
| 316 |
_, err = w.Write(encryptedData) |
| 317 |
if err != nil { |
| 318 |
return |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
return |
| 323 |
} |
| 324 |
|
| 325 |
func getEncryptedKeyAeadInstance(c CipherFunction, mode AEADMode, inputKey, associatedData []byte, version int) (aead cipher.AEAD, err error) { |
| 326 |
var blockCipher cipher.Block |
| 327 |
if version > 5 { |
| 328 |
hkdfReader := hkdf.New(sha256.New, inputKey, []byte{}, associatedData) |
| 329 |
|
| 330 |
encryptionKey := make([]byte, c.KeySize()) |
| 331 |
_, _ = readFull(hkdfReader, encryptionKey) |
| 332 |
|
| 333 |
blockCipher = c.new(encryptionKey) |
| 334 |
} else { |
| 335 |
blockCipher = c.new(inputKey) |
| 336 |
} |
| 337 |
return mode.new(blockCipher) |
| 338 |
} |