1 /* ==================================================================== 2 * Copyright (c) 2008 The OpenSSL Project. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * 3. All advertising materials mentioning features or use of this 17 * software must display the following acknowledgment: 18 * "This product includes software developed by the OpenSSL Project 19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 20 * 21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 22 * endorse or promote products derived from this software without 23 * prior written permission. For written permission, please contact 24 * openssl-core (at) openssl.org. 25 * 26 * 5. Products derived from this software may not be called "OpenSSL" 27 * nor may "OpenSSL" appear in their names without prior written 28 * permission of the OpenSSL Project. 29 * 30 * 6. Redistributions of any form whatsoever must retain the following 31 * acknowledgment: 32 * "This product includes software developed by the OpenSSL Project 33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 34 * 35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 * OF THE POSSIBILITY OF SUCH DAMAGE. 47 * ==================================================================== */ 48 #include <openssl/modes.h> 49 50 #include <assert.h> 51 52 #include "internal.h" 53 54 55 #ifndef STRICT_ALIGNMENT 56 # define STRICT_ALIGNMENT 0 57 #endif 58 59 void CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len, 60 const void *key, uint8_t ivec[16], 61 block128_f block) { 62 size_t n; 63 const uint8_t *iv = ivec; 64 65 assert(in && out && key && ivec); 66 67 if (STRICT_ALIGNMENT && 68 ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) { 69 while (len >= 16) { 70 for (n = 0; n < 16; ++n) { 71 out[n] = in[n] ^ iv[n]; 72 } 73 (*block)(out, out, key); 74 iv = out; 75 len -= 16; 76 in += 16; 77 out += 16; 78 } 79 } else { 80 while (len >= 16) { 81 for (n = 0; n < 16; n += sizeof(size_t)) { 82 *(size_t *)(out + n) = *(size_t *)(in + n) ^ *(size_t *)(iv + n); 83 } 84 (*block)(out, out, key); 85 iv = out; 86 len -= 16; 87 in += 16; 88 out += 16; 89 } 90 } 91 92 while (len) { 93 for (n = 0; n < 16 && n < len; ++n) { 94 out[n] = in[n] ^ iv[n]; 95 } 96 for (; n < 16; ++n) { 97 out[n] = iv[n]; 98 } 99 (*block)(out, out, key); 100 iv = out; 101 if (len <= 16) { 102 break; 103 } 104 len -= 16; 105 in += 16; 106 out += 16; 107 } 108 109 memcpy(ivec, iv, 16); 110 } 111 112 void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len, 113 const void *key, uint8_t ivec[16], 114 block128_f block) { 115 size_t n; 116 union { 117 size_t t[16 / sizeof(size_t)]; 118 uint8_t c[16]; 119 } tmp; 120 121 assert(in && out && key && ivec); 122 123 if (in != out) { 124 const uint8_t *iv = ivec; 125 126 if (STRICT_ALIGNMENT && 127 ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) { 128 while (len >= 16) { 129 (*block)(in, out, key); 130 for (n = 0; n < 16; ++n) 131 out[n] ^= iv[n]; 132 iv = in; 133 len -= 16; 134 in += 16; 135 out += 16; 136 } 137 } else if (16 % sizeof(size_t) == 0) { /* always true */ 138 while (len >= 16) { 139 size_t *out_t = (size_t *)out, *iv_t = (size_t *)iv; 140 141 (*block)(in, out, key); 142 for (n = 0; n < 16 / sizeof(size_t); n++) 143 out_t[n] ^= iv_t[n]; 144 iv = in; 145 len -= 16; 146 in += 16; 147 out += 16; 148 } 149 } 150 memcpy(ivec, iv, 16); 151 } else { 152 if (STRICT_ALIGNMENT && 153 ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) { 154 uint8_t c; 155 while (len >= 16) { 156 (*block)(in, tmp.c, key); 157 for (n = 0; n < 16; ++n) { 158 c = in[n]; 159 out[n] = tmp.c[n] ^ ivec[n]; 160 ivec[n] = c; 161 } 162 len -= 16; 163 in += 16; 164 out += 16; 165 } 166 } else if (16 % sizeof(size_t) == 0) { /* always true */ 167 while (len >= 16) { 168 size_t c, *out_t = (size_t *)out, *ivec_t = (size_t *)ivec; 169 const size_t *in_t = (const size_t *)in; 170 171 (*block)(in, tmp.c, key); 172 for (n = 0; n < 16 / sizeof(size_t); n++) { 173 c = in_t[n]; 174 out_t[n] = tmp.t[n] ^ ivec_t[n]; 175 ivec_t[n] = c; 176 } 177 len -= 16; 178 in += 16; 179 out += 16; 180 } 181 } 182 } 183 184 while (len) { 185 uint8_t c; 186 (*block)(in, tmp.c, key); 187 for (n = 0; n < 16 && n < len; ++n) { 188 c = in[n]; 189 out[n] = tmp.c[n] ^ ivec[n]; 190 ivec[n] = c; 191 } 192 if (len <= 16) { 193 for (; n < 16; ++n) { 194 ivec[n] = in[n]; 195 } 196 break; 197 } 198 len -= 16; 199 in += 16; 200 out += 16; 201 } 202 } 203