@@ -241,10 +241,14 @@ TGCM = class(TObject)
241241 // / <param name="Ciphertext">
242242 // / Encrypted data used in the calculation
243243 // / </param>
244+ // / <param name="CiphertextSize">
245+ // / Length of the ciphertext in bytes. Use when reading part of array.
246+ // / </param>
244247 // / <returns>
245248 // / Calculated raw hash value which will later get returned as AuthenticatedTag
246249 // / </returns>
247- function CalcGaloisHash (AuthenticatedData, Ciphertext: TBytes): T128;
250+ function CalcGaloisHash (AuthenticatedData, Ciphertext : TBytes; CiphertextSize:
251+ Integer): T128;
248252
249253 // / <summary>
250254 // / Encrypts a T128 value using the encryption method specified on init
@@ -529,23 +533,24 @@ procedure TGCM.Init(EncryptionMethod : TEncodeDecodeMethod;
529533 b^ := 1 ;
530534 end
531535 else
532- FY := CalcGaloisHash(nil , InitVector);
536+ FY := CalcGaloisHash(nil , InitVector, length(InitVector) );
533537
534538 FEncryptionMethod(@FY[0 ], @FE_K_Y0[0 ], 16 );
535539end ;
536540
537- function TGCM.CalcGaloisHash (AuthenticatedData, Ciphertext : TBytes): T128;
541+ function TGCM.CalcGaloisHash (AuthenticatedData, Ciphertext : TBytes;
542+ CiphertextSize: Integer): T128;
538543var
539544 AuthCipherLength : T128;
540545 x : T128;
541546 n : Uint64;
542547
543- procedure encode (data : TBytes);
548+ procedure encode (data : TBytes; dataSize: Integer );
544549 var
545550 i, mod_d, div_d, len_d : UInt64;
546551 hdata : T128;
547552 begin
548- len_d := length(data) ;
553+ len_d := dataSize ;
549554 if (len_d > 0 ) then
550555 begin
551556 n := 0 ;
@@ -571,9 +576,10 @@ function TGCM.CalcGaloisHash(AuthenticatedData, Ciphertext : TBytes): T128;
571576
572577begin
573578 x := nullbytes;
574- encode(AuthenticatedData);
575- encode(Ciphertext);
576- SetAuthenticationCipherLength(AuthCipherLength, length(AuthenticatedData) shl 3 , length(ciphertext) shl 3 );
579+ encode(AuthenticatedData, length(AuthenticatedData));
580+ Assert(length(Ciphertext) >= CiphertextSize);
581+ encode(Ciphertext, CiphertextSize);
582+ SetAuthenticationCipherLength(AuthCipherLength, length(AuthenticatedData) shl 3 , CiphertextSize shl 3 );
577583
578584 Result := poly_mult_H(XOR_T128(AuthCipherLength, x));
579585end ;
@@ -599,7 +605,7 @@ procedure TGCM.DecodeGCM(Source, Dest: TBytes; Size: Integer);
599605 XOR_ArrayWithT128(Source, i, UInt64(Size)-i, EncodeT128(FY), Dest);
600606 end ;
601607
602- a_tag := XOR_T128(CalcGaloisHash(DataToAuthenticate, Source), FE_K_Y0);
608+ a_tag := XOR_T128(CalcGaloisHash(DataToAuthenticate, Source, Size ), FE_K_Y0);
603609
604610 Setlength(FCalcAuthenticationTag, FCalcAuthenticationTagLength);
605611 Move(a_tag[0 ], FCalcAuthenticationTag[0 ], FCalcAuthenticationTagLength);
@@ -638,7 +644,7 @@ procedure TGCM.EncodeGCM(Source, Dest: TBytes; Size: Integer);
638644 XOR_ArrayWithT128(Source, i, UInt64(Size)-i, EncodeT128(FY), Dest);
639645 end ;
640646
641- AuthTag := XOR_T128(CalcGaloisHash(DataToAuthenticate, Dest), FE_K_Y0);
647+ AuthTag := XOR_T128(CalcGaloisHash(DataToAuthenticate, Dest, Size ), FE_K_Y0);
642648 Setlength(FCalcAuthenticationTag, FCalcAuthenticationTagLength);
643649 Move(AuthTag[0 ], FCalcAuthenticationTag[0 ], FCalcAuthenticationTagLength);
644650end ;
0 commit comments