iPXE
ecb.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 <assert.h>
00027 #include <ipxe/crypto.h>
00028 #include <ipxe/ecb.h>
00029 
00030 /** @file
00031  *
00032  * Electronic codebook (ECB)
00033  *
00034  */
00035 
00036 /**
00037  * Encrypt data
00038  *
00039  * @v ctx               Context
00040  * @v src               Data to encrypt
00041  * @v dst               Buffer for encrypted data
00042  * @v len               Length of data
00043  * @v raw_cipher        Underlying cipher algorithm
00044  */
00045 void ecb_encrypt ( void *ctx, const void *src, void *dst, size_t len,
00046                    struct cipher_algorithm *raw_cipher ) {
00047         size_t blocksize = raw_cipher->blocksize;
00048 
00049         assert ( ( len % blocksize ) == 0 );
00050 
00051         while ( len ) {
00052                 cipher_encrypt ( raw_cipher, ctx, src, dst, blocksize );
00053                 dst += blocksize;
00054                 src += blocksize;
00055                 len -= blocksize;
00056         }
00057 }
00058 
00059 /**
00060  * Decrypt data
00061  *
00062  * @v ctx               Context
00063  * @v src               Data to decrypt
00064  * @v dst               Buffer for decrypted data
00065  * @v len               Length of data
00066  * @v raw_cipher        Underlying cipher algorithm
00067  */
00068 void ecb_decrypt ( void *ctx, const void *src, void *dst, size_t len,
00069                    struct cipher_algorithm *raw_cipher ) {
00070         size_t blocksize = raw_cipher->blocksize;
00071 
00072         assert ( ( len % blocksize ) == 0 );
00073 
00074         while ( len ) {
00075                 cipher_decrypt ( raw_cipher, ctx, src, dst, blocksize );
00076                 dst += blocksize;
00077                 src += blocksize;
00078                 len -= blocksize;
00079         }
00080 }