1 /* $OpenBSD: xts128.c,v 1.5 2014/07/09 16:06:13 miod Exp $ */ 2 /* ==================================================================== 3 * Copyright (c) 2011 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 #include <machine/endian.h> 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 int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16], 64 const unsigned char *inp, unsigned char *out, 65 size_t len, int enc) 66 { 67 union { u64 u[2]; u32 d[4]; u8 c[16]; } tweak, scratch; 68 unsigned int i; 69 70 if (len<16) return -1; 71 72 memcpy(tweak.c, iv, 16); 73 74 (*ctx->block2)(tweak.c,tweak.c,ctx->key2); 75 76 if (!enc && (len%16)) len-=16; 77 78 while (len>=16) { 79 #ifdef __STRICT_ALIGNMENT 80 memcpy(scratch.c,inp,16); 81 scratch.u[0] ^= tweak.u[0]; 82 scratch.u[1] ^= tweak.u[1]; 83 #else 84 scratch.u[0] = ((u64*)inp)[0]^tweak.u[0]; 85 scratch.u[1] = ((u64*)inp)[1]^tweak.u[1]; 86 #endif 87 (*ctx->block1)(scratch.c,scratch.c,ctx->key1); 88 #ifdef __STRICT_ALIGNMENT 89 scratch.u[0] ^= tweak.u[0]; 90 scratch.u[1] ^= tweak.u[1]; 91 memcpy(out,scratch.c,16); 92 #else 93 ((u64*)out)[0] = scratch.u[0]^=tweak.u[0]; 94 ((u64*)out)[1] = scratch.u[1]^=tweak.u[1]; 95 #endif 96 inp += 16; 97 out += 16; 98 len -= 16; 99 100 if (len==0) return 0; 101 102 if (BYTE_ORDER == LITTLE_ENDIAN) { 103 unsigned int carry,res; 104 105 res = 0x87&(((int)tweak.d[3])>>31); 106 carry = (unsigned int)(tweak.u[0]>>63); 107 tweak.u[0] = (tweak.u[0]<<1)^res; 108 tweak.u[1] = (tweak.u[1]<<1)|carry; 109 } 110 else { 111 size_t c; 112 113 for (c=0,i=0;i<16;++i) { 114 /*+ substitutes for |, because c is 1 bit */ 115 c += ((size_t)tweak.c[i])<<1; 116 tweak.c[i] = (u8)c; 117 c = c>>8; 118 } 119 tweak.c[0] ^= (u8)(0x87&(0-c)); 120 } 121 } 122 if (enc) { 123 for (i=0;i<len;++i) { 124 u8 c = inp[i]; 125 out[i] = scratch.c[i]; 126 scratch.c[i] = c; 127 } 128 scratch.u[0] ^= tweak.u[0]; 129 scratch.u[1] ^= tweak.u[1]; 130 (*ctx->block1)(scratch.c,scratch.c,ctx->key1); 131 scratch.u[0] ^= tweak.u[0]; 132 scratch.u[1] ^= tweak.u[1]; 133 memcpy(out-16,scratch.c,16); 134 } 135 else { 136 union { u64 u[2]; u8 c[16]; } tweak1; 137 138 if (BYTE_ORDER == LITTLE_ENDIAN) { 139 unsigned int carry,res; 140 141 res = 0x87&(((int)tweak.d[3])>>31); 142 carry = (unsigned int)(tweak.u[0]>>63); 143 tweak1.u[0] = (tweak.u[0]<<1)^res; 144 tweak1.u[1] = (tweak.u[1]<<1)|carry; 145 } 146 else { 147 size_t c; 148 149 for (c=0,i=0;i<16;++i) { 150 /*+ substitutes for |, because c is 1 bit */ 151 c += ((size_t)tweak.c[i])<<1; 152 tweak1.c[i] = (u8)c; 153 c = c>>8; 154 } 155 tweak1.c[0] ^= (u8)(0x87&(0-c)); 156 } 157 #ifdef __STRICT_ALIGNMENT 158 memcpy(scratch.c,inp,16); 159 scratch.u[0] ^= tweak1.u[0]; 160 scratch.u[1] ^= tweak1.u[1]; 161 #else 162 scratch.u[0] = ((u64*)inp)[0]^tweak1.u[0]; 163 scratch.u[1] = ((u64*)inp)[1]^tweak1.u[1]; 164 #endif 165 (*ctx->block1)(scratch.c,scratch.c,ctx->key1); 166 scratch.u[0] ^= tweak1.u[0]; 167 scratch.u[1] ^= tweak1.u[1]; 168 169 for (i=0;i<len;++i) { 170 u8 c = inp[16+i]; 171 out[16+i] = scratch.c[i]; 172 scratch.c[i] = c; 173 } 174 scratch.u[0] ^= tweak.u[0]; 175 scratch.u[1] ^= tweak.u[1]; 176 (*ctx->block1)(scratch.c,scratch.c,ctx->key1); 177 #ifdef __STRICT_ALIGNMENT 178 scratch.u[0] ^= tweak.u[0]; 179 scratch.u[1] ^= tweak.u[1]; 180 memcpy (out,scratch.c,16); 181 #else 182 ((u64*)out)[0] = scratch.u[0]^tweak.u[0]; 183 ((u64*)out)[1] = scratch.u[1]^tweak.u[1]; 184 #endif 185 } 186 187 return 0; 188 } 189