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