Skip to content

Commit a778af1

Browse files
degjorvanordic-piks
authored andcommitted
nrf_security: cracen: clean up and refactor
Various small fixes Refactor of names to be more consistent Signed-off-by: Dag Erik Gjørvad <[email protected]>
1 parent ba41acf commit a778af1

File tree

23 files changed

+411
-399
lines changed

23 files changed

+411
-399
lines changed

subsys/nrf_security/src/drivers/cracen/cracenpsa/cracenpsa.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ if(CONFIG_PSA_NEED_CRACEN_ASYMMETRIC_SIGNATURE_DRIVER)
5151
${CMAKE_CURRENT_LIST_DIR}/src/ecc.c
5252
${CMAKE_CURRENT_LIST_DIR}/src/ed25519.c
5353
${CMAKE_CURRENT_LIST_DIR}/src/hmac.c
54+
${CMAKE_CURRENT_LIST_DIR}/src/rndinrange.c
5455
)
5556
endif()
5657

@@ -81,11 +82,10 @@ endif()
8182

8283
if(CONFIG_PSA_NEED_CRACEN_KEY_MANAGEMENT_DRIVER OR CONFIG_PSA_NEED_CRACEN_KMU_DRIVER OR CONFIG_MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
8384
list(APPEND cracen_driver_sources
84-
${CMAKE_CURRENT_LIST_DIR}/src/ed25519.c
8585
${CMAKE_CURRENT_LIST_DIR}/src/key_management.c
86-
${CMAKE_CURRENT_LIST_DIR}/src/ed25519.c
8786
${CMAKE_CURRENT_LIST_DIR}/src/ecdsa.c
8887
${CMAKE_CURRENT_LIST_DIR}/src/ecc.c
88+
${CMAKE_CURRENT_LIST_DIR}/src/rndinrange.c
8989
)
9090
endif()
9191

subsys/nrf_security/src/drivers/cracen/cracenpsa/include/cracen_psa_ecdsa.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@
99

1010
#include <psa/crypto.h>
1111

12-
int cracen_ecdsa_verify_message(const char *pubkey, const struct sxhashalg *hashalg,
12+
int cracen_ecdsa_verify_message(const uint8_t *pubkey, const struct sxhashalg *hashalg,
1313
const uint8_t *message, size_t message_length,
1414
const struct sx_pk_ecurve *curve, const uint8_t *signature);
1515

16-
int cracen_ecdsa_verify_digest(const char *pubkey, const uint8_t *digest, size_t digestsz,
16+
int cracen_ecdsa_verify_digest(const uint8_t *pubkey, const uint8_t *digest, size_t digestsz,
1717
const struct sx_pk_ecurve *curve, const uint8_t *signature);
1818

19-
int cracen_ecdsa_sign_message(const struct ecc_priv_key *privkey, const struct sxhashalg *hashalg,
20-
const struct sx_pk_ecurve *curve, const uint8_t *message,
21-
size_t message_length, uint8_t *signature);
19+
int cracen_ecdsa_sign_message(const struct cracen_ecc_priv_key *privkey,
20+
const struct sxhashalg *hashalg, const struct sx_pk_ecurve *curve,
21+
const uint8_t *message, size_t message_length, uint8_t *signature);
2222

23-
int cracen_ecdsa_sign_digest(const struct ecc_priv_key *privkey, const struct sxhashalg *hashalg,
24-
const struct sx_pk_ecurve *curve, const uint8_t *digest,
25-
size_t digest_length, uint8_t *signature);
23+
int cracen_ecdsa_sign_digest(const struct cracen_ecc_priv_key *privkey,
24+
const struct sxhashalg *hashalg, const struct sx_pk_ecurve *curve,
25+
const uint8_t *digest, size_t digest_length, uint8_t *signature);
2626

27-
int cracen_ecdsa_sign_message_deterministic(const struct ecc_priv_key *privkey,
27+
int cracen_ecdsa_sign_message_deterministic(const struct cracen_ecc_priv_key *privkey,
2828
const struct sxhashalg *hashalg,
2929
const struct sx_pk_ecurve *curve,
3030
const uint8_t *message, size_t message_length,
3131
uint8_t *signature);
3232

33-
int cracen_ecdsa_sign_digest_deterministic(const struct ecc_priv_key *privkey,
33+
int cracen_ecdsa_sign_digest_deterministic(const struct cracen_ecc_priv_key *privkey,
3434
const struct sxhashalg *hashalg,
3535
const struct sx_pk_ecurve *curve, const uint8_t *digest,
3636
size_t digestsz, uint8_t *signature);

subsys/nrf_security/src/drivers/cracen/cracenpsa/include/cracen_psa_eddsa.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7-
#include <psa/crypto.h>
7+
#ifndef CRACEN_PSA_EDDSA_H
8+
#define CRACEN_PSA_EDDSA_H
89

9-
int cracen_ed25519_sign(const uint8_t *priv_key, char *signature, const uint8_t *message,
10+
#include <stddef.h>
11+
#include <stdbool.h>
12+
#include <stdint.h>
13+
14+
int cracen_ed25519_sign(const uint8_t *priv_key, uint8_t *signature, const uint8_t *message,
1015
size_t message_length);
1116

12-
int cracen_ed25519_verify(const uint8_t *pub_key, const char *message, size_t message_length,
13-
const char *signature);
17+
int cracen_ed25519_verify(const uint8_t *pub_key, const uint8_t *message, size_t message_length,
18+
const uint8_t *signature);
1419

15-
int cracen_ed25519ph_sign(const uint8_t *priv_key, char *signature, const uint8_t *message,
20+
int cracen_ed25519ph_sign(const uint8_t *priv_key, uint8_t *signature, const uint8_t *message,
1621
size_t message_length, bool is_message);
1722

18-
int cracen_ed25519ph_verify(const uint8_t *pub_key, const char *message, size_t message_length,
19-
const char *signature, bool is_message);
23+
int cracen_ed25519ph_verify(const uint8_t *pub_key, const uint8_t *message, size_t message_length,
24+
const uint8_t *signature, bool is_message);
2025

2126
int cracen_ed25519_create_pubkey(const uint8_t *priv_key, uint8_t *pub_key);
27+
28+
#endif /* CRACEN_PSA_EDDSA_H */

subsys/nrf_security/src/drivers/cracen/cracenpsa/include/cracen_psa_primitives.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -373,25 +373,25 @@ struct cracen_pake_operation {
373373
};
374374
typedef struct cracen_pake_operation cracen_pake_operation_t;
375375

376-
struct ecdsa_signature {
376+
struct cracen_ecdsa_signature {
377377
size_t sz; /** Total signature size, in bytes. */
378-
char *r; /** Signature element "r". */
379-
char *s; /** Signature element "s". */
378+
uint8_t *r; /** Signature element "r". */
379+
uint8_t *s; /** Signature element "s". */
380380
};
381381

382-
struct ecc_priv_key {
382+
struct cracen_ecc_priv_key {
383383
const struct sx_pk_ecurve *curve;
384-
const char *d; /** Private key value d */
384+
const uint8_t *d; /** Private key value d */
385385
};
386386

387-
struct ecc_pub_key {
387+
struct cracen_ecc_pub_key {
388388
const struct sx_pk_ecurve *curve;
389-
char *qx; /** x coordinate of a point on the curve */
390-
char *qy; /** y coordinate of a point on the curve */
389+
uint8_t *qx; /** x coordinate of a point on the curve */
390+
uint8_t *qy; /** y coordinate of a point on the curve */
391391
};
392392

393-
struct ecc_keypair {
394-
struct ecc_priv_key priv_key;
395-
struct ecc_pub_key pub_key;
393+
struct cracen_ecc_keypair {
394+
struct cracen_ecc_priv_key priv_key;
395+
struct cracen_ecc_pub_key pub_key;
396396
};
397397
#endif /* CRACEN_PSA_PRIMITIVES_H */

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/common.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ LOG_MODULE_DECLARE(cracen, CONFIG_CRACEN_LOG_LEVEL);
3737

3838
#ifdef CONFIG_PSA_NEED_CRACEN_PLATFORM_KEYS
3939
/* Address from the IPS. May come from the MDK in the future. */
40-
#define DEVICE_SECRET_LENGTH 4
40+
#define DEVICE_SECRET_LENGTH 4
4141
#define DEVICE_SECRET_ADDRESS ((uint32_t *)0x0E001620)
4242
#endif
4343

@@ -397,7 +397,7 @@ psa_status_t rnd_in_range(uint8_t *n, size_t sz, const uint8_t *upperlimit, size
397397
}
398398
n[0] &= msb_mask;
399399

400-
int ge = si_be_cmp(n, upperlimit, sz, 0);
400+
int ge = cracen_be_cmp(n, upperlimit, sz, 0);
401401

402402
if (ge == -1) {
403403

@@ -903,7 +903,7 @@ psa_status_t cracen_get_opaque_size(const psa_key_attributes_t *attributes, size
903903
return PSA_ERROR_INVALID_ARGUMENT;
904904
}
905905

906-
void be_add(unsigned char *v, size_t sz, size_t summand)
906+
void cracen_be_add(uint8_t *v, size_t sz, size_t summand)
907907
{
908908
while (sz > 0) {
909909
sz--;
@@ -913,7 +913,7 @@ void be_add(unsigned char *v, size_t sz, size_t summand)
913913
}
914914
}
915915

916-
int be_cmp(const unsigned char *a, const unsigned char *b, size_t sz, int carry)
916+
int cracen_be_cmp(const uint8_t *a, const uint8_t *b, size_t sz, int carry)
917917
{
918918
unsigned int neq = 0;
919919
unsigned int gt = 0;
@@ -937,9 +937,9 @@ int be_cmp(const unsigned char *a, const unsigned char *b, size_t sz, int carry)
937937
return (gt ? 1 : 0) - (lt ? 1 : 0);
938938
}
939939

940-
int hash_all_inputs_with_context(struct sxhash *hashopctx, const char *inputs[],
941-
const size_t inputs_lengths[], size_t input_count,
942-
const struct sxhashalg *hashalg, char *digest)
940+
int cracen_hash_all_inputs_with_context(struct sxhash *hashopctx, const uint8_t *inputs[],
941+
const size_t input_lengths[], size_t input_count,
942+
const struct sxhashalg *hashalg, uint8_t *digest)
943943
{
944944
int status;
945945

@@ -949,7 +949,7 @@ int hash_all_inputs_with_context(struct sxhash *hashopctx, const char *inputs[],
949949
}
950950

951951
for (size_t i = 0; i < input_count; i++) {
952-
status = sx_hash_feed(hashopctx, inputs[i], inputs_lengths[i]);
952+
status = sx_hash_feed(hashopctx, inputs[i], input_lengths[i]);
953953
if (status != SX_OK) {
954954
return status;
955955
}
@@ -964,23 +964,25 @@ int hash_all_inputs_with_context(struct sxhash *hashopctx, const char *inputs[],
964964
return status;
965965
}
966966

967-
int hash_all_inputs(const char *inputs[], const size_t inputs_lengths[], size_t input_count,
968-
const struct sxhashalg *hashalg, char *digest)
967+
int cracen_hash_all_inputs(const uint8_t *inputs[], const size_t input_lengths[],
968+
size_t input_count, const struct sxhashalg *hashalg, uint8_t *digest)
969969
{
970970
struct sxhash hashopctx;
971971

972-
return hash_all_inputs_with_context(&hashopctx, inputs, inputs_lengths, input_count,
973-
hashalg, digest);
972+
return cracen_hash_all_inputs_with_context(&hashopctx, inputs, input_lengths, input_count,
973+
hashalg, digest);
974974
}
975975

976-
int hash_input(const char *input, const size_t input_length, const struct sxhashalg *hashalg,
977-
char *digest)
976+
int cracen_hash_input(const uint8_t *input, const size_t input_length,
977+
const struct sxhashalg *hashalg, uint8_t *digest)
978978
{
979-
return hash_all_inputs(&input, &input_length, 1, hashalg, digest);
979+
return cracen_hash_all_inputs(&input, &input_length, 1, hashalg, digest);
980980
}
981981

982-
int hash_input_with_context(struct sxhash *hashopctx, const char *input, const size_t input_length,
983-
const struct sxhashalg *hashalg, char *digest)
982+
int cracen_hash_input_with_context(struct sxhash *hashopctx, const uint8_t *input,
983+
const size_t input_length, const struct sxhashalg *hashalg,
984+
uint8_t *digest)
984985
{
985-
return hash_all_inputs_with_context(hashopctx, &input, &input_length, 1, hashalg, digest);
986+
return cracen_hash_all_inputs_with_context(hashopctx, &input, &input_length, 1, hashalg,
987+
digest);
986988
}

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/common.h

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ enum asn1_tags {
5555
* \param[in] curve_bits Curve bits.
5656
* \param[out] sicurve Pointer to curve struct for Cracen.
5757
*
58-
* \return PSA_SUCCESS on success or a valid PSA error code.
58+
* \return PSA_SUCCESS on success or a valid PSA status code.
5959
*/
6060
psa_status_t cracen_ecc_get_ecurve_from_psa(psa_ecc_family_t curve_family, size_t curve_bits,
6161
const struct sx_pk_ecurve **sicurve);
@@ -98,7 +98,7 @@ static inline size_t cracen_ecc_wstr_expected_pub_key_bytes(size_t priv_key_size
9898
* \param[in] in_pnt The public key to check.
9999
*
100100
* \return PSA_SUCCESS if the public key passed the check, a valid
101-
* PSA error code otherwise.
101+
* PSA status code otherwise.
102102
*
103103
*/
104104
psa_status_t cracen_ecc_check_public_key(const struct sx_pk_ecurve *curve,
@@ -115,7 +115,7 @@ psa_status_t cracen_ecc_check_public_key(const struct sx_pk_ecurve *curve,
115115
* \param[out] modulus Modulus (n) operand of n.
116116
* \param[out] exponent Public or private exponent, depending on \ref extract_pubkey.
117117
*
118-
* \return sicrypto statuscode.
118+
* \return sxsymcrypt status code.
119119
*/
120120
int cracen_signature_get_rsa_key(struct si_rsa_key *rsa, bool extract_pubkey, bool is_key_pair,
121121
const unsigned char *key, size_t keylen, struct sx_buf *modulus,
@@ -146,7 +146,7 @@ int cracen_signature_asn1_get_operand(unsigned char **p, const unsigned char *en
146146
*
147147
* @note Output number and upper limit must be big endian numbers of size @ref sz.
148148
*
149-
* @return psa_status_t
149+
* @return PSA status code.
150150
*/
151151
psa_status_t rnd_in_range(uint8_t *n, size_t sz, const uint8_t *upperlimit, size_t retrylimit);
152152

@@ -162,17 +162,26 @@ void cracen_xorbytes(char *a, const char *b, size_t sz);
162162
/**
163163
* @brief Loads key buffer and attributes.
164164
*
165-
* @return psa_status_t
165+
* @return PSA status code.
166166
*/
167167
psa_status_t cracen_load_keyref(const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
168168
size_t key_buffer_size, struct sxkeyref *k);
169169

170+
/**
171+
* @brief Do ECB operation.
172+
*
173+
* @return PSA status code.
174+
*/
175+
psa_status_t cracen_cipher_crypt_ecb(const struct sxkeyref *key, const uint8_t *input,
176+
size_t input_length, uint8_t *output, size_t output_size,
177+
size_t *output_length, enum cipher_operation dir);
178+
170179
/**
171180
* @brief Prepare ik key.
172181
*
173182
* @param user_data Owner ID.
174183
*
175-
* @return sxsymcrypt error code.
184+
* @return sxsymcrypt status code.
176185
*/
177186
int cracen_prepare_ik_key(const uint8_t *user_data);
178187

@@ -187,7 +196,7 @@ int cracen_prepare_ik_key(const uint8_t *user_data);
187196
* @param summand Summand.
188197
*
189198
*/
190-
void be_add(unsigned char *v, size_t v_size, size_t summand);
199+
void cracen_be_add(uint8_t *v, size_t v_size, size_t summand);
191200

192201
/**
193202
* @brief Big-Endian compare with carry.
@@ -202,51 +211,51 @@ void be_add(unsigned char *v, size_t v_size, size_t summand);
202211
* \retval 1 if a > b.
203212
* \retval -1 if a < b.
204213
*/
205-
int be_cmp(const unsigned char *a, const unsigned char *b, size_t sz, int carry);
214+
int cracen_be_cmp(const uint8_t *a, const uint8_t *b, size_t sz, int carry);
206215

207216
/**
208217
* @brief Hash several elements at different locations in memory
209218
*
210219
* @param inputs[in] Array of pointers to elements that will be hashed.
211-
* @param inputs_lengths[in] Array of lengths of elements to be hashed.
220+
* @param input_lengths[in] Array of lengths of elements to be hashed.
212221
* @param input_count[in] Number of elements to be hashed.
213222
* @param hashalg[in] Hash algorithm to be used in sxhashalg format.
214223
* @param digest[out] Buffer of at least sx_hash_get_alg_digestsz(hashalg) bytes.
215224
*
216225
* @return sxsymcrypt status code.
217226
*/
218-
int hash_all_inputs(const char *inputs[], const size_t inputs_lengths[], size_t input_count,
219-
const struct sxhashalg *hashalg, char *digest);
227+
int cracen_hash_all_inputs(const uint8_t *inputs[], const size_t input_lengths[],
228+
size_t input_count, const struct sxhashalg *hashalg, uint8_t *digest);
220229

221230
/**
222231
* @brief Hash several elements at different locations in memory with a previously created hash
223232
* context(sxhash)
224233
*
225234
* @param sxhashopctx[in] Pointer to the sxhash context.
226235
* @param inputs[in] Array of pointers to elements that will be hashed.
227-
* @param inputs_lengths[in] Array of lengths of elements to be hashed.
236+
* @param input_lengths[in] Array of lengths of elements to be hashed.
228237
* @param input_count[in] Number of elements to be hashed.
229238
* @param hashalg[in] Hash algorithm to be used in sxhashalg format.
230239
* @param digest[out] Buffer of at least sx_hash_get_alg_digestsz(hashalg) bytes.
231240
*
232241
* @return sxsymcrypt status code.
233242
*/
234-
int hash_all_inputs_with_context(struct sxhash *sxhashopctx, const char *inputs[],
235-
const size_t inputs_lengths[], size_t input_count,
236-
const struct sxhashalg *hashalg, char *digest);
243+
int cracen_hash_all_inputs_with_context(struct sxhash *sxhashopctx, const uint8_t *inputs[],
244+
const size_t input_lengths[], size_t input_count,
245+
const struct sxhashalg *hashalg, uint8_t *digest);
237246

238247
/**
239248
* @brief Hash a single element
240249
*
241-
* @param inputs[in] Pointer to the element that will be hashed.
250+
* @param input[in] Pointer to the element that will be hashed.
242251
* @param input_length[in] Length of the element to be hashed.
243252
* @param hashalg[in] Hash algorithm to be used in sxhashalg format.
244253
* @param digest[out] Buffer of at least sx_hash_get_alg_digestsz(hashalg) bytes.
245254
*
246255
* @return sxsymcrypt status code.
247256
*/
248-
int hash_input(const char *input, const size_t input_length, const struct sxhashalg *hashalg,
249-
char *digest);
257+
int cracen_hash_input(const uint8_t *input, const size_t input_length,
258+
const struct sxhashalg *hashalg, uint8_t *digest);
250259

251260
/**
252261
* @brief Hash a single element with a previously created hash context(sxhash)
@@ -259,8 +268,9 @@ int hash_input(const char *input, const size_t input_length, const struct sxhash
259268
*
260269
* @return sxsymcrypt status code.
261270
*/
262-
int hash_input_with_context(struct sxhash *hashopctx, const char *input, const size_t input_length,
263-
const struct sxhashalg *hashalg, char *digest);
271+
int cracen_hash_input_with_context(struct sxhash *hashopctx, const uint8_t *input,
272+
const size_t input_length, const struct sxhashalg *hashalg,
273+
uint8_t *digest);
264274

265275
/**
266276
* @brief Generate a random number within the specified range.
@@ -275,6 +285,6 @@ int hash_input_with_context(struct sxhash *hashopctx, const char *input, const s
275285
* @param out[out] Buffer to store the generated random number.
276286
* The size of `out` should be at least `nsz`.
277287
*
278-
* @return sxsymcrypt status code:
288+
* @return sxsymcrypt status code.
279289
*/
280-
int rndinrange_create(const unsigned char *n, size_t nsz, unsigned char *out);
290+
int cracen_get_rnd_in_range(const uint8_t *n, size_t nsz, uint8_t *out);

0 commit comments

Comments
 (0)