-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidea.el
1069 lines (1007 loc) · 35 KB
/
idea.el
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; -*- lisp -*-
;;;
;;; ----------------------------------------------------------------------
;;; IDEA encryption in elisp. Cool, ha?
;;; ----------------------------------------------------------------------
;;; Created : Thu Jun 29 08:11:25 1995 tri
;;; Last modified: Sat Feb 4 20:21:34 2017 tri
;;; ----------------------------------------------------------------------
;;; Copyright (C) 1995-1999, 2003, 2017
;;; Timo J. Rinne <[email protected]>
;;; ----------------------------------------------------------------------
;;; Any express or implied warranties are disclaimed. In no event
;;; shall the author be liable for any damages caused (directly or
;;; otherwise) by the use of this software.
;;;
;;; irchat-copyright.el applies only if used with irchat IRC client.
;;; Contact the author for additional copyright info.
;;;
(eval-and-compile
(require 'b64)
(require 'crc32))
(eval-and-compile
(provide 'idea))
(defvar idea-default-key-expand-version 3
"Which version of key expand is used as default (1 or 2 or 3).")
;;;; 16bit basic arithmetic operations for IDEA.
(defun idea-mask-16bit (x)
"Mask X with 0xffff"
(logand 65535 x))
(defun idea-+ (a b)
"16 bit add A and B. Ignore overflow."
(idea-mask-16bit (+ a b)))
(defun idea-- (a b)
"16 bit add A and B. Ignore overflow."
(idea-mask-16bit (- a b)))
(defun idea-/ (a b)
"16 bit add A and B. Ignore overflow."
(idea-mask-16bit (/ a b)))
(defun idea-% (a b)
"16 bit add A and B. Ignore overflow."
(idea-mask-16bit (% a b)))
(defun idea-* (a b)
"16 bit multiply A and B. Ignore overflow."
(let* ((al (logand 255 a))
(ah (logand 255 (/ a 256)))
(bl (logand 255 b))
(bh (logand 255 (/ b 256)))
(r1 (* al bl))
(r2 (+ (* al bh) (* ah bl)))
(l (idea-mask-16bit (+ r1 (* r2 256)))))
l))
(defun idea-xor-blocks (a b)
"Perform logical xor operation between encryption blocks A and B"
(list (idea-^ (nth 0 a) (nth 0 b))
(idea-^ (nth 1 a) (nth 1 b))
(idea-^ (nth 2 a) (nth 2 b))
(idea-^ (nth 3 a) (nth 3 b))))
(defun idea-& (a b)
"16 bit AND A and B. Ignore overflow."
(logand (idea-mask-16bit a) (idea-mask-16bit b)))
(defun idea-| (a b)
"16 bit OR A and B. Ignore overflow."
(logior (idea-mask-16bit a) (idea-mask-16bit b)))
(defun idea-^ (a b)
"16 bit XOR A and B. Ignore overflow."
(logxor (idea-mask-16bit a) (idea-mask-16bit b)))
(defun idea-mul (a b)
"A * B in idea style."
(let* ((al (logand 255 a))
(ah (logand 255 (/ a 256)))
(bl (logand 255 b))
(bh (logand 255 (/ b 256)))
(r1 (* al bl))
(r2 (+ (* al bh) (* ah bl)))
(r3 (* ah bh))
(l (+ r1 (* (logand 255 r2) 256)))
(h (+ (/ r2 256) (/ l 65536) r3))
(l (idea-mask-16bit l)))
(if (not (and (= 0 l) (= 0 h)))
(idea-mask-16bit (idea-+ (idea-- l h) (if (< l h) 1 0)))
(if (= 0 a)
(idea-mask-16bit (idea-- 1 b))
(idea-mask-16bit (idea-- 1 a))))))
(defun idea-random ()
"Generate 16 bit random value"
(if (fboundp 'irchat-random-16)
(irchat-random-16)
(idea-mask-16bit (abs (random)))))
(defun idea-random-char ()
"Generate 8 bit random value"
(if (fboundp 'irchat-random-8)
(irchat-random-8)
(idea-& 255 (idea-random))))
(defun idea-<< (x n)
"Shift integer X left N bits"
(if (> n 16)
(setq n 16))
(setq x (idea-mask-16bit x))
(if (or (< n 0)
(< x 0))
0
(while (> n 0)
(setq x (idea-mask-16bit (* x 2)))
(setq n (- n 1))))
x)
(defun idea->> (x n)
"Shift integer X right N bits"
(if (> n 16)
(setq n 16))
(setq x (idea-mask-16bit x))
(if (or (< n 0)
(< x 0))
0
(while (> n 0)
(setq x (/ x 2))
(setq n (- n 1))))
x)
(defun idea-additive-inverse (x)
(idea-mask-16bit (- 0 x)))
(defun idea-multiplicative-inverse (n)
"16 bit multiplicative inverse of N"
(let ((x (idea-mask-16bit n)))
(if (< x 2)
x
(let ((hlp2 (idea-/ 65537 x))
(y (idea-% 65537 x))
(do-brk '()))
(if (= 1 y)
(idea-mask-16bit (idea-- 1 hlp2))
(let ((hlp1 1)
(q)
(ret))
(while (not do-brk)
(setq q (idea-/ x y))
(setq x (idea-% x y))
(setq hlp1 (idea-+ hlp1 (idea-* q hlp2)))
(if (= 1 x)
(progn
(setq ret (idea-mask-16bit hlp1))
(setq do-brk t))
(progn
(setq q (idea-/ y x))
(setq y (idea-% y x))
(setq hlp2 (idea-+ hlp2 (idea-* q hlp1)))
(if (= 1 y)
(progn
(setq ret (idea-mask-16bit (idea-- 1 hlp2)))
(setq do-brk t))))))
ret))))))
(defun idea-shift-key-25bits-left (key)
"Shift expanded (128bit) idea key left 25 bits"
(list (idea-| (idea-<< (nth 1 key) 9) (idea->> (nth 2 key) 7))
(idea-| (idea-<< (nth 2 key) 9) (idea->> (nth 3 key) 7))
(idea-| (idea-<< (nth 3 key) 9) (idea->> (nth 4 key) 7))
(idea-| (idea-<< (nth 4 key) 9) (idea->> (nth 5 key) 7))
(idea-| (idea-<< (nth 5 key) 9) (idea->> (nth 6 key) 7))
(idea-| (idea-<< (nth 6 key) 9) (idea->> (nth 7 key) 7))
(idea-| (idea-<< (nth 7 key) 9) (idea->> (nth 0 key) 7))
(idea-| (idea-<< (nth 0 key) 9) (idea->> (nth 1 key) 7))))
;;; [(1 2 3 4) (5 6 7 8) (9 10 11 12)]
;;; transforms to
;;; [(1 5 9 2) (6 10 3 7) (11 4 8 12)]
;;; and
;;; [(1 2 3 4) (5 6 7 8) (9 10 11 12) (13 14 15 16)]
;;; transforms to
;;; [(1 5 9 13) (2 6 10 14) (3 7 11 15) (4 8 12 16)]
;;; transforms to
;;; [(1 2 3 4) (5 6 7 8) (9 10 11 12) (13 14 15 16)]
(defun idea-interlace-blocklist (bl)
"Make interlaced version of BLOCKLIST."
(let* ((l (length bl))
(r (make-vector l 0))
(i 0))
(while (< i l)
(aset r i
(let ((j (* 4 i)))
(list (nth (/ j l) (elt bl (% j l)))
(nth (/ (+ j 1) l) (elt bl (% (+ j 1) l)))
(nth (/ (+ j 2) l) (elt bl (% (+ j 2) l)))
(nth (/ (+ j 3) l) (elt bl (% (+ j 3) l))))))
(setq i (+ i 1)))
r))
(defun idea-expand-string-to-key (string &optional version)
"Expand string to full 128bit key (list of 8 16bit ints)"
(if (null version) (setq version idea-default-key-expand-version))
(cond ((= version 1) (idea-expand-string-to-key-version-1 string))
((= version 2) (idea-expand-string-to-key-version-2 string))
((= version 3) (idea-expand-string-to-key-version-3 string))
(t (error "Unknown key expansion version"))))
(defun idea-expand-string-to-key-version-1 (string)
"Expand string to full 128bit key (list of 8 16bit ints) (version 1)"
(if (= (length string) 0)
'(0 0 0 0 0 0 0 0)
(let* ((s (if (< (length string) 64)
(let* ((ss string)
(ss (concat ss (crc32-string ss)))
(ss (concat ss (crc32-string ss)))
(ss (concat ss (crc32-string ss)))
(ss (concat ss (crc32-string ss)))
(ss (concat ss (crc32-string ss)))
(ss (concat ss (crc32-string ss)))
(ss (concat ss (crc32-string ss)))
(ss (concat ss (crc32-string ss))))
ss)
string))
(l (length s))
(s (concat s (format "%d" l)))
(l (length s))
(x1 0)
(x2 (/ l 4))
(x3 (* 2 (/ l 4)))
(x4 (* 3 (/ l 4)))
(v1 (crc32-string (substring s x1 l)))
(v2 (crc32-string (substring s x2 l)))
(v3 (crc32-string (substring s x3 l)))
(v4 (crc32-string (substring s x4 l)))
(k1 (idea-4hex-to-int (substring v1 0 4)))
(k2 (idea-4hex-to-int (substring v1 4 8)))
(k3 (idea-4hex-to-int (substring v2 0 4)))
(k4 (idea-4hex-to-int (substring v2 4 8)))
(k5 (idea-4hex-to-int (substring v3 0 4)))
(k6 (idea-4hex-to-int (substring v3 4 8)))
(k7 (idea-4hex-to-int (substring v4 0 4)))
(k8 (idea-4hex-to-int (substring v4 4 8))))
(list k1 k2 k3 k4 k5 k6 k7 k8))))
(defun idea-expand-string-to-key-version-2 (string)
"Expand string to full 128bit key (list of 8 16bit ints) (version 2)"
(if (= (length string) 0)
'(0 0 0 0 0 0 0 0)
(let* ((s (if (> (length string) 3)
string
(concat string (crc32-string string 'raw-string))))
(l (length s))
(s1 (idea-expand-substring (concat (format "%c%c" 0 (logand l 255))
(substring s 0 (/ l 4)))))
(s2 (idea-expand-substring (concat (format "%c%c" 85 (logand l 255))
(substring s (/ l 4) (/ l 2)))))
(s3 (idea-expand-substring (concat (format "%c%c"
170
(logand l 255))
(substring s
(/ l 2)
(+ (/ l 2)
(/ l 4))))))
(s4 (idea-expand-substring (concat (format "%c%c"
255
(logand l 255))
(substring s
(+ (/ l 2) (/ l 4))
l))))
(v1 (crc32-string s1))
(v2 (crc32-string s2))
(v3 (crc32-string s3))
(v4 (crc32-string s4))
(k1 (idea-4hex-to-int (substring v1 0 4)))
(k2 (idea-4hex-to-int (substring v1 4 8)))
(k3 (idea-4hex-to-int (substring v2 0 4)))
(k4 (idea-4hex-to-int (substring v2 4 8)))
(k5 (idea-4hex-to-int (substring v3 0 4)))
(k6 (idea-4hex-to-int (substring v3 4 8)))
(k7 (idea-4hex-to-int (substring v4 0 4)))
(k8 (idea-4hex-to-int (substring v4 4 8))))
(list k1 k2 k3 k4 k5 k6 k7 k8))))
;;;
;;; Mystical constants in following function are just digits from pi
;;; taken with following scheme.
;;; - Digits are split into sequences of five 31415 58979 32384 ...
;;; - Any sequence forming a number > 65535 is dropped
;;;
(defun idea-expand-string-to-key-version-3 (str)
"Generate idea key from STRING."
(if (= (length str) 0)
'(0 0 0 0 0 0 0 0)
(let* ((kk '(31415 58979 32384 62643 38327 16939 5820 45923))
(ek (idea-build-encryption-key kk 666))
(bl (idea-cleartext-string-to-block-list str nil))
(bl (idea-interlace-blocklist bl))
(r1 '(7816 40628 62089 3482))
(r2 '(53421 17067 13282 30664))
(r3 '(44609 55058 22317 25359))
(r4 '(40812 17450 28410 27019))
(i 0)
(l (length bl)))
(while (< i l)
(setq r1 (idea-xor-blocks
(idea-crypt-transform-block (idea-xor-blocks
(elt bl i)
r2)
ek)
r1))
(if (< (+ i 1) l)
(setq r2 (idea-xor-blocks
(idea-crypt-transform-block (idea-xor-blocks
(elt bl (+ i 1))
r1)
ek)
r2)))
(setq i (+ i 1)))
(setq ek (idea-build-encryption-key (list (nth 0 r1) (nth 0 r2)
(nth 1 r1) (nth 1 r2)
(nth 2 r1) (nth 2 r2)
(nth 3 r1) (nth 3 r2))
666))
(setq bl (idea-interlace-blocklist bl))
(setq i 0)
(while (< i l)
(setq r3 (idea-xor-blocks
(idea-crypt-transform-block (idea-xor-blocks
(elt bl i)
r4)
ek)
r3))
(if (< (+ i 1) l)
(setq r4 (idea-xor-blocks
(idea-crypt-transform-block (idea-xor-blocks
(elt bl (+ i 1))
r3)
ek)
r4)))
(setq i (+ i 1)))
(list (nth 0 r3) (nth 0 r4) (nth 1 r3)
(nth 1 r4) (nth 2 r3) (nth 2 r4)
(nth 3 r3) (nth 3 r4)))))
(defun idea-expand-substring (string)
(if (= (length string) 0)
string
(progn
(setq string (concat (crc32-string string 'raw-string) string))
(let ((i (+ 3 (logand 3 (elt string 0)))))
(while (> i 0)
(setq i (- i 1))
(setq string (concat (crc32-string string 'raw-string) string))))
string)))
(defun idea-random-key ()
"Generate random IDEA key (list of 8 16bit values)"
(list (idea-random) (idea-random) (idea-random) (idea-random)
(idea-random) (idea-random) (idea-random) (idea-random)))
(defun idea-build-encryption-key (passphrase &optional version)
"Build idea encryption context from string or keylist (list of 8 16bit ints)"
(let* ((s1 (if (stringp passphrase)
(idea-expand-string-to-key passphrase version)
(if (listp passphrase)
passphrase
(error "IDEA key can be built from string or keylist."))))
(annotation (idea-build-key-annotation s1 "e" version))
(s2 (idea-shift-key-25bits-left s1))
(s3 (idea-shift-key-25bits-left s2))
(s4 (idea-shift-key-25bits-left s3))
(s5 (idea-shift-key-25bits-left s4))
(s6 (idea-shift-key-25bits-left s5))
(s7 (idea-shift-key-25bits-left s6))
;;; (s8 (idea-shift-key-25bits-left s7))
(r1 (list (nth 0 s1) (nth 1 s1) (nth 2 s1)
(nth 3 s1) (nth 4 s1) (nth 5 s1)))
(r2 (list (nth 6 s1) (nth 7 s1) (nth 0 s2)
(nth 1 s2) (nth 2 s2) (nth 3 s2)))
(r3 (list (nth 4 s2) (nth 5 s2) (nth 6 s2)
(nth 7 s2) (nth 0 s3) (nth 1 s3)))
(r4 (list (nth 2 s3) (nth 3 s3) (nth 4 s3)
(nth 5 s3) (nth 6 s3) (nth 7 s3)))
(r5 (list (nth 0 s4) (nth 1 s4) (nth 2 s4)
(nth 3 s4) (nth 4 s4) (nth 5 s4)))
(r6 (list (nth 6 s4) (nth 7 s4) (nth 0 s5)
(nth 1 s5) (nth 2 s5) (nth 3 s5)))
(r7 (list (nth 4 s5) (nth 5 s5) (nth 6 s5)
(nth 7 s5) (nth 0 s6) (nth 1 s6)))
(r8 (list (nth 2 s6) (nth 3 s6) (nth 4 s6)
(nth 5 s6) (nth 6 s6) (nth 7 s6)))
(r9 (list (nth 0 s7) (nth 1 s7) (nth 2 s7)
(nth 3 s7))))
(list r1 r2 r3 r4 r5 r6 r7 r8 r9 annotation)))
(defun idea-build-decryption-key (passphrase &optional version)
"Build idea decryption context from string or keylist (list of 8 16bit ints)"
(let* ((k (idea-build-encryption-key passphrase version))
(annotation (idea-build-key-annotation (list (nth 0 (nth 0 k))
(nth 1 (nth 0 k))
(nth 2 (nth 0 k))
(nth 3 (nth 0 k))
(nth 4 (nth 0 k))
(nth 5 (nth 0 k))
(nth 0 (nth 1 k))
(nth 1 (nth 1 k)))
"d"
version)))
(list
(list (idea-multiplicative-inverse (nth 0 (nth 8 k)))
(idea-additive-inverse (nth 1 (nth 8 k)))
(idea-additive-inverse (nth 2 (nth 8 k)))
(idea-multiplicative-inverse (nth 3 (nth 8 k)))
(nth 4 (nth 7 k))
(nth 5 (nth 7 k)))
(list (idea-multiplicative-inverse (nth 0 (nth 7 k)))
(idea-additive-inverse (nth 2 (nth 7 k)))
(idea-additive-inverse (nth 1 (nth 7 k)))
(idea-multiplicative-inverse (nth 3 (nth 7 k)))
(nth 4 (nth 6 k))
(nth 5 (nth 6 k)))
(list (idea-multiplicative-inverse (nth 0 (nth 6 k)))
(idea-additive-inverse (nth 2 (nth 6 k)))
(idea-additive-inverse (nth 1 (nth 6 k)))
(idea-multiplicative-inverse (nth 3 (nth 6 k)))
(nth 4 (nth 5 k))
(nth 5 (nth 5 k)))
(list (idea-multiplicative-inverse (nth 0 (nth 5 k)))
(idea-additive-inverse (nth 2 (nth 5 k)))
(idea-additive-inverse (nth 1 (nth 5 k)))
(idea-multiplicative-inverse (nth 3 (nth 5 k)))
(nth 4 (nth 4 k))
(nth 5 (nth 4 k)))
(list (idea-multiplicative-inverse (nth 0 (nth 4 k)))
(idea-additive-inverse (nth 2 (nth 4 k)))
(idea-additive-inverse (nth 1 (nth 4 k)))
(idea-multiplicative-inverse (nth 3 (nth 4 k)))
(nth 4 (nth 3 k))
(nth 5 (nth 3 k)))
(list (idea-multiplicative-inverse (nth 0 (nth 3 k)))
(idea-additive-inverse (nth 2 (nth 3 k)))
(idea-additive-inverse (nth 1 (nth 3 k)))
(idea-multiplicative-inverse (nth 3 (nth 3 k)))
(nth 4 (nth 2 k))
(nth 5 (nth 2 k)))
(list (idea-multiplicative-inverse (nth 0 (nth 2 k)))
(idea-additive-inverse (nth 2 (nth 2 k)))
(idea-additive-inverse (nth 1 (nth 2 k)))
(idea-multiplicative-inverse (nth 3 (nth 2 k)))
(nth 4 (nth 1 k))
(nth 5 (nth 1 k)))
(list (idea-multiplicative-inverse (nth 0 (nth 1 k)))
(idea-additive-inverse (nth 2 (nth 1 k)))
(idea-additive-inverse (nth 1 (nth 1 k)))
(idea-multiplicative-inverse (nth 3 (nth 1 k)))
(nth 4 (nth 0 k))
(nth 5 (nth 0 k)))
(list (idea-multiplicative-inverse (nth 0 (nth 0 k)))
(idea-additive-inverse (nth 1 (nth 0 k)))
(idea-additive-inverse (nth 2 (nth 0 k)))
(idea-multiplicative-inverse (nth 3 (nth 0 k))))
annotation)))
(defun idea-encryption-round (data subkey)
"Basic round of IDEA encryption. Method 1."
(let* ((v1 (idea-mul (nth 0 data) (nth 0 subkey)))
(v2 (idea-+ (nth 1 data) (nth 1 subkey)))
(v3 (idea-+ (nth 2 data) (nth 2 subkey)))
(v4 (idea-mul (nth 3 data) (nth 3 subkey)))
(v5 (idea-^ v1 v3))
(v6 (idea-^ v2 v4))
(v7 (idea-mul v5 (nth 4 subkey)))
(v8 (idea-+ v6 v7))
(v9 (idea-mul v8 (nth 5 subkey)))
(v10 (idea-+ v7 v9))
(v11 (idea-^ v1 v9))
(v12 (idea-^ v3 v9))
(v13 (idea-^ v2 v10))
(v14 (idea-^ v4 v10)))
(list v11 v12 v13 v14)))
(defun idea-final-transform (data subkey)
"Final transformation round of IDEA encryption."
(let* ((v1 (idea-mul (nth 0 data) (nth 0 subkey)))
(v2 (idea-+ (nth 2 data) (nth 1 subkey)))
(v3 (idea-+ (nth 1 data) (nth 2 subkey)))
(v4 (idea-mul (nth 3 data) (nth 3 subkey))))
(list v1 v2 v3 v4)))
(defun idea-hex-to-key (h)
"Expand 32 character HEXSTRING to idea key."
(let ((h1 (idea-4hex-to-int (substring h 0 4)))
(h2 (idea-4hex-to-int (substring h 4 8)))
(h3 (idea-4hex-to-int (substring h 8 12)))
(h4 (idea-4hex-to-int (substring h 12 16)))
(h5 (idea-4hex-to-int (substring h 16 20)))
(h6 (idea-4hex-to-int (substring h 20 24)))
(h7 (idea-4hex-to-int (substring h 24 28)))
(h8 (idea-4hex-to-int (substring h 28 32))))
(if (and (> h1 -1) (> h2 -1) (> h3 -1) (> h4 -1)
(> h5 -1) (> h6 -1) (> h7 -1) (> h8 -1))
(list h1 h2 h3 h4 h5 h6 h7 h8)
(list 0 0 0 0 0 0 0 0))))
(defun idea-4hex-to-int (hex)
"Convert four character long HEXSTRING to int"
(let* ((x0 (idea-hex-char-to-int (elt hex 3)))
(x1 (idea-hex-char-to-int (elt hex 2)))
(x2 (idea-hex-char-to-int (elt hex 1)))
(x3 (idea-hex-char-to-int (elt hex 0))))
(if (and (> x0 -1) (> x1 -1) (> x2 -1) (> x3 -1))
(+ x0
(* x1 16)
(* x2 256)
(* x3 4096))
-1)))
(defun idea-2hex-to-int (hex)
"Convert two character long HEXSTRING to int"
(let* ((x0 (idea-hex-char-to-int (elt hex 1)))
(x1 (idea-hex-char-to-int (elt hex 0))))
(if (and (> x0 -1) (> x1 -1))
(+ x0
(* x1 16))
-1)))
(defun idea-crypt-transform-block (data key)
"Make IDEA decryption transformation to DATA with key context KEY"
;;; Make 8 basic encryption rounds.
;;; Swap inner subblocks between basic rounds.
;;; Perform final transformation.
(idea-final-transform
(idea-encryption-round
(idea-encryption-round
(idea-encryption-round
(idea-encryption-round
(idea-encryption-round
(idea-encryption-round
(idea-encryption-round
(idea-encryption-round data (nth 0 key))
(nth 1 key))
(nth 2 key))
(nth 3 key))
(nth 4 key))
(nth 5 key))
(nth 6 key))
(nth 7 key))
(nth 8 key)))
(defun idea-encrypt-block (data key &optional version)
"Encrypt single data block (4 * 16bits) with key"
(let ((my-key (if (listp key) key
(if (stringp key) (idea-build-encryption-key key version)
(error
"IDEA key has to be key-context or string.")))))
(idea-crypt-transform-block data my-key)))
(defun idea-decrypt-block (data key &optional version)
"Decrypt single data block (4 * 16bits) with key"
(let ((my-key (if (listp key) key
(if (stringp key) (idea-build-decryption-key key version)
(error
"IDEA key has to be key-context or string.")))))
(idea-crypt-transform-block data my-key)))
(defun idea-fixed-string-to-block (str)
"Convert 8 byte STRING to idea block"
(list (idea-| (idea-<< (logand (elt str 0) 255) 8) (logand (elt str 1) 255))
(idea-| (idea-<< (logand (elt str 2) 255) 8) (logand (elt str 3) 255))
(idea-| (idea-<< (logand (elt str 4) 255) 8) (logand (elt str 5) 255))
(idea-| (idea-<< (logand (elt str 6) 255) 8) (logand (elt str 7) 255))))
(defun idea-block-to-fixed-string (block)
"Convert idea BLOCK to 8 byte string"
(let ((c1 (idea->> (nth 0 block) 8))
(c2 (idea-& (nth 0 block) 255))
(c3 (idea->> (nth 1 block) 8))
(c4 (idea-& (nth 1 block) 255))
(c5 (idea->> (nth 2 block) 8))
(c6 (idea-& (nth 2 block) 255))
(c7 (idea->> (nth 3 block) 8))
(c8 (idea-& (nth 3 block) 255)))
(concat (char-to-string c1) (char-to-string c2) (char-to-string c3)
(char-to-string c4) (char-to-string c5) (char-to-string c6)
(char-to-string c7) (char-to-string c8))))
(defun idea-add-padding (str rndpad)
"Add block padding to the STRING."
(let* ((len (length str))
(pad (- 8 (% len 8)))
(pad (if (> pad 0) pad 8))
(p1 (idea-| (idea-& (if rndpad (idea-random-char) 0) 31)
(idea-<< (- pad 1) 5)))
(p2 (if rndpad (idea-random-char) 0))
(p3 (if rndpad (idea-random-char) 0))
(p4 (if rndpad (idea-random-char) 0))
(p5 (if rndpad (idea-random-char) 0))
(p6 (if rndpad (idea-random-char) 0))
(p7 (if rndpad (idea-random-char) 0))
(p8 (if rndpad (idea-random-char) 0))
(ps (concat (char-to-string p1) (char-to-string p2)
(char-to-string p3) (char-to-string p4)
(char-to-string p5) (char-to-string p6)
(char-to-string p7) (char-to-string p8)))
(ps (substring ps 0 pad)))
(concat ps str)))
(defun idea-remove-padding (str)
"Remove block padding from the STRING"
(let* ((len (length str)))
(if (< len 8)
nil
(substring str (+ 1 (idea->> (elt str 0) 5)) len))))
(defun idea-cleartext-string-to-block-list (str &optional rndpad)
"Convert cleartext STRING the list of encryption blocks (CLEARTEXT)"
(let* ((str (idea-add-padding (idea-add-crc str) rndpad))
(l (length str))
(r (make-vector (/ l 8) 0))
(i 0))
(while (< i l)
(aset r (/ i 8) (idea-fixed-string-to-block (substring str i (+ i 8))))
(setq i (+ 8 i)))
r))
(defun idea-ciphertext-string-to-block-list (str)
"Convert ciphertext STRING the list of encryption blocks (CIPHERTEXT)"
(let* ((l (length str))
(r (make-vector (/ l 8) 0))
(i 0))
(while (< i l)
(aset r (/ i 8) (idea-fixed-string-to-block (substring str i (+ i 8))))
(setq i (+ 8 i)))
r))
(defun idea-ecb-encrypt-string (str key &optional version)
"Encrypt STRING with KEY (either key context or string)"
(let* ((my-key-type (idea-legal-key key))
(my-key (cond ((equal 'key-complete-decryption my-key-type)
(error "KEY is not an encryption key."))
((equal 'key-complete-encryption my-key-type)
key)
((or (equal 'key-string my-key-type)
(equal 'key-intlist my-key-type))
(idea-build-encryption-key key version))
(t (error "Invalid key."))))
(data (idea-cleartext-string-to-block-list str '()))
(l (length data))
(r (make-string (* l 8) 0))
(i 0))
(while (< i l)
(let ((b (idea-block-to-fixed-string (idea-encrypt-block (elt data i)
my-key)))
(so (* i 8)))
(aset r so (elt b 0))
(aset r (+ so 1) (elt b 1))
(aset r (+ so 2) (elt b 2))
(aset r (+ so 3) (elt b 3))
(aset r (+ so 4) (elt b 4))
(aset r (+ so 5) (elt b 5))
(aset r (+ so 6) (elt b 6))
(aset r (+ so 7) (elt b 7)))
(setq i (+ 1 i)))
(b64-encode-string r)))
(defun idea-ecb-decrypt-string (cipstr key &optional version)
"Decrypt STRING with KEY (either key context or string)"
(let ((str (b64-decode-string cipstr)))
(if (and str (= 0 (% (length str) 8)))
(let* ((my-key-type (idea-legal-key key))
(my-key (cond ((equal 'key-complete-encryption my-key-type)
(error "KEY is not an decryption key."))
((equal 'key-complete-decryption my-key-type)
key)
((or (equal 'key-string my-key-type)
(equal 'key-intlist my-key-type))
(idea-build-decryption-key key version))
(t (error "Invalid key."))))
(data (idea-ciphertext-string-to-block-list str))
(l (length data))
(r (make-string (* l 8) 0))
(i 0))
(while (< i l)
(let ((b (idea-block-to-fixed-string (idea-encrypt-block
(elt data i)
my-key)))
(so (* i 8)))
(aset r so (elt b 0))
(aset r (+ so 1) (elt b 1))
(aset r (+ so 2) (elt b 2))
(aset r (+ so 3) (elt b 3))
(aset r (+ so 4) (elt b 4))
(aset r (+ so 5) (elt b 5))
(aset r (+ so 6) (elt b 6))
(aset r (+ so 7) (elt b 7)))
(setq i (+ 1 i)))
(let ((ret (idea-check-crc (idea-remove-padding r))))
(if ret
ret
'())))
'())))
(defun idea-cbc-encrypt-string (str key &optional version)
"Encrypt STRING with KEY (either key context or string) cbc mode."
(let* ((my-key-type (idea-legal-key key))
(my-key (cond ((equal 'key-complete-decryption my-key-type)
(error "KEY is not an encryption key."))
((equal 'key-complete-encryption my-key-type)
key)
((or (equal 'key-string my-key-type)
(equal 'key-intlist my-key-type))
(idea-build-encryption-key key version))
(t (error "Invalid key."))))
(data (idea-cleartext-string-to-block-list str t))
(context '(0 0 0 0))
(l (length data))
(r (make-string (* l 8) 0))
(i 0))
(while (< i l)
(let* ((c (idea-encrypt-block (idea-xor-blocks (elt data i) context)
my-key))
(b (idea-block-to-fixed-string c))
(so (* i 8)))
(aset r so (elt b 0))
(aset r (+ so 1) (elt b 1))
(aset r (+ so 2) (elt b 2))
(aset r (+ so 3) (elt b 3))
(aset r (+ so 4) (elt b 4))
(aset r (+ so 5) (elt b 5))
(aset r (+ so 6) (elt b 6))
(aset r (+ so 7) (elt b 7))
(setq context c))
(setq i (+ 1 i)))
(b64-encode-string r)))
(defun idea-cbc-decrypt-string (cipstr key &optional version)
"Decrypt STRING with KEY (either key context or string) cbc mode."
(let ((str (b64-decode-string cipstr)))
(if (and str (= 0 (% (length str) 8)))
(let* ((my-key-type (idea-legal-key key))
(my-key (cond ((equal 'key-complete-encryption my-key-type)
(error "KEY is not an decryption key."))
((equal 'key-complete-decryption my-key-type)
key)
((or (equal 'key-string my-key-type)
(equal 'key-intlist my-key-type))
(idea-build-decryption-key key version))
(t (error "Invalid key."))))
(data (idea-ciphertext-string-to-block-list str))
(context '(0 0 0 0))
(l (length data))
(r (make-string (* l 8) 0))
(i 0))
(while (< i l)
(let* ((c (elt data i))
(b (idea-block-to-fixed-string
(idea-xor-blocks (idea-encrypt-block c my-key)
context)))
(so (* i 8)))
(aset r so (elt b 0))
(aset r (+ so 1) (elt b 1))
(aset r (+ so 2) (elt b 2))
(aset r (+ so 3) (elt b 3))
(aset r (+ so 4) (elt b 4))
(aset r (+ so 5) (elt b 5))
(aset r (+ so 6) (elt b 6))
(aset r (+ so 7) (elt b 7))
(setq context c))
(setq i (+ 1 i)))
(let ((ret (idea-check-crc (idea-remove-padding r))))
(if ret
ret
'())))
'())))
(defun idea-add-crc (str)
"Add 32bit crc to STRING"
(concat (crc32-string str) str))
(defun idea-check-crc (str)
"Check crc of the STRING. Return string without crc if ok, otherwise '()"
(if (< (length str) 8)
'()
(let ((realstr (substring str 8 (length str))))
(if (string= (substring str 0 8) (crc32-string realstr))
realstr
'()))))
(defun idea-hex-char-to-int (x)
(if (and (>= x ?0) (<= x ?9))
(- x ?0)
(if (and (>= x ?a) (<= x ?f))
(+ (- x ?a) 10)
(if (and (>= x ?A) (<= x ?F))
(+ (- x ?A) 10)
-1))))
(defun idea-build-key-annotation (key type &optional version)
"Build annotation table of KEY that is of TYPE \"e\" or \"\d\"."
(if (null version) (setq version idea-default-key-expand-version))
(cond ((= version 1) (idea-build-key-annotation-version-1 key type))
((= version 2) (idea-build-key-annotation-version-2 key type))
((= version 3) (idea-build-key-annotation-version-3 key type))
((= version 666) (concat type ":xxxxxxxxxx"))
(t (error "Unknown key expansion version"))))
(defun idea-build-key-annotation-version-1 (key type)
"Build annotation table of KEY that is of TYPE \"e\" or \"\d\"."
(let ((r (make-string 16 0)))
(aset r 15 (idea-& (nth 0 key) 255))
(aset r 14 (idea-& (idea->> (nth 0 key) 8) 255))
(aset r 13 (idea-& (nth 1 key) 255))
(aset r 12 (idea-& (idea->> (nth 1 key) 8) 255))
(aset r 11 (idea-& (nth 2 key) 255))
(aset r 10 (idea-& (idea->> (nth 2 key) 8) 255))
(aset r 9 (idea-& (nth 3 key) 255))
(aset r 8 (idea-& (idea->> (nth 3 key) 8) 255))
(aset r 7 (idea-& (nth 4 key) 255))
(aset r 6 (idea-& (idea->> (nth 4 key) 8) 255))
(aset r 5 (idea-& (nth 5 key) 255))
(aset r 4 (idea-& (idea->> (nth 5 key) 8) 255))
(aset r 3 (idea-& (nth 6 key) 255))
(aset r 2 (idea-& (idea->> (nth 6 key) 8) 255))
(aset r 1 (idea-& (nth 7 key) 255))
(aset r 0 (idea-& (idea->> (nth 7 key) 8) 255))
(concat type ":" (crc32-string r))))
(defun idea-build-key-annotation-version-2 (key type)
"Build annotation table of KEY that is of TYPE \"e\" or \"\d\"."
(if (and (= 0 (nth 0 key))
(= 0 (nth 1 key))
(= 0 (nth 2 key))
(= 0 (nth 3 key))
(= 0 (nth 4 key))
(= 0 (nth 5 key))
(= 0 (nth 6 key))
(= 0 (nth 7 key)))
(concat type ":000000000000")
(let ((r (make-string 9 0))
(s (make-string 9 0)))
(aset r 8 0)
(aset r 7 (idea-& (nth 0 key) 255))
(aset r 6 (idea-& (idea->> (nth 0 key) 8) 255))
(aset r 5 (idea-& (nth 1 key) 255))
(aset r 4 (idea-& (idea->> (nth 1 key) 8) 255))
(aset r 3 (idea-& (nth 2 key) 255))
(aset r 2 (idea-& (idea->> (nth 2 key) 8) 255))
(aset r 1 (idea-& (nth 3 key) 255))
(aset r 0 (idea-& (idea->> (nth 3 key) 8) 255))
(aset s 8 255)
(aset s 7 (idea-& (nth 4 key) 255))
(aset s 6 (idea-& (idea->> (nth 4 key) 8) 255))
(aset s 5 (idea-& (nth 5 key) 255))
(aset s 4 (idea-& (idea->> (nth 5 key) 8) 255))
(aset s 3 (idea-& (nth 6 key) 255))
(aset s 2 (idea-& (idea->> (nth 6 key) 8) 255))
(aset s 1 (idea-& (nth 7 key) 255))
(aset s 0 (idea-& (idea->> (nth 7 key) 8) 255))
(setq s (concat (crc32-string (concat r s) 'raw-string) s))
(setq r (concat (crc32-string (concat s r) 'raw-string) r))
(concat type
":"
(substring (crc32-string r) 0 6)
(substring (crc32-string s) 0 6)))))
(defun idea-build-key-annotation-version-3 (key type)
"Build annotation table of KEY that is of TYPE \"e\" or \"\d\"."
(if (and (= 0 (nth 0 key))
(= 0 (nth 1 key))
(= 0 (nth 2 key))
(= 0 (nth 3 key))
(= 0 (nth 4 key))
(= 0 (nth 5 key))
(= 0 (nth 6 key))
(= 0 (nth 7 key)))
(concat type ":0000000000000000")
(let ((r (make-string 16 0)))
(aset r 15 (idea-& (nth 0 key) 255))
(aset r 14 (idea-& (idea->> (nth 0 key) 8) 255))
(aset r 13 (idea-& (nth 1 key) 255))
(aset r 12 (idea-& (idea->> (nth 1 key) 8) 255))
(aset r 11 (idea-& (nth 2 key) 255))
(aset r 10 (idea-& (idea->> (nth 2 key) 8) 255))
(aset r 9 (idea-& (nth 3 key) 255))
(aset r 8 (idea-& (idea->> (nth 3 key) 8) 255))
(aset r 7 (idea-& (nth 4 key) 255))
(aset r 6 (idea-& (idea->> (nth 4 key) 8) 255))
(aset r 5 (idea-& (nth 5 key) 255))
(aset r 4 (idea-& (idea->> (nth 5 key) 8) 255))
(aset r 3 (idea-& (nth 6 key) 255))
(aset r 2 (idea-& (idea->> (nth 6 key) 8) 255))
(aset r 1 (idea-& (nth 7 key) 255))
(aset r 0 (idea-& (idea->> (nth 7 key) 8) 255))
(let ((ra (idea-expand-string-to-key-version-3 r)))
(concat type ":" (format "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
(+ ?a (% (nth 0 ra) 26))
(+ ?a (% (nth 1 ra) 26))
(+ ?a (% (nth 2 ra) 26))
(+ ?a (% (nth 3 ra) 26))
(+ ?a (% (nth 4 ra) 26))
(+ ?a (% (nth 5 ra) 26))
(+ ?a (% (nth 6 ra) 26))
(+ ?a (% (nth 7 ra) 26))
(+ ?a (% (/ (nth 0 ra) 256) 26))
(+ ?a (% (/ (nth 1 ra) 256) 26))
(+ ?a (% (/ (nth 2 ra) 256) 26))
(+ ?a (% (/ (nth 3 ra) 256) 26))
(+ ?a (% (/ (nth 4 ra) 256) 26))
(+ ?a (% (/ (nth 5 ra) 256) 26))
(+ ?a (% (/ (nth 6 ra) 256) 26))
(+ ?a (% (/ (nth 7 ra) 256) 26))))))))
(defun idea-legal-subkey-p (subkey)
"Is SUBKEY a legal subkey structure?"
(if (and (listp subkey)
(= 6 (length subkey))
(integerp (nth 0 subkey))
(integerp (nth 1 subkey))
(integerp (nth 2 subkey))
(integerp (nth 3 subkey))
(integerp (nth 4 subkey))
(integerp (nth 5 subkey))
(<= 0 (nth 0 subkey))
(>= 65535 (nth 0 subkey))
(<= 0 (nth 1 subkey))
(>= 65535 (nth 1 subkey))
(<= 0 (nth 2 subkey))
(>= 65535 (nth 2 subkey))
(<= 0 (nth 3 subkey))
(>= 65535 (nth 3 subkey))
(<= 0 (nth 4 subkey))
(>= 65535 (nth 4 subkey))
(<= 0 (nth 5 subkey))
(>= 65535 (nth 5 subkey)))
t
nil))
(defun idea-legal-finalkey-p (finalkey)
"Is FINALKEY a legal finalkey structure?"
(if (and (listp finalkey)
(= 4 (length finalkey))
(integerp (nth 0 finalkey))
(integerp (nth 1 finalkey))
(integerp (nth 2 finalkey))
(integerp (nth 3 finalkey))
(<= 0 (nth 0 finalkey))
(>= 65535 (nth 0 finalkey))
(<= 0 (nth 1 finalkey))
(>= 65535 (nth 1 finalkey))
(<= 0 (nth 2 finalkey))
(>= 65535 (nth 2 finalkey))
(<= 0 (nth 3 finalkey))
(>= 65535 (nth 3 finalkey)))
t
nil))
(defun idea-legal-key (key)
"Is KEY a idea key or can one be generated from it?"
(cond ((stringp key)
'key-string)
((and (listp key)
(= 8 (length key))
(integerp (nth 0 key))
(integerp (nth 1 key))
(integerp (nth 2 key))
(integerp (nth 3 key))
(integerp (nth 4 key))
(integerp (nth 5 key))
(integerp (nth 6 key))
(integerp (nth 7 key))
(<= 0 (nth 0 key))
(>= 65535 (nth 0 key))
(<= 0 (nth 1 key))
(>= 65535 (nth 1 key))
(<= 0 (nth 2 key))
(>= 65535 (nth 2 key))
(<= 0 (nth 3 key))
(>= 65535 (nth 3 key))
(<= 0 (nth 4 key))
(>= 65535 (nth 4 key))
(<= 0 (nth 5 key))
(>= 65535 (nth 5 key))
(<= 0 (nth 6 key))
(>= 65535 (nth 6 key))
(<= 0 (nth 7 key))
(>= 65535 (nth 7 key)))
'key-intlist)
((and (listp key)
(= 10 (length key))
(idea-legal-subkey-p (nth 0 key))
(idea-legal-subkey-p (nth 1 key))
(idea-legal-subkey-p (nth 2 key))
(idea-legal-subkey-p (nth 3 key))
(idea-legal-subkey-p (nth 4 key))