Skip to content
Permalink
develop
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
/**
* \copyright
* MIT License
*
* Copyright (c) 2021 Infineon Technologies AG
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE
*
* \endcopyright
*
* \author Infineon Technologies AG
*
* \file example_optiga_crypt_ecdsa_sign.c
*
* \brief This file provides the example for ECDSA Sign operation using #optiga_crypt_ecdsa_sign.
*
* \ingroup grOptigaExamples
*
* @{
*/
#include "optiga/optiga_crypt.h"
#include "optiga_example.h"
#ifdef OPTIGA_CRYPT_ECDSA_SIGN_ENABLED
#ifndef OPTIGA_INIT_DEINIT_DONE_EXCLUSIVELY
extern void example_optiga_init(void);
extern void example_optiga_deinit(void);
#endif
/**
* Callback when optiga_crypt_xxxx operation is completed asynchronously
*/
static volatile optiga_lib_status_t optiga_lib_status;
//lint --e{818} suppress "argument "context" is not used in the sample provided"
static void optiga_crypt_callback(void * context, optiga_lib_status_t return_status)
{
optiga_lib_status = return_status;
if (NULL != context)
{
// callback to upper layer here
}
}
//SHA-256 Digest to be signed
static const uint8_t digest [] =
{
// Size of digest to be chosen based on Curve
0x61, 0xC7, 0xDE, 0xF9, 0x0F, 0xD5, 0xCD, 0x7A,0x8B, 0x7A, 0x36, 0x41, 0x04, 0xE0, 0x0D, 0x82,
0x38, 0x46, 0xBF, 0xB7, 0x70, 0xEE, 0xBF, 0x8F,0x40, 0x25, 0x2E, 0x0A, 0x21, 0x42, 0xAF, 0x9C,
};
/**
* The below example demonstrates the signing of digest using
* the Private key in OPTIGA Key store.
*
* Example for #optiga_crypt_ecdsa_sign
*
*/
void example_optiga_crypt_ecdsa_sign(void)
{
//To store the signture generated
uint8_t signature [80];
uint16_t signature_length = sizeof(signature);
uint32_t time_taken = 0;
optiga_crypt_t * me = NULL;
optiga_lib_status_t return_status = !OPTIGA_LIB_SUCCESS;
do
{
#ifndef OPTIGA_INIT_DEINIT_DONE_EXCLUSIVELY
/**
* Open the application on OPTIGA which is a precondition to perform any other operations
* using optiga_util_open_application
*/
example_optiga_init();
#endif //OPTIGA_INIT_DEINIT_DONE_EXCLUSIVELY
OPTIGA_EXAMPLE_LOG_MESSAGE(__FUNCTION__);
/**
* 1. Create OPTIGA Crypt Instance
*/
me = optiga_crypt_create(0, optiga_crypt_callback, NULL);
if (NULL == me)
{
break;
}
START_PERFORMANCE_MEASUREMENT(time_taken);
/**
* 2. Sign the digest using Private key from Key Store ID E0F0
*/
optiga_lib_status = OPTIGA_LIB_BUSY;
return_status = optiga_crypt_ecdsa_sign(me,
digest,
sizeof(digest),
OPTIGA_KEY_ID_E0F0,
signature,
&signature_length);
WAIT_AND_CHECK_STATUS(return_status, optiga_lib_status);
READ_PERFORMANCE_MEASUREMENT(time_taken);
return_status = OPTIGA_LIB_SUCCESS;
} while (FALSE);
OPTIGA_EXAMPLE_LOG_STATUS(return_status);
#ifndef OPTIGA_INIT_DEINIT_DONE_EXCLUSIVELY
/**
* Close the application on OPTIGA after all the operations are executed
* using optiga_util_close_application
*/
example_optiga_deinit();
#endif //OPTIGA_INIT_DEINIT_DONE_EXCLUSIVELY
OPTIGA_EXAMPLE_LOG_PERFORMANCE_VALUE(time_taken, return_status);
if (me)
{
//Destroy the instance after the completion of usecase if not required.
return_status = optiga_crypt_destroy(me);
if(OPTIGA_LIB_SUCCESS != return_status)
{
//lint --e{774} suppress This is a generic macro
OPTIGA_EXAMPLE_LOG_STATUS(return_status);
}
}
}
#endif //OPTIGA_CRYPT_ECDSA_SIGN_ENABLED
/**
* @}
*/