Skip to content

Commit 573589f

Browse files
Convert USE_PSA_CRYPTO pk interface to the new PSA EC curve encoding
1 parent bbed72e commit 573589f

File tree

3 files changed

+70
-53
lines changed

3 files changed

+70
-53
lines changed

include/mbedtls/psa_util.h

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -160,81 +160,96 @@ static inline psa_algorithm_t mbedtls_psa_translate_md( mbedtls_md_type_t md_alg
160160
/* Translations for ECC. */
161161

162162
static inline int mbedtls_psa_get_ecc_oid_from_id(
163-
psa_ecc_curve_t curve, char const **oid, size_t *oid_len )
163+
psa_ecc_curve_t curve, size_t bits,
164+
char const **oid, size_t *oid_len )
164165
{
165166
switch( curve )
166167
{
168+
case PSA_ECC_CURVE_SECP_R1:
169+
switch( bits )
170+
{
167171
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
168-
case PSA_ECC_CURVE_SECP192R1:
169-
*oid = MBEDTLS_OID_EC_GRP_SECP192R1;
170-
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192R1 );
171-
return( 0 );
172+
case 192:
173+
*oid = MBEDTLS_OID_EC_GRP_SECP192R1;
174+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192R1 );
175+
return( 0 );
172176
#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
173177
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
174-
case PSA_ECC_CURVE_SECP224R1:
175-
*oid = MBEDTLS_OID_EC_GRP_SECP224R1;
176-
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224R1 );
177-
return( 0 );
178+
case 224:
179+
*oid = MBEDTLS_OID_EC_GRP_SECP224R1;
180+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224R1 );
181+
return( 0 );
178182
#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
179183
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
180-
case PSA_ECC_CURVE_SECP256R1:
181-
*oid = MBEDTLS_OID_EC_GRP_SECP256R1;
182-
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256R1 );
183-
return( 0 );
184+
case 256:
185+
*oid = MBEDTLS_OID_EC_GRP_SECP256R1;
186+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256R1 );
187+
return( 0 );
184188
#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
185189
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
186-
case PSA_ECC_CURVE_SECP384R1:
187-
*oid = MBEDTLS_OID_EC_GRP_SECP384R1;
188-
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP384R1 );
189-
return( 0 );
190+
case 384:
191+
*oid = MBEDTLS_OID_EC_GRP_SECP384R1;
192+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP384R1 );
193+
return( 0 );
190194
#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
191195
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
192-
case PSA_ECC_CURVE_SECP521R1:
193-
*oid = MBEDTLS_OID_EC_GRP_SECP521R1;
194-
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP521R1 );
195-
return( 0 );
196+
case 521:
197+
*oid = MBEDTLS_OID_EC_GRP_SECP521R1;
198+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP521R1 );
199+
return( 0 );
196200
#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
201+
}
202+
break;
203+
case PSA_ECC_CURVE_SECP_K1:
204+
switch( bits )
205+
{
197206
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
198-
case PSA_ECC_CURVE_SECP192K1:
199-
*oid = MBEDTLS_OID_EC_GRP_SECP192K1;
200-
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192K1 );
201-
return( 0 );
207+
case 192:
208+
*oid = MBEDTLS_OID_EC_GRP_SECP192K1;
209+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192K1 );
210+
return( 0 );
202211
#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
203212
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
204-
case PSA_ECC_CURVE_SECP224K1:
205-
*oid = MBEDTLS_OID_EC_GRP_SECP224K1;
206-
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224K1 );
207-
return( 0 );
213+
case 224:
214+
*oid = MBEDTLS_OID_EC_GRP_SECP224K1;
215+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224K1 );
216+
return( 0 );
208217
#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
209218
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
210-
case PSA_ECC_CURVE_SECP256K1:
211-
*oid = MBEDTLS_OID_EC_GRP_SECP256K1;
212-
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256K1 );
213-
return( 0 );
219+
case 256:
220+
*oid = MBEDTLS_OID_EC_GRP_SECP256K1;
221+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256K1 );
222+
return( 0 );
214223
#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
224+
}
225+
break;
226+
case PSA_ECC_CURVE_BRAINPOOL_P_R1:
227+
switch( bits )
228+
{
215229
#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
216-
case PSA_ECC_CURVE_BRAINPOOL_P256R1:
217-
*oid = MBEDTLS_OID_EC_GRP_BP256R1;
218-
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP256R1 );
219-
return( 0 );
230+
case 256:
231+
*oid = MBEDTLS_OID_EC_GRP_BP256R1;
232+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP256R1 );
233+
return( 0 );
220234
#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
221235
#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
222-
case PSA_ECC_CURVE_BRAINPOOL_P384R1:
223-
*oid = MBEDTLS_OID_EC_GRP_BP384R1;
224-
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP384R1 );
225-
return( 0 );
236+
case 384:
237+
*oid = MBEDTLS_OID_EC_GRP_BP384R1;
238+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP384R1 );
239+
return( 0 );
226240
#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
227241
#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
228-
case PSA_ECC_CURVE_BRAINPOOL_P512R1:
229-
*oid = MBEDTLS_OID_EC_GRP_BP512R1;
230-
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP512R1 );
231-
return( 0 );
242+
case 521:
243+
*oid = MBEDTLS_OID_EC_GRP_BP512R1;
244+
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP512R1 );
245+
return( 0 );
232246
#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
233-
default:
234-
(void) oid;
235-
(void) oid_len;
236-
return( -1 );
247+
}
248+
break;
237249
}
250+
(void) oid;
251+
(void) oid_len;
252+
return( -1 );
238253
}
239254

240255
#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH 1

library/pkwrite.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,20 @@ int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, si
272272
psa_key_type_t key_type;
273273
psa_key_handle_t handle;
274274
psa_ecc_curve_t curve;
275+
size_t bits;
275276

276277
handle = *((psa_key_handle_t*) key->pk_ctx );
277278
if( PSA_SUCCESS != psa_get_key_attributes( handle, &attributes ) )
278279
return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
279280
key_type = psa_get_key_type( &attributes );
281+
bits = psa_get_key_bits( &attributes );
280282
psa_reset_key_attributes( &attributes );
281283

282-
curve = PSA_KEY_TYPE_GET_CURVE( key_type );
284+
curve = PSA_KEY_TYPE_GET_CURVE( key_type ) & 0xff0000;
283285
if( curve == 0 )
284286
return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
285287

286-
ret = mbedtls_psa_get_ecc_oid_from_id( curve, &oid, &oid_len );
288+
ret = mbedtls_psa_get_ecc_oid_from_id( curve, bits, &oid, &oid_len );
287289
if( ret != 0 )
288290
return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
289291

tests/suites/test_suite_pk.function

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ psa_key_handle_t pk_psa_genkey( void )
102102
{
103103
psa_key_handle_t key;
104104
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
105-
const int curve = PSA_ECC_CURVE_SECP256R1;
106-
const psa_key_type_t type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve);
105+
const psa_key_type_t type =
106+
PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_CURVE_SECP_R1 );
107107
const size_t bits = 256;
108108

109109
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );

0 commit comments

Comments
 (0)