monotone

monotone Mtn Source Tree

Root/botan/ctr.cpp

1/*************************************************
2* CTR Mode Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/ctr.h>
7#include <botan/lookup.h>
8#include <botan/bit_ops.h>
9#include <algorithm>
10
11namespace Botan {
12
13/*************************************************
14* CTR-BE Constructor *
15*************************************************/
16CTR_BE::CTR_BE(const std::string& cipher_name) :
17 BlockCipherMode(cipher_name, "CTR-BE", block_size_of(cipher_name), 1)
18 {
19 }
20
21/*************************************************
22* CTR-BE Constructor *
23*************************************************/
24CTR_BE::CTR_BE(const std::string& cipher_name, const SymmetricKey& key,
25 const InitializationVector& iv) :
26 BlockCipherMode(cipher_name, "CTR-BE", block_size_of(cipher_name), 1)
27 {
28 set_key(key);
29 set_iv(iv);
30 }
31
32/*************************************************
33* CTR-BE Encryption/Decryption *
34*************************************************/
35void CTR_BE::write(const byte input[], u32bit length)
36 {
37 u32bit copied = std::min(BLOCK_SIZE - position, length);
38 xor_buf(buffer + position, input, copied);
39 send(buffer + position, copied);
40 input += copied;
41 length -= copied;
42 position += copied;
43
44 if(position == BLOCK_SIZE)
45 increment_counter();
46
47 while(length >= BLOCK_SIZE)
48 {
49 xor_buf(buffer, input, BLOCK_SIZE);
50 send(buffer, BLOCK_SIZE);
51
52 input += BLOCK_SIZE;
53 length -= BLOCK_SIZE;
54 increment_counter();
55 }
56
57 xor_buf(buffer + position, input, length);
58 send(buffer + position, length);
59 position += length;
60 }
61
62/*************************************************
63* Increment the counter and update the buffer *
64*************************************************/
65void CTR_BE::increment_counter()
66 {
67 for(s32bit j = BLOCK_SIZE - 1; j >= 0; --j)
68 if(++state[j])
69 break;
70 cipher->encrypt(state, buffer);
71 position = 0;
72 }
73
74}

Archive Download this file

Branches

Tags

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