openGPMP
Open Source Mathematics Package
Public Member Functions | Private Member Functions | Private Attributes | List of all members
RC6 Class Reference

Class implementing the RC6 cipher algorithm. More...

#include <rc6.hpp>

Public Member Functions

 RC6 (const std::vector< uint8_t > &key)
 Constructor for RC6 class. More...
 
std::vector< uint8_t > encrypt (const std::vector< uint8_t > &plaintext)
 Encrypts plaintext using RC6 algorithm. More...
 
std::vector< uint8_t > decrypt (const std::vector< uint8_t > &ciphertext)
 Decrypts ciphertext using RC6 algorithm. More...
 

Private Member Functions

void key_schedule (const std::vector< uint8_t > &key)
 Generates the key schedule from the given key. More...
 
std::vector< uint32_t > expand (const std::vector< uint8_t > &key)
 Performs key expansion. More...
 
uint32_t rotl (uint32_t val, int shift)
 Performs left rotation. More...
 
uint32_t rotr (uint32_t val, int shift)
 Performs right rotation. More...
 
void encrypt_block (const uint32_t plaintext[2], uint32_t ciphertext[2])
 Encrypts a single block of plaintext. More...
 
void decrypt_block (const uint32_t ciphertext[2], uint32_t plaintext[2])
 Decrypts a single block of ciphertext. More...
 

Private Attributes

const int w = 32
 
const int r = 20
 
const int b = 16
 
std::vector< uint32_t > S
 

Detailed Description

Class implementing the RC6 cipher algorithm.

Definition at line 44 of file rc6.hpp.

Constructor & Destructor Documentation

◆ RC6()

RC6::RC6 ( const std::vector< uint8_t > &  key)

Constructor for RC6 class.

Parameters
keyThe encryption key

Definition at line 37 of file rc6.cpp.

37  {
38  key_schedule(key);
39 }
void key_schedule(const std::vector< uint8_t > &key)
Generates the key schedule from the given key.
Definition: rc6.cpp:41

References key_schedule().

Member Function Documentation

◆ decrypt()

std::vector< uint8_t > RC6::decrypt ( const std::vector< uint8_t > &  ciphertext)

Decrypts ciphertext using RC6 algorithm.

Parameters
ciphertextThe ciphertext to decrypt
Returns
The decrypted plaintext

Definition at line 125 of file rc6.cpp.

125  {
126  if (ciphertext.size() % 8 != 0) {
127  throw std::invalid_argument(
128  "Ciphertext length must be a multiple of 8 bytes");
129  }
130  std::vector<uint8_t> plaintext;
131  plaintext.reserve(ciphertext.size());
132  for (size_t i = 0; i < ciphertext.size(); i += 8) {
133  uint32_t block[2];
134  uint32_t decrypted_block[2];
135  for (int j = 0; j < 2; ++j) {
136  block[j] =
137  (static_cast<uint32_t>(ciphertext[i + 4 * j]) << 24) |
138  (static_cast<uint32_t>(ciphertext[i + 4 * j + 1]) << 16) |
139  (static_cast<uint32_t>(ciphertext[i + 4 * j + 2]) << 8) |
140  static_cast<uint32_t>(ciphertext[i + 4 * j + 3]);
141  }
142  decrypt_block(block, decrypted_block);
143  for (int j = 0; j < 8; ++j) {
144  plaintext.push_back((decrypted_block[j / 4] >> (8 * (j % 4))) &
145  0xFF);
146  }
147  }
148  return plaintext;
149 }
void decrypt_block(const uint32_t ciphertext[2], uint32_t plaintext[2])
Decrypts a single block of ciphertext.
Definition: rc6.cpp:88

References decrypt_block().

◆ decrypt_block()

void RC6::decrypt_block ( const uint32_t  ciphertext[2],
uint32_t  plaintext[2] 
)
private

Decrypts a single block of ciphertext.

Parameters
ciphertextThe ciphertext block to decrypt
plaintextThe resulting plaintext block

Definition at line 88 of file rc6.cpp.

88  {
89  uint32_t A = ciphertext[0], B = ciphertext[1];
90  for (int i = r; i >= 1; --i) {
91  B = rotr(B - S[2 * i + 1], A) ^ A;
92  A = rotr(A - S[2 * i], B) ^ B;
93  }
94  B -= S[1];
95  A -= S[0];
96  plaintext[0] = A;
97  plaintext[1] = B;
98 }
const int r
Definition: rc6.hpp:68
std::vector< uint32_t > S
Definition: rc6.hpp:71
uint32_t rotr(uint32_t val, int shift)
Performs right rotation.
Definition: rc6.cpp:72
list A
Definition: linalg.py:22
list B
Definition: linalg.py:23

References python.linalg::A, python.linalg::B, r, rotr(), and S.

Referenced by decrypt().

◆ encrypt()

std::vector< uint8_t > RC6::encrypt ( const std::vector< uint8_t > &  plaintext)

Encrypts plaintext using RC6 algorithm.

Parameters
plaintextThe plaintext to encrypt
Returns
The encrypted ciphertext

Definition at line 100 of file rc6.cpp.

