Coverage Report

Created: 2024-11-21 07:03

/src/cryptopp/rc6.cpp
Line
Count
Source
1
// rc6.cpp - written and placed in the public domain by Sean Woods
2
// based on Wei Dai's RC5 code.
3
4
#include "pch.h"
5
#include "rc6.h"
6
#include "misc.h"
7
#include "secblock.h"
8
9
NAMESPACE_BEGIN(CryptoPP)
10
11
void RC6::Base::UncheckedSetKey(const byte *k, unsigned int keylen, const NameValuePairs &params)
12
10
{
13
10
  AssertValidKeyLength(keylen);
14
15
10
  r = GetRoundsAndThrowIfInvalid(params, this);
16
10
  sTable.New(2*(r+2));
17
18
10
  static const RC6_WORD MAGIC_P = 0xb7e15163L;    // magic constant P for wordsize
19
10
  static const RC6_WORD MAGIC_Q = 0x9e3779b9L;    // magic constant Q for wordsize
20
10
  static const int U=sizeof(RC6_WORD);
21
22
10
  const unsigned int c = STDMAX((keylen+U-1)/U, 1U);  // RC6 paper says c=1 if keylen==0
23
10
  SecBlock<RC6_WORD> l(c);
24
25
10
  GetUserKey(LITTLE_ENDIAN_ORDER, l.begin(), c, k, keylen);
26
27
10
  sTable[0] = MAGIC_P;
28
440
  for (unsigned j=1; j<sTable.size();j++)
29
430
    sTable[j] = sTable[j-1] + MAGIC_Q;
30
31
10
  RC6_WORD a=0, b=0;
32
10
  const unsigned n = 3*STDMAX((unsigned int)sTable.size(), c);
33
34
1.33k
  for (unsigned h=0; h < n; h++)
35
1.32k
  {
36
1.32k
    a = sTable[h % sTable.size()] = rotlConstant<3>((sTable[h % sTable.size()] + a + b));
37
1.32k
    b = l[h % c] = rotlMod((l[h % c] + a + b), (a+b));
38
1.32k
  }
39
10
}
40
41
typedef BlockGetAndPut<RC6::RC6_WORD, LittleEndian> Block;
42
43
void RC6::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
44
887
{
45
887
  const RC6_WORD *sptr = sTable;
46
887
  RC6_WORD a, b, c, d, t, u;
47
48
887
  Block::Get(inBlock)(a)(b)(c)(d);
49
887
  b += sptr[0];
50
887
  d += sptr[1];
51
887
  sptr += 2;
52
53
18.6k
  for(unsigned i=0; i<r; i++)
54
17.7k
  {
55
17.7k
    t = rotlConstant<5>(b*(2*b+1));
56
17.7k
    u = rotlConstant<5>(d*(2*d+1));
57
17.7k
    a = rotlMod(a^t,u) + sptr[0];
58
17.7k
    c = rotlMod(c^u,t) + sptr[1];
59
17.7k
    t = a; a = b; b = c; c = d; d = t;
60
17.7k
    sptr += 2;
61
17.7k
  }
62
63
887
  a += sptr[0];
64
887
  c += sptr[1];
65
66
887
  Block::Put(xorBlock, outBlock)(a)(b)(c)(d);
67
887
}
68
69
void RC6::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
70
841
{
71
841
  const RC6_WORD *sptr = sTable.end();
72
841
  RC6_WORD a, b, c, d, t, u;
73
74
841
  Block::Get(inBlock)(a)(b)(c)(d);
75
76
841
  sptr -= 2;
77
841
  c -= sptr[1];
78
841
  a -= sptr[0];
79
80
17.6k
  for (unsigned i=0; i < r; i++)
81
16.8k
  {
82
16.8k
    sptr -= 2;
83
16.8k
    t = a; a = d; d = c; c = b; b = t;
84
16.8k
    u = rotlConstant<5>(d*(2 * d + 1));
85
16.8k
    t = rotlConstant<5>(b*(2 * b + 1));
86
16.8k
    c = rotrMod(c-sptr[1], t) ^ u;
87
16.8k
    a = rotrMod(a-sptr[0], u) ^ t;
88
16.8k
  }
89
90
841
  d -= sTable[1];
91
841
  b -= sTable[0];
92
93
841
  Block::Put(xorBlock, outBlock)(a)(b)(c)(d);
94
841
}
95
96
NAMESPACE_END