Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / crypto / hmac.c
1 /*
2  * Copyright (C) 2007 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 /**
23  * @file
24  *
25  * Keyed-Hashing for Message Authentication
26  */
27
28 #include <string.h>
29 #include <assert.h>
30 #include <ipxe/crypto.h>
31 #include <ipxe/hmac.h>
32
33 /**
34  * Reduce HMAC key length
35  *
36  * @v digest            Digest algorithm to use
37  * @v digest_ctx        Digest context
38  * @v key               Key
39  * @v key_len           Length of key
40  */
41 static void hmac_reduce_key ( struct digest_algorithm *digest,
42                               void *key, size_t *key_len ) {
43         uint8_t digest_ctx[digest->ctxsize];
44
45         digest_init ( digest, digest_ctx );
46         digest_update ( digest, digest_ctx, key, *key_len );
47         digest_final ( digest, digest_ctx, key );
48         *key_len = digest->digestsize;
49 }
50
51 /**
52  * Initialise HMAC
53  *
54  * @v digest            Digest algorithm to use
55  * @v digest_ctx        Digest context
56  * @v key               Key
57  * @v key_len           Length of key
58  *
59  * The length of the key should be less than the block size of the
60  * digest algorithm being used.  (If the key length is greater, it
61  * will be replaced with its own digest, and key_len will be updated
62  * accordingly).
63  */
64 void hmac_init ( struct digest_algorithm *digest, void *digest_ctx,
65                  void *key, size_t *key_len ) {
66         unsigned char k_ipad[digest->blocksize];
67         unsigned int i;
68
69         /* Reduce key if necessary */
70         if ( *key_len > sizeof ( k_ipad ) )
71                 hmac_reduce_key ( digest, key, key_len );
72
73         /* Construct input pad */
74         memset ( k_ipad, 0, sizeof ( k_ipad ) );
75         memcpy ( k_ipad, key, *key_len );
76         for ( i = 0 ; i < sizeof ( k_ipad ) ; i++ ) {
77                 k_ipad[i] ^= 0x36;
78         }
79         
80         /* Start inner hash */
81         digest_init ( digest, digest_ctx );
82         digest_update ( digest, digest_ctx, k_ipad, sizeof ( k_ipad ) );
83 }
84
85 /**
86  * Finalise HMAC
87  *
88  * @v digest            Digest algorithm to use
89  * @v digest_ctx        Digest context
90  * @v key               Key
91  * @v key_len           Length of key
92  * @v hmac              HMAC digest to fill in
93  *
94  * The length of the key should be less than the block size of the
95  * digest algorithm being used.  (If the key length is greater, it
96  * will be replaced with its own digest, and key_len will be updated
97  * accordingly).
98  */
99 void hmac_final ( struct digest_algorithm *digest, void *digest_ctx,
100                   void *key, size_t *key_len, void *hmac ) {
101         unsigned char k_opad[digest->blocksize];
102         unsigned int i;
103
104         /* Reduce key if necessary */
105         if ( *key_len > sizeof ( k_opad ) )
106                 hmac_reduce_key ( digest, key, key_len );
107
108         /* Construct output pad */
109         memset ( k_opad, 0, sizeof ( k_opad ) );
110         memcpy ( k_opad, key, *key_len );
111         for ( i = 0 ; i < sizeof ( k_opad ) ; i++ ) {
112                 k_opad[i] ^= 0x5c;
113         }
114         
115         /* Finish inner hash */
116         digest_final ( digest, digest_ctx, hmac );
117
118         /* Perform outer hash */
119         digest_init ( digest, digest_ctx );
120         digest_update ( digest, digest_ctx, k_opad, sizeof ( k_opad ) );
121         digest_update ( digest, digest_ctx, hmac, digest->digestsize );
122         digest_final ( digest, digest_ctx, hmac );
123 }