1 /**
2 * EMSA-Raw
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.emsa_raw;
12 
13 import botan.constants;
14 static if (BOTAN_HAS_EMSA_RAW):
15 import botan.pk_pad.emsa;
16 import botan.utils.types;
17 import botan.utils.mem_ops;
18 import std.algorithm : swap;
19 
20 /**
21 * EMSA-Raw - sign inputs directly
22 * Don't use this unless you know what you are doing.
23 */
24 final class EMSARaw : EMSA
25 {
26 public:
27     /*
28     * EMSA-Raw Encode Operation
29     */
30     override void update(const(ubyte)* input, size_t length)
31     {
32         m_message ~= input[0 .. length];
33     }
34 
35     /*
36     * Return the raw (unencoded) data
37     */
38     override SecureVector!ubyte rawData()
39     {
40         SecureVector!ubyte output;
41         swap(m_message, output);
42         return output.move;
43     }
44 
45     /*
46     * EMSA-Raw Encode Operation
47     */
48     SecureVector!ubyte encodingOf(const ref SecureVector!ubyte msg,
49                                   size_t,
50                                   RandomNumberGenerator)
51     {
52         return msg.dup;
53     }
54 
55     /*
56     * EMSA-Raw Verify Operation
57     */
58     bool verify(const ref SecureVector!ubyte coded,
59                 const ref SecureVector!ubyte raw,
60                 size_t)
61     {
62         if (coded.length == raw.length)
63             return (coded == raw);
64         
65         if (coded.length > raw.length)
66             return false;
67         
68         // handle zero padding differences
69         const size_t leading_zeros_expected = raw.length - coded.length;
70         
71         bool same_modulo_leading_zeros = true;
72         
73         foreach (size_t i; 0 .. leading_zeros_expected)
74             if (raw[i])
75                 same_modulo_leading_zeros = false;
76         
77         if (!sameMem(coded.ptr, &raw[leading_zeros_expected], coded.length))
78             same_modulo_leading_zeros = false;
79         
80         return same_modulo_leading_zeros;
81     }
82 
83     SecureVector!ubyte m_message;
84 }