/src/openssl111/crypto/idea/i_cbc.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/idea.h> |
11 | | #include "idea_local.h" |
12 | | |
13 | | void IDEA_cbc_encrypt(const unsigned char *in, unsigned char *out, |
14 | | long length, IDEA_KEY_SCHEDULE *ks, unsigned char *iv, |
15 | | int encrypt) |
16 | 579 | { |
17 | 579 | register unsigned long tin0, tin1; |
18 | 579 | register unsigned long tout0, tout1, xor0, xor1; |
19 | 579 | register long l = length; |
20 | 579 | unsigned long tin[2]; |
21 | | |
22 | 579 | if (encrypt) { |
23 | 0 | n2l(iv, tout0); |
24 | 0 | n2l(iv, tout1); |
25 | 0 | iv -= 8; |
26 | 0 | for (l -= 8; l >= 0; l -= 8) { |
27 | 0 | n2l(in, tin0); |
28 | 0 | n2l(in, tin1); |
29 | 0 | tin0 ^= tout0; |
30 | 0 | tin1 ^= tout1; |
31 | 0 | tin[0] = tin0; |
32 | 0 | tin[1] = tin1; |
33 | 0 | IDEA_encrypt(tin, ks); |
34 | 0 | tout0 = tin[0]; |
35 | 0 | l2n(tout0, out); |
36 | 0 | tout1 = tin[1]; |
37 | 0 | l2n(tout1, out); |
38 | 0 | } |
39 | 0 | if (l != -8) { |
40 | 0 | n2ln(in, tin0, tin1, l + 8); |
41 | 0 | tin0 ^= tout0; |
42 | 0 | tin1 ^= tout1; |
43 | 0 | tin[0] = tin0; |
44 | 0 | tin[1] = tin1; |
45 | 0 | IDEA_encrypt(tin, ks); |
46 | 0 | tout0 = tin[0]; |
47 | 0 | l2n(tout0, out); |
48 | 0 | tout1 = tin[1]; |
49 | 0 | l2n(tout1, out); |
50 | 0 | } |
51 | 0 | l2n(tout0, iv); |
52 | 0 | l2n(tout1, iv); |
53 | 579 | } else { |
54 | 579 | n2l(iv, xor0); |
55 | 579 | n2l(iv, xor1); |
56 | 579 | iv -= 8; |
57 | 44.9k | for (l -= 8; l >= 0; l -= 8) { |
58 | 44.3k | n2l(in, tin0); |
59 | 44.3k | tin[0] = tin0; |
60 | 44.3k | n2l(in, tin1); |
61 | 44.3k | tin[1] = tin1; |
62 | 44.3k | IDEA_encrypt(tin, ks); |
63 | 44.3k | tout0 = tin[0] ^ xor0; |
64 | 44.3k | tout1 = tin[1] ^ xor1; |
65 | 44.3k | l2n(tout0, out); |
66 | 44.3k | l2n(tout1, out); |
67 | 44.3k | xor0 = tin0; |
68 | 44.3k | xor1 = tin1; |
69 | 44.3k | } |
70 | 579 | if (l != -8) { |
71 | 0 | n2l(in, tin0); |
72 | 0 | tin[0] = tin0; |
73 | 0 | n2l(in, tin1); |
74 | 0 | tin[1] = tin1; |
75 | 0 | IDEA_encrypt(tin, ks); |
76 | 0 | tout0 = tin[0] ^ xor0; |
77 | 0 | tout1 = tin[1] ^ xor1; |
78 | 0 | l2nn(tout0, tout1, out, l + 8); |
79 | 0 | xor0 = tin0; |
80 | 0 | xor1 = tin1; |
81 | 0 | } |
82 | 579 | l2n(xor0, iv); |
83 | 579 | l2n(xor1, iv); |
84 | 579 | } |
85 | 579 | tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; |
86 | 579 | tin[0] = tin[1] = 0; |
87 | 579 | } |
88 | | |
89 | | void IDEA_encrypt(unsigned long *d, IDEA_KEY_SCHEDULE *key) |
90 | 44.3k | { |
91 | 44.3k | register IDEA_INT *p; |
92 | 44.3k | register unsigned long x1, x2, x3, x4, t0, t1, ul; |
93 | | |
94 | 44.3k | x2 = d[0]; |
95 | 44.3k | x1 = (x2 >> 16); |
96 | 44.3k | x4 = d[1]; |
97 | 44.3k | x3 = (x4 >> 16); |
98 | | |
99 | 44.3k | p = &(key->data[0][0]); |
100 | | |
101 | 44.3k | E_IDEA(0); |
102 | 44.3k | E_IDEA(1); |
103 | 44.3k | E_IDEA(2); |
104 | 44.3k | E_IDEA(3); |
105 | 44.3k | E_IDEA(4); |
106 | 44.3k | E_IDEA(5); |
107 | 44.3k | E_IDEA(6); |
108 | 44.3k | E_IDEA(7); |
109 | | |
110 | 44.3k | x1 &= 0xffff; |
111 | 44.3k | idea_mul(x1, x1, *p, ul); |
112 | 44.3k | p++; |
113 | | |
114 | 44.3k | t0 = x3 + *(p++); |
115 | 44.3k | t1 = x2 + *(p++); |
116 | | |
117 | 44.3k | x4 &= 0xffff; |
118 | 44.3k | idea_mul(x4, x4, *p, ul); |
119 | | |
120 | 44.3k | d[0] = (t0 & 0xffff) | ((x1 & 0xffff) << 16); |
121 | 44.3k | d[1] = (x4 & 0xffff) | ((t1 & 0xffff) << 16); |
122 | 44.3k | } |