xref: /openbsd/lib/libcrypto/modes/ctr128.c (revision 42077c12)
1*42077c12Sbcook /* $OpenBSD: ctr128.c,v 1.7 2017/08/13 17:46:24 bcook Exp $ */
2f1535dc8Sdjm /* ====================================================================
3f1535dc8Sdjm  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
4f1535dc8Sdjm  *
5f1535dc8Sdjm  * Redistribution and use in source and binary forms, with or without
6f1535dc8Sdjm  * modification, are permitted provided that the following conditions
7f1535dc8Sdjm  * are met:
8f1535dc8Sdjm  *
9f1535dc8Sdjm  * 1. Redistributions of source code must retain the above copyright
10f1535dc8Sdjm  *    notice, this list of conditions and the following disclaimer.
11f1535dc8Sdjm  *
12f1535dc8Sdjm  * 2. Redistributions in binary form must reproduce the above copyright
13f1535dc8Sdjm  *    notice, this list of conditions and the following disclaimer in
14f1535dc8Sdjm  *    the documentation and/or other materials provided with the
15f1535dc8Sdjm  *    distribution.
16f1535dc8Sdjm  *
17f1535dc8Sdjm  * 3. All advertising materials mentioning features or use of this
18f1535dc8Sdjm  *    software must display the following acknowledgment:
19f1535dc8Sdjm  *    "This product includes software developed by the OpenSSL Project
20f1535dc8Sdjm  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21f1535dc8Sdjm  *
22f1535dc8Sdjm  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23f1535dc8Sdjm  *    endorse or promote products derived from this software without
24f1535dc8Sdjm  *    prior written permission. For written permission, please contact
25f1535dc8Sdjm  *    openssl-core@openssl.org.
26f1535dc8Sdjm  *
27f1535dc8Sdjm  * 5. Products derived from this software may not be called "OpenSSL"
28f1535dc8Sdjm  *    nor may "OpenSSL" appear in their names without prior written
29f1535dc8Sdjm  *    permission of the OpenSSL Project.
30f1535dc8Sdjm  *
31f1535dc8Sdjm  * 6. Redistributions of any form whatsoever must retain the following
32f1535dc8Sdjm  *    acknowledgment:
33f1535dc8Sdjm  *    "This product includes software developed by the OpenSSL Project
34f1535dc8Sdjm  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35f1535dc8Sdjm  *
36f1535dc8Sdjm  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37f1535dc8Sdjm  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38f1535dc8Sdjm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39f1535dc8Sdjm  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40f1535dc8Sdjm  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41f1535dc8Sdjm  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42f1535dc8Sdjm  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43f1535dc8Sdjm  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44f1535dc8Sdjm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45f1535dc8Sdjm  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46f1535dc8Sdjm  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47f1535dc8Sdjm  * OF THE POSSIBILITY OF SUCH DAMAGE.
48f1535dc8Sdjm  * ====================================================================
49f1535dc8Sdjm  *
50f1535dc8Sdjm  */
51f1535dc8Sdjm 
52ec07fdf1Sdjm #include <openssl/crypto.h>
53ec07fdf1Sdjm #include "modes_lcl.h"
54f1535dc8Sdjm #include <string.h>
55f1535dc8Sdjm 
56f1535dc8Sdjm #ifndef MODES_DEBUG
57f1535dc8Sdjm # ifndef NDEBUG
58f1535dc8Sdjm #  define NDEBUG
59f1535dc8Sdjm # endif
60f1535dc8Sdjm #endif
61f1535dc8Sdjm #include <assert.h>
62f1535dc8Sdjm 
63f1535dc8Sdjm /* NOTE: the IV/counter CTR mode is big-endian.  The code itself
64f1535dc8Sdjm  * is endian-neutral. */
65f1535dc8Sdjm 
66f1535dc8Sdjm /* increment counter (128-bit int) by 1 */
67f1535dc8Sdjm static void ctr128_inc(unsigned char *counter) {
68f1535dc8Sdjm 	u32 n=16;
69f1535dc8Sdjm 	u8  c;
70f1535dc8Sdjm 
71f1535dc8Sdjm 	do {
72f1535dc8Sdjm 		--n;
73f1535dc8Sdjm 		c = counter[n];
74f1535dc8Sdjm 		++c;
75f1535dc8Sdjm 		counter[n] = c;
76f1535dc8Sdjm 		if (c) return;
77f1535dc8Sdjm 	} while (n);
78f1535dc8Sdjm }
79f1535dc8Sdjm 
80f1535dc8Sdjm #if !defined(OPENSSL_SMALL_FOOTPRINT)
8121951995Smiod static void
8221951995Smiod ctr128_inc_aligned(unsigned char *counter)
8321951995Smiod {
84*42077c12Sbcook #if BYTE_ORDER == LITTLE_ENDIAN
85f1535dc8Sdjm 	ctr128_inc(counter);
86*42077c12Sbcook #else
87*42077c12Sbcook 	size_t *data, c, n;
88f1535dc8Sdjm 	data = (size_t *)counter;
89f1535dc8Sdjm 	n = 16 / sizeof(size_t);
90f1535dc8Sdjm 	do {
91f1535dc8Sdjm 		--n;
92f1535dc8Sdjm 		c = data[n];
93f1535dc8Sdjm 		++c;
94f1535dc8Sdjm 		data[n] = c;
95*42077c12Sbcook 		if (c)
96*42077c12Sbcook 			return;
97f1535dc8Sdjm 	} while (n);
98*42077c12Sbcook #endif
99f1535dc8Sdjm }
100f1535dc8Sdjm #endif
101f1535dc8Sdjm 
102f1535dc8Sdjm /* The input encrypted as though 128bit counter mode is being
103f1535dc8Sdjm  * used.  The extra state information to record how much of the
104f1535dc8Sdjm  * 128bit block we have used is contained in *num, and the
105f1535dc8Sdjm  * encrypted counter is kept in ecount_buf.  Both *num and
106f1535dc8Sdjm  * ecount_buf must be initialised with zeros before the first
107f1535dc8Sdjm  * call to CRYPTO_ctr128_encrypt().
108f1535dc8Sdjm  *
109f1535dc8Sdjm  * This algorithm assumes that the counter is in the x lower bits
110f1535dc8Sdjm  * of the IV (ivec), and that the application has full control over
111f1535dc8Sdjm  * overflow and the rest of the IV.  This implementation takes NO
112f1535dc8Sdjm  * responsability for checking that the counter doesn't overflow
113f1535dc8Sdjm  * into the rest of the IV when incremented.
114f1535dc8Sdjm  */
115f1535dc8Sdjm void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
116f1535dc8Sdjm 			size_t len, const void *key,
117f1535dc8Sdjm 			unsigned char ivec[16], unsigned char ecount_buf[16],
118f1535dc8Sdjm 			unsigned int *num, block128_f block)
119f1535dc8Sdjm {
120f1535dc8Sdjm 	unsigned int n;
121f1535dc8Sdjm 	size_t l=0;
122f1535dc8Sdjm 
123f1535dc8Sdjm 	assert(*num < 16);
124f1535dc8Sdjm 
125f1535dc8Sdjm 	n = *num;
126f1535dc8Sdjm 
127f1535dc8Sdjm #if !defined(OPENSSL_SMALL_FOOTPRINT)
128f1535dc8Sdjm 	if (16%sizeof(size_t) == 0) do { /* always true actually */
129f1535dc8Sdjm 		while (n && len) {
130f1535dc8Sdjm 			*(out++) = *(in++) ^ ecount_buf[n];
131f1535dc8Sdjm 			--len;
132f1535dc8Sdjm 			n = (n+1) % 16;
133f1535dc8Sdjm 		}
134f1535dc8Sdjm 
135d57444e6Smiod #ifdef __STRICT_ALIGNMENT
136f1535dc8Sdjm 		if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
137f1535dc8Sdjm 			break;
138f1535dc8Sdjm #endif
139f1535dc8Sdjm 		while (len>=16) {
140f1535dc8Sdjm 			(*block)(ivec, ecount_buf, key);
141f1535dc8Sdjm 			ctr128_inc_aligned(ivec);
142f1535dc8Sdjm 			for (; n<16; n+=sizeof(size_t))
143f1535dc8Sdjm 				*(size_t *)(out+n) =
144f1535dc8Sdjm 				*(size_t *)(in+n) ^ *(size_t *)(ecount_buf+n);
145f1535dc8Sdjm 			len -= 16;
146f1535dc8Sdjm 			out += 16;
147f1535dc8Sdjm 			in  += 16;
148f1535dc8Sdjm 			n = 0;
149f1535dc8Sdjm 		}
150f1535dc8Sdjm 		if (len) {
151f1535dc8Sdjm 			(*block)(ivec, ecount_buf, key);
152f1535dc8Sdjm  			ctr128_inc_aligned(ivec);
153f1535dc8Sdjm 			while (len--) {
154f1535dc8Sdjm 				out[n] = in[n] ^ ecount_buf[n];
155f1535dc8Sdjm 				++n;
156f1535dc8Sdjm 			}
157f1535dc8Sdjm 		}
158f1535dc8Sdjm 		*num = n;
159f1535dc8Sdjm 		return;
160f1535dc8Sdjm 	} while(0);
161f1535dc8Sdjm 	/* the rest would be commonly eliminated by x86* compiler */
162f1535dc8Sdjm #endif
163f1535dc8Sdjm 	while (l<len) {
164f1535dc8Sdjm 		if (n==0) {
165f1535dc8Sdjm 			(*block)(ivec, ecount_buf, key);
166f1535dc8Sdjm  			ctr128_inc(ivec);
167f1535dc8Sdjm 		}
168f1535dc8Sdjm 		out[l] = in[l] ^ ecount_buf[n];
169f1535dc8Sdjm 		++l;
170f1535dc8Sdjm 		n = (n+1) % 16;
171f1535dc8Sdjm 	}
172f1535dc8Sdjm 
173f1535dc8Sdjm 	*num=n;
174f1535dc8Sdjm }
175ec07fdf1Sdjm 
176ec07fdf1Sdjm /* increment upper 96 bits of 128-bit counter by 1 */
177ec07fdf1Sdjm static void ctr96_inc(unsigned char *counter) {
178ec07fdf1Sdjm 	u32 n=12;
179ec07fdf1Sdjm 	u8  c;
180ec07fdf1Sdjm 
181ec07fdf1Sdjm 	do {
182ec07fdf1Sdjm 		--n;
183ec07fdf1Sdjm 		c = counter[n];
184ec07fdf1Sdjm 		++c;
185ec07fdf1Sdjm 		counter[n] = c;
186ec07fdf1Sdjm 		if (c) return;
187ec07fdf1Sdjm 	} while (n);
188ec07fdf1Sdjm }
189ec07fdf1Sdjm 
190ec07fdf1Sdjm void CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out,
191ec07fdf1Sdjm 			size_t len, const void *key,
192ec07fdf1Sdjm 			unsigned char ivec[16], unsigned char ecount_buf[16],
193ec07fdf1Sdjm 			unsigned int *num, ctr128_f func)
194ec07fdf1Sdjm {
195ec07fdf1Sdjm 	unsigned int n,ctr32;
196ec07fdf1Sdjm 
197ec07fdf1Sdjm 	assert(*num < 16);
198ec07fdf1Sdjm 
199ec07fdf1Sdjm 	n = *num;
200ec07fdf1Sdjm 
201ec07fdf1Sdjm 	while (n && len) {
202ec07fdf1Sdjm 		*(out++) = *(in++) ^ ecount_buf[n];
203ec07fdf1Sdjm 		--len;
204ec07fdf1Sdjm 		n = (n+1) % 16;
205ec07fdf1Sdjm 	}
206ec07fdf1Sdjm 
207ec07fdf1Sdjm 	ctr32 = GETU32(ivec+12);
208ec07fdf1Sdjm 	while (len>=16) {
209ec07fdf1Sdjm 		size_t blocks = len/16;
210ec07fdf1Sdjm 		/*
211ec07fdf1Sdjm 		 * 1<<28 is just a not-so-small yet not-so-large number...
212ec07fdf1Sdjm 		 * Below condition is practically never met, but it has to
213ec07fdf1Sdjm 		 * be checked for code correctness.
214ec07fdf1Sdjm 		 */
215ec07fdf1Sdjm 		if (sizeof(size_t)>sizeof(unsigned int) && blocks>(1U<<28))
216ec07fdf1Sdjm 			blocks = (1U<<28);
217ec07fdf1Sdjm 		/*
218ec07fdf1Sdjm 		 * As (*func) operates on 32-bit counter, caller
219ec07fdf1Sdjm 		 * has to handle overflow. 'if' below detects the
220ec07fdf1Sdjm 		 * overflow, which is then handled by limiting the
221ec07fdf1Sdjm 		 * amount of blocks to the exact overflow point...
222ec07fdf1Sdjm 		 */
223ec07fdf1Sdjm 		ctr32 += (u32)blocks;
224ec07fdf1Sdjm 		if (ctr32 < blocks) {
225ec07fdf1Sdjm 			blocks -= ctr32;
226ec07fdf1Sdjm 			ctr32   = 0;
227ec07fdf1Sdjm 		}
228ec07fdf1Sdjm 		(*func)(in,out,blocks,key,ivec);
229ec07fdf1Sdjm 		/* (*ctr) does not update ivec, caller does: */
230ec07fdf1Sdjm 		PUTU32(ivec+12,ctr32);
231ec07fdf1Sdjm 		/* ... overflow was detected, propogate carry. */
232ec07fdf1Sdjm 		if (ctr32 == 0)	ctr96_inc(ivec);
233ec07fdf1Sdjm 		blocks *= 16;
234ec07fdf1Sdjm 		len -= blocks;
235ec07fdf1Sdjm 		out += blocks;
236ec07fdf1Sdjm 		in  += blocks;
237ec07fdf1Sdjm 	}
238ec07fdf1Sdjm 	if (len) {
239ec07fdf1Sdjm 		memset(ecount_buf,0,16);
240ec07fdf1Sdjm 		(*func)(ecount_buf,ecount_buf,1,key,ivec);
241ec07fdf1Sdjm 		++ctr32;
242ec07fdf1Sdjm 		PUTU32(ivec+12,ctr32);
243ec07fdf1Sdjm 		if (ctr32 == 0)	ctr96_inc(ivec);
244ec07fdf1Sdjm 		while (len--) {
245ec07fdf1Sdjm 			out[n] = in[n] ^ ecount_buf[n];
246ec07fdf1Sdjm 			++n;
247ec07fdf1Sdjm 		}
248ec07fdf1Sdjm 	}
249ec07fdf1Sdjm 
250ec07fdf1Sdjm 	*num=n;
251ec07fdf1Sdjm }
252