Skip to content

Commit 81f7909

Browse files
Merge pull request #325 from gilles-peskine-arm/psa-sign_hash
Rename psa_asymmetric_{sign_verify} to psa_{sign,verify}_hash
2 parents 1a60fa1 + 0168f2f commit 81f7909

20 files changed

+441
-260
lines changed

docs/getting_started.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ Mbed Crypto supports encrypting, decrypting, signing and verifying messages usin
119119
**Prerequisites to performing asymmetric signature operations:**
120120
* Initialize the library with a successful call to `psa_crypto_init()`.
121121
* Have a valid key with appropriate attributes set:
122-
* Usage flag `PSA_KEY_USAGE_SIGN` to allow signing.
123-
* Usage flag `PSA_KEY_USAGE_VERIFY` to allow signature verification.
122+
* Usage flag `PSA_KEY_USAGE_SIGN_HASH` to allow signing.
123+
* Usage flag `PSA_KEY_USAGE_VERIFY_HASH` to allow signature verification.
124124
* Algorithm set to the desired signature algorithm.
125125
126126
This example shows how to sign a hash that has already been calculated:
@@ -133,7 +133,7 @@ void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
133133
0x60, 0x41, 0x8a, 0xaf, 0x0c, 0xc5, 0xab, 0x58,
134134
0x7f, 0x42, 0xc2, 0x57, 0x0a, 0x88, 0x40, 0x95,
135135
0xa9, 0xe8, 0xcc, 0xac, 0xd0, 0xf6, 0x54, 0x5c};
136-
uint8_t signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
136+
uint8_t signature[PSA_SIGNATURE_MAX_SIZE] = {0};
137137
size_t signature_length;
138138
psa_key_handle_t handle;
139139
@@ -148,7 +148,7 @@ void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
148148
}
149149
150150
/* Set key attributes */
151-
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN);
151+
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
152152
psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_SIGN_RAW);
153153
psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
154154
psa_set_key_bits(&attributes, 1024);
@@ -161,10 +161,10 @@ void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
161161
}
162162
163163
/* Sign message using the key */
164-
status = psa_asymmetric_sign(handle, PSA_ALG_RSA_PKCS1V15_SIGN_RAW,
165-
hash, sizeof(hash),
166-
signature, sizeof(signature),
167-
&signature_length);
164+
status = psa_sign_hash(handle, PSA_ALG_RSA_PKCS1V15_SIGN_RAW,
165+
hash, sizeof(hash),
166+
signature, sizeof(signature),
167+
&signature_length);
168168
if (status != PSA_SUCCESS) {
169169
printf("Failed to sign\n");
170170
return;
@@ -861,7 +861,7 @@ Mbed Crypto provides a simple way to generate a key or key pair.
861861
}
862862

863863
/* Generate a key */
864-
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN);
864+
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
865865
psa_set_key_algorithm(&attributes,
866866
PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256));
867867
psa_set_key_type(&attributes,

include/mbedtls/pk.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ typedef struct mbedtls_pk_rsassa_pss_options
134134
#endif
135135

136136
#if defined(MBEDTLS_USE_PSA_CRYPTO)
137-
#if PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
138-
/* PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE is the maximum size of a signature made
137+
#if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
138+
/* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
139139
* through the PSA API in the PSA representation. */
140140
#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
141-
#define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE
141+
#define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
142142
#endif
143143

144144
#if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE

include/psa/crypto.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2879,7 +2879,7 @@ psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
28792879
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
28802880
* The size of the \p signature buffer is too small. You can
28812881
* determine a sufficient buffer size by calling
2882-
* #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2882+
* #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
28832883
* where \c key_type and \c key_bits are the type and bit-size
28842884
* respectively of \p handle.
28852885
* \retval #PSA_ERROR_NOT_SUPPORTED
@@ -2895,13 +2895,13 @@ psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
28952895
* It is implementation-dependent whether a failure to initialize
28962896
* results in this error code.
28972897
*/
2898-
psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
2899-
psa_algorithm_t alg,
2900-
const uint8_t *hash,
2901-
size_t hash_length,
2902-
uint8_t *signature,
2903-
size_t signature_size,
2904-
size_t *signature_length);
2898+
psa_status_t psa_sign_hash(psa_key_handle_t handle,
2899+
psa_algorithm_t alg,
2900+
const uint8_t *hash,
2901+
size_t hash_length,
2902+
uint8_t *signature,
2903+
size_t signature_size,
2904+
size_t *signature_length);
29052905

