浏览代码

some code refactorings

Ugochukwu Mmaduekwe 7 年之前
父节点
当前提交
f0e5768ee3

+ 0 - 4
CryptoLib.Tests/src/Asn1/StringTests.pas

@@ -152,10 +152,6 @@ begin
     Fail('DerT61String.GetString() result incorrect');
     Fail('DerT61String.GetString() result incorrect');
   end;
   end;
 
 
-  if (not(t61.ToString() = (t61String))) then
-  begin
-    Fail('DerT61String.ToString() result incorrect');
-  end;
 end;
 end;
 
 
 initialization
 initialization

+ 8 - 14
CryptoLib/src/Asn1/ClpDerApplicationSpecific.pas

@@ -317,7 +317,7 @@ end;
 class function TDerApplicationSpecific.ReplaceTagNumber(newTag: Int32;
 class function TDerApplicationSpecific.ReplaceTagNumber(newTag: Int32;
   const input: TCryptoLibByteArray): TCryptoLibByteArray;
   const input: TCryptoLibByteArray): TCryptoLibByteArray;
 var
 var
-  tagNo, index, b: Int32;
+  tagNo, index, b, remaining: Int32;
   tmp: TCryptoLibByteArray;
   tmp: TCryptoLibByteArray;
 begin
 begin
   tagNo := input[0] and $1F;
   tagNo := input[0] and $1F;
@@ -327,35 +327,29 @@ begin
   //
   //
   if (tagNo = $1F) then
   if (tagNo = $1F) then
   begin
   begin
-    // tagNo := 0;
 
 
-    b := input[index] and $FF;
+    b := input[index];
     System.Inc(index);
     System.Inc(index);
 
 
     // X.690-0207 8.1.2.4.2
     // X.690-0207 8.1.2.4.2
     // "c) bits 7 to 1 of the first subsequent octet shall not all be zero."
     // "c) bits 7 to 1 of the first subsequent octet shall not all be zero."
     if ((b and $7F) = 0) then // Note: -1 will pass
     if ((b and $7F) = 0) then // Note: -1 will pass
     begin
     begin
-      raise EInvalidOperationCryptoLibException.CreateRes(@SCorruptedStream);
+      raise EIOCryptoLibException.CreateRes(@SCorruptedStream);
     end;
     end;
 
 
-    while ((b >= 0) and ((b and $80) <> 0)) do
+    while ((b and $80) <> 0) do
     begin
     begin
-      // tagNo := tagNo or ((b and $7F));
-      // tagNo := tagNo shl 7;
-      b := input[index] and $FF;
+      b := input[index];
       System.Inc(index);
       System.Inc(index);
     end;
     end;
 
 
-    // tagNo := tagNo or (b and $7F);
   end;
   end;
 
 
-  System.SetLength(tmp, System.Length(input) - index + 1);
-
-  System.Move(input[index], tmp[1], System.Length(tmp) - 1 *
-    System.SizeOf(Byte));
-
+  remaining := System.Length(input) - index;
+  System.SetLength(tmp, 1 + remaining);
   tmp[0] := Byte(newTag);
   tmp[0] := Byte(newTag);
+  System.Move(input[index], tmp[1], remaining * System.SizeOf(Byte));
 
 
   result := tmp;
   result := tmp;
 end;
 end;

+ 7 - 4
CryptoLib/src/Math/EC/Multiplier/ClpFixedPointCombMultiplier.pas

@@ -22,6 +22,7 @@ unit ClpFixedPointCombMultiplier;
 interface
 interface
 
 
 uses
 uses
+  ClpBits,
   ClpBigInteger,
   ClpBigInteger,
   ClpNat,
   ClpNat,
   ClpCryptoLibTypes,
   ClpCryptoLibTypes,
