i_skey.c revision 61828
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. 861828Smarkm * 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). 1561828Smarkm * 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. 2261828Smarkm * 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 :-). 3761828Smarkm * 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)" 4061828Smarkm * 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. 5261828Smarkm * 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 * $FreeBSD: head/crypto/openssl/crypto/idea/i_skey.c 61828 2000-06-19 21:09:27Z markm $ 5861828Smarkm */ 5961828Smarkm 6061828Smarkm#include <openssl/idea.h> 6161828Smarkm#include "idea_lcl.h" 6261828Smarkm 6361828Smarkmstatic IDEA_INT inverse(unsigned int xin); 6461828Smarkmvoid idea_set_encrypt_key(unsigned char *key, IDEA_KEY_SCHEDULE *ks) 6561828Smarkm { 6661828Smarkm int i; 6761828Smarkm register IDEA_INT *kt,*kf,r0,r1,r2; 6861828Smarkm 6961828Smarkm kt= &(ks->data[0][0]); 7061828Smarkm n2s(key,kt[0]); n2s(key,kt[1]); n2s(key,kt[2]); n2s(key,kt[3]); 7161828Smarkm n2s(key,kt[4]); n2s(key,kt[5]); n2s(key,kt[6]); n2s(key,kt[7]); 7261828Smarkm 7361828Smarkm kf=kt; 7461828Smarkm kt+=8; 7561828Smarkm for (i=0; i<6; i++) 7661828Smarkm { 7761828Smarkm r2= kf[1]; 7861828Smarkm r1= kf[2]; 7961828Smarkm *(kt++)= ((r2<<9) | (r1>>7))&0xffff; 8061828Smarkm r0= kf[3]; 8161828Smarkm *(kt++)= ((r1<<9) | (r0>>7))&0xffff; 8261828Smarkm r1= kf[4]; 8361828Smarkm *(kt++)= ((r0<<9) | (r1>>7))&0xffff; 8461828Smarkm r0= kf[5]; 8561828Smarkm *(kt++)= ((r1<<9) | (r0>>7))&0xffff; 8661828Smarkm r1= kf[6]; 8761828Smarkm *(kt++)= ((r0<<9) | (r1>>7))&0xffff; 8861828Smarkm r0= kf[7]; 8961828Smarkm *(kt++)= ((r1<<9) | (r0>>7))&0xffff; 9061828Smarkm r1= kf[0]; 9161828Smarkm if (i >= 5) break; 9261828Smarkm *(kt++)= ((r0<<9) | (r1>>7))&0xffff; 9361828Smarkm *(kt++)= ((r1<<9) | (r2>>7))&0xffff; 9461828Smarkm kf+=8; 9561828Smarkm } 9661828Smarkm } 9761828Smarkm 9861828Smarkmvoid idea_set_decrypt_key(IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk) 9961828Smarkm { 10061828Smarkm int r; 10161828Smarkm register IDEA_INT *fp,*tp,t; 10261828Smarkm 10361828Smarkm tp= &(dk->data[0][0]); 10461828Smarkm fp= &(ek->data[8][0]); 10561828Smarkm for (r=0; r<9; r++) 10661828Smarkm { 10761828Smarkm *(tp++)=inverse(fp[0]); 10861828Smarkm *(tp++)=((int)(0x10000L-fp[2])&0xffff); 10961828Smarkm *(tp++)=((int)(0x10000L-fp[1])&0xffff); 11061828Smarkm *(tp++)=inverse(fp[3]); 11161828Smarkm if (r == 8) break; 11261828Smarkm fp-=6; 11361828Smarkm *(tp++)=fp[4]; 11461828Smarkm *(tp++)=fp[5]; 11561828Smarkm } 11661828Smarkm 11761828Smarkm tp= &(dk->data[0][0]); 11861828Smarkm t=tp[1]; 11961828Smarkm tp[1]=tp[2]; 12061828Smarkm tp[2]=t; 12161828Smarkm 12261828Smarkm t=tp[49]; 12361828Smarkm tp[49]=tp[50]; 12461828Smarkm tp[50]=t; 12561828Smarkm } 12661828Smarkm 12761828Smarkm/* taken directly from the 'paper' I'll have a look at it later */ 12861828Smarkmstatic IDEA_INT inverse(unsigned int xin) 12961828Smarkm { 13061828Smarkm long n1,n2,q,r,b1,b2,t; 13161828Smarkm 13261828Smarkm if (xin == 0) 13361828Smarkm b2=0; 13461828Smarkm else 13561828Smarkm { 13661828Smarkm n1=0x10001; 13761828Smarkm n2=xin; 13861828Smarkm b2=1; 13961828Smarkm b1=0; 14061828Smarkm 14161828Smarkm do { 14261828Smarkm r=(n1%n2); 14361828Smarkm q=(n1-r)/n2; 14461828Smarkm if (r == 0) 14561828Smarkm { if (b2 < 0) b2=0x10001+b2; } 14661828Smarkm else 14761828Smarkm { 14861828Smarkm n1=n2; 14961828Smarkm n2=r; 15061828Smarkm t=b2; 15161828Smarkm b2=b1-q*b2; 15261828Smarkm b1=t; 15361828Smarkm } 15461828Smarkm } while (r != 0); 15561828Smarkm } 15661828Smarkm return((IDEA_INT)b2); 15761828Smarkm } 158