29062906
/**
29072907
* \brief Verify the signature a hash or short message using a public key.
@@ -2941,12 +2941,12 @@ psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
29412941
* It is implementation-dependent whether a failure to initialize
29422942
* results in this error code.
29432943
*/
2944-
psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
2945-
psa_algorithm_t alg,
2946-
const uint8_t *hash,
2947-
size_t hash_length,
2948-
const uint8_t *signature,
2949-
size_t signature_length);
2944+
psa_status_t psa_verify_hash(psa_key_handle_t handle,
2945+
psa_algorithm_t alg,
2946+
const uint8_t *hash,
2947+
size_t hash_length,
2948+
const uint8_t *signature,
2949+
size_t signature_length);
29502950

29512951
/**
29522952
* \brief Encrypt a short message with a public key.

include/psa/crypto_compat.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* \file psa/crypto_compat.h
3+
*
4+
* \brief PSA cryptography module: Backward compatibility aliases
5+
*
6+
* This header declares alternative names for macro and functions.
7+
* New application code should not use these names.
8+
* These names may be removed in a future version of Mbed Crypto.
9+
*
10+
* \note This file may not be included directly. Applications must
11+
* include psa/crypto.h.
12+
*/
13+
/*
14+
* Copyright (C) 2019, ARM Limited, All Rights Reserved
15+
* SPDX-License-Identifier: Apache-2.0
16+
*
17+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
18+
* not use this file except in compliance with the License.
19+
* You may obtain a copy of the License at
20+
*
21+
* http://www.apache.org/licenses/LICENSE-2.0
22+
*
23+
* Unless required by applicable law or agreed to in writing, software
24+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26+
* See the License for the specific language governing permissions and
27+
* limitations under the License.
28+
*
29+
* This file is part of mbed TLS (https://tls.mbed.org)
30+
*/
31+
32+
#ifndef PSA_CRYPTO_COMPAT_H
33+
#define PSA_CRYPTO_COMPAT_H
34+
35+
#ifdef __cplusplus
36+
extern "C" {
37+
#endif
38+
39+
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
40+
41+
/*
42+
* Mechanism for declaring deprecated values
43+
*/
44+
#if defined(MBEDTLS_DEPRECATED_WARNING) && !defined(MBEDTLS_PSA_DEPRECATED)
45+
#define MBEDTLS_PSA_DEPRECATED __attribute__((deprecated))
46+
#else
47+
#define MBEDTLS_PSA_DEPRECATED
48+
#endif
49+
50+
typedef MBEDTLS_PSA_DEPRECATED size_t mbedtls_deprecated_size_t;
51+
typedef MBEDTLS_PSA_DEPRECATED psa_status_t mbedtls_deprecated_psa_status_t;
52+
typedef MBEDTLS_PSA_DEPRECATED psa_key_usage_t mbedtls_deprecated_psa_key_usage_t;
53+
54+
#define MBEDTLS_DEPRECATED_CONSTANT( type, value ) \
55+
( (mbedtls_deprecated_##type) ( value ) )
56+
57+
/*
58+
* Deprecated PSA Crypto error code definitions (PSA Crypto API <= 1.0 beta2)
59+
*/
60+
#define PSA_ERROR_UNKNOWN_ERROR \
61+
MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_GENERIC_ERROR )
62+
#define PSA_ERROR_OCCUPIED_SLOT \
63+
MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_ALREADY_EXISTS )
64+
#define PSA_ERROR_EMPTY_SLOT \
65+
MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_DOES_NOT_EXIST )
66+
#define PSA_ERROR_INSUFFICIENT_CAPACITY \
67+
MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_INSUFFICIENT_DATA )
68+
#define PSA_ERROR_TAMPERING_DETECTED \
69+
MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_CORRUPTION_DETECTED )
70+
71+
/*
72+
* Deprecated PSA Crypto numerical encodings (PSA Crypto API <= 1.0 beta3)
73+
*/
74+
#define PSA_KEY_USAGE_SIGN \
75+
MBEDTLS_DEPRECATED_CONSTANT( psa_key_usage_t, PSA_KEY_USAGE_SIGN_HASH )
76+
#define PSA_KEY_USAGE_VERIFY \
77+
MBEDTLS_DEPRECATED_CONSTANT( psa_key_usage_t, PSA_KEY_USAGE_VERIFY_HASH )
78+
79+
/*
80+
* Deprecated PSA Crypto size calculation macros (PSA Crypto API <= 1.0 beta3)
81+
*/
82+
#define PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE \
83+
MBEDTLS_DEPRECATED_CONSTANT( size_t, PSA_SIGNATURE_MAX_SIZE )
84+
#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ) \
85+
MBEDTLS_DEPRECATED_CONSTANT( size_t, PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ) )
86+
87+
/*
88+
* Deprecated PSA Crypto function names (PSA Crypto API <= 1.0 beta3)
89+
*/
90+
/* Make these macros and not wrappers so that there is no cost to
91+
* applications that don't use the deprecated names.
92+
*
93+
* Put backslash-newline after "#define" to bypass check-names.sh which
94+
* would otherwise complain about lowercase macro names.
95+
*/
96+
#define \
97+
psa_asymmetric_sign( key, alg, hash, hash_length, signature, signature_size, signature_length ) \
98+
( (mbedtls_deprecated_psa_status_t) psa_sign_hash( key, alg, hash, hash_length, signature, signature_size, signature_length ) )
99+
#define \
100+
psa_asymmetric_verify( key, alg, hash, hash_length, signature, signature_length ) \
101+
( (mbedtls_deprecated_psa_status_t) psa_verify_hash( key, alg, hash, hash_length, signature, signature_length ) )
102+
103+
#endif /* MBEDTLS_DEPRECATED_REMOVED */
104+
105+
#ifdef __cplusplus
106+
}
107+
#endif
108+
109+
#endif /* PSA_CRYPTO_COMPAT_H */

