iPXE
hmac.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  *
00023  * Alternatively, you may distribute this code in source or binary
00024  * form, with or without modification, provided that the following
00025  * conditions are met:
00026  *
00027  *  1. Redistributions of source code must retain the above copyright
00028  *     notice, this list of conditions and the above disclaimer.
00029  *
00030  *  2. Redistributions in binary form must reproduce the above
00031  *     copyright notice, this list of conditions and the above
00032  *     disclaimer in the documentation and/or other materials provided
00033  *     with the distribution.
00034  */
00035 
00036 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00037 
00038 /**
00039  * @file
00040  *
00041  * Keyed-Hashing for Message Authentication
00042  */
00043 
00044 #include <string.h>
00045 #include <assert.h>
00046 #include <ipxe/crypto.h>
00047 #include <ipxe/hmac.h>
00048 
00049 /**
00050  * Reduce HMAC key length
00051  *
00052  * @v digest            Digest algorithm to use
00053  * @v digest_ctx        Digest context
00054  * @v key               Key
00055  * @v key_len           Length of key
00056  */
00057 static void hmac_reduce_key ( struct digest_algorithm *digest,
00058                               void *key, size_t *key_len ) {
00059         uint8_t digest_ctx[digest->ctxsize];
00060 
00061         digest_init ( digest, digest_ctx );
00062         digest_update ( digest, digest_ctx, key, *key_len );
00063         digest_final ( digest, digest_ctx, key );
00064         *key_len = digest->digestsize;
00065 }
00066 
00067 /**
00068  * Initialise HMAC
00069  *
00070  * @v digest            Digest algorithm to use
00071  * @v digest_ctx        Digest context
00072  * @v key               Key
00073  * @v key_len           Length of key
00074  *
00075  * The length of the key should be less than the block size of the
00076  * digest algorithm being used.  (If the key length is greater, it
00077  * will be replaced with its own digest, and key_len will be updated
00078  * accordingly).
00079  */
00080 void hmac_init ( struct digest_algorithm *digest, void *digest_ctx,
00081                  void *key, size_t *key_len ) {
00082         unsigned char k_ipad[digest->blocksize];
00083         unsigned int i;
00084 
00085         /* Reduce key if necessary */
00086         if ( *key_len > sizeof ( k_ipad ) )
00087                 hmac_reduce_key ( digest, key, key_len );
00088 
00089         /* Construct input pad */
00090         memset ( k_ipad, 0, sizeof ( k_ipad ) );
00091         memcpy ( k_ipad, key, *key_len );
00092         for ( i = 0 ; i < sizeof ( k_ipad ) ; i++ ) {
00093                 k_ipad[i] ^= 0x36;
00094         }
00095         
00096         /* Start inner hash */
00097         digest_init ( digest, digest_ctx );
00098         digest_update ( digest, digest_ctx, k_ipad, sizeof ( k_ipad ) );
00099 }
00100 
00101 /**
00102  * Finalise HMAC
00103  *
00104  * @v digest            Digest algorithm to use
00105  * @v digest_ctx        Digest context
00106  * @v key               Key
00107  * @v key_len           Length of key
00108  * @v hmac              HMAC digest to fill in
00109  *
00110  * The length of the key should be less than the block size of the
00111  * digest algorithm being used.  (If the key length is greater, it
00112  * will be replaced with its own digest, and key_len will be updated
00113  * accordingly).
00114  */
00115 void hmac_final ( struct digest_algorithm *digest, void *digest_ctx,
00116                   void *key, size_t *key_len, void *hmac ) {
00117         unsigned char k_opad[digest->blocksize];
00118         unsigned int i;
00119 
00120         /* Reduce key if necessary */
00121         if ( *key_len > sizeof ( k_opad ) )
00122                 hmac_reduce_key ( digest, key, key_len );
00123 
00124         /* Construct output pad */
00125         memset ( k_opad, 0, sizeof ( k_opad ) );
00126         memcpy ( k_opad, key, *key_len );
00127         for ( i = 0 ; i < sizeof ( k_opad ) ; i++ ) {
00128                 k_opad[i] ^= 0x5c;
00129         }
00130         
00131         /* Finish inner hash */
00132         digest_final ( digest, digest_ctx, hmac );
00133 
00134         /* Perform outer hash */
00135         digest_init ( digest, digest_ctx );
00136         digest_update ( digest, digest_ctx, k_opad, sizeof ( k_opad ) );
00137         digest_update ( digest, digest_ctx, hmac, digest->digestsize );
00138         digest_final ( digest, digest_ctx, hmac );
00139 }