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

📄 rijndaeltest_ref.dpr

📁 Rijndael algorithm修正了CBC下BUG的版本(有源代码)工作在:D5。作者:Sergey Kirichenko。
💻 DPR
📖 第 1 页 / 共 2 页
字号:
{* rijndaeltest_ref   v2.0   August '99 *}

(* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                ---------------------------------                *
 *                            DELPHI                               *
 *         Implementation of the Known Answer Tests (KAT)          *
 *                   and Monte Carlo Test (MCT)                    *
 *                ---------------------------------                *
 *                                                   December 2000 *
 *                                                                 *
 * Authors: Paulo Barreto                                          *
 *          Vincent Rijmen                                         *
 *                                                                 *
 * Delphi translation by Sergey Kirichenko (ksv@cheerful.com)      *
 *                                                                 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *)


program rijndaeltest_ref;
{$APPTYPE CONSOLE}

{$define TRACE_KAT_MCT}

uses
  SysUtils,
  rijndael_api_ref;

const
  SUBMITTER = 'Joan Daemen';

function iif(bExpression: boolean; iResTrue,iResFalse: integer): integer;
begin
  if bExpression then
    result:= iResTrue
  else
    result:= iResFalse;
end;

function iif2(bExpression: boolean; sResTrue, sResFalse: string): string;
begin
  if bExpression then
    result:= sResTrue
  else
    result:= sResFalse;
end;

procedure wait;
begin
  write('* hit <enter> to exit...'); readln; writeln;
end;

procedure haltwait;
begin
  wait; halt;
end;

procedure blockPrint(var fp: textfile; const block: pBYTE; blockBits: integer; tag: string);
var
  i: integer;
begin
  write(fp, format('%s=',[tag]));
  for i:= 0 to (blockBits div 8)-1 do
    write(fp, format('%.2X',[block[i]]));
  writeln(fp,'');
  flush(fp);
end; {* blockPrint *}

procedure HexToBin(binBlock: pBYTE; const hexBlock: pchar; blockLen: integer);
var
  i,k,j,t: integer;
begin
  j:= 0;
  i:= 0;
  k:= 0;
  while (blockLen > 0) do
    begin
      t:= integer(hexBlock[i]);
      inc(i);
      if ((t >= ord('0')) and (t <= ord('9'))) then
        j:= (t - ord('0')) shl 4
      else
        if ((t >= ord('a')) and (t <= ord('f'))) then
          j:= (t - ord('a') + 10) shl 4
        else
          if ((t >= ord('A')) and (t <= ord('F'))) then
            j:= (t - ord('A') + 10) shl 4;

      t:= integer(hexBlock[i]);
      inc(i);
      if ((t >= ord('0')) and (t <= ord('9'))) then
        j:= j xor (t - ord('0'))
      else
        if ((t >= ord('a')) and (t <= ord('f'))) then
          j:= j xor (t - ord('a') + 10)
        else
          if ((t >= ord('A')) and (t <= ord('F'))) then
            j:= j xor (t - ord('A') + 10);

    binBlock[k]:= j;
    inc(k);
    blockLen:= blockLen- 8;
  end;
end; {* HexToBin *}

procedure rijndaelVKKAT(var fp: textfile; keyLength, blockLength: integer);
var
  i, j, r: integer;
  block: array [0..(4*MAXBC)-1] of BYTE;
  keyMaterial: array [0..320-1] of BYTE;
  byteVal: BYTE;
  keyInst: TkeyInstance;
  cipherInst: TcipherInstance;
begin
  byteVal:= BYTE('8');
{$ifdef TRACE_KAT_MCT}
  write(format('Executing Variable-Key KAT (key %d): ',[keyLength]));
{$endif} // ?TRACE_KAT_MCT
  write(fp,format(#13+#10+
		'=========='+#13+#10+
		#13+#10+
		'KEYSIZE=%d'+#13+#10+
		#13+#10,[keyLength]));
  flush(fp);
  FillChar(block, blockLength div 8, 0); // memset
  blockPrint(fp, addr(block), blockLength, 'PT');
  FillChar(keyMaterial, sizeof(keyMaterial),0);
  FillChar(keyMaterial, keyLength div 4,'0');
  for i:= 0 to keyLength-1 do
    begin
      keyMaterial[i div 4]:= byteVal; { set only the i-th bit of the i-th test key }
      keyInst.blockLen:= blockLength;
      r:= makeKey(addr(keyInst), DIR_ENCRYPT, keyLength, addr(keyMaterial));
      if (rTRUE <> r)  then
        begin
          writeln(format('makeKey error %d',[r]));
          haltwait; //(-1);
        end;
      writeln(fp, format(#13+#10+'I=%d', [i+1]));
      writeln(fp, format('KEY=%s',[StrPas(addr(keyMaterial))]));
      FillChar(block, blockLength div 8, 0);
      cipherInst.blockLen:= blockLength;
      r:= cipherInit(addr(cipherInst), MODE_ECB, nil);
      if (rTRUE <> r) then
        begin
          writeln(format('cipherInit error %d',[r]));
          haltwait; //(-1);
        end;
      r:= blocksEnCrypt(addr(cipherInst), addr(keyInst), addr(block), blockLength, addr(block));
      if (blockLength <> r) then
        begin
          writeln(format('blockEncrypt error %d',[r]));
          haltwait; //(-1);
        end;
      blockPrint(fp, addr(block), blockLength, 'CT');
      { now check decryption: }
      keyInst.blockLen:= blockLength;
      makeKey(addr(keyInst), DIR_DECRYPT, keyLength, addr(keyMaterial));
      blocksDeCrypt(addr(cipherInst), addr(keyInst), addr(block), blockLength, addr(block));
      for j:= 0 to (blockLength div 8)-1 do
        begin
          assert(block[j] = 0);      {!debug!}
        end;
      { undo changes for the next iteration: }
      keyMaterial[i div 4]:= BYTE('0');
      byteVal:=
        iif(byteVal = ord('8'), ord('4'),
        iif(byteVal = ord('4'), ord('2'),
        iif(byteVal = ord('2'), ord('1'),
        {(byteVal == '1')} ord('8'))));
    end;
  assert(byteVal = BYTE('8'));      {!debug!}
{$ifdef TRACE_KAT_MCT}
  writeln(' done.');
{$endif} // ?TRACE_KAT_MCT
end; {* rijndaelVKKAT *}

procedure rijndaelVTKAT(var fp: textfile; keyLength, blockLength: integer);
var
  i: integer;
  block: array [0..(4*MAXBC)-1] of BYTE;
  keyMaterial: array [0..320-1] of BYTE;
  keyInst: TkeyInstance;
  cipherInst: TcipherInstance;
begin
{$ifdef TRACE_KAT_MCT}
  write(format('Executing Variable-Text KAT (key %d): ',[keyLength]));
{$endif} // ?TRACE_KAT_MCT
  writeln(fp,format(#13+#10+
		'=========='+#13+#10+
		#13+#10+
		'KEYSIZE=%d'+#13+#10
		,[keyLength]));
  flush(fp);
  FillChar(keyMaterial, sizeof(keyMaterial), 0);
  FillChar(keyMaterial, keyLength div 4, '0');
  keyInst.blockLen:= blockLength;
  makeKey(addr(keyInst), DIR_ENCRYPT, keyLength, addr(keyMaterial));
  writeln(fp,format('KEY=%s',[StrPas(addr(keyMaterial))]));
  for i:= 0 to blockLength-1 do
    begin
      FillChar(block, blockLength div 8, 0);
      block[i div 8]:= block[i div 8] or (1 shl (7 - i mod 8)); { set only the i-th bit of the i-th test block }
      writeln(fp,format(#13+#10+'I=%d',[i+1]));
      blockPrint(fp, addr(block), blockLength, 'PT');
      cipherInst.blockLen:= blockLength;
      cipherInit(addr(cipherInst), MODE_ECB, nil);
      blocksEnCrypt(addr(cipherInst), addr(keyInst), addr(block), blockLength, addr(block));
      blockPrint(fp, addr(block), blockLength, 'CT');
    end;
{$ifdef TRACE_KAT_MCT}
  writeln(' done.');
{$endif} // ?TRACE_KAT_MCT
end;

procedure rijndaelTKAT(var fp: textfile; blockLength, keyLength: integer; var fin: textfile);
var
  i, j: integer;
  s: Cardinal; //unsigned int
  block,block2: array [0..(4*MAXBC)-1] of BYTE;
  keyMaterial: array [0..320-1] of BYTE;
  keyInst: TkeyInstance;
  cipherInst: TcipherInstance;
  ch1,ch2: char;
begin
{$ifdef TRACE_KAT_MCT}
  write(format('Executing Tables KAT (key %d): ',[keyLength]));
{$endif} // ?TRACE_KAT_MCT
  writeln(fp,format(#13+#10+
		'=========='+#13+#10+
		#13+#10+
		'KEYSIZE=%d'+
                #13+#10
		,[keyLength]));
  flush(fp);

  FillChar(keyMaterial, sizeof(keyMaterial), 0);
  for i:= 0 to 64-1 do
    begin
      writeln(fp,format(#13+#10+'I=%d', [i+1]));
      for j:= 0 to (keyLength div 4)-1 do
        read(fin,char(keyMaterial[j]));
      keyInst.blockLen:= blockLength;
      makeKey(addr(keyInst), DIR_ENCRYPT, keyLength, addr(keyMaterial));

      writeln(fp,format('KEY=%s',[StrPas(addr(keyMaterial))]));
      for j:= 0 to (blockLength div 8)-1 do
        begin
          read(fin,ch1);
          read(fin,ch1);
          read(fin,ch2);
          s:= strtoint('$'+ch1+ch2);
          block[j]:= s;
        end;
      readln(fin);
      blockPrint(fp, addr(block), blockLength, 'PT');
      cipherInst.blockLen:= blockLength;
      cipherInit(addr(cipherInst), MODE_ECB, nil);
      blocksEnCrypt(addr(cipherInst), addr(keyInst), addr(block), blockLength, addr(block2));
      blockPrint(fp, addr(block2), blockLength, 'CT');
    end;
  for i:= 64 to 128-1 do
    begin
      writeln(fp, format(#13+#10+'I=%d',[i+1]));
      for j:= 0 to (keyLength div 4)-1 do
        read(fin,char(keyMaterial[j]));
      keyInst.blockLen:= blockLength;
      makeKey(addr(keyInst), DIR_DECRYPT, keyLength, addr(keyMaterial));

      writeln(fp,format('KEY=%s',[StrPas(addr(keyMaterial))]));
      for j:= 0 to (blockLength div 8)-1 do
        begin
          read(fin,ch2);
          read(fin,ch1);
          read(fin,ch2);
          s:= strtoint('$'+ch1+ch2);
          block[j]:= s;
        end;
      readln(fin);
      cipherInst.blockLen:= blockLength;
      cipherInit(addr(cipherInst), MODE_ECB, nil);
      blocksDeCrypt(addr(cipherInst), addr(keyInst), addr(block), blockLength, addr(block2));
      blockPrint(fp, addr(block2), blockLength, 'PT');
      blockPrint(fp, addr(block), blockLength, 'CT');
    end;

{$ifdef TRACE_KAT_MCT}
  writeln(' done.');
{$endif} // ?TRACE_KAT_MCT
end;

procedure rijndaelIVKAT(var fp: textfile; keyLength, blockLength: integer);
var
  i, ROUNDS: integer;
  block,block2: array [0..(4*MAXBC)-1] of BYTE;
  keyMaterial: array [0..320-1] of BYTE;
  keyInst: TkeyInstance;
  cipherInst: TcipherInstance;
begin

{$ifdef TRACE_KAT_MCT}
  write(format('Executing Intermediate value KAT (key %d): ',[keyLength]));
{$endif} // ?TRACE_KAT_MCT

  case iif(keyLength >= blockLength, keyLength, blockLength) of
    128: ROUNDS:= 10;
    192: ROUNDS:= 12;
    256: ROUNDS:= 14;
    else
      exit; { this cannot happen }
  end;

  writeln(fp,format(#13+#10+
		'=========='+#13+#10+
		#13+#10+
		'KEYSIZE=%d',
		[keyLength]));
  flush(fp);
  FillChar(keyMaterial, sizeof(keyMaterial), 0);
  for i:= 0 to (keyLength div 8)-1 do
    StrLCopy(addr(keyMaterial[2*i]),pchar(format('%.2X',[i])),2);
  keyInst.blockLen:= blockLength;
  makeKey(addr(keyInst), DIR_ENCRYPT, keyLength, addr(keyMaterial));
  writeln(fp, format('KEY=%s', [StrPas(addr(keyMaterial))]));
  writeln(fp, #13+#10+'Intermediate Ciphertext Values (Encryption)'+#13+#10);
  for i:= 0 to (blockLength div 8)-1 do
    block[i]:= i;
  blockPrint(fp, addr(block), blockLength, 'PT');
  cipherInst.blockLen:= blockLength;
  cipherInit(addr(cipherInst), MODE_ECB, nil);
  for i:= 1 to ROUNDS-1 do
    begin
      cipherUpdateRounds(addr(cipherInst), addr(keyInst), addr(block), blockLength div 8, addr(block2), i);
      blockPrint(fp, addr(block2), blockLength, format('CT%d',[i]));
    end;
  cipherUpdateRounds(addr(cipherInst), addr(keyInst), addr(block), blockLength, addr(block2), ROUNDS);
  blockPrint(fp, addr(block2), blockLength, 'CT');

  keyInst.blockLen:= blockLength;
  makeKey(addr(keyInst), DIR_DECRYPT, keyLength, addr(keyMaterial));
  writeln(fp, #13+#10+'Intermediate Ciphertext Values (Decryption)'+#13+#10);
  blockPrint(fp, addr(block2), blockLength, 'CT');
  cipherInst.blockLen:= blockLength;
  cipherInit(addr(cipherInst), MODE_ECB, nil);
  for i:= 1 to ROUNDS-1 do
    begin
      cipherUpdateRounds(addr(cipherInst), addr(keyInst), addr(block2), blockLength, addr(block), ROUNDS-i);
      blockPrint(fp, addr(block), blockLength, format('PT%d',[i]));
    end;
  cipherUpdateRounds(addr(cipherInst), addr(keyInst), addr(block2), blockLength, addr(block), 0);
  blockPrint(fp, addr(block), blockLength, 'PT');

{$ifdef TRACE_KAT_MCT}
  writeln(' done.');
{$endif}
end;

procedure makeKATs(const vkFile, vtFile, tblFile, ivFile: string);
var
  fp, fp2: textfile;
begin
  { prepare Variable Key Known Answer Tests: }
  AssignFile(fp,vkFile);
  rewrite(fp);           // write mode
  writeln(fp,format(#13+#10+
		'========================='+#13+#10+
		#13+#10+
		'FILENAME:  "%s"'+#13+#10+
		#13+#10+
		'Electronic Codebook (ECB) Mode'+#13+#10+
		'Variable Key Known Answer Tests'+#13+#10+
		#13+#10+
		'Algorithm Name: Rijndael'+#13+#10+
		'Principal Submitter: %s',
		[vkFile,SUBMITTER]));
  flush(fp);

  rijndaelVKKAT(fp, 128, BITSPERBLOCK); { test for 128-bit key }
  rijndaelVKKAT(fp, 192, BITSPERBLOCK); { test for 192-bit key }
  rijndaelVKKAT(fp, 256, BITSPERBLOCK); { test for 256-bit key }

  write(fp,#13+#10+'==========');
  CloseFile(fp);

  { prepare Variable Text Known Answer Tests: }
  AssignFile(fp,vtFile);
  rewrite(fp);           // write mode
  writeln(fp,format(#13+#10+
		'========================='+#13+#10+
		#13+#10+
		'FILENAME:  "%s"'+#13+#10+
		#13+#10+
		'Electronic Codebook (ECB) Mode'+#13+#10+
		'Variable Text Known Answer Tests'+#13+#10+
		#13+#10+
		'Algorithm Name: Rijndael'+#13+#10+
		'Principal Submitter: %s',
		[vtFile,SUBMITTER]));
  flush(fp);

  rijndaelVTKAT(fp, 128, BITSPERBLOCK);
  rijndaelVTKAT(fp, 192, BITSPERBLOCK);
  rijndaelVTKAT(fp, 256, BITSPERBLOCK);

  write(fp,#13+#10+'==========');
  CloseFile(fp);

⌨️ 快捷键说明

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