aes_wrap.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 <stdlib.h>
00022 #include <string.h>
00023 #include <gpxe/crypto.h>
00024 #include <gpxe/aes.h>
00025 
00026 /**
00027  * Wrap a key or other data using AES Key Wrap (RFC 3394)
00028  *
00029  * @v kek       Key Encryption Key, 16 bytes
00030  * @v src       Data to encrypt
00031  * @v nblk      Number of 8-byte blocks in @a data
00032  * @ret dest    Encrypted data (8 bytes longer than input)
00033  *
00034  * The algorithm is implemented such that @a src and @a dest may point
00035  * to the same buffer.
00036  */
00037 int aes_wrap ( const void *kek, const void *src, void *dest, int nblk )
00038 {
00039         u8 *A = dest;
00040         u8 B[16];
00041         u8 *R;
00042         int i, j;
00043         void *aes_ctx = malloc ( AES_CTX_SIZE );
00044 
00045         if ( ! aes_ctx )
00046                 return -1;
00047 
00048         cipher_setkey ( &aes_algorithm, aes_ctx, kek, 16 );
00049 
00050         /* Set up */
00051         memset ( A, 0xA6, sizeof ( A ) );
00052         memmove ( dest + 8, src, nblk * 8 );
00053 
00054         /* Wrap */
00055         for ( j = 0; j < 6; j++ ) {
00056                 R = dest + 8;
00057                 for ( i = 1; i <= nblk; i++ ) {
00058                         memcpy ( B, A, 8 );
00059                         memcpy ( B + 8, R, 8 );
00060                         cipher_encrypt ( &aes_algorithm, aes_ctx, B, B, 16 );
00061                         memcpy ( A, B, 8 );
00062                         A[7] ^= ( nblk * j ) + i;
00063                         memcpy ( R, B + 8, 8 );
00064                         R += 8;
00065                 }
00066         }
00067 
00068         free ( aes_ctx );
00069         return 0;
00070 }
00071 
00072 /**
00073  * Unwrap a key or other data using AES Key Wrap (RFC 3394)
00074  *
00075  * @v kek       Key Encryption Key, 16 bytes
00076  * @v src       Data to decrypt
00077  * @v nblk      Number of 8-byte blocks in @e plaintext key
00078  * @ret dest    Decrypted data (8 bytes shorter than input)
00079  * @ret rc      Zero on success, nonzero on IV mismatch
00080  *
00081  * The algorithm is implemented such that @a src and @a dest may point
00082  * to the same buffer.
00083  */
00084 int aes_unwrap ( const void *kek, const void *src, void *dest, int nblk )
00085 {
00086         u8 A[8], B[16];
00087         u8 *R;
00088         int i, j;
00089         void *aes_ctx = malloc ( AES_CTX_SIZE );
00090 
00091         if ( ! aes_ctx )
00092                 return -1;
00093 
00094         cipher_setkey ( &aes_algorithm, aes_ctx, kek, 16 );
00095 
00096         /* Set up */
00097         memcpy ( A, src, 8 );
00098         memmove ( dest, src + 8, nblk * 8 );
00099 
00100         /* Unwrap */
00101         for ( j = 5; j >= 0; j-- ) {
00102                 R = dest + ( nblk - 1 ) * 8;
00103                 for ( i = nblk; i >= 1; i-- ) {
00104                         memcpy ( B, A, 8 );
00105                         memcpy ( B + 8, R, 8 );
00106                         B[7] ^= ( nblk * j ) + i;
00107                         cipher_decrypt ( &aes_algorithm, aes_ctx, B, B, 16 );
00108                         memcpy ( A, B, 8 );
00109                         memcpy ( R, B + 8, 8 );
00110                         R -= 8;
00111                 }
00112         }
00113 
00114         free ( aes_ctx );
00115 
00116         /* Check IV */
00117         for ( i = 0; i < 8; i++ ) {
00118                 if ( A[i] != 0xA6 )
00119                         return -1;
00120         }
00121 
00122         return 0;
00123 }

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