summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/idea/i_skey.c
diff options
context:
space:
mode:
authorbeck <>2023-07-07 12:51:58 +0000
committerbeck <>2023-07-07 12:51:58 +0000
commita9cdc101468391df513beb27c13bf27170418093 (patch)
tree4959b4936ded67fb6b286e463fbc27eee245ce20 /src/lib/libcrypto/idea/i_skey.c
parent5c0527aa790cba96ac1060e17d6a3909694d8e4e (diff)
downloadopenbsd-a9cdc101468391df513beb27c13bf27170418093.tar.gz
openbsd-a9cdc101468391df513beb27c13bf27170418093.tar.bz2
openbsd-a9cdc101468391df513beb27c13bf27170418093.zip
Hit idea with the loving mallet of knfmt
ok knfmt
Diffstat (limited to 'src/lib/libcrypto/idea/i_skey.c')
-rw-r--r--src/lib/libcrypto/idea/i_skey.c182
1 files changed, 96 insertions, 86 deletions
diff --git a/src/lib/libcrypto/idea/i_skey.c b/src/lib/libcrypto/idea/i_skey.c
index 4338165127..481c50ba55 100644
--- a/src/lib/libcrypto/idea/i_skey.c
+++ b/src/lib/libcrypto/idea/i_skey.c
@@ -1,25 +1,25 @@
1/* $OpenBSD: i_skey.c,v 1.5 2022/11/26 16:08:53 tb Exp $ */ 1/* $OpenBSD: i_skey.c,v 1.6 2023/07/07 12:51:58 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used. 19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or 20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -61,97 +61,107 @@
61#include "idea_local.h" 61#include "idea_local.h"
62 62
63static IDEA_INT inverse(unsigned int xin); 63static IDEA_INT inverse(unsigned int xin);
64void idea_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks) 64void
65 { 65idea_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks)
66{
66 int i; 67 int i;
67 IDEA_INT *kt,*kf,r0,r1,r2; 68 IDEA_INT *kt, *kf, r0, r1, r2;
68 69
69 kt= &(ks->data[0][0]); 70 kt = &(ks->data[0][0]);
70 n2s(key,kt[0]); n2s(key,kt[1]); n2s(key,kt[2]); n2s(key,kt[3]); 71 n2s(key, kt[0]);
71 n2s(key,kt[4]); n2s(key,kt[5]); n2s(key,kt[6]); n2s(key,kt[7]); 72 n2s(key, kt[1]);
73 n2s(key, kt[2]);
74 n2s(key, kt[3]);
75 n2s(key, kt[4]);
76 n2s(key, kt[5]);
77 n2s(key, kt[6]);
78 n2s(key, kt[7]);
72 79
73 kf=kt; 80 kf = kt;
74 kt+=8; 81 kt += 8;
75 for (i=0; i<6; i++) 82 for (i = 0; i < 6; i++)
76 { 83 {
77 r2= kf[1]; 84 r2 = kf[1];
78 r1= kf[2]; 85 r1 = kf[2];
79 *(kt++)= ((r2<<9) | (r1>>7))&0xffff; 86 *(kt++) = ((r2 << 9) | (r1 >> 7)) & 0xffff;
80 r0= kf[3]; 87 r0 = kf[3];
81 *(kt++)= ((r1<<9) | (r0>>7))&0xffff; 88 *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
82 r1= kf[4]; 89 r1 = kf[4];
83 *(kt++)= ((r0<<9) | (r1>>7))&0xffff; 90 *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
84 r0= kf[5]; 91 r0 = kf[5];
85 *(kt++)= ((r1<<9) | (r0>>7))&0xffff; 92 *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
86 r1= kf[6]; 93 r1 = kf[6];
87 *(kt++)= ((r0<<9) | (r1>>7))&0xffff; 94 *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
88 r0= kf[7]; 95 r0 = kf[7];
89 *(kt++)= ((r1<<9) | (r0>>7))&0xffff; 96 *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
90 r1= kf[0]; 97 r1 = kf[0];
91 if (i >= 5) break; 98 if (i >= 5)
92 *(kt++)= ((r0<<9) | (r1>>7))&0xffff; 99 break;
93 *(kt++)= ((r1<<9) | (r2>>7))&0xffff; 100 *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
94 kf+=8; 101 *(kt++) = ((r1 << 9) | (r2 >> 7)) & 0xffff;
95 } 102 kf += 8;
96 } 103 }
104}
97 105
98void idea_set_decrypt_key(IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk) 106void
99 { 107idea_set_decrypt_key(IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk)
108{
100 int r; 109 int r;
101 IDEA_INT *fp,*tp,t; 110 IDEA_INT *fp, *tp, t;
102 111
103 tp= &(dk->data[0][0]); 112 tp = &(dk->data[0][0]);
104 fp= &(ek->data[8][0]); 113 fp = &(ek->data[8][0]);
105 for (r=0; r<9; r++) 114 for (r = 0; r < 9; r++)
106 { 115 {
107 *(tp++)=inverse(fp[0]); 116 *(tp++) = inverse(fp[0]);
108 *(tp++)=((int)(0x10000L-fp[2])&0xffff); 117 *(tp++) = ((int)(0x10000L - fp[2]) & 0xffff);
109 *(tp++)=((int)(0x10000L-fp[1])&0xffff); 118 *(tp++) = ((int)(0x10000L - fp[1]) & 0xffff);
110 *(tp++)=inverse(fp[3]); 119 *(tp++) = inverse(fp[3]);
111 if (r == 8) break; 120 if (r == 8)
112 fp-=6; 121 break;
113 *(tp++)=fp[4]; 122 fp -= 6;
114 *(tp++)=fp[5]; 123 *(tp++) = fp[4];
115 } 124 *(tp++) = fp[5];
125 }
116 126
117 tp= &(dk->data[0][0]); 127 tp = &(dk->data[0][0]);
118 t=tp[1]; 128 t = tp[1];
119 tp[1]=tp[2]; 129 tp[1] = tp[2];
120 tp[2]=t; 130 tp[2] = t;
121 131
122 t=tp[49]; 132 t = tp[49];
123 tp[49]=tp[50]; 133 tp[49] = tp[50];
124 tp[50]=t; 134 tp[50] = t;
125 } 135}
126 136
127/* taken directly from the 'paper' I'll have a look at it later */ 137/* taken directly from the 'paper' I'll have a look at it later */
128static IDEA_INT inverse(unsigned int xin) 138static IDEA_INT
129 { 139inverse(unsigned int xin)
130 long n1,n2,q,r,b1,b2,t; 140{
141 long n1, n2, q, r, b1, b2, t;
131 142
132 if (xin == 0) 143 if (xin == 0)
133 b2=0; 144 b2 = 0;
134 else 145 else {
135 { 146 n1 = 0x10001;
136 n1=0x10001; 147 n2 = xin;
137 n2=xin; 148 b2 = 1;
138 b2=1; 149 b1 = 0;
139 b1=0;
140 150
141 do { 151 do {
142 r=(n1%n2); 152 r = (n1 % n2);
143 q=(n1-r)/n2; 153 q = (n1 - r)/n2;
144 if (r == 0) 154 if (r == 0) {
145 { if (b2 < 0) b2=0x10001+b2; } 155 if (b2 < 0)
146 else 156 b2 = 0x10001 + b2;
147 { 157 } else {
148 n1=n2; 158 n1 = n2;
149 n2=r; 159 n2 = r;
150 t=b2; 160 t = b2;
151 b2=b1-q*b2; 161 b2 = b1 - q*b2;
152 b1=t; 162 b1 = t;
153 } 163 }
154 } while (r != 0); 164 } while (r != 0);
155 }
156 return((IDEA_INT)b2);
157 } 165 }
166 return ((IDEA_INT)b2);
167}