/src/openssl/crypto/bf/bf_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 <openssl/blowfish.h> |
11 | | #include "bf_locl.h" |
12 | | |
13 | | /* |
14 | | * Blowfish as implemented from 'Blowfish: Springer-Verlag paper' (From |
15 | | * LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, CAMBRIDGE |
16 | | * SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993) |
17 | | */ |
18 | | |
19 | | #if (BF_ROUNDS != 16) && (BF_ROUNDS != 20) |
20 | | # error If you set BF_ROUNDS to some value other than 16 or 20, you will have \ |
21 | | to modify the code. |
22 | | #endif |
23 | | |
24 | | void BF_encrypt(BF_LONG *data, const BF_KEY *key) |
25 | 0 | { |
26 | 0 | register BF_LONG l, r; |
27 | 0 | register const BF_LONG *p, *s; |
28 | 0 |
|
29 | 0 | p = key->P; |
30 | 0 | s = &(key->S[0]); |
31 | 0 | l = data[0]; |
32 | 0 | r = data[1]; |
33 | 0 |
|
34 | 0 | l ^= p[0]; |
35 | 0 | BF_ENC(r, l, s, p[1]); |
36 | 0 | BF_ENC(l, r, s, p[2]); |
37 | 0 | BF_ENC(r, l, s, p[3]); |
38 | 0 | BF_ENC(l, r, s, p[4]); |
39 | 0 | BF_ENC(r, l, s, p[5]); |
40 | 0 | BF_ENC(l, r, s, p[6]); |
41 | 0 | BF_ENC(r, l, s, p[7]); |
42 | 0 | BF_ENC(l, r, s, p[8]); |
43 | 0 | BF_ENC(r, l, s, p[9]); |
44 | 0 | BF_ENC(l, r, s, p[10]); |
45 | 0 | BF_ENC(r, l, s, p[11]); |
46 | 0 | BF_ENC(l, r, s, p[12]); |
47 | 0 | BF_ENC(r, l, s, p[13]); |
48 | 0 | BF_ENC(l, r, s, p[14]); |
49 | 0 | BF_ENC(r, l, s, p[15]); |
50 | 0 | BF_ENC(l, r, s, p[16]); |
51 | | # if BF_ROUNDS == 20 |
52 | | BF_ENC(r, l, s, p[17]); |
53 | | BF_ENC(l, r, s, p[18]); |
54 | | BF_ENC(r, l, s, p[19]); |
55 | | BF_ENC(l, r, s, p[20]); |
56 | | # endif |
57 | 0 | r ^= p[BF_ROUNDS + 1]; |
58 | 0 |
|
59 | 0 | data[1] = l & 0xffffffffU; |
60 | 0 | data[0] = r & 0xffffffffU; |
61 | 0 | } |
62 | | |
63 | | void BF_decrypt(BF_LONG *data, const BF_KEY *key) |
64 | 0 | { |
65 | 0 | register BF_LONG l, r; |
66 | 0 | register const BF_LONG *p, *s; |
67 | 0 |
|
68 | 0 | p = key->P; |
69 | 0 | s = &(key->S[0]); |
70 | 0 | l = data[0]; |
71 | 0 | r = data[1]; |
72 | 0 |
|
73 | 0 | l ^= p[BF_ROUNDS + 1]; |
74 | | # if BF_ROUNDS == 20 |
75 | | BF_ENC(r, l, s, p[20]); |
76 | | BF_ENC(l, r, s, p[19]); |
77 | | BF_ENC(r, l, s, p[18]); |
78 | | BF_ENC(l, r, s, p[17]); |
79 | | # endif |
80 | 0 | BF_ENC(r, l, s, p[16]); |
81 | 0 | BF_ENC(l, r, s, p[15]); |
82 | 0 | BF_ENC(r, l, s, p[14]); |
83 | 0 | BF_ENC(l, r, s, p[13]); |
84 | 0 | BF_ENC(r, l, s, p[12]); |
85 | 0 | BF_ENC(l, r, s, p[11]); |
86 | 0 | BF_ENC(r, l, s, p[10]); |
87 | 0 | BF_ENC(l, r, s, p[9]); |
88 | 0 | BF_ENC(r, l, s, p[8]); |
89 | 0 | BF_ENC(l, r, s, p[7]); |
90 | 0 | BF_ENC(r, l, s, p[6]); |
91 | 0 | BF_ENC(l, r, s, p[5]); |
92 | 0 | BF_ENC(r, l, s, p[4]); |
93 | 0 | BF_ENC(l, r, s, p[3]); |
94 | 0 | BF_ENC(r, l, s, p[2]); |
95 | 0 | BF_ENC(l, r, s, p[1]); |
96 | 0 | r ^= p[0]; |
97 | 0 |
|
98 | 0 | data[1] = l & 0xffffffffU; |
99 | 0 | data[0] = r & 0xffffffffU; |
100 | 0 | } |
101 | | |
102 | | void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, |
103 | | const BF_KEY *schedule, unsigned char *ivec, int encrypt) |
104 | 0 | { |
105 | 0 | register BF_LONG tin0, tin1; |
106 | 0 | register BF_LONG tout0, tout1, xor0, xor1; |
107 | 0 | register long l = length; |
108 | 0 | BF_LONG tin[2]; |
109 | 0 |
|
110 | 0 | if (encrypt) { |
111 | 0 | n2l(ivec, tout0); |
112 | 0 | n2l(ivec, tout1); |
113 | 0 | ivec -= 8; |
114 | 0 | for (l -= 8; l >= 0; l -= 8) { |
115 | 0 | n2l(in, tin0); |
116 | 0 | n2l(in, tin1); |
117 | 0 | tin0 ^= tout0; |
118 | 0 | tin1 ^= tout1; |
119 | 0 | tin[0] = tin0; |
120 | 0 | tin[1] = tin1; |
121 | 0 | BF_encrypt(tin, schedule); |
122 | 0 | tout0 = tin[0]; |
123 | 0 | tout1 = tin[1]; |
124 | 0 | l2n(tout0, out); |
125 | 0 | l2n(tout1, out); |
126 | 0 | } |
127 | 0 | if (l != -8) { |
128 | 0 | n2ln(in, tin0, tin1, l + 8); |
129 | 0 | tin0 ^= tout0; |
130 | 0 | tin1 ^= tout1; |
131 | 0 | tin[0] = tin0; |
132 | 0 | tin[1] = tin1; |
133 | 0 | BF_encrypt(tin, schedule); |
134 | 0 | tout0 = tin[0]; |
135 | 0 | tout1 = tin[1]; |
136 | 0 | l2n(tout0, out); |
137 | 0 | l2n(tout1, out); |
138 | 0 | } |
139 | 0 | l2n(tout0, ivec); |
140 | 0 | l2n(tout1, ivec); |
141 | 0 | } else { |
142 | 0 | n2l(ivec, xor0); |
143 | 0 | n2l(ivec, xor1); |
144 | 0 | ivec -= 8; |
145 | 0 | for (l -= 8; l >= 0; l -= 8) { |
146 | 0 | n2l(in, tin0); |
147 | 0 | n2l(in, tin1); |
148 | 0 | tin[0] = tin0; |
149 | 0 | tin[1] = tin1; |
150 | 0 | BF_decrypt(tin, schedule); |
151 | 0 | tout0 = tin[0] ^ xor0; |
152 | 0 | tout1 = tin[1] ^ xor1; |
153 | 0 | l2n(tout0, out); |
154 | 0 | l2n(tout1, out); |
155 | 0 | xor0 = tin0; |
156 | 0 | xor1 = tin1; |
157 | 0 | } |
158 | 0 | if (l != -8) { |
159 | 0 | n2l(in, tin0); |
160 | 0 | n2l(in, tin1); |
161 | 0 | tin[0] = tin0; |
162 | 0 | tin[1] = tin1; |
163 | 0 | BF_decrypt(tin, schedule); |
164 | 0 | tout0 = tin[0] ^ xor0; |
165 | 0 | tout1 = tin[1] ^ xor1; |
166 | 0 | l2nn(tout0, tout1, out, l + 8); |
167 | 0 | xor0 = tin0; |
168 | 0 | xor1 = tin1; |
169 | 0 | } |
170 | 0 | l2n(xor0, ivec); |
171 | 0 | l2n(xor1, ivec); |
172 | 0 | } |
173 | 0 | tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; |
174 | 0 | tin[0] = tin[1] = 0; |
175 | 0 | } |