Skip to content

Commit

Permalink
[review] generate random in chunks
Browse files Browse the repository at this point in the history
Signed-off-by: Vesa Jääskeläinen <[email protected]>
  • Loading branch information
vesajaaskelainen committed Feb 13, 2021
1 parent 8325017 commit 16dab17
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions ta/pkcs11/src/pkcs11_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#define TOKEN_COUNT CFG_PKCS11_TA_TOKEN_COUNT
#endif

/* RNG chunk size used to split RNG generation to smaller sizes */
#define RNG_CHUNK_SIZE 512

/*
* Structure tracking client applications
*
Expand Down Expand Up @@ -1552,6 +1555,9 @@ enum pkcs11_rc entry_ck_generate_random(struct pkcs11_client *client,
struct serialargs ctrlargs = { };
struct pkcs11_session *session = NULL;
void *buffer = NULL;
size_t buffer_size = 0;
uint8_t *data = NULL;
size_t left = 0;

if (!client || ptypes != exp_pt)
return PKCS11_CKR_ARGUMENTS_BAD;
Expand All @@ -1571,13 +1577,23 @@ enum pkcs11_rc entry_ck_generate_random(struct pkcs11_client *client,
if (!out->memref.size)
return PKCS11_CKR_OK;

buffer = TEE_Malloc(out->memref.size, TEE_MALLOC_FILL_ZERO);
buffer_size = MIN(out->memref.size, RNG_CHUNK_SIZE);
buffer = TEE_Malloc(buffer_size, TEE_MALLOC_FILL_ZERO);
if (!buffer)
return PKCS11_CKR_DEVICE_MEMORY;

TEE_GenerateRandom(buffer, out->memref.size);
data = out->memref.buffer;
left = out->memref.size;

while (left) {
size_t count = MIN(left, buffer_size);

TEE_MemMove(out->memref.buffer, buffer, out->memref.size);
TEE_GenerateRandom(buffer, count);
TEE_MemMove(data, buffer, count);

data += count;
left -= count;
}

IMSG("PKCS11 session %"PRIu32": generate random", session->handle);

Expand Down

0 comments on commit 16dab17

Please sign in to comment.