@@ -68,7 +69,8 @@ function TFixedPointCombMultiplier.MultiplyPositive(const p: IECPoint;
 var
 var
   c: IECCurve;
   c: IECCurve;
   R, add: IECPoint;
   R, add: IECPoint;
-  size, width, d, top, i, j, secretIndex, fullComb: Int32;
+  size, width, d, top, i, j, fullComb: Int32;
+  secretIndex, secretBit: UInt32;
   info: IFixedPointPreCompInfo;
   info: IFixedPointPreCompInfo;
   lookupTable: IECLookupTable;
   lookupTable: IECLookupTable;
   LK: TCryptoLibUInt32Array;
   LK: TCryptoLibUInt32Array;
@@ -108,14 +110,15 @@ begin
     while j >= 0 do
     while j >= 0 do
     begin
     begin
 
 
+      secretBit := LK[TBits.Asr32(j, 5)] shr (j and $1F);
+      secretIndex := secretIndex xor (secretBit shr 1);
       secretIndex := secretIndex shl 1;
       secretIndex := secretIndex shl 1;
-
-      secretIndex := secretIndex or Int32(TNat.GetBit(LK, j));
+      secretIndex := secretIndex xor secretBit;
 
 
       System.Dec(j, d);
       System.Dec(j, d);
     end;
     end;
 
 
-    add := lookupTable.Lookup(secretIndex);
+    add := lookupTable.Lookup(Int32(secretIndex));
     R := R.TwicePlus(add);
     R := R.TwicePlus(add);
 
 
   end;
   end;

+ 24 - 5
CryptoLib/src/Math/Raw/ClpNat.pas

@@ -155,6 +155,10 @@ type
       const y: TCryptoLibUInt32Array; yOff: Int32;
       const y: TCryptoLibUInt32Array; yOff: Int32;
       const zz: TCryptoLibUInt32Array; zzOff: Int32); overload; static;
       const zz: TCryptoLibUInt32Array; zzOff: Int32); overload; static;
 
 
+    class procedure Mul(const x: TCryptoLibUInt32Array; xOff, xLen: Int32;
+      const y: TCryptoLibUInt32Array; yOff, yLen: Int32;
+      const zz: TCryptoLibUInt32Array; zzOff: Int32); overload; static;
+
     class function Mul31BothAdd(len: Int32; a: UInt32;
     class function Mul31BothAdd(len: Int32; a: UInt32;
       const x: TCryptoLibUInt32Array; b: UInt32;
       const x: TCryptoLibUInt32Array; b: UInt32;
       const y, z: TCryptoLibUInt32Array; zOff: Int32): UInt32; static;
       const y, z: TCryptoLibUInt32Array; zOff: Int32): UInt32; static;
@@ -1152,12 +1156,12 @@ class procedure TNat.Mul(len: Int32; const x, y, zz: TCryptoLibUInt32Array);
 var
 var
   I: Int32;
   I: Int32;
 begin
 begin
-  zz[len] := UInt32(MulWord(len, x[0], y, zz));
+  zz[len] := MulWord(len, x[0], y, zz);
 
 
   for I := 1 to System.Pred(len) do
   for I := 1 to System.Pred(len) do
 
 
   begin
   begin
-    zz[I + len] := UInt32(MulWordAddTo(len, x[I], y, 0, zz, I));
+    zz[I + len] := MulWordAddTo(len, x[I], y, 0, zz, I);
   end;
   end;
 end;
 end;
 
 
@@ -1167,12 +1171,27 @@ class procedure TNat.Mul(len: Int32; const x: TCryptoLibUInt32Array;
 var
 var
   I: Int32;
   I: Int32;
 begin
 begin
-  zz[zzOff + len] := UInt32(MulWord(len, x[xOff], y, yOff, zz, zzOff));
+  zz[zzOff + len] := MulWord(len, x[xOff], y, yOff, zz, zzOff);
 
 
   for I := 1 to System.Pred(len) do
   for I := 1 to System.Pred(len) do
   begin
   begin
-    zz[zzOff + I + len] := UInt32(MulWordAddTo(len, x[xOff + I], y, yOff, zz,
-      zzOff + I));
+    zz[zzOff + I + len] := MulWordAddTo(len, x[xOff + I], y, yOff, zz,
+      zzOff + I);
+  end;
+end;
+
+class procedure TNat.Mul(const x: TCryptoLibUInt32Array; xOff, xLen: Int32;
+  const y: TCryptoLibUInt32Array; yOff, yLen: Int32;
+  const zz: TCryptoLibUInt32Array; zzOff: Int32);
+var
+  I: Int32;
+begin
+  zz[zzOff + yLen] := MulWord(yLen, x[xOff], y, yOff, zz, zzOff);
+
+  for I := 1 to System.Pred(xLen) do
+  begin
+    zz[zzOff + I + yLen] := MulWordAddTo(yLen, x[xOff + I], y, yOff, zz,
+      zzOff + I);
   end;
   end;
 end;
 end;