X-Git-Url: https://git.openssl.org/?a=blobdiff_plain;f=crypto%2Fidea%2Fi_skey.c;h=2bbd43a5e1b7cb135fd0de4c8f7071d89131a6e2;hb=15dba5be6a4482a9ad7e5b846291f31e97e338ca;hp=00fcc1e586270d2a6247c1b3f7e79f6542a674a9;hpb=58964a492275ca9a59a0cd9c8155cb2491b4b909;p=openssl.git diff --git a/crypto/idea/i_skey.c b/crypto/idea/i_skey.c index 00fcc1e586..2bbd43a5e1 100644 --- a/crypto/idea/i_skey.c +++ b/crypto/idea/i_skey.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,118 +49,113 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */ -#include "idea.h" +#include #include "idea_lcl.h" -#ifndef NOPROTO static IDEA_INT inverse(unsigned int xin); -#else -static IDEA_INT inverse(); -#endif - -void idea_set_encrypt_key(key, ks) -unsigned char *key; -IDEA_KEY_SCHEDULE *ks; - { - int i; - register IDEA_INT *kt,*kf,r0,r1,r2; +void idea_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks) +{ + int i; + register IDEA_INT *kt, *kf, r0, r1, r2; - kt= &(ks->data[0][0]); - n2s(key,kt[0]); n2s(key,kt[1]); n2s(key,kt[2]); n2s(key,kt[3]); - n2s(key,kt[4]); n2s(key,kt[5]); n2s(key,kt[6]); n2s(key,kt[7]); + kt = &(ks->data[0][0]); + n2s(key, kt[0]); + n2s(key, kt[1]); + n2s(key, kt[2]); + n2s(key, kt[3]); + n2s(key, kt[4]); + n2s(key, kt[5]); + n2s(key, kt[6]); + n2s(key, kt[7]); - kf=kt; - kt+=8; - for (i=0; i<6; i++) - { - r2= kf[1]; - r1= kf[2]; - *(kt++)= ((r2<<9) | (r1>>7))&0xffff; - r0= kf[3]; - *(kt++)= ((r1<<9) | (r0>>7))&0xffff; - r1= kf[4]; - *(kt++)= ((r0<<9) | (r1>>7))&0xffff; - r0= kf[5]; - *(kt++)= ((r1<<9) | (r0>>7))&0xffff; - r1= kf[6]; - *(kt++)= ((r0<<9) | (r1>>7))&0xffff; - r0= kf[7]; - *(kt++)= ((r1<<9) | (r0>>7))&0xffff; - r1= kf[0]; - if (i >= 5) break; - *(kt++)= ((r0<<9) | (r1>>7))&0xffff; - *(kt++)= ((r1<<9) | (r2>>7))&0xffff; - kf+=8; - } - } + kf = kt; + kt += 8; + for (i = 0; i < 6; i++) { + r2 = kf[1]; + r1 = kf[2]; + *(kt++) = ((r2 << 9) | (r1 >> 7)) & 0xffff; + r0 = kf[3]; + *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff; + r1 = kf[4]; + *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff; + r0 = kf[5]; + *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff; + r1 = kf[6]; + *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff; + r0 = kf[7]; + *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff; + r1 = kf[0]; + if (i >= 5) + break; + *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff; + *(kt++) = ((r1 << 9) | (r2 >> 7)) & 0xffff; + kf += 8; + } +} -void idea_set_decrypt_key(ek, dk) -IDEA_KEY_SCHEDULE *ek; -IDEA_KEY_SCHEDULE *dk; - { - int r; - register IDEA_INT *fp,*tp,t; +void idea_set_decrypt_key(IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk) +{ + int r; + register IDEA_INT *fp, *tp, t; - tp= &(dk->data[0][0]); - fp= &(ek->data[8][0]); - for (r=0; r<9; r++) - { - *(tp++)=inverse(fp[0]); - *(tp++)=((int)(0x10000L-fp[2])&0xffff); - *(tp++)=((int)(0x10000L-fp[1])&0xffff); - *(tp++)=inverse(fp[3]); - if (r == 8) break; - fp-=6; - *(tp++)=fp[4]; - *(tp++)=fp[5]; - } + tp = &(dk->data[0][0]); + fp = &(ek->data[8][0]); + for (r = 0; r < 9; r++) { + *(tp++) = inverse(fp[0]); + *(tp++) = ((int)(0x10000L - fp[2]) & 0xffff); + *(tp++) = ((int)(0x10000L - fp[1]) & 0xffff); + *(tp++) = inverse(fp[3]); + if (r == 8) + break; + fp -= 6; + *(tp++) = fp[4]; + *(tp++) = fp[5]; + } - tp= &(dk->data[0][0]); - t=tp[1]; - tp[1]=tp[2]; - tp[2]=t; + tp = &(dk->data[0][0]); + t = tp[1]; + tp[1] = tp[2]; + tp[2] = t; - t=tp[49]; - tp[49]=tp[50]; - tp[50]=t; - } + t = tp[49]; + tp[49] = tp[50]; + tp[50] = t; +} /* taken directly from the 'paper' I'll have a look at it later */ -static IDEA_INT inverse(xin) -unsigned int xin; - { - long n1,n2,q,r,b1,b2,t; +static IDEA_INT inverse(unsigned int xin) +{ + long n1, n2, q, r, b1, b2, t; - if (xin == 0) - b2=0; - else - { - n1=0x10001; - n2=xin; - b2=1; - b1=0; + if (xin == 0) + b2 = 0; + else { + n1 = 0x10001; + n2 = xin; + b2 = 1; + b1 = 0; - do { - r=(n1%n2); - q=(n1-r)/n2; - if (r == 0) - { if (b2 < 0) b2=0x10001+b2; } - else - { - n1=n2; - n2=r; - t=b2; - b2=b1-q*b2; - b1=t; - } - } while (r != 0); - } - return((IDEA_INT)b2); - } + do { + r = (n1 % n2); + q = (n1 - r) / n2; + if (r == 0) { + if (b2 < 0) + b2 = 0x10001 + b2; + } else { + n1 = n2; + n2 = r; + t = b2; + b2 = b1 - q * b2; + b1 = t; + } + } while (r != 0); + } + return ((IDEA_INT) b2); +}