ctr128.c revision 238384
1238384Sjkim/* ====================================================================
2238384Sjkim * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
3238384Sjkim *
4238384Sjkim * Redistribution and use in source and binary forms, with or without
5238384Sjkim * modification, are permitted provided that the following conditions
6238384Sjkim * are met:
7238384Sjkim *
8238384Sjkim * 1. Redistributions of source code must retain the above copyright
9238384Sjkim *    notice, this list of conditions and the following disclaimer.
10238384Sjkim *
11238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
12238384Sjkim *    notice, this list of conditions and the following disclaimer in
13238384Sjkim *    the documentation and/or other materials provided with the
14238384Sjkim *    distribution.
15238384Sjkim *
16238384Sjkim * 3. All advertising materials mentioning features or use of this
17238384Sjkim *    software must display the following acknowledgment:
18238384Sjkim *    "This product includes software developed by the OpenSSL Project
19238384Sjkim *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20238384Sjkim *
21238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22238384Sjkim *    endorse or promote products derived from this software without
23238384Sjkim *    prior written permission. For written permission, please contact
24238384Sjkim *    openssl-core@openssl.org.
25238384Sjkim *
26238384Sjkim * 5. Products derived from this software may not be called "OpenSSL"
27238384Sjkim *    nor may "OpenSSL" appear in their names without prior written
28238384Sjkim *    permission of the OpenSSL Project.
29238384Sjkim *
30238384Sjkim * 6. Redistributions of any form whatsoever must retain the following
31238384Sjkim *    acknowledgment:
32238384Sjkim *    "This product includes software developed by the OpenSSL Project
33238384Sjkim *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34238384Sjkim *
35238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38238384Sjkim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE.
47238384Sjkim * ====================================================================
48238384Sjkim *
49238384Sjkim */
50238384Sjkim
51238384Sjkim#include <openssl/crypto.h>
52238384Sjkim#include "modes_lcl.h"
53238384Sjkim#include <string.h>
54238384Sjkim
55238384Sjkim#ifndef MODES_DEBUG
56238384Sjkim# ifndef NDEBUG
57238384Sjkim#  define NDEBUG
58238384Sjkim# endif
59238384Sjkim#endif
60238384Sjkim#include <assert.h>
61238384Sjkim
62238384Sjkim/* NOTE: the IV/counter CTR mode is big-endian.  The code itself
63238384Sjkim * is endian-neutral. */
64238384Sjkim
65238384Sjkim/* increment counter (128-bit int) by 1 */
66238384Sjkimstatic void ctr128_inc(unsigned char *counter) {
67238384Sjkim	u32 n=16;
68238384Sjkim	u8  c;
69238384Sjkim
70238384Sjkim	do {
71238384Sjkim		--n;
72238384Sjkim		c = counter[n];
73238384Sjkim		++c;
74238384Sjkim		counter[n] = c;
75238384Sjkim		if (c) return;
76238384Sjkim	} while (n);
77238384Sjkim}
78238384Sjkim
79238384Sjkim#if !defined(OPENSSL_SMALL_FOOTPRINT)
80238384Sjkimstatic void ctr128_inc_aligned(unsigned char *counter) {
81238384Sjkim	size_t *data,c,n;
82238384Sjkim	const union { long one; char little; } is_endian = {1};
83238384Sjkim
84238384Sjkim	if (is_endian.little) {
85238384Sjkim		ctr128_inc(counter);
86238384Sjkim		return;
87238384Sjkim	}
88238384Sjkim
89238384Sjkim	data = (size_t *)counter;
90238384Sjkim	n = 16/sizeof(size_t);
91238384Sjkim	do {
92238384Sjkim		--n;
93238384Sjkim		c = data[n];
94238384Sjkim		++c;
95238384Sjkim		data[n] = c;
96238384Sjkim		if (c) return;
97238384Sjkim	} while (n);
98238384Sjkim}
99238384Sjkim#endif
100238384Sjkim
101238384Sjkim/* The input encrypted as though 128bit counter mode is being
102238384Sjkim * used.  The extra state information to record how much of the
103238384Sjkim * 128bit block we have used is contained in *num, and the
104238384Sjkim * encrypted counter is kept in ecount_buf.  Both *num and
105238384Sjkim * ecount_buf must be initialised with zeros before the first
106238384Sjkim * call to CRYPTO_ctr128_encrypt().
107238384Sjkim *
108238384Sjkim * This algorithm assumes that the counter is in the x lower bits
109238384Sjkim * of the IV (ivec), and that the application has full control over
110238384Sjkim * overflow and the rest of the IV.  This implementation takes NO
111238384Sjkim * responsability for checking that the counter doesn't overflow
112238384Sjkim * into the rest of the IV when incremented.
113238384Sjkim */
114238384Sjkimvoid CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
115238384Sjkim			size_t len, const void *key,
116238384Sjkim			unsigned char ivec[16], unsigned char ecount_buf[16],
117238384Sjkim			unsigned int *num, block128_f block)
118238384Sjkim{
119238384Sjkim	unsigned int n;
120238384Sjkim	size_t l=0;
121238384Sjkim
122238384Sjkim	assert(in && out && key && ecount_buf && num);
123238384Sjkim	assert(*num < 16);
124238384Sjkim
125238384Sjkim	n = *num;
126238384Sjkim
127238384Sjkim#if !defined(OPENSSL_SMALL_FOOTPRINT)
128238384Sjkim	if (16%sizeof(size_t) == 0) do { /* always true actually */
129238384Sjkim		while (n && len) {
130238384Sjkim			*(out++) = *(in++) ^ ecount_buf[n];
131238384Sjkim			--len;
132238384Sjkim			n = (n+1) % 16;
133238384Sjkim		}
134238384Sjkim
135238384Sjkim#if defined(STRICT_ALIGNMENT)
136238384Sjkim		if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
137238384Sjkim			break;
138238384Sjkim#endif
139238384Sjkim		while (len>=16) {
140238384Sjkim			(*block)(ivec, ecount_buf, key);
141238384Sjkim			ctr128_inc_aligned(ivec);
142238384Sjkim			for (; n<16; n+=sizeof(size_t))
143238384Sjkim				*(size_t *)(out+n) =
144238384Sjkim				*(size_t *)(in+n) ^ *(size_t *)(ecount_buf+n);
145238384Sjkim			len -= 16;
146238384Sjkim			out += 16;
147238384Sjkim			in  += 16;
148238384Sjkim			n = 0;
149238384Sjkim		}
150238384Sjkim		if (len) {
151238384Sjkim			(*block)(ivec, ecount_buf, key);
152238384Sjkim 			ctr128_inc_aligned(ivec);
153238384Sjkim			while (len--) {
154238384Sjkim				out[n] = in[n] ^ ecount_buf[n];
155238384Sjkim				++n;
156238384Sjkim			}
157238384Sjkim		}
158238384Sjkim		*num = n;
159238384Sjkim		return;
160238384Sjkim	} while(0);
161238384Sjkim	/* the rest would be commonly eliminated by x86* compiler */
162238384Sjkim#endif
163238384Sjkim	while (l<len) {
164238384Sjkim		if (n==0) {
165238384Sjkim			(*block)(ivec, ecount_buf, key);
166238384Sjkim 			ctr128_inc(ivec);
167238384Sjkim		}
168238384Sjkim		out[l] = in[l] ^ ecount_buf[n];
169238384Sjkim		++l;
170238384Sjkim		n = (n+1) % 16;
171238384Sjkim	}
172238384Sjkim
173238384Sjkim	*num=n;
174238384Sjkim}
175238384Sjkim
176238384Sjkim/* increment upper 96 bits of 128-bit counter by 1 */
177238384Sjkimstatic void ctr96_inc(unsigned char *counter) {
178238384Sjkim	u32 n=12;
179238384Sjkim	u8  c;
180238384Sjkim
181238384Sjkim	do {
182238384Sjkim		--n;
183238384Sjkim		c = counter[n];
184238384Sjkim		++c;
185238384Sjkim		counter[n] = c;
186238384Sjkim		if (c) return;
187238384Sjkim	} while (n);
188238384Sjkim}
189238384Sjkim
190238384Sjkimvoid CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out,
191238384Sjkim			size_t len, const void *key,
192238384Sjkim			unsigned char ivec[16], unsigned char ecount_buf[16],
193238384Sjkim			unsigned int *num, ctr128_f func)
194238384Sjkim{
195238384Sjkim	unsigned int n,ctr32;
196238384Sjkim
197238384Sjkim	assert(in && out && key && ecount_buf && num);
198238384Sjkim	assert(*num < 16);
199238384Sjkim
200238384Sjkim	n = *num;
201238384Sjkim
202238384Sjkim	while (n && len) {
203238384Sjkim		*(out++) = *(in++) ^ ecount_buf[n];
204238384Sjkim		--len;
205238384Sjkim		n = (n+1) % 16;
206238384Sjkim	}
207238384Sjkim
208238384Sjkim	ctr32 = GETU32(ivec+12);
209238384Sjkim	while (len>=16) {
210238384Sjkim		size_t blocks = len/16;
211238384Sjkim		/*
212238384Sjkim		 * 1<<28 is just a not-so-small yet not-so-large number...
213238384Sjkim		 * Below condition is practically never met, but it has to
214238384Sjkim		 * be checked for code correctness.
215238384Sjkim		 */
216238384Sjkim		if (sizeof(size_t)>sizeof(unsigned int) && blocks>(1U<<28))
217238384Sjkim			blocks = (1U<<28);
218238384Sjkim		/*
219238384Sjkim		 * As (*func) operates on 32-bit counter, caller
220238384Sjkim		 * has to handle overflow. 'if' below detects the
221238384Sjkim		 * overflow, which is then handled by limiting the
222238384Sjkim		 * amount of blocks to the exact overflow point...
223238384Sjkim		 */
224238384Sjkim		ctr32 += (u32)blocks;
225238384Sjkim		if (ctr32 < blocks) {
226238384Sjkim			blocks -= ctr32;
227238384Sjkim			ctr32   = 0;
228238384Sjkim		}
229238384Sjkim		(*func)(in,out,blocks,key,ivec);
230238384Sjkim		/* (*ctr) does not update ivec, caller does: */
231238384Sjkim		PUTU32(ivec+12,ctr32);
232238384Sjkim		/* ... overflow was detected, propogate carry. */
233238384Sjkim		if (ctr32 == 0)	ctr96_inc(ivec);
234238384Sjkim		blocks *= 16;
235238384Sjkim		len -= blocks;
236238384Sjkim		out += blocks;
237238384Sjkim		in  += blocks;
238238384Sjkim	}
239238384Sjkim	if (len) {
240238384Sjkim		memset(ecount_buf,0,16);
241238384Sjkim		(*func)(ecount_buf,ecount_buf,1,key,ivec);
242238384Sjkim		++ctr32;
243238384Sjkim		PUTU32(ivec+12,ctr32);
244238384Sjkim		if (ctr32 == 0)	ctr96_inc(ivec);
245238384Sjkim		while (len--) {
246238384Sjkim			out[n] = in[n] ^ ecount_buf[n];
247238384Sjkim			++n;
248238384Sjkim		}
249238384Sjkim	}
250238384Sjkim
251238384Sjkim	*num=n;
252238384Sjkim}
253