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 00020 #ifndef __ALGORITHMS_CRYPTO_ECDH_H_ 00021 #define __ALGORITHMS_CRYPTO_ECDH_H_ 00022 00023 #include "algorithms/crypto/eccfp.h" 00024 #include "algorithms/crypto/sha1.h" 00025 #include <string.h> 00026 00027 namespace wiselib 00028 { 00038 template<typename OsModel_P> 00039 class ECDH 00040 { 00041 public: 00042 typedef OsModel_P OsModel; 00043 00046 ECDH(); 00047 ~ECDH(); 00049 00052 void enable( void ); 00053 void disable( void ); 00055 00060 int8_t gen_shared_secret(uint8_t *sharedkey, uint8_t key_length, NN_DIGIT *PrivateKey, Point *PublicKey); 00061 00062 private: 00063 //objects for necessary classes 00064 ECCFP eccfp; 00065 PMP pmp; 00066 }; 00067 00068 // ----------------------------------------------------------------------- 00069 template<typename OsModel_P> 00070 ECDH<OsModel_P>:: 00071 ECDH() 00072 {} 00073 // ----------------------------------------------------------------------- 00074 template<typename OsModel_P> 00075 ECDH<OsModel_P>:: 00076 ~ECDH() 00077 {} 00078 // ----------------------------------------------------------------------- 00079 template<typename OsModel_P> 00080 void 00081 ECDH<OsModel_P>:: 00082 enable( void ) 00083 { 00084 } 00085 // ----------------------------------------------------------------------- 00086 template<typename OsModel_P> 00087 void 00088 ECDH<OsModel_P>:: 00089 disable( void ) 00090 { 00091 00092 } 00093 //---------------------------------------------------------------------------- 00094 template<typename OsModel_P> 00095 int8_t 00096 ECDH<OsModel_P>:: 00097 gen_shared_secret(uint8_t *sharedkey, uint8_t key_length, NN_DIGIT *PrivateKey, Point *PublicKey ) 00098 { 00099 //point that consists the shared point 00100 Point SharedSecret; 00101 eccfp.p_clear(&SharedSecret); 00102 uint8_t z[KEYDIGITS*NN_DIGIT_LEN]; 00103 00104 //Alice multiplies Bob's public key with her private key 00105 //to generate shared secret 00106 eccfp.c_mul(&SharedSecret, PublicKey, PrivateKey); 00107 00108 //if SharedSecret = O, return 0 --> invalid 00109 if (pmp.Zero(SharedSecret.x, NUMWORDS) && pmp.Zero(SharedSecret.y, NUMWORDS)) 00110 { 00111 return 0; 00112 } 00113 else 00114 { 00115 // convert x coordinate to octet string Z 00116 pmp.Encode(z, KEYDIGITS * NN_DIGIT_LEN, SharedSecret.x, NUMWORDS); 00117 00118 // use KDF to derive a shared key of length key_length 00119 SHA1::KDF(sharedkey, key_length, z); 00120 00121 //return success 00122 return 1; 00123 } 00124 } 00125 //------------------------------------------------------------------------------ 00126 00127 } //end of wiselib namespace 00128 00129 #endif