1 /** 2 * Blinding for public key operations 3 * 4 * Copyright: 5 * (C) 1999-2010 Jack Lloyd 6 * (C) 2014-2015 Etienne Cimon 7 * 8 * License: 9 * Botan is released under the Simplified BSD License (see LICENSE.md) 10 */ 11 module botan.pubkey.blinding; 12 13 import botan.constants; 14 static if (BOTAN_HAS_PUBLIC_KEY_CRYPTO): 15 16 import botan.math.bigint.bigint; 17 import botan.math.numbertheory.reducer; 18 import botan.math.numbertheory.numthry; 19 20 /** 21 * Blinding Function Object 22 */ 23 struct Blinder 24 { 25 public: 26 /* 27 * Blind a number 28 */ 29 BigInt blind()(auto const ref BigInt i) 30 { 31 if (!m_reducer.initialized()) 32 return i.dup; 33 34 m_e = m_reducer.square(&m_e); 35 m_d = m_reducer.square(&m_d); 36 return m_reducer.multiply(&i, &m_e); 37 } 38 39 /* 40 * Unblind a number 41 */ 42 BigInt unblind()(auto const ref BigInt i) const 43 { 44 if (!m_reducer.initialized()) 45 return i.dup; 46 return m_reducer.multiply(&i, &m_d); 47 } 48 49 bool initialized() const { return m_reducer.initialized(); } 50 51 /** 52 * Construct a blinder 53 * Params: 54 * e = the forward (blinding) mask 55 * d = the inverse of mask (depends on algo) 56 * n = modulus of the group operations are performed in 57 */ 58 this()(auto const ref BigInt e, 59 auto const ref BigInt d, 60 auto const ref BigInt n) 61 { 62 if (e < 1 || d < 1 || n < 1) 63 throw new InvalidArgument("Blinder: Arguments too small"); 64 65 m_reducer = ModularReducer(n); 66 m_e = e.dup; 67 m_d = d.dup; 68 } 69 70 private: 71 ModularReducer m_reducer; 72 BigInt m_e, m_d; 73 }