1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! # RC5 Cipher implementation
//!
//! This algorithm is described in this paper:
//! https://www.grc.com/r&d/rc5.pdf

use {
  crate::cipher::{encrypt_block, expand_key},
  cipher::decrypt_block,
  error::Error,
  secrecy::{ExposeSecret, SecretVec, Zeroize},
  std::mem::size_of,
  word::Word,
};

pub mod cipher;
pub mod error;
pub mod word;

/// RC5 Context
///
/// This struct holds the expanded key and the number of rounds that the
/// algorithm will use. Use this struct if you are going to encrypt or
/// decrypt multiple buffers of data with the same key.
///
/// Otherwise you can use the shorthand free standing functions encrypt and
/// decrypt.
pub struct Context<W: Word = u32> {
  pub expanded_key: SecretVec<W>,
  pub rounds: usize,
}

impl<W: Word> Context<W> {
  pub fn new(mut key: Vec<u8>, rounds: usize) -> Result<Self, Error> {
    let expanded_key = expand_key::<W>(&key, rounds)?;
    key.zeroize();

    Ok(Self {
      expanded_key: SecretVec::new(expanded_key),
      rounds,
    })
  }

  /// Encrypts bytes using the RC5 context and returns the ciphertext.
  /// The plaintext must be a multiple of the block size. Padding is not
  /// implemented.
  pub fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>, Error> {
    let word_bytes = size_of::<W>();
    let block_size = 2 * word_bytes;

    if plaintext.len() % block_size != 0 {
      return Err(Error::InvalidInputLength);
    }

    let mut ciphertext = Vec::with_capacity(plaintext.len());
    for block in plaintext.chunks(block_size) {
      let block = [
        W::from_le_bytes(&block[0..word_bytes])?,
        W::from_le_bytes(&block[word_bytes..block_size])?,
      ];

      ciphertext.extend(
        encrypt_block::<W>(self.expanded_key.expose_secret(), block)?
          .into_iter()
          .map(|w| w.to_le_bytes())
          .flatten(),
      );
    }

    Ok(ciphertext)
  }

  pub fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>, Error> {
    let word_bytes = size_of::<W>();
    let block_size = 2 * word_bytes;

    if ciphertext.len() % block_size != 0 {
      return Err(Error::InvalidInputLength);
    }

    let mut plaintext = Vec::with_capacity(ciphertext.len());
    for block in ciphertext.chunks(block_size) {
      let block = [
        W::from_le_bytes(&block[0..word_bytes])?,
        W::from_le_bytes(&block[word_bytes..block_size])?,
      ];

      plaintext.extend(
        decrypt_block::<W>(self.expanded_key.expose_secret(), block)?
          .into_iter()
          .map(|w| w.to_le_bytes())
          .flatten(),
      );
    }

    Ok(plaintext)
  }
}

/// Given a key and plaintext, returns the ciphertext using a parametrized RC5.
///
/// This is the generic RC5 implementation, which uses a generic word size and
/// rounds count.
///
/// The word size is specified by using a type parameter, which must implement
/// Word trait. The key size is specified by the length of the key slice.
/// The rounds count is specified by the rounds parameter.
///
/// Usage example:
///
/// ```
/// use rc5::encrypt;
/// let key = [
///   0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00, 0x95, 0x2C, 0x49, 0x10,
///   0x48, 0x81, 0xFF, 0x48,
/// ];
///
/// let pt = vec![0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84];
/// let ct = vec![0x11, 0xE4, 0x3B, 0x86, 0xD2, 0x31, 0xEA, 0x64];
/// let res = encrypt::<u32>(&key, &pt, 12).unwrap();
/// assert_eq!(ct, res);
/// ```
pub fn encrypt<W: Word>(
  key: &[u8],
  plaintext: &[u8],
  rounds: usize,
) -> Result<Vec<u8>, Error> {
  Context::<W>::new(key.to_vec(), rounds)?.encrypt(plaintext)
}

/// Given a key and ciphertext, returns the plaintext using a parametrized RC5.
///
/// This is the generic RC5 implementation, which uses a generic word size and
/// rounds count.
///
/// The word size is specified by using a type parameter, which must implement
/// Word trait. The key size is specified by the length of the key slice.
/// The rounds count is specified by the rounds parameter.
///
/// Usage example:
///
/// ```
/// use rc5::decrypt;
/// let key = [
///   0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00, 0x95, 0x2C, 0x49, 0x10,
///   0x48, 0x81, 0xFF, 0x48,
/// ];
///
/// let pt = vec![0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84];
/// let ct = vec![0x11, 0xE4, 0x3B, 0x86, 0xD2, 0x31, 0xEA, 0x64];
/// let res = decrypt::<u32>(&key, &ct, 12).unwrap();
/// assert_eq!(pt, res);
/// ```
pub fn decrypt<W: Word>(
  key: &[u8],
  ciphertext: &[u8],
  rounds: usize,
) -> Result<Vec<u8>, Error> {
  Context::<W>::new(key.to_vec(), rounds)?.decrypt(ciphertext)
}

/// Given a key and plaintext, return the ciphertext using RC5/32/12/16.
///
/// This is the default RC5 implementation, which uses 32-bit words and 12
/// rounds and a key size of 16 bytes.
///
/// Usage example:
///
/// ```
/// use rc5::encrypt_default;
/// let key = [
///   0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00, 0x95, 0x2C, 0x49, 0x10,
///   0x48, 0x81, 0xFF, 0x48,
/// ];
///
/// let pt = vec![0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84];
/// let ct = vec![0x11, 0xE4, 0x3B, 0x86, 0xD2, 0x31, 0xEA, 0x64];
/// let res = encrypt_default(key, &pt).unwrap();
/// assert_eq!(ct, res);
/// ```
pub fn encrypt_default(
  key: [u8; 16],
  plaintext: &[u8],
) -> Result<Vec<u8>, Error> {
  encrypt::<u32>(&key, plaintext, 12)
}

/// Given a key and ciphertext, return the plaintext using RC5/32/12/16
///
/// Usage example:
///
/// ```
/// use rc5::decrypt_default;
/// let key = [
///   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
///   0x0C, 0x0D, 0x0E, 0x0F,
/// ];
///
/// let pt = vec![0x96, 0x95, 0x0D, 0xDA, 0x65, 0x4A, 0x3D, 0x62];
/// let ct = vec![0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77];
/// let res = decrypt_default(key, &ct).unwrap();
/// assert_eq!(pt, res);
/// ```
pub fn decrypt_default(
  key: [u8; 16],
  ciphertext: &[u8],
) -> Result<Vec<u8>, Error> {
  decrypt::<u32>(&key, ciphertext, 12)
}