00001 /* dh.cpp 00002 * 00003 * Copyright (C) 2003 Sawtooth Consulting Ltd. 00004 * 00005 * This file is part of yaSSL. 00006 * 00007 * yaSSL is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * There are special exceptions to the terms and conditions of the GPL as it 00013 * is applied to yaSSL. View the full text of the exception in the file 00014 * FLOSS-EXCEPTIONS in the directory of this software distribution. 00015 * 00016 * yaSSL is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU General Public License 00022 * along with this program; if not, write to the Free Software 00023 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00024 */ 00025 00026 00027 /* dh.cpp implements Diffie-Hellman support 00028 */ 00029 00030 #include "runtime.hpp" 00031 #include "dh.hpp" 00032 #include "asn.hpp" 00033 #include <math.h> 00034 00035 namespace TaoCrypt { 00036 00037 00038 namespace { // locals 00039 00040 unsigned int DiscreteLogWorkFactor(unsigned int n) 00041 { 00042 // assuming discrete log takes about the same time as factoring 00043 if (n<5) 00044 return 0; 00045 else 00046 return (unsigned int)(2.4 * pow((double)n, 1.0/3.0) * 00047 pow(log(double(n)), 2.0/3.0) - 5); 00048 } 00049 00050 } // namespace locals 00051 00052 00053 // Generate a DH Key Pair 00054 void DH::GenerateKeyPair(RandomNumberGenerator& rng, byte* priv, byte* pub) 00055 { 00056 GeneratePrivate(rng, priv); 00057 GeneratePublic(priv, pub); 00058 } 00059 00060 00061 // Generate private value 00062 void DH::GeneratePrivate(RandomNumberGenerator& rng, byte* priv) 00063 { 00064 Integer x(rng, Integer::One(), mySTL::min(p_ - 1, 00065 Integer::Power2(2*DiscreteLogWorkFactor(p_.BitCount())) ) ); 00066 x.Encode(priv, p_.ByteCount()); 00067 } 00068 00069 00070 // Generate public value 00071 void DH::GeneratePublic(const byte* priv, byte* pub) 00072 { 00073 const word32 bc(p_.ByteCount()); 00074 Integer x(priv, bc); 00075 Integer y(a_exp_b_mod_c(g_, x, p_)); 00076 y.Encode(pub, bc); 00077 } 00078 00079 00080 // Generate Agreement 00081 void DH::Agree(byte* agree, const byte* priv, const byte* otherPub, word32 00082 otherSz) 00083 { 00084 const word32 bc(p_.ByteCount()); 00085 Integer x(priv, bc); 00086 Integer y; 00087 if (otherSz) 00088 y.Decode(otherPub, otherSz); 00089 else 00090 y.Decode(otherPub, bc); 00091 00092 Integer z(a_exp_b_mod_c(y, x, p_)); 00093 z.Encode(agree, bc); 00094 } 00095 00096 00097 DH::DH(Source& source) 00098 { 00099 Initialize(source); 00100 } 00101 00102 00103 void DH::Initialize(Source& source) 00104 { 00105 DH_Decoder decoder(source); 00106 decoder.Decode(*this); 00107 } 00108 00109 00110 } // namespace
1.4.7

