Security Scol plugin
rc2.cpp
1// rc2.cpp - originally written and placed in the public domain by Wei Dai
2
3#include "pch.h"
4#include "rc2.h"
5#include "misc.h"
6#include "argnames.h"
7
8NAMESPACE_BEGIN(CryptoPP)
9
10void RC2::Base::UncheckedSetKey(const byte *key, unsigned int keyLen, const NameValuePairs &params)
11{
12 AssertValidKeyLength(keyLen);
13
14 int effectiveLen = params.GetIntValueWithDefault(Name::EffectiveKeyLength(), DEFAULT_EFFECTIVE_KEYLENGTH);
15 if (effectiveLen > MAX_EFFECTIVE_KEYLENGTH)
16 throw InvalidArgument("RC2: effective key length parameter exceeds maximum");
17
18 static const unsigned char PITABLE[256] = {
19 217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
20 198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,
21 23,154, 89,245,135,179, 79, 19, 97, 69,109,141, 9,129,125, 50,
22 189,143, 64,235,134,183,123, 11,240,149, 33, 34, 92,107, 78,130,
23 84,214,101,147,206, 96,178, 28,115, 86,192, 20,167,140,241,220,
24 18,117,202, 31, 59,190,228,209, 66, 61,212, 48,163, 60,182, 38,
25 111,191, 14,218, 70,105, 7, 87, 39,242, 29,155,188,148, 67, 3,
26 248, 17,199,246,144,239, 62,231, 6,195,213, 47,200,102, 30,215,
27 8,232,234,222,128, 82,238,247,132,170,114,172, 53, 77,106, 42,
28 150, 26,210,113, 90, 21, 73,116, 75,159,208, 94, 4, 24,164,236,
29 194,224, 65,110, 15, 81,203,204, 36,145,175, 80,161,244,112, 57,
30 153,124, 58,133, 35,184,180,122,252, 2, 54, 91, 37, 85,151, 49,
31 45, 93,250,152,227,138,146,174, 5,223, 41, 16,103,108,186,201,
32 211, 0,230,207,225,158,168, 44, 99, 22, 1, 63, 88,226,137,169,
33 13, 56, 52, 27,171, 51,255,176,187, 72, 12, 95,185,177,205, 46,
34 197,243,219, 71,229,165,156,119, 10,166, 32,104,254,127,193,173};
35
36 SecByteBlock L(128);
37 memcpy(L, key, keyLen);
38
39 int i;
40 for (i=keyLen; i<128; i++)
41 L[i] = PITABLE[(L[i-1] + L[i-keyLen]) & 255];
42
43 unsigned int T8 = (effectiveLen+7) / 8;
44 byte TM = byte((int)255 >> ((8-(effectiveLen%8))%8));
45 L[128-T8] = PITABLE[L[128-T8] & TM];
46
47 for (i=127-T8; i>=0; i--)
48 L[i] = PITABLE[L[i+1] ^ L[i+T8]];
49
50 for (i=0; i<64; i++)
51 K[i] = L[2*i] + (L[2*i+1] << 8);
52}
53
55
56void RC2::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
57{
58 word16 R0, R1, R2, R3;
59 Block::Get(inBlock)(R0)(R1)(R2)(R3);
60
61 for (int i = 0; i < 16; i++)
62 {
63 R0 += (R1 & ~R3) + (R2 & R3) + K[4*i+0];
64 R0 = rotlConstant<1>(R0);
65
66 R1 += (R2 & ~R0) + (R3 & R0) + K[4*i+1];
67 R1 = rotlConstant<2>(R1);
68
69 R2 += (R3 & ~R1) + (R0 & R1) + K[4*i+2];
70 R2 = rotlConstant<3>(R2);
71
72 R3 += (R0 & ~R2) + (R1 & R2) + K[4*i+3];
73 R3 = rotlConstant<5>(R3);
74
75 if (i == 4 || i == 10)
76 {
77 R0 = word16(R0 + K[R3 & 63]);
78 R1 = word16(R1 + K[R0 & 63]);
79 R2 = word16(R2 + K[R1 & 63]);
80 R3 = word16(R3 + K[R2 & 63]);
81 }
82 }
83
84 Block::Put(xorBlock, outBlock)(R0)(R1)(R2)(R3);
85}
86
87void RC2::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
88{
89 word16 R0, R1, R2, R3;
90 Block::Get(inBlock)(R0)(R1)(R2)(R3);
91
92 for (int i = 15; i >= 0; i--)
93 {
94 if (i == 4 || i == 10)
95 {
96 R3 = word16(R3 - K[R2 & 63]);
97 R2 = word16(R2 - K[R1 & 63]);
98 R1 = word16(R1 - K[R0 & 63]);
99 R0 = word16(R0 - K[R3 & 63]);
100 }
101
102 R3 = rotrConstant<5>(R3);
103 R3 = word16(R3 - ((R0 & ~R2) + (R1 & R2) + K[4*i+3]));
104
105 R2 = rotrConstant<3>(R2);
106 R2 = word16(R2 - ((R3 & ~R1) + (R0 & R1) + K[4*i+2]));
107
108 R1 = rotrConstant<2>(R1);
109 R1 = word16(R1 - ((R2 & ~R0) + (R3 & R0) + K[4*i+1]));
110
111 R0 = rotrConstant<1>(R0);
112 R0 = word16(R0 - ((R1 & ~R3) + (R2 & R3) + K[4*i+0]));
113 }
114
115 Block::Put(xorBlock, outBlock)(R0)(R1)(R2)(R3);
116}
117
118NAMESPACE_END
Standard names for retrieving values by name when working with NameValuePairs.
An invalid argument was detected.
Definition cryptlib.h:203
Interface for retrieving values given their names.
Definition cryptlib.h:322
CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Definition cryptlib.h:424
Access a block of memory.
Definition misc.h:2807
unsigned char byte
8-bit unsigned datatype
Definition config_int.h:56
unsigned short word16
16-bit unsigned datatype
Definition config_int.h:59
Utility functions for the Crypto++ library.
Precompiled header file.
Classes for the RC2 block cipher.
Access a block of memory.
Definition misc.h:2844