aboutsummaryrefslogtreecommitdiffstats
path: root/src/emsa2.cpp
blob: 052d9226a0cac5765288ce37c02039cd22174893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*************************************************
* EMSA2 Source File                              *
* (C) 1999-2007 Jack Lloyd                       *
*************************************************/

#include <botan/emsa.h>
#include <botan/hash_id.h>
#include <botan/lookup.h>

namespace Botan {

/*************************************************
* EMSA2 Update Operation                         *
*************************************************/
void EMSA2::update(const byte input[], u32bit length)
   {
   hash->update(input, length);
   }

/*************************************************
* Return the raw (unencoded) data                *
*************************************************/
SecureVector<byte> EMSA2::raw_data()
   {
   return hash->final();
   }

/*************************************************
* EMSA2 Encode Operation                         *
*************************************************/
SecureVector<byte> EMSA2::encoding_of(const MemoryRegion<byte>& msg,
                                      u32bit output_bits)
   {
   u32bit output_length = (output_bits + 1) / 8;

   if(msg.size() != hash->OUTPUT_LENGTH)
      throw Encoding_Error("EMSA2::encoding_of: Bad input length");
   if(output_length < hash->OUTPUT_LENGTH + 4)
      throw Encoding_Error("EMSA2::encoding_of: Output length is too small");

   bool empty = true;
   for(u32bit j = 0; j != hash->OUTPUT_LENGTH; ++j)
      if(empty_hash[j] != msg[j])
         empty = false;

   SecureVector<byte> output(output_length);

   output[0] = (empty ? 0x4B : 0x6B);
   output[output_length - 3 - hash->OUTPUT_LENGTH] = 0xBA;
   set_mem(output + 1, output_length - 4 - hash->OUTPUT_LENGTH, 0xBB);
   output.copy(output_length-2-hash->OUTPUT_LENGTH, msg, msg.size());
   output[output_length-2] = hash_id;
   output[output_length-1] = 0xCC;

   return output;
   }

/*************************************************
* EMSA2 Constructor                              *
*************************************************/
EMSA2::EMSA2(const std::string& hash_name)
   {
   hash_id = ieee1363_hash_id(hash_name);
   if(hash_id == 0)
      throw Encoding_Error("EMSA2 cannot be used with " + hash->name());
   hash = get_hash(hash_name);
   empty_hash = hash->final();
   }

}