GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/aes/aes_ige.c Lines: 84 84 100.0 %
Date: 2017-11-07 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
252
	OPENSSL_assert((length % AES_BLOCK_SIZE) == 0);
87
88
126
	len = length / AES_BLOCK_SIZE;
89
90
126
	if (AES_ENCRYPT == enc) {
91
66
		if (in != out && (UNALIGNED_MEMOPS_ARE_FAST ||
92
		    ((size_t)in|(size_t)out|(size_t)ivec) %
93
		    sizeof(long) == 0)) {
94
60
			aes_block_t *ivp = (aes_block_t *)ivec;
95
60
			aes_block_t *iv2p = (aes_block_t *)(ivec + AES_BLOCK_SIZE);
96
97
31152
			while (len) {
98
15516
				aes_block_t *inp = (aes_block_t *)in;
99
15516
				aes_block_t *outp = (aes_block_t *)out;
100
101
93096
				for (n = 0; n < N_WORDS; ++n)
102
31032
					outp->data[n] = inp->data[n] ^ ivp->data[n];
103
15516
				AES_encrypt((unsigned char *)outp->data, (unsigned char *)outp->data, key);
104
93096
				for (n = 0; n < N_WORDS; ++n)
105
31032
					outp->data[n] ^= iv2p->data[n];
106
				ivp = outp;
107
				iv2p = inp;
108
15516
				--len;
109
15516
				in += AES_BLOCK_SIZE;
110
15516
				out += AES_BLOCK_SIZE;
111
			}
112
60
			memcpy(ivec, ivp->data, AES_BLOCK_SIZE);
113
60
			memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE);
114
60
		} else {
115
6
			aes_block_t tmp, tmp2;
116
6
			aes_block_t iv;
117
6
			aes_block_t iv2;
118
119
6
			load_block(iv, ivec);
120
6
			load_block(iv2, ivec + AES_BLOCK_SIZE);
121
122
36
			while (len) {
123
12
				load_block(tmp, in);
124
72
				for (n = 0; n < N_WORDS; ++n)
125
24
					tmp2.data[n] = tmp.data[n] ^ iv.data[n];
126
12
				AES_encrypt((unsigned char *)tmp2.data,
127
				    (unsigned char *)tmp2.data, key);
128
72
				for (n = 0; n < N_WORDS; ++n)
129
24
					tmp2.data[n] ^= iv2.data[n];
130
12
				store_block(out, tmp2);
131
12
				iv = tmp2;
132
12
				iv2 = tmp;
133
12
				--len;
134
12
				in += AES_BLOCK_SIZE;
135
12
				out += AES_BLOCK_SIZE;
136
			}
137
6
			memcpy(ivec, iv.data, AES_BLOCK_SIZE);
138
6
			memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
139
6
		}
140
	} else {
141
60
		if (in != out && (UNALIGNED_MEMOPS_ARE_FAST ||
142
		    ((size_t)in|(size_t)out|(size_t)ivec) %
143
		    sizeof(long) == 0)) {
144
54
			aes_block_t *ivp = (aes_block_t *)ivec;
145
54
			aes_block_t *iv2p = (aes_block_t *)(ivec + AES_BLOCK_SIZE);
146
147
31140
			while (len) {
148
15516
				aes_block_t tmp;
149
15516
				aes_block_t *inp = (aes_block_t *)in;
150
15516
				aes_block_t *outp = (aes_block_t *)out;
151
152
93096
				for (n = 0; n < N_WORDS; ++n)
153
31032
					tmp.data[n] = inp->data[n] ^ iv2p->data[n];
154
31032
				AES_decrypt((unsigned char *)tmp.data,
155
15516
				    (unsigned char *)outp->data, key);
156
93096
				for (n = 0; n < N_WORDS; ++n)
157
31032
					outp->data[n] ^= ivp->data[n];
158
				ivp = inp;
159
				iv2p = outp;
160
15516
				--len;
161
15516
				in += AES_BLOCK_SIZE;
162
15516
				out += AES_BLOCK_SIZE;
163
15516
			}
164
54
			memcpy(ivec, ivp->data, AES_BLOCK_SIZE);
165
54
			memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE);
166
54
		} else {
167
6
			aes_block_t tmp, tmp2;
168
6
			aes_block_t iv;
169
6
			aes_block_t iv2;
170
171
6
			load_block(iv, ivec);
172
6
			load_block(iv2, ivec + AES_BLOCK_SIZE);
173
174
36
			while (len) {
175
12
				load_block(tmp, in);
176
12
				tmp2 = tmp;
177
72
				for (n = 0; n < N_WORDS; ++n)
178
24
					tmp.data[n] ^= iv2.data[n];
179
12
				AES_decrypt((unsigned char *)tmp.data,
180
				    (unsigned char *)tmp.data, key);
181
72
				for (n = 0; n < N_WORDS; ++n)
182
24
					tmp.data[n] ^= iv.data[n];
183
12
				store_block(out, tmp);
184
12
				iv = tmp2;
185
12
				iv2 = tmp;
186
12
				--len;
187
12
				in += AES_BLOCK_SIZE;
188
12
				out += AES_BLOCK_SIZE;
189
			}
190
6
			memcpy(ivec, iv.data, AES_BLOCK_SIZE);
191
6
			memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
192
6
		}
193
	}
194
126
}