Coverage Report

Created: 2026-10-31 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/cipher/e_rc2.cc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/cipher.h>
16
#include <openssl/nid.h>
17
18
#include "../fipsmodule/cipher/internal.h"
19
#include "../internal.h"
20
21
22
#define c2l(c, l)                         \
23
1.66k
  do {                                    \
24
1.66k
    (l) = ((uint32_t)(*((c)++)));         \
25
1.66k
    (l) |= ((uint32_t)(*((c)++))) << 8L;  \
26
1.66k
    (l) |= ((uint32_t)(*((c)++))) << 16L; \
27
1.66k
    (l) |= ((uint32_t)(*((c)++))) << 24L; \
28
1.66k
  } while (0)
29
30
#define c2ln(c, l1, l2, n)                     \
31
0
  do {                                         \
32
0
    (c) += (n);                                \
33
0
    (l1) = (l2) = 0;                           \
34
0
    switch (n) {                               \
35
0
      case 8:                                  \
36
0
        (l2) = ((uint32_t)(*(--(c)))) << 24L;  \
37
0
        [[fallthrough]];                       \
38
0
      case 7:                                  \
39
0
        (l2) |= ((uint32_t)(*(--(c)))) << 16L; \
40
0
        [[fallthrough]];                       \
41
0
      case 6:                                  \
42
0
        (l2) |= ((uint32_t)(*(--(c)))) << 8L;  \
43
0
        [[fallthrough]];                       \
44
0
      case 5:                                  \
45
0
        (l2) |= ((uint32_t)(*(--(c))));        \
46
0
        [[fallthrough]];                       \
47
0
      case 4:                                  \
48
0
        (l1) = ((uint32_t)(*(--(c)))) << 24L;  \
49
0
        [[fallthrough]];                       \
50
0
      case 3:                                  \
51
0
        (l1) |= ((uint32_t)(*(--(c)))) << 16L; \
52
0
        [[fallthrough]];                       \
53
0
      case 2:                                  \
54
0
        (l1) |= ((uint32_t)(*(--(c)))) << 8L;  \
55
0
        [[fallthrough]];                       \
56
0
      case 1:                                  \
57
0
        (l1) |= ((uint32_t)(*(--(c))));        \
58
0
    }                                          \
59
0
  } while (0)
60
61
#define l2c(l, c)                              \
62
1.66k
  do {                                         \
63
1.66k
    *((c)++) = (uint8_t)(((l)) & 0xff);        \
64
1.66k
    *((c)++) = (uint8_t)(((l) >> 8L) & 0xff);  \
65
1.66k
    *((c)++) = (uint8_t)(((l) >> 16L) & 0xff); \
66
1.66k
    *((c)++) = (uint8_t)(((l) >> 24L) & 0xff); \
67
1.66k
  } while (0)
68
69
#define l2cn(l1, l2, c, n)                          \
70
0
  do {                                              \
71
0
    (c) += (n);                                     \
72
0
    switch (n) {                                    \
73
0
      case 8:                                       \
74
0
        *(--(c)) = (uint8_t)(((l2) >> 24L) & 0xff); \
75
0
        [[fallthrough]];                            \
76
0
      case 7:                                       \
77
0
        *(--(c)) = (uint8_t)(((l2) >> 16L) & 0xff); \
78
0
        [[fallthrough]];                            \
79
0
      case 6:                                       \
80
0
        *(--(c)) = (uint8_t)(((l2) >> 8L) & 0xff);  \
81
0
        [[fallthrough]];                            \
82
0
      case 5:                                       \
83
0
        *(--(c)) = (uint8_t)(((l2)) & 0xff);        \
84
0
        [[fallthrough]];                            \
85
0
      case 4:                                       \
86
0
        *(--(c)) = (uint8_t)(((l1) >> 24L) & 0xff); \
87
0
        [[fallthrough]];                            \
88
0
      case 3:                                       \
89
0
        *(--(c)) = (uint8_t)(((l1) >> 16L) & 0xff); \
90
0
        [[fallthrough]];                            \
91
0
      case 2:                                       \
92
0
        *(--(c)) = (uint8_t)(((l1) >> 8L) & 0xff);  \
93
0
        [[fallthrough]];                            \
94
0
      case 1:                                       \
95
0
        *(--(c)) = (uint8_t)(((l1)) & 0xff);        \
96
0
    }                                               \
97
0
  } while (0)
98
99
typedef struct rc2_key_st {
100
  uint16_t data[64];
101
} RC2_KEY;
102
103
0
static void rc2_encrypt(uint32_t *d, RC2_KEY *key) {
104
0
  int i, n;
105
0
  uint16_t *p0, *p1;
106
0
  uint16_t x0, x1, x2, x3, t;
107
0
  uint32_t l;
108
109
0
  l = d[0];
110
0
  x0 = (uint16_t)l & 0xffff;
111
0
  x1 = (uint16_t)(l >> 16L);
112
0
  l = d[1];
113
0
  x2 = (uint16_t)l & 0xffff;
114
0
  x3 = (uint16_t)(l >> 16L);
115
116
0
  n = 3;
117
0
  i = 5;
118
119
0
  p0 = p1 = &key->data[0];
120
0
  for (;;) {
121
0
    t = (x0 + (x1 & ~x3) + (x2 & x3) + *(p0++)) & 0xffff;
122
0
    x0 = (t << 1) | (t >> 15);
123
0
    t = (x1 + (x2 & ~x0) + (x3 & x0) + *(p0++)) & 0xffff;
124
0
    x1 = (t << 2) | (t >> 14);
125
0
    t = (x2 + (x3 & ~x1) + (x0 & x1) + *(p0++)) & 0xffff;
126
0
    x2 = (t << 3) | (t >> 13);
127
0
    t = (x3 + (x0 & ~x2) + (x1 & x2) + *(p0++)) & 0xffff;
128
0
    x3 = (t << 5) | (t >> 11);
129
130
0
    if (--i == 0) {
131
0
      if (--n == 0) {
132
0
        break;
133
0
      }
134
0
      i = (n == 2) ? 6 : 5;
135
136
0
      x0 += p1[x3 & 0x3f];
137
0
      x1 += p1[x0 & 0x3f];
138
0
      x2 += p1[x1 & 0x3f];
139
0
      x3 += p1[x2 & 0x3f];
140
0
    }
141
0
  }
142
143
0
  d[0] = (uint32_t)(x0 & 0xffff) | ((uint32_t)(x1 & 0xffff) << 16L);
144
0
  d[1] = (uint32_t)(x2 & 0xffff) | ((uint32_t)(x3 & 0xffff) << 16L);
145
0
}
146
147
820
static void rc2_decrypt(uint32_t *d, RC2_KEY *key) {
148
820
  int i, n;
149
820
  uint16_t *p0, *p1;
150
820
  uint16_t x0, x1, x2, x3, t;
151
820
  uint32_t l;
152
153
820
  l = d[0];
154
820
  x0 = (uint16_t)l & 0xffff;
155
820
  x1 = (uint16_t)(l >> 16L);
156
820
  l = d[1];
157
820
  x2 = (uint16_t)l & 0xffff;
158
820
  x3 = (uint16_t)(l >> 16L);
159
160
820
  n = 3;
161
820
  i = 5;
162
163
820
  p0 = &key->data[63];
164
820
  p1 = &key->data[0];
165
13.1k
  for (;;) {
166
13.1k
    t = ((x3 << 11) | (x3 >> 5)) & 0xffff;
167
13.1k
    x3 = (t - (x0 & ~x2) - (x1 & x2) - *(p0--)) & 0xffff;
168
13.1k
    t = ((x2 << 13) | (x2 >> 3)) & 0xffff;
169
13.1k
    x2 = (t - (x3 & ~x1) - (x0 & x1) - *(p0--)) & 0xffff;
170
13.1k
    t = ((x1 << 14) | (x1 >> 2)) & 0xffff;
171
13.1k
    x1 = (t - (x2 & ~x0) - (x3 & x0) - *(p0--)) & 0xffff;
172
13.1k
    t = ((x0 << 15) | (x0 >> 1)) & 0xffff;
173
13.1k
    x0 = (t - (x1 & ~x3) - (x2 & x3) - *(p0--)) & 0xffff;
174
175
13.1k
    if (--i == 0) {
176
2.46k
      if (--n == 0) {
177
820
        break;
178
820
      }
179
1.64k
      i = (n == 2) ? 6 : 5;
180
181
1.64k
      x3 = (x3 - p1[x2 & 0x3f]) & 0xffff;
182
1.64k
      x2 = (x2 - p1[x1 & 0x3f]) & 0xffff;
183
1.64k
      x1 = (x1 - p1[x0 & 0x3f]) & 0xffff;
184
1.64k
      x0 = (x0 - p1[x3 & 0x3f]) & 0xffff;
185
1.64k
    }
186
13.1k
  }
187
188
820
  d[0] = (uint32_t)(x0 & 0xffff) | ((uint32_t)(x1 & 0xffff) << 16L);
189
820
  d[1] = (uint32_t)(x2 & 0xffff) | ((uint32_t)(x3 & 0xffff) << 16L);
190
820
}
191
192
static void rc2_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
193
14
                            RC2_KEY *ks, uint8_t *iv, int encrypt) {
194
14
  uint32_t tin0, tin1;
195
14
  uint32_t tout0, tout1, xor0, xor1;
196
14
  long l = length;
197
14
  uint32_t tin[2];
198
199
14
  if (encrypt) {
200
0
    c2l(iv, tout0);
201
0
    c2l(iv, tout1);
202
0
    iv -= 8;
203
0
    for (l -= 8; l >= 0; l -= 8) {
204
0
      c2l(in, tin0);
205
0
      c2l(in, tin1);
206
0
      tin0 ^= tout0;
207
0
      tin1 ^= tout1;
208
0
      tin[0] = tin0;
209
0
      tin[1] = tin1;
210
0
      rc2_encrypt(tin, ks);
211
0
      tout0 = tin[0];
212
0
      l2c(tout0, out);
213
0
      tout1 = tin[1];
214
0
      l2c(tout1, out);
215
0
    }
216
0
    if (l != -8) {
217
0
      c2ln(in, tin0, tin1, l + 8);
218
0
      tin0 ^= tout0;
219
0
      tin1 ^= tout1;
220
0
      tin[0] = tin0;
221
0
      tin[1] = tin1;
222
0
      rc2_encrypt(tin, ks);
223
0
      tout0 = tin[0];
224
0
      l2c(tout0, out);
225
0
      tout1 = tin[1];
226
0
      l2c(tout1, out);
227
0
    }
228
0
    l2c(tout0, iv);
229
0
    l2c(tout1, iv);
230
14
  } else {
231
14
    c2l(iv, xor0);
232
14
    c2l(iv, xor1);
233
14
    iv -= 8;
234
834
    for (l -= 8; l >= 0; l -= 8) {
235
820
      c2l(in, tin0);
236
820
      tin[0] = tin0;
237
820
      c2l(in, tin1);
238
820
      tin[1] = tin1;
239
820
      rc2_decrypt(tin, ks);
240
820
      tout0 = tin[0] ^ xor0;
241
820
      tout1 = tin[1] ^ xor1;
242
820
      l2c(tout0, out);
243
820
      l2c(tout1, out);
244
820
      xor0 = tin0;
245
820
      xor1 = tin1;
246
820
    }
247
14
    if (l != -8) {
248
0
      c2l(in, tin0);
249
0
      tin[0] = tin0;
250
0
      c2l(in, tin1);
251
0
      tin[1] = tin1;
252
0
      rc2_decrypt(tin, ks);
253
0
      tout0 = tin[0] ^ xor0;
254
0
      tout1 = tin[1] ^ xor1;
255
0
      l2cn(tout0, tout1, out, l + 8);
256
0
      xor0 = tin0;
257
0
      xor1 = tin1;
258
0
    }
259
14
    l2c(xor0, iv);
260
14
    l2c(xor1, iv);
261
14
  }
262
14
  tin[0] = tin[1] = 0;
263
14
}
264
265
static const uint8_t key_table[256] = {
266
    0xd9, 0x78, 0xf9, 0xc4, 0x19, 0xdd, 0xb5, 0xed, 0x28, 0xe9, 0xfd, 0x79,
267
    0x4a, 0xa0, 0xd8, 0x9d, 0xc6, 0x7e, 0x37, 0x83, 0x2b, 0x76, 0x53, 0x8e,
268
    0x62, 0x4c, 0x64, 0x88, 0x44, 0x8b, 0xfb, 0xa2, 0x17, 0x9a, 0x59, 0xf5,
269
    0x87, 0xb3, 0x4f, 0x13, 0x61, 0x45, 0x6d, 0x8d, 0x09, 0x81, 0x7d, 0x32,
270
    0xbd, 0x8f, 0x40, 0xeb, 0x86, 0xb7, 0x7b, 0x0b, 0xf0, 0x95, 0x21, 0x22,
271
    0x5c, 0x6b, 0x4e, 0x82, 0x54, 0xd6, 0x65, 0x93, 0xce, 0x60, 0xb2, 0x1c,
272
    0x73, 0x56, 0xc0, 0x14, 0xa7, 0x8c, 0xf1, 0xdc, 0x12, 0x75, 0xca, 0x1f,
273
    0x3b, 0xbe, 0xe4, 0xd1, 0x42, 0x3d, 0xd4, 0x30, 0xa3, 0x3c, 0xb6, 0x26,
274
    0x6f, 0xbf, 0x0e, 0xda, 0x46, 0x69, 0x07, 0x57, 0x27, 0xf2, 0x1d, 0x9b,
275
    0xbc, 0x94, 0x43, 0x03, 0xf8, 0x11, 0xc7, 0xf6, 0x90, 0xef, 0x3e, 0xe7,
276
    0x06, 0xc3, 0xd5, 0x2f, 0xc8, 0x66, 0x1e, 0xd7, 0x08, 0xe8, 0xea, 0xde,
277
    0x80, 0x52, 0xee, 0xf7, 0x84, 0xaa, 0x72, 0xac, 0x35, 0x4d, 0x6a, 0x2a,
278
    0x96, 0x1a, 0xd2, 0x71, 0x5a, 0x15, 0x49, 0x74, 0x4b, 0x9f, 0xd0, 0x5e,
279
    0x04, 0x18, 0xa4, 0xec, 0xc2, 0xe0, 0x41, 0x6e, 0x0f, 0x51, 0xcb, 0xcc,
280
    0x24, 0x91, 0xaf, 0x50, 0xa1, 0xf4, 0x70, 0x39, 0x99, 0x7c, 0x3a, 0x85,
281
    0x23, 0xb8, 0xb4, 0x7a, 0xfc, 0x02, 0x36, 0x5b, 0x25, 0x55, 0x97, 0x31,
282
    0x2d, 0x5d, 0xfa, 0x98, 0xe3, 0x8a, 0x92, 0xae, 0x05, 0xdf, 0x29, 0x10,
283
    0x67, 0x6c, 0xba, 0xc9, 0xd3, 0x00, 0xe6, 0xcf, 0xe1, 0x9e, 0xa8, 0x2c,
284
    0x63, 0x16, 0x01, 0x3f, 0x58, 0xe2, 0x89, 0xa9, 0x0d, 0x38, 0x34, 0x1b,
285
    0xab, 0x33, 0xff, 0xb0, 0xbb, 0x48, 0x0c, 0x5f, 0xb9, 0xb1, 0xcd, 0x2e,
286
    0xc5, 0xf3, 0xdb, 0x47, 0xe5, 0xa5, 0x9c, 0x77, 0x0a, 0xa6, 0x20, 0x68,
287
    0xfe, 0x7f, 0xc1, 0xad,
288
};
289
290
static int rc2_set_key(RC2_KEY *key, size_t len, const uint8_t *data,
291
7
                       int bits) {
292
7
  size_t i, j;
293
7
  uint8_t *k;
294
7
  unsigned int c, d;
295
296
7
  k = (uint8_t *)&key->data[0];
297
7
  *k = 0;  // for if there is a zero length key
298
299
7
  if (len == 0) {
300
0
    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
301
0
    return 0;
302
0
  }
303
7
  if (len > 128) {
304
0
    len = 128;
305
0
  }
306
307
7
  if (bits <= 0) {
308
0
    bits = 1024;
309
0
  }
310
7
  if (bits > 1024) {
311
0
    bits = 1024;
312
0
  }
313
314
42
  for (i = 0; i < len; i++) {
315
35
    k[i] = data[i];
316
35
  }
317
318
  // expand table
319
7
  d = k[len - 1];
320
7
  j = 0;
321
868
  for (i = len; i < 128; i++, j++) {
322
861
    d = key_table[(k[j] + d) & 0xff];
323
861
    k[i] = d;
324
861
  }
325
326
  // hmm.... key reduction to 'bits' bits
327
328
7
  j = (bits + 7) >> 3;
329
7
  i = 128 - j;
330
7
  c = (0xff >> (-bits & 0x07));
331
332
7
  d = key_table[k[i] & c];
333
7
  k[i] = d;
334
335
868
  while (i > 0) {
336
861
    --i;
337
861
    d = key_table[k[i + j] ^ d];
338
861
    k[i] = d;
339
861
  }
340
341
  // copy from bytes into uint16_t's
342
455
  for (i = 0; i < 64; ++i) {
343
448
    j = 63 - i;
344
448
    key->data[j] = ((k[2 * j + 1] << 8) | k[2 * j]) & 0xffff;
345
448
  }
346
347
7
  return 1;
348
7
}
349
350
typedef struct {
351
  int key_bits;  // effective key bits
352
  RC2_KEY ks;    // key schedule
353
} EVP_RC2_KEY;
354
355
static int rc2_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
356
7
                        const uint8_t *iv, int enc) {
357
7
  EVP_RC2_KEY *rc2_key = (EVP_RC2_KEY *)ctx->cipher_data;
358
7
  if (!rc2_set_key(&rc2_key->ks, EVP_CIPHER_CTX_key_length(ctx), key,
359
7
                   rc2_key->key_bits)) {
360
0
    return 0;
361
0
  }
362
7
  return 1;
363
7
}
364
365
static int rc2_cbc_cipher_update(EVP_CIPHER_CTX *ctx, uint8_t *out,
366
14
                                 const uint8_t *in, size_t len) {
367
14
  EVP_RC2_KEY *key = (EVP_RC2_KEY *)ctx->cipher_data;
368
14
  static const size_t kChunkSize = 0x10000;
369
370
14
  while (len >= kChunkSize) {
371
0
    rc2_cbc_encrypt(in, out, kChunkSize, &key->ks, ctx->iv, ctx->encrypt);
372
0
    len -= kChunkSize;
373
0
    in += kChunkSize;
374
0
    out += kChunkSize;
375
0
  }
376
14
  if (len) {
377
14
    rc2_cbc_encrypt(in, out, len, &key->ks, ctx->iv, ctx->encrypt);
378
14
  }
379
14
  return 1;
380
14
}
381
382
7
static int rc2_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) {
383
7
  EVP_RC2_KEY *key = (EVP_RC2_KEY *)ctx->cipher_data;
384
385
7
  switch (type) {
386
7
    case EVP_CTRL_INIT:
387
7
      key->key_bits = EVP_CIPHER_CTX_key_length(ctx) * 8;
388
7
      return 1;
389
0
    case EVP_CTRL_SET_RC2_KEY_BITS:
390
      // Should be overridden by later call to `EVP_CTRL_INIT`, but
391
      // people call it, so it may as well work.
392
0
      key->key_bits = arg;
393
0
      return 1;
394
395
0
    default:
396
0
      return -1;
397
7
  }
398
7
}
399
400
static const EVP_CIPHER rc2_40_cbc = {
401
    /*nid=*/NID_rc2_40_cbc,
402
    /*block_size=*/8,
403
    /*key_len=*/5 /* 40 bit */,
404
    /*iv_len=*/8,
405
    /*ctx_size=*/sizeof(EVP_RC2_KEY),
406
    /*flags=*/EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
407
    /*init=*/rc2_init_key,
408
    /*cipher_update=*/rc2_cbc_cipher_update,
409
    /*cipher_final=*/nullptr,
410
    /*update_aad=*/nullptr,
411
    /*cleanup=*/nullptr,
412
    /*ctrl=*/rc2_ctrl,
413
};
414
415
7
const EVP_CIPHER *EVP_rc2_40_cbc() { return &rc2_40_cbc; }
416
417
static const EVP_CIPHER rc2_cbc = {
418
    /*nid=*/NID_rc2_cbc,
419
    /*block_size=*/8,
420
    /*key_len=*/16 /* 128 bit */,
421
    /*iv_len=*/8,
422
    /*ctx_size=*/sizeof(EVP_RC2_KEY),
423
    /*flags=*/EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
424
    /*init=*/rc2_init_key,
425
    /*cipher_update=*/rc2_cbc_cipher_update,
426
    /*cipher_final=*/nullptr,
427
    /*update_aad=*/nullptr,
428
    /*cleanup=*/nullptr,
429
    /*ctrl=*/rc2_ctrl,
430
};
431
432
0
const EVP_CIPHER *EVP_rc2_cbc() { return &rc2_cbc; }