yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
| 8 | */ |
| 9 | |
| 10 | #include <openssl/rc2.h> |
| 11 | #include "rc2_local.h" |
| 12 | |
| 13 | void RC2_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, |
| 14 | RC2_KEY *ks, unsigned char *iv, int encrypt) |
| 15 | { |
| 16 | register unsigned long tin0, tin1; |
| 17 | register unsigned long tout0, tout1, xor0, xor1; |
| 18 | register long l = length; |
| 19 | unsigned long tin[2]; |
| 20 | |
| 21 | if (encrypt) { |
| 22 | c2l(iv, tout0); |
| 23 | c2l(iv, tout1); |
| 24 | iv -= 8; |
| 25 | for (l -= 8; l >= 0; l -= 8) { |
| 26 | c2l(in, tin0); |
| 27 | c2l(in, tin1); |
| 28 | tin0 ^= tout0; |
| 29 | tin1 ^= tout1; |
| 30 | tin[0] = tin0; |
| 31 | tin[1] = tin1; |
| 32 | RC2_encrypt(tin, ks); |
| 33 | tout0 = tin[0]; |
| 34 | l2c(tout0, out); |
| 35 | tout1 = tin[1]; |
| 36 | l2c(tout1, out); |
| 37 | } |
| 38 | if (l != -8) { |
| 39 | c2ln(in, tin0, tin1, l + 8); |
| 40 | tin0 ^= tout0; |
| 41 | tin1 ^= tout1; |
| 42 | tin[0] = tin0; |
| 43 | tin[1] = tin1; |
| 44 | RC2_encrypt(tin, ks); |
| 45 | tout0 = tin[0]; |
| 46 | l2c(tout0, out); |
| 47 | tout1 = tin[1]; |
| 48 | l2c(tout1, out); |
| 49 | } |
| 50 | l2c(tout0, iv); |
| 51 | l2c(tout1, iv); |
| 52 | } else { |
| 53 | c2l(iv, xor0); |
| 54 | c2l(iv, xor1); |
| 55 | iv -= 8; |
| 56 | for (l -= 8; l >= 0; l -= 8) { |
| 57 | c2l(in, tin0); |
| 58 | tin[0] = tin0; |
| 59 | c2l(in, tin1); |
| 60 | tin[1] = tin1; |
| 61 | RC2_decrypt(tin, ks); |
| 62 | tout0 = tin[0] ^ xor0; |
| 63 | tout1 = tin[1] ^ xor1; |
| 64 | l2c(tout0, out); |
| 65 | l2c(tout1, out); |
| 66 | xor0 = tin0; |
| 67 | xor1 = tin1; |
| 68 | } |
| 69 | if (l != -8) { |
| 70 | c2l(in, tin0); |
| 71 | tin[0] = tin0; |
| 72 | c2l(in, tin1); |
| 73 | tin[1] = tin1; |
| 74 | RC2_decrypt(tin, ks); |
| 75 | tout0 = tin[0] ^ xor0; |
| 76 | tout1 = tin[1] ^ xor1; |
| 77 | l2cn(tout0, tout1, out, l + 8); |
| 78 | xor0 = tin0; |
| 79 | xor1 = tin1; |
| 80 | } |
| 81 | l2c(xor0, iv); |
| 82 | l2c(xor1, iv); |
| 83 | } |
| 84 | tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; |
| 85 | tin[0] = tin[1] = 0; |
| 86 | } |
| 87 | |
| 88 | void RC2_encrypt(unsigned long *d, RC2_KEY *key) |
| 89 | { |
| 90 | int i, n; |
| 91 | register RC2_INT *p0, *p1; |
| 92 | register RC2_INT x0, x1, x2, x3, t; |
| 93 | unsigned long l; |
| 94 | |
| 95 | l = d[0]; |
| 96 | x0 = (RC2_INT) l & 0xffff; |
| 97 | x1 = (RC2_INT) (l >> 16L); |
| 98 | l = d[1]; |
| 99 | x2 = (RC2_INT) l & 0xffff; |
| 100 | x3 = (RC2_INT) (l >> 16L); |
| 101 | |
| 102 | n = 3; |
| 103 | i = 5; |
| 104 | |
| 105 | p0 = p1 = &(key->data[0]); |
| 106 | for (;;) { |
| 107 | t = (x0 + (x1 & ~x3) + (x2 & x3) + *(p0++)) & 0xffff; |
| 108 | x0 = (t << 1) | (t >> 15); |
| 109 | t = (x1 + (x2 & ~x0) + (x3 & x0) + *(p0++)) & 0xffff; |
| 110 | x1 = (t << 2) | (t >> 14); |
| 111 | t = (x2 + (x3 & ~x1) + (x0 & x1) + *(p0++)) & 0xffff; |
| 112 | x2 = (t << 3) | (t >> 13); |
| 113 | t = (x3 + (x0 & ~x2) + (x1 & x2) + *(p0++)) & 0xffff; |
| 114 | x3 = (t << 5) | (t >> 11); |
| 115 | |
| 116 | if (--i == 0) { |
| 117 | if (--n == 0) |
| 118 | break; |
| 119 | i = (n == 2) ? 6 : 5; |
| 120 | |
| 121 | x0 += p1[x3 & 0x3f]; |
| 122 | x1 += p1[x0 & 0x3f]; |
| 123 | x2 += p1[x1 & 0x3f]; |
| 124 | x3 += p1[x2 & 0x3f]; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | d[0] = |
| 129 | (unsigned long)(x0 & 0xffff) | ((unsigned long)(x1 & 0xffff) << 16L); |
| 130 | d[1] = |
| 131 | (unsigned long)(x2 & 0xffff) | ((unsigned long)(x3 & 0xffff) << 16L); |
| 132 | } |
| 133 | |
| 134 | void RC2_decrypt(unsigned long *d, RC2_KEY *key) |
| 135 | { |
| 136 | int i, n; |
| 137 | register RC2_INT *p0, *p1; |
| 138 | register RC2_INT x0, x1, x2, x3, t; |
| 139 | unsigned long l; |
| 140 | |
| 141 | l = d[0]; |
| 142 | x0 = (RC2_INT) l & 0xffff; |
| 143 | x1 = (RC2_INT) (l >> 16L); |
| 144 | l = d[1]; |
| 145 | x2 = (RC2_INT) l & 0xffff; |
| 146 | x3 = (RC2_INT) (l >> 16L); |
| 147 | |
| 148 | n = 3; |
| 149 | i = 5; |
| 150 | |
| 151 | p0 = &(key->data[63]); |
| 152 | p1 = &(key->data[0]); |
| 153 | for (;;) { |
| 154 | t = ((x3 << 11) | (x3 >> 5)) & 0xffff; |
| 155 | x3 = (t - (x0 & ~x2) - (x1 & x2) - *(p0--)) & 0xffff; |
| 156 | t = ((x2 << 13) | (x2 >> 3)) & 0xffff; |
| 157 | x2 = (t - (x3 & ~x1) - (x0 & x1) - *(p0--)) & 0xffff; |
| 158 | t = ((x1 << 14) | (x1 >> 2)) & 0xffff; |
| 159 | x1 = (t - (x2 & ~x0) - (x3 & x0) - *(p0--)) & 0xffff; |
| 160 | t = ((x0 << 15) | (x0 >> 1)) & 0xffff; |
| 161 | x0 = (t - (x1 & ~x3) - (x2 & x3) - *(p0--)) & 0xffff; |
| 162 | |
| 163 | if (--i == 0) { |
| 164 | if (--n == 0) |
| 165 | break; |
| 166 | i = (n == 2) ? 6 : 5; |
| 167 | |
| 168 | x3 = (x3 - p1[x2 & 0x3f]) & 0xffff; |
| 169 | x2 = (x2 - p1[x1 & 0x3f]) & 0xffff; |
| 170 | x1 = (x1 - p1[x0 & 0x3f]) & 0xffff; |
| 171 | x0 = (x0 - p1[x3 & 0x3f]) & 0xffff; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | d[0] = |
| 176 | (unsigned long)(x0 & 0xffff) | ((unsigned long)(x1 & 0xffff) << 16L); |
| 177 | d[1] = |
| 178 | (unsigned long)(x2 & 0xffff) | ((unsigned long)(x3 & 0xffff) << 16L); |
| 179 | } |