1 /**
2 * EME PKCS#1 v1.5
3 * 
4 * Copyright:
5 * (C) 1999-2007 Jack Lloyd
6 * (C) 2014-2015 Etienne Cimon
7 *
8 * License:
9 * Botan is released under the Simplified BSD License (see LICENSE.md)
10 */
11 module botan.pk_pad.eme_pkcs;
12 
13 import botan.constants;
14 static if (BOTAN_HAS_EME_PKCS1_V15):
15 import botan.pk_pad.eme;
16 import botan.utils.types;
17 import botan.utils.mem_ops;
18 
19 /**
20 * EME from PKCS #1 v1.5
21 */
22 final class EMEPKCS1v15 : EME
23 {
24 public:
25     /*
26     * Return the max input size for a given key size
27     */
28     override size_t maximumInputSize(size_t keybits) const
29     {
30         if (keybits / 8 > 10)
31             return ((keybits / 8) - 10);
32         else
33             return 0;
34     }
35 
36     /*
37     * PKCS1 Pad Operation
38     */
39     override SecureVector!ubyte pad(const(ubyte)* input, size_t inlen, size_t olen, RandomNumberGenerator rng) const
40     {
41         olen /= 8;
42         
43         if (olen < 10)
44             throw new EncodingError("PKCS1: Output space too small");
45         if (inlen > olen - 10)
46             throw new EncodingError("PKCS1: Input is too large");
47         
48         SecureVector!ubyte output = SecureVector!ubyte(olen);
49         
50         output[0] = 0x02;
51         foreach (size_t j; 1 .. (olen - inlen - 1))
52             while (output[j] == 0)
53                 output[j] = rng.nextByte();
54         bufferInsert(output, olen - inlen, input, inlen);
55         
56         return output;
57     }
58 
59     /*
60     * PKCS1 Unpad Operation
61     */
62     override SecureVector!ubyte unpad(const(ubyte)* input, size_t inlen, size_t key_len) const
63     {
64         if (inlen != key_len / 8 || inlen < 10 || input[0] != 0x02)
65             throw new DecodingError("PKCS1::unpad");
66         
67         size_t seperator = 0;
68         foreach (size_t j; 0 .. inlen)
69             if (input[j] == 0)
70         {
71             seperator = j;
72             break;
73         }
74         if (seperator < 9)
75             throw new DecodingError("PKCS1::unpad");
76         
77         return SecureVector!ubyte(input[seperator + 1 .. inlen]);
78     }
79 
80 }