sha1extra.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
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 #include <gpxe/crypto.h>
00022 #include <gpxe/sha1.h>
00023 #include <gpxe/hmac.h>
00024 #include <stdint.h>
00025 #include <byteswap.h>
00026 
00027 /**
00028  * SHA1 pseudorandom function for creating derived keys
00029  *
00030  * @v key       Master key with which this call is associated
00031  * @v key_len   Length of key
00032  * @v label     NUL-terminated ASCII string describing purpose of PRF data
00033  * @v data      Further data that should be included in the PRF
00034  * @v data_len  Length of further PRF data
00035  * @v prf_len   Bytes of PRF to generate
00036  * @ret prf     Pseudorandom function bytes
00037  *
00038  * This is the PRF variant used by 802.11, defined in IEEE 802.11-2007
00039  * 8.5.5.1. EAP-FAST uses a different SHA1-based PRF, and TLS uses an
00040  * MD5-based PRF.
00041  */
00042 void prf_sha1 ( const void *key, size_t key_len, const char *label,
00043                 const void *data, size_t data_len, void *prf, size_t prf_len )
00044 {
00045         u32 blk;
00046         u8 keym[key_len];       /* modifiable copy of key */
00047         u8 in[strlen ( label ) + 1 + data_len + 1]; /* message to HMAC */
00048         u8 *in_blknr;           /* pointer to last byte of in, block number */
00049         u8 out[SHA1_SIZE];      /* HMAC-SHA1 result */
00050         u8 sha1_ctx[SHA1_CTX_SIZE]; /* SHA1 context */
00051         const size_t label_len = strlen ( label );
00052 
00053         /* The HMAC-SHA-1 is calculated using the given key on the
00054            message text `label', followed by a NUL, followed by one
00055            byte indicating the block number (0 for first). */
00056 
00057         memcpy ( keym, key, key_len );
00058 
00059         memcpy ( in, label, strlen ( label ) + 1 );
00060         memcpy ( in + label_len + 1, data, data_len );
00061         in_blknr = in + label_len + 1 + data_len;
00062 
00063         for ( blk = 0 ;; blk++ ) {
00064                 *in_blknr = blk;
00065 
00066                 hmac_init ( &sha1_algorithm, sha1_ctx, keym, &key_len );
00067                 hmac_update ( &sha1_algorithm, sha1_ctx, in, sizeof ( in ) );
00068                 hmac_final ( &sha1_algorithm, sha1_ctx, keym, &key_len, out );
00069 
00070                 if ( prf_len <= SHA1_SIZE ) {
00071                         memcpy ( prf, out, prf_len );
00072                         break;
00073                 }
00074 
00075                 memcpy ( prf, out, SHA1_SIZE );
00076                 prf_len -= SHA1_SIZE;
00077                 prf += SHA1_SIZE;
00078         }
00079 }
00080 
00081 /**
00082  * PBKDF2 key derivation function inner block operation
00083  *
00084  * @v passphrase        Passphrase from which to derive key
00085  * @v pass_len          Length of passphrase
00086  * @v salt              Salt to include in key
00087  * @v salt_len          Length of salt
00088  * @v iterations        Number of iterations of SHA1 to perform
00089  * @v blocknr           Index of this block, starting at 1
00090  * @ret block           SHA1_SIZE bytes of PBKDF2 data
00091  *
00092  * The operation of this function is described in RFC 2898.
00093  */
00094 static void pbkdf2_sha1_f ( const void *passphrase, size_t pass_len,
00095                             const void *salt, size_t salt_len,
00096                             int iterations, u32 blocknr, u8 *block )
00097 {
00098         u8 pass[pass_len];      /* modifiable passphrase */
00099         u8 in[salt_len + 4];    /* input buffer to first round */
00100         u8 last[SHA1_SIZE];     /* output of round N, input of N+1 */
00101         u8 sha1_ctx[SHA1_CTX_SIZE];
00102         u8 *next_in = in;       /* changed to `last' after first round */
00103         int next_size = sizeof ( in );
00104         int i, j;
00105 
00106         blocknr = htonl ( blocknr );
00107 
00108         memcpy ( pass, passphrase, pass_len );
00109         memcpy ( in, salt, salt_len );
00110         memcpy ( in + salt_len, &blocknr, 4 );
00111         memset ( block, 0, SHA1_SIZE );
00112 
00113         for ( i = 0; i < iterations; i++ ) {
00114                 hmac_init ( &sha1_algorithm, sha1_ctx, pass, &pass_len );
00115                 hmac_update ( &sha1_algorithm, sha1_ctx, next_in, next_size );
00116                 hmac_final ( &sha1_algorithm, sha1_ctx, pass, &pass_len, last );
00117 
00118                 for ( j = 0; j < SHA1_SIZE; j++ ) {
00119                         block[j] ^= last[j];
00120                 }
00121 
00122                 next_in = last;
00123                 next_size = SHA1_SIZE;
00124         }
00125 }
00126 
00127 /**
00128  * PBKDF2 key derivation function using SHA1
00129  *
00130  * @v passphrase        Passphrase from which to derive key
00131  * @v pass_len          Length of passphrase
00132  * @v salt              Salt to include in key
00133  * @v salt_len          Length of salt
00134  * @v iterations        Number of iterations of SHA1 to perform
00135  * @v key_len           Length of key to generate
00136  * @ret key             Generated key bytes
00137  *
00138  * This is used most notably in 802.11 WPA passphrase hashing, in
00139  * which case the salt is the SSID, 4096 iterations are used, and a
00140  * 32-byte key is generated that serves as the Pairwise Master Key for
00141  * EAPOL authentication.
00142  *
00143  * The operation of this function is further described in RFC 2898.
00144  */
00145 void pbkdf2_sha1 ( const void *passphrase, size_t pass_len,
00146                    const void *salt, size_t salt_len,
00147                    int iterations, void *key, size_t key_len )
00148 {
00149         u32 blocks = ( key_len + SHA1_SIZE - 1 ) / SHA1_SIZE;
00150         u32 blk;
00151         u8 buf[SHA1_SIZE];
00152 
00153         for ( blk = 1; blk <= blocks; blk++ ) {
00154                 pbkdf2_sha1_f ( passphrase, pass_len, salt, salt_len,
00155                                 iterations, blk, buf );
00156                 if ( key_len <= SHA1_SIZE ) {
00157                         memcpy ( key, buf, key_len );
00158                         break;
00159                 }
00160 
00161                 memcpy ( key, buf, SHA1_SIZE );
00162                 key_len -= SHA1_SIZE;
00163                 key += SHA1_SIZE;
00164         }
00165 }

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