Skip to content

Commit bcbba84

Browse files
Vge0rgerlubos
authored andcommitted
nrf_security: Apply is_first_block to Cracen HMAC
The is_first_block variable should be used by the Cracen HMAC as well as CMAC. So move the variable to the MAC operation instead of the CMAC specific one. Also use this variable to HMAC as well. The call sx_hash_resume_state expects that the sx_hash_save_state was called before so this makes sure that the sx_hash_resume_state is only called in this case Signed-off-by: Georgios Vasilakis <[email protected]>
1 parent a4570b6 commit bcbba84

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ struct cracen_mac_operation_s {
192192
/* Buffer for input data to fill up the next block */
193193
uint8_t input_buffer[SX_MAX(SX_HASH_MAX_ENABLED_BLOCK_SIZE, SX_BLKCIPHER_PRIV_SZ)];
194194

195+
bool is_first_block;
195196
union {
196197
#if defined(PSA_NEED_CRACEN_HMAC)
197198
struct {
@@ -203,8 +204,6 @@ struct cracen_mac_operation_s {
203204
#if defined(PSA_NEED_CRACEN_CMAC)
204205
struct {
205206
struct sxmac ctx;
206-
bool is_first_block;
207-
208207
struct sxkeyref keyref;
209208
uint8_t key_buffer[CRACEN_MAX_AES_KEY_SIZE];
210209
} cmac;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ psa_status_t cracen_cmac_setup(cracen_mac_operation_t *operation,
5050

5151
/* As only AES is supported it is always the same block size*/
5252
operation->bytes_left_for_next_block = AES_BLOCK_SIZE;
53-
operation->cmac.is_first_block = true;
53+
operation->is_first_block = true;
5454

5555
return PSA_SUCCESS;
5656
}
@@ -76,7 +76,7 @@ psa_status_t cracen_cmac_update(cracen_mac_operation_t *operation, const uint8_t
7676
/* The state can only be resumed if is not the first time data are
7777
* processed.
7878
*/
79-
if (!operation->cmac.is_first_block) {
79+
if (!operation->is_first_block) {
8080
sx_status = sx_mac_resume_state(&operation->cmac.ctx);
8181
if (sx_status) {
8282
return silex_statuscodes_to_psa(sx_status);
@@ -111,18 +111,18 @@ psa_status_t cracen_cmac_update(cracen_mac_operation_t *operation, const uint8_t
111111
* empty input buffer
112112
*/
113113
operation->bytes_left_for_next_block = AES_BLOCK_SIZE;
114-
operation->cmac.is_first_block = false;
114+
operation->is_first_block = false;
115115
}
116116

117117
if (block_bytes) {
118118
sx_status = sx_mac_feed(&operation->cmac.ctx, input, block_bytes);
119119
if (sx_status) {
120120
return silex_statuscodes_to_psa(sx_status);
121121
}
122-
operation->cmac.is_first_block = false;
122+
operation->is_first_block = false;
123123
}
124124

125-
if (!operation->cmac.is_first_block) {
125+
if (!operation->is_first_block) {
126126
/* save state and wait until processed */
127127
sx_status = sx_mac_save_state(&operation->cmac.ctx);
128128
if (sx_status) {
@@ -156,7 +156,7 @@ psa_status_t cracen_cmac_finish(cracen_mac_operation_t *operation)
156156
return PSA_ERROR_INVALID_ARGUMENT;
157157
}
158158

159-
if (!operation->cmac.is_first_block) {
159+
if (!operation->is_first_block) {
160160
sx_status = sx_mac_resume_state(&operation->cmac.ctx);
161161
if (sx_status) {
162162
return silex_statuscodes_to_psa(sx_status);

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ psa_status_t cracen_hmac_setup(cracen_mac_operation_t *operation,
3737
}
3838

3939
operation->bytes_left_for_next_block = sx_hash_get_alg_blocksz(sx_hash_algo);
40+
operation->is_first_block = true;
4041

4142
return PSA_SUCCESS;
4243
}
@@ -71,6 +72,8 @@ psa_status_t cracen_hmac_update(cracen_mac_operation_t *operation, const uint8_t
7172
return PSA_SUCCESS;
7273
}
7374

75+
operation->is_first_block = false;
76+
7477
/* Feed the data that are currently in the input buffer to the driver */
7578
sx_status = sx_hash_feed(&operation->hmac.hashctx, operation->input_buffer,
7679
(block_size - operation->bytes_left_for_next_block));
@@ -130,9 +133,11 @@ psa_status_t cracen_hmac_finish(cracen_mac_operation_t *operation)
130133
digestsz = sx_hash_get_alg_digestsz(sx_hash_algo);
131134
block_size = sx_hash_get_alg_blocksz(sx_hash_algo);
132135

133-
sx_status = sx_hash_resume_state(&operation->hmac.hashctx);
134-
if (sx_status != SX_OK) {
135-
return silex_statuscodes_to_psa(sx_status);
136+
if (!operation->is_first_block) {
137+
sx_status = sx_hash_resume_state(&operation->hmac.hashctx);
138+
if (sx_status != SX_OK) {
139+
return silex_statuscodes_to_psa(sx_status);
140+
}
136141
}
137142

138143
/* Process the data that are left in the input buffer. */

0 commit comments

Comments
 (0)