@@ -15,18 +15,28 @@ interface
1515
1616type
1717
18+ { $SCOPEDENUMS ON}
19+ TGostSBox = (gsbTestParamSet, gsbCryptoProParamSet);
20+ { $SCOPEDENUMS OFF}
21+
1822 TGost = class sealed(TBlockHash, ICryptoNotBuildIn, ITransformBlock)
1923
2024 strict private
2125
2226 class var
2327
24- FSBox1, FSBox2, FSBox3, FSBox4: THashLibUInt32Array;
28+ FSBox1_Test, FSBox2_Test, FSBox3_Test, FSBox4_Test: THashLibUInt32Array;
29+ FSBox1_CryptoPro, FSBox2_CryptoPro, FSBox3_CryptoPro,
30+ FSBox4_CryptoPro: THashLibUInt32Array;
2531
2632 var
2733 FState, FHash: THashLibUInt32Array;
34+ FSBox1, FSBox2, FSBox3, FSBox4: THashLibUInt32Array;
35+ FSBoxType: TGostSBox;
2836
2937 procedure Compress (APtr: PCardinal);
38+ class procedure ComputeSBoxes (const ASBox: THashLibMatrixUInt32Array;
39+ out ASBox1, ASBox2, ASBox3, ASBox4: THashLibUInt32Array); static;
3040 class constructor Gost();
3141
3242 strict protected
@@ -36,7 +46,7 @@ TGost = class sealed(TBlockHash, ICryptoNotBuildIn, ITransformBlock)
3646 AIndex: Int32); override;
3747
3848 public
39- constructor Create();
49+ constructor Create(ASBoxType: TGostSBox = TGostSBox.gsbTestParamSet );
4050 procedure Initialize (); override;
4151 function Clone (): IHash; override;
4252
@@ -50,7 +60,7 @@ function TGost.Clone(): IHash;
5060var
5161 LHashInstance: TGost;
5262begin
53- LHashInstance := TGost.Create();
63+ LHashInstance := TGost.Create(FSBoxType );
5464 LHashInstance.FState := System.Copy(FState);
5565 LHashInstance.FHash := System.Copy(FHash);
5666 LHashInstance.FBuffer := FBuffer.Clone();
@@ -336,11 +346,60 @@ procedure TGost.Compress(APtr: PCardinal);
336346
337347end ;
338348
339- constructor TGost.Create;
349+ class procedure TGost.ComputeSBoxes (const ASBox: THashLibMatrixUInt32Array;
350+ out ASBox1, ASBox2, ASBox3, ASBox4: THashLibUInt32Array);
351+ var
352+ LIdx, LA, LB: Int32;
353+ ax, bx, cx, dx: UInt32;
354+ begin
355+ System.SetLength(ASBox1, 256 );
356+ System.SetLength(ASBox2, 256 );
357+ System.SetLength(ASBox3, 256 );
358+ System.SetLength(ASBox4, 256 );
359+
360+ LIdx := 0 ;
361+
362+ for LA := 0 to 15 do
363+ begin
364+ ax := ASBox[1 , LA] shl 15 ;
365+ bx := ASBox[3 , LA] shl 23 ;
366+ cx := ASBox[5 , LA];
367+ cx := TBits.RotateRight32(cx, 1 );
368+ dx := ASBox[7 , LA] shl 7 ;
369+
370+ for LB := 0 to 15 do
371+ begin
372+ ASBox1[LIdx] := ax or (ASBox[0 , LB] shl 11 );
373+ ASBox2[LIdx] := bx or (ASBox[2 , LB] shl 19 );
374+ ASBox3[LIdx] := cx or (ASBox[4 , LB] shl 27 );
375+ ASBox4[LIdx] := dx or (ASBox[6 , LB] shl 3 );
376+ System.Inc(LIdx);
377+ end ;
378+ end ;
379+ end ;
380+
381+ constructor TGost.Create(ASBoxType: TGostSBox);
340382begin
341383 Inherited Create(32 , 32 );
342384 System.SetLength(FState, 8 );
343385 System.SetLength(FHash, 8 );
386+ FSBoxType := ASBoxType;
387+ case ASBoxType of
388+ TGostSBox.gsbTestParamSet:
389+ begin
390+ FSBox1 := FSBox1_Test;
391+ FSBox2 := FSBox2_Test;
392+ FSBox3 := FSBox3_Test;
393+ FSBox4 := FSBox4_Test;
394+ end ;
395+ TGostSBox.gsbCryptoProParamSet:
396+ begin
397+ FSBox1 := FSBox1_CryptoPro;
398+ FSBox2 := FSBox2_CryptoPro;
399+ FSBox3 := FSBox3_CryptoPro;
400+ FSBox4 := FSBox4_CryptoPro;
401+ end ;
402+ end ;
344403end ;
345404
346405procedure TGost.Finish ;
@@ -375,9 +434,8 @@ function TGost.GetResult: THashLibByteArray;
375434class constructor TGost.Gost;
376435var
377436 LSBox: THashLibMatrixUInt32Array;
378- LIdx, LA, LB: Int32;
379- ax, bx, cx, dx: UInt32;
380437begin
438+ // DSbox_Test (id-GostR3411-94-TestParamSet)
381439 LSBox := THashLibMatrixUInt32Array.Create(THashLibUInt32Array.Create(4 , 10 , 9 ,
382440 2 , 13 , 8 , 0 , 14 , 6 , 11 , 1 , 12 , 7 , 15 , 5 , 3 ), THashLibUInt32Array.Create(14 ,
383441 11 , 4 , 12 , 6 , 13 , 15 , 10 , 2 , 3 , 8 , 1 , 0 , 7 , 5 , 9 ),
@@ -389,31 +447,22 @@ function TGost.GetResult: THashLibByteArray;
389447 9 , 0 , 10 , 14 , 7 , 6 , 8 , 2 , 12 ), THashLibUInt32Array.Create(1 , 15 , 13 , 0 , 5 ,
390448 7 , 10 , 4 , 9 , 2 , 3 , 14 , 6 , 11 , 8 , 12 ));
391449
392- System.SetLength(FSBox1, 256 );
393- System.SetLength(FSBox2, 256 );
394- System.SetLength(FSBox3, 256 );
395- System.SetLength(FSBox4, 256 );
396-
397- LIdx := 0 ;
398-
399- for LA := 0 to 15 do
400- begin
401- ax := LSBox[1 , LA] shl 15 ;
402- bx := LSBox[3 , LA] shl 23 ;
403- cx := LSBox[5 , LA];
404- cx := TBits.RotateRight32(cx, 1 );
405- dx := LSBox[7 , LA] shl 7 ;
406-
407- for LB := 0 to 15 do
408- begin
409- FSBox1[LIdx] := ax or (LSBox[0 , LB] shl 11 );
410- FSBox2[LIdx] := bx or (LSBox[2 , LB] shl 19 );
411- FSBox3[LIdx] := cx or (LSBox[4 , LB] shl 27 );
412- FSBox4[LIdx] := dx or (LSBox[6 , LB] shl 3 );
413- System.Inc(LIdx);
414- end ;
415- end ;
416-
450+ ComputeSBoxes(LSBox, FSBox1_Test, FSBox2_Test, FSBox3_Test, FSBox4_Test);
451+
452+ // DSbox_A (id-GostR3411-94-CryptoProParamSet)
453+ LSBox := THashLibMatrixUInt32Array.Create(THashLibUInt32Array.Create(10 , 4 , 5 ,
454+ 6 , 8 , 1 , 3 , 7 , 13 , 12 , 14 , 0 , 9 , 2 , 11 , 15 ), THashLibUInt32Array.Create(5 ,
455+ 15 , 4 , 0 , 2 , 13 , 11 , 9 , 1 , 7 , 6 , 3 , 12 , 14 , 10 , 8 ),
456+ THashLibUInt32Array.Create(7 , 15 , 12 , 14 , 9 , 4 , 1 , 0 , 3 , 11 , 5 , 2 , 6 , 10 , 8 ,
457+ 13 ), THashLibUInt32Array.Create(4 , 10 , 7 , 12 , 0 , 15 , 2 , 8 , 14 , 1 , 6 , 5 , 13 ,
458+ 11 , 9 , 3 ), THashLibUInt32Array.Create(7 , 6 , 4 , 11 , 9 , 12 , 2 , 10 , 1 , 8 , 0 ,
459+ 14 , 15 , 13 , 3 , 5 ), THashLibUInt32Array.Create(7 , 6 , 2 , 4 , 13 , 9 , 15 , 0 , 10 ,
460+ 1 , 5 , 11 , 8 , 14 , 12 , 3 ), THashLibUInt32Array.Create(13 , 14 , 4 , 1 , 7 , 0 , 5 ,
461+ 10 , 3 , 12 , 8 , 15 , 6 , 2 , 9 , 11 ), THashLibUInt32Array.Create(1 , 3 , 10 , 9 , 5 ,
462+ 11 , 4 , 15 , 8 , 6 , 7 , 14 , 13 , 0 , 2 , 12 ));
463+
464+ ComputeSBoxes(LSBox, FSBox1_CryptoPro, FSBox2_CryptoPro, FSBox3_CryptoPro,
465+ FSBox4_CryptoPro);
417466end ;
418467
419468procedure TGost.Initialize ;
0 commit comments