xref: /optee_os/core/lib/libtomcrypt/src/prngs/sprng.c (revision 12941fdcbaa31bd0c6ab241022a7eba66c801467)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2001-2007, Tom St Denis
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
30  *
31  * LibTomCrypt is a library that provides various cryptographic
32  * algorithms in a highly modular and flexible manner.
33  *
34  * The library is free for all purposes without any express
35  * guarantee it works.
36  *
37  * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
38  */
39 #include "tomcrypt.h"
40 
41 /**
42    @file sprng.c
43    Secure PRNG, Tom St Denis
44 */
45 
46 /* A secure PRNG using the RNG functions.  Basically this is a
47  * wrapper that allows you to use a secure RNG as a PRNG
48  * in the various other functions.
49  */
50 
51 #ifdef LTC_SPRNG
52 
53 const struct ltc_prng_descriptor sprng_desc =
54 {
55     "sprng", 0,
56     &sprng_start,
57     &sprng_add_entropy,
58     &sprng_ready,
59     &sprng_read,
60     &sprng_done,
61     &sprng_export,
62     &sprng_import,
63     &sprng_test
64 };
65 
66 /**
67   Start the PRNG
68   @param prng     [out] The PRNG state to initialize
69   @return CRYPT_OK if successful
70 */
71 int sprng_start(prng_state *prng)
72 {
73    LTC_UNUSED_PARAM(prng);
74    return CRYPT_OK;
75 }
76 
77 /**
78   Add entropy to the PRNG state
79   @param in       The data to add
80   @param inlen    Length of the data to add
81   @param prng     PRNG state to update
82   @return CRYPT_OK if successful
83 */
84 int sprng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
85 {
86    LTC_UNUSED_PARAM(in);
87    LTC_UNUSED_PARAM(inlen);
88    LTC_UNUSED_PARAM(prng);
89    return CRYPT_OK;
90 }
91 
92 /**
93   Make the PRNG ready to read from
94   @param prng   The PRNG to make active
95   @return CRYPT_OK if successful
96 */
97 int sprng_ready(prng_state *prng)
98 {
99    LTC_UNUSED_PARAM(prng);
100    return CRYPT_OK;
101 }
102 
103 /**
104   Read from the PRNG
105   @param out      Destination
106   @param outlen   Length of output
107   @param prng     The active PRNG to read from
108   @return Number of octets read
109 */
110 unsigned long sprng_read(unsigned char *out, unsigned long outlen, prng_state *prng)
111 {
112    LTC_ARGCHK(out != NULL);
113    LTC_UNUSED_PARAM(prng);
114    return rng_get_bytes(out, outlen, NULL);
115 }
116 
117 /**
118   Terminate the PRNG
119   @param prng   The PRNG to terminate
120   @return CRYPT_OK if successful
121 */
122 int sprng_done(prng_state *prng)
123 {
124    LTC_UNUSED_PARAM(prng);
125    return CRYPT_OK;
126 }
127 
128 /**
129   Export the PRNG state
130   @param out       [out] Destination
131   @param outlen    [in/out] Max size and resulting size of the state
132   @param prng      The PRNG to export
133   @return CRYPT_OK if successful
134 */
135 int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
136 {
137    LTC_ARGCHK(outlen != NULL);
138    LTC_UNUSED_PARAM(out);
139    LTC_UNUSED_PARAM(prng);
140 
141    *outlen = 0;
142    return CRYPT_OK;
143 }
144 
145 /**
146   Import a PRNG state
147   @param in       The PRNG state
148   @param inlen    Size of the state
149   @param prng     The PRNG to import
150   @return CRYPT_OK if successful
151 */
152 int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
153 {
154   LTC_UNUSED_PARAM(in);
155   LTC_UNUSED_PARAM(inlen);
156   LTC_UNUSED_PARAM(prng);
157    return CRYPT_OK;
158 }
159 
160 /**
161   PRNG self-test
162   @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
163 */
164 int sprng_test(void)
165 {
166    return CRYPT_OK;
167 }
168 
169 #endif
170 
171 
172 
173 
174 /* $Source: /cvs/libtom/libtomcrypt/src/prngs/sprng.c,v $ */
175 /* $Revision: 1.6 $ */
176 /* $Date: 2007/05/12 14:32:35 $ */
177