| 1 |
// Copyright 2023 Proton AG. 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 |
"crypto/cipher" |
| 9 |
"crypto/sha256" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"strconv" |
| 13 |
|
| 14 |
"github.com/ProtonMail/go-crypto/openpgp/errors" |
| 15 |
"golang.org/x/crypto/hkdf" |
| 16 |
) |
| 17 |
|
| 18 |
// parseAead parses a V2 SEIPD packet (AEAD) as specified in |
| 19 |
// https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-07.html#section-5.13.2 |
| 20 |
func (se *SymmetricallyEncrypted) parseAead(r io.Reader) error { |
| 21 |
headerData := make([]byte, 3) |
| 22 |
if n, err := io.ReadFull(r, headerData); n < 3 { |
| 23 |
return errors.StructuralError("could not read aead header: " + err.Error()) |
| 24 |
} |
| 25 |
|
| 26 |
// Cipher |
| 27 |
se.Cipher = CipherFunction(headerData[0]) |
| 28 |
// cipherFunc must have block size 16 to use AEAD |
| 29 |
if se.Cipher.blockSize() != 16 { |
| 30 |
return errors.UnsupportedError("invalid aead cipher: " + strconv.Itoa(int(se.Cipher))) |
| 31 |
} |
| 32 |
|
| 33 |
// Mode |
| 34 |
se.Mode = AEADMode(headerData[1]) |
| 35 |
if se.Mode.TagLength() == 0 { |
| 36 |
return errors.UnsupportedError("unknown aead mode: " + strconv.Itoa(int(se.Mode))) |
| 37 |
} |
| 38 |
|
| 39 |
// Chunk size |
| 40 |
se.ChunkSizeByte = headerData[2] |
| 41 |
if se.ChunkSizeByte > 16 { |
| 42 |
return errors.UnsupportedError("invalid aead chunk size byte: " + strconv.Itoa(int(se.ChunkSizeByte))) |
| 43 |
} |
| 44 |
|
| 45 |
// Salt |
| 46 |
if n, err := io.ReadFull(r, se.Salt[:]); n < aeadSaltSize { |
| 47 |
return errors.StructuralError("could not read aead salt: " + err.Error()) |
| 48 |
} |
| 49 |
|
| 50 |
return nil |
| 51 |
} |
| 52 |
|
| 53 |
// associatedData for chunks: tag, version, cipher, mode, chunk size byte |
| 54 |
func (se *SymmetricallyEncrypted) associatedData() []byte { |
| 55 |
return []byte{ |
| 56 |
0xD2, |
| 57 |
symmetricallyEncryptedVersionAead, |
| 58 |
byte(se.Cipher), |
| 59 |
byte(se.Mode), |
| 60 |
se.ChunkSizeByte, |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// decryptAead decrypts a V2 SEIPD packet (AEAD) as specified in |
| 65 |
// https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-07.html#section-5.13.2 |
| 66 |
func (se *SymmetricallyEncrypted) decryptAead(inputKey []byte) (io.ReadCloser, error) { |
| 67 |
if se.Cipher.KeySize() != len(inputKey) { |
| 68 |
return nil, errors.StructuralError(fmt.Sprintf("invalid session key length for cipher: got %d bytes, but expected %d bytes", len(inputKey), se.Cipher.KeySize())) |
| 69 |
} |
| 70 |
|
| 71 |
aead, nonce, err := getSymmetricallyEncryptedAeadInstance(se.Cipher, se.Mode, inputKey, se.Salt[:], se.associatedData()) |
| 72 |
if err != nil { |
| 73 |
return nil, err |
| 74 |
} |
| 75 |
|
| 76 |
// Carry the first tagLen bytes |
| 77 |
chunkSize := decodeAEADChunkSize(se.ChunkSizeByte) |
| 78 |
tagLen := se.Mode.TagLength() |
| 79 |
chunkBytes := make([]byte, chunkSize+tagLen*2) |
| 80 |
peekedBytes := chunkBytes[chunkSize+tagLen:] |
| 81 |
n, err := io.ReadFull(se.Contents, peekedBytes) |
| 82 |
if n < tagLen || (err != nil && err != io.EOF) { |
| 83 |
return nil, errors.StructuralError("not enough data to decrypt:" + err.Error()) |
| 84 |
} |
| 85 |
|
| 86 |
return &aeadDecrypter{ |
| 87 |
aeadCrypter: aeadCrypter{ |
| 88 |
aead: aead, |
| 89 |
chunkSize: decodeAEADChunkSize(se.ChunkSizeByte), |
| 90 |
nonce: nonce, |
| 91 |
associatedData: se.associatedData(), |
| 92 |
chunkIndex: nonce[len(nonce)-8:], |
| 93 |
packetTag: packetTypeSymmetricallyEncryptedIntegrityProtected, |
| 94 |
}, |
| 95 |
reader: se.Contents, |
| 96 |
chunkBytes: chunkBytes, |
| 97 |
peekedBytes: peekedBytes, |
| 98 |
}, nil |
| 99 |
} |
| 100 |
|
| 101 |
// serializeSymmetricallyEncryptedAead encrypts to a writer a V2 SEIPD packet (AEAD) as specified in |
| 102 |
// https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-07.html#section-5.13.2 |
| 103 |
func serializeSymmetricallyEncryptedAead(ciphertext io.WriteCloser, cipherSuite CipherSuite, chunkSizeByte byte, rand io.Reader, inputKey []byte) (Contents io.WriteCloser, err error) { |
| 104 |
// cipherFunc must have block size 16 to use AEAD |
| 105 |
if cipherSuite.Cipher.blockSize() != 16 { |
| 106 |
return nil, errors.InvalidArgumentError("invalid aead cipher function") |
| 107 |
} |
| 108 |
|
| 109 |
if cipherSuite.Cipher.KeySize() != len(inputKey) { |
| 110 |
return nil, errors.InvalidArgumentError("error in aead serialization: bad key length") |
| 111 |
} |
| 112 |
|
| 113 |
// Data for en/decryption: tag, version, cipher, aead mode, chunk size |
| 114 |
prefix := []byte{ |
| 115 |
0xD2, |
| 116 |
symmetricallyEncryptedVersionAead, |
| 117 |
byte(cipherSuite.Cipher), |
| 118 |
byte(cipherSuite.Mode), |
| 119 |
chunkSizeByte, |
| 120 |
} |
| 121 |
|
| 122 |
// Write header (that correspond to prefix except first byte) |
| 123 |
n, err := ciphertext.Write(prefix[1:]) |
| 124 |
if err != nil || n < 4 { |
| 125 |
return nil, err |
| 126 |
} |
| 127 |
|
| 128 |
// Random salt |
| 129 |
salt := make([]byte, aeadSaltSize) |
| 130 |
if _, err := io.ReadFull(rand, salt); err != nil { |
| 131 |
return nil, err |
| 132 |
} |
| 133 |
|
| 134 |
if _, err := ciphertext.Write(salt); err != nil { |
| 135 |
return nil, err |
| 136 |
} |
| 137 |
|
| 138 |
aead, nonce, err := getSymmetricallyEncryptedAeadInstance(cipherSuite.Cipher, cipherSuite.Mode, inputKey, salt, prefix) |
| 139 |
if err != nil { |
| 140 |
return nil, err |
| 141 |
} |
| 142 |
|
| 143 |
chunkSize := decodeAEADChunkSize(chunkSizeByte) |
| 144 |
tagLen := aead.Overhead() |
| 145 |
chunkBytes := make([]byte, chunkSize+tagLen) |
| 146 |
return &aeadEncrypter{ |
| 147 |
aeadCrypter: aeadCrypter{ |
| 148 |
aead: aead, |
| 149 |
chunkSize: chunkSize, |
| 150 |
associatedData: prefix, |
| 151 |
nonce: nonce, |
| 152 |
chunkIndex: nonce[len(nonce)-8:], |
| 153 |
packetTag: packetTypeSymmetricallyEncryptedIntegrityProtected, |
| 154 |
}, |
| 155 |
writer: ciphertext, |
| 156 |
chunkBytes: chunkBytes, |
| 157 |
}, nil |
| 158 |
} |
| 159 |
|
| 160 |
func getSymmetricallyEncryptedAeadInstance(c CipherFunction, mode AEADMode, inputKey, salt, associatedData []byte) (aead cipher.AEAD, nonce []byte, err error) { |
| 161 |
hkdfReader := hkdf.New(sha256.New, inputKey, salt, associatedData) |
| 162 |
|
| 163 |
encryptionKey := make([]byte, c.KeySize()) |
| 164 |
_, _ = readFull(hkdfReader, encryptionKey) |
| 165 |
|
| 166 |
blockCipher := c.new(encryptionKey) |
| 167 |
aead, err = mode.new(blockCipher) |
| 168 |
if err != nil { |
| 169 |
return nil, nil, err |
| 170 |
} |
| 171 |
|
| 172 |
nonce = make([]byte, mode.IvLength()) |
| 173 |
|
| 174 |
// Last 64 bits of nonce are the counter |
| 175 |
_, _ = readFull(hkdfReader, nonce[:len(nonce)-8]) |
| 176 |
|
| 177 |
return |
| 178 |
} |