iPXE
rbg.c
Go to the documentation of this file.
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 * Alternatively, you may distribute this code in source or binary
24 * form, with or without modification, provided that the following
25 * conditions are met:
26 *
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the above disclaimer.
29 *
30 * 2. Redistributions in binary form must reproduce the above
31 * copyright notice, this list of conditions and the above
32 * disclaimer in the documentation and/or other materials provided
33 * with the distribution.
34 */
35
36FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
37FILE_SECBOOT ( PERMITTED );
38
39/** @file
40 *
41 * RBG mechanism
42 *
43 * This mechanism is designed to comply with ANS X9.82 Part 4 (April
44 * 2011 Draft) Section 10. This standard is unfortunately not freely
45 * available.
46 *
47 * The chosen RBG design is that of a DRBG with a live entropy source
48 * with no conditioning function. Only a single security strength is
49 * supported. No seedfile is used since there may be no non-volatile
50 * storage available. The system UUID is used as the personalisation
51 * string.
52 */
53
54#include <stdint.h>
55#include <string.h>
56#include <ipxe/init.h>
57#include <ipxe/settings.h>
58#include <ipxe/uuid.h>
59#include <ipxe/crypto.h>
60#include <ipxe/drbg.h>
61#include <ipxe/rbg.h>
62
63/** The RBG */
65
66/**
67 * Start up RBG
68 *
69 * @ret rc Return status code
70 *
71 * This is the RBG_Startup function defined in ANS X9.82 Part 4 (April
72 * 2011 Draft) Section 9.1.2.2.
73 */
74static int rbg_startup ( void ) {
75 union uuid uuid;
76 int len;
77 int rc;
78
79 /* Record that startup has been attempted (even if unsuccessful) */
80 rbg.started = 1;
81
82 /* Try to obtain system UUID for use as personalisation
83 * string, in accordance with ANS X9.82 Part 3-2007 Section
84 * 8.5.2. If no UUID is available, proceed without a
85 * personalisation string.
86 */
87 if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting, &uuid ) ) < 0 ) {
88 rc = len;
89 DBGC ( &rbg, "RBG could not fetch personalisation string: "
90 "%s\n", strerror ( rc ) );
91 len = 0;
92 }
93
94 /* Instantiate DRBG */
95 if ( ( rc = drbg_instantiate ( &rbg.state, &uuid, len ) ) != 0 ) {
96 DBGC ( &rbg, "RBG could not instantiate DRBG: %s\n",
97 strerror ( rc ) );
98 return rc;
99 }
100
101 return 0;
102}
103
104/**
105 * Generate bits using RBG
106 *
107 * @v additional Additional input
108 * @v additional_len Length of additional input
109 * @v prediction_resist Prediction resistance is required
110 * @v data Output buffer
111 * @v len Length of output buffer
112 * @ret rc Return status code
113 *
114 * This is the RBG_Generate function defined in ANS X9.82 Part 4
115 * (April 2011 Draft) Section 9.1.2.2.
116 */
117int rbg_generate ( const void *additional, size_t additional_len,
118 int prediction_resist, void *data, size_t len ) {
119
120 /* Attempt startup, if not already attempted */
121 if ( ! rbg.started )
122 rbg_startup();
123
124 /* Generate bits. The DRBG will itself return an error if it
125 * is not valid (e.g. due to an instantiation failure).
126 */
127 return drbg_generate ( &rbg.state, additional, additional_len,
128 prediction_resist, data, len );
129}
130
131/**
132 * Shut down RBG
133 *
134 */
135static void rbg_shutdown ( void ) {
136
137 /* Uninstantiate DRBG */
138 drbg_uninstantiate ( &rbg.state );
139
140 /* Clear startup attempted flag */
141 rbg.started = 0;
142}
143
144/** RBG startup function */
145static void rbg_startup_fn ( void ) {
146
147 /* Start up RBG (if not already started on demand). There is
148 * no way to report an error at this stage, but a failed
149 * startup will result in an invalid DRBG that refuses to
150 * generate bits.
151 */
152 if ( ! rbg.started )
153 rbg_startup();
154}
155
156/** RBG shutdown function */
157static void rbg_shutdown_fn ( int booting __unused ) {
158
159 /* Shut down RBG */
160 rbg_shutdown();
161}
162
163/** RBG startup table entry */
164struct startup_fn startup_rbg __startup_fn ( STARTUP_NORMAL ) = {
165 .name = "rbg",
166 .startup = rbg_startup_fn,
167 .shutdown = rbg_shutdown_fn,
168};
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
void drbg_uninstantiate(struct drbg_state *state)
Uninstantiate DRBG.
Definition drbg.c:424
int drbg_instantiate(struct drbg_state *state, const void *personal, size_t personal_len)
Instantiate DRBG.
Definition drbg.c:79
int drbg_generate(struct drbg_state *state, const void *additional, size_t additional_len, int prediction_resist, void *data, size_t len)
Generate pseudorandom bits using DRBG.
Definition drbg.c:284
DRBG mechanism.
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:573
#define DBGC(...)
Definition compiler.h:505
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define STARTUP_NORMAL
Normal startup.
Definition init.h:65
Cryptographic API.
Configuration settings.
String functions.
#define __startup_fn(startup_order)
Declare a startup/shutdown function.
Definition init.h:53
int rbg_generate(const void *additional, size_t additional_len, int prediction_resist, void *data, size_t len)
Generate bits using RBG.
Definition rbg.c:117
static void rbg_startup_fn(void)
RBG startup function.
Definition rbg.c:145
struct random_bit_generator rbg
The RBG.
Definition rbg.c:64
static void rbg_shutdown(void)
Shut down RBG.
Definition rbg.c:135
static void rbg_shutdown_fn(int booting __unused)
RBG shutdown function.
Definition rbg.c:157
static int rbg_startup(void)
Start up RBG.
Definition rbg.c:74
RBG mechanism.
uint16_t additional
Additional sense code and qualifier.
Definition scsi.h:13
int fetch_uuid_setting(struct settings *settings, const struct setting *setting, union uuid *uuid)
Fetch value of UUID setting.
Definition settings.c:1085
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
A startup/shutdown function.
Definition init.h:43
A universally unique ID.
Definition uuid.h:16
Universally unique IDs.