2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 * BF low level APIs are deprecated for public use, but still ok for internal
14 #include "internal/deprecated.h"
16 #include <openssl/blowfish.h>
20 * Blowfish as implemented from 'Blowfish: Springer-Verlag paper' (From
21 * LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, CAMBRIDGE
22 * SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
25 #if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
26 # error If you set BF_ROUNDS to some value other than 16 or 20, you will have \
30 void BF_encrypt(BF_LONG *data, const BF_KEY *key)
32 register BF_LONG l, r;
33 register const BF_LONG *p, *s;
41 BF_ENC(r, l, s, p[1]);
42 BF_ENC(l, r, s, p[2]);
43 BF_ENC(r, l, s, p[3]);
44 BF_ENC(l, r, s, p[4]);
45 BF_ENC(r, l, s, p[5]);
46 BF_ENC(l, r, s, p[6]);
47 BF_ENC(r, l, s, p[7]);
48 BF_ENC(l, r, s, p[8]);
49 BF_ENC(r, l, s, p[9]);
50 BF_ENC(l, r, s, p[10]);
51 BF_ENC(r, l, s, p[11]);
52 BF_ENC(l, r, s, p[12]);
53 BF_ENC(r, l, s, p[13]);
54 BF_ENC(l, r, s, p[14]);
55 BF_ENC(r, l, s, p[15]);
56 BF_ENC(l, r, s, p[16]);
58 BF_ENC(r, l, s, p[17]);
59 BF_ENC(l, r, s, p[18]);
60 BF_ENC(r, l, s, p[19]);
61 BF_ENC(l, r, s, p[20]);
63 r ^= p[BF_ROUNDS + 1];
65 data[1] = l & 0xffffffffU;
66 data[0] = r & 0xffffffffU;
69 void BF_decrypt(BF_LONG *data, const BF_KEY *key)
71 register BF_LONG l, r;
72 register const BF_LONG *p, *s;
79 l ^= p[BF_ROUNDS + 1];
81 BF_ENC(r, l, s, p[20]);
82 BF_ENC(l, r, s, p[19]);
83 BF_ENC(r, l, s, p[18]);
84 BF_ENC(l, r, s, p[17]);
86 BF_ENC(r, l, s, p[16]);
87 BF_ENC(l, r, s, p[15]);
88 BF_ENC(r, l, s, p[14]);
89 BF_ENC(l, r, s, p[13]);
90 BF_ENC(r, l, s, p[12]);
91 BF_ENC(l, r, s, p[11]);
92 BF_ENC(r, l, s, p[10]);
93 BF_ENC(l, r, s, p[9]);
94 BF_ENC(r, l, s, p[8]);
95 BF_ENC(l, r, s, p[7]);
96 BF_ENC(r, l, s, p[6]);
97 BF_ENC(l, r, s, p[5]);
98 BF_ENC(r, l, s, p[4]);
99 BF_ENC(l, r, s, p[3]);
100 BF_ENC(r, l, s, p[2]);
101 BF_ENC(l, r, s, p[1]);
104 data[1] = l & 0xffffffffU;
105 data[0] = r & 0xffffffffU;
108 void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
109 const BF_KEY *schedule, unsigned char *ivec, int encrypt)
111 register BF_LONG tin0, tin1;
112 register BF_LONG tout0, tout1, xor0, xor1;
113 register long l = length;
120 for (l -= 8; l >= 0; l -= 8) {
127 BF_encrypt(tin, schedule);
134 n2ln(in, tin0, tin1, l + 8);
139 BF_encrypt(tin, schedule);
151 for (l -= 8; l >= 0; l -= 8) {
156 BF_decrypt(tin, schedule);
157 tout0 = tin[0] ^ xor0;
158 tout1 = tin[1] ^ xor1;
169 BF_decrypt(tin, schedule);
170 tout0 = tin[0] ^ xor0;
171 tout1 = tin[1] ^ xor1;
172 l2nn(tout0, tout1, out, l + 8);
179 tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;