monotone

monotone Mtn Source Tree

Root/botan/ctr.cpp

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

Archive Download this file

Branches

Tags

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