Crypto++  5.6.3
Free C++ class library of cryptographic schemes
blumshub.cpp
1 // blumshub.cpp - 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 
10  : modn(n),
11  current(modn.Square(modn.Square(seed))),
12  maxBits(BitPrecision(n.BitCount())-1),
13  bitsLeft(maxBits)
14 {
15 }
16 
18 {
19  if (bitsLeft==0)
20  {
21  current = modn.Square(current);
22  bitsLeft = maxBits;
23  }
24 
25  return current.GetBit(--bitsLeft);
26 }
27 
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--)
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
bool GetBit(size_t i) const
Provides the i-th bit of the Integer.
Definition: integer.cpp:3043
void ProcessData(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt an array of bytes.
Definition: blumshub.cpp:42
void Seek(lword index)
Seek to an absolute position.
Definition: blumshub.cpp:55
Square block cipher.
Definition: square.h:24
const Integer & Square(const Integer &a) const
Square an element in the ring.
Definition: modarith.h:179
unsigned int GenerateBit()
Generate new random bit and return it.
Definition: blumshub.cpp:17
byte GenerateByte()
Generate new random byte and return it.
Definition: blumshub.cpp:28
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: blumshub.cpp:36
Multiple precision integer with arithmetic operations.
Definition: integer.h:31
Classes for Blum Blum Shub generator.
Crypto++ library namespace.
BlumBlumShub without factorization of the modulus.
Definition: blumshub.h:17
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
Definition: misc.h:645
the value is positive or 0
Definition: integer.h:57