Skip to content

Commit cc2dbc6

Browse files
Change the encoding of key types to have a parity bit
Change the encoding of key types, EC curve families and DH group families to make the low-order bit a parity bit (with even parity). This ensures that distinct key type values always have a Hamming distance of at least 2, which makes it easier for implementations to resist single bit flips.
1 parent 9d658f6 commit cc2dbc6

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

include/psa/crypto_extra.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
329329
* string. The length of the byte string is the length of the base prime `p`
330330
* in bytes.
331331
*/
332-
#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x6004)
332+
#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x4002)
333333

334334
/** DSA key pair (private and public key).
335335
*
@@ -347,7 +347,7 @@ psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
347347
* Add 1 to the resulting integer and use this as the private key *x*.
348348
*
349349
*/
350-
#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t)0x7004)
350+
#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t)0x7002)
351351

352352
/** Whether a key type is an DSA key (pair or public-only). */
353353
#define PSA_KEY_TYPE_IS_DSA(type) \
@@ -418,7 +418,7 @@ psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
418418
* #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_GROUP_CUSTOM), the group data comes
419419
* from domain parameters set by psa_set_key_domain_parameters().
420420
*/
421-
#define PSA_DH_GROUP_CUSTOM ((psa_dh_group_t) 0x80)
421+
#define PSA_DH_GROUP_CUSTOM ((psa_dh_group_t) 0x7e)
422422

423423

