?? rijndaeltest_ref.dpr
字號:
{ prepare Tables Known Answer Tests: }
AssignFile(fp,tblFile);
rewrite(fp); // write mode
writeln(fp,format(
'/* Description of what tables are tested:'+#13+#10+
' The provided implementations each use a different set of tables'+#13+#10+
' - Java implementation: uses no tables'+#13+#10+
' - reference C implementation: uses Logtable, Alogtable, S, Si, rcon'+#13+#10+
' - fast C implementation: uses Logtable, Alogtable, rcon'+#13+#10+
' and additionally, T1, T2, T3, T4, T5, T6, T7, T8'+#13+#10+
' and (for the inverse key schedule only) U1, U2, U3, U4.'+#13+#10+
' All these tables are tested.'+#13+#10+
#13+#10+
'========================='+#13+#10+
#13+#10+
'FILENAME: "%s"'+#13+#10+
#13+#10+
'Electronic Codebook (ECB) Mode'+#13+#10+
'Tables Known Answer Tests'+#13+#10+
#13+#10+
'Algorithm Name: Rijndael'+#13+#10+
'Principal Submitter: %s',
[tblFile,SUBMITTER]));
flush(fp);
if fileexists('table.128') then
begin
AssignFile(fp2,'table.128');
reset(fp2); // read mode
rijndaelTKAT(fp, 128, 128, fp2);
CloseFile(fp2);
end
else
begin
writeln('Table Known Answer test expects file table.128');
haltwait; // (EXIT_FAILURE);
end;
if fileexists('table.192') then
begin
AssignFile(fp2,'table.192');
reset(fp2); // read mode
rijndaelTKAT(fp, 128, 192, fp2);
CloseFile(fp2);
end
else
begin
writeln('Table Known Answer test expects file table.192');
haltwait; //(EXIT_FAILURE);
end;
if fileexists('table.256') then
begin
AssignFile(fp2,'table.256');
reset(fp2); // read mode
rijndaelTKAT(fp, 128, 256, fp2);
CloseFile(fp2);
end
else
begin
writeln('Table Known Answer test expects file table.256');
haltwait; // (EXIT_FAILURE);
end;
write(fp,#13+#10+'==========');
CloseFile(fp);
{ prepare Intermediate Values Known Answer Tests: }
AssignFile(fp,ivFile);
rewrite(fp);
writeln(fp,format(#13+#10+
'========================='+#13+#10+
#13+#10+
'FILENAME: "%s"'+#13+#10+
#13+#10+
'Electronic Codebook (ECB) Mode'+#13+#10+
'Intermediate Value Known Answer Tests'+#13+#10+
#13+#10+
'Algorithm Name: Rijndael'+#13+#10+
'Principal Submitter: %s',
[ivFile,SUBMITTER]));
flush(fp);
rijndaelIVKAT(fp, 128, BITSPERBLOCK);
rijndaelIVKAT(fp, 192, BITSPERBLOCK);
rijndaelIVKAT(fp, 256, BITSPERBLOCK);
write(fp,#13+#10+'==========');
CloseFile(fp);
end;
procedure rijndaelECB_MCT(var fp: textfile; const initKey: pchar; keyLength: integer;
const initBlock: pchar; blockLength: integer; direction: byte);
var
i, j: integer;
inBlock, outBlock, binKey: array [0..(4*MAXBC)-1] of BYTE;
keyMaterial: array [0..320-1] of byte;
keyInst: TkeyInstance;
cipherInst: TcipherInstance;
{$ifdef TRACE_KAT_MCT}
elapsed: TDateTime;
{$endif} // ?TRACE_KAT_MCT
begin
{$ifdef TRACE_KAT_MCT}
write(format('Executing ECB MCT (%s, key %d): ',[iif2(direction= DIR_ENCRYPT,'ENCRYPT','DECRYPT'),keyLength]));
elapsed:= time;
{$endif} // ?TRACE_KAT_MCT
writeln(fp,format(#13+#10+
'========================='+#13+#10+
#13+#10+
'KEYSIZE=%d',[keyLength]));
flush(fp);
HexToBin(addr(outBlock), initBlock, blockLength);
HexToBin(addr(binKey), initKey, keyLength);
for i:= 0 to 400-1 do
begin
{$ifdef TRACE_KAT_MCT}
write('.');
{$endif} // ?TRACE_KAT_MCT
writeln(fp, format(#13+#10+'I=%d',[i]));
{ prepare key: }
for j:= 0 to (keyLength div 8)-1 do
StrLCopy(addr(keyMaterial[2*j]),pchar(format('%.2X',[binKey[j]])),2);
keyMaterial[keyLength div 4]:= 0;
writeln(fp, format('KEY=%s', [StrPas(addr(keyMaterial))]));
keyInst.blockLen:= blockLength;
makeKey(addr(keyInst), direction, keyLength, addr(keyMaterial));
{ do encryption/decryption: }
blockPrint(fp, addr(outBlock), blockLength, iif2(direction = DIR_ENCRYPT, 'PT', 'CT'));
cipherInst.blockLen:= blockLength;
cipherInit(addr(cipherInst), MODE_ECB, nil);
if (direction = DIR_ENCRYPT) then
for j:= 0 to 10000-1 do
begin
Move(outBlock, inBlock, blockLength div 8); //memcpy
blocksEnCrypt(addr(cipherInst), addr(keyInst), addr(inBlock), blockLength, addr(outBlock));
end
else
for j:= 0 to 10000-1 do
begin
Move(outBlock, inBlock, blockLength div 8); //memcpy
blocksDeCrypt(addr(cipherInst), addr(keyInst), addr(inBlock), blockLength, addr(outBlock));
end;
blockPrint(fp, addr(outBlock), blockLength, iif2(direction = DIR_ENCRYPT, 'CT', 'PT'));
{ prepare new key: }
case keyLength of
128: for j:= 0 to (128 div 8)-1 do
binKey[j]:= binKey[j] xor outBlock[j];
192: begin
for j:= 0 to (64 div 8)-1 do
binKey[j]:= binKey[j] xor inBlock[j + (64 div 8)];
for j:= 0 to (128 div 8)-1 do
binKey[j + (64 div 8)]:= binKey[j + (64 div 8)] xor outBlock[j];
end;
256: begin
for j:= 0 to (128 div 8)-1 do
binKey[j]:= binKey[j] xor inBlock[j];
for j:= 0 to (128 div 8)-1 do
binKey[j + (128 div 8)]:= binKey[j + (128 div 8)] xor outBlock[j];
end;
end;
end;
{$ifdef TRACE_KAT_MCT}
elapsed:= time -elapsed;
writeln(format(' done (%s).', [timetostr(elapsed)]));
{$endif} // ?TRACE_KAT_MCT
end; {* rijndaelECB_MCT *}
procedure rijndaelCBC_MCT(var fp: textfile; const initKey: pchar; keyLength: integer;
const initIV, initBlock: pchar; blockLength: integer; direction: BYTE);
var
i, j, r, t: integer;
inBlock, outBlock, binKey, cv: array [0..(256 div 8)-1] of byte;
keyMaterial: array [0..320-1] of byte;
iv: array [0..64] of byte;
keyInst: TkeyInstance;
cipherInst: TcipherInstance;
{$ifdef TRACE_KAT_MCT}
elapsed: TDateTime;
{$endif} // ?TRACE_KAT_MCT
begin
{$ifdef TRACE_KAT_MCT}
write(format('Executing CBC MCT (%s, key %d): ',[iif2(direction= DIR_ENCRYPT,'ENCRYPT','DECRYPT'),keyLength]));
elapsed:= time;
{$endif} // ?TRACE_KAT_MCT
writeln(fp,format(#13+#10+
'=========='+#13+#10+
#13+#10+
'KEYSIZE=%d',[keyLength]));
flush(fp);
HexToBin(addr(inBlock), initBlock, blockLength); { this is either PT0 or CT0 }
HexToBin(addr(cv), initIV, blockLength);
HexToBin(addr(binKey), initKey, keyLength);
for i:= 0 to 400-1 do
begin
{$ifdef TRACE_KAT_MCT}
write('.');
{$endif} // ?TRACE_KAT_MCT
writeln(fp, format(#13+#10+'I=%d',[i]));
{ prepare key: }
for j:= 0 to (keyLength div 8)-1 do
StrLCopy(addr(keyMaterial[2*j]),pchar(format('%.2X',[binKey[j]])),2);
keyMaterial[keyLength div 4]:= 0;
writeln(fp, format('KEY=%s', [StrPas(addr(keyMaterial))]));
keyInst.blockLen:= blockLength;
r:= makeKey(addr(keyInst), direction, keyLength, addr(keyMaterial));
if (rTRUE <> r) then
begin
writeln(format('makeKey error %d',[r]));
haltwait; //-1
end;
{ do encryption/decryption: }
blockPrint(fp, addr(cv), blockLength, 'IV');
blockPrint(fp, addr(inBlock), blockLength, iif2(direction = DIR_ENCRYPT, 'PT', 'CT'));
if (direction = DIR_ENCRYPT) then
for j:= 0 to 10000-1 do
begin
for t:= 0 to (blockLength div 8)-1 do
StrLCopy(addr(iv[2*t]),pchar(format('%.2X',[cv[t]])),2);
cipherInst.blockLen:= blockLength;
r:= cipherInit(addr(cipherInst), MODE_CBC, addr(iv));
if (rTRUE <> r) then
begin
writeln(format('cipherInit error %d',[r]));
haltwait;
end;
r:= blocksEnCrypt(addr(cipherInst), addr(keyInst), addr(inBlock), blockLength, addr(outBlock));
if (blockLength <> r) then
begin
writeln(format('blockEncrypt error %d',[r]));
haltwait;
end;
Move(cv, inBlock, blockLength div 8);
Move(outBlock, cv, blockLength div 8);
end
else
for j:= 0 to 10000-1 do
begin
for t:= 0 to (blockLength div 8)-1 do
StrLCopy(addr(iv[2*t]),pchar(format('%.2X',[cv[t]])),2);
cipherInst.blockLen:= blockLength;
cipherInit(addr(cipherInst), MODE_CBC, addr(iv));
blocksDeCrypt(addr(cipherInst), addr(keyInst), addr(inBlock), blockLength, addr(outBlock));
Move(inBlock, cv, blockLength div 8);
Move(outBlock, inBlock, blockLength div 8);
end;
blockPrint(fp, addr(outBlock), blockLength, iif2(direction = DIR_ENCRYPT, 'CT', 'PT'));
{ prepare new key: }
case (keyLength) of
128: for j:= 0 to (128 div 8)-1 do
binKey[j]:= binKey[j] xor outBlock[j];
192: begin
for j:= 0 to (64 div 8)-1 do
if (direction = DIR_ENCRYPT) then
binKey[j]:= binKey[j] xor inBlock[j + (64 div 8)]
else
binKey[j]:= binKey[j] xor cv[j + (64 div 8)];
for j:= 0 to (128 div 8)-1 do
binKey[j+ (64 div 8)]:= binKey[j+ (64 div 8)] xor outBlock[j];
end;
256: begin
for j:= 0 to (128 div 8)-1 do
if (direction = DIR_ENCRYPT) then
binKey[j]:= binKey[j] xor inBlock[j]
else
binKey[j]:= binKey[j] xor cv[j];
for j:= 0 to (128 div 8)-1 do
binKey[j+ (128 div 8)]:= binKey[j+ (128 div 8)] xor outBlock[j];
end;
end;
end;
{$ifdef TRACE_KAT_MCT}
elapsed:= time -elapsed;
writeln(format(' done (%s).', [timetostr(elapsed)]));
{$endif} // ?TRACE_KAT_MCT
end; {* rijndaelCBC_MCT *}
procedure makeMCTs(const ecbEncryptionFile, ecbDecryptionFile,
cbcEncryptionFile, cbcDecryptionFile: string);
var
fp: textfile;
begin
{ prepare ECB Encryption Monte Carlo Tests: }
AssignFile(fp,ecbEncryptionFile);
rewrite(fp);
writeln(fp,format(#13+#10+
'========================='+#13+#10+
#13+#10+
'FILENAME: "%s"'+#13+#10+
#13+#10+
'Electronic Codebook (ECB) Mode - ENCRYPTION'+#13+#10+
'Monte Carlo Test'+#13+#10+
#13+#10+
'Algorithm Name: Rijndael'+#13+#10+
'Principal Submitter: %s',
[ecbEncryptionFile,SUBMITTER]));
flush(fp);
rijndaelECB_MCT(fp,
'00000000000000000000000000000000', 128,
'00000000000000000000000000000000', BITSPERBLOCK, DIR_ENCRYPT);
rijndaelECB_MCT(fp,
'000000000000000000000000000000000000000000000000', 192,
'00000000000000000000000000000000', BITSPERBLOCK, DIR_ENCRYPT);
rijndaelECB_MCT(fp,
'0000000000000000000000000000000000000000000000000000000000000000', 256,
'00000000000000000000000000000000', BITSPERBLOCK, DIR_ENCRYPT);
write(fp,#13+#10+'===========');
CloseFile(fp);
{ prepare ECB Decryption Monte Carlo Tests: }
AssignFile(fp,ecbDecryptionFile);
rewrite(fp);
writeln(fp,format(#13+#10+
'========================='+#13+#10+
#13+#10+
'FILENAME: "%s"'+#13+#10+
#13+#10+
'Electronic Codebook (ECB) Mode - DECRYPTION'+#13+#10+
'Monte Carlo Test'+#13+#10+
#13+#10+
'Algorithm Name: Rijndael'+#13+#10+
'Principal Submitter: %s',
[ecbDecryptionFile,SUBMITTER]));
flush(fp);
rijndaelECB_MCT(fp,
'00000000000000000000000000000000', 128,
'00000000000000000000000000000000', BITSPERBLOCK, DIR_DECRYPT);
rijndaelECB_MCT(fp,
'000000000000000000000000000000000000000000000000', 192,
'00000000000000000000000000000000', BITSPERBLOCK, DIR_DECRYPT);
rijndaelECB_MCT(fp,
'0000000000000000000000000000000000000000000000000000000000000000', 256,
'00000000000000000000000000000000', BITSPERBLOCK, DIR_DECRYPT);
write(fp,#13+#10+'===========');
CloseFile(fp);
{ prepare CBC Encryption Monte Carlo Tests: }
AssignFile(fp,cbcEncryptionFile);
rewrite(fp);
writeln(fp,format(#13+#10+
'========================='+#13+#10+
#13+#10+
'FILENAME: "%s"'+#13+#10+
#13+#10+
'Cipher Block Chaining (CBC) Mode - ENCRYPTION'+#13+#10+
'Monte Carlo Test'+#13+#10+
#13+#10+
'Algorithm Name: Rijndael'+#13+#10+
'Principal Submitter: %s',
[cbcEncryptionFile,SUBMITTER]));
flush(fp);
rijndaelCBC_MCT(fp,
'00000000000000000000000000000000', 128,
'00000000000000000000000000000000', '00000000000000000000000000000000', BITSPERBLOCK,
DIR_ENCRYPT);
rijndaelCBC_MCT(fp,
'000000000000000000000000000000000000000000000000', 192,
'00000000000000000000000000000000', '00000000000000000000000000000000', BITSPERBLOCK,
DIR_ENCRYPT);
rijndaelCBC_MCT(fp,
'0000000000000000000000000000000000000000000000000000000000000000', 256,
'00000000000000000000000000000000', '00000000000000000000000000000000', BITSPERBLOCK,
DIR_ENCRYPT);
write(fp,#13+#10+'===========');
CloseFile(fp);
{ prepare CBC Decryption Monte Carlo Tests: }
AssignFile(fp,cbcDecryptionFile);
rewrite(fp);
writeln(fp,format(#13+#10+
'========================='+#13+#10+
#13+#10+
'FILENAME: "%s"'+#13+#10+
#13+#10+
'Cipher Block Chaining (CBC) Mode - DECRYPTION'+#13+#10+
'Monte Carlo Test'+#13+#10+
#13+#10+
'Algorithm Name: Rijndael'+#13+#10+
'Principal Submitter: %s',
[cbcDecryptionFile,SUBMITTER]));
flush(fp);
rijndaelCBC_MCT(fp,
'00000000000000000000000000000000', 128,
'00000000000000000000000000000000', '00000000000000000000000000000000', BITSPERBLOCK,
DIR_DECRYPT);
rijndaelCBC_MCT(fp,
'000000000000000000000000000000000000000000000000', 192,
'00000000000000000000000000000000', '00000000000000000000000000000000', BITSPERBLOCK,
DIR_DECRYPT);
rijndaelCBC_MCT(fp,
'0000000000000000000000000000000000000000000000000000000000000000', 256,
'00000000000000000000000000000000', '00000000000000000000000000000000', BITSPERBLOCK,
DIR_DECRYPT);
write(fp,#13+#10+'===========');
CloseFile(fp);
end; {* makeMCTs *}
begin
{* main *}
makeKATs('ecb_vk.txt', 'ecb_vt.txt', 'ecb_tbl.txt', 'ecb_iv.txt');
makeMCTs('ecb_e_m.txt', 'ecb_d_m.txt', 'cbc_e_m.txt', 'cbc_d_m.txt');
wait;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -