Skip to content

Commit fa4486d

Browse files
Specify psa_generator_import_key for each key type
psa_generator_import_key() was only specified for "symmetric keys", and there were some mistakes in the specification. Rewrite the specification and extend it to other key types. * For most private key types, specify that the function draws a byte string repeatedly until the byte string is suitable. * For DES, despite being a symmetric key type, re-drawing is necessary. * For Montgomery curves, despite being asymmetric, no re-drawing is necessary. * Specify the behavior for every standard key type other than RSA. An implementation doesn't have to support all key types, but if it does, it's better to have a standard.
1 parent 3be6b7f commit fa4486d

File tree

1 file changed

+65
-18
lines changed

1 file changed

+65
-18
lines changed

include/psa/crypto.h

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,24 +2878,73 @@ psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
28782878
uint8_t *output,
28792879
size_t output_length);
28802880

2881-
/** Create a symmetric key from data read from a generator.
2882-
*
2883-
* This function reads a sequence of bytes from a generator and imports
2884-
* these bytes as a key.
2885-
* The data that is read is discarded from the generator. The generator's
2886-
* capacity is decreased by the number of bytes read.
2887-
*
2888-
* This function is equivalent to calling #psa_generator_read and
2889-
* passing the resulting output to #psa_import_key, but
2890-
* if the implementation provides an isolation boundary then
2891-
* the key material is not exposed outside the isolation boundary.
2881+
/** Generate a key deterministically from data read from a generator.
2882+
*
2883+
* This function uses the output of a generator to derive a key.
2884+
* How much output it consumes and how the key is derived depends on the
2885+
* key type.
2886+
*
2887+
* - For key types for which the key is an arbitrary sequence of bytes
2888+
* of a given size,
2889+
* this function is functionally equivalent to calling #psa_generator_read
2890+
* and passing the resulting output to #psa_import_key.
2891+
* However, this function has a security benefit:
2892+
* if the implementation provides an isolation boundary then
2893+
* the key material is not exposed outside the isolation boundary.
2894+
* As a consequence, for these key types, this function always consumes
2895+
* exactly (\p bits / 8) bytes from the generator.
2896+
* The following key types defined in this specification follow this scheme:
2897+
*
2898+
* - #PSA_KEY_TYPE_AES;
2899+
* - #PSA_KEY_TYPE_ARIA;
2900+
* - #PSA_KEY_TYPE_ARC4;
2901+
* - #PSA_KEY_TYPE_CAMELLIA;
2902+
* - #PSA_KEY_TYPE_CHACHAPOLY;
2903+
* - #PSA_KEY_TYPE_DERIVE;
2904+
* - #PSA_KEY_TYPE_HMAC.
2905+
*
2906+
* - For ECC keys on a Montgomery elliptic curve
2907+
* (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
2908+
* Montgomery curve), this function always draws a byte string whose
2909+
* length is determined by the curve, and sets the mandatory bits
2910+
* accordingly. That is:
2911+
*
2912+
* - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
2913+
* and process it as specified in RFC 7748 §5.
2914+
* - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
2915+
* and process it as specified in RFC 7748 §5.
2916+
*
2917+
* - For key types for which the key is represented by a single sequence of
2918+
* \p bits bits with constraints as to which bit sequences are acceptable,
2919+
* this function draws a byte string of length (\p bits / 8) bytes rounded
2920+
* up to the nearest whole number of bytes. If the resulting byte string
2921+
* is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
2922+
* This process is repeated until an acceptable byte string is drawn.
2923+
* The byte string drawn from the generator is interpreted as specified
2924+
* for the output produced by psa_export_key().
2925+
* The following key types defined in this specification follow this scheme:
2926+
*
2927+
* - #PSA_KEY_TYPE_DES;
2928+
* - #PSA_KEY_TYPE_DH_KEYPAIR;
2929+
* - #PSA_KEY_TYPE_DSA_KEYPAIR;
2930+
* - ECC keys on a Weierstrass elliptic curve, i.e.
2931+
* #PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
2932+
* Weierstrass curve.
2933+
*
2934+
* - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
2935+
* the way in which the generator output is consumed is
2936+
* implementation-defined.
2937+
*
2938+
* In all cases, the data that is read is discarded from the generator.
2939+
* The generator's capacity is decreased by the number of bytes read.
28922940
*
28932941
* \param handle Handle to the slot where the key will be stored.
28942942
* It must have been obtained by calling
28952943
* psa_allocate_key() or psa_create_key() and must
28962944
* not contain key material yet.
28972945
* \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2898-
* This must be a symmetric key type.
2946+
* This must be a secret key type or a key pair type
2947+
* .
28992948
* \param bits Key size in bits.
29002949
* \param[in,out] generator The generator object to read from.
29012950
*
@@ -2904,12 +2953,10 @@ psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
29042953
* If the key is persistent, the key material and the key's metadata
29052954
* have been saved to persistent storage.
29062955
* \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
2907-
* There were fewer than \p bits * 8 bytes
2908-
* in the generator. Note that in this case, no
2909-
* output is written to the output buffer.
2910-
* The generator's capacity is set to 0, thus
2911-
* subsequent calls to this function will not
2912-
* succeed, even with a smaller output buffer.
2956+
* There was not enough data to create the desired key.
2957+
* Note that in this case, no output is written to the output buffer.
2958+
* The generator's capacity is set to 0, thus subsequent calls to
2959+
* this function will not succeed, even with a smaller output buffer.
29132960
* \retval #PSA_ERROR_NOT_SUPPORTED
29142961
* The key type or key size is not supported, either by the
29152962
* implementation in general or in this particular slot.

0 commit comments

Comments
 (0)