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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
25 * Keyed-Hashing for Message Authentication
30 #include <ipxe/crypto.h>
31 #include <ipxe/hmac.h>
34 * Reduce HMAC key length
36 * @v digest Digest algorithm to use
37 * @v digest_ctx Digest context
39 * @v key_len Length of key
41 static void hmac_reduce_key ( struct digest_algorithm *digest,
42 void *key, size_t *key_len ) {
43 uint8_t digest_ctx[digest->ctxsize];
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;
54 * @v digest Digest algorithm to use
55 * @v digest_ctx Digest context
57 * @v key_len Length of key
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
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];
69 /* Reduce key if necessary */
70 if ( *key_len > sizeof ( k_ipad ) )
71 hmac_reduce_key ( digest, key, key_len );
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++ ) {
80 /* Start inner hash */
81 digest_init ( digest, digest_ctx );
82 digest_update ( digest, digest_ctx, k_ipad, sizeof ( k_ipad ) );
88 * @v digest Digest algorithm to use
89 * @v digest_ctx Digest context
91 * @v key_len Length of key
92 * @v hmac HMAC digest to fill in
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
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];
104 /* Reduce key if necessary */
105 if ( *key_len > sizeof ( k_opad ) )
106 hmac_reduce_key ( digest, key, key_len );
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++ ) {
115 /* Finish inner hash */
116 digest_final ( digest, digest_ctx, hmac );
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 );