161828Smarkm/* crypto/idea/i_skey.c */
261828Smarkm/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
361828Smarkm * All rights reserved.
461828Smarkm *
561828Smarkm * This package is an SSL implementation written
661828Smarkm * by Eric Young (eay@cryptsoft.com).
761828Smarkm * The implementation was written so as to conform with Netscapes SSL.
8296465Sdelphij *
961828Smarkm * This library is free for commercial and non-commercial use as long as
1061828Smarkm * the following conditions are aheared to.  The following conditions
1161828Smarkm * apply to all code found in this distribution, be it the RC4, RSA,
1261828Smarkm * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1361828Smarkm * included with this distribution is covered by the same copyright terms
1461828Smarkm * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296465Sdelphij *
1661828Smarkm * Copyright remains Eric Young's, and as such any Copyright notices in
1761828Smarkm * the code are not to be removed.
1861828Smarkm * If this package is used in a product, Eric Young should be given attribution
1961828Smarkm * as the author of the parts of the library used.
2061828Smarkm * This can be in the form of a textual message at program startup or
2161828Smarkm * in documentation (online or textual) provided with the package.
22296465Sdelphij *
2361828Smarkm * Redistribution and use in source and binary forms, with or without
2461828Smarkm * modification, are permitted provided that the following conditions
2561828Smarkm * are met:
2661828Smarkm * 1. Redistributions of source code must retain the copyright
2761828Smarkm *    notice, this list of conditions and the following disclaimer.
2861828Smarkm * 2. Redistributions in binary form must reproduce the above copyright
2961828Smarkm *    notice, this list of conditions and the following disclaimer in the
3061828Smarkm *    documentation and/or other materials provided with the distribution.
3161828Smarkm * 3. All advertising materials mentioning features or use of this software
3261828Smarkm *    must display the following acknowledgement:
3361828Smarkm *    "This product includes cryptographic software written by
3461828Smarkm *     Eric Young (eay@cryptsoft.com)"
3561828Smarkm *    The word 'cryptographic' can be left out if the rouines from the library
3661828Smarkm *    being used are not cryptographic related :-).
37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3861828Smarkm *    the apps directory (application code) you must include an acknowledgement:
3961828Smarkm *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296465Sdelphij *
4161828Smarkm * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4261828Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4361828Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4461828Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4561828Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4661828Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4761828Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4861828Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4961828Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5061828Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5161828Smarkm * SUCH DAMAGE.
52296465Sdelphij *
5361828Smarkm * The licence and distribution terms for any publically available version or
5461828Smarkm * derivative of this code cannot be changed.  i.e. this code cannot simply be
5561828Smarkm * copied and put under another distribution licence
5661828Smarkm * [including the GNU Public Licence.]
5761828Smarkm */
5861828Smarkm
5961828Smarkm#include <openssl/idea.h>
60194206Ssimon#include <openssl/crypto.h>
61194206Ssimon#ifdef OPENSSL_FIPS
62296465Sdelphij# include <openssl/fips.h>
63194206Ssimon#endif
64194206Ssimon
6561828Smarkm#include "idea_lcl.h"
6661828Smarkm
6761828Smarkmstatic IDEA_INT inverse(unsigned int xin);
68194206Ssimon
69194206Ssimon#ifdef OPENSSL_FIPS
7068654Skrisvoid idea_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks)
71296465Sdelphij{
72296465Sdelphij    if (FIPS_mode())
73296465Sdelphij        FIPS_BAD_ABORT(IDEA)
74296465Sdelphij            private_idea_set_encrypt_key(key, ks);
75296465Sdelphij}
76296465Sdelphij
77194206Ssimonvoid private_idea_set_encrypt_key(const unsigned char *key,
78296465Sdelphij                                  IDEA_KEY_SCHEDULE *ks)
79194206Ssimon#else
80194206Ssimonvoid idea_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks)
81194206Ssimon#endif
82296465Sdelphij{
83296465Sdelphij    int i;
84296465Sdelphij    register IDEA_INT *kt, *kf, r0, r1, r2;
8561828Smarkm
86296465Sdelphij    kt = &(ks->data[0][0]);
87296465Sdelphij    n2s(key, kt[0]);
88296465Sdelphij    n2s(key, kt[1]);
89296465Sdelphij    n2s(key, kt[2]);
90296465Sdelphij    n2s(key, kt[3]);
91296465Sdelphij    n2s(key, kt[4]);
92296465Sdelphij    n2s(key, kt[5]);
93296465Sdelphij    n2s(key, kt[6]);
94296465Sdelphij    n2s(key, kt[7]);
9561828Smarkm
96296465Sdelphij    kf = kt;
97296465Sdelphij    kt += 8;
98296465Sdelphij    for (i = 0; i < 6; i++) {
99296465Sdelphij        r2 = kf[1];
100296465Sdelphij        r1 = kf[2];
101296465Sdelphij        *(kt++) = ((r2 << 9) | (r1 >> 7)) & 0xffff;
102296465Sdelphij        r0 = kf[3];
103296465Sdelphij        *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
104296465Sdelphij        r1 = kf[4];
105296465Sdelphij        *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
106296465Sdelphij        r0 = kf[5];
107296465Sdelphij        *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
108296465Sdelphij        r1 = kf[6];
109296465Sdelphij        *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
110296465Sdelphij        r0 = kf[7];
111296465Sdelphij        *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
112296465Sdelphij        r1 = kf[0];
113296465Sdelphij        if (i >= 5)
114296465Sdelphij            break;
115296465Sdelphij        *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
116296465Sdelphij        *(kt++) = ((r1 << 9) | (r2 >> 7)) & 0xffff;
117296465Sdelphij        kf += 8;
118296465Sdelphij    }
119296465Sdelphij}
12061828Smarkm
121160817Ssimonvoid idea_set_decrypt_key(const IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk)
122296465Sdelphij{
123296465Sdelphij    int r;
124296465Sdelphij    register IDEA_INT *tp, t;
125296465Sdelphij    const IDEA_INT *fp;
12661828Smarkm
127296465Sdelphij    tp = &(dk->data[0][0]);
128296465Sdelphij    fp = &(ek->data[8][0]);
129296465Sdelphij    for (r = 0; r < 9; r++) {
130296465Sdelphij        *(tp++) = inverse(fp[0]);
131296465Sdelphij        *(tp++) = ((int)(0x10000L - fp[2]) & 0xffff);
132296465Sdelphij        *(tp++) = ((int)(0x10000L - fp[1]) & 0xffff);
133296465Sdelphij        *(tp++) = inverse(fp[3]);
134296465Sdelphij        if (r == 8)
135296465Sdelphij            break;
136296465Sdelphij        fp -= 6;
137296465Sdelphij        *(tp++) = fp[4];
138296465Sdelphij        *(tp++) = fp[5];
139296465Sdelphij    }
14061828Smarkm
141296465Sdelphij    tp = &(dk->data[0][0]);
142296465Sdelphij    t = tp[1];
143296465Sdelphij    tp[1] = tp[2];
144296465Sdelphij    tp[2] = t;
14561828Smarkm
146296465Sdelphij    t = tp[49];
147296465Sdelphij    tp[49] = tp[50];
148296465Sdelphij    tp[50] = t;
149296465Sdelphij}
15061828Smarkm
15161828Smarkm/* taken directly from the 'paper' I'll have a look at it later */
15261828Smarkmstatic IDEA_INT inverse(unsigned int xin)
153296465Sdelphij{
154296465Sdelphij    long n1, n2, q, r, b1, b2, t;
15561828Smarkm
156296465Sdelphij    if (xin == 0)
157296465Sdelphij        b2 = 0;
158296465Sdelphij    else {
159296465Sdelphij        n1 = 0x10001;
160296465Sdelphij        n2 = xin;
161296465Sdelphij        b2 = 1;
162296465Sdelphij        b1 = 0;
16361828Smarkm
164296465Sdelphij        do {
165296465Sdelphij            r = (n1 % n2);
166296465Sdelphij            q = (n1 - r) / n2;
167296465Sdelphij            if (r == 0) {
168296465Sdelphij                if (b2 < 0)
169296465Sdelphij                    b2 = 0x10001 + b2;
170296465Sdelphij            } else {
171296465Sdelphij                n1 = n2;
172296465Sdelphij                n2 = r;
173296465Sdelphij                t = b2;
174296465Sdelphij                b2 = b1 - q * b2;
175296465Sdelphij                b1 = t;
176296465Sdelphij            }
177296465Sdelphij        } while (r != 0);
178296465Sdelphij    }
179296465Sdelphij    return ((IDEA_INT) b2);
180296465Sdelphij}
181