PSA crypto: PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE macro

Test it for RSA.
This commit is contained in:
Gilles Peskine 2018-02-03 23:57:22 +01:00 committed by itayzafrir
parent 20035e3579
commit 0189e7512d
3 changed files with 86 additions and 4 deletions

View File

@ -103,6 +103,9 @@ typedef enum {
*/
psa_status_t psa_crypto_init(void);
#define BITS_TO_BYTES(bits) (((bits) + 7) / 8)
#define BYTES_TO_BITS(bytes) ((bytes) * 8)
/**@}*/
/** \defgroup crypto_types Key and algorithm types
@ -122,7 +125,8 @@ typedef uint32_t psa_key_type_t;
#define PSA_KEY_TYPE_ASYMMETRIC_MASK 0x60000000
#define PSA_KEY_TYPE_ASYMMETRIC_MASK_PUBLIC 0x40000000
#define PSA_KEY_TYPE_ASYMMETRIC_MASK_KEYPAIR 0x60000000
#define PSA_KEY_TYPE_ECC_TEST_MASK 0x7fff0000
#define PSA_KEY_TYPE_ASYMMETRIC_TEST_MASK 0x5fff0000
#define PSA_KEY_TYPE_RSA_TEST_VALUE 0x40000000
#define PSA_KEY_TYPE_ECC_TEST_VALUE 0x40010000
#define PSA_KEY_TYPE_IS_VENDOR(type) \
@ -133,8 +137,10 @@ typedef uint32_t psa_key_type_t;
(((type) & PSA_KEY_TYPE_ASYMMETRIC_MASK) == PSA_KEY_TYPE_ASYMMETRIC_MASK_PUBLIC)
#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
(((type) & PSA_KEY_TYPE_ASYMMETRIC_MASK) == PSA_KEY_TYPE_ASYMMETRIC_MASK_KEYPAIR)
#define PSA_KEY_TYPE_IS_RSA(type) \
(((type) & PSA_KEY_TYPE_ASYMMETRIC_TEST_MASK) == PSA_KEY_TYPE_RSA_TEST_VALUE)
#define PSA_KEY_TYPE_IS_ECC(type) \
(((type) & PSA_KEY_TYPE_ECC_TEST_MASK) == PSA_KEY_TYPE_ECC_TEST_VALUE)
(((type) & PSA_KEY_TYPE_ASYMMETRIC_TEST_MASK) == PSA_KEY_TYPE_ECC_TEST_VALUE)
typedef uint32_t psa_algorithm_t;
@ -247,6 +253,41 @@ psa_status_t psa_export_key(psa_key_slot_t key,
* @{
*/
/**
* \brief Maximum ECDSA signature size for a given curve bit size
*
* \param curve_bits Curve size in bits
* \return Maximum signature size in bytes
*
* \note This macro returns a compile-time constant if its argument is one.
*
* \warning This macro may evaluate its argument multiple times.
*/
/*
* RFC 4492 page 20:
*
* Ecdsa-Sig-Value ::= SEQUENCE {
* r INTEGER,
* s INTEGER
* }
*
* Size is at most
* 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
* twice that + 1 (tag) + 2 (len) for the sequence
* (assuming curve_bytes is less than 126 for r and s,
* and less than 124 (total len <= 255) for the sequence)
*/
#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
/*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
/*V of r,s*/ ((curve_bits) + 8) / 8))
#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
(PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, BITS_TO_BYTES(key_bits)) : \
PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
0)
/**
* \brief Sign a hash or short message with a private key.
*

View File

@ -37,6 +37,24 @@ import_export:"3082025a0201000281806c49704e91f3df44fc99e9b3c0fee5025cc04d09529a1
#depends_on:MBEDTLS_PK_C:MBEDTLS_PK_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED
#import_export:"3077020101042049c9a8c18c4b885638c431cf1df1c994131609b580d4fd43a0cab17db2f13eeea00a06082a8648ce3d030107a144034200047772656f814b399279d5e1f1781fac6f099a3c5ca1b0e35351834b08b65e0b572590cdaf8f769361bcf34acfc11e5e074e8426bdde04be6e653945449617de45":PSA_KEY_TYPE_ECC_NISTP256R1:256:0:PSA_SUCCESS:1
#
PSA signature size: RSA keypair, 1024 bits, PKCS#1 v1.5 raw
signature_size:PSA_KEY_TYPE_RSA_KEYPAIR:1024:PSA_ALG_RSA_PKCS1V15_RAW:128
PSA signature size: RSA public key, 1024 bits, PKCS#1 v1.5 raw
signature_size:PSA_KEY_TYPE_RSA_PUBLIC_KEY:1024:PSA_ALG_RSA_PKCS1V15_RAW:128
PSA signature size: RSA keypair, 1024 bits, PKCS#1 v1.5 SHA-256
signature_size:PSA_KEY_TYPE_RSA_KEYPAIR:1024:PSA_ALG_RSA_PKCS1V15(PSA_ALG_SHA_256):128
PSA signature size: RSA keypair, 1024 bits, PSS
signature_size:PSA_KEY_TYPE_RSA_KEYPAIR:1024:PSA_ALG_RSA_PSS_MGF1:128
PSA signature size: RSA keypair, 1023 bits, PKCS#1 v1.5 raw
signature_size:PSA_KEY_TYPE_RSA_KEYPAIR:1023:PSA_ALG_RSA_PKCS1V15_RAW:128
PSA signature size: RSA keypair, 1025 bits, PKCS#1 v1.5 raw
signature_size:PSA_KEY_TYPE_RSA_KEYPAIR:1025:PSA_ALG_RSA_PKCS1V15_RAW:129
PSA sign RSA PKCS#1 v1.5, raw
depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_PKCS1_V15
sign_deterministic:PSA_KEY_TYPE_RSA_KEYPAIR:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b24":PSA_ALG_RSA_PKCS1V15_RAW:"616263":"2c7744983f023ac7bb1c55529d83ed11a76a7898a1bb5ce191375a4aa7495a633d27879ff58eba5a57371c34feb1180e8b850d552476ebb5634df620261992f12ebee9097041dbbea85a42d45b344be5073ceb772ffc604954b9158ba81ec3dc4d9d65e3ab7aa318165f38c36f841f1c69cb1cfa494aa5cbb4d6c0efbafb043a"

View File

@ -133,6 +133,18 @@ exit:
}
/* END_CASE */
/* BEGIN_CASE */
void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg )
{
psa_key_type_t type = type_arg;
psa_algorithm_t alg = alg_arg;
size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg);
TEST_ASSERT( actual_size == (size_t) expected_size_arg );
exit:
;
}
/* END_CASE */
/* BEGIN_CASE */
void sign_deterministic( int key_type_arg, char *key_hex,
int alg_arg, char *input_hex, char *output_hex )
@ -142,12 +154,14 @@ void sign_deterministic( int key_type_arg, char *key_hex,
psa_algorithm_t alg = alg_arg;
unsigned char *key_data = NULL;
size_t key_size;
size_t key_bits;
unsigned char *input_data = NULL;
size_t input_size;
unsigned char *output_data = NULL;
size_t output_size;
unsigned char signature[512];
size_t signature_length;
unsigned char *signature = NULL;
size_t signature_size;
key_data = mbedtls_calloc( 1, strlen( key_hex ) / 2 );
TEST_ASSERT( key_data != NULL );
@ -163,11 +177,19 @@ void sign_deterministic( int key_type_arg, char *key_hex,
TEST_ASSERT( psa_import_key( slot, key_type,
key_data, key_size ) == PSA_SUCCESS );
TEST_ASSERT( psa_get_key_information( slot,
NULL,
&key_bits ) == PSA_SUCCESS );
signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits );
TEST_ASSERT( signature_size != 0 );
signature = mbedtls_calloc( 1, signature_size );
TEST_ASSERT( signature != NULL );
TEST_ASSERT( psa_asymmetric_sign( slot, alg,
input_data, input_size,
NULL, 0,
signature, sizeof( signature ),
signature, signature_size,
&signature_length ) == PSA_SUCCESS );
TEST_ASSERT( signature_length == output_size );
TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 );
@ -177,6 +199,7 @@ exit:
mbedtls_free( key_data );
mbedtls_free( input_data );
mbedtls_free( output_data );
mbedtls_free( signature );
mbedtls_psa_crypto_free( );
}
/* END_CASE */