Files
oemcrypto/oemcrypto/opk/oemcrypto_ta/wtpi_reference/ecc_util.h
Fred Gylys-Colwell 0a16cb2594 Version 17 plus test updates and OPK v17
This is the first public release of OPK v17.
See the file CHANGELOG.md for details.
2022-04-13 19:36:27 -07:00

76 lines
3.7 KiB
C

/* Copyright 2021 Google LLC. All Rights Reserved. This file and proprietary
source code may only be used and distributed under the Widevine
License Agreement. */
#ifndef OEMCRYPTO_TA_ECC_UTIL_H_
#define OEMCRYPTO_TA_ECC_UTIL_H_
#include "stdbool.h"
#include "stdint.h"
#include "openssl/ec.h"
/* Checks to see that |ecc_key| is a valid ECC key. Returns false if not a
valid key.
Caller retains ownership of |ecc_key| and it must not be NULL. */
bool CheckECCKey(const EC_KEY* ecc_key);
/* Attempts to deserialize |size| bytes of |serialized_bytes| into an ECC
key and store the result in |ecc_key|.
The provided |serialized_bytes| must contain a valid ASN.1 DER encoded
PKCS8 PrivateKeyInfo containing an ECPrivateKey using named curves
(non-parameterized). Only supported curves by this API are secp256r1
(required for ECC), secp384r1 (optional) and secp521r1 (optional).
Note: If the public key is not included, then it is computed from
the private key.
Returns false if |serialized_bytes| can not be deserialized.
Caller retains ownership of all pointers and they must not be NULL. */
bool DeserializeECCPrivateKey(const uint8_t* serialized_bytes, size_t size,
EC_KEY** ecc_key);
/* Signs |message_length| bytes of |message| using |ecc_key| using ECDSA
as defined in SEC.1. The hash algorithm used depends on which curve
is used by |ecc_key|:
- SHA-256 / secp256r1 (required)
- SHA-384 / secp384r1 (optional support)
- SHA-512 / secp521r1 (optional support)
The output |signature| will be an ASN.1 DER encoded ECDSA-Sig-Value.
Returns false if the signature could not be computed.
|message_length| must be > 0 and *|signature_length| must be larger
or equal to ECDSA_size(ecc_key).
Note: It is common that the final ECDSA signature to be slightly
smaller than indicated by ECDSA_size(ecc_key), check *|signature_length|
if length has been truncated.
Caller retains ownership of all pointers and they must not be NULL. */
bool ECCSignECDSA(EC_KEY* ecc_key, const uint8_t* message,
size_t message_length, uint8_t* signature,
size_t* signature_length);
/* Derives the OEMCrypto session key used for deriving other keys.
The provided |key_source| should be the value provided to OEMCrypto
by OEMCrypto_DeriveKeysFromSessionKey().
For ECC based DRM certificates, |key_source| will be an ASN.1 DER
encoded SubjectPublicKey containing an ephemeral ECC public key
used for deriving the session key via ECDH using a Widevine-specific
Key Derivation Function (KDF). The Widevine KDF is simply a SHA-256
applied to the raw ECDH shared secret.
The output |session_key| will be an AES-256 to be used for
CMAC-AES-256 algorithm. *|session_key_length| should be at least 32
bytes.
Returns false if the provided |key_source| is not a properly encoded
ECC public key, or it belongs to a curve different than |ecc_key|.
Caller retains ownership of all pointers and they must not be NULL. */
bool ECCWidevineECDHSessionKey(EC_KEY* ecc_key, const uint8_t* key_source,
size_t key_source_length, uint8_t* session_key,
size_t* session_key_length);
/**
* Generates a new pair of serialized EC SECP256R1 keys. The serialization
* format is ASN.1 DER encoded PKCS8 PrivateKeyInfo/SubjectPublicKeyInfo.
* |private_key_data_length| and |public_key_data_length| indicate the buffer
* size, and will be updated to the actual size used. */
bool NewEccKeyPair(uint8_t* private_key_data, size_t* private_key_data_length,
uint8_t* public_key_data, size_t* public_key_data_length);
#endif /* OEMCRYPTO_TA_ECC_UTIL_H_ */