/src/openssl/crypto/rc5/rc5_enc.c
Line | Count | Source (jump to first uncovered line) |
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 <stdio.h> |
11 | | #include <openssl/rc5.h> |
12 | | #include "rc5_locl.h" |
13 | | |
14 | | void RC5_32_cbc_encrypt(const unsigned char *in, unsigned char *out, |
15 | | long length, RC5_32_KEY *ks, unsigned char *iv, |
16 | | int encrypt) |
17 | 0 | { |
18 | 0 | register unsigned long tin0, tin1; |
19 | 0 | register unsigned long tout0, tout1, xor0, xor1; |
20 | 0 | register long l = length; |
21 | 0 | unsigned long tin[2]; |
22 | 0 |
|
23 | 0 | if (encrypt) { |
24 | 0 | c2l(iv, tout0); |
25 | 0 | c2l(iv, tout1); |
26 | 0 | iv -= 8; |
27 | 0 | for (l -= 8; l >= 0; l -= 8) { |
28 | 0 | c2l(in, tin0); |
29 | 0 | c2l(in, tin1); |
30 | 0 | tin0 ^= tout0; |
31 | 0 | tin1 ^= tout1; |
32 | 0 | tin[0] = tin0; |
33 | 0 | tin[1] = tin1; |
34 | 0 | RC5_32_encrypt(tin, ks); |
35 | 0 | tout0 = tin[0]; |
36 | 0 | l2c(tout0, out); |
37 | 0 | tout1 = tin[1]; |
38 | 0 | l2c(tout1, out); |
39 | 0 | } |
40 | 0 | if (l != -8) { |
41 | 0 | c2ln(in, tin0, tin1, l + 8); |
42 | 0 | tin0 ^= tout0; |
43 | 0 | tin1 ^= tout1; |
44 | 0 | tin[0] = tin0; |
45 | 0 | tin[1] = tin1; |
46 | 0 | RC5_32_encrypt(tin, ks); |
47 | 0 | tout0 = tin[0]; |
48 | 0 | l2c(tout0, out); |
49 | 0 | tout1 = tin[1]; |
50 | 0 | l2c(tout1, out); |
51 | 0 | } |
52 | 0 | l2c(tout0, iv); |
53 | 0 | l2c(tout1, iv); |
54 | 0 | } else { |
55 | 0 | c2l(iv, xor0); |
56 | 0 | c2l(iv, xor1); |
57 | 0 | iv -= 8; |
58 | 0 | for (l -= 8; l >= 0; l -= 8) { |
59 | 0 | c2l(in, tin0); |
60 | 0 | tin[0] = tin0; |
61 | 0 | c2l(in, tin1); |
62 | 0 | tin[1] = tin1; |
63 | 0 | RC5_32_decrypt(tin, ks); |
64 | 0 | tout0 = tin[0] ^ xor0; |
65 | 0 | tout1 = tin[1] ^ xor1; |
66 | 0 | l2c(tout0, out); |
67 | 0 | l2c(tout1, out); |
68 | 0 | xor0 = tin0; |
69 | 0 | xor1 = tin1; |
70 | 0 | } |
71 | 0 | if (l != -8) { |
72 | 0 | c2l(in, tin0); |
73 | 0 | tin[0] = tin0; |
74 | 0 | c2l(in, tin1); |
75 | 0 | tin[1] = tin1; |
76 | 0 | RC5_32_decrypt(tin, ks); |
77 | 0 | tout0 = tin[0] ^ xor0; |
78 | 0 | tout1 = tin[1] ^ xor1; |
79 | 0 | l2cn(tout0, tout1, out, l + 8); |
80 | 0 | xor0 = tin0; |
81 | 0 | xor1 = tin1; |
82 | 0 | } |
83 | 0 | l2c(xor0, iv); |
84 | 0 | l2c(xor1, iv); |
85 | 0 | } |
86 | 0 | tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; |
87 | 0 | tin[0] = tin[1] = 0; |
88 | 0 | } |
89 | | |
90 | | void RC5_32_encrypt(unsigned long *d, RC5_32_KEY *key) |
91 | 0 | { |
92 | 0 | RC5_32_INT a, b, *s; |
93 | 0 |
|
94 | 0 | s = key->data; |
95 | 0 |
|
96 | 0 | a = d[0] + s[0]; |
97 | 0 | b = d[1] + s[1]; |
98 | 0 | E_RC5_32(a, b, s, 2); |
99 | 0 | E_RC5_32(a, b, s, 4); |
100 | 0 | E_RC5_32(a, b, s, 6); |
101 | 0 | E_RC5_32(a, b, s, 8); |
102 | 0 | E_RC5_32(a, b, s, 10); |
103 | 0 | E_RC5_32(a, b, s, 12); |
104 | 0 | E_RC5_32(a, b, s, 14); |
105 | 0 | E_RC5_32(a, b, s, 16); |
106 | 0 | if (key->rounds == 12) { |
107 | 0 | E_RC5_32(a, b, s, 18); |
108 | 0 | E_RC5_32(a, b, s, 20); |
109 | 0 | E_RC5_32(a, b, s, 22); |
110 | 0 | E_RC5_32(a, b, s, 24); |
111 | 0 | } else if (key->rounds == 16) { |
112 | 0 | /* Do a full expansion to avoid a jump */ |
113 | 0 | E_RC5_32(a, b, s, 18); |
114 | 0 | E_RC5_32(a, b, s, 20); |
115 | 0 | E_RC5_32(a, b, s, 22); |
116 | 0 | E_RC5_32(a, b, s, 24); |
117 | 0 | E_RC5_32(a, b, s, 26); |
118 | 0 | E_RC5_32(a, b, s, 28); |
119 | 0 | E_RC5_32(a, b, s, 30); |
120 | 0 | E_RC5_32(a, b, s, 32); |
121 | 0 | } |
122 | 0 | d[0] = a; |
123 | 0 | d[1] = b; |
124 | 0 | } |
125 | | |
126 | | void RC5_32_decrypt(unsigned long *d, RC5_32_KEY *key) |
127 | 0 | { |
128 | 0 | RC5_32_INT a, b, *s; |
129 | 0 |
|
130 | 0 | s = key->data; |
131 | 0 |
|
132 | 0 | a = d[0]; |
133 | 0 | b = d[1]; |
134 | 0 | if (key->rounds == 16) { |
135 | 0 | D_RC5_32(a, b, s, 32); |
136 | 0 | D_RC5_32(a, b, s, 30); |
137 | 0 | D_RC5_32(a, b, s, 28); |
138 | 0 | D_RC5_32(a, b, s, 26); |
139 | 0 | /* Do a full expansion to avoid a jump */ |
140 | 0 | D_RC5_32(a, b, s, 24); |
141 | 0 | D_RC5_32(a, b, s, 22); |
142 | 0 | D_RC5_32(a, b, s, 20); |
143 | 0 | D_RC5_32(a, b, s, 18); |
144 | 0 | } else if (key->rounds == 12) { |
145 | 0 | D_RC5_32(a, b, s, 24); |
146 | 0 | D_RC5_32(a, b, s, 22); |
147 | 0 | D_RC5_32(a, b, s, 20); |
148 | 0 | D_RC5_32(a, b, s, 18); |
149 | 0 | } |
150 | 0 | D_RC5_32(a, b, s, 16); |
151 | 0 | D_RC5_32(a, b, s, 14); |
152 | 0 | D_RC5_32(a, b, s, 12); |
153 | 0 | D_RC5_32(a, b, s, 10); |
154 | 0 | D_RC5_32(a, b, s, 8); |
155 | 0 | D_RC5_32(a, b, s, 6); |
156 | 0 | D_RC5_32(a, b, s, 4); |
157 | 0 | D_RC5_32(a, b, s, 2); |
158 | 0 | d[0] = a - s[0]; |
159 | 0 | d[1] = b - s[1]; |
160 | 0 | } |