psa: Move xxx_load_representation functions to RSA/ECP specific modules
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
This commit is contained in:
parent
3c8ca3ad00
commit
00b7bfc2c4
8 changed files with 359 additions and 205 deletions
|
@ -62,6 +62,8 @@ set(src_crypto
|
||||||
poly1305.c
|
poly1305.c
|
||||||
psa_crypto.c
|
psa_crypto.c
|
||||||
psa_crypto_driver_wrappers.c
|
psa_crypto_driver_wrappers.c
|
||||||
|
psa_crypto_ecp.c
|
||||||
|
psa_crypto_rsa.c
|
||||||
psa_crypto_se.c
|
psa_crypto_se.c
|
||||||
psa_crypto_slot_management.c
|
psa_crypto_slot_management.c
|
||||||
psa_crypto_storage.c
|
psa_crypto_storage.c
|
||||||
|
|
|
@ -119,6 +119,8 @@ OBJS_CRYPTO= \
|
||||||
poly1305.o \
|
poly1305.o \
|
||||||
psa_crypto.o \
|
psa_crypto.o \
|
||||||
psa_crypto_driver_wrappers.o \
|
psa_crypto_driver_wrappers.o \
|
||||||
|
psa_crypto_ecp.o \
|
||||||
|
psa_crypto_rsa.o \
|
||||||
psa_crypto_se.o \
|
psa_crypto_se.o \
|
||||||
psa_crypto_slot_management.o \
|
psa_crypto_slot_management.o \
|
||||||
psa_crypto_storage.o \
|
psa_crypto_storage.o \
|
||||||
|
|
|
@ -32,6 +32,8 @@
|
||||||
#include "psa_crypto_core.h"
|
#include "psa_crypto_core.h"
|
||||||
#include "psa_crypto_invasive.h"
|
#include "psa_crypto_invasive.h"
|
||||||
#include "psa_crypto_driver_wrappers.h"
|
#include "psa_crypto_driver_wrappers.h"
|
||||||
|
#include "psa_crypto_ecp.h"
|
||||||
|
#include "psa_crypto_rsa.h"
|
||||||
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
||||||
#include "psa_crypto_se.h"
|
#include "psa_crypto_se.h"
|
||||||
#endif
|
#endif
|
||||||
|
@ -531,102 +533,6 @@ static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
|
||||||
return( PSA_SUCCESS );
|
return( PSA_SUCCESS );
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
|
|
||||||
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
|
|
||||||
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
|
|
||||||
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
|
|
||||||
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
|
||||||
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
|
||||||
|
|
||||||
/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
|
|
||||||
* that are not a multiple of 8) well. For example, there is only
|
|
||||||
* mbedtls_rsa_get_len(), which returns a number of bytes, and no
|
|
||||||
* way to return the exact bit size of a key.
|
|
||||||
* To keep things simple, reject non-byte-aligned key sizes. */
|
|
||||||
static psa_status_t psa_check_rsa_key_byte_aligned(
|
|
||||||
const mbedtls_rsa_context *rsa )
|
|
||||||
{
|
|
||||||
mbedtls_mpi n;
|
|
||||||
psa_status_t status;
|
|
||||||
mbedtls_mpi_init( &n );
|
|
||||||
status = mbedtls_to_psa_error(
|
|
||||||
mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
|
|
||||||
if( status == PSA_SUCCESS )
|
|
||||||
{
|
|
||||||
if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
|
|
||||||
status = PSA_ERROR_NOT_SUPPORTED;
|
|
||||||
}
|
|
||||||
mbedtls_mpi_free( &n );
|
|
||||||
return( status );
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Load the contents of a key buffer into an internal RSA representation
|
|
||||||
*
|
|
||||||
* \param[in] type The type of key contained in \p data.
|
|
||||||
* \param[in] data The buffer from which to load the representation.
|
|
||||||
* \param[in] data_length The size in bytes of \p data.
|
|
||||||
* \param[out] p_rsa Returns a pointer to an RSA context on success.
|
|
||||||
* The caller is responsible for freeing both the
|
|
||||||
* contents of the context and the context itself
|
|
||||||
* when done.
|
|
||||||
*/
|
|
||||||
static psa_status_t mbedtls_psa_rsa_load_representation(
|
|
||||||
psa_key_type_t type, const uint8_t *data, size_t data_length,
|
|
||||||
mbedtls_rsa_context **p_rsa )
|
|
||||||
{
|
|
||||||
psa_status_t status;
|
|
||||||
mbedtls_pk_context ctx;
|
|
||||||
size_t bits;
|
|
||||||
mbedtls_pk_init( &ctx );
|
|
||||||
|
|
||||||
/* Parse the data. */
|
|
||||||
if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
|
|
||||||
status = mbedtls_to_psa_error(
|
|
||||||
mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
|
|
||||||
else
|
|
||||||
status = mbedtls_to_psa_error(
|
|
||||||
mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
|
|
||||||
if( status != PSA_SUCCESS )
|
|
||||||
goto exit;
|
|
||||||
|
|
||||||
/* We have something that the pkparse module recognizes. If it is a
|
|
||||||
* valid RSA key, store it. */
|
|
||||||
if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
|
|
||||||
{
|
|
||||||
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
|
|
||||||
* supports non-byte-aligned key sizes, but not well. For example,
|
|
||||||
* mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
|
|
||||||
bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
|
|
||||||
if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
|
|
||||||
{
|
|
||||||
status = PSA_ERROR_NOT_SUPPORTED;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
|
|
||||||
if( status != PSA_SUCCESS )
|
|
||||||
goto exit;
|
|
||||||
|
|
||||||
/* Copy out the pointer to the RSA context, and reset the PK context
|
|
||||||
* such that pk_free doesn't free the RSA context we just grabbed. */
|
|
||||||
*p_rsa = mbedtls_pk_rsa( ctx );
|
|
||||||
ctx.pk_info = NULL;
|
|
||||||
|
|
||||||
exit:
|
|
||||||
mbedtls_pk_free( &ctx );
|
|
||||||
return( status );
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
|
|
||||||
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
|
|
||||||
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
|
|
||||||
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
|
|
||||||
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
|
||||||
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
||||||
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
||||||
|
|
||||||
|
@ -757,115 +663,6 @@ exit:
|
||||||
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
||||||
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
|
|
||||||
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
|
|
||||||
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
|
||||||
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
|
|
||||||
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
|
|
||||||
/** Load the contents of a key buffer into an internal ECP representation
|
|
||||||
*
|
|
||||||
* \param[in] type The type of key contained in \p data.
|
|
||||||
* \param[in] data The buffer from which to load the representation.
|
|
||||||
* \param[in] data_length The size in bytes of \p data.
|
|
||||||
* \param[out] p_ecp Returns a pointer to an ECP context on success.
|
|
||||||
* The caller is responsible for freeing both the
|
|
||||||
* contents of the context and the context itself
|
|
||||||
* when done.
|
|
||||||
*/
|
|
||||||
static psa_status_t mbedtls_psa_ecp_load_representation(
|
|
||||||
psa_key_type_t type, const uint8_t *data, size_t data_length,
|
|
||||||
mbedtls_ecp_keypair **p_ecp )
|
|
||||||
{
|
|
||||||
mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
|
|
||||||
psa_status_t status;
|
|
||||||
mbedtls_ecp_keypair *ecp = NULL;
|
|
||||||
size_t curve_size = data_length;
|
|
||||||
|
|
||||||
if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
|
|
||||||
PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
|
|
||||||
{
|
|
||||||
/* A Weierstrass public key is represented as:
|
|
||||||
* - The byte 0x04;
|
|
||||||
* - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
|
|
||||||
* - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
|
|
||||||
* So its data length is 2m+1 where m is the curve size in bits.
|
|
||||||
*/
|
|
||||||
if( ( data_length & 1 ) == 0 )
|
|
||||||
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
||||||
curve_size = data_length / 2;
|
|
||||||
|
|
||||||
/* Montgomery public keys are represented in compressed format, meaning
|
|
||||||
* their curve_size is equal to the amount of input. */
|
|
||||||
|
|
||||||
/* Private keys are represented in uncompressed private random integer
|
|
||||||
* format, meaning their curve_size is equal to the amount of input. */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Allocate and initialize a key representation. */
|
|
||||||
ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
|
|
||||||
if( ecp == NULL )
|
|
||||||
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
||||||
mbedtls_ecp_keypair_init( ecp );
|
|
||||||
|
|
||||||
/* Load the group. */
|
|
||||||
grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
|
|
||||||
curve_size );
|
|
||||||
if( grp_id == MBEDTLS_ECP_DP_NONE )
|
|
||||||
{
|
|
||||||
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
status = mbedtls_to_psa_error(
|
|
||||||
mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
|
|
||||||
if( status != PSA_SUCCESS )
|
|
||||||
goto exit;
|
|
||||||
|
|
||||||
/* Load the key material. */
|
|
||||||
if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
|
|
||||||
{
|
|
||||||
/* Load the public value. */
|
|
||||||
status = mbedtls_to_psa_error(
|
|
||||||
mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
|
|
||||||
data,
|
|
||||||
data_length ) );
|
|
||||||
if( status != PSA_SUCCESS )
|
|
||||||
goto exit;
|
|
||||||
|
|
||||||
/* Check that the point is on the curve. */
|
|
||||||
status = mbedtls_to_psa_error(
|
|
||||||
mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
|
|
||||||
if( status != PSA_SUCCESS )
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Load and validate the secret value. */
|
|
||||||
status = mbedtls_to_psa_error(
|
|
||||||
mbedtls_ecp_read_key( ecp->grp.id,
|
|
||||||
ecp,
|
|
||||||
data,
|
|
||||||
data_length ) );
|
|
||||||
if( status != PSA_SUCCESS )
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
*p_ecp = ecp;
|
|
||||||
exit:
|
|
||||||
if( status != PSA_SUCCESS )
|
|
||||||
{
|
|
||||||
mbedtls_ecp_keypair_free( ecp );
|
|
||||||
mbedtls_free( ecp );
|
|
||||||
}
|
|
||||||
|
|
||||||
return( status );
|
|
||||||
}
|
|
||||||
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
|
|
||||||
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
|
|
||||||
* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
|
||||||
* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
|
|
||||||
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
|
||||||
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
|
||||||
/** Export an ECP key to export representation
|
/** Export an ECP key to export representation
|
||||||
|
|
139
library/psa_crypto_ecp.c
Normal file
139
library/psa_crypto_ecp.c
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
/*
|
||||||
|
* PSA ECP layer on top of Mbed TLS crypto
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* Copyright The Mbed TLS Contributors
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
* not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||||
|
|
||||||
|
#include <psa/crypto.h>
|
||||||
|
#include "psa_crypto_core.h"
|
||||||
|
#include "psa_crypto_ecp.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "mbedtls/platform.h"
|
||||||
|
#if !defined(MBEDTLS_PLATFORM_C)
|
||||||
|
#define mbedtls_calloc calloc
|
||||||
|
#define mbedtls_free free
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <mbedtls/ecp.h>
|
||||||
|
#include <mbedtls/error.h>
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
|
||||||
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
|
||||||
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
||||||
|
defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
|
||||||
|
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
|
||||||
|
psa_status_t mbedtls_psa_ecp_load_representation(
|
||||||
|
psa_key_type_t type, const uint8_t *data, size_t data_length,
|
||||||
|
mbedtls_ecp_keypair **p_ecp )
|
||||||
|
{
|
||||||
|
mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
|
||||||
|
psa_status_t status;
|
||||||
|
mbedtls_ecp_keypair *ecp = NULL;
|
||||||
|
size_t curve_size = data_length;
|
||||||
|
|
||||||
|
if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
|
||||||
|
PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
|
||||||
|
{
|
||||||
|
/* A Weierstrass public key is represented as:
|
||||||
|
* - The byte 0x04;
|
||||||
|
* - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
|
||||||
|
* - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
|
||||||
|
* So its data length is 2m+1 where m is the curve size in bits.
|
||||||
|
*/
|
||||||
|
if( ( data_length & 1 ) == 0 )
|
||||||
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
||||||
|
curve_size = data_length / 2;
|
||||||
|
|
||||||
|
/* Montgomery public keys are represented in compressed format, meaning
|
||||||
|
* their curve_size is equal to the amount of input. */
|
||||||
|
|
||||||
|
/* Private keys are represented in uncompressed private random integer
|
||||||
|
* format, meaning their curve_size is equal to the amount of input. */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Allocate and initialize a key representation. */
|
||||||
|
ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
|
||||||
|
if( ecp == NULL )
|
||||||
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
||||||
|
mbedtls_ecp_keypair_init( ecp );
|
||||||
|
|
||||||
|
/* Load the group. */
|
||||||
|
grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
|
||||||
|
curve_size );
|
||||||
|
if( grp_id == MBEDTLS_ECP_DP_NONE )
|
||||||
|
{
|
||||||
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
status = mbedtls_to_psa_error(
|
||||||
|
mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
|
||||||
|
if( status != PSA_SUCCESS )
|
||||||
|
goto exit;
|
||||||
|
|
||||||
|
/* Load the key material. */
|
||||||
|
if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
|
||||||
|
{
|
||||||
|
/* Load the public value. */
|
||||||
|
status = mbedtls_to_psa_error(
|
||||||
|
mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
|
||||||
|
data,
|
||||||
|
data_length ) );
|
||||||
|
if( status != PSA_SUCCESS )
|
||||||
|
goto exit;
|
||||||
|
|
||||||
|
/* Check that the point is on the curve. */
|
||||||
|
status = mbedtls_to_psa_error(
|
||||||
|
mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
|
||||||
|
if( status != PSA_SUCCESS )
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Load and validate the secret value. */
|
||||||
|
status = mbedtls_to_psa_error(
|
||||||
|
mbedtls_ecp_read_key( ecp->grp.id,
|
||||||
|
ecp,
|
||||||
|
data,
|
||||||
|
data_length ) );
|
||||||
|
if( status != PSA_SUCCESS )
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
*p_ecp = ecp;
|
||||||
|
exit:
|
||||||
|
if( status != PSA_SUCCESS )
|
||||||
|
{
|
||||||
|
mbedtls_ecp_keypair_free( ecp );
|
||||||
|
mbedtls_free( ecp );
|
||||||
|
}
|
||||||
|
|
||||||
|
return( status );
|
||||||
|
}
|
||||||
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
|
||||||
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
|
||||||
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
||||||
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
|
||||||
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
|
||||||
|
|
||||||
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
41
library/psa_crypto_ecp.h
Normal file
41
library/psa_crypto_ecp.h
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* PSA ECP layer on top of Mbed TLS crypto
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* Copyright The Mbed TLS Contributors
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
* not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef PSA_CRYPTO_ECP_H
|
||||||
|
#define PSA_CRYPTO_ECP_H
|
||||||
|
|
||||||
|
#include <psa/crypto.h>
|
||||||
|
#include <mbedtls/ecp.h>
|
||||||
|
|
||||||
|
/** Load the contents of a key buffer into an internal ECP representation
|
||||||
|
*
|
||||||
|
* \param[in] type The type of key contained in \p data.
|
||||||
|
* \param[in] data The buffer from which to load the representation.
|
||||||
|
* \param[in] data_length The size in bytes of \p data.
|
||||||
|
* \param[out] p_ecp Returns a pointer to an ECP context on success.
|
||||||
|
* The caller is responsible for freeing both the
|
||||||
|
* contents of the context and the context itself
|
||||||
|
* when done.
|
||||||
|
*/
|
||||||
|
psa_status_t mbedtls_psa_ecp_load_representation( psa_key_type_t type,
|
||||||
|
const uint8_t *data,
|
||||||
|
size_t data_length,
|
||||||
|
mbedtls_ecp_keypair **p_ecp );
|
||||||
|
#endif /* PSA_CRYPTO_ECP */
|
128
library/psa_crypto_rsa.c
Normal file
128
library/psa_crypto_rsa.c
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
/*
|
||||||
|
* PSA RSA layer on top of Mbed TLS crypto
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* Copyright The Mbed TLS Contributors
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
* not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||||
|
|
||||||
|
#include <psa/crypto.h>
|
||||||
|
#include "psa_crypto_core.h"
|
||||||
|
#include "psa_crypto_rsa.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "mbedtls/platform.h"
|
||||||
|
#if !defined(MBEDTLS_PLATFORM_C)
|
||||||
|
#define mbedtls_calloc calloc
|
||||||
|
#define mbedtls_free free
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <mbedtls/rsa.h>
|
||||||
|
#include <mbedtls/error.h>
|
||||||
|
#include <mbedtls/pk.h>
|
||||||
|
#include <mbedtls/pk_internal.h>
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
|
||||||
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
|
||||||
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
|
||||||
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
|
||||||
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
|
||||||
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
||||||
|
|
||||||
|
/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
|
||||||
|
* that are not a multiple of 8) well. For example, there is only
|
||||||
|
* mbedtls_rsa_get_len(), which returns a number of bytes, and no
|
||||||
|
* way to return the exact bit size of a key.
|
||||||
|
* To keep things simple, reject non-byte-aligned key sizes. */
|
||||||
|
static psa_status_t psa_check_rsa_key_byte_aligned(
|
||||||
|
const mbedtls_rsa_context *rsa )
|
||||||
|
{
|
||||||
|
mbedtls_mpi n;
|
||||||
|
psa_status_t status;
|
||||||
|
mbedtls_mpi_init( &n );
|
||||||
|
status = mbedtls_to_psa_error(
|
||||||
|
mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
|
||||||
|
if( status == PSA_SUCCESS )
|
||||||
|
{
|
||||||
|
if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
|
||||||
|
status = PSA_ERROR_NOT_SUPPORTED;
|
||||||
|
}
|
||||||
|
mbedtls_mpi_free( &n );
|
||||||
|
return( status );
|
||||||
|
}
|
||||||
|
|
||||||
|
psa_status_t mbedtls_psa_rsa_load_representation(
|
||||||
|
psa_key_type_t type, const uint8_t *data, size_t data_length,
|
||||||
|
mbedtls_rsa_context **p_rsa )
|
||||||
|
{
|
||||||
|
psa_status_t status;
|
||||||
|
mbedtls_pk_context ctx;
|
||||||
|
size_t bits;
|
||||||
|
mbedtls_pk_init( &ctx );
|
||||||
|
|
||||||
|
/* Parse the data. */
|
||||||
|
if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
|
||||||
|
status = mbedtls_to_psa_error(
|
||||||
|
mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
|
||||||
|
else
|
||||||
|
status = mbedtls_to_psa_error(
|
||||||
|
mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
|
||||||
|
if( status != PSA_SUCCESS )
|
||||||
|
goto exit;
|
||||||
|
|
||||||
|
/* We have something that the pkparse module recognizes. If it is a
|
||||||
|
* valid RSA key, store it. */
|
||||||
|
if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
|
||||||
|
{
|
||||||
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
|
||||||
|
* supports non-byte-aligned key sizes, but not well. For example,
|
||||||
|
* mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
|
||||||
|
bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
|
||||||
|
if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
|
||||||
|
{
|
||||||
|
status = PSA_ERROR_NOT_SUPPORTED;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
|
||||||
|
if( status != PSA_SUCCESS )
|
||||||
|
goto exit;
|
||||||
|
|
||||||
|
/* Copy out the pointer to the RSA context, and reset the PK context
|
||||||
|
* such that pk_free doesn't free the RSA context we just grabbed. */
|
||||||
|
*p_rsa = mbedtls_pk_rsa( ctx );
|
||||||
|
ctx.pk_info = NULL;
|
||||||
|
|
||||||
|
exit:
|
||||||
|
mbedtls_pk_free( &ctx );
|
||||||
|
return( status );
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
|
||||||
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
|
||||||
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
|
||||||
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
|
||||||
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
|
||||||
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
||||||
|
|
||||||
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
41
library/psa_crypto_rsa.h
Normal file
41
library/psa_crypto_rsa.h
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* PSA RSA layer on top of Mbed TLS crypto
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* Copyright The Mbed TLS Contributors
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
* not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef PSA_CRYPTO_RSA_H
|
||||||
|
#define PSA_CRYPTO_RSA_H
|
||||||
|
|
||||||
|
#include <psa/crypto.h>
|
||||||
|
#include <mbedtls/rsa.h>
|
||||||
|
|
||||||
|
/** Load the contents of a key buffer into an internal RSA representation
|
||||||
|
*
|
||||||
|
* \param[in] type The type of key contained in \p data.
|
||||||
|
* \param[in] data The buffer from which to load the representation.
|
||||||
|
* \param[in] data_length The size in bytes of \p data.
|
||||||
|
* \param[out] p_rsa Returns a pointer to an RSA context on success.
|
||||||
|
* The caller is responsible for freeing both the
|
||||||
|
* contents of the context and the context itself
|
||||||
|
* when done.
|
||||||
|
*/
|
||||||
|
psa_status_t mbedtls_psa_rsa_load_representation( psa_key_type_t type,
|
||||||
|
const uint8_t *data,
|
||||||
|
size_t data_length,
|
||||||
|
mbedtls_rsa_context **p_rsa );
|
||||||
|
#endif /* PSA_CRYPTO_RSA_H */
|
|
@ -248,9 +248,11 @@
|
||||||
<ClInclude Include="..\..\library\common.h" />
|
<ClInclude Include="..\..\library\common.h" />
|
||||||
<ClInclude Include="..\..\library\psa_crypto_core.h" />
|
<ClInclude Include="..\..\library\psa_crypto_core.h" />
|
||||||
<ClInclude Include="..\..\library\psa_crypto_driver_wrappers.h" />
|
<ClInclude Include="..\..\library\psa_crypto_driver_wrappers.h" />
|
||||||
|
<ClInclude Include="..\..\library\psa_crypto_ecp.h" />
|
||||||
<ClInclude Include="..\..\library\psa_crypto_invasive.h" />
|
<ClInclude Include="..\..\library\psa_crypto_invasive.h" />
|
||||||
<ClInclude Include="..\..\library\psa_crypto_its.h" />
|
<ClInclude Include="..\..\library\psa_crypto_its.h" />
|
||||||
<ClInclude Include="..\..\library\psa_crypto_random_impl.h" />
|
<ClInclude Include="..\..\library\psa_crypto_random_impl.h" />
|
||||||
|
<ClInclude Include="..\..\library\psa_crypto_rsa.h" />
|
||||||
<ClInclude Include="..\..\library\psa_crypto_se.h" />
|
<ClInclude Include="..\..\library\psa_crypto_se.h" />
|
||||||
<ClInclude Include="..\..\library\psa_crypto_service_integration.h" />
|
<ClInclude Include="..\..\library\psa_crypto_service_integration.h" />
|
||||||
<ClInclude Include="..\..\library\psa_crypto_slot_management.h" />
|
<ClInclude Include="..\..\library\psa_crypto_slot_management.h" />
|
||||||
|
@ -318,6 +320,8 @@
|
||||||
<ClCompile Include="..\..\library\poly1305.c" />
|
<ClCompile Include="..\..\library\poly1305.c" />
|
||||||
<ClCompile Include="..\..\library\psa_crypto.c" />
|
<ClCompile Include="..\..\library\psa_crypto.c" />
|
||||||
<ClCompile Include="..\..\library\psa_crypto_driver_wrappers.c" />
|
<ClCompile Include="..\..\library\psa_crypto_driver_wrappers.c" />
|
||||||
|
<ClCompile Include="..\..\library\psa_crypto_ecp.c" />
|
||||||
|
<ClCompile Include="..\..\library\psa_crypto_rsa.c" />
|
||||||
<ClCompile Include="..\..\library\psa_crypto_se.c" />
|
<ClCompile Include="..\..\library\psa_crypto_se.c" />
|
||||||
<ClCompile Include="..\..\library\psa_crypto_slot_management.c" />
|
<ClCompile Include="..\..\library\psa_crypto_slot_management.c" />
|
||||||
<ClCompile Include="..\..\library\psa_crypto_storage.c" />
|
<ClCompile Include="..\..\library\psa_crypto_storage.c" />
|
||||||
|
|
Loading…
Reference in a new issue