aboutsummaryrefslogtreecommitdiffstats
path: root/src/eme_pkcs.cpp
blob: a9bf3062521f54ccd182e4c7121afc55b521573d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*************************************************
* PKCS1 EME Source File                          *
* (C) 1999-2007 Jack Lloyd                       *
*************************************************/

#include <botan/eme.h>
#include <botan/rng.h>

namespace Botan {

/*************************************************
* PKCS1 Pad Operation                            *
*************************************************/
SecureVector<byte> EME_PKCS1v15::pad(const byte in[], u32bit inlen,
                                     u32bit olen) const
   {
   olen /= 8;

   if(olen < 10)
      throw Encoding_Error("PKCS1: Output space too small");
   if(inlen > olen - 10)
      throw Encoding_Error("PKCS1: Input is too large");

   SecureVector<byte> out(olen);

   out[0] = 0x02;
   for(u32bit j = 1; j != olen - inlen - 1; ++j)
      while(out[j] == 0)
         out[j] = Global_RNG::random();
   out.copy(olen - inlen, in, inlen);

   return out;
   }

/*************************************************
* PKCS1 Unpad Operation                          *
*************************************************/
SecureVector<byte> EME_PKCS1v15::unpad(const byte in[], u32bit inlen,
                                       u32bit key_len) const
   {
   if(inlen != key_len / 8 || inlen < 10 || in[0] != 0x02)
      throw Decoding_Error("PKCS1::unpad");

   u32bit seperator = 0;
   for(u32bit j = 0; j != inlen; ++j)
      if(in[j] == 0)
         {
         seperator = j;
         break;
         }
   if(seperator < 9)
      throw Decoding_Error("PKCS1::unpad");

   return SecureVector<byte>(in + seperator + 1, inlen - seperator - 1);
   }

/*************************************************
* Return the max input size for a given key size *
*************************************************/
u32bit EME_PKCS1v15::maximum_input_size(u32bit keybits) const
   {
   if(keybits / 8 > 10)
      return ((keybits / 8) - 10);
   else
      return 0;
   }

}