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
11namespace Botan {
12
13namespace {
14
15/*
16* EMSA3 Encode Operation
17*/
18SecureVector<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*/
43void EMSA3::update(const byte input[], size_t length)
44 {
45 hash->update(input, length);
46 }
47
48/*
49* Return the raw (unencoded) data
50*/
52 {
53 return hash->final();
54 }
55
56/*
57* EMSA3 Encode Operation
58*/
60 size_t output_bits,
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*/
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*/
94EMSA3::EMSA3(HashFunction* hash_in) : hash(hash_in)
95 {
96 hash_id = pkcs_hash_id(hash->name());
97 }
98
99/*
100* EMSA3 Destructor
101*/
103 {
104 delete hash;
105 }
106
107/*
108* EMSA3_Raw Update Operation
109*/
110void 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*/
119 {
121 std::swap(ret, message);
122 return ret;
123 }
124
125/*
126* EMSA3_Raw Encode Operation
127*/
129 size_t output_bits,
131 {
132 return emsa3_encoding(msg, output_bits, 0, 0);
133 }
134
135/*
136* Default signature decoding
137*/
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}
SecureVector< byte > raw_data()
Definition emsa3.cpp:118
bool verify(const MemoryRegion< byte > &, const MemoryRegion< byte > &, size_t)
Definition emsa3.cpp:138
void update(const byte[], size_t)
Definition emsa3.cpp:110
SecureVector< byte > encoding_of(const MemoryRegion< byte > &, size_t, RandomNumberGenerator &rng)
Definition emsa3.cpp:128
EMSA3(HashFunction *hash)
Definition emsa3.cpp:94
SecureVector< byte > raw_data()
Definition emsa3.cpp:51
bool verify(const MemoryRegion< byte > &, const MemoryRegion< byte > &, size_t)
Definition emsa3.cpp:73
void update(const byte[], size_t)
Definition emsa3.cpp:43
SecureVector< byte > encoding_of(const MemoryRegion< byte > &, size_t, RandomNumberGenerator &rng)
Definition emsa3.cpp:59
size_t size() const
Definition secmem.h:29
void set_mem(T *ptr, size_t n, byte val)
Definition mem_ops.h:45
MemoryVector< byte > pkcs_hash_id(const std::string &name)
Definition hash_id.cpp:60
void swap(Botan::MemoryRegion< T > &x, Botan::MemoryRegion< T > &y)
Definition secmem.h:438
Encoding_Error(const std::string &name)
Definition exceptn.h:131