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" |
| 28 | |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include "mbedtls/platform.h" |
| 32 | #if !defined(MBEDTLS_PLATFORM_C) |
| 33 | #define mbedtls_calloc calloc |
| 34 | #define mbedtls_free free |
| 35 | #endif |
| 36 | |
| 37 | #include <mbedtls/ecp.h> |
| 38 | #include <mbedtls/error.h> |
| 39 | |
| 40 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 41 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 42 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 43 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \ |
| 44 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 45 | psa_status_t mbedtls_psa_ecp_load_representation( |
| 46 | psa_key_type_t type, const uint8_t *data, size_t data_length, |
| 47 | mbedtls_ecp_keypair **p_ecp ) |
| 48 | { |
| 49 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
| 50 | psa_status_t status; |
| 51 | mbedtls_ecp_keypair *ecp = NULL; |
| 52 | size_t curve_size = data_length; |
| 53 | |
| 54 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 55 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
| 56 | { |
| 57 | /* A Weierstrass public key is represented as: |
| 58 | * - The byte 0x04; |
| 59 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 60 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 61 | * So its data length is 2m+1 where m is the curve size in bits. |
| 62 | */ |
| 63 | if( ( data_length & 1 ) == 0 ) |
| 64 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 65 | curve_size = data_length / 2; |
| 66 | |
| 67 | /* Montgomery public keys are represented in compressed format, meaning |
| 68 | * their curve_size is equal to the amount of input. */ |
| 69 | |
| 70 | /* Private keys are represented in uncompressed private random integer |
| 71 | * format, meaning their curve_size is equal to the amount of input. */ |
| 72 | } |
| 73 | |
| 74 | /* Allocate and initialize a key representation. */ |
| 75 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 76 | if( ecp == NULL ) |
| 77 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 78 | mbedtls_ecp_keypair_init( ecp ); |
| 79 | |
| 80 | /* Load the group. */ |
| 81 | grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ), |
| 82 | curve_size ); |
| 83 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
| 84 | { |
| 85 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 86 | goto exit; |
| 87 | } |
| 88 | |
| 89 | status = mbedtls_to_psa_error( |
| 90 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 91 | if( status != PSA_SUCCESS ) |
| 92 | goto exit; |
| 93 | |
| 94 | /* Load the key material. */ |
| 95 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 96 | { |
| 97 | /* Load the public value. */ |
| 98 | status = mbedtls_to_psa_error( |
| 99 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 100 | data, |
| 101 | data_length ) ); |
| 102 | if( status != PSA_SUCCESS ) |
| 103 | goto exit; |
| 104 | |
| 105 | /* Check that the point is on the curve. */ |
| 106 | status = mbedtls_to_psa_error( |
| 107 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 108 | if( status != PSA_SUCCESS ) |
| 109 | goto exit; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | /* Load and validate the secret value. */ |
| 114 | status = mbedtls_to_psa_error( |
| 115 | mbedtls_ecp_read_key( ecp->grp.id, |
| 116 | ecp, |
| 117 | data, |
| 118 | data_length ) ); |
| 119 | if( status != PSA_SUCCESS ) |
| 120 | goto exit; |
| 121 | } |
| 122 | |
| 123 | *p_ecp = ecp; |
| 124 | exit: |
| 125 | if( status != PSA_SUCCESS ) |
| 126 | { |
| 127 | mbedtls_ecp_keypair_free( ecp ); |
| 128 | mbedtls_free( ecp ); |
| 129 | } |
| 130 | |
| 131 | return( status ); |
| 132 | } |
| 133 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 134 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 135 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 136 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || |
| 137 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 138 | |
| 139 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |