Wiselib
wiselib.testing/algorithms/crypto/eschenauer_gligor/eschenauer_gligor_crypto_handler.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  **                                                                       **
00019  ** Author: Christoph Knecht, University of Bern 2010                     **
00020  ***************************************************************************/
00021 #ifndef ESCHENAUER_GLIGOR_CRYPTO_HANDLER_H
00022 #define ESCHENAUER_GLIGOR_CRYPTO_HANDLER_H
00023 
00024 #include <string.h>
00025 #include "algorithm/aes.h"
00026 
00027 namespace wiselib
00028 {
00029 
00030    template<typename OsModel_P, typename CryptoRoutine_P>
00031    class EschenauerGligorCryptoHandler
00032    {
00033    public:
00034       typedef OsModel_P OsModel;
00035       typedef CryptoRoutine_P CryptoRoutine;
00036 
00037       inline EschenauerGligorCryptoHandler();
00038       inline void encrypt( uint8_t*, uint8_t*, uint16_t );
00039       inline void decrypt( uint8_t*, uint8_t*, uint16_t );
00040       inline void key_setup( uint8_t* );
00041 
00042    private:
00043       CryptoRoutine crypto_;
00044       uint8_t aes_block_[16];
00045       AES<OsModel> aes_;
00046    };
00047    // -----------------------------------------------------------------------
00048    // -----------------------------------------------------------------------
00049    // -----------------------------------------------------------------------
00050    template<typename OsModel_P, typename CryptoRoutine_P>
00051    EschenauerGligorCryptoHandler<OsModel_P, CryptoRoutine_P>::
00052    EschenauerGligorCryptoHandler()
00053    {}
00054    // -----------------------------------------------------------------------
00055    template<typename OsModel_P, typename CryptoRoutine_P>
00056    void
00057    EschenauerGligorCryptoHandler<OsModel_P, CryptoRoutine_P>::
00058    encrypt( uint8_t *data_in, uint8_t *data_out, uint16_t data_size_in )
00059    {
00060       // AES processes 16 Byte Blocks!
00061       uint16_t blocks = data_size_in / 16;
00062       uint8_t bytes_left = data_size_in % 16;
00063 
00064       uint16_t i;
00065       for( i = 0; i < blocks; i++ )
00066       {
00067          memcpy( aes_block_, data_in + 16 * i, 16 );
00068          crypto_.encrypt( aes_block_, aes_block_ );
00069          memcpy( data_out + 16 * i, aes_block_, 16 );
00070       }
00071 
00072       if( bytes_left > 0 )
00073       {
00074          memset( aes_block_, 0x00, 16 );
00075          memcpy( aes_block_, data_in + 16 * blocks, bytes_left );
00076          crypto_.encrypt( aes_block_, aes_block_ );
00077          memcpy( data_out + 16 * blocks, aes_block_, 16 );
00078       }
00079    }
00080    // -----------------------------------------------------------------------
00081    template<typename OsModel_P, typename CryptoRoutine_P>
00082    void
00083    EschenauerGligorCryptoHandler<OsModel_P, CryptoRoutine_P>::
00084    decrypt( uint8_t *data_in, uint8_t *data_out, uint16_t data_size_in )
00085    {
00086       uint16_t blocks = data_size_in / 16;
00087       uint16_t i;
00088 
00089       for( i = 0; i < blocks; i++ )
00090       {
00091          memcpy( aes_block_, data_in + 16 * i, 16 );
00092          crypto_.decrypt( aes_block_, aes_block_ );
00093          memcpy( data_out + 16 * i, aes_block_, 16 );
00094       }
00095    }
00096    // -----------------------------------------------------------------------
00097    template<typename OsModel_P, typename CryptoRoutine_P>
00098    void
00099    EschenauerGligorCryptoHandler<OsModel_P, CryptoRoutine_P>::
00100    key_setup( uint8_t *key )
00101    {
00102       crypto_.key_setup( key, 128 );
00103    }
00104 }
00105 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines