7#include <packager/media/base/aes_key_wrap.h>
13#include <absl/log/check.h>
14#include <absl/log/log.h>
15#include <mbedtls/nist_kw.h>
21bool IsValidWrappingKeySize(
size_t size) {
22 return size == 16 || size == 24 || size == 32;
27bool AesKeyUnwrap(
const std::vector<uint8_t>& wrapping_key,
28 const std::vector<uint8_t>& wrapped_data,
29 std::vector<uint8_t>* data) {
31 if (!IsValidWrappingKeySize(wrapping_key.size())) {
32 LOG(ERROR) <<
"Invalid AES key wrap key size: " << wrapping_key.size();
36 mbedtls_nist_kw_context context;
37 mbedtls_nist_kw_init(&context);
38 int rv = mbedtls_nist_kw_setkey(
39 &context, MBEDTLS_CIPHER_ID_AES, wrapping_key.data(),
40 static_cast<unsigned>(wrapping_key.size()) * 8,
43 LOG(ERROR) <<
"AES key unwrap setkey failed: " << rv;
44 mbedtls_nist_kw_free(&context);
48 data->resize(wrapped_data.size());
49 size_t output_size = 0;
50 rv = mbedtls_nist_kw_unwrap(&context, MBEDTLS_KW_MODE_KW, wrapped_data.data(),
51 wrapped_data.size(), data->data(), &output_size,
53 mbedtls_nist_kw_free(&context);
55 LOG(ERROR) <<
"AES key unwrap failed: " << rv;
58 data->resize(output_size);
All the methods that are virtual are virtual for mocking.