100  {
101  if (plaintext.size() % 8 != 0) {
102  throw std::invalid_argument(
103  "Plaintext length must be a multiple of 8 bytes");
104  }
105  std::vector<uint8_t> ciphertext;
106  ciphertext.reserve(plaintext.size());
107  for (size_t i = 0; i < plaintext.size(); i += 8) {
108  uint32_t block[2];
109  uint32_t encrypted_block[2];
110  for (int j = 0; j < 8; ++j) {
111  block[j / 4] |=
112  (static_cast<uint32_t>(plaintext[i + j]) << (8 * (j % 4)));
113  }
114  encrypt_block(block, encrypted_block);
115  for (int j = 0; j < 2; ++j) {
116  ciphertext.push_back((encrypted_block[j] >> 24) & 0xFF);
117  ciphertext.push_back((encrypted_block[j] >> 16) & 0xFF);
118  ciphertext.push_back((encrypted_block[j] >> 8) & 0xFF);
119  ciphertext.push_back(encrypted_block[j] & 0xFF);
120  }
121  }
122  return ciphertext;
123 }
void encrypt_block(const uint32_t plaintext[2], uint32_t ciphertext[2])
Encrypts a single block of plaintext.
Definition: rc6.cpp:76

References encrypt_block().

◆ encrypt_block()

void RC6::encrypt_block ( const uint32_t  plaintext[2],
uint32_t  ciphertext[2] 
)
private

Encrypts a single block of plaintext.

Parameters
plaintextThe plaintext block to encrypt
ciphertextThe resulting ciphertext block

Definition at line 76 of file rc6.cpp.

76  {
77  uint32_t A = plaintext[0], B = plaintext[1];
78  A += S[0];
79  B += S[1];
80  for (int i = 1; i <= r; ++i) {
81  A = rotl(A ^ B, B) + S[2 * i];
82  B = rotl(B ^ A, A) + S[2 * i + 1];
83  }
84  ciphertext[0] = A;
85  ciphertext[1] = B;
86 }
uint32_t rotl(uint32_t val, int shift)
Performs left rotation.
Definition: rc6.cpp:68

References python.linalg::A, python.linalg::B, r, rotl(), and S.

Referenced by encrypt().

◆ expand()

std::vector< uint32_t > RC6::expand ( const std::vector< uint8_t > &  key)
private

Performs key expansion.

Parameters
keyThe encryption key
Returns
The expanded key schedule

Definition at line 60 of file rc6.cpp.

60  {
61  std::vector<uint32_t> L(b / 4);
62  for (size_t i = 0; i < key.size(); i++) {
63  L[i / 4] |= (static_cast<uint32_t>(key[i]) << (8 * (i % 4)));
64  }
65  return L;
66 }
const int b
Definition: rc6.hpp:69

References b.

Referenced by key_schedule().

◆ key_schedule()

void RC6::key_schedule ( const std::vector< uint8_t > &  key)
private

Generates the key schedule from the given key.

Parameters
keyThe encryption key

Definition at line 41 of file rc6.cpp.

41  {
42  std::vector<uint32_t> L = expand(key);
43  S.resize(2 * (r + 1));
44  const uint32_t P = 0xB7E15163;
45  const uint32_t Q = 0x9E3779B9;
46  S[0] = P;
47  for (int i = 1; i < 2 * (r + 1); ++i) {
48  S[i] = S[i - 1] + Q;
49  }
50  uint32_t A = 0, B = 0;
51  int i = 0, j = 0;
52  for (int k = 0; k < 3 * std::max(b, 2 * (r + 1)); ++k) {
53  A = S[i] = rotl((S[i] + A + B), 3);
54  B = L[j] = rotl((L[j] + A + B), (A + B));
55  i = (i + 1) % (2 * (r + 1));
56  j = (j + 1) % b;
57  }
58 }
std::vector< uint32_t > expand(const std::vector< uint8_t > &key)
Performs key expansion.
Definition: rc6.cpp:60

References python.linalg::A, b, python.linalg::B, expand(), r, rotl(), and S.

Referenced by RC6().

◆ rotl()

uint32_t RC6::rotl ( uint32_t  val,
int  shift 
)
private

Performs left rotation.

Parameters
valThe value to rotate
shiftThe number of bits to rotate by
Returns
The rotated value

Definition at line 68 of file rc6.cpp.

68  {
69  return (val << shift) | (val >> (w - shift));
70 }
const int w
Definition: rc6.hpp:67

References w.

Referenced by encrypt_block(), and key_schedule().

◆ rotr()

uint32_t RC6::rotr ( uint32_t  val,
int  shift 
)
private

Performs right rotation.

Parameters
valThe value to rotate
shiftThe number of bits to rotate by
Returns
The rotated value

Definition at line 72 of file rc6.cpp.

72  {
73  return (val >> shift) | (val << (w - shift));
74 }

References w.

Referenced by decrypt_block().

Member Data Documentation

◆ b

const int RC6::b = 16
private

Number of bytes in key

Definition at line 69 of file rc6.hpp.

Referenced by expand(), and key_schedule().

◆ r

const int RC6::r = 20
private

Number of rounds

Definition at line 68 of file rc6.hpp.

Referenced by decrypt_block(), encrypt_block(), and key_schedule().

◆ S

std::vector<uint32_t> RC6::S
private

Internal state array

Definition at line 71 of file rc6.hpp.

Referenced by decrypt_block(), encrypt_block(), and key_schedule().

◆ w

const int RC6::w = 32
private

Word size

Definition at line 67 of file rc6.hpp.

Referenced by rotl(), and rotr().


The documentation for this class was generated from the following files: