Botan: src/pk_pad/emsa3/emsa3.cpp Source File

Botan 1.10.17

emsa3.cpp
Go to the documentation of this file.
1 /*
2 * EMSA3 and EMSA3_Raw
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/emsa3.h>
9 #include <botan/hash_id.h>
10
11 namespace Botan {
12
13 namespace {
14
15 /*
16 * EMSA3 Encode Operation
17 */
18 SecureVector<byte> emsa3_encoding(const MemoryRegion<byte>& msg,
19 size_t output_bits,
20 const byte hash_id[],
21 size_t hash_id_length)
22 {
23 size_t output_length = output_bits / 8;
24 if(output_length < hash_id_length + msg.size() + 10)
25 throw Encoding_Error("emsa3_encoding: Output length is too small");
26
27 SecureVector<byte> T(output_length);
28 const size_t P_LENGTH = output_length - msg.size() - hash_id_length - 2;
29
30 T[0] = 0x01;
31 set_mem(&T[1], P_LENGTH, 0xFF);
32 T[P_LENGTH+1] = 0x00;
33 T.copy(P_LENGTH+2, hash_id, hash_id_length);
34 T.copy(output_length-msg.size(), &msg[0], msg.size());
35 return T;
36 }
37
38 }
39
40 /*
41 * EMSA3 Update Operation
42 */
43 void EMSA3::update(const byte input[], size_t length)
44 {
45 hash->update(input, length);
46 }
47
48 /*
49 * Return the raw (unencoded) data
50 */
51 SecureVector<byte> EMSA3::raw_data()
52 {
53 return hash->final();
54 }
55
56 /*
57 * EMSA3 Encode Operation
58 */
59 SecureVector<byte> EMSA3::encoding_of(const MemoryRegion<byte>& msg,
60 size_t output_bits,
61 RandomNumberGenerator&)
62 {
63 if(msg.size() != hash->output_length())
64 throw Encoding_Error("EMSA3::encoding_of: Bad input length");
65
66 return emsa3_encoding(msg, output_bits,
67 &hash_id[0], hash_id.size());
68 }
69
70 /*
71 * Default signature decoding
72 */
73 bool EMSA3::verify(const MemoryRegion<byte>& coded,
74 const MemoryRegion<byte>& raw,
75 size_t key_bits)
76 {
77 if(raw.size() != hash->output_length())
78 return false;
79
80 try
81 {
82 return (coded == emsa3_encoding(raw, key_bits,
83 &hash_id[0], hash_id.size()));
84 }
85 catch(...)
86 {
87 return false;
88 }
89 }
90
91 /*
92 * EMSA3 Constructor
93 */
94 EMSA3::EMSA3(HashFunction* hash_in) : hash(hash_in)
95 {
96 hash_id = pkcs_hash_id(hash->name());
97 }
98
99 /*
100 * EMSA3 Destructor
101 */
102 EMSA3::~EMSA3()
103 {
104 delete hash;
105 }
106
107 /*
108 * EMSA3_Raw Update Operation
109 */
110 void EMSA3_Raw::update(const byte input[], size_t length)
111 {
112 message += std::make_pair(input, length);
113 }
114
115 /*
116 * Return the raw (unencoded) data
117 */
118 SecureVector<byte> EMSA3_Raw::raw_data()
119 {
120 SecureVector<byte> ret;
121 std::swap(ret, message);
122 return ret;
123 }
124
125 /*
126 * EMSA3_Raw Encode Operation
127 */
128 SecureVector<byte> EMSA3_Raw::encoding_of(const MemoryRegion<byte>& msg,
129 size_t output_bits,
130 RandomNumberGenerator&)
131 {
132 return emsa3_encoding(msg, output_bits, 0, 0);
133 }
134
135 /*
136 * Default signature decoding
137 */
138 bool EMSA3_Raw::verify(const MemoryRegion<byte>& coded,
139 const MemoryRegion<byte>& raw,
140 size_t key_bits)
141 {
142 try
143 {
144 return (coded == emsa3_encoding(raw, key_bits, 0, 0));
145 }
146 catch(...)
147 {
148 return false;
149 }
150 }
151
152 }
Botan::RandomNumberGenerator
Definition: rng.h:20
Botan::EMSA3_Raw::update
void update(const byte[], size_t)
Definition: emsa3.cpp:110
Botan::pkcs_hash_id
MemoryVector< byte > pkcs_hash_id(const std::string &name)
Definition: hash_id.cpp:60
Botan::EMSA3_Raw::verify
bool verify(const MemoryRegion< byte > &, const MemoryRegion< byte > &, size_t)
Definition: emsa3.cpp:138
Botan::EMSA3::verify
bool verify(const MemoryRegion< byte > &, const MemoryRegion< byte > &, size_t)
Definition: emsa3.cpp:73
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::EMSA3::~EMSA3
~EMSA3()
Definition: emsa3.cpp:102
Botan::EMSA3::encoding_of
SecureVector< byte > encoding_of(const MemoryRegion< byte > &, size_t, RandomNumberGenerator &rng)
Definition: emsa3.cpp:59
Botan::Buffered_Computation::update
void update(const byte in[], size_t length)
Definition: buf_comp.h:33
Botan::EMSA3_Raw::raw_data
SecureVector< byte > raw_data()
Definition: emsa3.cpp:118
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::Algorithm::name
virtual std::string name() const =0
Botan::Buffered_Computation::final
void final(byte out[])
Definition: buf_comp.h:80
Botan::set_mem
void set_mem(T *ptr, size_t n, byte val)
Definition: mem_ops.h:45
Botan::EMSA3::raw_data
SecureVector< byte > raw_data()
Definition: emsa3.cpp:51
Botan::HashFunction
Definition: hash.h:20
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::SecureVector< byte >
std::swap
void swap(Botan::MemoryRegion< T > &x, Botan::MemoryRegion< T > &y)
Definition: secmem.h:438
Botan::EMSA3_Raw::encoding_of
SecureVector< byte > encoding_of(const MemoryRegion< byte > &, size_t, RandomNumberGenerator &rng)
Definition: emsa3.cpp:128
Botan::Encoding_Error
Definition: exceptn.h:129
Botan::Buffered_Computation::output_length
virtual size_t output_length() const =0
Botan::EMSA3::EMSA3
EMSA3(HashFunction *hash)
Definition: emsa3.cpp:94
Botan::EMSA3::update
void update(const byte[], size_t)
Definition: emsa3.cpp:43