2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
24 * Keyed-Hashing for Message Authentication
29 #include <gpxe/crypto.h>
30 #include <gpxe/hmac.h>
33 * Reduce HMAC key length
35 * @v digest Digest algorithm to use
36 * @v digest_ctx Digest context
38 * @v key_len Length of key
40 static void hmac_reduce_key ( struct digest_algorithm
*digest
,
41 void *key
, size_t *key_len
) {
42 uint8_t digest_ctx
[digest
->ctxsize
];
44 digest_init ( digest
, digest_ctx
);
45 digest_update ( digest
, digest_ctx
, key
, *key_len
);
46 digest_final ( digest
, digest_ctx
, key
);
47 *key_len
= digest
->digestsize
;
53 * @v digest Digest algorithm to use
54 * @v digest_ctx Digest context
56 * @v key_len Length of key
58 * The length of the key should be less than the block size of the
59 * digest algorithm being used. (If the key length is greater, it
60 * will be replaced with its own digest, and key_len will be updated
63 void hmac_init ( struct digest_algorithm
*digest
, void *digest_ctx
,
64 void *key
, size_t *key_len
) {
65 unsigned char k_ipad
[digest
->blocksize
];
68 /* Reduce key if necessary */
69 if ( *key_len
> sizeof ( k_ipad
) )
70 hmac_reduce_key ( digest
, key
, key_len
);
72 /* Construct input pad */
73 memset ( k_ipad
, 0, sizeof ( k_ipad
) );
74 memcpy ( k_ipad
, key
, *key_len
);
75 for ( i
= 0 ; i
< sizeof ( k_ipad
) ; i
++ ) {
79 /* Start inner hash */
80 digest_init ( digest
, digest_ctx
);
81 digest_update ( digest
, digest_ctx
, k_ipad
, sizeof ( k_ipad
) );
87 * @v digest Digest algorithm to use
88 * @v digest_ctx Digest context
90 * @v key_len Length of key
91 * @v hmac HMAC digest to fill in
93 * The length of the key should be less than the block size of the
94 * digest algorithm being used. (If the key length is greater, it
95 * will be replaced with its own digest, and key_len will be updated
98 void hmac_final ( struct digest_algorithm
*digest
, void *digest_ctx
,
99 void *key
, size_t *key_len
, void *hmac
) {
100 unsigned char k_opad
[digest
->blocksize
];
103 /* Reduce key if necessary */
104 if ( *key_len
> sizeof ( k_opad
) )
105 hmac_reduce_key ( digest
, key
, key_len
);
107 /* Construct output pad */
108 memset ( k_opad
, 0, sizeof ( k_opad
) );
109 memcpy ( k_opad
, key
, *key_len
);
110 for ( i
= 0 ; i
< sizeof ( k_opad
) ; i
++ ) {
114 /* Finish inner hash */
115 digest_final ( digest
, digest_ctx
, hmac
);
117 /* Perform outer hash */
118 digest_init ( digest
, digest_ctx
);
119 digest_update ( digest
, digest_ctx
, k_opad
, sizeof ( k_opad
) );
120 digest_update ( digest
, digest_ctx
, hmac
, digest
->digestsize
);
121 digest_final ( digest
, digest_ctx
, hmac
);