Botan  1.11.15
src/lib/pubkey/blinding.cpp
Go to the documentation of this file.
00001 /*
00002 * Blinding for public key operations
00003 * (C) 1999-2010 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/blinding.h>
00009 #include <botan/numthry.h>
00010 
00011 #if defined(BOTAN_HAS_SYSTEM_RNG)
00012   #include <botan/system_rng.h>
00013 #else
00014   #include <botan/auto_rng.h>
00015 #endif
00016 
00017 namespace Botan {
00018 
00019 // TODO: use Montgomery
00020 
00021 Blinder::Blinder(const BigInt& modulus,
00022                  std::function<BigInt (const BigInt&)> fwd_func,
00023                  std::function<BigInt (const BigInt&)> inv_func)
00024    {
00025    m_reducer = Modular_Reducer(modulus);
00026 
00027 #if defined(BOTAN_HAS_SYSTEM_RNG)
00028    auto& rng = system_rng();
00029 #else
00030    AutoSeeded_RNG rng;
00031 #endif
00032 
00033    const BigInt k(rng, modulus.bits() - 1);
00034 
00035    m_e = fwd_func(k);
00036    m_d = inv_func(k);
00037    }
00038 
00039 BigInt Blinder::blind(const BigInt& i) const
00040    {
00041    if(!m_reducer.initialized())
00042       throw std::runtime_error("Blinder not initialized, cannot blind");
00043 
00044    m_e = m_reducer.square(m_e);
00045    m_d = m_reducer.square(m_d);
00046    return m_reducer.multiply(i, m_e);
00047    }
00048 
00049 BigInt Blinder::unblind(const BigInt& i) const
00050    {
00051    if(!m_reducer.initialized())
00052       throw std::runtime_error("Blinder not initialized, cannot unblind");
00053 
00054    return m_reducer.multiply(i, m_d);
00055    }
00056 
00057 }