ro.cpp
Go to the documentation of this file.
1 
11 #include <cstdint>
12 #include <iostream>
13 
14 #include "ro.hpp"
15 
16 
17 using namespace cryptoTools;
18 
19 
20 RO::RO(SHAx * hash, uint32_t outlen)
21 {
22  hashfunction = hash;
23  prg = new PRG(hash);
24  nout = outlen;
25  vectNout.push_back( (outlen>>24) % 0x100);
26  vectNout.push_back( (outlen>>16) % 0x100);
27  vectNout.push_back( (outlen>> 8) % 0x100);
28  vectNout.push_back( outlen % 0x100);
29 }
30 
31 
32 std::vector<uint8_t> RO::query(std::vector<uint8_t> d)
33 {
34  // computing the length of the output and the number of bits
35  // to set to zero.
36  unsigned int len = (nout%8 == 0) ? nout/8 : (nout/8) +1,
37  padding = (nout%8 == 0) ? 0 :
38  (nout%8 > 0) ? 8-(nout%8) : 8+(nout%8);
39  // computing the seed
40  std::vector<uint8_t> in(vectNout);
41  in.insert(in.end(), d.begin(), d.end());
42  hashfunction->hash(in);
43  std::vector<uint8_t> digest(hashfunction->getHash());
44  // calling the PRG
45  prg->initialize(digest);
46  std::vector<uint8_t> out;
47  for (unsigned int i=0; i<len; i++)
48  out.push_back(prg->getNextRandByte());
49  // setting the padding first bits to zero.
50  for (unsigned int i=8; i>=8-padding; i--)
51  out[0] &= ~(1<<i);
52  return out;
53 
54 }