| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title AesGcmEncryptor.cpp | ||
| 3 | // \author cadena and claradavisb | ||
| 4 | // \brief cpp file for AesGcmEncryptor component implementation class | ||
| 5 | // ====================================================================== | ||
| 6 | |||
| 7 | #include "Svc/Ccsds/AesGcmEncryptor/AesGcmEncryptor.hpp" | ||
| 8 | #include "Svc/Ccsds/Utils/SdlsAuthMask.hpp" | ||
| 9 | |||
| 10 | #include <openssl/crypto.h> | ||
| 11 | #include <openssl/evp.h> | ||
| 12 | #include <openssl/rand.h> | ||
| 13 | |||
| 14 | namespace Svc { | ||
| 15 | |||
| 16 | namespace Ccsds { | ||
| 17 | |||
| 18 | //! Length of the AES-GCM initialization vector, in bytes | ||
| 19 | static constexpr U32 GCM_IV_LEN = 12; | ||
| 20 | //! Length of the AES-GCM authentication tag (the SDLS MAC), in bytes | ||
| 21 | static constexpr U32 GCM_TAG_LEN = 16; | ||
| 22 | //! Length of an AES-256 key, in bytes | ||
| 23 | static constexpr FwSizeType AES_256_KEY_LEN = 32; | ||
| 24 | |||
| 25 | // ---------------------------------------------------------------------- | ||
| 26 | // Component construction and destruction | ||
| 27 | // ---------------------------------------------------------------------- | ||
| 28 | |||
| 29 | // Build the cipher state once so that encrypting a frame allocates nothing. | ||
| 30 | // Only the key and the IV change, and those are supplied per | ||
| 31 | // frame by a single EVP_EncryptInit_ex. | ||
| 32 | 13 | AesGcmEncryptor ::AesGcmEncryptor(const char* const compName) | |
| 33 | : AesGcmEncryptorComponentBase(compName), | ||
| 34 |
2/2✓ Branch 3 taken 13312 times.
✓ Branch 4 taken 13 times.
|
13325 | m_outBuf(), |
| 35 | 13 | m_bufferState(BufferOwnershipState::OWNED), | |
| 36 | 13 | m_cipher(nullptr), | |
| 37 | 13 | m_ctx(nullptr), | |
| 38 | 13 | m_aad(0, 0), | |
| 39 | 13 | m_aadVcId(0), | |
| 40 | 26 | m_aadSaIndex(0) { | |
| 41 |
1/1✓ Branch 3 taken 13 times.
|
13 | this->m_cipher = EVP_CIPHER_fetch(nullptr, "AES-256-GCM", nullptr); |
| 42 | 13 | FW_ASSERT(this->m_cipher != nullptr); | |
| 43 |
1/1✓ Branch 3 taken 13 times.
|
13 | this->m_ctx = EVP_CIPHER_CTX_new(); |
| 44 | 13 | FW_ASSERT(this->m_ctx != nullptr); | |
| 45 | |||
| 46 |
1/1✓ Branch 9 taken 13 times.
|
13 | int status = EVP_EncryptInit_ex(this->m_ctx, this->m_cipher, nullptr, nullptr, nullptr); |
| 47 | 13 | FW_ASSERT(status == 1, static_cast<FwAssertArgType>(status)); | |
| 48 |
1/1✓ Branch 5 taken 13 times.
|
13 | status = EVP_CIPHER_CTX_ctrl(this->m_ctx, EVP_CTRL_GCM_SET_IVLEN, static_cast<int>(GCM_IV_LEN), nullptr); |
| 49 | 13 | FW_ASSERT(status == 1, static_cast<FwAssertArgType>(status)); | |
| 50 | 13 | } | |
| 51 | |||
| 52 | 26 | AesGcmEncryptor ::~AesGcmEncryptor() { | |
| 53 | 26 | EVP_CIPHER_CTX_free(this->m_ctx); | |
| 54 | 26 | EVP_CIPHER_free(this->m_cipher); | |
| 55 | 26 | } | |
| 56 | |||
| 57 | // ---------------------------------------------------------------------- | ||
| 58 | // Handler implementations for typed input ports | ||
| 59 | // ---------------------------------------------------------------------- | ||
| 60 | |||
| 61 | 19 | void AesGcmEncryptor ::encryptIn_handler(FwIndexType portNum, | |
| 62 | U16 securityAssociationIndex, | ||
| 63 | Fw::Buffer& data, | ||
| 64 | const ComCfg::FrameContext& context) { | ||
| 65 | 19 | FW_ASSERT(this->m_ctx != nullptr); | |
| 66 | |||
| 67 | // The previous frame is still downstream and m_outBuf holds ciphertext that has not been | ||
| 68 | // sent. Overwriting it would substitute this frame's contents into the one already in | ||
| 69 | // flight, and the MAC would cover the substitution, so the ground would accept it. Drop | ||
| 70 | // this frame instead | ||
| 71 |
2/2✓ Branch 4 taken 1 times.
✓ Branch 5 taken 18 times.
|
19 | if (this->m_bufferState != BufferOwnershipState::OWNED) { |
| 72 |
1/1✓ Branch 5 taken 1 times.
|
1 | this->log_WARNING_HI_OutputBufferBusy(); |
| 73 |
2/2✓ Branch 5 taken 1 times.
✓ Branch 8 taken 1 times.
|
1 | this->failFrame(data, context, Svc::Ccsds::SdlsStatus::ENCRYPTION_FAILURE); |
| 74 | 1 | return; | |
| 75 | } | ||
| 76 | |||
| 77 | // m_outBuf layout: IV (12) | ciphertext (N) | MAC (16) | ||
| 78 |
1/1✓ Branch 4 taken 18 times.
|
18 | const FwSizeType requiredSize = static_cast<FwSizeType>(data.getSize()) + GCM_IV_LEN + GCM_TAG_LEN; |
| 79 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17 times.
|
18 | if (requiredSize > MAX_OUTPUT_SIZE) { |
| 80 |
2/2✓ Branch 5 taken 1 times.
✓ Branch 8 taken 1 times.
|
1 | this->failFrame(data, context, Svc::Ccsds::SdlsStatus::ENCRYPTION_FAILURE); |
| 81 | 1 | return; | |
| 82 | } | ||
| 83 | |||
| 84 |
1/1✓ Branch 2 taken 17 times.
|
17 | Svc::Ccsds::SdlsKeyBuffer key; |
| 85 |
1/1✓ Branch 3 taken 17 times.
|
17 | const Svc::Ccsds::SdlsStatus keyStatus = this->keyGet_out(0, securityAssociationIndex, key); |
| 86 |
7/7✓ Branch 2 taken 16 times.
✓ Branch 3 taken 1 times.
✓ Branch 6 taken 16 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 15 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 15 times.
|
17 | if ((keyStatus != Svc::Ccsds::SdlsStatus::SUCCESS) || (key.getSize() != AES_256_KEY_LEN)) { |
| 87 |
2/2✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
|
2 | this->failFrame(data, context, Svc::Ccsds::SdlsStatus::KEY_ERROR); |
| 88 | 2 | return; | |
| 89 | } | ||
| 90 | |||
| 91 | 15 | U8* const iv = this->m_outBuf; | |
| 92 | 15 | U8* const ciphertext = this->m_outBuf + GCM_IV_LEN; | |
| 93 | |||
| 94 | // A repeated IV under one key breaks GCM, so this generates a new one per frame | ||
| 95 |
2/3✓ Branch 1 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
|
15 | if (RAND_bytes(iv, static_cast<int>(GCM_IV_LEN)) != 1) { |
| 96 | ✗ | this->failFrame(data, context, Svc::Ccsds::SdlsStatus::ENCRYPTION_FAILURE); | |
| 97 | ✗ | return; | |
| 98 | } | ||
| 99 | |||
| 100 | 15 | int len = 0; | |
| 101 | 15 | int cipherLen = 0; | |
| 102 | // The AAD mask depends only on the VC and the SA, so it is rebuilt when either changes | ||
| 103 | 15 | const U8 vcId = context.get_vcId(); | |
| 104 |
3/4✓ Branch 4 taken 5 times.
✓ Branch 5 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 5 times.
|
15 | if ((vcId != this->m_aadVcId) || (securityAssociationIndex != this->m_aadSaIndex)) { |
| 105 | 10 | this->m_aad = Svc::Ccsds::Utils::SdlsTmAuthMask(vcId, securityAssociationIndex); | |
| 106 | 10 | this->m_aadVcId = vcId; | |
| 107 | 10 | this->m_aadSaIndex = securityAssociationIndex; | |
| 108 | } | ||
| 109 | |||
| 110 | // Re-keying the context the constructor built. Passing a null cipher here reuses the | ||
| 111 | // algorithm and IV length already set, which is what keeps this path allocation-free. | ||
| 112 | // The first EVP_EncryptUpdate has a null output, so its input is absorbed as AAD; it must precede the plaintext | ||
| 113 | bool encryptSucceeded = | ||
| 114 |
1/1✓ Branch 7 taken 15 times.
|
15 | (EVP_EncryptInit_ex(this->m_ctx, nullptr, nullptr, key.getBuffAddr(), iv) == 1) && |
| 115 |
2/3✓ Branch 7 taken 15 times.
✓ Branch 9 taken 15 times.
✗ Branch 10 not taken.
|
15 | (EVP_EncryptUpdate(this->m_ctx, nullptr, &len, this->m_aad.bytes, |
| 116 |
1/2✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
|
30 | static_cast<int>(sizeof(this->m_aad.bytes))) == 1) && |
| 117 |
4/5✓ Branch 4 taken 15 times.
✓ Branch 10 taken 15 times.
✓ Branch 17 taken 15 times.
✓ Branch 19 taken 15 times.
✗ Branch 20 not taken.
|
15 | (EVP_EncryptUpdate(this->m_ctx, ciphertext, &len, data.getData(), static_cast<int>(data.getSize())) == 1); |
| 118 | // The cipher context holds the key schedule now, so the stack copy is dead. | ||
| 119 | // OPENSSL_cleanse wipes the stack copy to prevent it from being recovered by a memory dump or other attack. | ||
| 120 |
1/1✓ Branch 5 taken 15 times.
|
15 | OPENSSL_cleanse(key.getBuffAddr(), key.getCapacity()); |
| 121 |
1/2✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
|
15 | if (encryptSucceeded) { |
| 122 | 15 | cipherLen = len; | |
| 123 |
1/1✓ Branch 6 taken 15 times.
|
15 | encryptSucceeded = (EVP_EncryptFinal_ex(this->m_ctx, ciphertext + cipherLen, &len) == 1); |
| 124 | } | ||
| 125 |
1/2✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
|
15 | if (encryptSucceeded) { |
| 126 | 15 | cipherLen += len; | |
| 127 | // The MAC is written straight into its place in m_outBuf, after the ciphertext | ||
| 128 |
1/1✓ Branch 5 taken 15 times.
|
15 | encryptSucceeded = (EVP_CIPHER_CTX_ctrl(this->m_ctx, EVP_CTRL_GCM_GET_TAG, static_cast<int>(GCM_TAG_LEN), |
| 129 | 15 | ciphertext + cipherLen) == 1); | |
| 130 | } | ||
| 131 | |||
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (!encryptSucceeded) { |
| 133 | ✗ | this->failFrame(data, context, Svc::Ccsds::SdlsStatus::ENCRYPTION_FAILURE); | |
| 134 | ✗ | return; | |
| 135 | } | ||
| 136 | |||
| 137 | 15 | const U32 outLen = GCM_IV_LEN + static_cast<U32>(cipherLen) + GCM_TAG_LEN; | |
| 138 |
1/1✓ Branch 4 taken 15 times.
|
15 | Fw::Buffer cipherBuf(this->m_outBuf, outLen); |
| 139 | |||
| 140 | // Marked in flight before it is emitted, | ||
| 141 | // so that a subsequent frame arriving before the return of this one is dropped | ||
| 142 | 15 | this->m_bufferState = BufferOwnershipState::NOT_OWNED; | |
| 143 | |||
| 144 | // The plaintext has been copied; hand it back before passing the ciphertext downstream | ||
| 145 |
1/1✓ Branch 5 taken 15 times.
|
15 | this->bufferReturnOut_out(0, data, context); |
| 146 |
2/2✓ Branch 6 taken 15 times.
✓ Branch 10 taken 15 times.
|
15 | this->encryptOut_out(0, Svc::Ccsds::SdlsStatus::SUCCESS, cipherBuf, context); |
| 147 | 19 | } | |
| 148 | |||
| 149 | 8 | void AesGcmEncryptor ::encryptReturnIn_handler(FwIndexType portNum, | |
| 150 | Fw::Buffer& data, | ||
| 151 | const ComCfg::FrameContext& context) { | ||
| 152 | // A failed frame reported its status with an empty buffer | ||
| 153 |
2/2✓ Branch 4 taken 1 times.
✓ Branch 5 taken 7 times.
|
8 | if (!data.isValid()) { |
| 154 | 1 | return; | |
| 155 | } | ||
| 156 | // Only m_outBuf is ever emitted on encryptOut, so anything else is a wiring error | ||
| 157 | 7 | FW_ASSERT(data.getData() == this->m_outBuf); | |
| 158 | 7 | FW_ASSERT(this->m_bufferState == BufferOwnershipState::NOT_OWNED, | |
| 159 | static_cast<FwAssertArgType>(this->m_bufferState)); | ||
| 160 | 7 | this->m_bufferState = BufferOwnershipState::OWNED; | |
| 161 | } | ||
| 162 | |||
| 163 | // ---------------------------------------------------------------------- | ||
| 164 | // Helpers | ||
| 165 | // ---------------------------------------------------------------------- | ||
| 166 | |||
| 167 | 4 | void AesGcmEncryptor ::failFrame(Fw::Buffer& data, const ComCfg::FrameContext& context, Svc::Ccsds::SdlsStatus status) { | |
| 168 | // Return the plaintext to its sender | ||
| 169 |
1/1✓ Branch 5 taken 4 times.
|
4 | this->bufferReturnOut_out(0, data, context); |
| 170 | // Svc::Ccsds::CcsdsSdlsFramer still needs a status on encryptIn to release the com status | ||
| 171 | // So a failed frame reports one carrying no data rather than nothing. | ||
| 172 |
1/1✓ Branch 2 taken 4 times.
|
4 | Fw::Buffer empty; |
| 173 |
1/1✓ Branch 5 taken 4 times.
|
4 | this->encryptOut_out(0, status, empty, context); |
| 174 | 8 | } | |
| 175 | |||
| 176 | } // namespace Ccsds | ||
| 177 | |||
| 178 | } // namespace Svc | ||
| 179 |