Скачиваний:
20
Добавлен:
01.05.2014
Размер:
5 Кб
Скачать
☆
{License, info, etc
 ------------------
This implementation is made by me, Walied Othman, to contact me
mail to rainwolf@submanifold.be or triade@submanifold.be ,
always mention wether it 's about the FGInt or about the 6xs, 
preferably in the subject line.
This source code is free, but only to other free software, 
it's a two-way street, if you use this code in an application from which 
you won't make any money of (e.g. software for the good of mankind) 
then go right ahead, I won't stop you, I do demand acknowledgement for 
my work.  However, if you're using this code in a commercial application, 
an application from which you'll make money, then yes, I charge a 
license-fee, as described in the license agreement for commercial use, see 
the textfile in this zip-file.
If you 're going to use these implementations, let me know, so I ca, put a link 
on my page if desired, I 'm always curious as to see where the spawn of my 
mind ends up in.  If any algorithm is patented in your country, you should
acquire a license before using this software.  Modified versions of this
software must contain an acknowledgement of the original author (=me).

This implementation is available at 
http://www.submanifold.be

copyright 2000, Walied Othman
This header may not be removed.}

Unit ECDSA;

{$H+}

Interface

Uses FGInt, ECGFp;


Procedure ECDSASign(M : String; p, a, x, n, k : TFGInt; B : TECPoint; Var r, s : String);
Procedure ECDSAVerify(M, r, s : String; p, a, n : TFGInt; B, Bx : TECPoint; Var Valid : Boolean);

Implementation


// Sign a string M using ECDSA defined on an elliptic curve
// y^2 = x^3 + a*x + b over GF(p), where 4*a^3 + 27*b^2 mod p doesn 't
// equal zero, B is the base point on the curve, x is the secret parameter,
// n is the order of B, and k is random
// The output are the strings r and s

Procedure ECDSASign(M : String; p, a, x, n, k : TFGInt; B : TECPoint; Var r, s : String);
Var
   RP, tempp : TECPoint;
   tempg1, tempg2, tempg3, zero : TFGInt;
   temps : String;
   i : longint;
Begin
   Base2StringToFGInt('0', zero);
   Repeat
      ECPointKMultiple(B, p, a, k, RP);
      FGIntMod(RP.XCoordinate, n, tempg1);
      While FGIntCompareAbs(tempg1, zero) = Eq Do
      Begin
         ECAddPoints(RP, B, p, a, tempp);
         ECPointDestroy(RP);
         ECPointCopy(tempp, RP);
         ECPointDestroy(tempp);
         FGIntDestroy(tempg1);
         FGIntMod(RP.XCoordinate, n, tempg1);
      End;
      FGIntToBase256String(tempg1, r);
      FGIntDestroy(tempg1);
      FGIntToBase2String(n, temps);
      i := length(temps) - 1;
      ConvertBase256To2(M, temps);
      While Length(temps) > i Do delete(temps, length(temps), 1);
      Base2StringToFGInt(temps, tempg1);
      FGIntMulMod(RP.XCoordinate, x, n, tempg2);
      FGIntAddMod(tempg1, tempg2, n, tempg3);
      FGIntDestroy(tempg2);
      FGIntDestroy(tempg1);
      FGIntModInv(k, n, tempg1);
      FGIntMulMod(tempg1, tempg3, n, tempg2);
      FGIntDestroy(tempg3);
      FGIntDestroy(tempg1);
      FGIntToBase256String(tempg2, s);
      FGIntDestroy(tempg2);
      If s = chr(0) Then
      Begin
         ECAddPoints(RP, B, p, a, tempp);
         ECPointDestroy(RP);
         ECPointCopy(tempp, RP);
         ECPointDestroy(tempp);
      End;
   Until s <> chr(0);
   ECPointDestroy(RP);
   FGIntDestroy(zero);
End;


// Verify an ECDSA signature defined on an elliptic curve
// y^2 = x^3 + a*x + b over GF(p), where 4*a^3 + 27*b^2 mod p doesn 't
// equal zero, x is your private parameter as defined above, 
// B is the base point on the curve, Bx is B*x where x is secret,
// n is the order of B, M is the signed message and r and s form
// the signature

Procedure ECDSAVerify(M, r, s : String; p, a, n : TFGInt; B, Bx : TECPoint; Var Valid : Boolean);
Var
   RP, tempp1, tempp2 : TECPoint;
   tempg1, tempg2, tempg3, u1, u2 : TFGInt;
   temps : String;
   i : longint;
Begin
   valid := true;
   If (r = chr(0)) Or (s = chr(0)) Then
   Begin
      valid := false;
      exit;
   End;
   Base256StringToFGInt(r, tempg1);
   If Not (FGIntCompareAbs(tempg1, n) = St) Then
   Begin
      FGIntDestroy(tempg1);
      valid := false;
      exit;
   End;
   FGIntDestroy(tempg1);
   Base256StringToFGInt(s, tempg1);
   If Not (FGIntCompareAbs(tempg1, n) = St) Then
   Begin
      FGIntDestroy(tempg1);
      valid := false;
      exit;
   End;
   FGIntDestroy(tempg1);
   FGIntToBase2String(n, temps);
   i := length(temps) - 1;
   ConvertBase256To2(M, temps);
   While Length(temps) > i Do delete(temps, length(temps), 1);
   Base2StringToFGInt(temps, tempg1);
   Base256StringToFGInt(s, tempg2);
   FGIntModInv(tempg2, n, tempg3);
   FGIntMulMod(tempg3, tempg1, n, u1);
   FGIntDestroy(tempg1);
   FGIntDestroy(tempg2);
   Base256StringToFGInt(r, tempg1);
   FGIntMulMod(tempg1, tempg3, n, u2);
   FGIntDestroy(tempg3);
   ECPointKMultiple(B, p, a, u1, tempp1);
   ECPointKMultiple(Bx, p, a, u2, tempp2);
   FGIntDestroy(u1);
   FGIntDestroy(u2);
   ECAddPoints(tempp1, tempp2, p, a, RP);
   ECPointDestroy(tempp1);
   ECPointDestroy(tempp2);
   FGIntMod(RP.XCoordinate, n, tempg2);
   Valid := (FGIntCompareAbs(tempg1, tempg2) = Eq);
   FGIntDestroy(tempg1);
   FGIntDestroy(tempg2);
   ECPointDestroy(RP);
End;


End.
Соседние файлы в папке Delphi source