aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/rng
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2015-08-21 19:21:16 -0400
committerJack Lloyd <[email protected]>2015-08-21 19:21:16 -0400
commitca155a7e54ec39e60f9dd6c53567ebf283b3e8d0 (patch)
tree97a257b7c4cce8a0f46433ae88ea5485892635ac /src/lib/rng
parentbae7c12ecf78457c146467ecfbc6a5577cf6f529 (diff)
Add power analysis countermeasures for ECC point multiplications.
The plain PointGFp operator* now uses Montgomery ladder exclusively. Adds a blinded point multiply algorithm which uses exponent and point randomization, as well as a Montgomery ladder technique that takes a random walk of the possible addition chains for k.
Diffstat (limited to 'src/lib/rng')
-rw-r--r--src/lib/rng/rng.h30
1 files changed, 24 insertions, 6 deletions
diff --git a/src/lib/rng/rng.h b/src/lib/rng/rng.h
index b1f78f75d..6ee67f66f 100644
--- a/src/lib/rng/rng.h
+++ b/src/lib/rng/rng.h
@@ -47,17 +47,35 @@ class BOTAN_DLL RandomNumberGenerator
}
/**
- * Return a random byte
- * @return random byte
+ * Only usable with POD types, only useful with integers
+ * get_random<u64bit>()
*/
- byte next_byte()
+ template<typename T> T get_random()
{
- byte out;
- this->randomize(&out, 1);
- return out;
+ T r;
+ this->randomize(reinterpret_cast<byte*>(&r), sizeof(r));
+ return r;
}
/**
+ * Return a value in range [0,2^bits)
+ */
+ u64bit gen_mask(size_t bits)
+ {
+ if(bits == 0 || bits > 64)
+ throw std::invalid_argument("RandomNumberGenerator::gen_mask invalid argument");
+
+ const u64bit mask = ((1 << bits) - 1);
+ return this->get_random<u64bit>() & mask;
+ }
+
+ /**
+ * Return a random byte
+ * @return random byte
+ */
+ byte next_byte() { return get_random<byte>(); }
+
+ /**
* Check whether this RNG is seeded.
* @return true if this RNG was already seeded, false otherwise.
*/