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/cast.h>
|
---|
11 | #include "cast_local.h"
|
---|
12 |
|
---|
13 | void CAST_encrypt(CAST_LONG *data, const CAST_KEY *key)
|
---|
14 | {
|
---|
15 | CAST_LONG l, r, t;
|
---|
16 | const CAST_LONG *k;
|
---|
17 |
|
---|
18 | k = &(key->data[0]);
|
---|
19 | l = data[0];
|
---|
20 | r = data[1];
|
---|
21 |
|
---|
22 | E_CAST(0, k, l, r, +, ^, -);
|
---|
23 | E_CAST(1, k, r, l, ^, -, +);
|
---|
24 | E_CAST(2, k, l, r, -, +, ^);
|
---|
25 | E_CAST(3, k, r, l, +, ^, -);
|
---|
26 | E_CAST(4, k, l, r, ^, -, +);
|
---|
27 | E_CAST(5, k, r, l, -, +, ^);
|
---|
28 | E_CAST(6, k, l, r, +, ^, -);
|
---|
29 | E_CAST(7, k, r, l, ^, -, +);
|
---|
30 | E_CAST(8, k, l, r, -, +, ^);
|
---|
31 | E_CAST(9, k, r, l, +, ^, -);
|
---|
32 | E_CAST(10, k, l, r, ^, -, +);
|
---|
33 | E_CAST(11, k, r, l, -, +, ^);
|
---|
34 | if (!key->short_key) {
|
---|
35 | E_CAST(12, k, l, r, +, ^, -);
|
---|
36 | E_CAST(13, k, r, l, ^, -, +);
|
---|
37 | E_CAST(14, k, l, r, -, +, ^);
|
---|
38 | E_CAST(15, k, r, l, +, ^, -);
|
---|
39 | }
|
---|
40 |
|
---|
41 | data[1] = l & 0xffffffffL;
|
---|
42 | data[0] = r & 0xffffffffL;
|
---|
43 | }
|
---|
44 |
|
---|
45 | void CAST_decrypt(CAST_LONG *data, const CAST_KEY *key)
|
---|
46 | {
|
---|
47 | CAST_LONG l, r, t;
|
---|
48 | const CAST_LONG *k;
|
---|
49 |
|
---|
50 | k = &(key->data[0]);
|
---|
51 | l = data[0];
|
---|
52 | r = data[1];
|
---|
53 |
|
---|
54 | if (!key->short_key) {
|
---|
55 | E_CAST(15, k, l, r, +, ^, -);
|
---|
56 | E_CAST(14, k, r, l, -, +, ^);
|
---|
57 | E_CAST(13, k, l, r, ^, -, +);
|
---|
58 | E_CAST(12, k, r, l, +, ^, -);
|
---|
59 | }
|
---|
60 | E_CAST(11, k, l, r, -, +, ^);
|
---|
61 | E_CAST(10, k, r, l, ^, -, +);
|
---|
62 | E_CAST(9, k, l, r, +, ^, -);
|
---|
63 | E_CAST(8, k, r, l, -, +, ^);
|
---|
64 | E_CAST(7, k, l, r, ^, -, +);
|
---|
65 | E_CAST(6, k, r, l, +, ^, -);
|
---|
66 | E_CAST(5, k, l, r, -, +, ^);
|
---|
67 | E_CAST(4, k, r, l, ^, -, +);
|
---|
68 | E_CAST(3, k, l, r, +, ^, -);
|
---|
69 | E_CAST(2, k, r, l, -, +, ^);
|
---|
70 | E_CAST(1, k, l, r, ^, -, +);
|
---|
71 | E_CAST(0, k, r, l, +, ^, -);
|
---|
72 |
|
---|
73 | data[1] = l & 0xffffffffL;
|
---|
74 | data[0] = r & 0xffffffffL;
|
---|
75 | }
|
---|
76 |
|
---|
77 | void CAST_cbc_encrypt(const unsigned char *in, unsigned char *out,
|
---|
78 | long length, const CAST_KEY *ks, unsigned char *iv,
|
---|
79 | int enc)
|
---|
80 | {
|
---|
81 | register CAST_LONG tin0, tin1;
|
---|
82 | register CAST_LONG tout0, tout1, xor0, xor1;
|
---|
83 | register long l = length;
|
---|
84 | CAST_LONG tin[2];
|
---|
85 |
|
---|
86 | if (enc) {
|
---|
87 | n2l(iv, tout0);
|
---|
88 | n2l(iv, tout1);
|
---|
89 | iv -= 8;
|
---|
90 | for (l -= 8; l >= 0; l -= 8) {
|
---|
91 | n2l(in, tin0);
|
---|
92 | n2l(in, tin1);
|
---|
93 | tin0 ^= tout0;
|
---|
94 | tin1 ^= tout1;
|
---|
95 | tin[0] = tin0;
|
---|
96 | tin[1] = tin1;
|
---|
97 | CAST_encrypt(tin, ks);
|
---|
98 | tout0 = tin[0];
|
---|
99 | tout1 = tin[1];
|
---|
100 | l2n(tout0, out);
|
---|
101 | l2n(tout1, out);
|
---|
102 | }
|
---|
103 | if (l != -8) {
|
---|
104 | n2ln(in, tin0, tin1, l + 8);
|
---|
105 | tin0 ^= tout0;
|
---|
106 | tin1 ^= tout1;
|
---|
107 | tin[0] = tin0;
|
---|
108 | tin[1] = tin1;
|
---|
109 | CAST_encrypt(tin, ks);
|
---|
110 | tout0 = tin[0];
|
---|
111 | tout1 = tin[1];
|
---|
112 | l2n(tout0, out);
|
---|
113 | l2n(tout1, out);
|
---|
114 | }
|
---|
115 | l2n(tout0, iv);
|
---|
116 | l2n(tout1, iv);
|
---|
117 | } else {
|
---|
118 | n2l(iv, xor0);
|
---|
119 | n2l(iv, xor1);
|
---|
120 | iv -= 8;
|
---|
121 | for (l -= 8; l >= 0; l -= 8) {
|
---|
122 | n2l(in, tin0);
|
---|
123 | n2l(in, tin1);
|
---|
124 | tin[0] = tin0;
|
---|
125 | tin[1] = tin1;
|
---|
126 | CAST_decrypt(tin, ks);
|
---|
127 | tout0 = tin[0] ^ xor0;
|
---|
128 | tout1 = tin[1] ^ xor1;
|
---|
129 | l2n(tout0, out);
|
---|
130 | l2n(tout1, out);
|
---|
131 | xor0 = tin0;
|
---|
132 | xor1 = tin1;
|
---|
133 | }
|
---|
134 | if (l != -8) {
|
---|
135 | n2l(in, tin0);
|
---|
136 | n2l(in, tin1);
|
---|
137 | tin[0] = tin0;
|
---|
138 | tin[1] = tin1;
|
---|
139 | CAST_decrypt(tin, ks);
|
---|
140 | tout0 = tin[0] ^ xor0;
|
---|
141 | tout1 = tin[1] ^ xor1;
|
---|
142 | l2nn(tout0, tout1, out, l + 8);
|
---|
143 | xor0 = tin0;
|
---|
144 | xor1 = tin1;
|
---|
145 | }
|
---|
146 | l2n(xor0, iv);
|
---|
147 | l2n(xor1, iv);
|
---|
148 | }
|
---|
149 | tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
|
---|
150 | tin[0] = tin[1] = 0;
|
---|
151 | }
|
---|