-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIDEA
More file actions
executable file
·269 lines (269 loc) · 11 KB
/
IDEA
File metadata and controls
executable file
·269 lines (269 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
SUBROUTINE IDEA(MODE, SVAL, MAT DTA, MAT IK)
* ********************************************************************** *
* ********************************************************************** *
* The IDEA(tm) block cipher is covered by a patent held by ETH and a *
* Swiss company called Ascom-Tech AG. The Swiss patent number is *
* PCT/CH91/00117. International patents are pending. IDEA(tm) is a *
* trademark of Ascom-Tech AG. There is no license fee required for *
* noncommercial use. Commercial users may obtain licensing details from *
* Dieter Profos, Ascom Tech AG, Solothurn Lab, Postfach 151, 4502 *
* Solothurn, Switzerland, Tel +41 65 242885, Fax +41 65 235761. *
* *
* The IDEA block cipher uses a 64-bit block size, and a 128-bit key *
* size. It breaks the 64-bit cipher block into four 16-bit words *
* because all of the primitive inner operations are done with 16-bit *
* arithmetic. It likewise breaks the 128-bit cipher key into eight *
* 16-bit words. *
* *
* For further information on the IDEA cipher, see these papers: *
* 1) Xuejia Lai, "Detailed Description and a Software Implementation of *
* the IPES Cipher", Institute for Signal and Information *
* Processing, ETH-Zentrum, Zurich, Switzerland, 1991 *
* *
* 2) Xuejia Lai, James L. Massey, Sean Murphy, "Markov Ciphers and *
* Differential Cryptanalysis", Advances in Cryptology - EUROCRYPT'91 *
* *
* Passed: MODE - 1 = expand 128 bit user key passed in 'SVAL' *
* to full 832 bit key. Return in 'IK()' *
* - 2 = given a 128 bit user key passed in 'SVAL' *
* expand, then build a 832 bit decryption key *
* return this in array 'IK()' *
* - 3 = passed a string in 'SVAL', and a full key in *
* 'IK()' (ie: result of a 'MODE' = 1 or 2 call) *
* perform the en/de-cryption, and return the *
* result in 'SVAL'. *
* SVAL - string value used as input on 'MODE' 1 & 2 calls. *
* This value unchanged on return. *
* DTA - When 'MODE' = 3, this contains the data values to *
* encrypt on entry (ie ASCII decimal values) and *
* the 'massaged' result char values on return. *
* IK - when 'MODE' = 3, this array must contain the sub- *
* key values (ie: expanded key). *
* *
* Returned: IK - for 'MODE' values of 1 or 2, this array will *
* represent the sub-key (ie: expanded) values. *
* DTA - when 'MODE' = 3, this will contain the result of *
* the en/de-cryption operation. *
* *
* Note: This function is not really suited for use with R83 type systems *
* due to the probability of 'CHAR(255)' (ie: 0xFF) characters appearing *
* in the data. This problem makes this (IDEA) a poor block-cipher choice *
* on these platforms. In an attempt to avoid this, we have made the *
* external data interface to this function an array of character decimal *
* values. It is up to the calling function to re-assemble as a string. *
* Note that we have used the intrinsic bit functions for uv/udt. (dmm) *
* ********************************************************************** *
* ********************************************************************** *
*
EQU KEYLEN TO 52 ;* Full key length
EQU MAX16 TO 65535 ;* 0xFFFF
EQU MAX16P1 TO 65536 ;* MAX16 + 1
*
DIM IK(52), DK(52), DTA(8)
*
ON MODE GOSUB 1000, 2000, 3000
RETURN
*
* ********************************************** *
* Expand a 128 bit user key to an encryption key *
* Pass user key in 'SVAL', return array 'IK' *
* ********************************************** *
1000*
FOR J = 1 TO 8
IK(J) = OCONV(SVAL[J*4-3, 4], 'MCXD')
NEXT J
FOR J = 9 TO KEYLEN
Z = MOD(J-1, 8)
BEGIN CASE
CASE Z < 6
IK(J) = (MOD(IK(J-7), 128) * 512) + INT(IK(J-6) / 128)
CASE Z = 6
IK(J) = (MOD(IK(J-7), 128) * 512) + INT(IK(J-14) / 128)
CASE 1
IK(J) = (MOD(IK(J-15), 128) * 512) + INT(IK(J-14) / 128)
END CASE
NEXT J
RETURN
*
* ************************************************ *
* Build an decryption key from a user key by first *
* expanding to an encryption key, then inverting! *
* Passed user key in 'SVAL', return array 'IK' *
* ************************************************ *
2000*
GOSUB 1000 ;* Expand user key into 'IK()'
* Now convert 'IK()' into decrypt key 'DK()'
EPTR = 5 ;* 'IK()' pointer
DPTR = KEYLEN ;* 'DK()' ptr - we work backwards!
X = IK(1) ; GOSUB 5000 ; D1 = X
D2 = 65536 - IK(2)
D3 = 65536 - IK(3)
X = IK(4) ; GOSUB 5000 ; D4 = X
DK(DPTR) = D4 ; DPTR = DPTR - 1
DK(DPTR) = D3 ; DPTR = DPTR - 1
DK(DPTR) = D2 ; DPTR = DPTR - 1
DK(DPTR) = D1 ; DPTR = DPTR - 1
FOR RPTR = 1 TO 7
D1 = IK(EPTR) ; EPTR = EPTR + 1
D2 = IK(EPTR) ; EPTR = EPTR + 1
DK(DPTR) = D2 ; DPTR = DPTR - 1
DK(DPTR) = D1 ; DPTR = DPTR - 1
X = IK(EPTR) ; EPTR = EPTR + 1
GOSUB 5000 ; D1 = X
D2 = 65536 - IK(EPTR) ; EPTR = EPTR + 1
D3 = 65536 - IK(EPTR) ; EPTR = EPTR + 1
X = IK(EPTR) ; EPTR = EPTR + 1
GOSUB 5000 ; D4 = X
DK(DPTR) = D4 ; DPTR = DPTR - 1
DK(DPTR) = D2 ; DPTR = DPTR - 1
DK(DPTR) = D3 ; DPTR = DPTR - 1
DK(DPTR) = D1 ; DPTR = DPTR - 1
NEXT RPTR
D1 = IK(EPTR) ; EPTR = EPTR + 1
D2 = IK(EPTR) ; EPTR = EPTR + 1
DK(DPTR) = D2 ; DPTR = DPTR - 1
DK(DPTR) = D1 ; DPTR = DPTR - 1
X = IK(EPTR) ; EPTR = EPTR + 1
GOSUB 5000 ; D1 = X
D2 = 65536 - IK(EPTR) ; EPTR = EPTR + 1
D3 = 65536 - IK(EPTR) ; EPTR = EPTR + 1
X = IK(EPTR) ; EPTR = EPTR + 1
GOSUB 5000 ; D4 = X
DK(DPTR) = D4 ; DPTR = DPTR - 1
DK(DPTR) = D3 ; DPTR = DPTR - 1
DK(DPTR) = D2 ; DPTR = DPTR - 1
DK(DPTR) = D1 ; DPTR = DPTR - 1
MAT IK = MAT DK ;* Return 'IK()'
RETURN
*
* *********************************************** *
* Do IDEA encryption or decryption (depending on *
* key passed). We expect the key in array 'IK()', *
* the 64 bit block to en/decrypt in 'DTA()'. We *
* return the result of the operation in 'DTA().' *
* *********************************************** *
3000*
IPTR = 1 ;* Pointer into 'IK()' key array!
D1 = DTA(1) * 256 + DTA(2)
D2 = DTA(3) * 256 + DTA(4)
D3 = DTA(5) * 256 + DTA(6)
D4 = DTA(7) * 256 + DTA(8)
FOR RPTR = 1 TO 8
A = D1 ; B = IK(IPTR) ; IPTR = IPTR + 1
GOSUB 4000 ; D1 = A
D2 = D2 + IK(IPTR) ; IPTR = IPTR + 1
IF D2 > MAX16 THEN D2 = MOD(D2, MAX16P1)
D3 = D3 + IK(IPTR) ; IPTR = IPTR + 1
IF D3 > MAX16 THEN D3 = MOD(D3, MAX16P1)
A = D4 ; B = IK(IPTR) ; IPTR = IPTR + 1
GOSUB 4000 ; D4 = A
*
S1 = D2
S2 = D3
D3 = BITXOR(D3, D1) ;* uv/udt
D2 = BITXOR(D2, D4) ;* uv/udt
*r83 A = D3 ; B = D1
*r83 GOSUB 6000 ; D3 = A
*r83 A = D2 ; B = D4
*r83 GOSUB 6000 ; D2 = A
*
A = D3 ; B = IK(IPTR) ; IPTR = IPTR + 1
GOSUB 4000 ; D3 = A
D2 = D2 + D3
IF D2 > MAX16 THEN D2 = MOD(D2, MAX16P1)
A = D2 ; B = IK(IPTR) ; IPTR = IPTR + 1
GOSUB 4000 ; D2 = A
D3 = D3 + D2
IF D3 > MAX16 THEN D3 = MOD(D3, MAX16P1)
*
D1 = BITXOR(D1, D2) ;* uv/udt
D4 = BITXOR(D4, D3) ;* uv/udt
D2 = BITXOR(D2, S2) ;* uv/udt
D3 = BITXOR(D3, S1) ;* uv/udt
*r83 A = D1 ; B = D2
*r83 GOSUB 6000 ; D1 = A
*r83 A = D4 ; B = D3
*r83 GOSUB 6000 ; D4 = A
*r83 A = D2 ; B = S2
*r83 GOSUB 6000 ; D2 = A
*r83 A = D3 ; B = S1
*r83 GOSUB 6000 ; D3 = A
NEXT RPTR
A = D1 ; B = IK(IPTR) ; IPTR = IPTR + 1
GOSUB 4000 ; D1 = A
D3 = D3 + IK(IPTR) ; IPTR = IPTR + 1
IF D3 > MAX16 THEN D3 = MOD(D3, MAX16P1)
D2 = D2 + IK(IPTR) ; IPTR = IPTR + 1
IF D2 > MAX16 THEN D2 = MOD(D2, MAX16P1)
A = D4 ; B = IK(IPTR)
GOSUB 4000 ; D4 = A
* Re-assemble 8 char values for return!
DTA(1) = MOD(INT(D1 / 256), 256)
DTA(2) = MOD(D1, 256)
DTA(3) = MOD(INT(D3 / 256), 256)
DTA(4) = MOD(D3, 256)
DTA(5) = MOD(INT(D2 / 256), 256)
DTA(6) = MOD(D2, 256)
DTA(7) = MOD(INT(D4 / 256), 256)
DTA(8) = MOD(D4, 256)
RETURN
*
* *************************************** *
* Multiply 'A' * 'B' modulo 65537, return *
* result in variable 'A'. Variable 'B' is *
* not changed by this function. *
* *************************************** *
4000*
T0 = A * B
IF T0 = 0 THEN
A = MOD(65537 - A - B, 65536)
END ELSE A = MOD(T0, 65537)
RETURN
*
* ********************************************** *
* Computes multiplicative inverse using Euclid's *
* greatest common divisor algorithm. Note that *
* zero and one are 'self inverse'. Passed 'X', *
* compute the inverse, returning result in 'X'. *
* ********************************************** *
5000*
IF X > 1 THEN
T1 = INT(65537 / X)
T2 = MOD(65537, X)
IF T2 = 1 THEN
X = MOD(65537 - T1, 65536)
END ELSE
T0 = 1
LOOP
T3 = INT(X / T2)
X = MOD(X, T2)
T0 = T0 + (T1 * T3)
IF X = 1 THEN
X = T0
RETURN
END
T3 = INT(T2 / X)
T2 = MOD(T2, X)
T1 = T1 + (T0 * T3)
WHILE T2 # 1 DO REPEAT
X = 65537 - T1
END
END
RETURN
*
* ********************************* *
* XOR passed variables 'A' and 'B' *
* return the 16 bit result in 'A'. *
* ********************************* *
6000*
T0 = 0 ; T1 = 1
FOR T2 = 1 TO 16 ;* Note we only XOR 16 bits!!
IF MOD(INT(A / T1), 2) # MOD(INT(B / T1), 2) THEN
T0 = T0 + T1
END
T1 = T1 * 2
NEXT T2
A = T0
RETURN
*
END