monotone

monotone Mtn Source Tree

Root/botan/emsa2.cpp

1/*************************************************
2* EMSA2 Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/emsa.h>
7#include <botan/hash_id.h>
8#include <botan/lookup.h>
9
10namespace Botan {
11
12/*************************************************
13* EMSA2 Update Operation *
14*************************************************/
15void EMSA2::update(const byte input[], u32bit length)
16 {
17 hash->update(input, length);
18 }
19
20/*************************************************
21* Return the raw (unencoded) data *
22*************************************************/
23SecureVector<byte> EMSA2::raw_data()
24 {
25 return hash->final();
26 }
27
28/*************************************************
29* EMSA2 Encode Operation *
30*************************************************/
31SecureVector<byte> EMSA2::encoding_of(const MemoryRegion<byte>& msg,
32 u32bit output_bits)
33 {
34 u32bit output_length = (output_bits + 1) / 8;
35
36 if(msg.size() != hash->OUTPUT_LENGTH)
37 throw Encoding_Error("EMSA2::encoding_of: Bad input length");
38 if(output_length < hash->OUTPUT_LENGTH + 4)
39 throw Encoding_Error("EMSA2::encoding_of: Output length is too small");
40
41 bool empty = true;
42 for(u32bit j = 0; j != hash->OUTPUT_LENGTH; ++j)
43 if(empty_hash[j] != msg[j])
44 empty = false;
45
46 SecureVector<byte> output(output_length);
47
48 output[0] = (empty ? 0x4B : 0x6B);
49 output[output_length - 3 - hash->OUTPUT_LENGTH] = 0xBA;
50 set_mem(output + 1, output_length - 4 - hash->OUTPUT_LENGTH, 0xBB);
51 output.copy(output_length-2-hash->OUTPUT_LENGTH, msg, msg.size());
52 output[output_length-2] = hash_id;
53 output[output_length-1] = 0xCC;
54
55 return output;
56 }
57
58/*************************************************
59* EMSA2 Constructor *
60*************************************************/
61EMSA2::EMSA2(const std::string& hash_name)
62 {
63 hash_id = ieee1363_hash_id(hash_name);
64 if(hash_id == 0)
65 throw Encoding_Error("EMSA2 cannot be used with " + hash->name());
66 hash = get_hash(hash_name);
67 empty_hash = hash->final();
68 }
69
70}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status