Copyright consolidation 09/10
[openssl.git] / crypto / bf / bf_cbc.c
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 void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
14                     const BF_KEY *schedule, unsigned char *ivec, int encrypt)
15 {
16     register BF_LONG tin0, tin1;
17     register BF_LONG tout0, tout1, xor0, xor1;
18     register long l = length;
19     BF_LONG tin[2];
20
21     if (encrypt) {
22         n2l(ivec, tout0);
23         n2l(ivec, tout1);
24         ivec -= 8;
25         for (l -= 8; l >= 0; l -= 8) {
26             n2l(in, tin0);
27             n2l(in, tin1);
28             tin0 ^= tout0;
29             tin1 ^= tout1;
30             tin[0] = tin0;
31             tin[1] = tin1;
32             BF_encrypt(tin, schedule);
33             tout0 = tin[0];
34             tout1 = tin[1];
35             l2n(tout0, out);
36             l2n(tout1, out);
37         }
38         if (l != -8) {
39             n2ln(in, tin0, tin1, l + 8);
40             tin0 ^= tout0;
41             tin1 ^= tout1;
42             tin[0] = tin0;
43             tin[1] = tin1;
44             BF_encrypt(tin, schedule);
45             tout0 = tin[0];
46             tout1 = tin[1];
47             l2n(tout0, out);
48             l2n(tout1, out);
49         }
50         l2n(tout0, ivec);
51         l2n(tout1, ivec);
52     } else {
53         n2l(ivec, xor0);
54         n2l(ivec, xor1);
55         ivec -= 8;
56         for (l -= 8; l >= 0; l -= 8) {
57             n2l(in, tin0);
58             n2l(in, tin1);
59             tin[0] = tin0;
60             tin[1] = tin1;
61             BF_decrypt(tin, schedule);
62             tout0 = tin[0] ^ xor0;
63             tout1 = tin[1] ^ xor1;
64             l2n(tout0, out);
65             l2n(tout1, out);
66             xor0 = tin0;
67             xor1 = tin1;
68         }
69         if (l != -8) {
70             n2l(in, tin0);
71             n2l(in, tin1);
72             tin[0] = tin0;
73             tin[1] = tin1;
74             BF_decrypt(tin, schedule);
75             tout0 = tin[0] ^ xor0;
76             tout1 = tin[1] ^ xor1;
77             l2nn(tout0, tout1, out, l + 8);
78             xor0 = tin0;
79             xor1 = tin1;
80         }
81         l2n(xor0, ivec);
82         l2n(xor1, ivec);
83     }
84     tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
85     tin[0] = tin[1] = 0;
86 }