Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA ECP layer on top of Mbed TLS crypto |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
| 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | #include "common.h" |
| 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 24 | |
| 25 | #include <psa/crypto.h> |
| 26 | #include "psa_crypto_core.h" |
| 27 | #include "psa_crypto_ecp.h" |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame^] | 28 | #include "psa_crypto_random_impl.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 29 | |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include "mbedtls/platform.h" |
| 33 | #if !defined(MBEDTLS_PLATFORM_C) |
| 34 | #define mbedtls_calloc calloc |
| 35 | #define mbedtls_free free |
| 36 | #endif |
| 37 | |
| 38 | #include <mbedtls/ecp.h> |
| 39 | #include <mbedtls/error.h> |
| 40 | |
| 41 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 42 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 43 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 44 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \ |
| 45 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 46 | psa_status_t mbedtls_psa_ecp_load_representation( |
| 47 | psa_key_type_t type, const uint8_t *data, size_t data_length, |
| 48 | mbedtls_ecp_keypair **p_ecp ) |
| 49 | { |
| 50 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
| 51 | psa_status_t status; |
| 52 | mbedtls_ecp_keypair *ecp = NULL; |
| 53 | size_t curve_size = data_length; |
| 54 | |
| 55 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 56 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
| 57 | { |
| 58 | /* A Weierstrass public key is represented as: |
| 59 | * - The byte 0x04; |
| 60 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 61 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 62 | * So its data length is 2m+1 where m is the curve size in bits. |
| 63 | */ |
| 64 | if( ( data_length & 1 ) == 0 ) |
| 65 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 66 | curve_size = data_length / 2; |
| 67 | |
| 68 | /* Montgomery public keys are represented in compressed format, meaning |
| 69 | * their curve_size is equal to the amount of input. */ |
| 70 | |
| 71 | /* Private keys are represented in uncompressed private random integer |
| 72 | * format, meaning their curve_size is equal to the amount of input. */ |
| 73 | } |
| 74 | |
| 75 | /* Allocate and initialize a key representation. */ |
| 76 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 77 | if( ecp == NULL ) |
| 78 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 79 | mbedtls_ecp_keypair_init( ecp ); |
| 80 | |
| 81 | /* Load the group. */ |
| 82 | grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ), |
| 83 | curve_size ); |
| 84 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
| 85 | { |
| 86 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 87 | goto exit; |
| 88 | } |
| 89 | |
| 90 | status = mbedtls_to_psa_error( |
| 91 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 92 | if( status != PSA_SUCCESS ) |
| 93 | goto exit; |
| 94 | |
| 95 | /* Load the key material. */ |
| 96 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 97 | { |
| 98 | /* Load the public value. */ |
| 99 | status = mbedtls_to_psa_error( |
| 100 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 101 | data, |
| 102 | data_length ) ); |
| 103 | if( status != PSA_SUCCESS ) |
| 104 | goto exit; |
| 105 | |
| 106 | /* Check that the point is on the curve. */ |
| 107 | status = mbedtls_to_psa_error( |
| 108 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 109 | if( status != PSA_SUCCESS ) |
| 110 | goto exit; |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | /* Load and validate the secret value. */ |
| 115 | status = mbedtls_to_psa_error( |
| 116 | mbedtls_ecp_read_key( ecp->grp.id, |
| 117 | ecp, |
| 118 | data, |
| 119 | data_length ) ); |
| 120 | if( status != PSA_SUCCESS ) |
| 121 | goto exit; |
| 122 | } |
| 123 | |
| 124 | *p_ecp = ecp; |
| 125 | exit: |
| 126 | if( status != PSA_SUCCESS ) |
| 127 | { |
| 128 | mbedtls_ecp_keypair_free( ecp ); |
| 129 | mbedtls_free( ecp ); |
| 130 | } |
| 131 | |
| 132 | return( status ); |
| 133 | } |
| 134 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 135 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 136 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 137 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || |
| 138 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 139 | |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame^] | 140 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 141 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 142 | psa_status_t mbedtls_psa_ecp_export_key( psa_key_type_t type, |
| 143 | mbedtls_ecp_keypair *ecp, |
| 144 | uint8_t *data, |
| 145 | size_t data_size, |
| 146 | size_t *data_length ) |
| 147 | { |
| 148 | psa_status_t status; |
| 149 | |
| 150 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 151 | { |
| 152 | /* Check whether the public part is loaded */ |
| 153 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 154 | { |
| 155 | /* Calculate the public key */ |
| 156 | status = mbedtls_to_psa_error( |
| 157 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 158 | mbedtls_psa_get_random, |
| 159 | MBEDTLS_PSA_RANDOM_STATE ) ); |
| 160 | if( status != PSA_SUCCESS ) |
| 161 | return( status ); |
| 162 | } |
| 163 | |
| 164 | status = mbedtls_to_psa_error( |
| 165 | mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q, |
| 166 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 167 | data_length, |
| 168 | data, |
| 169 | data_size ) ); |
| 170 | if( status != PSA_SUCCESS ) |
| 171 | memset( data, 0, data_size ); |
| 172 | |
| 173 | return( status ); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) |
| 178 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 179 | |
| 180 | status = mbedtls_to_psa_error( |
| 181 | mbedtls_ecp_write_key( ecp, |
| 182 | data, |
| 183 | PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) ); |
| 184 | if( status == PSA_SUCCESS ) |
| 185 | *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits ); |
| 186 | else |
| 187 | memset( data, 0, data_size ); |
| 188 | |
| 189 | return( status ); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | psa_status_t mbedtls_psa_ecp_export_public_key( |
| 194 | const psa_key_attributes_t *attributes, |
| 195 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 196 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 197 | { |
| 198 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 199 | mbedtls_ecp_keypair *ecp = NULL; |
| 200 | |
| 201 | status = mbedtls_psa_ecp_load_representation( |
| 202 | attributes->core.type, key_buffer, key_buffer_size, &ecp ); |
| 203 | if( status != PSA_SUCCESS ) |
| 204 | return( status ); |
| 205 | |
| 206 | status = mbedtls_psa_ecp_export_key( |
| 207 | PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 208 | PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type ) ), |
| 209 | ecp, data, data_size, data_length ); |
| 210 | |
| 211 | mbedtls_ecp_keypair_free( ecp ); |
| 212 | mbedtls_free( ecp ); |
| 213 | |
| 214 | return( status ); |
| 215 | } |
| 216 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 217 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
| 218 | |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 219 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |