monotone

monotone Mtn Source Tree

Root/botan/eme1.cpp

1/*************************************************
2* EME1 Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/eme.h>
7#include <botan/rng.h>
8#include <botan/lookup.h>
9#include <botan/look_pk.h>
10#include <memory>
11
12namespace Botan {
13
14/*************************************************
15* EME1 Pad Operation *
16*************************************************/
17SecureVector<byte> EME1::pad(const byte in[], u32bit in_length,
18 u32bit key_length) const
19 {
20 key_length /= 8;
21
22 if(in_length > key_length - 2*HASH_LENGTH - 1)
23 throw Exception("EME1: Input is too large");
24
25 SecureVector<byte> out(key_length);
26
27 out.clear();
28
29 Global_RNG::randomize(out, HASH_LENGTH);
30
31 out.copy(HASH_LENGTH, Phash, Phash.size());
32 out[out.size() - in_length - 1] = 0x01;
33 out.copy(out.size() - in_length, in, in_length);
34 mgf->mask(out, HASH_LENGTH, out + HASH_LENGTH, out.size() - HASH_LENGTH);
35 mgf->mask(out + HASH_LENGTH, out.size() - HASH_LENGTH, out, HASH_LENGTH);
36
37 return out;
38 }
39
40/*************************************************
41* EME1 Unpad Operation *
42*************************************************/
43SecureVector<byte> EME1::unpad(const byte in[], u32bit in_length,
44 u32bit key_length) const
45 {
46 key_length /= 8;
47 if(in_length > key_length)
48 throw Decoding_Error("Invalid EME1 encoding");
49
50 SecureVector<byte> tmp(key_length);
51 tmp.copy(key_length - in_length, in, in_length);
52
53 mgf->mask(tmp + HASH_LENGTH, tmp.size() - HASH_LENGTH, tmp, HASH_LENGTH);
54 mgf->mask(tmp, HASH_LENGTH, tmp + HASH_LENGTH, tmp.size() - HASH_LENGTH);
55
56 for(u32bit j = 0; j != Phash.size(); ++j)
57 if(tmp[j+HASH_LENGTH] != Phash[j])
58 throw Decoding_Error("Invalid EME1 encoding");
59
60 for(u32bit j = HASH_LENGTH + Phash.size(); j != tmp.size(); ++j)
61 {
62 if(tmp[j] && tmp[j] != 0x01)
63 throw Decoding_Error("Invalid EME1 encoding");
64 if(tmp[j] && tmp[j] == 0x01)
65 {
66 SecureVector<byte> retval(tmp + j + 1, tmp.size() - j - 1);
67 return retval;
68 }
69 }
70 throw Decoding_Error("Invalid EME1 encoding");
71 }
72
73/*************************************************
74* Return the max input size for a given key size *
75*************************************************/
76u32bit EME1::maximum_input_size(u32bit keybits) const
77 {
78 if(keybits / 8 > 2*HASH_LENGTH + 1)
79 return ((keybits / 8) - 2*HASH_LENGTH - 1);
80 else
81 return 0;
82 }
83
84/*************************************************
85* EME1 Constructor *
86*************************************************/
87EME1::EME1(const std::string& hash_name, const std::string& mgf_name,
88 const std::string& P) :
89 HASH_LENGTH(output_length_of(hash_name))
90 {
91 mgf = get_mgf(mgf_name + "(" + hash_name + ")");
92 std::auto_ptr<HashFunction> hash(get_hash(hash_name));
93 Phash = hash->process(P);
94 }
95
96}

Archive Download this file

Branches

Tags

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