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