monotone

monotone Mtn Source Tree

Root/botan/emsa1.cpp

1/*************************************************
2* EMSA1 Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/emsa.h>
7#include <botan/lookup.h>
8
9namespace Botan {
10
11/*************************************************
12* EMSA1 Update Operation *
13*************************************************/
14void EMSA1::update(const byte input[], u32bit length)
15 {
16 hash->update(input, length);
17 }
18
19/*************************************************
20* Return the raw (unencoded) data *
21*************************************************/
22SecureVector<byte> EMSA1::raw_data()
23 {
24 return hash->final();
25 }
26
27/*************************************************
28* EMSA1 Encode Operation *
29*************************************************/
30SecureVector<byte> EMSA1::encoding_of(const MemoryRegion<byte>& msg,
31 u32bit output_bits)
32 {
33 if(msg.size() != hash->OUTPUT_LENGTH)
34 throw Invalid_Argument("EMSA1::encoding_of: Invalid size for input");
35 if(msg.bits() <= output_bits)
36 return msg;
37
38 u32bit shift = msg.bits() - output_bits;
39
40 u32bit byte_shift = shift / 8, bit_shift = shift % 8;
41 SecureVector<byte> digest(msg.size() - byte_shift);
42
43 for(u32bit j = 0; j != msg.size() - byte_shift; j++)
44 digest[j] = msg[j];
45
46 if(bit_shift)
47 {
48 byte carry = 0;
49 for(u32bit j = 0; j != digest.size(); j++)
50 {
51 byte temp = digest[j];
52 digest[j] = (temp >> bit_shift) | carry;
53 carry = (temp << (8 - bit_shift));
54 }
55 }
56 return digest;
57 }
58
59/*************************************************
60* EMSA1 Decode/Verify Operation *
61*************************************************/
62bool EMSA1::verify(const MemoryRegion<byte>& coded,
63 const MemoryRegion<byte>& raw, u32bit key_bits) throw()
64 {
65 try {
66 SecureVector<byte> our_coding = encoding_of(raw, key_bits);
67
68 if(our_coding == coded) return true;
69 if(our_coding[0] != 0) return false;
70 if(our_coding.size() <= coded.size()) return false;
71
72 u32bit offset = 0;
73 while(our_coding[offset] == 0 && offset < our_coding.size())
74 offset++;
75 if(our_coding.size() - offset != coded.size())
76 return false;
77
78 for(u32bit j = 0; j != coded.size(); j++)
79 if(coded[j] != our_coding[j+offset])
80 return false;
81
82 return true;
83 }
84 catch(Invalid_Argument)
85 {
86 return false;
87 }
88 }
89
90/*************************************************
91* EMSA1 Constructor *
92*************************************************/
93EMSA1::EMSA1(const std::string& hash_name) :
94 hash(get_hash(hash_name))
95 {
96 }
97
98}

Archive Download this file

Branches

Tags

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