cbc.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 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 #include <string.h>
00022 #include <assert.h>
00023 #include <gpxe/crypto.h>
00024 #include <gpxe/cbc.h>
00025 
00026 /** @file
00027  *
00028  * Cipher-block chaining
00029  *
00030  */
00031 
00032 /**
00033  * XOR data blocks
00034  *
00035  * @v src               Input data
00036  * @v dst               Second input data and output data buffer
00037  * @v len               Length of data
00038  */
00039 static void cbc_xor ( const void *src, void *dst, size_t len ) {
00040         const uint32_t *srcl = src;
00041         uint32_t *dstl = dst;
00042         unsigned int i;
00043 
00044         /* Assume that block sizes will always be dword-aligned, for speed */
00045         assert ( ( len % sizeof ( *srcl ) ) == 0 );
00046 
00047         for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
00048                 dstl[i] ^= srcl[i];
00049 }
00050 
00051 /**
00052  * Encrypt data
00053  *
00054  * @v ctx               Context
00055  * @v src               Data to encrypt
00056  * @v dst               Buffer for encrypted data
00057  * @v len               Length of data
00058  * @v raw_cipher        Underlying cipher algorithm
00059  * @v cbc_ctx           CBC context
00060  */
00061 void cbc_encrypt ( void *ctx, const void *src, void *dst, size_t len,
00062                    struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
00063         size_t blocksize = raw_cipher->blocksize;
00064 
00065         assert ( ( len % blocksize ) == 0 );
00066 
00067         while ( len ) {
00068                 cbc_xor ( src, cbc_ctx, blocksize );
00069                 cipher_encrypt ( raw_cipher, ctx, cbc_ctx, dst, blocksize );
00070                 memcpy ( cbc_ctx, dst, blocksize );
00071                 dst += blocksize;
00072                 src += blocksize;
00073                 len -= blocksize;
00074         }
00075 }
00076 
00077 /**
00078  * Decrypt data
00079  *
00080  * @v ctx               Context
00081  * @v src               Data to decrypt
00082  * @v dst               Buffer for decrypted data
00083  * @v len               Length of data
00084  * @v raw_cipher        Underlying cipher algorithm
00085  * @v cbc_ctx           CBC context
00086  */
00087 void cbc_decrypt ( void *ctx, const void *src, void *dst, size_t len,
00088                    struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
00089         size_t blocksize = raw_cipher->blocksize;
00090 
00091         assert ( ( len % blocksize ) == 0 );
00092 
00093         while ( len ) {
00094                 cipher_decrypt ( raw_cipher, ctx, src, dst, blocksize );
00095                 cbc_xor ( cbc_ctx, dst, blocksize );
00096                 memcpy ( cbc_ctx, src, blocksize );
00097                 dst += blocksize;
00098                 src += blocksize;
00099                 len -= blocksize;
00100         }
00101 }

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