-
Notifications
You must be signed in to change notification settings - Fork 1
/
gosthash.c
1171 lines (1099 loc) · 41.7 KB
/
gosthash.c
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
/*!
\file gosthash.c
Copyright (c) 2011-2014 Anatoly Georgievski
Implementation of GOST R 34.11-94 hash function
HMAC GOST R
PBKDF2 HMAC GOSTR
\see [RFC 5830] GOST 28147-89: Encryption, Decryption, and Message Authentication Code (MAC) Algorithms
\see [RFC 5831] GOST R 34.11-94, March 2010
\see [RFC 4357] Additional Cryptographic Algorithms for Use with GOST 28147-89,
GOST R 34.10-94, GOST R 34.10-2001, and GOST R 34.11-94 Algorithms, 2006
OID: 1.2.643.2.2.35.1 id-GostR3410-2001-CryptoPro-A-ParamSet
OID: 1.2.643.2.2.30.1 id-GostR3411-94-CryptoProParamSet
This file is distributed under GPL
/usr/gcc/4.3/bin/gcc -DDEBUG_GOST -o gost src/gosthash.c src/base64.c
$ gcc -Os -s -DDEBUG_GOST -o gosthash.exe gosthash.c base64.c
echo -n "8JaanTcVv6ndF8Xp/N011Lp46e68LjaUT9FhnEyQGs8=" | base64 -id | od -A n -X
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "hmac.h"
#if defined(__sun__) || defined(__linux__) || defined(__FreeBSD__)
#define _aligned_malloc(size, align) memalign(align, size)
#define _aligned_free(ptr) free(ptr)
#else
#include <malloc.h>
#endif // __sun__ __linux__ __FreeBSD__
typedef char v16qi __attribute__((__vector_size__(16)));
//typedef uint8_t v4qi __attribute__((__vector_size__(4)));
typedef uint32_t v4si __attribute__((__vector_size__(16)));
typedef uint16_t v8hi __attribute__((__vector_size__(16)));
/* Internal representation of GOST substitution blocks */
typedef struct _GostSubstBlock gost_subst_block;
struct _GostSubstBlock {
v16qi k8;
v16qi k7;
v16qi k6;
v16qi k5;
v16qi k4;
v16qi k3;
v16qi k2;
v16qi k1;
};
typedef struct _GostCtx gost_ctx;
struct _GostCtx {
// uint32_t k[8];
/* Constant s-boxes -- set up in gost_init(). */
v16qi k87[16],k65[16],k43[16],k21[16];
// uint32_t k87[256],k65[256],k43[256],k21[256];
};
uint8_t* base64_enc(char *dst, char *src, int length);
//typedef uint8_t v4qi __attribute__((vector_size(4)));
typedef long long int v2di __attribute__((__vector_size__(16)));
typedef uint32_t v4si __attribute__((__vector_size__(16)));
typedef int32_t v4Si __attribute__((__vector_size__(16)));
//typedef long long int v2d_i __attribute__((__vector_size__(16)));
typedef uint64_t v4di __attribute__((__vector_size__(32)));
typedef uint8_t v4qi __attribute__((__vector_size__(4)));
typedef char v16qi __attribute__ ((__vector_size__(16)));
//typedef char v32qi __attribute__ ((__vector_size__(32)));
//typedef unsigned long long v2di __attribute__((mode(V2DI)));
union _v256 {
v2di q[2];
uint64_t d[4];
uint32_t s[8];
uint16_t h[16];
};
typedef union _v256 v256;
/*
typedef struct _GostHashCtx gost_hash_ctx;
struct _GostHashCtx {
int len;
gost_ctx *cipher_ctx;
int left;
uint8_t H[32];
uint8_t S[32];
uint8_t remainder[32];
};*/
/* Substitution blocks from test examples for GOST R 34.11-94 */
const gost_subst_block GostR3411_94_TestParamSet = {
{0X1,0XF,0XD,0X0,0X5,0X7,0XA,0X4,0X9,0X2,0X3,0XE,0X6,0XB,0X8,0XC},
{0XD,0XB,0X4,0X1,0X3,0XF,0X5,0X9,0X0,0XA,0XE,0X7,0X6,0X8,0X2,0XC},
{0X4,0XB,0XA,0X0,0X7,0X2,0X1,0XD,0X3,0X6,0X8,0X5,0X9,0XC,0XF,0XE},
{0X6,0XC,0X7,0X1,0X5,0XF,0XD,0X8,0X4,0XA,0X9,0XE,0X0,0X3,0XB,0X2},
{0X7,0XD,0XA,0X1,0X0,0X8,0X9,0XF,0XE,0X4,0X6,0XC,0XB,0X2,0X5,0X3},
{0X5,0X8,0X1,0XD,0XA,0X3,0X4,0X2,0XE,0XF,0XC,0X7,0X6,0X0,0X9,0XB},
{0XE,0XB,0X4,0XC,0X6,0XD,0XF,0XA,0X2,0X3,0X8,0X1,0X0,0X7,0X5,0X9},
{0X4,0XA,0X9,0X2,0XD,0X8,0X0,0XE,0X6,0XB,0X1,0XC,0X7,0XF,0X5,0X3}
};
/* Substitution blocks for hash function 1.2.643.2.9.1.6.1 */
const gost_subst_block GostR3411_94_CryptoProParamSet= {
{0x1,0x3,0xA,0x9,0x5,0xB,0x4,0xF,0x8,0x6,0x7,0xE,0xD,0x0,0x2,0xC},
{0xD,0xE,0x4,0x1,0x7,0x0,0x5,0xA,0x3,0xC,0x8,0xF,0x6,0x2,0x9,0xB},
{0x7,0x6,0x2,0x4,0xD,0x9,0xF,0x0,0xA,0x1,0x5,0xB,0x8,0xE,0xC,0x3},
{0x7,0x6,0x4,0xB,0x9,0xC,0x2,0xA,0x1,0x8,0x0,0xE,0xF,0xD,0x3,0x5},
{0x4,0xA,0x7,0xC,0x0,0xF,0x2,0x8,0xE,0x1,0x6,0x5,0xD,0xB,0x9,0x3},
{0x7,0xF,0xC,0xE,0x9,0x4,0x1,0x0,0x3,0xB,0x5,0x2,0x6,0xA,0x8,0xD},
{0x5,0xF,0x4,0x0,0x2,0xD,0xB,0x9,0x1,0x7,0x6,0x3,0xC,0xE,0xA,0x8},
{0xA,0x4,0x5,0x6,0x8,0x1,0x3,0x7,0xD,0xC,0xE,0x0,0x9,0x2,0xB,0xF}
};
typedef struct _HashParams HashParams;
struct _HashParams {
const char* name;
const gost_subst_block* paramset;
gost_ctx* ctx;
};
static HashParams hash_params[] = {
{"GOST R 34.11-94 Test ParamSet", &GostR3411_94_TestParamSet, NULL},
{"GOST R 34.11-94 Crypto-Pro ParamSet", &GostR3411_94_CryptoProParamSet, NULL},
// {NULL, NULL, 0},
};
static inline void CLR(v256 *x)
{
//register v2di z = (v2di){0,0};
x->q[0] =(v2di){0}; //^= x->q[0];
x->q[1] =(v2di){0}; //^= x->q[1];
}
static inline void MOV(v256 *x, const v256 *a)
{
x->q[0] = a->q[0];
x->q[1] = a->q[1];
}
static inline void XOR(v256 *x, const v256 *a)
{
x->q[0] ^= a->q[0];
x->q[1] ^= a->q[1];
}
#if defined(__arm__)
extern void UADD(v256 *a, v256 *b);
#elif defined(__x86_64__)//0
static inline
void UADD(v256 *a, v256 *b)
{
uint64_t ac;
__asm volatile (
" movq (%1), %0 \n" // ac = a[i]
" addq (%2), %0 \n" // ac = a + b[i]
" movq %0, (%1) \n" // r[i]=ac
" movq 8(%1), %0 \n" // ac = a[i]
" adcq 8(%2), %0 \n" // ac = a + b[i]
" movq %0, 8(%1) \n" // r[i]=ac
" movq 16(%1), %0 \n" // ac = a[i]
" adcq 16(%2), %0 \n" // ac = a + b[i]
" movq %0, 16(%1) \n" // r[i]=ac
" movq 24(%1), %0 \n" // ac = a[i]
" adcq 24(%2), %0 \n" // ac = a + b[i]
" movq %0, 24(%1) \n" // r[i]=ac
:"=&r"(ac)
:"r"(a), "r"(b)
:"cc","memory"
);
}
#elif defined(__i386__)
static
void UADD(v256 *a, v256 *b)
{
uint32_t ac;
__asm volatile (
" movl (%1), %0 \n" // ac = a[i]
" addl (%2), %0 \n" // ac = a + b[i]
" movl %0, (%1) \n" // r[i]=ac
" movl 4(%1), %0 \n" // ac = a[i]
" adcl 4(%2), %0 \n" // ac = a + b[i]
" movl %0, 4(%1) \n" // r[i]=ac
" movl 8(%1), %0 \n" // ac = a[i]
" adcl 8(%2), %0 \n" // ac = a + b[i]
" movl %0, 8(%1) \n" // r[i]=ac
" movl 12(%1), %0 \n" // ac = a[i]
" adcl 12(%2), %0 \n" // ac = a + b[i]
" movl %0, 12(%1) \n" // r[i]=ac
" movl 16(%1), %0 \n" // ac = a[i]
" adcl 16(%2), %0 \n" // ac = a + b[i]
" movl %0, 16(%1) \n" // r[i]=ac
" movl 20(%1), %0 \n" // ac = a[i]
" adcl 20(%2), %0 \n" // ac = a + b[i]
" movl %0, 20(%1) \n" // r[i]=ac
" movl 24(%1), %0 \n" // ac = a[i]
" adcl 24(%2), %0 \n" // ac = a + b[i]
" movl %0, 24(%1) \n" // r[i]=ac
" movl 28(%1), %0 \n" // ac = a[i]
" adcl 28(%2), %0 \n" // ac = a + b[i]
" movl %0, 28(%1) \n" // r[i]=ac
:"=&r"(ac)
:"r"(a), "r"(b)
:"cc","memory"
);
}
/*
#define ADD(a,b) ({ register uint32_t ac;\
asm ("addl %1, %0 \n" :"=&r"(ac):"g"(b),"0"(a) :"cc" );\
ac;})
#define ADC(a,b) ({ register uint32_t ac;\
asm ("adcl %1, %0 \n" :"=&r"(ac):"g"(b),"0"(a) :"cc" );\
ac;})
static void UADD(v256 *a, v256 *b)
{
a->s[0] = ADD(a->s[0],b->s[0]);
a->s[1] = ADC(a->s[1],b->s[1]);
a->s[2] = ADC(a->s[2],b->s[2]);
a->s[3] = ADC(a->s[3],b->s[3]);
a->s[4] = ADC(a->s[4],b->s[4]);
a->s[5] = ADC(a->s[5],b->s[5]);
a->s[6] = ADC(a->s[6],b->s[6]);
a->s[7] = ADC(a->s[7],b->s[7]);
}
*/
#elif 0
void UADD(v256 *r, v256 *a)
{
uint64_t c;
c =(uint64_t)r->s[0]+a->s[0];
r->s[0]=c;
c>>=32;
c+=(uint64_t)r->s[1]+a->s[1];
r->s[1]=c;
c>>=32;
c+=(uint64_t)r->s[2]+a->s[2];
r->s[2]=c;
c>>=32;
c+=(uint64_t)r->s[3]+a->s[3];
r->s[3]=c;
c>>=32;
c+=(uint64_t)r->s[4]+a->s[4];
r->s[4]=c;
c>>=32;
c+=(uint64_t)r->s[5]+a->s[5];
r->s[5]=c;
c>>=32;
c+=(uint64_t)r->s[6]+a->s[6];
r->s[6]=c;
c>>=32;
c+=(uint64_t)r->s[7]+a->s[7];
r->s[7]=c;
}
#else
//static inline
void UADD(v256 *r, const v256 *a)
{
int i;
uint64_t cy = 0;
for (i=0; i<8; i++)
{ register uint64_t t = (uint64_t)r->s[i] + a->s[i] + cy;
r->s[i] = t;
cy = t >> 32;
}
// return cy;
}
#endif
static
void A(v256 *x)
{
#ifdef __clang__
v2di v0 = __builtin_shufflevector(x->q[0],x->q[1], 1,2);
v2di v1 = __builtin_shufflevector(x->q[1],x->q[0], 1,2);
#else
v2di v0 = __builtin_shuffle(x->q[0],x->q[1], (v2di){1,2});
v2di v1 = __builtin_shuffle(x->q[1],x->q[0], (v2di){1,2});
#endif // __clang__
v1[1]^=v0[0];
x->q[0] = v0;
x->q[1] = v1;
}
static
void AA(v256 *x)
{
v2di z = x->q[0];
x->q[0] = x->q[1];
#ifdef __clang__
x->q[1] = z ^ __builtin_shufflevector(z,x->q[1], 1,2);//(v2di){z[1],x->q[0][0]};
#else
x->q[1] = z ^ __builtin_shuffle(z,x->q[1], (v2di){1,2});//(v2di){z[1],x->q[0][0]};
#endif // __clang__
}
#if 0 // это тест который делает тоже самое переставляет байты
void __attribute__((constructor)) test2()
{
v256 x, y, z = {.q={
[0] = {0x0807060504030201ULL, 0x1817161514131211ULL},
[1] = {0x2827262524232221ULL, 0x3837363534333231ULL}}};
y.q[0] = __builtin_ia32_punpcklbw128(z.q[0],z.q[1]);
y.q[1] = __builtin_ia32_punpckhbw128(z.q[0],z.q[1]);
x.q[0] = __builtin_ia32_punpcklbw128(y.q[0],y.q[1]);
x.q[1] = __builtin_ia32_punpckhbw128(y.q[0],y.q[1]);
int i;
for (i=0; i<32; i++)
printf (" %02X", x.b[i]);
_Exit(0);
}
#endif
static void XP(const v256 *x, const v256 *c, v256 *a)
{
register v256 z;
z.q[0] = x->q[0] ^ c->q[0];
z.q[1] = x->q[1] ^ c->q[1];
#ifdef __clang__
a->q[0] = (v2di)__builtin_shufflevector((v16qi)z.q[0],(v16qi)z.q[1], 0, 8,16,24, 1, 9,17,25, 2,10,18,26, 3,11,19,27);
a->q[1] = (v2di)__builtin_shufflevector((v16qi)z.q[0],(v16qi)z.q[1], 4,12,20,28, 5,13,21,29, 6,14,22,30, 7,15,23,31);
#else
a->q[0] = (v2di)__builtin_shuffle((v16qi)z.q[0],(v16qi)z.q[1], (v16qi){ 0, 8,16,24, 1, 9,17,25, 2,10,18,26, 3,11,19,27});
a->q[1] = (v2di)__builtin_shuffle((v16qi)z.q[0],(v16qi)z.q[1], (v16qi){ 4,12,20,28, 5,13,21,29, 6,14,22,30, 7,15,23,31});
#endif // __clang__
}
#if 0
void print_K(uint64_t * K, char* idx)
{
printf("%s = ", idx);
int i;
for (i=0; i<4; i++){
if ((i&0x1)==0x0) printf("\t");
printf(" %08X %08X", (uint32_t)(K[3-i]>>32), (uint32_t)K[3-i]);
if ((i&0x1)==0x1) printf("\n");
}
}
#endif
/* Part of GOST 28147 algorithm moved into separate function */
static inline
uint32_t f(gost_ctx *c, uint32_t x)
{
const uint8_t* k87 = (uint8_t*)c->k87;
const uint8_t* k65 = (uint8_t*)c->k65;
const uint8_t* k43 = (uint8_t*)c->k43;
const uint8_t* k21 = (uint8_t*)c->k21;
uint32_t h = x>>16;
x = k87[h>>8 & 0xFF]<<24 | k65[h & 0xFF]<<16 | k43[x>>8 & 0xFF]<<8 | k21[x & 0xFF];
return x<<11 | x>>(32-11);
}
/*
static inline uint32_t f_(gost_ctx *cc, uint32_t x)
{
const gost_subst_block *c = &GostR3411_94_CryptoProParamSet;
x = c->k8[x>>28 & 0xF]<<28 | c->k7[x>>24 & 0xF]<<24 |
c->k6[x>>20 & 0xF]<<20 | c->k5[x>>16 & 0xF]<<16 |
c->k4[x>>12 & 0xF]<<12 | c->k3[x>>8 & 0xF]<<8 |
c->k2[x>>4 & 0xF]<<4 | c->k1[x & 0xF] ;
return x<<11 | x>>(32-11);
}*/
/* Low-level encryption routine - encrypts one 64 bit block*/
static v4si Enc(gost_ctx *c, const v256 * k, v4si in)
{
const v4si s0 = (v4si)k->q[0];
const v4si s1 = (v4si)k->q[1];
// register uint32_t n1,n2;
// n1 = in[0], n2 = in[1];//>>32; /* As named in the GOST */
/* Instead of swapping halves, swap names each round */
in[1] ^= f(c,in[0]+s0[0]); in[0] ^= f(c,in[1]+s0[1]);
in[1] ^= f(c,in[0]+s0[2]); in[0] ^= f(c,in[1]+s0[3]);
in[1] ^= f(c,in[0]+s1[0]); in[0] ^= f(c,in[1]+s1[1]);
in[1] ^= f(c,in[0]+s1[2]); in[0] ^= f(c,in[1]+s1[3]);
in[1] ^= f(c,in[0]+s0[0]); in[0] ^= f(c,in[1]+s0[1]);
in[1] ^= f(c,in[0]+s0[2]); in[0] ^= f(c,in[1]+s0[3]);
in[1] ^= f(c,in[0]+s1[0]); in[0] ^= f(c,in[1]+s1[1]);
in[1] ^= f(c,in[0]+s1[2]); in[0] ^= f(c,in[1]+s1[3]);
in[1] ^= f(c,in[0]+s0[0]); in[0] ^= f(c,in[1]+s0[1]);
in[1] ^= f(c,in[0]+s0[2]); in[0] ^= f(c,in[1]+s0[3]);
in[1] ^= f(c,in[0]+s1[0]); in[0] ^= f(c,in[1]+s1[1]);
in[1] ^= f(c,in[0]+s1[2]); in[0] ^= f(c,in[1]+s1[3]);
in[1] ^= f(c,in[0]+s1[3]); in[0] ^= f(c,in[1]+s1[2]);
in[1] ^= f(c,in[0]+s1[1]); in[0] ^= f(c,in[1]+s1[0]);
in[1] ^= f(c,in[0]+s0[3]); in[0] ^= f(c,in[1]+s0[2]);
in[1] ^= f(c,in[0]+s0[1]); in[0] ^= f(c,in[1]+s0[0]);
return (v4si){in[2],in[3],in[1],in[0]};
// return ((uint64_t)n1)<<32 | n2;
}
/*
static inline uint16_t do_phi1(v256 *x)
{// не использует sse
return x->h[0] ^ x->h[1] ^ x->h[2] ^ x->h[3] ^ x->h[12] ^ x->h[15];
// return x->d[0] ^ x->d[0]>>16 ^ x->d[0]>>32 ^ x->d[0]>>48 ^ x->d[3] ^ x->d[3]>>48;
} */
/*
void do_phi4(uint64_t *x)
{
uint16_t* e = (void*)x;
e[0] ^= e[1] ^ e[2] ^ e[3] ^ e[12] ^ e[15];
e[1] ^= e[2] ^ e[3] ^ e[4] ^ e[13] ^ e[0];
e[2] ^= e[3] ^ e[4] ^ e[5] ^ e[14] ^ e[1];
e[3] ^= e[4] ^ e[5] ^ e[6] ^ e[15] ^ e[2];
} */
static
void R12(v256 * x)
{
v8hi h0 = (v8hi)x->q[0];
v8hi h1 = (v8hi)x->q[1];
v8hi hz = (v8hi){0};
v8hi v0,v1;
#ifdef __clang__
v0 = __builtin_shufflevector(h0,hz,8,8,8,8,0,0,0,0);
v0^= __builtin_shufflevector(h0,hz,8,8,8,8,2,8,2,2);
v0^= __builtin_shufflevector(h0,hz,8,8,8,8,3,4,3,4);
v0^= __builtin_shufflevector(h0,hz,8,8,8,8,1,8,5,6);
v0^= __builtin_shufflevector(hz,h1,12,0,0,0,12,12,12,12);
v0^= __builtin_shufflevector(hz,h1,0,13,0,0, 0,13,13,13);
v0^= __builtin_shufflevector(hz,h1,0,0,14,0, 0, 0,14,14);
v0^= __builtin_shufflevector(hz,h1,0,0,0,15,15,15,15, 0);
x->q[0] = (v2di)v0;
v1 = __builtin_shufflevector(h0,h0,1,0,1,0,0,1,2,0);
v1^= __builtin_shufflevector(h0,h0,3,1,2,1,3,4,5,1);
v1^= __builtin_shufflevector(h0,hz,5,3,4,5,6,7,8,2);
v1^= __builtin_shufflevector(h0,h1, 7,4,5,6,7, 8,8,6);
v1^= __builtin_shufflevector(h0,h1,13,6,7,8,9,10,9,9);
v1^= __builtin_shufflevector(hz,h1,0, 8,9,10,11,12,11,10);
v1^= __builtin_shufflevector(hz,h1,0,12,0,12,12,13,13, 0);
v1^= __builtin_shufflevector(hz,h1,14,14,13,14,13,14,14,14);
v1^= __builtin_shufflevector(hz,h1,15, 0,15,15, 0, 0,15, 0);
x->q[1] = (v2di)v1;
#else
v0 = __builtin_shuffle(h0,hz,(v8hi){8,8,8,8,0,0,0,0});
// v0 = __builtin_shuffle(h0,hz,(v8hi){-1,-1,-1,-1,0,0,0,0});
v0^= __builtin_shuffle(h0,hz,(v8hi){8,8,8,8,2,8,2,2});
// v0^= __builtin_shuffle(h0,hz,(v8hi){-1,-1,-1,-1,2,-1,2,2});
v0^= __builtin_shuffle(h0,hz,(v8hi){8,8,8,8,3,4,3,4});
v0^= __builtin_shuffle(h0,hz,(v8hi){8,8,8,8,1,8,5,6});
v0^= __builtin_shuffle(hz,h1,(v8hi){12,0,0,0,12,12,12,12});
v0^= __builtin_shuffle(hz,h1,(v8hi){0,13,0,0, 0,13,13,13});
v0^= __builtin_shuffle(hz,h1,(v8hi){0,0,14,0, 0, 0,14,14});
v0^= __builtin_shuffle(hz,h1,(v8hi){0,0,0,15,15,15,15, 0});
x->q[0] = (v2di)v0;
v1 = __builtin_shuffle(h0,(v8hi){1,0,1,0,0,1,2,0});
v1^= __builtin_shuffle(h0,(v8hi){3,1,2,1,3,4,5,1});
v1^= __builtin_shuffle(h0,hz,(v8hi){5,3,4,5,6,7,8,2});
v1^= __builtin_shuffle(h0,h1,(v8hi){7,4,5,6,7,8,8,6});
v1^= __builtin_shuffle(h0,h1,(v8hi){13,6,7,8,9,10,9,9});
v1^= __builtin_shuffle(hz,h1,(v8hi){ 0,8,9,10,11,12,11,10});
v1^= __builtin_shuffle(hz,h1,(v8hi){0,12,0,12,12,13,13, 0});
v1^= __builtin_shuffle(hz,h1,(v8hi){14,14,13,14,13,14,14,14});
v1^= __builtin_shuffle(hz,h1,(v8hi){15, 0,15,15, 0, 0,15, 0});
x->q[1] = (v2di)v1;
#endif
// register v256 y = *x;
/*
x->d[0] = y.d[3];
x->h[ 4] = y.h[ 0] ^ y.h[ 1] ^ y.h[ 2] ^ y.h[ 3] ^ y.h[12] ^ y.h[15];
x->h[ 5] = y.h[ 0] ^ y.h[ 4] ^ y.h[12] ^ y.h[13] ^ y.h[15];
x->h[ 6] = y.h[ 0] ^ y.h[ 2] ^ y.h[ 3] ^ y.h[ 5] ^ y.h[12] ^ y.h[13] ^ y.h[14] ^ y.h[15];
x->h[ 7] = y.h[ 0] ^ y.h[ 2] ^ y.h[ 4] ^ y.h[ 6] ^ y.h[12] ^ y.h[13] ^ y.h[14];
x->h[ 8] = y.h[ 1] ^ y.h[ 3] ^ y.h[ 5] ^ y.h[ 7] ^ y.h[13] ^ y.h[14] ^ y.h[15];
x->h[ 9] = y.h[ 0] ^ y.h[ 1] ^ y.h[ 3] ^ y.h[ 4] ^ y.h[ 6] ^ y.h[ 8] ^ y.h[12] ^ y.h[14];
x->h[10] = y.h[ 1] ^ y.h[ 2] ^ y.h[ 4] ^ y.h[ 5] ^ y.h[ 7] ^ y.h[ 9] ^ y.h[13] ^ y.h[15];
x->h[11] = y.h[ 0] ^ y.h[ 1] ^ y.h[ 5] ^ y.h[ 6] ^ y.h[ 8] ^ y.h[10] ^ y.h[12] ^ y.h[14] ^ y.h[15];
x->h[12] = y.h[ 0] ^ y.h[ 3] ^ y.h[ 6] ^ y.h[ 7] ^ y.h[ 9] ^ y.h[11] ^ y.h[12] ^ y.h[13];
x->h[13] = y.h[ 1] ^ y.h[ 4] ^ y.h[ 7] ^ y.h[ 8] ^ y.h[10] ^ y.h[12] ^ y.h[13] ^ y.h[14];
x->h[14] = y.h[ 2] ^ y.h[ 5] ^ y.h[ 8] ^ y.h[ 9] ^ y.h[11] ^ y.h[13] ^ y.h[14] ^ y.h[15];
x->h[15] = y.h[ 0] ^ y.h[ 1] ^ y.h[ 2] ^ y.h[ 6] ^ y.h[ 9] ^ y.h[10] ^ y.h[14];
*/
}
#if 0
static void do_phi12(v256 *x)
{
x->h[0] ^= x->h[1] ^ x->h[2] ^ x->h[3] ^ x->h[12]^ x->h[15];
x->h[1] ^= x->h[2] ^ x->h[3] ^ x->h[4] ^ x->h[13]^ x->h[0];
x->h[2] ^= x->h[3] ^ x->h[4] ^ x->h[5] ^ x->h[14]^ x->h[1];
x->h[3] ^= x->h[4] ^ x->h[5] ^ x->h[6] ^ x->h[15]^ x->h[2];
x->h[4] ^= x->h[5] ^ x->h[6] ^ x->h[7] ^ x->h[0] ^ x->h[3];
x->h[5] ^= x->h[6] ^ x->h[7] ^ x->h[8] ^ x->h[1] ^ x->h[4];
x->h[6] ^= x->h[7] ^ x->h[8] ^ x->h[9] ^ x->h[2] ^ x->h[5];
x->h[7] ^= x->h[8] ^ x->h[9] ^ x->h[10]^ x->h[3] ^ x->h[6];
x->h[8] ^= x->h[9] ^ x->h[10]^ x->h[11]^ x->h[4] ^ x->h[7];
x->h[9] ^= x->h[10]^ x->h[11]^ x->h[12]^ x->h[5] ^ x->h[8];
x->h[10]^= x->h[11]^ x->h[12]^ x->h[13]^ x->h[6] ^ x->h[9];
x->h[11]^= x->h[12]^ x->h[13]^ x->h[14]^ x->h[7] ^ x->h[10];
}
#endif
#if 0
static void do_phi16(v256 *x)
{
// uint16_t* e = (void*)x;
x->h[0] ^= x->h[1] ^ x->h[2] ^ x->h[3] ^ x->h[12]^ x->h[15];
x->h[1] ^= x->h[2] ^ x->h[3] ^ x->h[4] ^ x->h[13]^ x->h[0];
x->h[2] ^= x->h[3] ^ x->h[4] ^ x->h[5] ^ x->h[14]^ x->h[1];
x->h[3] ^= x->h[4] ^ x->h[5] ^ x->h[6] ^ x->h[15]^ x->h[2];
x->h[4] ^= x->h[5] ^ x->h[6] ^ x->h[7] ^ x->h[0] ^ x->h[3];
x->h[5] ^= x->h[6] ^ x->h[7] ^ x->h[8] ^ x->h[1] ^ x->h[4];
x->h[6] ^= x->h[7] ^ x->h[8] ^ x->h[9] ^ x->h[2] ^ x->h[5];
x->h[7] ^= x->h[8] ^ x->h[9] ^ x->h[10]^ x->h[3] ^ x->h[6];
x->h[8] ^= x->h[9] ^ x->h[10]^ x->h[11]^ x->h[4] ^ x->h[7];
x->h[9] ^= x->h[10]^ x->h[11]^ x->h[12]^ x->h[5] ^ x->h[8];
x->h[10]^= x->h[11]^ x->h[12]^ x->h[13]^ x->h[6] ^ x->h[9];
x->h[11]^= x->h[12]^ x->h[13]^ x->h[14]^ x->h[7] ^ x->h[10];
x->h[12]^= x->h[13]^ x->h[14]^ x->h[15]^ x->h[8] ^ x->h[11];
x->h[13]^= x->h[14]^ x->h[15]^ x->h[0] ^ x->h[9] ^ x->h[12];
x->h[14]^= x->h[15]^ x->h[0] ^ x->h[1] ^ x->h[10]^ x->h[13];
x->h[15]^= x->h[0] ^ x->h[1] ^ x->h[2] ^ x->h[11]^ x->h[14];
}
#endif
static
void R61(v256 *x, v256 *y)
{
v8hi h0 = (v8hi)x->q[0];
v8hi h1 = (v8hi)x->q[1];
v8hi hz = (v8hi){0};
v8hi v0,v1;
#ifdef __clang__
v0 = __builtin_shufflevector(h0,h0,1,0,1,0,0,0,1,0);
v0^= __builtin_shufflevector(h0,h0,3,1,2,1,3,2,3,1);
v0^= __builtin_shufflevector(h0,h0,7,3,4,5,6,3,4,3);
v0^= __builtin_shufflevector(h0,hz,8,4,5,6,7,4,5,4);
v0^= __builtin_shufflevector(h0,h1,10,8,9,10,11,7,8,5);
v0^= __builtin_shufflevector(h0,h1,11,11,12,12,12,8,9,6);
v0^= __builtin_shufflevector(hz,h1,13,14,15,13,13,13,14,9);
v0^= __builtin_shufflevector(hz,h1,14,0,0,15,14,14,15,10);
v0^= __builtin_shufflevector(hz,h1,15,0,0,0,15,0,0,12);
y->q[0] = (v2di)v0;
v1 = __builtin_shufflevector(h0,h0, 1, 2, 3,0,0,1,0,1);
v1^= __builtin_shufflevector(h0,h0, 2, 3, 4,1,4,5,1,2);
v1^= __builtin_shufflevector(h0,h0, 4, 5, 6,2,5,6,3,4);
v1^= __builtin_shufflevector(h0,h0, 5, 6, 7,3,6,7,6,7);
v1^= __builtin_shufflevector(h0,h1, 6, 7, 8,4,8,9,7,8);
v1^= __builtin_shufflevector(h0,h1, 7, 8, 9,5,9,10,8,9);
v1^= __builtin_shufflevector(h0,h1,10,11,12,7,10,11,10,11);
v1^= __builtin_shufflevector(hz,h1,11,12,13,8,11,12,11,12);
v1^= __builtin_shufflevector(hz,h1,13,14,15,9,12,13,13,14);
v1^= __builtin_shufflevector(hz,h1, 0, 0,0,10,13,14,14,15);
v1^= __builtin_shufflevector(hz,h1, 0, 0,0,12,14,15, 0, 0);
v1^= __builtin_shufflevector(hz,h1, 0, 0,0,13, 0, 0, 0, 0);
v1^= __builtin_shufflevector(hz,h1, 0, 0,0,14, 0, 0, 0, 0);
v1^= __builtin_shufflevector(hz,h1, 0, 0,0,15, 0, 0, 0, 0);
y->q[1] = (v2di)v1;
#else
v0 = __builtin_shuffle(h0, (v8hi){1,0,1,0,0,0,1,0});
v0^= __builtin_shuffle(h0, (v8hi){3,1,2,1,3,2,3,1});
v0^= __builtin_shuffle(h0, (v8hi){7,3,4,5,6,3,4,3});
v0^= __builtin_shuffle(h0,hz,(v8hi){8,4,5,6,7,4,5,4});
v0^= __builtin_shuffle(h0,h1,(v8hi){10,8,9,10,11,7,8,5});
v0^= __builtin_shuffle(h0,h1,(v8hi){11,11,12,12,12,8,9,6});
v0^= __builtin_shuffle(hz,h1,(v8hi){13,14,15,13,13,13,14,9});
v0^= __builtin_shuffle(hz,h1,(v8hi){14,0,0,15,14,14,15,10});
v0^= __builtin_shuffle(hz,h1,(v8hi){15,0,0,0,15,0,0,12});
y->q[0] = (v2di)v0;
v1 = __builtin_shuffle(h0, (v8hi){ 1, 2, 3,0,0,1,0,1});
v1^= __builtin_shuffle(h0, (v8hi){ 2, 3, 4,1,4,5,1,2});
v1^= __builtin_shuffle(h0, (v8hi){ 4, 5, 6,2,5,6,3,4});
v1^= __builtin_shuffle(h0, (v8hi){ 5, 6, 7,3,6,7,6,7});
v1^= __builtin_shuffle(h0,h1,(v8hi){ 6, 7, 8,4,8,9,7,8});
v1^= __builtin_shuffle(h0,h1,(v8hi){ 7, 8, 9,5,9,10,8,9});
v1^= __builtin_shuffle(h0,h1,(v8hi){10,11,12,7,10,11,10,11});
v1^= __builtin_shuffle(hz,h1,(v8hi){11,12,13,8,11,12,11,12});
v1^= __builtin_shuffle(hz,h1,(v8hi){13,14,15,9,12,13,13,14});
v1^= __builtin_shuffle(hz,h1,(v8hi){ 0, 0,0,10,13,14,14,15});
v1^= __builtin_shuffle(hz,h1,(v8hi){ 0, 0,0,12,14,15, 0, 0});
v1^= __builtin_shuffle(hz,h1,(v8hi){ 0, 0,0,13, 0, 0, 0, 0});
v1^= __builtin_shuffle(hz,h1,(v8hi){ 0, 0,0,14, 0, 0, 0, 0});
v1^= __builtin_shuffle(hz,h1,(v8hi){ 0, 0,0,15, 0, 0, 0, 0});
y->q[1] = (v2di)v1;
#endif
/*
y->h[ 0] = x->h[ 1] ^ x->h[ 3] ^ x->h[ 7] ^ x->h[10] ^ x->h[11] ^ x->h[13] ^ x->h[14] ^ x->h[15];
y->h[ 1] = x->h[ 0] ^ x->h[ 1] ^ x->h[ 3] ^ x->h[ 4] ^ x->h[ 8] ^ x->h[11] ^ x->h[14];
y->h[ 2] = x->h[ 1] ^ x->h[ 2] ^ x->h[ 4] ^ x->h[ 5] ^ x->h[ 9] ^ x->h[12] ^ x->h[15];
y->h[ 3] = x->h[ 0] ^ x->h[ 1] ^ x->h[ 5] ^ x->h[ 6] ^ x->h[10] ^ x->h[12] ^ x->h[13] ^ x->h[15];
y->h[ 4] = x->h[ 0] ^ x->h[ 3] ^ x->h[ 6] ^ x->h[ 7] ^ x->h[11] ^ x->h[12] ^ x->h[13] ^ x->h[14] ^ x->h[15];
y->h[ 5] = x->h[ 0] ^ x->h[ 2] ^ x->h[ 3] ^ x->h[ 4] ^ x->h[ 7] ^ x->h[ 8] ^ x->h[13] ^ x->h[14];
y->h[ 6] = x->h[ 1] ^ x->h[ 3] ^ x->h[ 4] ^ x->h[ 5] ^ x->h[ 8] ^ x->h[ 9] ^ x->h[14] ^ x->h[15];
y->h[ 7] = x->h[ 0] ^ x->h[ 1] ^ x->h[ 3] ^ x->h[ 4] ^ x->h[ 5] ^ x->h[ 6] ^ x->h[ 9] ^ x->h[10] ^ x->h[12];
y->h[ 8] = x->h[ 1] ^ x->h[ 2] ^ x->h[ 4] ^ x->h[ 5] ^ x->h[ 6] ^ x->h[ 7] ^ x->h[10] ^ x->h[11] ^ x->h[13];
y->h[ 9] = x->h[ 2] ^ x->h[ 3] ^ x->h[ 5] ^ x->h[ 6] ^ x->h[ 7] ^ x->h[ 8] ^ x->h[11] ^ x->h[12] ^ x->h[14];
y->h[10] = x->h[ 3] ^ x->h[ 4] ^ x->h[ 6] ^ x->h[ 7] ^ x->h[ 8] ^ x->h[ 9] ^ x->h[12] ^ x->h[13] ^ x->h[15];
y->h[11] = x->h[ 0] ^ x->h[ 1] ^ x->h[ 2] ^ x->h[ 3] ^ x->h[ 4] ^ x->h[ 5] ^ x->h[ 7] ^ x->h[ 8] ^ x->h[ 9] ^ x->h[10] ^ x->h[12] ^ x->h[13] ^ x->h[14] ^ x->h[15];
y->h[12] = x->h[ 0] ^ x->h[ 4] ^ x->h[ 5] ^ x->h[ 6] ^ x->h[ 8] ^ x->h[ 9] ^ x->h[10] ^ x->h[11] ^ x->h[12] ^ x->h[13] ^ x->h[14];
y->h[13] = x->h[ 1] ^ x->h[ 5] ^ x->h[ 6] ^ x->h[ 7] ^ x->h[ 9] ^ x->h[10] ^ x->h[11] ^ x->h[12] ^ x->h[13] ^ x->h[14] ^ x->h[15];
y->h[14] = x->h[ 0] ^ x->h[ 1] ^ x->h[ 3] ^ x->h[ 6] ^ x->h[ 7] ^ x->h[ 8] ^ x->h[10] ^ x->h[11] ^ x->h[13] ^ x->h[14];
y->h[15] = x->h[ 1] ^ x->h[ 2] ^ x->h[ 4] ^ x->h[ 7] ^ x->h[ 8] ^ x->h[ 9] ^ x->h[11] ^ x->h[12] ^ x->h[14] ^ x->h[15];
*/
}
static
void R(v256 *x)
{
// const uint16_t z =do_phi1(x);
// register v8hi q = (v8hi){z,0};
v8hi h0 = (v8hi)x->q[0];
v8hi h1 = (v8hi)x->q[1];
v8hi hz = (v8hi){0};
v8hi v0,v1;
// v8hi z = (v8hi){0};
#ifdef __clang__
v0 = __builtin_shufflevector(h0,h1,1,2,3,4,5,6,7,8);
v1 = __builtin_shufflevector(h1,h0,1,2,3,4,5,6,7,8);
v1^= __builtin_shufflevector(hz,v0,1,2,3,4,5,6,7,8);
v1^= __builtin_shufflevector(h0,h0,0,0,0,0,0,0,0,2);
v1^= __builtin_shufflevector(h0,h0,0,0,0,0,0,0,0,3);
v1^= __builtin_shufflevector(h1,h1,0,0,0,0,0,0,0,4);
v1^= __builtin_shufflevector(h1,h1,0,0,0,0,0,0,0,7);
#else
v0 = __builtin_shuffle(h0, h1,(v8hi){1,2,3,4,5,6,7,8});
v1 = __builtin_shuffle(h1, h0,(v8hi){1,2,3,4,5,6,7,8});
v1^= __builtin_shuffle(hz, v0,(v8hi){1,2,3,4,5,6,7,8});
v1^= __builtin_shuffle(h0, (v8hi){0,0,0,0,0,0,0,2});
v1^= __builtin_shuffle(h0, (v8hi){0,0,0,0,0,0,0,3});
v1^= __builtin_shuffle(h1, (v8hi){0,0,0,0,0,0,0,4});
v1^= __builtin_shuffle(h1, (v8hi){0,0,0,0,0,0,0,7});
#endif
x->q[0]=(v2di)v0;
x->q[1]=(v2di)v1;
// x->q[1] = x->h[0] ^ x->h[1] ^ x->h[2] ^ x->h[3] ^ x->h[12] ^ x->h[15];
}
#if 0
static inline void R4(v256 *x)
{
do_phi4(x);
v2di y = (v2di){x->d[1], x->d[2]};
x->q[1] = (v2di){x->d[3], x->d[0]};
x->q[0] = y;
/* const uint64_t z = x[0];
x[0] = x[1];
x[1] = x[2];
x[2] = x[3];
x[3] = z ;*/
}
#endif
#if 0
static inline void R12_(v256 *x)
{
do_phi12(x);
v2di y = (v2di){x->d[3], x->d[0]};
x->q[1] = (v2di){x->d[1], x->d[2]};
x->q[0] = y;
/* const uint64_t z = x[3];
x[3] = x[2];
x[2] = x[1];
x[1] = x[0];
x[0] = z;*/
}
#endif
#if 0
static void R60(v256 *x)
{
do_phi16(x);
do_phi16(x);
do_phi16(x);
do_phi12(x);
// do_phi60(x);
#if 0// defined (__AVX__)
x->v = (v4di){x->d[3], x->d[0], x->d[1], x->d[2]};
#elif defined (__SSE2__)
v2di y = (v2di){x->d[3], x->d[0]};
x->q[1] = (v2di){x->d[1], x->d[2]};
x->q[0] = y;
#else
const uint64_t z = x->d[3];
x->d[3] = x->d[2];
x->d[2] = x->d[1];
x->d[1] = x->d[0];
x->d[0] = z;
#endif
}
#endif
static const v256 C1 = {.q = {
[0] = {0x000000FFFF00FFFFULL, 0xFF00FF00FF00FF00ULL},
[1] = {0x00FF00FF00FF00FFULL, 0xFF0000FF00FFFF00ULL}}};
static void gost_step(gost_ctx *c, v256* H, v256* M)
{
v256 V,U,K,S;
v4si s0,s1;
MOV(&U,H); MOV(&V, M);
XP(&U,&V,&K);
s0 = (v4si) H->q[0];
s0 = Enc(c,&K,s0);
A(&U); AA(&V); XP(&U,&V,&K);
#ifdef __clang__
s0 = __builtin_shufflevector((v4si) H->q[0], s0, 2,3,6,7);
#else
s0 = __builtin_shuffle((v4si) H->q[0], s0, (v4si){2,3,6,7});
#endif
S.q[0] = (v2di) Enc(c,&K,s0);
XOR(&U, &C1);
A(&U); AA(&V); XP(&U,&V,&K);
s1 = (v4si) H->q[1];
s1 = Enc(c,&K,s1);
A(&U); AA(&V); XP(&U,&V,&K);
#ifdef __clang__
s1 = __builtin_shufflevector((v4si) H->q[1], s1, 2,3,6,7);
#else
s1 = __builtin_shuffle((v4si) H->q[1], s1, (v4si){2,3,6,7});
#endif
S.q[1] = (v2di)Enc(c,&K,s1);
R12(&S);
XOR(&S, M);
R(&S);
XOR(&S, H);
R61(&S, H);
// R(&S);
// MOV(H,&S);
}
#if 0
static int* stack=NULL;
static void __attribute__((constructor)) init_stack(){
int addr;
stack = &addr;
}
static void check_stack(){
int addr;
printf("Stack size: %d\n",&addr-stack);
}
#endif
/* Initalize context. Provides default value for subst_block */
static void gost_init(gost_ctx *c, const gost_subst_block *b)
{
// check_stack();
if(!b) {
b=&GostR3411_94_TestParamSet;
}
int i;
v16qi k7 = b->k7;
v16qi k5 = b->k5;
v16qi k3 = b->k3;
v16qi k1 = b->k1;
for (i = 0; i < 16; i++) {
#if 1//def __clang__
uint8_t q;
q = b->k8[i]<<4;
c->k87[i] = (v16qi){q,q,q,q, q,q,q,q, q,q,q,q, q,q,q,q} ^ k7;
q = b->k6[i]<<4;
c->k65[i] = (v16qi){q,q,q,q, q,q,q,q, q,q,q,q, q,q,q,q} ^ k5;
q = b->k4[i]<<4;
c->k43[i] = (v16qi){q,q,q,q, q,q,q,q, q,q,q,q, q,q,q,q} ^ k3;
q = b->k2[i]<<4;
c->k21[i] = (v16qi){q,q,q,q, q,q,q,q, q,q,q,q, q,q,q,q} ^ k1;
#else
v16qi k= {0};
k[0] = b->k8[i]<<4;
k = __builtin_shuffle(k, (v16qi){0});
//__builtin_memset(&k, b->k8[i]<<4 ,16);
c->k87[i] = k ^ k7;
k[0] = b->k6[i]<<4;
k = __builtin_shuffle(k, (v16qi){0});
//__builtin_memset(&k, b->k6[i]<<4 ,16);
c->k65[i] = k ^ k5;
k[0] = b->k4[i]<<4;
k = __builtin_shuffle(k, (v16qi){0});
//__builtin_memset(&k, b->k4[i]<<4 ,16);
c->k43[i] = k ^ k3;
k[0] = b->k2[i]<<4;
k = __builtin_shuffle(k, (v16qi){0});
//__builtin_memset(&k, b->k2[i]<<4 ,16);
c->k21[i] = k ^ k1;
#endif
}
/*
uint32_t x;
for (i = 0; i < 256; i++) {
x = (b->k8[i>>4] <<4 | b->k7 [i &15])<<24;
c->k87[i] = x<<11 | x>>(32-11);
x = (b->k6[i>>4] <<4 | b->k5 [i &15])<<16;
c->k65[i] = x<<11 | x>>(32-11);
x = (b->k4[i>>4] <<4 | b->k3 [i &15])<<8;
c->k43[i] = x<<11 | x>>(32-11);
x = b->k2[i>>4] <<4 | b->k1 [i &15];
c->k21[i] = x<<11 | x>>(32-11);
}
*/
}
typedef struct _Gost94Ctx Gost94Ctx;
struct _Gost94Ctx {
v256 H,M,S;
unsigned int size; // длина данных в битах
unsigned int offset; // длина хеша
unsigned int hlen; // длина хеша
gost_ctx * ctx;
};
static void gost94_init_CP(Gost94Ctx *ctx)
{
if (1){//hash_params[1].ctx== NULL) {
gost_ctx* gct = _aligned_malloc(sizeof(gost_ctx),16);
gost_init(gct, hash_params[1].paramset);
hash_params[1].ctx = gct;
}
ctx->ctx = hash_params[1].ctx; ctx->hlen=32;
CLR(&ctx->H);CLR(&ctx->S);
ctx->size=0, ctx->offset=0;
}
#if 0
static void __attribute__((constructor)) gost94_init_(){
if (hash_params[1].ctx== NULL) {
gost_ctx* gct = _aligned_malloc(sizeof(gost_ctx),16);
// printf("%s: Init paramset\n", __FUNCTION__);
gost_init(gct, hash_params[1].paramset);
// printf("%s: Init paramset done\n", __FUNCTION__);
hash_params[1].ctx = gct;
}
}
#endif // 0
static void gost94_init_T(Gost94Ctx *ctx)
{
if (hash_params[0].ctx== NULL) {
gost_ctx* gct = _aligned_malloc(sizeof(gost_ctx),16);
gost_init(gct, hash_params[0].paramset);
hash_params[0].ctx = gct;
}
ctx->ctx = hash_params[0].ctx; ctx->hlen=32;
CLR(&ctx->H);CLR(&ctx->S);
ctx->size=0, ctx->offset=0;
}
static void gost94_update(Gost94Ctx *ctx, const uint8_t* msg, unsigned int mlen)
{
while (mlen>0)
{
unsigned int len = (mlen>32-ctx->offset)?32-ctx->offset:mlen;
memcpy((uint8_t*)&ctx->M + ctx->offset, msg, len);
ctx->offset+=len;
msg+=len;
mlen-=len;
if (ctx->offset == 32)
{
gost_step(ctx->ctx, &ctx->H, &ctx->M);
UADD(&ctx->S, &ctx->M);
ctx->size += 256;
ctx->offset=0;
}
}
}
static void gost94_final (Gost94Ctx *ctx, uint8_t * tag, unsigned int tlen)
{
if (ctx->offset>0){
__builtin_memset((uint8_t*)&ctx->M + ctx->offset, 0, 32-ctx->offset);
gost_step(ctx->ctx, &ctx->H, &ctx->M);
UADD(&ctx->S, &ctx->M);
ctx->size += ctx->offset<<3;
ctx->offset=0;
}
ctx->M.q[0] = (v2di){ctx->size,0};
ctx->M.q[1] = (v2di){0};
gost_step(ctx->ctx, &ctx->H,&ctx->M);
gost_step(ctx->ctx, &ctx->H,&ctx->S);
if (tag && tlen) __builtin_memcpy(tag, &ctx->H.d[0], tlen);
}
MESSAGE_DIGEST(MD_GOSTR341194_CP){
.id = MD_GOSTR341194_CP,
.name = "GOST R 34.11-94 CryptoPro",
.block_len = 32,
.hash_len = 32,
.ctx_size = sizeof(Gost94Ctx),
.init = (void*)gost94_init_CP,
.update = (void*)gost94_update,
.final = (void*)gost94_final,
};
MESSAGE_DIGEST(MD_GOSTR341194){
.id = MD_GOSTR341194,
.name = "GOST R 34.11-94 Test",
.block_len = 32,
.hash_len = 32,
.ctx_size = sizeof(Gost94Ctx),
.init = (void*)gost94_init_T,
.update = (void*)gost94_update,
.final = (void*)gost94_final,
};
#ifdef DEBUG_GOST
#include "stdio.h"
///
static uint8_t num(uint8_t ch) {
return ch>='A'?ch-'A'+10: ch-'0';
}
#if 0
extern uint8_t* base64_enc(uint8_t *dst, uint8_t *src, int length);
void gost_base64(gost_ctx* ctx, char* message, int length)
{
int len;
uint8_t hash[32];
gost_digest(ctx, message, length, hash);
char b64[48];
base64_enc(b64, hash, 32);
printf("Digest: %s\n", b64);
}
#endif
static void GOST(const MDigest *md, char* message)
{
int len;
char hash[32];
len = strlen(message);
// printf("length = %d\n", len);
// gost_digest2(ctx, (uint8_t*)message, len, (uint8_t*)hash);
digest(md, (uint8_t*)&hash[0], md->hash_len, (uint8_t*)message, len);
char ch;//
if (len>=128) {
ch = message[128];
message[128]= '\0';
}
printf("GOST(\"%s\") = \n",message);
if (len>=128) message[128] = ch;
int i;
for (i=0; i<32; i++){
printf("%02X", (uint8_t)hash[i]);
}
printf("\n");
char b64[48];
base64_enc(b64, hash, 32);
printf("Digest: %s\n", b64);
}
int main (int argc, char *argv[])
{
// gost_ctx cipher_ctx;
// gost_init(&cipher_ctx, NULL);//&GostR3411_94_CryptoProParamSet);
if (argc > 1 ) {
char* filename = argv[1];
FILE* fp = fopen(filename, "r");
char* buf = malloc(2000000+4);
int len = fread(buf, 1, 2000000, fp);
// char* s = &buf[len-1];
// while (s[0]=='\r' || s[0]=='\n' || s[0]==' ') { s--, len--; }
buf[len]='\0';
// gost_init(&cipher_ctx, NULL);
// GOST(&cipher_ctx, buf);
const MDigest *md = digest_select(MD_GOSTR341194_CP);
// gost_init(&cipher_ctx, &GostR3411_94_CryptoProParamSet);
GOST(md, buf);
fclose(fp);
free(buf);
return 0;
}
int len, i;
uint8_t * m;
// uint8_t msg[]="73657479622032333D6874676E656C202C6567617373656D2073692073696854";
/*
len = strlen(msg) >>1;
m=malloc(len);
for (i=0; i<len; i++){
m[i] = num(msg[2*i])<<4 | num(msg[2*i+1]);
}
gost_digest(&cipher_ctx, m, len, NULL);
free(m);
uint8_t msg1[] =
"7365747962203035203D206874676E656C207361682065676"
"17373656D206C616E696769726F206568742065736F70707553";
len = strlen(msg1) >>1;
m=malloc(len);
for (i=0; i<len; i++){
m[i] = num(msg1[2*i])<<4 | num(msg1[2*i+1]);
}
gost_digest(&cipher_ctx, m, len, NULL);
free(m);
*/
const MDigest *md = digest_select(MD_GOSTR341194);
GOST(md, "This is message, length=32 bytes");
GOST(md, "The quick brown fox jumps over the lazy dog");
GOST(md, "The quick brown fox jumps over the lazy cog");
GOST(md, "Suppose the original message has length = 50 bytes");
GOST(md, "");
GOST(md, "a");