monotone

monotone Mtn Source Tree

Root/botan/arc4.cpp

1/*************************************************
2* ARC4 Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/arc4.h>
7#include <botan/bit_ops.h>
8#include <botan/parsing.h>
9
10namespace Botan {
11
12/*************************************************
13* Combine cipher stream with message *
14*************************************************/
15void ARC4::cipher(const byte in[], byte out[], u32bit length)
16 {
17 while(length >= buffer.size() - position)
18 {
19 xor_buf(out, in, buffer.begin() + position, buffer.size() - position);
20 length -= (buffer.size() - position);
21 in += (buffer.size() - position);
22 out += (buffer.size() - position);
23 generate();
24 }
25 xor_buf(out, in, buffer.begin() + position, length);
26 position += length;
27 }
28
29/*************************************************
30* Generate cipher stream *
31*************************************************/
32void ARC4::generate()
33 {
34 u32bit SX, SY;
35 for(u32bit j = 0; j != buffer.size(); j += 4)
36 {
37 SX = state[X+1]; Y = (Y + SX) % 256; SY = state[Y];
38 state[X+1] = SY; state[Y] = SX;
39 buffer[j] = state[(SX + SY) % 256];
40
41 SX = state[X+2]; Y = (Y + SX) % 256; SY = state[Y];
42 state[X+2] = SY; state[Y] = SX;
43 buffer[j+1] = state[(SX + SY) % 256];
44
45 SX = state[X+3]; Y = (Y + SX) % 256; SY = state[Y];
46 state[X+3] = SY; state[Y] = SX;
47 buffer[j+2] = state[(SX + SY) % 256];
48
49 X = (X + 4) % 256;
50 SX = state[X]; Y = (Y + SX) % 256; SY = state[Y];
51 state[X] = SY; state[Y] = SX;
52 buffer[j+3] = state[(SX + SY) % 256];
53 }
54 position = 0;
55 }
56
57/*************************************************
58* ARC4 Key Schedule *
59*************************************************/
60void ARC4::key(const byte key[], u32bit length)
61 {
62 clear();
63 for(u32bit j = 0; j != 256; j++)
64 state[j] = j;
65 for(u32bit j = 0, state_index = 0; j != 256; j++)
66 {
67 state_index = (state_index + key[j % length] + state[j]) % 256;
68 std::swap(state[j], state[state_index]);
69 }
70 for(u32bit j = 0; j <= SKIP; j += buffer.size())
71 generate();
72 position += (SKIP % buffer.size());
73 }
74
75/*************************************************
76* Return the name of this type *
77*************************************************/
78std::string ARC4::name() const
79 {
80 if(SKIP == 0) return "ARC4";
81 if(SKIP == 256) return "MARK-4";
82 else return "RC4_skip(" + to_string(SKIP) + ")";
83 }
84
85/*************************************************
86* Clear memory of sensitive data *
87*************************************************/
88void ARC4::clear() throw()
89 {
90 state.clear();
91 buffer.clear();
92 position = X = Y = 0;
93 }
94
95/*************************************************
96* ARC4 Constructor *
97*************************************************/
98// monotone-specific: max keysize is 256 bytes
99ARC4::ARC4(u32bit s) : StreamCipher(1, 256), SKIP(s)
100 {
101 clear();
102 }
103
104}

Archive Download this file

Branches

Tags

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