Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / crypto / rbg.c
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * RBG mechanism
25  *
26  * This mechanism is designed to comply with ANS X9.82 Part 4 (April
27  * 2011 Draft) Section 10.  This standard is unfortunately not freely
28  * available.
29  *
30  * The chosen RBG design is that of a DRBG with a live entropy source
31  * with no conditioning function.  Only a single security strength is
32  * supported.  No seedfile is used since there may be no non-volatile
33  * storage available.  The system UUID is used as the personalisation
34  * string.
35  */
36
37 #include <stdint.h>
38 #include <string.h>
39 #include <ipxe/init.h>
40 #include <ipxe/settings.h>
41 #include <ipxe/uuid.h>
42 #include <ipxe/crypto.h>
43 #include <ipxe/drbg.h>
44 #include <ipxe/rbg.h>
45
46 /** The RBG */
47 struct random_bit_generator rbg;
48
49 /**
50  * Start up RBG
51  *
52  * @ret rc              Return status code
53  *
54  * This is the RBG_Startup function defined in ANS X9.82 Part 4 (April
55  * 2011 Draft) Section 9.1.2.2.
56  */
57 static int rbg_startup ( void ) {
58         union uuid uuid;
59         int len;
60         int rc;
61
62         /* Try to obtain system UUID for use as personalisation
63          * string, in accordance with ANS X9.82 Part 3-2007 Section
64          * 8.5.2.  If no UUID is available, proceed without a
65          * personalisation string.
66          */
67         if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting, &uuid ) ) < 0 ) {
68                 rc = len;
69                 DBGC ( &rbg, "RBG could not fetch personalisation string: "
70                        "%s\n", strerror ( rc ) );
71                 len = 0;
72         }
73
74         /* Instantiate DRBG */
75         if ( ( rc = drbg_instantiate ( &rbg.state, &uuid, len ) ) != 0 ) {
76                 DBGC ( &rbg, "RBG could not instantiate DRBG: %s\n",
77                        strerror ( rc ) );
78                 return rc;
79         }
80
81         return 0;
82 }
83
84 /**
85  * Shut down RBG
86  *
87  */
88 static void rbg_shutdown ( void ) {
89
90         /* Uninstantiate DRBG */
91         drbg_uninstantiate ( &rbg.state );
92 }
93
94 /** RBG startup function */
95 static void rbg_startup_fn ( void ) {
96
97         /* Start up RBG.  There is no way to report an error at this
98          * stage, but a failed startup will result in an invalid DRBG
99          * that refuses to generate bits.
100          */
101         rbg_startup();
102 }
103
104 /** RBG shutdown function */
105 static void rbg_shutdown_fn ( int booting __unused ) {
106
107         /* Shut down RBG */
108         rbg_shutdown();
109 }
110
111 /** RBG startup table entry */
112 struct startup_fn startup_rbg __startup_fn ( STARTUP_NORMAL ) = {
113         .startup = rbg_startup_fn,
114         .shutdown = rbg_shutdown_fn,
115 };