xref: /optee_os/core/lib/libtomcrypt/src/prngs/sprng.c (revision 8411e6ad673d20c4742ed30c785e3f5cdea54dfa)
1*8411e6adSJerome Forissier /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2*8411e6adSJerome Forissier /* SPDX-License-Identifier: Unlicense */
35a913ee7SJerome Forissier #include "tomcrypt_private.h"
4b0104773SPascal Brand 
5b0104773SPascal Brand /**
6b0104773SPascal Brand    @file sprng.c
7b0104773SPascal Brand    Secure PRNG, Tom St Denis
8b0104773SPascal Brand */
9b0104773SPascal Brand 
10b0104773SPascal Brand /* A secure PRNG using the RNG functions.  Basically this is a
11b0104773SPascal Brand  * wrapper that allows you to use a secure RNG as a PRNG
12b0104773SPascal Brand  * in the various other functions.
13b0104773SPascal Brand  */
14b0104773SPascal Brand 
15b0104773SPascal Brand #ifdef LTC_SPRNG
16b0104773SPascal Brand 
17b0104773SPascal Brand const struct ltc_prng_descriptor sprng_desc =
18b0104773SPascal Brand {
19b0104773SPascal Brand     "sprng", 0,
20b0104773SPascal Brand     &sprng_start,
21b0104773SPascal Brand     &sprng_add_entropy,
22b0104773SPascal Brand     &sprng_ready,
23b0104773SPascal Brand     &sprng_read,
24b0104773SPascal Brand     &sprng_done,
25b0104773SPascal Brand     &sprng_export,
26b0104773SPascal Brand     &sprng_import,
27b0104773SPascal Brand     &sprng_test
28b0104773SPascal Brand };
29b0104773SPascal Brand 
30b0104773SPascal Brand /**
31b0104773SPascal Brand   Start the PRNG
32b0104773SPascal Brand   @param prng     [out] The PRNG state to initialize
33b0104773SPascal Brand   @return CRYPT_OK if successful
34b0104773SPascal Brand */
sprng_start(prng_state * prng)35b0104773SPascal Brand int sprng_start(prng_state *prng)
36b0104773SPascal Brand {
37a50cb361SMatt Ma    LTC_UNUSED_PARAM(prng);
38b0104773SPascal Brand    return CRYPT_OK;
39b0104773SPascal Brand }
40b0104773SPascal Brand 
41b0104773SPascal Brand /**
42b0104773SPascal Brand   Add entropy to the PRNG state
43b0104773SPascal Brand   @param in       The data to add
44b0104773SPascal Brand   @param inlen    Length of the data to add
45b0104773SPascal Brand   @param prng     PRNG state to update
46b0104773SPascal Brand   @return CRYPT_OK if successful
47b0104773SPascal Brand */
sprng_add_entropy(const unsigned char * in,unsigned long inlen,prng_state * prng)48b0104773SPascal Brand int sprng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
49b0104773SPascal Brand {
50a50cb361SMatt Ma    LTC_UNUSED_PARAM(in);
51a50cb361SMatt Ma    LTC_UNUSED_PARAM(inlen);
52a50cb361SMatt Ma    LTC_UNUSED_PARAM(prng);
53b0104773SPascal Brand    return CRYPT_OK;
54b0104773SPascal Brand }
55b0104773SPascal Brand 
56b0104773SPascal Brand /**
57b0104773SPascal Brand   Make the PRNG ready to read from
58b0104773SPascal Brand   @param prng   The PRNG to make active
59b0104773SPascal Brand   @return CRYPT_OK if successful
60b0104773SPascal Brand */
sprng_ready(prng_state * prng)61b0104773SPascal Brand int sprng_ready(prng_state *prng)
62b0104773SPascal Brand {
63a50cb361SMatt Ma    LTC_UNUSED_PARAM(prng);
64b0104773SPascal Brand    return CRYPT_OK;
65b0104773SPascal Brand }
66b0104773SPascal Brand 
67b0104773SPascal Brand /**
68b0104773SPascal Brand   Read from the PRNG
69b0104773SPascal Brand   @param out      Destination
70b0104773SPascal Brand   @param outlen   Length of output
71b0104773SPascal Brand   @param prng     The active PRNG to read from
72b0104773SPascal Brand   @return Number of octets read
73b0104773SPascal Brand */
sprng_read(unsigned char * out,unsigned long outlen,prng_state * prng)74b0104773SPascal Brand unsigned long sprng_read(unsigned char *out, unsigned long outlen, prng_state *prng)
75b0104773SPascal Brand {
76b0104773SPascal Brand    LTC_ARGCHK(out != NULL);
77a50cb361SMatt Ma    LTC_UNUSED_PARAM(prng);
78b0104773SPascal Brand    return rng_get_bytes(out, outlen, NULL);
79b0104773SPascal Brand }
80b0104773SPascal Brand 
81b0104773SPascal Brand /**
82b0104773SPascal Brand   Terminate the PRNG
83b0104773SPascal Brand   @param prng   The PRNG to terminate
84b0104773SPascal Brand   @return CRYPT_OK if successful
85b0104773SPascal Brand */
sprng_done(prng_state * prng)86b0104773SPascal Brand int sprng_done(prng_state *prng)
87b0104773SPascal Brand {
88a50cb361SMatt Ma    LTC_UNUSED_PARAM(prng);
89b0104773SPascal Brand    return CRYPT_OK;
90b0104773SPascal Brand }
91b0104773SPascal Brand 
92b0104773SPascal Brand /**
93b0104773SPascal Brand   Export the PRNG state
94b0104773SPascal Brand   @param out       [out] Destination
95b0104773SPascal Brand   @param outlen    [in/out] Max size and resulting size of the state
96b0104773SPascal Brand   @param prng      The PRNG to export
97b0104773SPascal Brand   @return CRYPT_OK if successful
98b0104773SPascal Brand */
995a913ee7SJerome Forissier /* NOLINTNEXTLINE(readability-non-const-parameter) - silence clang-tidy warning */
sprng_export(unsigned char * out,unsigned long * outlen,prng_state * prng)100b0104773SPascal Brand int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
101b0104773SPascal Brand {
102b0104773SPascal Brand    LTC_ARGCHK(outlen != NULL);
103a50cb361SMatt Ma    LTC_UNUSED_PARAM(out);
104a50cb361SMatt Ma    LTC_UNUSED_PARAM(prng);
105b0104773SPascal Brand 
106b0104773SPascal Brand    *outlen = 0;
107b0104773SPascal Brand    return CRYPT_OK;
108b0104773SPascal Brand }
109b0104773SPascal Brand 
110b0104773SPascal Brand /**
111b0104773SPascal Brand   Import a PRNG state
112b0104773SPascal Brand   @param in       The PRNG state
113b0104773SPascal Brand   @param inlen    Size of the state
114b0104773SPascal Brand   @param prng     The PRNG to import
115b0104773SPascal Brand   @return CRYPT_OK if successful
116b0104773SPascal Brand */
sprng_import(const unsigned char * in,unsigned long inlen,prng_state * prng)117b0104773SPascal Brand int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
118b0104773SPascal Brand {
119a50cb361SMatt Ma   LTC_UNUSED_PARAM(in);
120a50cb361SMatt Ma   LTC_UNUSED_PARAM(inlen);
121a50cb361SMatt Ma   LTC_UNUSED_PARAM(prng);
122b0104773SPascal Brand    return CRYPT_OK;
123b0104773SPascal Brand }
124b0104773SPascal Brand 
125b0104773SPascal Brand /**
126b0104773SPascal Brand   PRNG self-test
127b0104773SPascal Brand   @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
128b0104773SPascal Brand */
sprng_test(void)129b0104773SPascal Brand int sprng_test(void)
130b0104773SPascal Brand {
1315a913ee7SJerome Forissier #ifndef LTC_TEST
1325a913ee7SJerome Forissier    return CRYPT_NOP;
1335a913ee7SJerome Forissier #else
1345a913ee7SJerome Forissier    prng_state st;
1355a913ee7SJerome Forissier    unsigned char en[] = { 0x01, 0x02, 0x03, 0x04 };
1365a913ee7SJerome Forissier    unsigned char out[1000];
1375a913ee7SJerome Forissier    int err;
1385a913ee7SJerome Forissier 
1395a913ee7SJerome Forissier    if ((err = sprng_start(&st)) != CRYPT_OK)                         return err;
1405a913ee7SJerome Forissier    if ((err = sprng_add_entropy(en, sizeof(en), &st)) != CRYPT_OK)   return err;
1415a913ee7SJerome Forissier    if ((err = sprng_ready(&st)) != CRYPT_OK)                         return err;
1425a913ee7SJerome Forissier    if (sprng_read(out, 500, &st) != 500)                             return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
1435a913ee7SJerome Forissier    if ((err = sprng_done(&st)) != CRYPT_OK)                          return err;
1445a913ee7SJerome Forissier 
145b0104773SPascal Brand    return CRYPT_OK;
1465a913ee7SJerome Forissier #endif
147b0104773SPascal Brand }
148b0104773SPascal Brand 
149b0104773SPascal Brand #endif
150b0104773SPascal Brand 
151b0104773SPascal Brand 
152b0104773SPascal Brand 
153