Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cc_internal.h Source File

cc_internal.h

00001 /*
00002  *  cc_internal.h
00003  *
00004  *  Internal utility functions and definitions,
00005  *  used for converting mbedtls types to CC types, and vice versa
00006  *
00007  *  Copyright (C) 2018, Arm Limited, All Rights Reserved
00008  *  SPDX-License-Identifier: Apache-2.0
00009  *
00010  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00011  *  not use this file except in compliance with the License.
00012  *  You may obtain a copy of the License at
00013  *
00014  *  http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  *  Unless required by applicable law or agreed to in writing, software
00017  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00018  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  *  See the License for the specific language governing permissions and
00020  *  limitations under the License.
00021  *
00022  */
00023 
00024 #ifndef __CC_INTERNAL_H__
00025 #define __CC_INTERNAL_H__
00026 #include "crys_ecpki_types.h"
00027 #include "crys_ec_mont_api.h"
00028 #include "mbedtls/ecp.h"
00029 #include <stddef.h>
00030 #include <stdint.h>
00031 
00032 #define CURVE_25519_KEY_SIZE    32
00033 
00034 #ifdef __cplusplus
00035 extern "C" {
00036 #endif
00037 
00038 #define MAX_KEY_SIZE_IN_BYTES ( ( CRYS_ECPKI_MODUL_MAX_LENGTH_IN_WORDS ) * SASI_32BIT_WORD_SIZE)
00039 
00040 /* ECC utility functions and structures*/
00041 typedef struct cc_ecc_ws_keygen_params{
00042     CRYS_ECPKI_UserPublKey_t  pubKey;
00043     CRYS_ECPKI_UserPrivKey_t  privKey;
00044     CRYS_ECPKI_KG_TempData_t  kgTempData;
00045 } cc_ecc_ws_keygen_params_t;
00046 
00047 typedef struct cc_ecc_ws_comp_shared_params{
00048     CRYS_ECPKI_UserPublKey_t  pubKey;
00049     CRYS_ECPKI_UserPrivKey_t  privKey;
00050     CRYS_ECDH_TempData_t      ecdhTempData;
00051 } cc_ecc_ws_comp_shared_params_t;
00052 
00053 typedef struct cc_ecc_ws_verify_params{
00054     CRYS_ECPKI_UserPublKey_t  pubKey;
00055     CRYS_ECDSA_VerifyUserContext_t  verifyContext;
00056 } cc_ecc_ws_verify_params_t;
00057 
00058 typedef struct cc_ecc_ws_sign_params{
00059     CRYS_ECPKI_UserPrivKey_t  privKey;
00060     CRYS_ECDSA_SignUserContext_t  signContext;
00061 } cc_ecc_ws_sign_params_t;
00062 
00063 typedef struct cc_ecc_25519_keygen_params{
00064     uint8_t pubKey[CURVE_25519_KEY_SIZE];
00065     uint8_t privKey[CURVE_25519_KEY_SIZE];
00066     CRYS_ECMONT_TempBuff_t  kgTempData;
00067 } cc_ecc_25519_keygen_params_t;
00068 
00069 typedef cc_ecc_25519_keygen_params_t cc_ecc_25519_comp_shared_params_t;
00070 
00071 /**
00072  * \brief      This function converts mbedtls type mbedtls_ecp_group_id
00073  *             to Cryptocell type CRYS_ECPKI_DomainID_t
00074  *
00075  * \param grp_id           The mbedtls mbedtls_ecp_group_id to convert
00076  *
00077  * \return     \c The corresponding CRYS_ECPKI_DomainID_t.
00078  *                CRYS_ECPKI_DomainID_OffMode if not recognized.
00079  */
00080 CRYS_ECPKI_DomainID_t  convert_mbedtls_grp_id_to_crys_domain_id( mbedtls_ecp_group_id grp_id );
00081 
00082 /* f_rng conversion from mbedtls type to cc type*/
00083 typedef struct
00084 {
00085     int  (*f_rng)( void* ctx, unsigned char* output, size_t outSizeBytes );
00086     void* ctx;
00087 
00088 }mbedtls_rand_func_container;
00089 
00090 /**
00091  * \brief      This function converts mbedtls f_rng type to
00092  *             Cryptocell f_rng type(SaSiRndGenerateVectWorkFunc_t)
00093  *
00094  *             Note: The Mbed TLS type f_rng signature is:
00095  *             int  (*f_rng)( void* ctx, unsigned char* output, size_t outSizeBytes );
00096  *             while CC f_rng signature is:
00097  *             uint32_t (*SaSiRndGenerateVectWorkFunc_t)(
00098  *                        void  *rndState_ptr,
00099  *                        uint16_t outSizeBytes,
00100  *                        uint8_t          *out_ptr)
00101  *
00102  *             so the Mbed TLS f_rng can't  be sent as is to the CC API.
00103  *
00104  *             In addition, this function manipulates the different random data,
00105  *             to adjust between the way Cryptocell reads the random data. This is done for
00106  *             different standard tests to pass.
00107  *
00108  *
00109  * \param mbedtls_rand      The mbedtls rnd context pointer
00110  * \param outSizeBytes      The size of the output buffer
00111  * \param out_ptr           Pointer to the output buffer
00112  *
00113  * \return     \c The corresponding CRYS_ECPKI_DomainID_t.
00114  *                CRYS_ECPKI_DomainID_OffMode if not recognized.
00115  */
00116 
00117 uint32_t convert_mbedtls_to_cc_rand( void* mbedtls_rand, uint16_t outSizeBytes, uint8_t* out_ptr );
00118 
00119 /**
00120  * \brief      This function convertsCryptocell error
00121  *             Mbed TLS related error.
00122  *
00123  *
00124  * \return     \c The corresponding Mbed TLS error,
00125  *                MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED as default, if none found
00126  */
00127 int convert_CrysError_to_mbedtls_err( CRYSError_t  Crys_err );
00128 
00129 #ifdef __cplusplus
00130 }
00131 #endif
00132 
00133 #endif /* __CC_INTERNAL_H__ */