GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/aes/aes_ige.c Lines: 84 84 100.0 %
Date: 2017-11-13 Branches: 31 32 96.9 %

Line Branch Exec Source
1
/* $OpenBSD: aes_ige.c,v 1.7 2015/02/10 09:46:30 miod Exp $ */
2
/* ====================================================================
3
 * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in
14
 *    the documentation and/or other materials provided with the
15
 *    distribution.
16
 *
17
 * 3. All advertising materials mentioning features or use of this
18
 *    software must display the following acknowledgment:
19
 *    "This product includes software developed by the OpenSSL Project
20
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21
 *
22
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23
 *    endorse or promote products derived from this software without
24
 *    prior written permission. For written permission, please contact
25
 *    openssl-core@openssl.org.
26
 *
27
 * 5. Products derived from this software may not be called "OpenSSL"
28
 *    nor may "OpenSSL" appear in their names without prior written
29
 *    permission of the OpenSSL Project.
30
 *
31
 * 6. Redistributions of any form whatsoever must retain the following
32
 *    acknowledgment:
33
 *    "This product includes software developed by the OpenSSL Project
34
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35
 *
36
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47
 * OF THE POSSIBILITY OF SUCH DAMAGE.
48
 * ====================================================================
49
 *
50
 */
51
52
#include <openssl/aes.h>
53
#include <openssl/crypto.h>
54
55
#include "aes_locl.h"
56
57
#define N_WORDS (AES_BLOCK_SIZE / sizeof(unsigned long))
58
typedef struct {
59
	unsigned long data[N_WORDS];
60
} aes_block_t;
61
62
/* XXX: probably some better way to do this */
63
#if defined(__i386__) || defined(__x86_64__)
64
#define UNALIGNED_MEMOPS_ARE_FAST 1
65
#else
66
#define UNALIGNED_MEMOPS_ARE_FAST 0
67
#endif
68
69
#if UNALIGNED_MEMOPS_ARE_FAST
70
#define load_block(d, s)        (d) = *(const aes_block_t *)(s)
71
#define store_block(d, s)       *(aes_block_t *)(d) = (s)
72
#else
73
#define load_block(d, s)        memcpy((d).data, (s), AES_BLOCK_SIZE)
74
#define store_block(d, s)       memcpy((d), (s).data, AES_BLOCK_SIZE)
75
#endif
76
77
/* N.B. The IV for this mode is _twice_ the block size */
78
79
void
80
AES_ige_encrypt(const unsigned char *in, unsigned char *out, size_t length,
81
    const AES_KEY *key, unsigned char *ivec, const int enc)
