Botan  1.11.15
src/lib/pubkey/ecdh/ecdh.cpp
Go to the documentation of this file.
00001 /*
00002 * ECDH implemenation
00003 * (C) 2007 Manuel Hartl, FlexSecure GmbH
00004 *     2007 Falko Strenzke, FlexSecure GmbH
00005 *     2008-2010 Jack Lloyd
00006 *
00007 * Botan is released under the Simplified BSD License (see license.txt)
00008 */
00009 
00010 #include <botan/internal/pk_utils.h>
00011 #include <botan/ecdh.h>
00012 
00013 namespace Botan {
00014 
00015 namespace {
00016 
00017 /**
00018 * ECDH operation
00019 */
00020 class ECDH_KA_Operation : public PK_Ops::Key_Agreement
00021    {
00022    public:
00023       typedef ECDH_PrivateKey Key_Type;
00024 
00025       ECDH_KA_Operation(const ECDH_PrivateKey& key, const std::string&) :
00026          curve(key.domain().get_curve()),
00027          cofactor(key.domain().get_cofactor())
00028          {
00029          l_times_priv = inverse_mod(cofactor, key.domain().get_order()) * key.private_value();
00030          }
00031 
00032       secure_vector<byte> agree(const byte w[], size_t w_len);
00033    private:
00034       const CurveGFp& curve;
00035       const BigInt& cofactor;
00036       BigInt l_times_priv;
00037    };
00038 
00039 secure_vector<byte> ECDH_KA_Operation::agree(const byte w[], size_t w_len)
00040    {
00041    PointGFp point = OS2ECP(w, w_len, curve);
00042 
00043    PointGFp S = (cofactor * point) * l_times_priv;
00044 
00045    BOTAN_ASSERT(S.on_the_curve(),
00046                 "ECDH agreed value was on the curve");
00047 
00048    return BigInt::encode_1363(S.get_affine_x(),
00049                               curve.get_p().bytes());
00050    }
00051 
00052 }
00053 
00054 BOTAN_REGISTER_PK_KEY_AGREE_OP("ECDH", ECDH_KA_Operation);
00055 
00056 }