⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gostdsa.pas

📁 GOSTDSA算法的Pascal实现和例子
💻 PAS
字号:
{License, info, etc
-------------------

This implementation is made by Walied Othman, to contact me 
mail to Walied.Othman@Student.KULeuven.ac.be or 
Triade@ace.Ulyssis.Student.KULeuven.ac.be, or ICQ me on 20388046.  
If you 're going to use these implementations, at least mention my 
name or something and notify me so I may even put a link on my page.  
This implementation is freeware and according to the coderpunks' 
manifesto it should remain so, so don 't use these implementations 
in commercial applications.  Encryption, as a tool to ensure privacy 
should be free and accessible for anyone.  If you plan to use these 
implementations in a commercial application, contact me before 
doing so.  If any algorithm is patented in your country, you should 
acquire a license before using this software.  Modified versions of this 
software must remain in the public domain and must contain an 
acknowledgement of the original author (=me).  
This implementaion is available at 
http://ace.ulyssis.student.kuleuven.ac.be/~triade/GInt/index.htm


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

Unit GOSTDSA;

Interface

Uses Windows, SysUtils, Controls, GInt;


Procedure GOSTDSAPrimeSearch(q : TGInt; Var p : TGInt);
Procedure GOSTDSASign(p, q, a, x : TGInt; Var k : TGInt; M : String; Var r, s : String);
Procedure GOSTDSAVerify(p, q, a, y : TGInt; m, r, s : String; Var ok : Boolean);

Implementation


{$H+}

// Searches for a prime p such that p mod q = 1, when calling
// this procedure, provide a random GInt for p

Procedure GOSTDSAPrimeSearch(q : TGInt; Var p : TGInt);
Var
   q2, one, temp1, temp2 : TGInt;
   ok : boolean;
Begin
   GIntAdd(q, q, q2);
   GIntMod(p, q, temp1);
   DecStrToGInt('1', one);
   GIntSub(p, temp1, temp2);
   GIntDestroy(temp1);
   GIntAdd(temp2, one, temp1);
   GIntDestroy(temp2);
   GIntDestroy(one);
   If (temp1^.value Mod 2) = 0 Then
   Begin
      GIntadd(temp1, q, temp2);
      GIntDestroy(temp1);
      temp1 := temp2;
   End;
   GIntdestroy(p);
   p := temp1;

   ok := false;
   While Not ok Do
   Begin
      GIntadd(p, q2, temp1);
      GIntDestroy(p);
      p := temp1;
      GIntPrimeTest(p, 5, ok);
   End;
   GIntDestroy(q2);
End;


// p is a prime, (according to the standard, p is i, where i
// ranges from 509 to 512 and from 1020 to 1024, bits long)
// q is a primefactor of p-1, (in the standard q is between 254 and 256 bit)
// a is any number < p-1, such that a^q mod p = 1
// x secret key, a number < q
// k random, less than q, same k must not be used twice and kept secret
// M the string you want to sign
// r,s form the signature

Procedure GOSTDSASign(p, q, a, x : TGInt; Var k : TGInt; M : String; Var r, s : String);
Var
   temp1, temp2, temp3, RGInt, SGInt, zero, one : TGInt;
   zerok : boolean;
Begin
   DecStrToGInt('1', one);
   DecStrToGInt('0', zero);
   Repeat
      GIntModExp(a, k, p, temp1);
      GIntMod(temp1, q, RGInt);
      GIntdestroy(temp1);
      If GIntCompareAbs(RGInt, zero) = Eq Then zerok := true Else zerok := false;
      If zerok Then
      Begin
         GIntDestroy(RGInt);
         GIntRandom1(k, temp1);
         GIntDestroy(k);
         k := temp1;
      End;
   Until Not zerok;
   strtoGInt(m, temp2);
   GIntMod(temp2, q, temp1);
   If GIntCompareAbs(temp1, zero) = Eq Then
   Begin
      GIntDestroy(temp2);
      GIntCopy(one, temp2);
   End;
   GIntDestroy(temp1);
   GIntMulMod(x, RGInt, q, temp1);
   GIntMulMod(k, temp2, q, temp3);
   GIntAddMod(temp1, temp3, q, SGInt);
   GIntDestroy(temp1);
   GIntDestroy(temp2);
   GIntDestroy(temp3);
   GIntToStr(RGInt, r);
   GIntToStr(SGInt, s);
   GIntDestroy(RGInt);
   GIntDestroy(SGInt);
   GIntDestroy(one);
   GIntDestroy(zero);
End;


// p is a prime, (according to the standard, p is i, where i
// ranges from 509 to 512 and from 1020 to 1024, bits long)
// q is a primefactor of p-1, (in the standard q is between 254 and 256 bit)
// a is any number < p-1, such that a^q mod p = 1
// y = a^x mod p
// m is the signed string, r,s form the signature, ok returns
// true if the signature is valid

Procedure GOSTDSAVerify(p, q, a, y : TGInt; m, r, s : String; Var ok : Boolean);
Var
   w, u1, u2, v, RGInt, SGInt, temp1, temp2, temp3, two : TGInt;
Begin
   StrToGInt(s, SGInt);
   StrToGInt(r, RGInt);
   StrToGInt(m, temp1);
   DecStrToGInt('2', two);
   GIntSub(q, two, temp2);
   GIntModExp(temp1, temp2, q, v);
   GIntDestroy(temp1);
   GIntDestroy(temp2);
   GIntMulMod(SGInt, v, q, u1);
   GIntDestroy(SGInt);
   GIntsub(q, RGInt, temp1);
   GIntMulMod(v, temp1, q, u2);
   GIntDestroy(v);
   GIntDestroy(temp1);
   GIntModExp(a, u1, p, temp1);
   GIntModExp(y, u2, p, temp2);
   GIntMulMod(temp1, temp2, p, temp3);
   GIntDestroy(temp1);
   GIntDestroy(temp2);
   GIntMod(temp3, q, w);
   GIntDestroy(temp3);
   GIntDestroy(u1);
   GIntDestroy(u2);
   If GIntCompareAbs(RGInt, w) = Eq Then ok := true Else ok := false;
   GIntDestroy(w);
   GIntDestroy(RGInt);
End;

End.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -