iPXE
hash_df.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 
36 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
37 FILE_SECBOOT ( PERMITTED );
38 
39 /** @file
40  *
41  * Hash-based derivation function (Hash_df)
42  *
43  * This algorithm is designed to comply with ANS X9.82 Part 3-2007
44  * Section 10.5.2. This standard is not freely available, but most of
45  * the text appears to be shared with NIST SP 800-90, which can be
46  * downloaded from
47  *
48  * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
49  *
50  * Where possible, references are given to both documents. In the
51  * case of any disagreement, ANS X9.82 takes priority over NIST SP
52  * 800-90. (In particular, note that some algorithms that are
53  * Approved by NIST SP 800-90 are not Approved by ANS X9.82.)
54  */
55 
56 #include <stdint.h>
57 #include <string.h>
58 #include <assert.h>
59 #include <byteswap.h>
60 #include <ipxe/crypto.h>
61 #include <ipxe/hash_df.h>
62 
63 /**
64  * Distribute entropy throughout a buffer
65  *
66  * @v hash Underlying hash algorithm
67  * @v input Input data
68  * @v input_len Length of input data, in bytes
69  * @v output Output buffer
70  * @v output_len Length of output buffer, in bytes
71  *
72  * This is the Hash_df function defined in ANS X9.82 Part 3-2007
73  * Section 10.5.2 (NIST SP 800-90 Section 10.4.1).
74  *
75  * The number of bits requested is implicit in the length of the
76  * output buffer. Requests must be for an integral number of bytes.
77  *
78  * The output buffer is filled incrementally with each iteration of
79  * the central loop, rather than constructing an overall "temp" and
80  * then taking the leftmost(no_of_bits_to_return) bits.
81  *
82  * There is no way for the Hash_df function to fail. The returned
83  * status SUCCESS is implicit.
84  */
85 void hash_df ( struct digest_algorithm *hash, const void *input,
86  size_t input_len, void *output, size_t output_len ) {
87  uint8_t context[hash->ctxsize];
88  uint8_t digest[hash->digestsize];
89  size_t frag_len;
90  struct {
91  uint8_t pad[3];
92  uint8_t counter;
93  uint32_t no_of_bits_to_return;
94  } __attribute__ (( packed )) prefix;
95  void *temp;
96  size_t remaining;
97 
98  DBGC ( &hash_df, "HASH_DF input:\n" );
99  DBGC_HDA ( &hash_df, 0, input, input_len );
100 
101  /* Sanity checks */
102  assert ( input != NULL );
103  assert ( output != NULL );
104 
105  /* 1. temp = the Null string
106  * 2. len = ceil ( no_of_bits_to_return / outlen )
107  *
108  * (Nothing to do. We fill the output buffer incrementally,
109  * rather than constructing the complete "temp" in-memory.
110  * "len" is implicit in the number of iterations required to
111  * fill the output buffer, and so is not calculated
112  * explicitly.)
113  */
114 
115  /* 3. counter = an 8-bit binary value representing the integer "1" */
116  prefix.counter = 1;
117 
118  /* 4. For i = 1 to len do */
119  for ( temp = output, remaining = output_len ; remaining > 0 ; ) {
120 
121  /* Comment: in step 5.1 (sic), no_of_bits_to_return is
122  * used as a 32-bit string.
123  *
124  * 4.1 temp = temp || Hash ( counter || no_of_bits_to_return
125  * || input_string )
126  */
127  prefix.no_of_bits_to_return = htonl ( output_len * 8 );
128  digest_init ( hash, context );
129  digest_update ( hash, context, &prefix.counter,
130  ( sizeof ( prefix ) -
131  offsetof ( typeof ( prefix ), counter ) ) );
132  digest_update ( hash, context, input, input_len );
133  digest_final ( hash, context, digest );
134 
135  /* 4.2 counter = counter + 1 */
136  prefix.counter++;
137 
138  /* 5. requested_bits = Leftmost ( no_of_bits_to_return )
139  * of temp
140  *
141  * (We fill the output buffer incrementally.)
142  */
143  frag_len = sizeof ( digest );
144  if ( frag_len > remaining )
145  frag_len = remaining;
146  memcpy ( temp, digest, frag_len );
147  temp += frag_len;
148  remaining -= frag_len;
149  }
150 
151  /* 6. Return SUCCESS and requested_bits */
152  DBGC ( &hash_df, "HASH_DF output:\n" );
153  DBGC_HDA ( &hash_df, 0, output, output_len );
154  return;
155 }
#define __attribute__(x)
Definition: compiler.h:10
pseudo_bit_t hash[0x00010]
Definition: arbel.h:13
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:224
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:230
#define DBGC(...)
Definition: compiler.h:505
Definition: bnxt_hsi.h:68
char prefix[4]
Definition: vmconsole.c:53
Cryptographic API.
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:25
u32 pad[9]
Padding.
Definition: ar9003_mac.h:47
Hash-based derivation function (Hash_df)
#define htonl(value)
Definition: byteswap.h:134
FILE_SECBOOT(PERMITTED)
void * memcpy(void *dest, const void *src, size_t len) __nonnull
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define DBGC_HDA(...)
Definition: compiler.h:506
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:219
void hash_df(struct digest_algorithm *hash, const void *input, size_t input_len, void *output, size_t output_len)
Distribute entropy throughout a buffer.
Definition: hash_df.c:85
unsigned char uint8_t
Definition: stdint.h:10
unsigned int uint32_t
Definition: stdint.h:12
A message digest algorithm.
Definition: crypto.h:19
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition: acpi.c:48
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)