ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 10.06.2021
Просмотров: 3562
Скачиваний: 3
386
Глава
18.
Криптографическая
защита
Продолжение
листинга
18.2
add ebx,[j]
mov ecx,BIGNUM_DWORD
sub ecx,[i]
cmp ecx,0
je @_mul_2
@_mul_1:
mov eax,[esi]
mov edx,[edi]
mul edx
add [ebx],eax
adc [ebx+4],edx
pushfd
cmp ecx,1
je @_mul_1_1
popfd
adc dword ptr[ebx+8],0
pushfd
@_mul_1_1:
popfd
add esi,4
add ebx,4
loop @_mul_1
@_mul_2:
mov eax,[esi]
mov edx,[edi]
mul edx
add [ebx],eax
popad
end;
end;
end;
procedure BN_a_shl_k(var a: TBigNum; k: Integer; var res: TBig-
Num);
var i,j: Integer;
var d,u: Cardinal;
begin
for j := 0 to BIGNUM_DWORD do res[j] := a[j];
if (k<=0) then Exit;
for j := 0 to BIGNUM_DWORD do res[j] := 0;
i := k div 32;
Криптографическая
система
RSA
387
Продолжение
листинга
18.2
if (i>BIGNUM_DWORD) then Exit;
for j := i to BIGNUM_DWORD do
res[j] := a[j-i];
i := k mod 32;
if (i=0) then Exit;
d := 0;
for j := 0 to BIGNUM_DWORD do
begin
u := res[j] shr (32-i);
res[j] := (res[j] shl i) + d;
d := u;
end;
end;
procedure BN_a_shr_k(var a: TBigNum; k: Integer;
var res: TBigNum);
var i,j: Integer;
var d,u: Cardinal;
begin
for j := 0 to BIGNUM_DWORD do res[j] := a[j];
if (k<=0) then Exit;
for j := 0 to BIGNUM_DWORD do res[j] := 0;
i := k div 32;
if (i>BIGNUM_DWORD) then Exit;
for j := i to BIGNUM_DWORD do
res[j-i] := a[j];
i := k mod 32;
if (i=0) then Exit;
u := 0;
for j := BIGNUM_DWORD downto 0 do
begin
d := res[j] shl (32-i);
res[j] := (res[j] shr i) + u;
u := d;
end;
end;
function BN_a_upbit(var a: TBigNum): Integer;
var i,j: Integer;
begin
i := BIGNUM_DWORD;
while (i>=0) and (a[i]=0) do Dec(i);
388
Глава
18.
Криптографическая
защита
Продолжение
листинга
18.2
Result := 0;
if (i<0) then Exit;
j := 31;
while (j>0) and (a[i] and (1 shl j) = 0) do Dec(j);
Result := i*32 + j + 1;
end;
procedure BN_a_setbit_k(var a: TBigNum; k: Integer);
begin
if (k<0) or (k>32*BIGNUM_DWORD-1) then
begin
Exit;
end;
a[k shr 5] := a[k shr 5] or (1 shl (k and 31));
end;
procedure BN_a_mod_b(var a,b,res: TBigNum);
var k: Integer;
var n1,n2,n3: TBigNum;
begin
FillChar(n3,sizeof(n3),0);
if (BN_a_cmp_b(b,n3)=0) then Exit;
Move(a,n1,sizeof(a));
while (BN_a_cmp_b(n1,b)>=0) do
begin
k := BN_a_upbit(n1) - BN_a_upbit(b);
BN_a_shl_k(b,k,n2);
if (BN_a_cmp_b(n2,n1)>0) then
begin
BN_a_shr_k(n2,1,n3);
Move(n3,n2,sizeof(n3));
end;
BN_a_sub_b(n1,n2,n3);
Move(n3,n1,sizeof(n3));
end;
Move(n1,res,sizeof(n1));
end;
procedure BN_a_div_b(var a,b,res: TBigNum);
var k: Integer;
var n1,n2,n3: TBigNum;
begin
Криптографическая
система
RSA
389
Продолжение
листинга
18.2
FillChar(res,sizeof(res),0);
FillChar(n3,sizeof(n3),0);
if (BN_a_cmp_b(b,n3)=0) then Exit;
Move(a,n1,sizeof(a));
while (BN_a_cmp_b(n1,b)>=0) do
begin
k := BN_a_upbit(n1) - BN_a_upbit(b);
BN_a_shl_k(b,k,n2);
if (BN_a_cmp_b(n2,n1)>0) then
begin
BN_a_shr_k(n2,1,n3);
Move(n3,n2,sizeof(n3));
Dec(k);
end;
BN_a_sub_b(n1,n2,n3);
Move(n3,n1,sizeof(n3));
BN_a_setbit_k(res,k);
end;
end;
procedure BN_a_exp_b_mod_c(var a,b,c,res: TBigNum);
var i,n: Integer;
var n1,n2,n3: TBigNum;
begin
FillChar(n3,sizeof(n3),0);
if (BN_a_cmp_b(c,n3)=0) then Exit;
for i := 0 to BIGNUM_DWORD do res[i] := 0;
if (BN_a_cmp_b(b,n3)=0) then
begin
res[0] := 1;
Exit;
end;
Move(a,n1,sizeof(a));
for i := 0 to BIGNUM_DWORD do n2[i] := 0;
n2[0] := 1;
n := BN_a_upbit(b)-1;
i := 0;
while (i<=n) do
begin
if ( (b[i shr 5] shr (i and 31)) and 1 = 1 ) then
begin
390
Глава
18.
Криптографическая
защита
Продолжение
листинга
18.2
BN_a_mul_b(n2,n1,n3);
BN_a_mod_b(n3,c,n2);
end;
BN_a_mul_b(n1,n1,n3);
BN_a_mod_b(n3,c,n1);
Inc(i);
end;
Move(n2,res,sizeof(n2));
end;
procedure BN_ab_GCD(var a,b,res: TBigNum);
var i: Integer;
var n1,n2,n3,nzero: TBigNum;
begin
res[0] := 1;
for i := 1 to BIGNUM_DWORD do res[i] := 0;
for i := 0 to BIGNUM_DWORD do nzero[i] := 0;
if (BN_a_cmp_b(a,nzero)=0) or (BN_a_cmp_b(b,nzero)=0) then
Exit;
if (BN_a_cmp_b(a,b)>0) then
begin
Move(a,n1,sizeof(a));
Move(b,n2,sizeof(a));
end
else
begin
Move(b,n1,sizeof(a));
Move(a,n2,sizeof(a));
end;
while (BN_a_cmp_b(n2,nzero)<>0) do
begin
BN_a_mod_b(n1,n2,n3);
Move(n2,n1,sizeof(n1));
Move(n3,n2,sizeof(n3));
end;
Move(n1,res,sizeof(n1));
end;
procedure BN_a_modinv_b(var a,b,res: TBigNum);
var i: Integer;
var n1,n2,n3,n4,n5,n6,n7: TBigNum;
var nzero,none: TBigNum;