monotone

monotone Mtn Source Tree

Root/botan/cfb.cpp

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

Archive Download this file

Branches

Tags

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