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