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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 /**
00022  * @file
00023  *
00024  * Keyed-Hashing for Message Authentication
00025  */
00026 
00027 #include <string.h>
00028 #include <assert.h>
00029 #include <gpxe/crypto.h>
00030 #include <gpxe/hmac.h>
00031 
00032 /**
00033  * Reduce HMAC key length
00034  *
00035  * @v digest            Digest algorithm to use
00036  * @v digest_ctx        Digest context
00037  * @v key               Key
00038  * @v key_len           Length of key
00039  */
00040 static void hmac_reduce_key ( struct digest_algorithm *digest,
00041                               void *key, size_t *key_len ) {
00042         uint8_t digest_ctx[digest->ctxsize];
00043 
00044         digest_init ( digest, digest_ctx );
00045         digest_update ( digest, digest_ctx, key, *key_len );
00046         digest_final ( digest, digest_ctx, key );
00047         *key_len = digest->digestsize;
00048 }
00049 
00050 /**
00051  * Initialise HMAC
00052  *
00053  * @v digest            Digest algorithm to use
00054  * @v digest_ctx        Digest context
00055  * @v key               Key
00056  * @v key_len           Length of key
00057  *
00058  * The length of the key should be less than the block size of the
00059  * digest algorithm being used.  (If the key length is greater, it
00060  * will be replaced with its own digest, and key_len will be updated
00061  * accordingly).
00062  */
00063 void hmac_init ( struct digest_algorithm *digest, void *digest_ctx,
00064                  void *key, size_t *key_len ) {
00065         unsigned char k_ipad[digest->blocksize];
00066         unsigned int i;
00067 
00068         /* Reduce key if necessary */
00069         if ( *key_len > sizeof ( k_ipad ) )
00070                 hmac_reduce_key ( digest, key, key_len );
00071 
00072         /* Construct input pad */
00073         memset ( k_ipad, 0, sizeof ( k_ipad ) );
00074         memcpy ( k_ipad, key, *key_len );
00075         for ( i = 0 ; i < sizeof ( k_ipad ) ; i++ ) {
00076                 k_ipad[i] ^= 0x36;
00077         }
00078         
00079         /* Start inner hash */
00080         digest_init ( digest, digest_ctx );
00081         digest_update ( digest, digest_ctx, k_ipad, sizeof ( k_ipad ) );
00082 }
00083 
00084 /**
00085  * Finalise HMAC
00086  *
00087  * @v digest            Digest algorithm to use
00088  * @v digest_ctx        Digest context
00089  * @v key               Key
00090  * @v key_len           Length of key
00091  * @v hmac              HMAC digest to fill in
00092  *
00093  * The length of the key should be less than the block size of the
00094  * digest algorithm being used.  (If the key length is greater, it
00095  * will be replaced with its own digest, and key_len will be updated
00096  * accordingly).
00097  */
00098 void hmac_final ( struct digest_algorithm *digest, void *digest_ctx,
00099                   void *key, size_t *key_len, void *hmac ) {
00100         unsigned char k_opad[digest->blocksize];
00101         unsigned int i;
00102 
00103         /* Reduce key if necessary */
00104         if ( *key_len > sizeof ( k_opad ) )
00105                 hmac_reduce_key ( digest, key, key_len );
00106 
00107         /* Construct output pad */
00108         memset ( k_opad, 0, sizeof ( k_opad ) );
00109         memcpy ( k_opad, key, *key_len );
00110         for ( i = 0 ; i < sizeof ( k_opad ) ; i++ ) {
00111                 k_opad[i] ^= 0x5c;
00112         }
00113         
00114         /* Finish inner hash */
00115         digest_final ( digest, digest_ctx, hmac );
00116 
00117         /* Perform outer hash */
00118         digest_init ( digest, digest_ctx );
00119         digest_update ( digest, digest_ctx, k_opad, sizeof ( k_opad ) );
00120         digest_update ( digest, digest_ctx, hmac, digest->digestsize );
00121         digest_final ( digest, digest_ctx, hmac );
00122 }

Generated on Tue Apr 6 20:00:52 2010 for gPXE by  doxygen 1.5.7.1