Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ecdsa_alt.c Source File

ecdsa_alt.c

00001 /*
00002  *  ecdsa_alt.c
00003  *
00004  *  Copyright (C) 2018, Arm Limited, All Rights Reserved
00005  *  SPDX-License-Identifier: Apache-2.0
00006  *
00007  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008  *  not use this file except in compliance with the License.
00009  *  You may obtain a copy of the License at
00010  *
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  *
00019  */
00020 
00021 #include "mbedtls/ecdsa.h"
00022 #include <string.h>
00023 #include "crys_ecpki_ecdsa.h"
00024 #include "crys_ecpki_build.h"
00025 #include "crys_common.h"
00026 #include "crys_ecpki_kg.h"
00027 #include "crys_ecpki_domain.h"
00028 #include "crys_ec_edw_api.h"
00029 #include "mbedtls/platform.h"
00030 #include "mbedtls/platform_util.h"
00031 #include "cc_internal.h"
00032 
00033 static CRYS_ECPKI_HASH_OpMode_t  message_size_to_hash_mode( size_t blen )
00034 {
00035     CRYS_ECPKI_HASH_OpMode_t  hash_mode;
00036     switch( blen )
00037     {
00038     case CRYS_HASH_SHA1_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t):
00039             hash_mode = CRYS_ECPKI_AFTER_HASH_SHA1_mode ;
00040     break;
00041     case CRYS_HASH_SHA224_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t):
00042             hash_mode = CRYS_ECPKI_AFTER_HASH_SHA224_mode ;
00043     break;
00044     case CRYS_HASH_SHA256_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t):
00045             hash_mode = CRYS_ECPKI_AFTER_HASH_SHA256_mode ;
00046     break;
00047     case CRYS_HASH_SHA384_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t):
00048             hash_mode = CRYS_ECPKI_AFTER_HASH_SHA384_mode ;
00049     break;
00050     case CRYS_HASH_SHA512_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t):
00051             hash_mode = CRYS_ECPKI_AFTER_HASH_SHA512_mode ;
00052     break;
00053     default:
00054         hash_mode = CRYS_ECPKI_HASH_OpModeLast ;
00055     }
00056 
00057     return hash_mode;
00058 }
00059 
00060 #if defined(MBEDTLS_ECDSA_SIGN_ALT)
00061 int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
00062                 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
00063                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00064 {
00065     int ret = 0;
00066     CRYSError_t  CrysRet = CRYS_OK;
00067     void* pHeap = NULL;
00068     size_t heapSize = 0;
00069     uint8_t* pSignature = NULL;
00070     CRYS_ECPKI_HASH_OpMode_t  hash_mode = message_size_to_hash_mode( blen );
00071     uint32_t signature_size =  ( ( grp->nbits  + 7 ) / 8 ) *2;
00072     const uint32_t signature_size_for_heap = signature_size;
00073     mbedtls_rand_func_container cc_rand = { f_rng, p_rng };
00074     const CRYS_ECPKI_Domain_t *  pDomain =  CRYS_ECPKI_GetEcDomain ( convert_mbedtls_grp_id_to_crys_domain_id( grp->id  ) );
00075 
00076 #if SIZE_MAX > UINT_MAX
00077     if( blen > 0xFFFFFFFF )
00078     {
00079         return ( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
00080     }
00081 #endif
00082 
00083     if ( pDomain != NULL )
00084     {
00085         uint8_t temp_buf[ MAX_KEY_SIZE_IN_BYTES ] = {0};
00086         cc_ecc_ws_sign_params_t* signParams = mbedtls_calloc( 1, sizeof(cc_ecc_ws_sign_params_t) );
00087         if ( signParams == NULL)
00088             return ( MBEDTLS_ERR_ECP_ALLOC_FAILED );
00089         pHeap = signParams;
00090         heapSize = sizeof(cc_ecc_ws_sign_params_t);
00091 
00092         pSignature = mbedtls_calloc( 1, signature_size_for_heap );
00093         if ( pSignature == NULL)
00094         {
00095             ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
00096             goto cleanup;
00097         }
00098 
00099         MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, temp_buf, mbedtls_mpi_size( d ) ) );
00100 
00101         CrysRet = CRYS_ECPKI_BuildPrivKey( pDomain,
00102                                        temp_buf,
00103                                        mbedtls_mpi_size( d ),
00104                                        &signParams->privKey);
00105         if( CrysRet != CRYS_OK )
00106         {
00107             ret = convert_CrysError_to_mbedtls_err( CrysRet );
00108             mbedtls_platform_zeroize( temp_buf, sizeof(temp_buf) );
00109             goto cleanup;
00110         }
00111 
00112         CrysRet =  CRYS_ECDSA_Sign( &cc_rand,
00113                                 convert_mbedtls_to_cc_rand,
00114                                 &signParams->signContext,
00115                                 &signParams->privKey,
00116                                 hash_mode,
00117                                 (uint8_t*)buf,
00118                                 blen,
00119                                 pSignature,
00120                                 &signature_size );
00121         mbedtls_platform_zeroize( temp_buf, sizeof(temp_buf) );
00122         if( CrysRet != CRYS_OK )
00123         {
00124             ret = convert_CrysError_to_mbedtls_err( CrysRet );
00125             goto cleanup;
00126         }
00127     }
00128     else
00129     {
00130         ret =  MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
00131         goto cleanup;
00132     }
00133 
00134     MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( r, pSignature, ( ( grp->nbits  + 7 ) / 8 ) ) );
00135 
00136     MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( s, pSignature + ( ( grp->nbits  + 7 ) / 8 ), ( ( grp->nbits  + 7 ) / 8 ) ) );
00137 
00138 
00139 cleanup:
00140 
00141     if ( pHeap )
00142     {
00143         mbedtls_platform_zeroize( pHeap, heapSize );
00144         mbedtls_free( pHeap );
00145     }
00146 
00147     if( pSignature )
00148     {
00149         mbedtls_platform_zeroize( pSignature, signature_size_for_heap );
00150         mbedtls_free( pSignature );
00151 
00152     }
00153 
00154     return ( ret ) ;
00155 }
00156 #endif /* MBEDTLS_ECDSA_SIGN_ALT*/
00157 
00158 #if defined(MBEDTLS_ECDSA_VERIFY_ALT)
00159 //need to normalize the coordinates
00160 int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
00161                   const unsigned char *buf, size_t blen,
00162                   const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s)
00163 {
00164     int ret = 0;
00165     CRYSError_t  CrysRet = CRYS_OK;
00166     void* pHeap = NULL;
00167     size_t heapSize = 0;
00168     uint8_t * pSignature = NULL;
00169     CRYS_ECPKI_HASH_OpMode_t  hash_mode = message_size_to_hash_mode( blen );
00170     size_t temp_size = 0;
00171     uint32_t signature_size = ( ( grp->nbits  + 7 ) / 8 ) * 2;
00172     const CRYS_ECPKI_Domain_t *  pDomain =  CRYS_ECPKI_GetEcDomain ( convert_mbedtls_grp_id_to_crys_domain_id( grp->id  ) );
00173 
00174 #if SIZE_MAX > UINT_MAX
00175     if( blen > 0xFFFFFFFF )
00176     {
00177         return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
00178     }
00179 #endif
00180 
00181     if ( pDomain )
00182     {
00183         uint8_t temp_buf[ 2*MAX_KEY_SIZE_IN_BYTES + 1 ] = {0};
00184 
00185         cc_ecc_ws_verify_params_t* verifyParams = mbedtls_calloc( 1, sizeof(cc_ecc_ws_verify_params_t) );
00186         if ( verifyParams == NULL)
00187             return ( MBEDTLS_ERR_ECP_ALLOC_FAILED );
00188         pHeap = verifyParams;
00189         heapSize = sizeof(cc_ecc_ws_verify_params_t);
00190 
00191         pSignature = mbedtls_calloc( 1, signature_size );
00192         if ( pSignature == NULL)
00193         {
00194             ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
00195             goto cleanup;
00196         }
00197 
00198         MBEDTLS_MPI_CHK( mbedtls_ecp_point_write_binary( grp, Q, MBEDTLS_ECP_PF_UNCOMPRESSED,
00199                                         &temp_size, temp_buf, sizeof(temp_buf) ) );
00200 
00201         CrysRet = CRYS_ECPKI_BuildPublKey(pDomain, temp_buf, temp_size, &verifyParams->pubKey);
00202         if( CrysRet != CRYS_OK )
00203         {
00204             ret = convert_CrysError_to_mbedtls_err( CrysRet );
00205             goto cleanup;
00206         }
00207 
00208         MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( r, pSignature, ( ( grp->nbits  + 7 ) / 8 )  ) );
00209         MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( s, pSignature + ( ( grp->nbits  + 7 ) / 8 ), ( ( grp->nbits  + 7 ) / 8 ) ) );
00210         CrysRet =  CRYS_ECDSA_Verify ( &verifyParams->verifyContext,
00211                                        &verifyParams->pubKey,
00212                                        hash_mode,
00213                                        pSignature,
00214                                        signature_size,
00215                                        (uint8_t*)buf,
00216                                        blen );
00217         if( CrysRet != CRYS_OK )
00218         {
00219             ret = convert_CrysError_to_mbedtls_err( CrysRet );
00220             goto cleanup;
00221         }
00222     }
00223     else
00224         ret =  MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
00225 
00226 cleanup:
00227 
00228     if( pHeap )
00229     {
00230         mbedtls_platform_zeroize( pHeap, heapSize );
00231         mbedtls_free( pHeap );
00232     }
00233 
00234     if( pSignature )
00235     {
00236         mbedtls_platform_zeroize( pSignature, signature_size );
00237         mbedtls_free( pSignature );
00238 
00239     }
00240 
00241     return ret;
00242 }
00243 #endif /* MBEDTLS_ECDSA_VERIFY_ALT */
00244 
00245 #if defined(MBEDTLS_ECDSA_GENKEY_ALT)
00246 int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
00247                   int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00248 {
00249     int ret = 0;
00250     CRYSError_t  CrysRet = CRYS_OK;
00251     void* pHeap = NULL;
00252     size_t heapSize = 0;
00253     uint32_t key_size = 2*MAX_KEY_SIZE_IN_BYTES + 1;
00254     const CRYS_ECPKI_Domain_t *  pDomain =  CRYS_ECPKI_GetEcDomain ( convert_mbedtls_grp_id_to_crys_domain_id( gid ) );
00255     mbedtls_rand_func_container cc_rand = { f_rng, p_rng };
00256 
00257 
00258     if ( pDomain )
00259     {
00260         uint8_t temp_buf[ 2 * MAX_KEY_SIZE_IN_BYTES + 1 ] = {0};
00261 
00262         cc_ecc_ws_keygen_params_t* kgParams =  mbedtls_calloc( 1, sizeof(cc_ecc_ws_keygen_params_t) );
00263         if ( kgParams == NULL )
00264             return ( MBEDTLS_ERR_ECP_ALLOC_FAILED );
00265 
00266         pHeap = kgParams;
00267         heapSize = sizeof(cc_ecc_ws_keygen_params_t);
00268 
00269         CrysRet =  CRYS_ECPKI_GenKeyPair( &cc_rand, convert_mbedtls_to_cc_rand, pDomain,
00270                                           &kgParams->privKey, &kgParams->pubKey,
00271                                           &kgParams->kgTempData, NULL );
00272         if ( CrysRet != CRYS_OK )
00273         {
00274             ret = convert_CrysError_to_mbedtls_err( CrysRet );
00275             goto cleanup;
00276         }
00277 
00278         MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ctx->grp , gid ) );
00279 
00280         CrysRet = CRYS_ECPKI_ExportPublKey( &kgParams->pubKey, CRYS_EC_PointUncompressed , temp_buf,  &key_size );
00281         if ( CrysRet != CRYS_OK )
00282         {
00283             ret = convert_CrysError_to_mbedtls_err( CrysRet );
00284             goto cleanup;
00285         }
00286 
00287         ret = mbedtls_ecp_point_read_binary( &ctx->grp , &ctx->Q , temp_buf, key_size );
00288         if ( ret != 0 )
00289             goto cleanup;
00290 
00291         memset ( temp_buf, 0 , sizeof(temp_buf) );
00292 
00293         CrysRet = CRYS_COMMON_ConvertLswMswWordsToMsbLsbBytes( temp_buf, (ctx->grp .nbits +7)/8,
00294                                                                kgParams->privKey.PrivKeyDbBuff,
00295                                                                4*((((ctx->grp .nbits +7)/8)+3)/4) );
00296         if ( CrysRet != CRYS_OK )
00297         {
00298             ret = convert_CrysError_to_mbedtls_err( CrysRet );
00299             mbedtls_platform_zeroize( temp_buf, sizeof(temp_buf) );
00300             goto cleanup;
00301         }
00302 
00303         ret = mbedtls_mpi_read_binary( &ctx->d , temp_buf, (ctx->grp .nbits +7)/8 );
00304         mbedtls_platform_zeroize( temp_buf, sizeof(temp_buf) );
00305         if ( ret != 0 )
00306         {
00307             goto cleanup;
00308         }
00309     }
00310     else
00311         ret =  MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
00312 
00313 
00314 cleanup:
00315     if ( pHeap )
00316     {
00317         mbedtls_platform_zeroize( pHeap, heapSize );
00318         mbedtls_free ( pHeap );
00319     }
00320     return ( ret );
00321 }
00322 #endif /* MBEDTLS_ECDSA_GENKEY_ALT */