|
| 1 | +/* |
| 2 | + * ecdsa_alt.c |
| 3 | + * |
| 4 | + * Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | + * not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#include "mbedtls/ecdsa.h" |
| 22 | +#include <string.h> |
| 23 | +#include "crys_ecpki_ecdsa.h" |
| 24 | +#include "crys_ecpki_build.h" |
| 25 | +#include "crys_common.h" |
| 26 | +#include "crys_ecpki_kg.h" |
| 27 | +#include "crys_ecpki_domain.h" |
| 28 | +#include "crys_ec_edw_api.h" |
| 29 | +#include "mbedtls/platform.h" |
| 30 | +#include "cc_internal.h" |
| 31 | + |
| 32 | +/* Implementation that should never be optimized out by the compiler */ |
| 33 | +static void mbedtls_zeroize( void *v, size_t n ) { |
| 34 | + volatile unsigned char *p = (unsigned char*)v; |
| 35 | + while( n-- ) *p++ = 0; |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +static CRYS_ECPKI_HASH_OpMode_t message_size_to_hash_mode( size_t blen ) |
| 40 | +{ |
| 41 | + CRYS_ECPKI_HASH_OpMode_t hash_mode; |
| 42 | + switch( blen ) |
| 43 | + { |
| 44 | + case CRYS_HASH_SHA1_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t): |
| 45 | + hash_mode = CRYS_ECPKI_AFTER_HASH_SHA1_mode; |
| 46 | + break; |
| 47 | + case CRYS_HASH_SHA224_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t): |
| 48 | + hash_mode = CRYS_ECPKI_AFTER_HASH_SHA224_mode; |
| 49 | + break; |
| 50 | + case CRYS_HASH_SHA256_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t): |
| 51 | + hash_mode = CRYS_ECPKI_AFTER_HASH_SHA256_mode; |
| 52 | + break; |
| 53 | + case CRYS_HASH_SHA384_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t): |
| 54 | + hash_mode = CRYS_ECPKI_AFTER_HASH_SHA384_mode; |
| 55 | + break; |
| 56 | + case CRYS_HASH_SHA512_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t): |
| 57 | + hash_mode = CRYS_ECPKI_AFTER_HASH_SHA512_mode; |
| 58 | + break; |
| 59 | + default: |
| 60 | + hash_mode = CRYS_ECPKI_HASH_OpModeLast; |
| 61 | + } |
| 62 | + |
| 63 | + return hash_mode; |
| 64 | +} |
| 65 | + |
| 66 | +#if defined(MBEDTLS_ECDSA_SIGN_ALT) |
| 67 | +int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 68 | + const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 69 | + int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 70 | +{ |
| 71 | + int ret = 0; |
| 72 | + CRYSError_t CrysRet = CRYS_OK; |
| 73 | + void* pHeap = NULL; |
| 74 | + size_t heapSize = 0; |
| 75 | + uint8_t* pSignature = NULL; |
| 76 | + CRYS_ECPKI_HASH_OpMode_t hash_mode = message_size_to_hash_mode( blen ); |
| 77 | + uint32_t signature_size = ( ( grp->nbits + 7 ) / 8 ) *2; |
| 78 | + const uint32_t signature_size_for_heap = signature_size; |
| 79 | + mbedtls_rand_func_container cc_rand = { f_rng, p_rng }; |
| 80 | + const CRYS_ECPKI_Domain_t* pDomain = CRYS_ECPKI_GetEcDomain ( convert_mbedtls_grp_id_to_crys_domain_id( grp->id ) ); |
| 81 | + |
| 82 | + if( blen > 0xFFFFFFFF ) |
| 83 | + { |
| 84 | + ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 85 | + goto cleanup; |
| 86 | + } |
| 87 | + |
| 88 | + if ( pDomain != NULL ) |
| 89 | + { |
| 90 | + uint8_t temp_buf[ MAX_KEY_SIZE_IN_BYTES ] = {0}; |
| 91 | + cc_ecc_ws_sign_params_t* signParams = mbedtls_calloc( 1, sizeof(cc_ecc_ws_sign_params_t) ); |
| 92 | + if ( signParams == NULL) |
| 93 | + return ( MBEDTLS_ERR_ECP_ALLOC_FAILED ); |
| 94 | + pHeap = signParams; |
| 95 | + heapSize = sizeof(cc_ecc_ws_sign_params_t); |
| 96 | + |
| 97 | + pSignature = mbedtls_calloc( 1, signature_size_for_heap ); |
| 98 | + if ( pSignature == NULL) |
| 99 | + { |
| 100 | + ret = MBEDTLS_ERR_ECP_ALLOC_FAILED; |
| 101 | + goto cleanup; |
| 102 | + } |
| 103 | + |
| 104 | + MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, temp_buf, mbedtls_mpi_size( d ) ) ); |
| 105 | + |
| 106 | + CrysRet = CRYS_ECPKI_BuildPrivKey( pDomain, |
| 107 | + temp_buf, |
| 108 | + mbedtls_mpi_size( d ), |
| 109 | + &signParams->privKey); |
| 110 | + if( CrysRet != CRYS_OK ) |
| 111 | + { |
| 112 | + ret = convert_CrysError_to_mbedtls_err( CrysRet ); |
| 113 | + mbedtls_zeroize( temp_buf, sizeof(temp_buf) ); |
| 114 | + goto cleanup; |
| 115 | + } |
| 116 | + |
| 117 | + CrysRet = CRYS_ECDSA_Sign( &cc_rand, |
| 118 | + convert_mbedtls_to_cc_rand, |
| 119 | + &signParams->signContext, |
| 120 | + &signParams->privKey, |
| 121 | + hash_mode, |
| 122 | + (uint8_t*)buf, |
| 123 | + blen, |
| 124 | + pSignature, |
| 125 | + &signature_size ); |
| 126 | + mbedtls_zeroize( temp_buf, sizeof(temp_buf) ); |
| 127 | + if( CrysRet != CRYS_OK ) |
| 128 | + { |
| 129 | + ret = convert_CrysError_to_mbedtls_err( CrysRet ); |
| 130 | + goto cleanup; |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
| 137 | + goto cleanup; |
| 138 | + } |
| 139 | + |
| 140 | + MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( r, pSignature, ( ( grp->nbits + 7 ) / 8 ) ) ); |
| 141 | + |
| 142 | + MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( s, pSignature + ( ( grp->nbits + 7 ) / 8 ), ( ( grp->nbits + 7 ) / 8 ) ) ); |
| 143 | + |
| 144 | + |
| 145 | +cleanup: |
| 146 | + |
| 147 | + if ( pHeap ) |
| 148 | + { |
| 149 | + mbedtls_zeroize( pHeap, heapSize ); |
| 150 | + mbedtls_free( pHeap ); |
| 151 | + } |
| 152 | + |
| 153 | + if( pSignature ) |
| 154 | + { |
| 155 | + mbedtls_zeroize( pSignature, signature_size_for_heap ); |
| 156 | + mbedtls_free( pSignature ); |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + return ( ret ) ; |
| 161 | +} |
| 162 | +#endif /* MBEDTLS_ECDSA_SIGN_ALT*/ |
| 163 | + |
| 164 | +#if defined(MBEDTLS_ECDSA_VERIFY_ALT) |
| 165 | +//need to normalize the coordinates |
| 166 | +int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, |
| 167 | + const unsigned char *buf, size_t blen, |
| 168 | + const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s) |
| 169 | +{ |
| 170 | + int ret = 0; |
| 171 | + CRYSError_t CrysRet = CRYS_OK; |
| 172 | + void* pHeap = NULL; |
| 173 | + size_t heapSize = 0; |
| 174 | + uint8_t * pSignature = NULL; |
| 175 | + CRYS_ECPKI_HASH_OpMode_t hash_mode = message_size_to_hash_mode( blen ); |
| 176 | + size_t temp_size = 0; |
| 177 | + uint32_t signature_size = ( ( grp->nbits + 7 ) / 8 ) * 2; |
| 178 | + const CRYS_ECPKI_Domain_t* pDomain = CRYS_ECPKI_GetEcDomain ( convert_mbedtls_grp_id_to_crys_domain_id( grp->id ) ); |
| 179 | + |
| 180 | + if( blen > 0xFFFFFFFF ) |
| 181 | + { |
| 182 | + ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 183 | + goto cleanup; |
| 184 | + } |
| 185 | + |
| 186 | + if ( pDomain ) |
| 187 | + { |
| 188 | + uint8_t temp_buf[ 2*MAX_KEY_SIZE_IN_BYTES + 1 ] = {0}; |
| 189 | + |
| 190 | + cc_ecc_ws_verify_params_t* verifyParams = mbedtls_calloc( 1, sizeof(cc_ecc_ws_verify_params_t) ); |
| 191 | + if ( verifyParams == NULL) |
| 192 | + return ( MBEDTLS_ERR_ECP_ALLOC_FAILED ); |
| 193 | + pHeap = verifyParams; |
| 194 | + heapSize = sizeof(cc_ecc_ws_verify_params_t); |
| 195 | + |
| 196 | + pSignature = mbedtls_calloc( 1, signature_size ); |
| 197 | + if ( pSignature == NULL) |
| 198 | + { |
| 199 | + ret = MBEDTLS_ERR_ECP_ALLOC_FAILED; |
| 200 | + goto cleanup; |
| 201 | + } |
| 202 | + |
| 203 | + MBEDTLS_MPI_CHK( mbedtls_ecp_point_write_binary( grp, Q, MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 204 | + &temp_size, temp_buf, sizeof(temp_buf) ) ); |
| 205 | + |
| 206 | + CrysRet = CRYS_ECPKI_BuildPublKey(pDomain, temp_buf, temp_size, &verifyParams->pubKey); |
| 207 | + if( CrysRet != CRYS_OK ) |
| 208 | + { |
| 209 | + ret = convert_CrysError_to_mbedtls_err( CrysRet ); |
| 210 | + goto cleanup; |
| 211 | + } |
| 212 | + |
| 213 | + MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( r, pSignature, ( ( grp->nbits + 7 ) / 8 ) ) ); |
| 214 | + MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( s, pSignature + ( ( grp->nbits + 7 ) / 8 ), ( ( grp->nbits + 7 ) / 8 ) ) ); |
| 215 | + CrysRet = CRYS_ECDSA_Verify ( &verifyParams->verifyContext, |
| 216 | + &verifyParams->pubKey, |
| 217 | + hash_mode, |
| 218 | + pSignature, |
| 219 | + signature_size, |
| 220 | + (uint8_t*)buf, |
| 221 | + blen ); |
| 222 | + if( CrysRet != CRYS_OK ) |
| 223 | + { |
| 224 | + ret = convert_CrysError_to_mbedtls_err( CrysRet ); |
| 225 | + goto cleanup; |
| 226 | + } |
| 227 | + } |
| 228 | + else |
| 229 | + ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
| 230 | + |
| 231 | +cleanup: |
| 232 | + |
| 233 | + if( pHeap ) |
| 234 | + { |
| 235 | + mbedtls_zeroize( pHeap, heapSize ); |
| 236 | + mbedtls_free( pHeap ); |
| 237 | + } |
| 238 | + if( pSignature ) |
| 239 | + { |
| 240 | + mbedtls_zeroize( pSignature, signature_size ); |
| 241 | + mbedtls_free( pSignature ); |
| 242 | + |
| 243 | + } |
| 244 | + |
| 245 | + return ret; |
| 246 | +} |
| 247 | +#endif /* MBEDTLS_ECDSA_VERIFY_ALT */ |
| 248 | + |
| 249 | +#if defined(MBEDTLS_ECDSA_GENKEY_ALT) |
| 250 | +int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid, |
| 251 | + int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 252 | +{ |
| 253 | + int ret = 0; |
| 254 | + CRYSError_t CrysRet = CRYS_OK; |
| 255 | + void* pHeap = NULL; |
| 256 | + size_t heapSize = 0; |
| 257 | + uint32_t key_size = 2*MAX_KEY_SIZE_IN_BYTES + 1; |
| 258 | + const CRYS_ECPKI_Domain_t* pDomain = CRYS_ECPKI_GetEcDomain ( convert_mbedtls_grp_id_to_crys_domain_id( gid ) ); |
| 259 | + mbedtls_rand_func_container cc_rand = { f_rng, p_rng }; |
| 260 | + |
| 261 | + |
| 262 | + if ( pDomain ) |
| 263 | + { |
| 264 | + uint8_t temp_buf[ 2 * MAX_KEY_SIZE_IN_BYTES + 1 ] = {0}; |
| 265 | + |
| 266 | + cc_ecc_ws_keygen_params_t* kgParams = mbedtls_calloc( 1, sizeof(cc_ecc_ws_keygen_params_t) ); |
| 267 | + if ( kgParams == NULL ) |
| 268 | + return ( MBEDTLS_ERR_ECP_ALLOC_FAILED ); |
| 269 | + |
| 270 | + pHeap = kgParams; |
| 271 | + heapSize = sizeof(cc_ecc_ws_keygen_params_t); |
| 272 | + |
| 273 | + CrysRet = CRYS_ECPKI_GenKeyPair( &cc_rand, convert_mbedtls_to_cc_rand, pDomain, |
| 274 | + &kgParams->privKey, &kgParams->pubKey, |
| 275 | + &kgParams->kgTempData, NULL ); |
| 276 | + if ( CrysRet != CRYS_OK ) |
| 277 | + { |
| 278 | + ret = convert_CrysError_to_mbedtls_err( CrysRet ); |
| 279 | + goto cleanup; |
| 280 | + } |
| 281 | + |
| 282 | + MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ctx->grp, gid ) ); |
| 283 | + |
| 284 | + CrysRet = CRYS_ECPKI_ExportPublKey( &kgParams->pubKey, CRYS_EC_PointUncompressed, temp_buf, &key_size ); |
| 285 | + if ( CrysRet != CRYS_OK ) |
| 286 | + { |
| 287 | + ret = convert_CrysError_to_mbedtls_err( CrysRet ); |
| 288 | + goto cleanup; |
| 289 | + } |
| 290 | + |
| 291 | + ret = mbedtls_ecp_point_read_binary( &ctx->grp, &ctx->Q, temp_buf, key_size ); |
| 292 | + if ( ret != 0 ) |
| 293 | + goto cleanup; |
| 294 | + |
| 295 | + memset ( temp_buf, 0 , sizeof(temp_buf) ); |
| 296 | + |
| 297 | + CrysRet = CRYS_COMMON_ConvertLswMswWordsToMsbLsbBytes( temp_buf, (ctx->grp.nbits+7)/8, |
| 298 | + kgParams->privKey.PrivKeyDbBuff, |
| 299 | + 4*((((ctx->grp.nbits+7)/8)+3)/4) ); |
| 300 | + if ( CrysRet != CRYS_OK ) |
| 301 | + { |
| 302 | + ret = convert_CrysError_to_mbedtls_err( CrysRet ); |
| 303 | + mbedtls_zeroize( temp_buf, sizeof(temp_buf) ); |
| 304 | + goto cleanup; |
| 305 | + } |
| 306 | + |
| 307 | + ret = mbedtls_mpi_read_binary( &ctx->d, temp_buf, (ctx->grp.nbits+7)/8 ); |
| 308 | + mbedtls_zeroize( temp_buf, sizeof(temp_buf) ); |
| 309 | + if ( ret != 0 ) |
| 310 | + { |
| 311 | + goto cleanup; |
| 312 | + } |
| 313 | + |
| 314 | + } |
| 315 | + else |
| 316 | + ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
| 317 | + |
| 318 | + |
| 319 | +cleanup: |
| 320 | + if ( pHeap ) |
| 321 | + { |
| 322 | + mbedtls_zeroize( pHeap, heapSize ); |
| 323 | + mbedtls_free ( pHeap ); |
| 324 | + } |
| 325 | + return ( ret ); |
| 326 | +} |
| 327 | +#endif /* MBEDTLS_ECDSA_GENKEY_ALT */ |
0 commit comments