1 /**
2 * EME Classes
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;
12 
13 import memutils.vector;
14 public import botan.rng.rng;
15 /**
16 * Encoding Method for Encryption
17 */
18 class EME
19 {
20 public:
21     /**
22     * Return the maximum input size in bytes we can support
23     * Params:
24     *  keybits = the size of the key in bits
25     * Returns: upper bound of input in bytes
26     */
27     abstract size_t maximumInputSize(size_t keybits) const;
28 
29     /**
30     * Encode an input
31     * Params:
32     *  msg = the plaintext
33     *  msg_len = length of plaintext in bytes
34     *  key_bits = length of the key in bits
35     *  rng = a random number generator
36     * Returns: encoded plaintext
37     */
38     final SecureVector!ubyte encode(const(ubyte)* msg, size_t msg_len,
39                                     size_t key_bits,
40                                     RandomNumberGenerator rng) const
41     {
42         return pad(msg, msg_len, key_bits, rng);
43     }
44 
45     /**
46     * Encode an input
47     * Params:
48     *  msg = the plaintext
49     *  key_bits = length of the key in bits
50     *  rng = a random number generator
51     * Returns: encoded plaintext
52     */
53     final SecureVector!ubyte encode(const ref SecureVector!ubyte msg, size_t key_bits, RandomNumberGenerator rng) const
54     {
55         return pad(msg.ptr, msg.length, key_bits, rng);
56     }
57 
58     /**
59     * Decode an input
60     * Params:
61     *  msg = the encoded plaintext
62     *  msg_len = length of encoded plaintext in bytes
63     *  key_bits = length of the key in bits
64     * Returns: plaintext
65     */
66     final SecureVector!ubyte decode(const(ubyte)* msg, size_t msg_len, size_t key_bits) const
67     {
68         return unpad(msg, msg_len, key_bits);
69     }
70 
71 
72     /**
73     * Decode an input
74     * Params:
75     *  msg = the encoded plaintext
76     *  key_bits = length of the key in bits
77     * Returns: plaintext
78     */
79     final SecureVector!ubyte decode(const ref SecureVector!ubyte msg, size_t key_bits) const
80     {
81         return unpad(msg.ptr, msg.length, key_bits);
82     }
83 
84     ~this() {}
85 protected:
86     /**
87     * Encode an input
88     * Params:
89     *  input = the plaintext
90     *  in_length = length of plaintext in bytes
91     *  key_length = length of the key in bits
92     *  rng = a random number generator
93     * Returns: encoded plaintext
94     */
95     abstract SecureVector!ubyte pad(const(ubyte)* input,
96                                      size_t in_length,
97                                      size_t key_length,
98                                      RandomNumberGenerator rng) const;
99 
100     /**
101     * Decode an input
102     * Params:
103     *  input = the encoded plaintext
104     *  in_length = length of encoded plaintext in bytes
105     *  key_length = length of the key in bits
106     * Returns: plaintext
107     */
108     abstract SecureVector!ubyte unpad(const(ubyte)* input, size_t in_length, size_t key_length) const;
109 }