~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/eme_pkcs.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

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