Wiselib
|
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 DIFFIE_HELLMAN_CRYPTO_HANDLER_H 00022 #define DIFFIE_HELLMAN_CRYPTO_HANDLER_H 00023 00024 #include <string.h> 00025 #include "algorithm/aes.h" 00026 #include "algorithm/diffie_hellman_config.h" 00027 00028 namespace wiselib 00029 { 00030 00031 template<typename OsModel_P, typename CryptoRoutine_P> 00032 class DiffieHellmanCryptoHandler 00033 { 00034 public: 00035 typedef OsModel_P OsModel; 00036 typedef CryptoRoutine_P CryptoRoutine; 00037 00038 inline DiffieHellmanCryptoHandler(); 00039 inline void encrypt( uint8_t*, uint8_t*, uint16_t ); 00040 inline void decrypt( uint8_t*, uint8_t*, uint16_t ); 00041 inline void key_setup( uint8_t* ); 00042 00043 private: 00044 CryptoRoutine crypto_; 00045 uint8_t aes_block_[16]; 00046 AES<OsModel> aes_; 00047 }; 00048 // ----------------------------------------------------------------------- 00049 // ----------------------------------------------------------------------- 00050 // ----------------------------------------------------------------------- 00051 template<typename OsModel_P, typename CryptoRoutine_P> 00052 DiffieHellmanCryptoHandler<OsModel_P, CryptoRoutine_P>:: 00053 DiffieHellmanCryptoHandler() 00054 {} 00055 // ----------------------------------------------------------------------- 00056 template<typename OsModel_P, typename CryptoRoutine_P> 00057 void 00058 DiffieHellmanCryptoHandler<OsModel_P, CryptoRoutine_P>:: 00059 encrypt( uint8_t *data_in, uint8_t *data_out, uint16_t data_size_in ) 00060 { 00061 // AES processes 16 Byte Blocks! 00062 uint16_t blocks = data_size_in / 16; 00063 uint8_t bytes_left = data_size_in % 16; 00064 00065 uint16_t i; 00066 for( i = 0; i < blocks; i++ ) 00067 { 00068 memcpy( aes_block_, data_in + 16 * i, 16 ); 00069 crypto_.encrypt( aes_block_, aes_block_ ); 00070 memcpy( data_out + 16 * i, aes_block_, 16 ); 00071 } 00072 00073 if( bytes_left > 0 ) 00074 { 00075 memset( aes_block_, 0x00, 16 ); 00076 memcpy( aes_block_, data_in + 16 * blocks, bytes_left ); 00077 crypto_.encrypt( aes_block_, aes_block_ ); 00078 memcpy( data_out + 16 * blocks, aes_block_, 16 ); 00079 } 00080 } 00081 // ----------------------------------------------------------------------- 00082 template<typename OsModel_P, typename CryptoRoutine_P> 00083 void 00084 DiffieHellmanCryptoHandler<OsModel_P, CryptoRoutine_P>:: 00085 decrypt( uint8_t *data_in, uint8_t *data_out, uint16_t data_size_in ) 00086 { 00087 uint16_t blocks = data_size_in / 16; 00088 uint16_t i; 00089 00090 for( i = 0; i < blocks; i++ ) 00091 { 00092 memcpy( aes_block_, data_in + 16 * i, 16 ); 00093 crypto_.decrypt( aes_block_, aes_block_ ); 00094 memcpy( data_out + 16 * i, aes_block_, 16 ); 00095 } 00096 } 00097 // ----------------------------------------------------------------------- 00098 template<typename OsModel_P, typename CryptoRoutine_P> 00099 void 00100 DiffieHellmanCryptoHandler<OsModel_P, CryptoRoutine_P>:: 00101 key_setup( uint8_t *key ) 00102 { 00103 crypto_.key_setup( key, 128 ); 00104 } 00105 } 00106 #endif