include/psa/crypto_extra.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,15 @@
3232

3333
#include "mbedtls/platform_util.h"
3434

35+
#include "crypto_compat.h"
36+
3537
#ifdef __cplusplus
3638
extern "C" {
3739
#endif
3840

3941
/* UID for secure storage seed */
4042
#define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52
4143

42-
/*
43-
* Deprecated PSA Crypto error code definitions
44-
*/
45-
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
46-
#define PSA_ERROR_UNKNOWN_ERROR \
47-
MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_GENERIC_ERROR )
48-
#define PSA_ERROR_OCCUPIED_SLOT \
49-
MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_ALREADY_EXISTS )
50-
#define PSA_ERROR_EMPTY_SLOT \
51-
MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_DOES_NOT_EXIST )
52-
#define PSA_ERROR_INSUFFICIENT_CAPACITY \
53-
MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_INSUFFICIENT_DATA )
54-
#define PSA_ERROR_TAMPERING_DETECTED \
55-
MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_CORRUPTION_DETECTED )
56-
#endif
5744

5845
/** \addtogroup attributes
5946
* @{

include/psa/crypto_sizes.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@
411411
#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
412412
(PSA_BITS_TO_BYTES(curve_bits) * 2)
413413

414-
/** Sufficient signature buffer size for psa_asymmetric_sign().
414+
/** Sufficient signature buffer size for psa_sign_hash().
415415
*
416416
* This macro returns a sufficient buffer size for a signature using a key
417417
* of the specified type and size, with the specified algorithm.
@@ -429,31 +429,31 @@
429429
*
430430
* \return If the parameters are valid and supported, return
431431
* a buffer size in bytes that guarantees that
432-
* psa_asymmetric_sign() will not fail with
432+
* psa_sign_hash() will not fail with
433433
* #PSA_ERROR_BUFFER_TOO_SMALL.
434434
* If the parameters are a valid combination that is not supported
435435
* by the implementation, this macro shall return either a
436436
* sensible size or 0.
437437
* If the parameters are not valid, the
438438
* return value is unspecified.
439439
*/
440-
#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
440+
#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
441441
(PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
442442
PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
443443
((void)alg, 0))
444444

