monotone

monotone Mtn Source Tree

Root/botan/filters.cpp

1/*************************************************
2* Filters Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/filters.h>
7#include <botan/lookup.h>
8#include <algorithm>
9
10namespace Botan {
11
12/*************************************************
13* StreamCipher_Filter Constructor *
14*************************************************/
15StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name) :
16 buffer(DEFAULT_BUFFERSIZE)
17 {
18 base_ptr = cipher = get_stream_cipher(sc_name);
19 }
20
21/*************************************************
22* StreamCipher_Filter Constructor *
23*************************************************/
24StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name,
25 const SymmetricKey& key) :
26 buffer(DEFAULT_BUFFERSIZE)
27 {
28 base_ptr = cipher = get_stream_cipher(sc_name);
29 cipher->set_key(key);
30 }
31
32/*************************************************
33* Set the IV of a stream cipher *
34*************************************************/
35void StreamCipher_Filter::set_iv(const InitializationVector& iv)
36 {
37 cipher->resync(iv.begin(), iv.length());
38 }
39
40/*************************************************
41* Write data into a StreamCipher_Filter *
42*************************************************/
43void StreamCipher_Filter::write(const byte input[], u32bit length)
44 {
45 while(length)
46 {
47 u32bit copied = std::min(length, buffer.size());
48 cipher->encrypt(input, buffer, copied);
49 send(buffer, copied);
50 input += copied;
51 length -= copied;
52 }
53 }
54
55/*************************************************
56* Hash_Filter Constructor *
57*************************************************/
58Hash_Filter::Hash_Filter(const std::string& hash_name, u32bit len) :
59 OUTPUT_LENGTH(len)
60 {
61 hash = get_hash(hash_name);
62 }
63
64/*************************************************
65* Complete a calculation by a Hash_Filter *
66*************************************************/
67void Hash_Filter::end_msg()
68 {
69 SecureVector<byte> output = hash->final();
70 if(OUTPUT_LENGTH)
71 send(output, std::min(OUTPUT_LENGTH, output.size()));
72 else
73 send(output);
74 }
75
76/*************************************************
77* MAC_Filter Constructor *
78*************************************************/
79MAC_Filter::MAC_Filter(const std::string& mac_name, u32bit len) :
80 OUTPUT_LENGTH(len)
81 {
82 base_ptr = mac = get_mac(mac_name);
83 }
84
85/*************************************************
86* MAC_Filter Constructor *
87*************************************************/
88MAC_Filter::MAC_Filter(const std::string& mac_name, const SymmetricKey& key,
89 u32bit len) : OUTPUT_LENGTH(len)
90 {
91 base_ptr = mac = get_mac(mac_name);
92 mac->set_key(key);
93 }
94
95/*************************************************
96* Complete a calculation by a MAC_Filter *
97*************************************************/
98void MAC_Filter::end_msg()
99 {
100 SecureVector<byte> output = mac->final();
101 if(OUTPUT_LENGTH)
102 send(output, std::min(OUTPUT_LENGTH, output.size()));
103 else
104 send(output);
105 }
106
107}

Archive Download this file

Branches

Tags

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