aes_ofb.c revision 296465
118334Speter/* crypto/aes/aes_ofb.c -*- mode:C; c-file-style: "eay" -*- */
250397Sobrien/* ====================================================================
318334Speter * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
418334Speter *
518334Speter * Redistribution and use in source and binary forms, with or without
618334Speter * modification, are permitted provided that the following conditions
718334Speter * are met:
818334Speter *
918334Speter * 1. Redistributions of source code must retain the above copyright
1018334Speter *    notice, this list of conditions and the following disclaimer.
1118334Speter *
1218334Speter * 2. Redistributions in binary form must reproduce the above copyright
1318334Speter *    notice, this list of conditions and the following disclaimer in
1418334Speter *    the documentation and/or other materials provided with the
1518334Speter *    distribution.
1618334Speter *
1718334Speter * 3. All advertising materials mentioning features or use of this
1818334Speter *    software must display the following acknowledgment:
1918334Speter *    "This product includes software developed by the OpenSSL Project
2018334Speter *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
2118334Speter *
2218334Speter * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2318334Speter *    endorse or promote products derived from this software without
2418334Speter *    prior written permission. For written permission, please contact
2518334Speter *    openssl-core@openssl.org.
2618334Speter *
2718334Speter * 5. Products derived from this software may not be called "OpenSSL"
2818334Speter *    nor may "OpenSSL" appear in their names without prior written
2918334Speter *    permission of the OpenSSL Project.
3018334Speter *
3118334Speter * 6. Redistributions of any form whatsoever must retain the following
3250397Sobrien *    acknowledgment:
3318334Speter *    "This product includes software developed by the OpenSSL Project
3418334Speter *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
3518334Speter *
3618334Speter * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
3750397Sobrien * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3850397Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3950397Sobrien * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4052284Sobrien * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4118334Speter * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4218334Speter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4318334Speter * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4418334Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4518334Speter * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4650397Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4718334Speter * OF THE POSSIBILITY OF SUCH DAMAGE.
4818334Speter * ====================================================================
4918334Speter *
5018334Speter */
5118334Speter/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5218334Speter * All rights reserved.
5318334Speter *
5418334Speter * This package is an SSL implementation written
5518334Speter * by Eric Young (eay@cryptsoft.com).
5618334Speter * The implementation was written so as to conform with Netscapes SSL.
5718334Speter *
5818334Speter * This library is free for commercial and non-commercial use as long as
5952284Sobrien * the following conditions are aheared to.  The following conditions
6018334Speter * apply to all code found in this distribution, be it the RC4, RSA,
6152284Sobrien * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
6252284Sobrien * included with this distribution is covered by the same copyright terms
6318334Speter * except that the holder is Tim Hudson (tjh@cryptsoft.com).
6452284Sobrien *
6518334Speter * Copyright remains Eric Young's, and as such any Copyright notices in
6618334Speter * the code are not to be removed.
6718334Speter * If this package is used in a product, Eric Young should be given attribution
6818334Speter * as the author of the parts of the library used.
6952284Sobrien * This can be in the form of a textual message at program startup or
7018334Speter * in documentation (online or textual) provided with the package.
7118334Speter *
7218334Speter * Redistribution and use in source and binary forms, with or without
7318334Speter * modification, are permitted provided that the following conditions
7450397Sobrien * are met:
7550397Sobrien * 1. Redistributions of source code must retain the copyright
7618334Speter *    notice, this list of conditions and the following disclaimer.
7718334Speter * 2. Redistributions in binary form must reproduce the above copyright
7818334Speter *    notice, this list of conditions and the following disclaimer in the
7918334Speter *    documentation and/or other materials provided with the distribution.
8018334Speter * 3. All advertising materials mentioning features or use of this software
8118334Speter *    must display the following acknowledgement:
8218334Speter *    "This product includes cryptographic software written by
8318334Speter *     Eric Young (eay@cryptsoft.com)"
8418334Speter *    The word 'cryptographic' can be left out if the rouines from the library
8518334Speter *    being used are not cryptographic related :-).
8652284Sobrien * 4. If you include any Windows specific code (or a derivative thereof) from
8752284Sobrien *    the apps directory (application code) you must include an acknowledgement:
8852284Sobrien *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
8918334Speter *
9018334Speter * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
9118334Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9218334Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9318334Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
9418334Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
9518334Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
9618334Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
9718334Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
9818334Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
9918334Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
10018334Speter * SUCH DAMAGE.
10118334Speter *
10218334Speter * The licence and distribution terms for any publically available version or
10318334Speter * derivative of this code cannot be changed.  i.e. this code cannot simply be
10418334Speter * copied and put under another distribution licence
10518334Speter * [including the GNU Public Licence.]
10618334Speter */
10752284Sobrien
10818334Speter#ifndef AES_DEBUG
10918334Speter# ifndef NDEBUG
11018334Speter#  define NDEBUG
11118334Speter# endif
11218334Speter#endif
11318334Speter#include <assert.h>
11418334Speter
11518334Speter#include <openssl/aes.h>
11618334Speter#include "aes_locl.h"
11718334Speter
11818334Speter/*
11918334Speter * The input and output encrypted as though 128bit ofb mode is being used.
12018334Speter * The extra state information to record how much of the 128bit block we have
12118334Speter * used is contained in *num;
12218334Speter */
12318334Spetervoid AES_ofb128_encrypt(const unsigned char *in, unsigned char *out,
12418334Speter                        const unsigned long length, const AES_KEY *key,
12552284Sobrien                        unsigned char *ivec, int *num)
12618334Speter{
12718334Speter
12818334Speter    unsigned int n;
12952284Sobrien    unsigned long l = length;
13018334Speter
13118334Speter    assert(in && out && key && ivec && num);
13218334Speter
13352284Sobrien    n = *num;
13418334Speter
13518334Speter    while (l--) {
13618334Speter        if (n == 0) {
13718334Speter            AES_encrypt(ivec, ivec, key);
13818334Speter        }
13918334Speter        *(out++) = *(in++) ^ ivec[n];
14018334Speter        n = (n + 1) % AES_BLOCK_SIZE;
14118334Speter    }
14218334Speter
14318334Speter    *num = n;
14418334Speter}
14518334Speter