Botan  1.10.9
mgf1.cpp
Go to the documentation of this file.
1 /*
2 * MGF1
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/mgf1.h>
9 #include <botan/exceptn.h>
10 #include <botan/internal/xor_buf.h>
11 #include <algorithm>
12 #include <memory>
13 
14 namespace Botan {
15 
16 /*
17 * MGF1 Mask Generation Function
18 */
19 void MGF1::mask(const byte in[], size_t in_len, byte out[],
20  size_t out_len) const
21  {
22  u32bit counter = 0;
23 
24  while(out_len)
25  {
26  hash->update(in, in_len);
27  hash->update_be(counter);
28  SecureVector<byte> buffer = hash->final();
29 
30  size_t xored = std::min<size_t>(buffer.size(), out_len);
31  xor_buf(out, &buffer[0], xored);
32  out += xored;
33  out_len -= xored;
34 
35  ++counter;
36  }
37  }
38 
39 /*
40 * MGF1 Constructor
41 */
42 MGF1::MGF1(HashFunction* h) : hash(h)
43  {
44  if(!hash)
45  throw Invalid_Argument("MGF1 given null hash object");
46  }
47 
48 /*
49 * MGF1 Destructor
50 */
52  {
53  delete hash;
54  }
55 
56 }
MGF1(HashFunction *hash)
Definition: mgf1.cpp:42
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
void mask(const byte[], size_t, byte[], size_t) const
Definition: mgf1.cpp:19
unsigned char byte
Definition: types.h:22
void update(const byte in[], size_t length)
Definition: buf_comp.h:33
size_t size() const
Definition: secmem.h:29
void final(byte out[])
Definition: buf_comp.h:80
void update_be(const T in)
Definition: buf_comp.h:48
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
unsigned int u32bit
Definition: types.h:32