No-Typo-Check: From a third party header file Bug: 260918793 Test: unit tests Test: atp v2/widevine-eng/drm_compliance Change-Id: I36effd6a10a99bdb2399ab1f4a0fad026d607c70
64 lines
1.9 KiB
C++
64 lines
1.9 KiB
C++
// Copyright 2022 Google LLC. All Rights Reserved. This file and proprietary
|
|
// source code may only be used and distributed under the Widevine License
|
|
// Agreement.
|
|
//
|
|
// Implements utility functions for serializing and deserializing the fake key
|
|
// handles used by the Ref and Testbed.
|
|
//
|
|
#ifndef WVOEC_UTIL_KEY_HANDLE_H_
|
|
#define WVOEC_UTIL_KEY_HANDLE_H_
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include "OEMCryptoCENC.h"
|
|
#include "log.h"
|
|
|
|
namespace wvoec {
|
|
namespace util {
|
|
// Size of a key handle, which for this implementation is just a session ID.
|
|
constexpr size_t kKeyHandleSize = sizeof(OEMCrypto_SESSION);
|
|
|
|
OEMCryptoResult SerializeSessionToKeyHandle(OEMCrypto_SESSION session,
|
|
uint8_t* key_handle,
|
|
size_t* key_handle_length) {
|
|
if (key_handle_length == nullptr) {
|
|
LOGE("Null key handle length");
|
|
return OEMCrypto_ERROR_INVALID_CONTEXT;
|
|
}
|
|
|
|
if (key_handle == nullptr || *key_handle_length < kKeyHandleSize) {
|
|
*key_handle_length = kKeyHandleSize;
|
|
return OEMCrypto_ERROR_SHORT_BUFFER;
|
|
}
|
|
|
|
*key_handle_length = kKeyHandleSize;
|
|
memcpy(key_handle, &session, kKeyHandleSize);
|
|
return OEMCrypto_SUCCESS;
|
|
}
|
|
|
|
OEMCryptoResult DeserializeKeyHandleToSession(const uint8_t* key_handle,
|
|
size_t key_handle_length,
|
|
OEMCrypto_SESSION* session) {
|
|
if (key_handle == nullptr) {
|
|
LOGE("Null key handle");
|
|
return OEMCrypto_ERROR_INVALID_CONTEXT;
|
|
}
|
|
if (session == nullptr) {
|
|
LOGE("Null session");
|
|
return OEMCrypto_ERROR_INVALID_CONTEXT;
|
|
}
|
|
|
|
if (key_handle_length != kKeyHandleSize) {
|
|
LOGE("Invalid key handle length");
|
|
return OEMCrypto_ERROR_INVALID_CONTEXT;
|
|
}
|
|
|
|
memcpy(session, key_handle, kKeyHandleSize);
|
|
return OEMCrypto_SUCCESS;
|
|
}
|
|
} // namespace util
|
|
} // namespace wvoec
|
|
#endif // WVOEC_UTIL_KEY_HANDLE_H_
|