Botan  1.10.9
eme_pkcs.cpp
Go to the documentation of this file.
1 /*
2 * PKCS1 EME
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/eme_pkcs.h>
9 
10 namespace Botan {
11 
12 /*
13 * PKCS1 Pad Operation
14 */
15 SecureVector<byte> EME_PKCS1v15::pad(const byte in[], size_t inlen,
16  size_t olen,
17  RandomNumberGenerator& rng) const
18  {
19  olen /= 8;
20 
21  if(olen < 10)
22  throw Encoding_Error("PKCS1: Output space too small");
23  if(inlen > olen - 10)
24  throw Encoding_Error("PKCS1: Input is too large");
25 
26  SecureVector<byte> out(olen);
27 
28  out[0] = 0x02;
29  for(size_t j = 1; j != olen - inlen - 1; ++j)
30  while(out[j] == 0)
31  out[j] = rng.next_byte();
32  out.copy(olen - inlen, in, inlen);
33 
34  return out;
35  }
36 
37 /*
38 * PKCS1 Unpad Operation
39 */
40 SecureVector<byte> EME_PKCS1v15::unpad(const byte in[], size_t inlen,
41  size_t key_len) const
42  {
43  if(inlen != key_len / 8 || inlen < 10 || in[0] != 0x02)
44  throw Decoding_Error("PKCS1::unpad");
45 
46  size_t seperator = 0;
47  for(size_t j = 0; j != inlen; ++j)
48  if(in[j] == 0)
49  {
50  seperator = j;
51  break;
52  }
53  if(seperator < 9)
54  throw Decoding_Error("PKCS1::unpad");
55 
56  return SecureVector<byte>(in + seperator + 1, inlen - seperator - 1);
57  }
58 
59 /*
60 * Return the max input size for a given key size
61 */
62 size_t EME_PKCS1v15::maximum_input_size(size_t keybits) const
63  {
64  if(keybits / 8 > 10)
65  return ((keybits / 8) - 10);
66  else
67  return 0;
68  }
69 
70 }
size_t maximum_input_size(size_t) const
Definition: eme_pkcs.cpp:62
unsigned char byte
Definition: types.h:22
RandomNumberGenerator * rng
Definition: global_rng.cpp:165