|
| 1 | +// Copyright (c) 2026, Sudipto Chandra |
| 2 | +// All rights reserved. Check LICENSE file for details. |
| 3 | + |
| 4 | +// ignore_for_file: library_annotations |
| 5 | + |
| 6 | +// The Blowfish and Twofish engines only exist in pointycastle >= 4.0.0, which |
| 7 | +// requires Dart SDK 3.2+. On the 2.19 floor pub resolves an older pointycastle |
| 8 | +// without these libraries, so this file cannot compile there. The generic |
| 9 | +// `stable-only` tag lets CI exclude it (`-x stable-only`) on non-stable SDKs, |
| 10 | +// where the file is skipped at the parse phase before it is ever compiled. |
| 11 | +@Tags(['vm-only', 'stable-only']) |
| 12 | + |
| 13 | +import 'dart:typed_data' show Uint8List; |
| 14 | + |
| 15 | +import 'package:cipherlib/cipherlib.dart' as my; |
| 16 | +import 'package:cipherlib/codecs.dart'; |
| 17 | +import 'package:hashlib/random.dart'; |
| 18 | +import 'package:pointycastle/pointycastle.dart' as pc; |
| 19 | +import 'package:pointycastle/block/blowfish.dart'; |
| 20 | +import 'package:pointycastle/block/modes/cbc.dart'; |
| 21 | +import 'package:pointycastle/block/modes/ecb.dart'; |
| 22 | +import 'package:pointycastle/block/twofish.dart'; |
| 23 | +import 'package:pointycastle/stream/ctr.dart'; |
| 24 | +import 'package:test/test.dart'; |
| 25 | + |
| 26 | +void main() { |
| 27 | + group('Blowfish', () { |
| 28 | + test('pointycastle: ECB encryption', () { |
| 29 | + // pointycastle only supports keys of 4 to 56 bytes |
| 30 | + for (int k = 4; k <= 56; k += 4) { |
| 31 | + var key = randomBytes(k); |
| 32 | + for (int j = 8; j < 200; j += 8) { |
| 33 | + var text = randomBytes(j); |
| 34 | + var result = my.Blowfish.noPadding(key).ecb().encrypt(text); |
| 35 | + var instance = ECBBlockCipher(BlowfishEngine()); |
| 36 | + instance.init(true, pc.KeyParameter(key)); |
| 37 | + var out = Uint8List(j); |
| 38 | + for (int i = 0; i < j; i += 8) { |
| 39 | + instance.processBlock(text, i, out, i); |
| 40 | + } |
| 41 | + expect(toHex(out), equals(toHex(result)), |
| 42 | + reason: '[key: $k, size: $j]'); |
| 43 | + } |
| 44 | + } |
| 45 | + }); |
| 46 | + test('pointycastle: CBC encryption', () { |
| 47 | + // pointycastle only supports keys of 4 to 56 bytes |
| 48 | + for (int k = 4; k <= 56; k += 4) { |
| 49 | + var key = randomBytes(k); |
| 50 | + for (int j = 8; j < 200; j += 8) { |
| 51 | + var text = randomBytes(j); |
| 52 | + var iv = randomBytes(8); |
| 53 | + var result = my.Blowfish.noPadding(key).cbc(iv).encrypt(text); |
| 54 | + var instance = CBCBlockCipher(BlowfishEngine()); |
| 55 | + instance.init(true, pc.ParametersWithIV(pc.KeyParameter(key), iv)); |
| 56 | + var out = Uint8List(j); |
| 57 | + for (int i = 0; i < j; i += 8) { |
| 58 | + instance.processBlock(text, i, out, i); |
| 59 | + } |
| 60 | + expect(toHex(out), equals(toHex(result)), |
| 61 | + reason: '[key: $k, size: $j]'); |
| 62 | + } |
| 63 | + } |
| 64 | + }); |
| 65 | + test('pointycastle: CTR encryption', () { |
| 66 | + // pointycastle only supports keys of 4 to 56 bytes |
| 67 | + for (int k = 4; k <= 56; k += 4) { |
| 68 | + var key = randomBytes(k); |
| 69 | + for (int j = 0; j < 200; ++j) { |
| 70 | + var text = randomBytes(j); |
| 71 | + var iv = randomBytes(8); |
| 72 | + var result = my.Blowfish(key).ctr(iv).encrypt(text); |
| 73 | + var instance = CTRStreamCipher(BlowfishEngine()); |
| 74 | + instance.init(true, pc.ParametersWithIV(pc.KeyParameter(key), iv)); |
| 75 | + var out = instance.process(text); |
| 76 | + expect(toHex(out), equals(toHex(result)), |
| 77 | + reason: '[key: $k, size: $j]'); |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + group('Twofish', () { |
| 84 | + test('pointycastle: ECB encryption', () { |
| 85 | + for (int k in [16, 24, 32]) { |
| 86 | + var key = randomBytes(k); |
| 87 | + for (int j = 16; j < 300; j += 16) { |
| 88 | + var text = randomBytes(j); |
| 89 | + var result = my.Twofish.noPadding(key).ecb().encrypt(text); |
| 90 | + var instance = ECBBlockCipher(TwofishEngine()); |
| 91 | + instance.init(true, pc.KeyParameter(key)); |
| 92 | + var out = Uint8List(j); |
| 93 | + for (int i = 0; i < j; i += 16) { |
| 94 | + instance.processBlock(text, i, out, i); |
| 95 | + } |
| 96 | + expect(toHex(out), equals(toHex(result)), |
| 97 | + reason: '[key: $k, size: $j]'); |
| 98 | + } |
| 99 | + } |
| 100 | + }); |
| 101 | + test('pointycastle: CBC encryption', () { |
| 102 | + for (int k in [16, 24, 32]) { |
| 103 | + var key = randomBytes(k); |
| 104 | + for (int j = 16; j < 300; j += 16) { |
| 105 | + var text = randomBytes(j); |
| 106 | + var iv = randomBytes(16); |
| 107 | + var result = my.Twofish.noPadding(key).cbc(iv).encrypt(text); |
| 108 | + var instance = CBCBlockCipher(TwofishEngine()); |
| 109 | + instance.init(true, pc.ParametersWithIV(pc.KeyParameter(key), iv)); |
| 110 | + var out = Uint8List(j); |
| 111 | + for (int i = 0; i < j; i += 16) { |
| 112 | + instance.processBlock(text, i, out, i); |
| 113 | + } |
| 114 | + expect(toHex(out), equals(toHex(result)), |
| 115 | + reason: '[key: $k, size: $j]'); |
| 116 | + } |
| 117 | + } |
| 118 | + }); |
| 119 | + test('pointycastle: CTR encryption', () { |
| 120 | + for (int k in [16, 24, 32]) { |
| 121 | + var key = randomBytes(k); |
| 122 | + for (int j = 0; j < 200; ++j) { |
| 123 | + var text = randomBytes(j); |
| 124 | + var iv = randomBytes(16); |
| 125 | + var result = my.Twofish(key).ctr(iv, 128).encrypt(text); |
| 126 | + var instance = CTRStreamCipher(TwofishEngine()); |
| 127 | + instance.init(true, pc.ParametersWithIV(pc.KeyParameter(key), iv)); |
| 128 | + var out = instance.process(text); |
| 129 | + expect(toHex(out), equals(toHex(result)), |
| 130 | + reason: '[key: $k, size: $j]'); |
| 131 | + } |
| 132 | + } |
| 133 | + }); |
| 134 | + }); |
| 135 | +} |
0 commit comments