82
{
83
	size_t n;
84
	size_t len;
85
86
126
	OPENSSL_assert((length % AES_BLOCK_SIZE) == 0);
87
88
63
	len = length / AES_BLOCK_SIZE;
89
90
63
	if (AES_ENCRYPT == enc) {
91
33
		if (in != out && (UNALIGNED_MEMOPS_ARE_FAST ||
92
		    ((size_t)in|(size_t)out|(size_t)ivec) %
93
		    sizeof(long) == 0)) {
94
30
			aes_block_t *ivp = (aes_block_t *)ivec;
95
30
			aes_block_t *iv2p = (aes_block_t *)(ivec + AES_BLOCK_SIZE);
96
97
15576
			while (len) {
98
7758
				aes_block_t *inp = (aes_block_t *)in;
99
7758
				aes_block_t *outp = (aes_block_t *)out;
100
101
46548
				for (n = 0; n < N_WORDS; ++n)
102
15516
					outp->data[n] = inp->data[n] ^ ivp->data[n];
103
7758
				AES_encrypt((unsigned char *)outp->data, (unsigned char *)outp->data, key);
104
46548
				for (n = 0; n < N_WORDS; ++n)
105
15516
					outp->data[n] ^= iv2p->data[n];
106
				ivp = outp;
107
				iv2p = inp;
108
7758
				--len;
109
7758
				in += AES_BLOCK_SIZE;
110
7758
				out += AES_BLOCK_SIZE;
111
			}
112
30
			memcpy(ivec, ivp->data, AES_BLOCK_SIZE);
113
30
			memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE);
114
30
		} else {
115
3
			aes_block_t tmp, tmp2;
116
3
			aes_block_t iv;
117
3
			aes_block_t iv2;
118
119
3
			load_block(iv, ivec);
120
3
			load_block(iv2, ivec + AES_BLOCK_SIZE);
121
122
18
			while (len) {
123
6
				load_block(tmp, in);
124
36
				for (n = 0; n < N_WORDS; ++n)
125
12
					tmp2.data[n] = tmp.data[n] ^ iv.data[n];
126
6
				AES_encrypt((unsigned char *)tmp2.data,
127
				    (unsigned char *)tmp2.data, key);
128
36
				for (n = 0; n < N_WORDS; ++n)
129
12
					tmp2.data[n] ^= iv2.data[n];
130
6
				store_block(out, tmp2);
131
6
				iv = tmp2;
132
6
				iv2 = tmp;
133
6
				--len;
134
6
				in += AES_BLOCK_SIZE;
135
6
				out += AES_BLOCK_SIZE;
136
			}
137
3
			memcpy(ivec, iv.data, AES_BLOCK_SIZE);
138
3
			memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
139
3
		}
140
	} else {
141
30
		if (in != out && (UNALIGNED_MEMOPS_ARE_FAST ||
142
		    ((size_t)in|(size_t)out|(size_t)ivec) %
143
		    sizeof(long) == 0)) {
144
27
			aes_block_t *ivp = (aes_block_t *)ivec;
145
27
			aes_block_t *iv2p = (aes_block_t *)(ivec + AES_BLOCK_SIZE);
146
147
15570
			while (len) {
148
7758
				aes_block_t tmp;
149
7758
				aes_block_t *inp = (aes_block_t *)in;
150
7758
				aes_block_t *outp = (aes_block_t *)out;
151
152
46548
				for (n = 0; n < N_WORDS; ++n)
153
15516
					tmp.data[n] = inp->data[n] ^ iv2p->data[n];
154
15516
				AES_decrypt((unsigned char *)tmp.data,
155
7758
				    (unsigned char *)outp->data, key);
156
46548
				for (n = 0; n < N_WORDS; ++n)
157
15516
					outp->data[n] ^= ivp->data[n];
158
				ivp = inp;
159
				iv2p = outp;
160
7758
				--len;
161
7758
				in += AES_BLOCK_SIZE;
162
7758
				out += AES_BLOCK_SIZE;
163
7758
			}
164
27
			memcpy(ivec, ivp->data, AES_BLOCK_SIZE);
165
27
			memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE);
166
27
		} else {
167
3
			aes_block_t tmp, tmp2;
168
3
			aes_block_t iv;
169
3
			aes_block_t iv2;
170
171
3
			load_block(iv, ivec);
172
3
			load_block(iv2, ivec + AES_BLOCK_SIZE);
173
174
18
			while (len) {
175
6
				load_block(tmp, in);
176
6
				tmp2 = tmp;
177
36
				for (n = 0; n < N_WORDS; ++n)
178
12
					tmp.data[n] ^= iv2.data[n];
179
6
				AES_decrypt((unsigned char *)tmp.data,
180
				    (unsigned char *)tmp.data, key);
181
36
				for (n = 0; n < N_WORDS; ++n)
182
12
					tmp.data[n] ^= iv.data[n];
183
6
				store_block(out, tmp);
184
6
				iv = tmp2;
185
6
				iv2 = tmp;
186
6
				--len;
187
6
				in += AES_BLOCK_SIZE;
188
6
				out += AES_BLOCK_SIZE;
189
			}
190
3
			memcpy(ivec, iv.data, AES_BLOCK_SIZE);
191
3
			memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
192
3
		}
193
	}
194
63
}