/** * Copyright Notice: * Copyright 2021-2022 DMTF. All rights reserved. * License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md **/ /** @file * Shang-Mi2 Asymmetric Wrapper Implementation. **/ #include "internal_crypt_lib.h" /** * Allocates and Initializes one Shang-Mi2 context for subsequent use. * * The key is generated before the function returns. * * @param nid cipher NID * * @return Pointer to the Shang-Mi2 context that has been initialized. * If the allocations fails, sm2_new_by_nid() returns NULL. * **/ void *libspdm_sm2_dsa_new_by_nid(size_t nid) { return NULL; } /** * Release the specified sm2 context. * * @param[in] sm2_context Pointer to the sm2 context to be released. * **/ void libspdm_sm2_dsa_free(void *sm2_context) { } /** * Sets the public key component into the established sm2 context. * * The public_size is 64. first 32-byte is X, second 32-byte is Y. * * @param[in, out] ec_context Pointer to sm2 context being set. * @param[in] public Pointer to the buffer to receive generated public X,Y. * @param[in] public_size The size of public buffer in bytes. * * @retval true sm2 public key component was set successfully. * @retval false Invalid sm2 public key component. * **/ bool libspdm_sm2_dsa_set_pub_key(void *sm2_context, const uint8_t *public_key, size_t public_key_size) { return false; } /** * Gets the public key component from the established sm2 context. * * The public_size is 64. first 32-byte is X, second 32-byte is Y. * * @param[in, out] sm2_context Pointer to sm2 context being set. * @param[out] public Pointer to the buffer to receive generated public X,Y. * @param[in, out] public_size On input, the size of public buffer in bytes. * On output, the size of data returned in public buffer in bytes. * * @retval true sm2 key component was retrieved successfully. * @retval false Invalid sm2 key component. * **/ bool libspdm_sm2_dsa_get_pub_key(void *sm2_context, uint8_t *public_key, size_t *public_key_size) { return false; } /** * Validates key components of sm2 context. * NOTE: This function performs integrity checks on all the sm2 key material, so * the sm2 key structure must contain all the private key data. * * If sm2_context is NULL, then return false. * * @param[in] sm2_context Pointer to sm2 context to check. * * @retval true sm2 key components are valid. * @retval false sm2 key components are not valid. * **/ bool libspdm_sm2_dsa_check_key(const void *sm2_context) { return false; } /** * Generates sm2 key and returns sm2 public key (X, Y), based upon GB/T 32918.3-2016: SM2 - Part3. * * This function generates random secret, and computes the public key (X, Y), which is * returned via parameter public, public_size. * X is the first half of public with size being public_size / 2, * Y is the second half of public with size being public_size / 2. * sm2 context is updated accordingly. * If the public buffer is too small to hold the public X, Y, false is returned and * public_size is set to the required buffer size to obtain the public X, Y. * * The public_size is 64. first 32-byte is X, second 32-byte is Y. * * If sm2_context is NULL, then return false. * If public_size is NULL, then return false. * If public_size is large enough but public is NULL, then return false. * * @param[in, out] sm2_context Pointer to the sm2 context. * @param[out] public_data Pointer to the buffer to receive generated public X,Y. * @param[in, out] public_size On input, the size of public buffer in bytes. * On output, the size of data returned in public buffer in bytes. * * @retval true sm2 public X,Y generation succeeded. * @retval false sm2 public X,Y generation failed. * @retval false public_size is not large enough. * **/ bool libspdm_sm2_dsa_generate_key(void *sm2_context, uint8_t *public_data, size_t *public_size) { return false; } /** * Allocates and Initializes one Shang-Mi2 context for subsequent use. * * The key is generated before the function returns. * * @param nid cipher NID * * @return Pointer to the Shang-Mi2 context that has been initialized. * If the allocations fails, sm2_new_by_nid() returns NULL. * **/ void *libspdm_sm2_key_exchange_new_by_nid(size_t nid) { return NULL; } /** * Release the specified sm2 context. * * @param[in] sm2_context Pointer to the sm2 context to be released. * **/ void libspdm_sm2_key_exchange_free(void *sm2_context) { } /** * Initialize the specified sm2 context. * * @param[in] sm2_context Pointer to the sm2 context to be released. * @param[in] hash_nid hash NID, only SM3 is valid. * @param[in] id_a the ID-A of the key exchange context. * @param[in] id_a_size size of ID-A key exchange context. * @param[in] id_b the ID-B of the key exchange context. * @param[in] id_b_size size of ID-B key exchange context. * @param[in] is_initiator if the caller is initiator. * true: initiator * false: not an initiator * * @retval true sm2 context is initialized. * @retval false sm2 context is not initialized. **/ bool libspdm_sm2_key_exchange_init(const void *sm2_context, size_t hash_nid, const uint8_t *id_a, size_t id_a_size, const uint8_t *id_b, size_t id_b_size, bool is_initiator) { return false; } /** * Generates sm2 key and returns sm2 public key (X, Y), based upon GB/T 32918.3-2016: SM2 - Part3. * * This function generates random secret, and computes the public key (X, Y), which is * returned via parameter public, public_size. * X is the first half of public with size being public_size / 2, * Y is the second half of public with size being public_size / 2. * sm2 context is updated accordingly. * If the public buffer is too small to hold the public X, Y, false is returned and * public_size is set to the required buffer size to obtain the public X, Y. * * The public_size is 64. first 32-byte is X, second 32-byte is Y. * * If sm2_context is NULL, then return false. * If public_size is NULL, then return false. * If public_size is large enough but public is NULL, then return false. * * @param[in, out] sm2_context Pointer to the sm2 context. * @param[out] public_data Pointer to the buffer to receive generated public X,Y. * @param[in, out] public_size On input, the size of public buffer in bytes. * On output, the size of data returned in public buffer in bytes. * * @retval true sm2 public X,Y generation succeeded. * @retval false sm2 public X,Y generation failed. * @retval false public_size is not large enough. * **/ bool libspdm_sm2_key_exchange_generate_key(void *sm2_context, uint8_t *public_data, size_t *public_size) { return false; } /** * Computes exchanged common key, based upon GB/T 32918.3-2016: SM2 - Part3. * * Given peer's public key (X, Y), this function computes the exchanged common key, * based on its own context including value of curve parameter and random secret. * X is the first half of peer_public with size being peer_public_size / 2, * Y is the second half of peer_public with size being peer_public_size / 2. * * If sm2_context is NULL, then return false. * If peer_public is NULL, then return false. * If peer_public_size is 0, then return false. * If key is NULL, then return false. * * The id_a_size and id_b_size must be smaller than 2^16-1. * The peer_public_size is 64. first 32-byte is X, second 32-byte is Y. * The key_size must be smaller than 2^32-1, limited by KDF function. * * @param[in, out] sm2_context Pointer to the sm2 context. * @param[in] peer_public Pointer to the peer's public X,Y. * @param[in] peer_public_size size of peer's public X,Y in bytes. * @param[out] key Pointer to the buffer to receive generated key. * @param[in] key_size On input, the size of key buffer in bytes. * * @retval true sm2 exchanged key generation succeeded. * @retval false sm2 exchanged key generation failed. * **/ bool libspdm_sm2_key_exchange_compute_key(void *sm2_context, const uint8_t *peer_public, size_t peer_public_size, uint8_t *key, size_t *key_size) { return false; } /** * Carries out the SM2 signature, based upon GB/T 32918.2-2016: SM2 - Part2. * * This function carries out the SM2 signature. * If the signature buffer is too small to hold the contents of signature, false * is returned and sig_size is set to the required buffer size to obtain the signature. * * If sm2_context is NULL, then return false. * If message is NULL, then return false. * hash_nid must be SM3_256. * If sig_size is large enough but signature is NULL, then return false. * * The id_a_size must be smaller than 2^16-1. * The sig_size is 64. first 32-byte is R, second 32-byte is S. * * @param[in] sm2_context Pointer to sm2 context for signature generation. * @param[in] hash_nid hash NID * @param[in] id_a the ID-A of the signing context. * @param[in] id_a_size size of ID-A signing context. * @param[in] message Pointer to octet message to be signed (before hash). * @param[in] size size of the message in bytes. * @param[out] signature Pointer to buffer to receive SM2 signature. * @param[in, out] sig_size On input, the size of signature buffer in bytes. * On output, the size of data returned in signature buffer in bytes. * * @retval true signature successfully generated in SM2. * @retval false signature generation failed. * @retval false sig_size is too small. * **/ bool libspdm_sm2_dsa_sign(const void *sm2_context, size_t hash_nid, const uint8_t *id_a, size_t id_a_size, const uint8_t *message, size_t size, uint8_t *signature, size_t *sig_size) { return false; } /** * Verifies the SM2 signature, based upon GB/T 32918.2-2016: SM2 - Part2. * * If sm2_context is NULL, then return false. * If message is NULL, then return false. * If signature is NULL, then return false. * hash_nid must be SM3_256. * * The id_a_size must be smaller than 2^16-1. * The sig_size is 64. first 32-byte is R, second 32-byte is S. * * @param[in] sm2_context Pointer to SM2 context for signature verification. * @param[in] hash_nid hash NID * @param[in] id_a the ID-A of the signing context. * @param[in] id_a_size size of ID-A signing context. * @param[in] message Pointer to octet message to be checked (before hash). * @param[in] size size of the message in bytes. * @param[in] signature Pointer to SM2 signature to be verified. * @param[in] sig_size size of signature in bytes. * * @retval true Valid signature encoded in SM2. * @retval false Invalid signature or invalid sm2 context. * **/ bool libspdm_sm2_dsa_verify(const void *sm2_context, size_t hash_nid, const uint8_t *id_a, size_t id_a_size, const uint8_t *message, size_t size, const uint8_t *signature, size_t sig_size) { return false; }