Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Jan 3, 2024
1 parent 8112cad commit 6d1f347
Show file tree
Hide file tree
Showing 12 changed files with 639 additions and 304 deletions.
102 changes: 68 additions & 34 deletions test/crypto/aead_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,35 @@ void main() {
'key': '7c77d6e813bed5ac98baa417477a2e7d',
'nonce': '1a8c98dcd73d38393b2bf1569deefc19',
'header': '65d2017990d62528',
'cipher': '02083e3979da014812f59f11d52630da30137327d10649b0aa6e1c181db617d7f2',
'cipher':
'02083e3979da014812f59f11d52630da30137327d10649b0aa6e1c181db617d7f2',
},
{
'name': 'Test Case 8',
'msg': '1bda122bce8a8dbaf1877d962b8592dd2d56',
'key': '5fff20cafab119ca2fc73549e20f5b0d',
'nonce': 'dde59b97d722156d4d9aff2bc7559826',
'header': '54b9f04e6a09189a',
'cipher': '2ec47b2c4954a489afc7ba4897edcdae8cc33b60450599bd02c96382902aef7f832a',
'cipher':
'2ec47b2c4954a489afc7ba4897edcdae8cc33b60450599bd02c96382902aef7f832a',
},
{
'name': 'Test Case 9',
'msg': '6cf36720872b8513f6eab1a8a44438d5ef11',
'key': 'a4a4782bcffd3ec5e7ef6d8c34a56123',
'nonce': 'b781fcf2f75fa5a8de97a9ca48e522ec',
'header': '899a175897561d7e',
'cipher': '0de18fd0fdd91e7af19f1d8ee8733938b1e8e7f6d2231618102fdb7fe55ff1991700',
'cipher':
'0de18fd0fdd91e7af19f1d8ee8733938b1e8e7f6d2231618102fdb7fe55ff1991700',
},
{
'name': 'Test Case 10',
'msg': 'ca40d7446e545ffaed3bd12a740a659ffbbb3ceab7',
'key': '8395fcf1e95bebd697bd010bc766aac3',
'nonce': '22e7add93cfc6393c57ec0b3c17d6b44',
'header': '126735fcc320d25a',
'cipher': 'cb8920f87a6c75cff39627b56e3ed197c552d295a7cfc46afc253b4652b1af3795b124ab6e',
'cipher':
'cb8920f87a6c75cff39627b56e3ed197c552d295a7cfc46afc253b4652b1af3795b124ab6e',
},
];
for (var map in testVectors) {
Expand All @@ -103,21 +107,27 @@ void main() {

/// encryption test
var ct = eax.encrypt(msg, nonce, header);
expect(ct, equals(cipher), reason: 'encryption test $map["name"] did not match output');
expect(ct, equals(cipher),
reason: 'encryption test $map["name"] did not match output');

/// decryption test with verification
var pt = eax.decrypt(cipher, nonce, header);
expect(pt, equals(msg), reason: 'decryption test $map["name"] did not match output');
expect(pt, equals(msg),
reason: 'decryption test $map["name"] did not match output');

/// testing without additional data
ct = eax.encrypt(msg, nonce, Uint8List(0));
pt = eax.decrypt(ct, nonce, Uint8List(0));
expect(pt, equals(msg), reason: 'test $map["name"] did not match output');
expect(pt, equals(msg),
reason: 'test $map["name"] did not match output');

/// testing with multiple additional data
ct = eax.encrypt(msg, nonce, Uint8List.fromList([...header, ...header, ...header]));
pt = eax.decrypt(ct, nonce, Uint8List.fromList([...header, ...header, ...header]));
expect(pt, equals(msg), reason: 'test $map["name"] did not match output');
ct = eax.encrypt(
msg, nonce, Uint8List.fromList([...header, ...header, ...header]));
pt = eax.decrypt(
ct, nonce, Uint8List.fromList([...header, ...header, ...header]));
expect(pt, equals(msg),
reason: 'test $map["name"] did not match output');
});
}
});
Expand Down Expand Up @@ -179,7 +189,8 @@ void main() {
'N': 'bbaa99887766554433221107',
'A': '000102030405060708090a0b0c0d0e0f1011121314151617',
'P': '000102030405060708090a0b0c0d0e0f1011121314151617',
'C': '1ca2207308c87c010756104d8840ce1952f09673a448a122c92c62241051f57356d7f3c90bb0e07f',
'C':
'1ca2207308c87c010756104d8840ce1952f09673a448a122c92c62241051f57356d7f3c90bb0e07f',
},
{
'name': 'Test Case 9',
Expand All @@ -193,14 +204,16 @@ void main() {
'N': 'bbaa99887766554433221109',
'A': '',
'P': '000102030405060708090a0b0c0d0e0f1011121314151617',
'C': '221bd0de7fa6fe993eccd769460a0af2d6cded0c395b1c3ce725f32494b9f914d85c0b1eb38357ff',
'C':
'221bd0de7fa6fe993eccd769460a0af2d6cded0c395b1c3ce725f32494b9f914d85c0b1eb38357ff',
},
{
'name': 'Test Case 11',
'N': 'bbaa9988776655443322110a',
'A': '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
'P': '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
'C': 'bd6f6c496201c69296c11efd138a467abd3c707924b964deaffc40319af5a48540fbba186c5553c68ad9f592a79a4240',
'C':
'bd6f6c496201c69296c11efd138a467abd3c707924b964deaffc40319af5a48540fbba186c5553c68ad9f592a79a4240',
},
{
'name': 'Test Case 12',
Expand All @@ -214,28 +227,33 @@ void main() {
'N': 'bbaa9988776655443322110c',
'A': '',
'P': '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
'C': '2942bfc773bda23cabc6acfd9bfd5835bd300f0973792ef46040c53f1432bcdfb5e1dde3bc18a5f840b52e653444d5df',
'C':
'2942bfc773bda23cabc6acfd9bfd5835bd300f0973792ef46040c53f1432bcdfb5e1dde3bc18a5f840b52e653444d5df',
},
{
'name': 'Test Case 14',
'N': 'bbaa9988776655443322110d',
'A': '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021222324252627',
'P': '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021222324252627',
'A':
'000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021222324252627',
'P':
'000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021222324252627',
'C':
'd5ca91748410c1751ff8a2f618255b68a0a12e093ff454606e59f9c1d0ddc54b65e8628e568bad7aed07ba06a4a69483a7035490c5769e60',
},
{
'name': 'Test Case 15',
'N': 'bbaa9988776655443322110e',
'A': '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021222324252627',
'A':
'000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021222324252627',
'P': '',
'C': 'c5cd9d1850c141e358649994ee701b68',
},
{
'name': 'Test Case 16',
'N': 'bbaa9988776655443322110f',
'A': '',
'P': '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021222324252627',
'P':
'000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021222324252627',
'C':
'4412923493c57d5de0d700f753cce0d1d2d95060122e9f15a5ddbfc5787e50b5cc55ee507bcb084e479ad363ac366b95a98ca5f3000b1479',
},
Expand All @@ -251,21 +269,27 @@ void main() {

/// encryption test
var ct = ocb.encrypt(msg, nonce, header);
expect(ct, equals(cipher), reason: 'encryption test $map["name"] did not match output');
expect(ct, equals(cipher),
reason: 'encryption test $map["name"] did not match output');

/// decryption test with verification
var pt = ocb.decrypt(cipher, nonce, header);
expect(pt, equals(msg), reason: 'decryption test $map["name"] did not match output');
expect(pt, equals(msg),
reason: 'decryption test $map["name"] did not match output');

/// testing without additional data
ct = ocb.encrypt(msg, nonce, Uint8List(0));
pt = ocb.decrypt(ct, nonce, Uint8List(0));
expect(pt, equals(msg), reason: 'test $map["name"] did not match output');
expect(pt, equals(msg),
reason: 'test $map["name"] did not match output');

/// testing with multiple additional data
ct = ocb.encrypt(msg, nonce, Uint8List.fromList([...header, ...header, ...header]));
pt = ocb.decrypt(ct, nonce, Uint8List.fromList([...header, ...header, ...header]));
expect(pt, equals(msg), reason: 'test $map["name"] did not match output');
ct = ocb.encrypt(
msg, nonce, Uint8List.fromList([...header, ...header, ...header]));
pt = ocb.decrypt(
ct, nonce, Uint8List.fromList([...header, ...header, ...header]));
expect(pt, equals(msg),
reason: 'test $map["name"] did not match output');
});
}
});
Expand Down Expand Up @@ -400,7 +424,8 @@ void main() {
},
{
'name': 'Test Case 13',
'key': '0000000000000000000000000000000000000000000000000000000000000000',
'key':
'0000000000000000000000000000000000000000000000000000000000000000',
'iv': '000000000000000000000000',
'aad': '',
'input': '',
Expand All @@ -409,7 +434,8 @@ void main() {
},
{
'name': 'Test Case 14',
'key': '0000000000000000000000000000000000000000000000000000000000000000',
'key':
'0000000000000000000000000000000000000000000000000000000000000000',
'iv': '000000000000000000000000',
'aad': '',
'input': '00000000000000000000000000000000',
Expand All @@ -418,7 +444,8 @@ void main() {
},
{
'name': 'Test Case 15',
'key': 'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
'key':
'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
'iv': 'cafebabefacedbaddecaf888',
'aad': '',
'input':
Expand All @@ -429,7 +456,8 @@ void main() {
},
{
'name': 'Test Case 16',
'key': 'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
'key':
'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
'iv': 'cafebabefacedbaddecaf888',
'aad': 'feedfacedeadbeeffeedfacedeadbeefabaddad2',
'input':
Expand All @@ -440,7 +468,8 @@ void main() {
},
{
'name': 'Test Case 17',
'key': 'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
'key':
'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
'iv': 'cafebabefacedbad',
'aad': 'feedfacedeadbeeffeedfacedeadbeefabaddad2',
'input':
Expand All @@ -451,7 +480,8 @@ void main() {
},
{
'name': 'Test Case 18',
'key': 'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
'key':
'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
'iv':
'9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b',
'aad': 'feedfacedeadbeeffeedfacedeadbeefabaddad2',
Expand Down Expand Up @@ -484,17 +514,21 @@ void main() {

/// decryption test with verification
var pt = gcm.decrypt(Uint8List.fromList([...output, ...mac]), iv, aad);
expect(pt, equals(input), reason: 'decryption test $map["name"] did not match output');
expect(pt, equals(input),
reason: 'decryption test $map["name"] did not match output');

/// testing without additional data
ct = gcm.encrypt(input, iv, Uint8List(0));
pt = gcm.decrypt(ct, iv, Uint8List(0));
expect(pt, equals(input), reason: 'test $map["name"] did not match output');
expect(pt, equals(input),
reason: 'test $map["name"] did not match output');

/// testing with multiple additional data
ct = gcm.encrypt(input, iv, Uint8List.fromList([...aad, ...aad, ...aad]));
ct = gcm.encrypt(
input, iv, Uint8List.fromList([...aad, ...aad, ...aad]));
pt = gcm.decrypt(ct, iv, Uint8List.fromList([...aad, ...aad, ...aad]));
expect(pt, equals(input), reason: 'test $map["name"] did not match output');
expect(pt, equals(input),
reason: 'test $map["name"] did not match output');
});
}
});
Expand Down
34 changes: 23 additions & 11 deletions test/crypto/asymmetric_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ void main() {

final engine = ElGamalEngine();
engine.init(true, PublicKeyParameter<ElGamalPublicKey>(publicKey));
expect(engine.outputBlockSize, 2048 ~/ 4, reason: "2048 outputBlockSize on encryption failed.");
expect(engine.outputBlockSize, 2048 ~/ 4,
reason: "2048 outputBlockSize on encryption failed.");

final message = faker.randomGenerator.string(prime.byteLength).stringToBytes();
final message =
faker.randomGenerator.string(prime.byteLength).stringToBytes();
final plainText = message;
final cipherText = Uint8List(engine.outputBlockSize);
engine.processBlock(plainText, 0, plainText.length, cipherText, 0);

engine.init(false, PrivateKeyParameter<ElGamalPrivateKey>(privateKey));
engine.processBlock(cipherText, 0, cipherText.length, plainText, 0);

expect(engine.outputBlockSize, (2048 ~/ 8) - 1, reason: "2048 outputBlockSize on decryption failed.");
expect(engine.outputBlockSize, (2048 ~/ 8) - 1,
reason: "2048 outputBlockSize on decryption failed.");
expect(message, equals(plainText), reason: '2048 bit test failed');
});

Expand All @@ -54,8 +57,10 @@ void main() {
final keyPair = keyGen.generateKeyPair();

final engine = ElGamalEngine();
engine.init(true, PublicKeyParameter<ElGamalPublicKey>(keyPair.publicKey));
expect(engine.outputBlockSize, 2048 ~/ 4, reason: "2048 outputBlockSize on encryption failed.");
engine.init(
true, PublicKeyParameter<ElGamalPublicKey>(keyPair.publicKey));
expect(engine.outputBlockSize, 2048 ~/ 4,
reason: "2048 outputBlockSize on encryption failed.");

final message = faker.randomGenerator
.string(
Expand All @@ -66,8 +71,10 @@ void main() {
final cipherText = Uint8List(engine.outputBlockSize);
engine.processBlock(plainText, 0, plainText.length, cipherText, 0);

engine.init(false, PrivateKeyParameter<ElGamalPrivateKey>(keyPair.privateKey));
expect(engine.outputBlockSize, (2048 ~/ 8) - 1, reason: "2048 outputBlockSize on decryption failed.");
engine.init(
false, PrivateKeyParameter<ElGamalPrivateKey>(keyPair.privateKey));
expect(engine.outputBlockSize, (2048 ~/ 8) - 1,
reason: "2048 outputBlockSize on decryption failed.");

engine.processBlock(cipherText, 0, cipherText.length, plainText, 0);
expect(message, equals(plainText), reason: '2048 bit test failed');
Expand All @@ -77,17 +84,20 @@ void main() {
group('DSA signer', (() {
final prime = BigInt.parse(
'18162137922021319783039084688936650943756676913936359487644230524033744038416716640315362119531957065117691808427264479820376952164824666951768158261158011465383622961646140102588814345836301228952436368478471605675244965240663395560393520043446488959085100446039711212792538929022066114201273802300832755863321968834472697137828335693635804943342266260626637109532050720555091308772127451653755872557451491248990382251482443887099369600464777831608990878007068814783464418617901906104198108074206903909793174142273612557335586051547777907089455850194383049226681257881252609518716935491786310874981455522347755965263');
final order = BigInt.parse('66237724685660121164013399122464209600863963990328366006106224772467420697001');
final order = BigInt.parse(
'66237724685660121164013399122464209600863963990328366006106224772467420697001');
final generator = BigInt.parse(
'693117913754860276426473281664845283847827874078831837566781765224888221249721170562868171267423414589469577529462100295651372077651432728133000631159241493543615314643538727517688679127996156948227084734117835362992668110067565260029784027890436946325120986252996031434727021897912364504906315612769347582107563243931645185214720978427405505592726492013000567393825423372958968113840944031967516897982342279432489751673785605929305317306465162868047750314617256208362522435739505165126345703258213171855603629264914838294962758085762547804458344916991647240641414079945072645196179749686064721950595476461703347596');
final publicExponent = BigInt.parse(
'4782172781034192998693423575547001875121450860285384286800905429000791264609187331114128879953098324124068264308502771574321383159125468535909140265000623111180145376619101796653939164726433685123758969904269839090078039075889777192894408978360938352224532502867949812284119229251938382586523438070582120724642233025483830096315195183698731083960541920397786806884931581765685611516570402002856528710913800628319231592500299988215432415250617341963426900030521545457294059283280813544106638008667172693314074865970063708048659226032286932981816054377197606962836326219108126026753202594298484748732919893514434857817');
final secretExponent = BigInt.parse('4058353653331808916569051257818485824406724876273357155551230247524105887159');
final secretExponent = BigInt.parse(
'4058353653331808916569051257818485824406724876273357155551230247524105887159');

final privateKey = DSAPrivateKey(secretExponent, prime, order, generator);
final publicKey = DSAPublicKey(publicExponent, prime, order, generator);

final message = faker.randomGenerator.string(prime.byteLength).stringToBytes();
final message =
faker.randomGenerator.string(prime.byteLength).stringToBytes();

test('With sha1 test', (() {
final signer = DSASigner(Digest('SHA-1'));
Expand Down Expand Up @@ -146,6 +156,8 @@ void main() {

final aliceShared = bobPublic.modPow(alicePrivate, prime);
final bobShared = alicePublic.modPow(bobPrivate, prime);
expect(aliceShared, bobShared, reason: 'Failed asserting that Alice and Bob share the same BigInteger.');
expect(aliceShared, bobShared,
reason:
'Failed asserting that Alice and Bob share the same BigInteger.');
}));
}
12 changes: 8 additions & 4 deletions test/crypto/symmetric_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ void main() {
_blockCipherVectorTest(
3,
CBCBlockCipher(TwofishEngine()),
_kpWithIV('0123456789abcdef1234567890abcdef', '1234567890abcdef0123456789abcdef'),
_kpWithIV('0123456789abcdef1234567890abcdef',
'1234567890abcdef0123456789abcdef'),
input,
'd6bfdbb2090562e960273783127e2658',
);
Expand Down Expand Up @@ -319,14 +320,17 @@ ParametersWithIV<KeyParameter> _kpWithIV(String key, String iv) {
return ParametersWithIV(_kp(key), iv.hexToBytes());
}

void _blockCipherVectorTest(int id, BlockCipher engine, CipherParameters params, String input, String output) {
void _blockCipherVectorTest(int id, BlockCipher engine, CipherParameters params,
String input, String output) {
final inBytes = input.hexToBytes();
final outBytes = output.hexToBytes();

final cipher = BufferedCipher(engine);
cipher.init(true, params);
expect(outBytes, equals(cipher.process(inBytes)), reason: '${cipher.algorithmName} test $id did not match output');
expect(outBytes, equals(cipher.process(inBytes)),
reason: '${cipher.algorithmName} test $id did not match output');

cipher.init(false, params);
expect(inBytes, equals(cipher.process(outBytes)), reason: '${cipher.algorithmName} test $id did not match input');
expect(inBytes, equals(cipher.process(outBytes)),
reason: '${cipher.algorithmName} test $id did not match input');
}
3 changes: 2 additions & 1 deletion test/data/s2k_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const mode1Password123456 = 'jAwECQECqEKnqVn6Qio=';
const mode1PasswordFoobar = 'jAwECQECvJVYRYE8fDc=';
const mode3Aes128Password13Times0123456789 = 'jA0EBwMCBuRhXKRI+d3u';
const mode3Aes192Password123 = 'jA0ECAMCj4F0xdlhx3nu';
const mode3EncryptedKeyPasswordBgtyhn = 'jC4EBwMCglmgbpjalBzuCM5Wxz59Z2/MekL482qgQu4ZSP0MmZ9OiPAOHkSr7OjG';
const mode3EncryptedKeyPasswordBgtyhn =
'jC4EBwMCglmgbpjalBzuCM5Wxz59Z2/MekL482qgQu4ZSP0MmZ9OiPAOHkSr7OjG';
const mode3Password9876 = 'jA0ECQMCuWfqllPbasgr';
const mode3PasswordQwerty = 'jA0ECQMCeEXwW1X3tJ7x';
const mode3TwofishPassword13Times0123456789 = 'jA0ECgMCUe38FUVAZazu';
Loading

0 comments on commit 6d1f347

Please sign in to comment.