monotone

monotone Mtn Source Tree

Root/botan/cfb.cpp

1/*************************************************
2* CFB Mode Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/cfb.h>
7#include <botan/lookup.h>
8#include <botan/parsing.h>
9#include <botan/bit_ops.h>
10#include <algorithm>
11
12namespace Botan {
13
14namespace {
15
16/*************************************************
17* Check the feedback size *
18*************************************************/
19void check_feedback(u32bit BLOCK_SIZE, u32bit FEEDBACK_SIZE, u32bit bits,
20 const std::string& name)
21 {
22 if(FEEDBACK_SIZE == 0 || FEEDBACK_SIZE > BLOCK_SIZE || bits % 8 != 0)
23 throw Invalid_Argument(name + ": Invalid feedback size " +
24 to_string(bits));
25 }
26
27}
28
29/*************************************************
30* CFB Encryption Constructor *
31*************************************************/
32CFB_Encryption::CFB_Encryption(const std::string& cipher_name,
33 u32bit fback_bits) :
34 BlockCipherMode(cipher_name, "CFB", block_size_of(cipher_name), 1),
35 FEEDBACK_SIZE(fback_bits ? fback_bits / 8: BLOCK_SIZE)
36 {
37 check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name());
38 }
39
40/*************************************************
41* CFB Encryption Constructor *
42*************************************************/
43CFB_Encryption::CFB_Encryption(const std::string& cipher_name,
44 const SymmetricKey& key,
45 const InitializationVector& iv,
46 u32bit fback_bits) :
47 BlockCipherMode(cipher_name, "CFB", block_size_of(cipher_name), 1),
48 FEEDBACK_SIZE(fback_bits ? fback_bits / 8: BLOCK_SIZE)
49 {
50 check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name());
51 set_key(key);
52 set_iv(iv);
53 }
54
55/*************************************************
56* Encrypt data in CFB mode *
57*************************************************/
58void CFB_Encryption::write(const byte input[], u32bit length)
59 {
60 while(length)
61 {
62 u32bit xored = std::min(FEEDBACK_SIZE - position, length);
63 xor_buf(buffer + position, input, xored);
64 send(buffer + position, xored);
65 input += xored;
66 length -= xored;
67 position += xored;
68 if(position == FEEDBACK_SIZE)
69 feedback();
70 }
71 }
72
73/*************************************************
74* Do the feedback *
75*************************************************/
76void CFB_Encryption::feedback()
77 {
78 for(u32bit j = 0; j != BLOCK_SIZE - FEEDBACK_SIZE; ++j)
79 state[j] = state[j + FEEDBACK_SIZE];
80 state.copy(BLOCK_SIZE - FEEDBACK_SIZE, buffer, FEEDBACK_SIZE);
81 cipher->encrypt(state, buffer);
82 position = 0;
83 }
84
85/*************************************************
86* CFB Decryption Constructor *
87*************************************************/
88CFB_Decryption::CFB_Decryption(const std::string& cipher_name,
89 u32bit fback_bits) :
90 BlockCipherMode(cipher_name, "CFB", block_size_of(cipher_name), 1),
91 FEEDBACK_SIZE(fback_bits ? fback_bits / 8 : BLOCK_SIZE)
92 {
93 check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name());
94 }
95
96/*************************************************
97* CFB Decryption Constructor *
98*************************************************/
99CFB_Decryption::CFB_Decryption(const std::string& cipher_name,
100 const SymmetricKey& key,
101 const InitializationVector& iv,
102 u32bit fback_bits) :
103 BlockCipherMode(cipher_name, "CFB", block_size_of(cipher_name), 1),
104 FEEDBACK_SIZE(fback_bits ? fback_bits / 8 : BLOCK_SIZE)
105 {
106 check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name());
107 set_key(key);
108 set_iv(iv);
109 }
110
111/*************************************************
112* Decrypt data in CFB mode *
113*************************************************/
114void CFB_Decryption::write(const byte input[], u32bit length)
115 {
116 while(length)
117 {
118 u32bit xored = std::min(FEEDBACK_SIZE - position, length);
119 xor_buf(buffer + position, input, xored);
120 send(buffer + position, xored);
121 buffer.copy(position, input, xored);
122 input += xored;
123 length -= xored;
124 position += xored;
125 if(position == FEEDBACK_SIZE)
126 feedback();
127 }
128 }
129
130/*************************************************
131* Do the feedback *
132*************************************************/
133void CFB_Decryption::feedback()
134 {
135 for(u32bit j = 0; j != BLOCK_SIZE - FEEDBACK_SIZE; ++j)
136 state[j] = state[j + FEEDBACK_SIZE];
137 state.copy(BLOCK_SIZE - FEEDBACK_SIZE, buffer, FEEDBACK_SIZE);
138 cipher->encrypt(state, buffer);
139 position = 0;
140 }
141
142}

Archive Download this file

Branches

Tags

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