8 #include <botan/emsa1.h>
14 SecureVector<byte> emsa1_encoding(
const MemoryRegion<byte>& msg,
17 if(8*msg.size() <= output_bits)
20 size_t shift = 8*msg.size() - output_bits;
22 size_t byte_shift = shift / 8, bit_shift = shift % 8;
23 SecureVector<byte> digest(msg.size() - byte_shift);
25 for(
size_t j = 0; j != msg.size() - byte_shift; ++j)
31 for(
size_t j = 0; j != digest.size(); ++j)
33 byte temp = digest[j];
34 digest[j] = (temp >> bit_shift) | carry;
35 carry = (temp << (8 - bit_shift));
46 void EMSA1::update(
const byte input[],
size_t length)
48 hash->
update(input, length);
54 SecureVector<byte> EMSA1::raw_data()
62 SecureVector<byte> EMSA1::encoding_of(
const MemoryRegion<byte>& msg,
64 RandomNumberGenerator&)
67 throw Encoding_Error(
"EMSA1::encoding_of: Invalid size for input");
68 return emsa1_encoding(msg, output_bits);
74 bool EMSA1::verify(
const MemoryRegion<byte>& coded,
75 const MemoryRegion<byte>& raw,
size_t key_bits)
79 throw Encoding_Error(
"EMSA1::encoding_of: Invalid size for input");
81 SecureVector<byte> our_coding = emsa1_encoding(raw, key_bits);
83 if(our_coding == coded)
return true;
84 if(our_coding[0] != 0)
return false;
85 if(our_coding.size() <= coded.size())
return false;
88 while(our_coding[offset] == 0 && offset < our_coding.size())
90 if(our_coding.size() - offset != coded.size())
93 for(
size_t j = 0; j != coded.size(); ++j)
94 if(coded[j] != our_coding[j+offset])
std::invalid_argument Invalid_Argument
void update(const byte in[], size_t length)
virtual size_t output_length() const =0