Crypto++ 8.1 Free C++ class library of cryptographic schemes

blumshub.cpp
1 // blumshub.cpp - originally written and placed in the public domain by Wei Dai
2
3 #include "pch.h"
4 #include "blumshub.h"
5 #include "integer.h"
6
7 NAMESPACE_BEGIN(CryptoPP)
8
9 PublicBlumBlumShub::PublicBlumBlumShub(const Integer &n, const Integer &seed)
10 : modn(n),
11 current(modn.Square(modn.Square(seed))),
12 maxBits(BitPrecision(n.BitCount())-1),
13 bitsLeft(maxBits)
14 {
15 }
16
17 unsigned int PublicBlumBlumShub::GenerateBit()
18 {
19 if (bitsLeft==0)
20 {
21 current = modn.Square(current);
22 bitsLeft = maxBits;
23 }
24
25 return static_cast<unsigned int>(current.GetBit(--bitsLeft));
26 }
27
28 byte PublicBlumBlumShub::GenerateByte()
29 {
30 byte b=0;
31 for (int i=0; i<8; i++)
32 b = byte((b << 1) | PublicBlumBlumShub::GenerateBit());
33 return b;
34 }
35
36 void PublicBlumBlumShub::GenerateBlock(byte *output, size_t size)
37 {
38 while (size--)
39 *output++ = PublicBlumBlumShub::GenerateByte();
40 }
41
42 void PublicBlumBlumShub::ProcessData(byte *outString, const byte *inString, size_t length)
43 {
44 while (length--)
45 *outString++ = *inString++ ^ PublicBlumBlumShub::GenerateByte();
46 }
47
48 BlumBlumShub::BlumBlumShub(const Integer &p, const Integer &q, const Integer &seed)
49 : PublicBlumBlumShub(p*q, seed),
50 p(p), q(q),
51 x0(modn.Square(seed))
52 {
53 }
54
55 void BlumBlumShub::Seek(lword index)
56 {
57 Integer i(Integer::POSITIVE, index);
58 i *= 8;
59 Integer e = a_exp_b_mod_c (2, i / maxBits + 1, (p-1)*(q-1));
60 current = modn.Exponentiate(x0, e);
61 bitsLeft = maxBits - i % maxBits;
62 }
63
64 NAMESPACE_END
ModularArithmetic::Square
const Integer & Square(const Integer &a) const
Square an element in the ring.
Definition: modarith.h:181
PublicBlumBlumShub::ProcessData
void ProcessData(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt an array of bytes.
Definition: blumshub.cpp:42
BlumBlumShub::Seek
void Seek(lword index)
Seek to an absolute position.
Definition: blumshub.cpp:55
Square
Square block cipher.
Definition: square.h:24
PublicBlumBlumShub::GenerateBit
unsigned int GenerateBit()
Generate new random bit and return it.
Definition: blumshub.cpp:17
PublicBlumBlumShub::GenerateByte
byte GenerateByte()
Generate new random byte and return it.
Definition: blumshub.cpp:28
PublicBlumBlumShub::GenerateBlock
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: blumshub.cpp:36
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:49
pch.h
Precompiled header file.
blumshub.h
Classes for Blum Blum Shub generator.
integer.h
Multiple precision integer with arithmetic operations.
CryptoPP
Crypto++ library namespace.
Integer::GetBit
bool GetBit(size_t i) const
Provides the i-th bit of the Integer.
Definition: integer.cpp:3103
PublicBlumBlumShub
BlumBlumShub without factorization of the modulus.
Definition: blumshub.h:16
BitPrecision
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
Definition: misc.h:722
Integer::POSITIVE
the value is positive or 0
Definition: integer.h:75