Skip to content

Commit 25384a2

Browse files
committed
psa: Simplify RSA public key format
Remove pkcs-1 and rsaEncryption front matter from RSA public keys. Move code that was shared between RSA and other key types (like EC keys) to be used only with non-RSA keys.
1 parent d3a0c2c commit 25384a2

File tree

6 files changed

+104
-90
lines changed

6 files changed

+104
-90
lines changed

include/psa/crypto.h

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,17 @@ psa_status_t psa_export_key(psa_key_handle_t handle,
474474
* minimize the risk that an invalid input is accidentally interpreted
475475
* according to a different format.
476476
*
477-
* The format is the DER representation defined by RFC 5280 as
478-
* `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
477+
* For standard key types, the output format is as follows:
478+
* - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
479+
* the representation defined by RFC 3279 §2.3.1 as `RSAPublicKey`.
480+
* ```
481+
* RSAPublicKey ::= SEQUENCE {
482+
* modulus INTEGER, -- n
483+
* publicExponent INTEGER } -- e
484+
* ```
485+
*
486+
* For other public key types, the format is the DER representation defined by
487+
* RFC 5280 as `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
479488
* specified below.
480489
* ```
481490
* SubjectPublicKeyInfo ::= SEQUENCE {
@@ -485,21 +494,6 @@ psa_status_t psa_export_key(psa_key_handle_t handle,
485494
* algorithm OBJECT IDENTIFIER,
486495
* parameters ANY DEFINED BY algorithm OPTIONAL }
487496
* ```
488-
*
489-
* - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
490-
* the `subjectPublicKey` format is defined by RFC 3279 §2.3.1 as
491-
* `RSAPublicKey`,
492-
* with the OID `rsaEncryption`,
493-
* and with the parameters `NULL`.
494-
* ```
495-
* pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
496-
* rsadsi(113549) pkcs(1) 1 }
497-
* rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
498-
*
499-
* RSAPublicKey ::= SEQUENCE {
500-
* modulus INTEGER, -- n
501-
* publicExponent INTEGER } -- e
502-
* ```
503497
* - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
504498
* the `subjectPublicKey` format is defined by RFC 3279 §2.3.2 as
505499
* `DSAPublicKey`,

include/psa/crypto_sizes.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -417,25 +417,16 @@
417417
/* Maximum size of the export encoding of an RSA public key.
418418
* Assumes that the public exponent is less than 2^32.
419419
*
420-
* SubjectPublicKeyInfo ::= SEQUENCE {
421-
* algorithm AlgorithmIdentifier,
422-
* subjectPublicKey BIT STRING } -- contains RSAPublicKey
423-
* AlgorithmIdentifier ::= SEQUENCE {
424-
* algorithm OBJECT IDENTIFIER,
425-
* parameters NULL }
426420
* RSAPublicKey ::= SEQUENCE {
427421
* modulus INTEGER, -- n
428422
* publicExponent INTEGER } -- e
429423
*
430-
* - 3 * 4 bytes of SEQUENCE overhead;
431-
* - 1 + 1 + 9 bytes of algorithm (RSA OID);
432-
* - 2 bytes of NULL;
433-
* - 4 bytes of BIT STRING overhead;
424+
* - 4 bytes of SEQUENCE overhead;
434425
* - n : INTEGER;
435426
* - 7 bytes for the public exponent.
436427
*/
437428
#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
438-
(PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 36)
429+
(PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
439430

440431
/* Maximum size of the export encoding of an RSA key pair.
441432
* Assumes thatthe public exponent is less than 2^32 and that the size

library/psa_crypto.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
#include "mbedtls/arc4.h"
6363
#include "mbedtls/asn1.h"
64+
#include "mbedtls/asn1write.h"
6465
#include "mbedtls/bignum.h"
6566
#include "mbedtls/blowfish.h"
6667
#include "mbedtls/camellia.h"
@@ -899,6 +900,22 @@ psa_status_t psa_get_key_information( psa_key_handle_t handle,
899900
return( PSA_SUCCESS );
900901
}
901902

903+
#if defined(MBEDTLS_RSA_C)
904+
static int pk_write_pubkey_simple( mbedtls_pk_context *key,
905+
unsigned char *buf, size_t size )
906+
{
907+
int ret;
908+
unsigned char *c;
909+
size_t len = 0;
910+
911+
c = buf + size;
912+
913+
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
914+
915+
return( (int) len );
916+
}
917+
#endif /* defined(MBEDTLS_RSA_C) */
918+
902919
static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
903920
uint8_t *data,
904921
size_t data_size,
@@ -969,9 +986,20 @@ static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
969986
#endif
970987
}
971988
if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
972-
ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
989+
{
990+
if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
991+
{
992+
ret = pk_write_pubkey_simple( &pk, data, data_size );
993+
}
994+
else
995+
{
996+
ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
997+
}
998+
}
973999
else
1000+
{
9741001
ret = mbedtls_pk_write_key_der( &pk, data, data_size );
1002+
}
9751003
if( ret < 0 )
9761004
{
9771005
/* If data_size is 0 then data may be NULL and then the

0 commit comments

Comments
 (0)