Debian Codegraph @

openpgp/packet/aead_encrypted.go

raw
1 // Copyright (C) 2019 ProtonTech AG
2
3 package packet
4
5 import (
6 "io"
7 "strconv"
8
9 "github.com/ProtonMail/go-crypto/openpgp/errors"
10 "github.com/ProtonMail/go-crypto/openpgp/internal/algorithm"
11 )
12
13 // AEADEncrypted represents an AEAD Encrypted Packet.
14 // See https://www.ietf.org/archive/id/draft-koch-openpgp-2015-rfc4880bis-00.html#name-aead-encrypted-data-packet-t
15 type AEADEncrypted struct {
16 cipher CipherFunction
17 mode AEADMode
18 chunkSizeByte byte
19 Contents io.Reader // Encrypted chunks and tags
20 initialNonce []byte // Referred to as IV in RFC4880-bis
21 }
22
23 // Only currently defined version
24 const aeadEncryptedVersion = 1
25
26 func (ae *AEADEncrypted) parse(buf io.Reader) error {
27 headerData := make([]byte, 4)
28 if n, err := io.ReadFull(buf, headerData); n < 4 {
29 return errors.AEADError("could not read aead header:" + err.Error())
30 }
31 // Read initial nonce
32 mode := AEADMode(headerData[2])
33 nonceLen := mode.IvLength()
34
35 // This packet supports only EAX and OCB
36 // https://www.ietf.org/archive/id/draft-koch-openpgp-2015-rfc4880bis-00.html#name-aead-encrypted-data-packet-t
37 if nonceLen == 0 || mode > AEADModeOCB {
38 return errors.AEADError("unknown mode")
39 }
40
41 initialNonce := make([]byte, nonceLen)
42 if n, err := io.ReadFull(buf, initialNonce); n < nonceLen {
43 return errors.AEADError("could not read aead nonce:" + err.Error())
44 }
45 ae.Contents = buf
46 ae.initialNonce = initialNonce
47 c := headerData[1]
48 if _, ok := algorithm.CipherById[c]; !ok {
49 return errors.UnsupportedError("unknown cipher: " + string(c))
50 }
51 ae.cipher = CipherFunction(c)
52 ae.mode = mode
53 ae.chunkSizeByte = headerData[3]
54 if ae.chunkSizeByte > 16 {
55 return errors.UnsupportedError("invalid aead chunk size byte: " + strconv.Itoa(int(ae.chunkSizeByte)))
56 }
57 return nil
58 }
59
60 // Decrypt returns a io.ReadCloser from which decrypted bytes can be read, or
61 // an error.
62 func (ae *AEADEncrypted) Decrypt(ciph CipherFunction, key []byte) (io.ReadCloser, error) {
63 return ae.decrypt(key)
64 }
65
66 // decrypt prepares an aeadCrypter and returns a ReadCloser from which
67 // decrypted bytes can be read (see aeadDecrypter.Read()).
68 func (ae *AEADEncrypted) decrypt(key []byte) (io.ReadCloser, error) {
69 if ae.cipher.KeySize() != len(key) {
70 return nil, errors.StructuralError("invalid session key length for cipher: got " + strconv.Itoa(len(key)) + " bytes, but expected " + strconv.Itoa(ae.cipher.KeySize()) + " bytes")
71 }
72 blockCipher := ae.cipher.new(key)
73 aead, err := ae.mode.new(blockCipher)
74 if err != nil {
75 return nil, err
76 }
77
78 // Carry the first tagLen bytes
79 chunkSize := decodeAEADChunkSize(ae.chunkSizeByte)
80 tagLen := ae.mode.TagLength()
81 chunkBytes := make([]byte, chunkSize+tagLen*2)
82 peekedBytes := chunkBytes[chunkSize+tagLen:]
83 n, err := io.ReadFull(ae.Contents, peekedBytes)
84 if n < tagLen || (err != nil && err != io.EOF) {
85 return nil, errors.AEADError("Not enough data to decrypt:" + err.Error())
86 }
87
88 return &aeadDecrypter{
89 aeadCrypter: aeadCrypter{
90 aead: aead,
91 chunkSize: chunkSize,
92 nonce: ae.initialNonce,
93 associatedData: ae.associatedData(),
94 chunkIndex: make([]byte, 8),
95 packetTag: packetTypeAEADEncrypted,
96 },
97 reader: ae.Contents,
98 chunkBytes: chunkBytes,
99 peekedBytes: peekedBytes,
100 }, nil
101 }
102
103 // associatedData for chunks: tag, version, cipher, mode, chunk size byte
104 func (ae *AEADEncrypted) associatedData() []byte {
105 return []byte{
106 0xD4,
107 aeadEncryptedVersion,
108 byte(ae.cipher),
109 byte(ae.mode),
110 ae.chunkSizeByte}
111 }