ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 10.06.2021
Просмотров: 3569
Скачиваний: 3
Криптографическая
система
RSA
381
Продолжение
листинга
18.2
interface
const BIGNUM_DWORD = 31;
type TBigNum = array[0..BIGNUM_DWORD] of Cardinal;
procedure BN_bignum_to_hex(var a: TBigNum; var s: string);
procedure BN_hex_to_bignum(var s: string; var a: TBigNum);
procedure BN_bignum_to_dec(var a: TBigNum; var s: string);
procedure BN_dec_to_bignum(var s: string; var a: TBigNum);
function BN_a_cmp_b(var a,b: TBigNum): Integer;
procedure BN_a_add_b(var a,b,res: TBigNum);
procedure BN_a_sub_b(var a,b,res: TBigNum);
procedure BN_a_mul_b(var a,b,res: TBigNum);
procedure BN_a_shl_k(var a: TBigNum; k: Integer;
var res: TBigNum);
procedure BN_a_shr_k(var a: TBigNum; k: Integer;
var res: TBigNum);
function BN_a_upbit(var a: TBigNum): Integer;
procedure BN_a_setbit_k(var a: TBigNum; k: Integer);
procedure BN_a_mod_b(var a,b,res: TBigNum);
procedure BN_a_div_b(var a,b,res: TBigNum);
procedure BN_a_exp_b_mod_c(var a,b,c,res: TBigNum);
procedure BN_ab_GCD(var a,b,res: TBigNum);
procedure BN_a_modinv_b(var a,b,res: TBigNum);
function BN_PrimeTest(var a: TBigNum): Integer;
implementation
uses SysUtils;
var primes: array[0..53] of Integer =
( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89,
97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223,
227, 229, 233, 239, 241, 251);
procedure BN_bignum_to_hex(var a: TBigNum; var s: string);
var i: Integer;
begin
i := BIGNUM_DWORD;
while (i>=0) and (a[i]=0) do Dec(i);
382
Глава
18.
Криптографическая
защита
Продолжение
листинга
18.2
s := '0';
if (i<0) then Exit;
s := '';
while (i>=0) do
begin
s := s + IntToHex(a[i],8);
Dec(i);
end;
while (Length(s)>1) and (s[1]='0') do Delete(s,1,1);
end;
procedure BN_hex_to_bignum(var s: string; var a: TBigNum);
var i,j,l: Integer;
var n1,n2,n3,n4: TBigNum;
var n16: TBigNum;
begin
for i := 0 to BIGNUM_DWORD do a[i] := 0;
for i := 0 to BIGNUM_DWORD do n16[i] := 0; n16[0] := 16;
for i := 0 to BIGNUM_DWORD do n1[i] := 0; n1[0] := 1;
for i := 0 to BIGNUM_DWORD do n2[i] := 0;
l := Length(s);
for i := l downto 1 do
begin
j := Ord(UpCase(s[i]));
case j of
Ord('0')..Ord('9'): j := j - Ord('0');
Ord('A')..Ord('F'): j := j - Ord('A') + 10;
else Exit;
end;
n2[0] := Cardinal(j);
BN_a_mul_b(n1,n2,n3);
BN_a_add_b(a,n3,n4);
Move(n4,a,sizeof(n4));
BN_a_mul_b(n1,n16,n3);
Move(n3,n1,sizeof(n3));
end;
end;
procedure BN_bignum_to_dec(var a: TBigNum; var s: string);
var i: Integer;
var n1,n2: TBigNum;
Криптографическая
система
RSA
383
Продолжение
листинга
18.2
var nzero,nten: TBigNum;
begin
for i := 0 to BIGNUM_DWORD do nzero[i] := 0;
for i := 0 to BIGNUM_DWORD do nten[i] := 0; nten[0] := 10;
s := '0';
if (BN_a_cmp_b(a,nzero)=0) then Exit;
Move(a,n1,sizeof(a));
s := '';
repeat
BN_a_mod_b(n1,nten,n2);
s := Chr(n2[0]+48)+s;
BN_a_div_b(n1,nten,n2);
Move(n2,n1,sizeof(n2));
until (BN_a_cmp_b(n1,nzero)=0);
while (Length(s)>1) and (s[1]='0') do Delete(s,1,1);
end;
procedure BN_dec_to_bignum(var s: string; var a: TBigNum);
var i,j,l: Integer;
var n1,n2,n3,n4: TBigNum;
var nten: TBigNum;
begin
for i := 0 to BIGNUM_DWORD do a[i] := 0;
for i := 0 to BIGNUM_DWORD do nten[i] := 0; nten[0] := 10;
for i := 0 to BIGNUM_DWORD do n1[i] := 0; n1[0] := 1;
for i := 0 to BIGNUM_DWORD do n2[i] := 0;
l := Length(s);
for i := l downto 1 do
begin
j := Ord(s[i])-48;
if (j<0) or (j>9) then Exit;
n2[0] := Cardinal(j);
BN_a_mul_b(n1,n2,n3);
BN_a_add_b(a,n3,n4);
Move(n4,a,sizeof(n4));
BN_a_mul_b(n1,nten,n3);
Move(n3,n1,sizeof(n3));
end;
end;
function BN_a_cmp_b(var a,b: TBigNum): Integer;
var i: Integer;
384
Глава
18.
Криптографическая
защита
Продолжение
листинга
18.2
begin
i := BIGNUM_DWORD;
while (i>=0) and (a[i]=b[i]) do Dec(i);
Result := 0;
if (i>=0) and (a[i]>b[i]) then Result := 1;
if (i>=0) and (a[i]<b[i]) then Result := -1;
end;
procedure BN_a_add_b(var a,b,res: TBigNum);
begin
asm
pushad
mov esi,[a]
mov edi,[b]
mov ebx,[res]
mov ecx,BIGNUM_DWORD
mov eax,[esi]
add eax,[edi]
pushfd
mov [ebx],eax
add esi,4
add edi,4
add ebx,4
@_add_1:
mov eax,[esi]
popfd
adc eax,[edi]
pushfd
mov [ebx],eax
add esi,4
add edi,4
add ebx,4
loop @_add_1
@_add_2:
popfd
popad
end;
end;
procedure BN_a_sub_b(var a,b,res: TBigNum);
begin
asm
Криптографическая
система
RSA
385
Продолжение
листинга
18.2
pushad
mov esi,[a]
mov edi,[b]
mov ebx,[res]
mov ecx,BIGNUM_DWORD
mov eax,[esi]
sub eax,[edi]
pushfd
mov [ebx],eax
add esi,4
add edi,4
add ebx,4
@_sub_1:
mov eax,[esi]
popfd
sbb eax,[edi]
pushfd
mov [ebx],eax
add esi,4
add edi,4
add ebx,4
loop @_sub_1
@_sub_2:
popfd
popad
end;
end;
procedure BN_a_mul_b(var a,b,res: TBigNum);
var i,j: Integer;
begin
for j := 0 to BIGNUM_DWORD do res[j] := 0;
for i := 0 to BIGNUM_DWORD do
begin
j := i*4;
asm
pushad
mov esi,[a]
mov edi,[b]
add edi,[j]
mov ebx,[res]