Software APIs
prng_sca.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include "sw/device/tests/penetrationtests/firmware/sca/prng_sca.h"
6 
8 #include "sw/device/lib/base/status.h"
10 #include "sw/device/lib/testing/test_framework/ottf_test_config.h"
11 #include "sw/device/lib/testing/test_framework/ujson_ottf.h"
12 #include "sw/device/lib/ujson/ujson.h"
13 #include "sw/device/sca/lib/prng.h"
14 #include "sw/device/tests/penetrationtests/json/prng_sca_commands.h"
15 
16 /**
17  * Seed PRNG command handler.
18  *
19  * Seed the SCA internal PRNG. Only 4-byte seeds are supported.
20  * The uJSON data contains:
21  * - seed: A buffer holding the seed.
22  * - seed_len: Seed length.
23  *
24  * @param uj The received uJSON data.
25  */
26 status_t handle_prng_sca_seed_prng(ujson_t *uj) {
27  cryptotest_prng_sca_lfsr_t uj_data;
28  TRY(ujson_deserialize_cryptotest_prng_sca_lfsr_t(uj, &uj_data));
29 
30  if (uj_data.seed_length != sizeof(uint32_t)) {
31  return OUT_OF_RANGE();
32  }
33  prng_seed(read_32(uj_data.seed));
34 
35  return OK_STATUS();
36 }
37 
38 /**
39  * PRNG SCA command handler.
40  *
41  * Command hanlder for the PRNG SCA command.
42  *
43  * @param uj The received uJSON data.
44  */
45 status_t handle_prng_sca(ujson_t *uj) {
46  prng_sca_subcommand_t cmd;
47  TRY(ujson_deserialize_prng_sca_subcommand_t(uj, &cmd));
48  switch (cmd) {
49  case kPrngScaSubcommandSeedPrng:
50  return handle_prng_sca_seed_prng(uj);
51  break;
52  default:
53  LOG_ERROR("Unrecognized PRNG SCA subcommand: %d", cmd);
54  return INVALID_ARGUMENT();
55  }
56  return OK_STATUS();
57 }