Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / crypto / hash_df.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  * Hash-based derivation function (Hash_df)
25  *
26  * This algorithm is designed to comply with ANS X9.82 Part 3-2007
27  * Section 10.5.2.  This standard is not freely available, but most of
28  * the text appears to be shared with NIST SP 800-90, which can be
29  * downloaded from
30  *
31  *     http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
32  *
33  * Where possible, references are given to both documents.  In the
34  * case of any disagreement, ANS X9.82 takes priority over NIST SP
35  * 800-90.  (In particular, note that some algorithms that are
36  * Approved by NIST SP 800-90 are not Approved by ANS X9.82.)
37  */
38
39 #include <stdint.h>
40 #include <string.h>
41 #include <assert.h>
42 #include <byteswap.h>
43 #include <ipxe/crypto.h>
44 #include <ipxe/hash_df.h>
45
46 /**
47  * Distribute entropy throughout a buffer
48  *
49  * @v hash              Underlying hash algorithm
50  * @v input             Input data
51  * @v input_len         Length of input data, in bytes
52  * @v output            Output buffer
53  * @v output_len        Length of output buffer, in bytes
54  *
55  * This is the Hash_df function defined in ANS X9.82 Part 3-2007
56  * Section 10.5.2 (NIST SP 800-90 Section 10.4.1).
57  *
58  * The number of bits requested is implicit in the length of the
59  * output buffer.  Requests must be for an integral number of bytes.
60  *
61  * The output buffer is filled incrementally with each iteration of
62  * the central loop, rather than constructing an overall "temp" and
63  * then taking the leftmost(no_of_bits_to_return) bits.
64  *
65  * There is no way for the Hash_df function to fail.  The returned
66  * status SUCCESS is implicit.
67  */
68 void hash_df ( struct digest_algorithm *hash, const void *input,
69                size_t input_len, void *output, size_t output_len ) {
70         uint8_t context[hash->ctxsize];
71         uint8_t digest[hash->digestsize];
72         size_t frag_len;
73         struct {
74                 uint8_t pad[3];
75                 uint8_t counter;
76                 uint32_t no_of_bits_to_return;
77         } __attribute__ (( packed )) prefix;
78         void *temp;
79         size_t remaining;
80
81         DBGC ( &hash_df, "HASH_DF input:\n" );
82         DBGC_HDA ( &hash_df, 0, input, input_len );
83
84         /* Sanity checks */
85         assert ( input != NULL );
86         assert ( output != NULL );
87
88         /* 1.  temp = the Null string
89          * 2.  len = ceil ( no_of_bits_to_return / outlen )
90          *
91          * (Nothing to do.  We fill the output buffer incrementally,
92          * rather than constructing the complete "temp" in-memory.
93          * "len" is implicit in the number of iterations required to
94          * fill the output buffer, and so is not calculated
95          * explicitly.)
96          */
97
98         /* 3.  counter = an 8-bit binary value representing the integer "1" */
99         prefix.counter = 1;
100
101         /* 4.  For i = 1 to len do */
102         for ( temp = output, remaining = output_len ; remaining > 0 ; ) {
103
104                 /* Comment: in step 5.1 (sic), no_of_bits_to_return is
105                  * used as a 32-bit string.
106                  *
107                  * 4.1  temp = temp || Hash ( counter || no_of_bits_to_return
108                  *                            || input_string )
109                  */
110                 prefix.no_of_bits_to_return = htonl ( output_len * 8 );
111                 digest_init ( hash, context );
112                 digest_update ( hash, context, &prefix.counter,
113                                 ( sizeof ( prefix ) -
114                                   offsetof ( typeof ( prefix ), counter ) ) );
115                 digest_update ( hash, context, input, input_len );
116                 digest_final ( hash, context, digest );
117
118                 /* 4.2  counter = counter + 1 */
119                 prefix.counter++;
120
121                 /* 5.    requested_bits = Leftmost ( no_of_bits_to_return )
122                  *       of temp
123                  *
124                  * (We fill the output buffer incrementally.)
125                  */
126                 frag_len = sizeof ( digest );
127                 if ( frag_len > remaining )
128                         frag_len = remaining;
129                 memcpy ( temp, digest, frag_len );
130                 temp += frag_len;
131                 remaining -= frag_len;
132         }
133
134         /* 6.  Return SUCCESS and requested_bits */
135         DBGC ( &hash_df, "HASH_DF output:\n" );
136         DBGC_HDA ( &hash_df, 0, output, output_len );
137         return;
138 }