xref: /dflybsd-src/crypto/libressl/crypto/modes/ofb128.c (revision 72c3367655e64985522b7a48ddfab613e869dc68)
1*72c33676SMaxim Ag /* $OpenBSD: ofb128.c,v 1.4 2015/02/10 09:46:30 miod Exp $ */
2f5b1c8a1SJohn Marino /* ====================================================================
3f5b1c8a1SJohn Marino  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
4f5b1c8a1SJohn Marino  *
5f5b1c8a1SJohn Marino  * Redistribution and use in source and binary forms, with or without
6f5b1c8a1SJohn Marino  * modification, are permitted provided that the following conditions
7f5b1c8a1SJohn Marino  * are met:
8f5b1c8a1SJohn Marino  *
9f5b1c8a1SJohn Marino  * 1. Redistributions of source code must retain the above copyright
10f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer.
11f5b1c8a1SJohn Marino  *
12f5b1c8a1SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
13f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer in
14f5b1c8a1SJohn Marino  *    the documentation and/or other materials provided with the
15f5b1c8a1SJohn Marino  *    distribution.
16f5b1c8a1SJohn Marino  *
17f5b1c8a1SJohn Marino  * 3. All advertising materials mentioning features or use of this
18f5b1c8a1SJohn Marino  *    software must display the following acknowledgment:
19f5b1c8a1SJohn Marino  *    "This product includes software developed by the OpenSSL Project
20f5b1c8a1SJohn Marino  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21f5b1c8a1SJohn Marino  *
22f5b1c8a1SJohn Marino  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23f5b1c8a1SJohn Marino  *    endorse or promote products derived from this software without
24f5b1c8a1SJohn Marino  *    prior written permission. For written permission, please contact
25f5b1c8a1SJohn Marino  *    openssl-core@openssl.org.
26f5b1c8a1SJohn Marino  *
27f5b1c8a1SJohn Marino  * 5. Products derived from this software may not be called "OpenSSL"
28f5b1c8a1SJohn Marino  *    nor may "OpenSSL" appear in their names without prior written
29f5b1c8a1SJohn Marino  *    permission of the OpenSSL Project.
30f5b1c8a1SJohn Marino  *
31f5b1c8a1SJohn Marino  * 6. Redistributions of any form whatsoever must retain the following
32f5b1c8a1SJohn Marino  *    acknowledgment:
33f5b1c8a1SJohn Marino  *    "This product includes software developed by the OpenSSL Project
34f5b1c8a1SJohn Marino  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35f5b1c8a1SJohn Marino  *
36f5b1c8a1SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37f5b1c8a1SJohn Marino  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38f5b1c8a1SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39f5b1c8a1SJohn Marino  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40f5b1c8a1SJohn Marino  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41f5b1c8a1SJohn Marino  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42f5b1c8a1SJohn Marino  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43f5b1c8a1SJohn Marino  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44f5b1c8a1SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45f5b1c8a1SJohn Marino  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46f5b1c8a1SJohn Marino  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47f5b1c8a1SJohn Marino  * OF THE POSSIBILITY OF SUCH DAMAGE.
48f5b1c8a1SJohn Marino  * ====================================================================
49f5b1c8a1SJohn Marino  *
50f5b1c8a1SJohn Marino  */
51f5b1c8a1SJohn Marino 
52f5b1c8a1SJohn Marino #include <openssl/crypto.h>
53f5b1c8a1SJohn Marino #include "modes_lcl.h"
54f5b1c8a1SJohn Marino #include <string.h>
55f5b1c8a1SJohn Marino 
56f5b1c8a1SJohn Marino #ifndef MODES_DEBUG
57f5b1c8a1SJohn Marino # ifndef NDEBUG
58f5b1c8a1SJohn Marino #  define NDEBUG
59f5b1c8a1SJohn Marino # endif
60f5b1c8a1SJohn Marino #endif
61f5b1c8a1SJohn Marino 
62f5b1c8a1SJohn Marino /* The input and output encrypted as though 128bit ofb mode is being
63f5b1c8a1SJohn Marino  * used.  The extra state information to record how much of the
64f5b1c8a1SJohn Marino  * 128bit block we have used is contained in *num;
65f5b1c8a1SJohn Marino  */
CRYPTO_ofb128_encrypt(const unsigned char * in,unsigned char * out,size_t len,const void * key,unsigned char ivec[16],int * num,block128_f block)66f5b1c8a1SJohn Marino void CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out,
67f5b1c8a1SJohn Marino 			size_t len, const void *key,
68f5b1c8a1SJohn Marino 			unsigned char ivec[16], int *num,
69f5b1c8a1SJohn Marino 			block128_f block)
70f5b1c8a1SJohn Marino {
71f5b1c8a1SJohn Marino 	unsigned int n;
72f5b1c8a1SJohn Marino 	size_t l=0;
73f5b1c8a1SJohn Marino 
74f5b1c8a1SJohn Marino 	n = *num;
75f5b1c8a1SJohn Marino 
76f5b1c8a1SJohn Marino #if !defined(OPENSSL_SMALL_FOOTPRINT)
77f5b1c8a1SJohn Marino 	if (16%sizeof(size_t) == 0) do { /* always true actually */
78f5b1c8a1SJohn Marino 		while (n && len) {
79f5b1c8a1SJohn Marino 			*(out++) = *(in++) ^ ivec[n];
80f5b1c8a1SJohn Marino 			--len;
81f5b1c8a1SJohn Marino 			n = (n+1) % 16;
82f5b1c8a1SJohn Marino 		}
83f5b1c8a1SJohn Marino #ifdef __STRICT_ALIGNMENT
84f5b1c8a1SJohn Marino 		if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
85f5b1c8a1SJohn Marino 			break;
86f5b1c8a1SJohn Marino #endif
87f5b1c8a1SJohn Marino 		while (len>=16) {
88f5b1c8a1SJohn Marino 			(*block)(ivec, ivec, key);
89f5b1c8a1SJohn Marino 			for (; n<16; n+=sizeof(size_t))
90f5b1c8a1SJohn Marino 				*(size_t*)(out+n) =
91f5b1c8a1SJohn Marino 				*(size_t*)(in+n) ^ *(size_t*)(ivec+n);
92f5b1c8a1SJohn Marino 			len -= 16;
93f5b1c8a1SJohn Marino 			out += 16;
94f5b1c8a1SJohn Marino 			in  += 16;
95f5b1c8a1SJohn Marino 			n = 0;
96f5b1c8a1SJohn Marino 		}
97f5b1c8a1SJohn Marino 		if (len) {
98f5b1c8a1SJohn Marino 			(*block)(ivec, ivec, key);
99f5b1c8a1SJohn Marino 			while (len--) {
100f5b1c8a1SJohn Marino 				out[n] = in[n] ^ ivec[n];
101f5b1c8a1SJohn Marino 				++n;
102f5b1c8a1SJohn Marino 			}
103f5b1c8a1SJohn Marino 		}
104f5b1c8a1SJohn Marino 		*num = n;
105f5b1c8a1SJohn Marino 		return;
106f5b1c8a1SJohn Marino 	} while(0);
107f5b1c8a1SJohn Marino 	/* the rest would be commonly eliminated by x86* compiler */
108f5b1c8a1SJohn Marino #endif
109f5b1c8a1SJohn Marino 	while (l<len) {
110f5b1c8a1SJohn Marino 		if (n==0) {
111f5b1c8a1SJohn Marino 			(*block)(ivec, ivec, key);
112f5b1c8a1SJohn Marino 		}
113f5b1c8a1SJohn Marino 		out[l] = in[l] ^ ivec[n];
114f5b1c8a1SJohn Marino 		++l;
115f5b1c8a1SJohn Marino 		n = (n+1) % 16;
116f5b1c8a1SJohn Marino 	}
117f5b1c8a1SJohn Marino 
118f5b1c8a1SJohn Marino 	*num=n;
119f5b1c8a1SJohn Marino }
120