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