424424
/**

include/psa/crypto_values.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,12 @@
294294
#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x8000)
295295

296296
#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7000)
297-
#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x4000)
298-
#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x5000)
299-
#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x6000)
297+
#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x1000)
298+
#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x2000)
299+
#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x4000)
300300
#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x7000)
301301

302-
#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x1000)
302+
#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x3000)
303303

304304
/** Whether a key type is vendor-defined.
305305
*
@@ -313,8 +313,8 @@
313313
* This encompasses both symmetric keys and non-key data.
314314
*/
315315
#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
316-
(((type) & PSA_KEY_TYPE_CATEGORY_MASK & ~(psa_key_type_t)0x1000) == \
317-
PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
316+
(((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_RAW || \
317+
((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
318318

319319
/** Whether a key type is asymmetric: either a key pair or a public key. */
320320
#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
@@ -357,7 +357,7 @@
357357
*
358358
* A "key" of this type cannot be used for any cryptographic operation.
359359
* Applications may use this type to store arbitrary data in the keystore. */
360-
#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x5001)
360+
#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x1001)
361361

362362
/** HMAC key.
363363
*
@@ -367,21 +367,21 @@
367367
* HMAC keys should generally have the same size as the underlying hash.
368368
* This size can be calculated with #PSA_HASH_SIZE(\c alg) where
369369
* \c alg is the HMAC algorithm or the underlying hash algorithm. */
370-
#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x5100)
370+
#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x1100)
371371

372372
/** A secret for key derivation.
373373
*
374374
* The key policy determines which key derivation algorithm the key
375375
* can be used for.
376376
*/
377-
#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x5200)
377+
#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x1200)
378378

379379
/** Key for a cipher, AEAD or MAC algorithm based on the AES block cipher.
380380
*
381381
* The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
382382
* 32 bytes (AES-256).
383383
*/
384-
#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x4042)
384+
#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x2040)
385385

386386
/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
387387
*
@@ -392,17 +392,17 @@
392392
* deprecated and should only be used to decrypt legacy data. 3-key 3DES
393393
* is weak and deprecated and should only be used in legacy protocols.
394394
*/
395-
#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x4032)
395+
#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x2031)
396396

397397
/** Key for a cipher, AEAD or MAC algorithm based on the
398398
* Camellia block cipher. */
399-
#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x4044)
399+
#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x2043)
400400

401401
/** Key for the RC4 stream cipher.
402402
*
403403
* Note that RC4 is weak and deprecated and should only be used in
404404
* legacy protocols. */
405-
#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x4002)
405+
#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x2002)
406406

407407
/** Key for the ChaCha20 stream cipher or the Chacha20-Poly1305 AEAD algorithm.
408408
*
@@ -411,17 +411,17 @@
411411
* Implementations must support 12-byte nonces, may support 8-byte nonces,
412412
* and should reject other sizes.
413413
*/
414-
#define PSA_KEY_TYPE_CHACHA20 ((psa_key_type_t)0x4004)
414+
#define PSA_KEY_TYPE_CHACHA20 ((psa_key_type_t)0x2004)
415415

416416
/** RSA public key. */
417-
#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x6002)
417+
#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x4001)
418418
/** RSA key pair (private and public key). */
419-
#define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t)0x7002)
419+
#define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t)0x7001)
420420
/** Whether a key type is an RSA key (pair or public-only). */
421421
#define PSA_KEY_TYPE_IS_RSA(type) \
422422
(PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
423423

424-
#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x6100)
424+
#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x4100)
425425
#define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t)0x7100)
426426
#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x00ff)
427427
/** Elliptic curve key pair.
@@ -466,7 +466,7 @@
466466
* _SEC 2: Recommended Elliptic Curve Domain Parameters_.
467467
* https://www.secg.org/sec2-v2.pdf
468468
*/
469-
#define PSA_ECC_CURVE_SECP_K1 ((psa_ecc_curve_t) 0x16)
469+
#define PSA_ECC_CURVE_SECP_K1 ((psa_ecc_curve_t) 0x17)
470470

471471
/** SEC random curves over prime fields.
472472
*
@@ -478,7 +478,7 @@
478478
*/
479479
#define PSA_ECC_CURVE_SECP_R1 ((psa_ecc_curve_t) 0x12)
480480
/* SECP160R2 (SEC2 v1, obsolete) */
481-
#define PSA_ECC_CURVE_SECP_R2 ((psa_ecc_curve_t) 0x1a)
481+
#define PSA_ECC_CURVE_SECP_R2 ((psa_ecc_curve_t) 0x1b)
482482

483483
/** SEC Koblitz curves over binary fields.
484484
*
@@ -488,7 +488,7 @@
488488
* _SEC 2: Recommended Elliptic Curve Domain Parameters_.
489489
* https://www.secg.org/sec2-v2.pdf
490490
*/
491-
#define PSA_ECC_CURVE_SECT_K1 ((psa_ecc_curve_t) 0x26)
491+
#define PSA_ECC_CURVE_SECT_K1 ((psa_ecc_curve_t) 0x27)
492492

493493
/** SEC random curves over binary fields.
494494
*
@@ -508,7 +508,7 @@
508508
* _SEC 2: Recommended Elliptic Curve Domain Parameters_.
509509
* https://www.secg.org/sec2-v2.pdf
510510
*/
511-
#define PSA_ECC_CURVE_SECT_R2 ((psa_ecc_curve_t) 0x2a)
511+
#define PSA_ECC_CURVE_SECT_R2 ((psa_ecc_curve_t) 0x2b)
512512

513513
/** Brainpool P random curves.
514514
*
@@ -529,9 +529,9 @@
529529
* _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
530530
* The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
531531
*/
532-
#define PSA_ECC_CURVE_XED ((psa_ecc_curve_t) 0x40)
532+
#define PSA_ECC_CURVE_XED ((psa_ecc_curve_t) 0x41)
533533

534-
#define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t)0x6200)
534+
#define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t)0x4200)
535535
#define PSA_KEY_TYPE_DH_KEY_PAIR_BASE ((psa_key_type_t)0x7200)
536536
#define PSA_KEY_TYPE_DH_GROUP_MASK ((psa_key_type_t)0x00ff)
537537
/** Diffie-Hellman key pair.
@@ -574,7 +574,7 @@
574574
* 2048, 3072, 4096, 6144, 8192. A given implementation may support
575575
* all of these sizes or only a subet.
576576
*/
577-
#define PSA_DH_GROUP_RFC7919 ((psa_dh_group_t) 0x02)
577+
#define PSA_DH_GROUP_RFC7919 ((psa_dh_group_t) 0x03)
578578

579579
#define PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) \
580580
(((type) >> 4) & 7)

tests/suites/test_suite_psa_crypto_metadata.function

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@
5656
#define TEST_CLASSIFICATION_MACRO( flag, alg, flags ) \
5757
TEST_ASSERT( PSA_##flag( alg ) == !! ( ( flags ) & flag ) )
5858

59+
/* Check the parity of value.
60+
* Return 0 if value has even parity and a nonzero value otherwise. */
61+
int test_parity( uint32_t value )
62+
{
63+
value ^= value >> 16;
64+
value ^= value >> 8;
65+
value ^= value >> 4;
66+
return( 0x9669 & 1 << ( value & 0xf ) );
67+
}
68+
#define TEST_PARITY( value ) \
69+
TEST_ASSERT( test_parity( value ) )
70+
5971
void algorithm_classification( psa_algorithm_t alg, unsigned flags )
6072
{
6173
TEST_CLASSIFICATION_MACRO( ALG_IS_VENDOR_DEFINED, alg, flags );
@@ -113,6 +125,8 @@ void key_type_classification( psa_key_type_t type, unsigned flags )
113125
( PSA_KEY_TYPE_IS_DH( type ) &&
114126
PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) );
115127

128+
TEST_PARITY( type );
129+
116130
exit: ;
117131
}
118132

@@ -481,6 +495,8 @@ void ecc_key_family( int curve_arg )
481495
psa_key_type_t public_type = PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve );
482496
psa_key_type_t pair_type = PSA_KEY_TYPE_ECC_KEY_PAIR( curve );
483497

498+
test_parity( curve );
499+
484500
test_key_type( public_type, KEY_TYPE_IS_ECC | KEY_TYPE_IS_PUBLIC_KEY );
485501
test_key_type( pair_type, KEY_TYPE_IS_ECC | KEY_TYPE_IS_KEY_PAIR );
486502

@@ -496,6 +512,8 @@ void dh_key_family( int group_arg )
496512
psa_key_type_t public_type = PSA_KEY_TYPE_DH_PUBLIC_KEY( group );
497513
psa_key_type_t pair_type = PSA_KEY_TYPE_DH_KEY_PAIR( group );
498514

515+
test_parity( group );
516+
499517
test_key_type( public_type, KEY_TYPE_IS_DH | KEY_TYPE_IS_PUBLIC_KEY );
500518
test_key_type( pair_type, KEY_TYPE_IS_DH | KEY_TYPE_IS_KEY_PAIR );
501519

0 commit comments

Comments
 (0)