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