1 /* $OpenBSD: cbc128.c,v 1.8 2023/07/08 14:56:54 beck Exp $ */ 2 /* ==================================================================== 3 * Copyright (c) 2008 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/crypto.h> 53 #include "modes_local.h" 54 #include <string.h> 55 56 #ifndef MODES_DEBUG 57 # ifndef NDEBUG 58 # define NDEBUG 59 # endif 60 #endif 61 62 #undef STRICT_ALIGNMENT 63 #ifdef __STRICT_ALIGNMENT 64 #define STRICT_ALIGNMENT 1 65 #else 66 #define STRICT_ALIGNMENT 0 67 #endif 68 69 void 70 CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out, 71 size_t len, const void *key, 72 unsigned char ivec[16], block128_f block) 73 { 74 size_t n; 75 const unsigned char *iv = ivec; 76 77 #if !defined(OPENSSL_SMALL_FOOTPRINT) 78 if (STRICT_ALIGNMENT && 79 ((size_t)in|(size_t)out|(size_t)ivec) % sizeof(size_t) != 0) { 80 while (len >= 16) { 81 for (n = 0; n < 16; ++n) 82 out[n] = in[n] ^ iv[n]; 83 (*block)(out, out, key); 84 iv = out; 85 len -= 16; 86 in += 16; 87 out += 16; 88 } 89 } else { 90 while (len >= 16) { 91 for (n = 0; n < 16; n += sizeof(size_t)) 92 *(size_t *)(out + n) = 93 *(size_t *)(in + n) ^ *(size_t *)(iv + n); 94 (*block)(out, out, key); 95 iv = out; 96 len -= 16; 97 in += 16; 98 out += 16; 99 } 100 } 101 #endif 102 while (len) { 103 for (n = 0; n < 16 && n < len; ++n) 104 out[n] = in[n] ^ iv[n]; 105 for (; n < 16; ++n) 106 out[n] = iv[n]; 107 (*block)(out, out, key); 108 iv = out; 109 if (len <= 16) 110 break; 111 len -= 16; 112 in += 16; 113 out += 16; 114 } 115 memmove(ivec, iv, 16); 116 } 117 LCRYPTO_ALIAS(CRYPTO_cbc128_encrypt); 118 119 void 120 CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out, 121 size_t len, const void *key, 122 unsigned char ivec[16], block128_f block) 123 { 124 size_t n; 125 union { 126 size_t t[16/sizeof(size_t)]; 127 unsigned char c[16]; 128 } tmp; 129 130 #if !defined(OPENSSL_SMALL_FOOTPRINT) 131 if (in != out) { 132 const unsigned char *iv = ivec; 133 134 if (STRICT_ALIGNMENT && 135 ((size_t)in|(size_t)out|(size_t)ivec) % sizeof(size_t) != 136 0) { 137 while (len >= 16) { 138 (*block)(in, out, key); 139 for (n = 0; n < 16; ++n) 140 out[n] ^= iv[n]; 141 iv = in; 142 len -= 16; 143 in += 16; 144 out += 16; 145 } 146 } else if (16 % sizeof(size_t) == 0) { /* always true */ 147 while (len >= 16) { 148 size_t *out_t = (size_t *)out, 149 *iv_t = (size_t *)iv; 150 151 (*block)(in, out, key); 152 for (n = 0; n < 16/sizeof(size_t); n++) 153 out_t[n] ^= iv_t[n]; 154 iv = in; 155 len -= 16; 156 in += 16; 157 out += 16; 158 } 159 } 160 memmove(ivec, iv, 16); 161 } else { 162 if (STRICT_ALIGNMENT && 163 ((size_t)in|(size_t)out|(size_t)ivec) % sizeof(size_t) != 164 0) { 165 unsigned char c; 166 while (len >= 16) { 167 (*block)(in, tmp.c, key); 168 for (n = 0; n < 16; ++n) { 169 c = in[n]; 170 out[n] = tmp.c[n] ^ ivec[n]; 171 ivec[n] = c; 172 } 173 len -= 16; 174 in += 16; 175 out += 16; 176 } 177 } else if (16 % sizeof(size_t) == 0) { /* always true */ 178 while (len >= 16) { 179 size_t c, *out_t = (size_t *)out, 180 *ivec_t = (size_t *)ivec; 181 const size_t *in_t = (const size_t *)in; 182 183 (*block)(in, tmp.c, key); 184 for (n = 0; n < 16/sizeof(size_t); n++) { 185 c = in_t[n]; 186 out_t[n] = tmp.t[n] ^ ivec_t[n]; 187 ivec_t[n] = c; 188 } 189 len -= 16; 190 in += 16; 191 out += 16; 192 } 193 } 194 } 195 #endif 196 while (len) { 197 unsigned char c; 198 (*block)(in, tmp.c, key); 199 for (n = 0; n < 16 && n < len; ++n) { 200 c = in[n]; 201 out[n] = tmp.c[n] ^ ivec[n]; 202 ivec[n] = c; 203 } 204 if (len <= 16) { 205 for (; n < 16; ++n) 206 ivec[n] = in[n]; 207 break; 208 } 209 len -= 16; 210 in += 16; 211 out += 16; 212 } 213 } 214 LCRYPTO_ALIAS(CRYPTO_cbc128_decrypt); 215