ae_error_t CertificateProvisioningProtocol::msg1_create_seq3_0(const TLVsMsg& seq3_1_tlv_epid_gid, const provision_request_header_t& serializedHeader, const upse::Buffer& ek1, TLVsMsg& seq3_0_tlv_block_cipher_text, upse::Buffer& mac) { //* 3.0 Block Cipher Text TLV (TLV Type, Type, Version, Size, [IV, EncryptedPayload is 3.1]) ae_error_t status = AE_FAILURE; tlv_status_t tlv_status; do { status = get_random_value(IV_SIZE, M1IV); if (AE_FAILED(status)) break; upse::Buffer aad; status = aad.Alloc(sizeof(serializedHeader)); if (AE_FAILED(status)) break; upse::BufferWriter aadWriter(aad); status = aadWriter.writeRaw((const uint8_t*)&serializedHeader, sizeof(serializedHeader)); if (AE_FAILED(status)) break; upse::Buffer epidGid; status = epidGid.Alloc(seq3_1_tlv_epid_gid.get_tlv_msg(), seq3_1_tlv_epid_gid.get_tlv_msg_size()); if (AE_FAILED(status)) break; upse::Buffer encryptedPayload; status = aesGCMEncrypt(M1IV, ek1, epidGid, aad, encryptedPayload, mac); if (AE_FAILED(status)) break; tlv_status = seq3_0_tlv_block_cipher_text.add_block_cipher_text(M1IV.getData(), encryptedPayload.getData(), encryptedPayload.getSize()); status = tlv_error_2_pve_error(tlv_status); if (AE_FAILED(status)) break; status = AE_SUCCESS; } while (0); return status; }
ae_error_t CertificateProvisioningProtocol::msg3_seq3_0_create_block_cipher_text_tlv(const TLVsMsg& quote, const TLVsMsg& epidSigTLV, const TLVsMsg& csrTLV, const TLVsMsg& nonceTLV, const provision_request_header_t& requestHeader, const upse::Buffer& ek2, TLVsMsg& blockCipherTextTLV, upse::Buffer& mac) { ae_error_t status = AE_FAILURE; tlv_status_t tlv_status; upse::Buffer plainText; upse::Buffer encryptedPayload; do { status = get_random_value(IV_SIZE, M3IV); if (AE_FAILED(status)) break; status = plainText.Alloc(quote.get_tlv_msg_size() + epidSigTLV.get_tlv_msg_size() + csrTLV.get_tlv_msg_size()); if (AE_FAILED(status)) break; upse::BufferWriter plainTextWriter(plainText); status = plainTextWriter.writeRaw(quote.get_tlv_msg(), quote.get_tlv_msg_size()); if (AE_FAILED(status)) break; status = plainTextWriter.writeRaw(epidSigTLV.get_tlv_msg(), epidSigTLV.get_tlv_msg_size()); if (AE_FAILED(status)) break; status = plainTextWriter.writeRaw(csrTLV.get_tlv_msg(), csrTLV.get_tlv_msg_size()); if (AE_FAILED(status)) break; uint32_t payloadSize = BLOCK_CIPHER_TEXT_TLV_PAYLOAD_SIZE(plainText.getSize()); uint32_t blockCipherTextHeaderSize = get_tlv_total_size(payloadSize) - payloadSize; // Calculate AAD (concatenation of Request header, Nonce, Block Cipher Text TLV header and IV from Block Cipher Text TLV) upse::Buffer aad; status = aad.Alloc(static_cast<uint32_t>(sizeof(requestHeader) + nonceTLV.get_tlv_msg_size() + blockCipherTextHeaderSize + M3IV.getSize())); if (AE_FAILED(status)) break; TLVsMsg tmpBlockCipherTextTLV; tlv_status = tmpBlockCipherTextTLV.add_block_cipher_text(M3IV.getData(), NULL, plainText.getSize()); status = tlv_error_2_pve_error(tlv_status); if (AE_FAILED(status)) break; upse::BufferWriter aadWriter(aad); status = aadWriter.writeRaw((const uint8_t*)&requestHeader, sizeof(requestHeader)); if (AE_FAILED(status)) break; status = aadWriter.writeRaw(nonceTLV.get_tlv_msg(), nonceTLV.get_tlv_msg_size()); if (AE_FAILED(status)) break; status = aadWriter.writeRaw(tmpBlockCipherTextTLV.get_tlv_msg(), blockCipherTextHeaderSize); if (AE_FAILED(status)) break; status = aadWriter.writeRaw(M3IV.getData(), M3IV.getSize()); if (AE_FAILED(status)) break; status = aesGCMEncrypt(M3IV, ek2, plainText, aad, encryptedPayload, mac); if (AE_FAILED(status)) break; tlv_status = blockCipherTextTLV.add_block_cipher_text(M3IV.getData(), encryptedPayload.getData(), encryptedPayload.getSize()); status = tlv_error_2_pve_error(tlv_status); if (AE_FAILED(status)) break; status = AE_SUCCESS; } while (0); return status; }