These changes are the raw update to qemu-2.6.
[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  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27  *
28  * RBG mechanism
29  *
30  * This mechanism is designed to comply with ANS X9.82 Part 4 (April
31  * 2011 Draft) Section 10.  This standard is unfortunately not freely
32  * available.
33  *
34  * The chosen RBG design is that of a DRBG with a live entropy source
35  * with no conditioning function.  Only a single security strength is
36  * supported.  No seedfile is used since there may be no non-volatile
37  * storage available.  The system UUID is used as the personalisation
38  * string.
39  */
40
41 #include <stdint.h>
42 #include <string.h>
43 #include <ipxe/init.h>
44 #include <ipxe/settings.h>
45 #include <ipxe/uuid.h>
46 #include <ipxe/crypto.h>
47 #include <ipxe/drbg.h>
48 #include <ipxe/rbg.h>
49
50 /** The RBG */
51 struct random_bit_generator rbg;
52
53 /**
54  * Start up RBG
55  *
56  * @ret rc              Return status code
57  *
58  * This is the RBG_Startup function defined in ANS X9.82 Part 4 (April
59  * 2011 Draft) Section 9.1.2.2.
60  */
61 static int rbg_startup ( void ) {
62         union uuid uuid;
63         int len;
64         int rc;
65
66         /* Try to obtain system UUID for use as personalisation
67          * string, in accordance with ANS X9.82 Part 3-2007 Section
68          * 8.5.2.  If no UUID is available, proceed without a
69          * personalisation string.
70          */
71         if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting, &uuid ) ) < 0 ) {
72                 rc = len;
73                 DBGC ( &rbg, "RBG could not fetch personalisation string: "
74                        "%s\n", strerror ( rc ) );
75                 len = 0;
76         }
77
78         /* Instantiate DRBG */
79         if ( ( rc = drbg_instantiate ( &rbg.state, &uuid, len ) ) != 0 ) {
80                 DBGC ( &rbg, "RBG could not instantiate DRBG: %s\n",
81                        strerror ( rc ) );
82                 return rc;
83         }
84
85         return 0;
86 }
87
88 /**
89  * Shut down RBG
90  *
91  */
92 static void rbg_shutdown ( void ) {
93
94         /* Uninstantiate DRBG */
95         drbg_uninstantiate ( &rbg.state );
96 }
97
98 /** RBG startup function */
99 static void rbg_startup_fn ( void ) {
100
101         /* Start up RBG.  There is no way to report an error at this
102          * stage, but a failed startup will result in an invalid DRBG
103          * that refuses to generate bits.
104          */
105         rbg_startup();
106 }
107
108 /** RBG shutdown function */
109 static void rbg_shutdown_fn ( int booting __unused ) {
110
111         /* Shut down RBG */
112         rbg_shutdown();
113 }
114
115 /** RBG startup table entry */
116 struct startup_fn startup_rbg __startup_fn ( STARTUP_NORMAL ) = {
117         .startup = rbg_startup_fn,
118         .shutdown = rbg_shutdown_fn,
119 };