445445
#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
446446
PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
447447

448-
/** \def PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE
448+
/** \def PSA_SIGNATURE_MAX_SIZE
449449
*
450450
* Maximum size of an asymmetric signature.
451451
*
452452
* This macro must expand to a compile-time constant integer. This value
453453
* should be the maximum size of a signature supported by the implementation,
454454
* in bytes, and must be no smaller than this maximum.
455455
*/
456-
#define PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE \
456+
#define PSA_SIGNATURE_MAX_SIZE \
457457
(PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
458458
PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
459459
PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
@@ -682,7 +682,7 @@
682682
*
683683
* \return If the parameters are valid and supported, return
684684
* a buffer size in bytes that guarantees that
685-
* psa_asymmetric_sign() will not fail with
685+
* psa_sign_hash() will not fail with
686686
* #PSA_ERROR_BUFFER_TOO_SMALL.
687687
* If the parameters are a valid combination that is not supported
688688
* by the implementation, this macro shall return either a

include/psa/crypto_values.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -766,17 +766,17 @@
766766
* Then you may create and use a key as follows:
767767
* - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
768768
* ```
769-
* psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN); // or VERIFY
769+
* psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
770770
* psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
771771
* ```
772772
* - Import or generate key material.
773-
* - Call psa_asymmetric_sign() or psa_asymmetric_verify(), passing
773+
* - Call psa_sign_hash() or psa_verify_hash(), passing
774774
* an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
775775
* call to sign or verify a message may use a different hash.
776776
* ```
777-
* psa_asymmetric_sign(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
778-
* psa_asymmetric_sign(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
779-
* psa_asymmetric_sign(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
777+
* psa_sign_hash(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
778+
* psa_sign_hash(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
779+
* psa_sign_hash(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
780780
* ```
781781
*
782782
* This value may not be used to build other algorithms that are
@@ -1641,7 +1641,7 @@
16411641
*
16421642
* For a key pair, this concerns the private key.
16431643
*/
1644-
#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
1644+
#define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t)0x00000400)
16451645

16461646
/** Whether the key may be used to verify a message signature.
16471647
*
@@ -1651,7 +1651,7 @@
16511651
*
16521652
* For a key pair, this concerns the public key.
16531653
*/
1654-
#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1654+
#define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t)0x00000800)
16551655

16561656
/** Whether the key may be used to derive other keys.
16571657
*/

library/pk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
621621

622622
/* prepare the key attributes */
623623
psa_set_key_type( &attributes, key_type );
624-
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
624+
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
625625
psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(hash_alg) );
626626

627627
/* import private key into PSA */

library/pk_wrap.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
578578
psa_sig_md = PSA_ALG_ECDSA( psa_md );
579579

580580
psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
581-
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
581+
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
582582
psa_set_key_algorithm( &attributes, psa_sig_md );
583583

584584
status = psa_import_key( &attributes,
@@ -605,9 +605,9 @@ static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
605605
goto cleanup;
606606
}
607607

608-
if( psa_asymmetric_verify( key_handle, psa_sig_md,
609-
hash, hash_len,
610-
buf, 2 * signature_part_size )
608+
if( psa_verify_hash( key_handle, psa_sig_md,
609+
hash, hash_len,
610+
buf, 2 * signature_part_size )
611611
!= PSA_SUCCESS )
612612
{
613613
ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
@@ -1023,8 +1023,8 @@ static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
10231023
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
10241024

10251025
/* make the signature */
1026-
status = psa_asymmetric_sign( *key, alg, hash, hash_len,
1027-
sig, buf_len, sig_len );
1026+
status = psa_sign_hash( *key, alg, hash, hash_len,
1027+
sig, buf_len, sig_len );
10281028
if( status != PSA_SUCCESS )
10291029
return( mbedtls_psa_err_translate_pk( status ) );
10301030

0 commit comments

Comments
 (0)