1238384Sjkim/* ==================================================================== 2238384Sjkim * Copyright (c) 2008 The OpenSSL Project. All rights reserved. 3238384Sjkim * 4238384Sjkim * Redistribution and use in source and binary forms, with or without 5238384Sjkim * modification, are permitted provided that the following conditions 6238384Sjkim * are met: 7238384Sjkim * 8238384Sjkim * 1. Redistributions of source code must retain the above copyright 9296341Sdelphij * notice, this list of conditions and the following disclaimer. 10238384Sjkim * 11238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright 12238384Sjkim * notice, this list of conditions and the following disclaimer in 13238384Sjkim * the documentation and/or other materials provided with the 14238384Sjkim * distribution. 15238384Sjkim * 16238384Sjkim * 3. All advertising materials mentioning features or use of this 17238384Sjkim * software must display the following acknowledgment: 18238384Sjkim * "This product includes software developed by the OpenSSL Project 19238384Sjkim * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 20238384Sjkim * 21238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 22238384Sjkim * endorse or promote products derived from this software without 23238384Sjkim * prior written permission. For written permission, please contact 24238384Sjkim * openssl-core@openssl.org. 25238384Sjkim * 26238384Sjkim * 5. Products derived from this software may not be called "OpenSSL" 27238384Sjkim * nor may "OpenSSL" appear in their names without prior written 28238384Sjkim * permission of the OpenSSL Project. 29238384Sjkim * 30238384Sjkim * 6. Redistributions of any form whatsoever must retain the following 31238384Sjkim * acknowledgment: 32238384Sjkim * "This product includes software developed by the OpenSSL Project 33238384Sjkim * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 34238384Sjkim * 35238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 36238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38238384Sjkim * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 39238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE. 47238384Sjkim * ==================================================================== 48238384Sjkim * 49238384Sjkim */ 50238384Sjkim 51238384Sjkim#include <openssl/crypto.h> 52238384Sjkim#include "modes_lcl.h" 53238384Sjkim#include <string.h> 54238384Sjkim 55238384Sjkim#ifndef MODES_DEBUG 56238384Sjkim# ifndef NDEBUG 57238384Sjkim# define NDEBUG 58238384Sjkim# endif 59238384Sjkim#endif 60238384Sjkim#include <assert.h> 61238384Sjkim 62296341Sdelphij/* 63296341Sdelphij * The input and output encrypted as though 128bit ofb mode is being used. 64296341Sdelphij * The extra state information to record how much of the 128bit block we have 65296341Sdelphij * used is contained in *num; 66238384Sjkim */ 67238384Sjkimvoid CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out, 68296341Sdelphij size_t len, const void *key, 69296341Sdelphij unsigned char ivec[16], int *num, block128_f block) 70238384Sjkim{ 71296341Sdelphij unsigned int n; 72296341Sdelphij size_t l = 0; 73238384Sjkim 74296341Sdelphij assert(in && out && key && ivec && num); 75238384Sjkim 76296341Sdelphij n = *num; 77238384Sjkim 78238384Sjkim#if !defined(OPENSSL_SMALL_FOOTPRINT) 79296341Sdelphij if (16 % sizeof(size_t) == 0) { /* always true actually */ 80296341Sdelphij do { 81296341Sdelphij while (n && len) { 82296341Sdelphij *(out++) = *(in++) ^ ivec[n]; 83296341Sdelphij --len; 84296341Sdelphij n = (n + 1) % 16; 85296341Sdelphij } 86296341Sdelphij# if defined(STRICT_ALIGNMENT) 87296341Sdelphij if (((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 88296341Sdelphij 0) 89296341Sdelphij break; 90296341Sdelphij# endif 91296341Sdelphij while (len >= 16) { 92296341Sdelphij (*block) (ivec, ivec, key); 93296341Sdelphij for (; n < 16; n += sizeof(size_t)) 94296341Sdelphij *(size_t *)(out + n) = 95296341Sdelphij *(size_t *)(in + n) ^ *(size_t *)(ivec + n); 96296341Sdelphij len -= 16; 97296341Sdelphij out += 16; 98296341Sdelphij in += 16; 99296341Sdelphij n = 0; 100296341Sdelphij } 101296341Sdelphij if (len) { 102296341Sdelphij (*block) (ivec, ivec, key); 103296341Sdelphij while (len--) { 104296341Sdelphij out[n] = in[n] ^ ivec[n]; 105296341Sdelphij ++n; 106296341Sdelphij } 107296341Sdelphij } 108296341Sdelphij *num = n; 109296341Sdelphij return; 110296341Sdelphij } while (0); 111296341Sdelphij } 112296341Sdelphij /* the rest would be commonly eliminated by x86* compiler */ 113238384Sjkim#endif 114296341Sdelphij while (l < len) { 115296341Sdelphij if (n == 0) { 116296341Sdelphij (*block) (ivec, ivec, key); 117296341Sdelphij } 118296341Sdelphij out[l] = in[l] ^ ivec[n]; 119296341Sdelphij ++l; 120296341Sdelphij n = (n + 1) % 16; 121296341Sdelphij } 122238384Sjkim 123296341Sdelphij *num = n; 124238384Sjkim} 125