|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -import 'dart:typed_data'; |
16 | | - |
17 | 15 | import 'package:cryptography/cryptography.dart'; |
18 | 16 | import 'package:cryptography/dart.dart'; |
19 | 17 | import 'package:cryptography/src/_internal/hex.dart'; |
@@ -750,121 +748,4 @@ void _main() { |
750 | 748 | }); |
751 | 749 | }); |
752 | 750 | }); |
753 | | - |
754 | | - Future<void> testRounds({ |
755 | | - required AesGcm algorithm, |
756 | | - List<int> aad = const [], |
757 | | - int rounds = 1000, |
758 | | - required String expectedHex, |
759 | | - }) async { |
760 | | - var secretKeyBytes = hexToBytes( |
761 | | - '02020202020202020202020202020202' |
762 | | - '02020202020202020202020202020202', |
763 | | - ).sublist(0, algorithm.secretKeyLength); |
764 | | - var nonce = hexToBytes( |
765 | | - '03030303030303030303030303030303', |
766 | | - ); |
767 | | - final hashAlgorithm = Sha256(); |
768 | | - |
769 | | - var data = List<int>.filled(rounds + 1000, 1); |
770 | | - late Mac mac; |
771 | | - for (var i = 0; i < rounds; i++) { |
772 | | - // Encrypt |
773 | | - final secretBox = await algorithm.encrypt( |
774 | | - data, |
775 | | - secretKey: SecretKey(secretKeyBytes), |
776 | | - nonce: nonce, |
777 | | - aad: aad, |
778 | | - ); |
779 | | - expect(secretBox.nonce, nonce); |
780 | | - mac = secretBox.mac; |
781 | | - expect(mac.bytes, hasLength(16)); |
782 | | - |
783 | | - // Test that decryption works |
784 | | - final decryptedSecretBox = await algorithm.decrypt( |
785 | | - secretBox, |
786 | | - secretKey: SecretKey(secretKeyBytes), |
787 | | - aad: aad, |
788 | | - ); |
789 | | - expect(decryptedSecretBox, data); |
790 | | - |
791 | | - // Change data. |
792 | | - // Put MAC somewhere in the data. |
793 | | - data = Uint8List.fromList(secretBox.cipherText); |
794 | | - data.setRange(100, 100 + 16, secretBox.mac.bytes); |
795 | | - |
796 | | - // Change size for the next round |
797 | | - data = data.sublist(1); |
798 | | - |
799 | | - // Change secret key |
800 | | - secretKeyBytes = (await hashAlgorithm.hash(data)) |
801 | | - .bytes |
802 | | - .sublist(0, algorithm.secretKeyLength); |
803 | | - |
804 | | - // Change nonce |
805 | | - nonce = (await hashAlgorithm.hash(secretKeyBytes)).bytes.sublist(0, 12); |
806 | | - } |
807 | | - |
808 | | - expect(data, hasLength(1000)); |
809 | | - final hash = await hashAlgorithm.hash(data); |
810 | | - expect( |
811 | | - hexFromBytes(hash.bytes), |
812 | | - expectedHex, |
813 | | - ); |
814 | | - |
815 | | - // We don't need to test MAC because its part of the clearText at each round |
816 | | - // after the first one. |
817 | | - } |
818 | | - |
819 | | - // |
820 | | - // The following test vectors were calculated with Web Cryptography API. |
821 | | - // |
822 | | - |
823 | | - test('AesGcm.with128bits(): 1 000 cycles', () async { |
824 | | - await testRounds( |
825 | | - algorithm: AesGcm.with128bits(), |
826 | | - expectedHex: '' |
827 | | - 'f3 52 fb 3f a0 3d 70 25 f3 0f 48 01 eb 3a d2 85\n' |
828 | | - '89 53 06 4d 8c 53 25 38 96 ca 71 c2 90 f0 3b 06', |
829 | | - ); |
830 | | - }); |
831 | | - |
832 | | - test('AesGcm.with128bits(): 1 000 cycles, AAD', () async { |
833 | | - await testRounds( |
834 | | - algorithm: AesGcm.with128bits(), |
835 | | - aad: [1, 2, 3], |
836 | | - expectedHex: '' |
837 | | - 'e5 a3 57 59 6c 11 c0 ab 18 e6 b3 f5 71 6a f9 46\n' |
838 | | - '25 ce 95 7d eb 29 c4 bf 24 80 6d 33 e2 f5 1f 2b', |
839 | | - ); |
840 | | - }); |
841 | | - |
842 | | - test('AesGcm.with192bits(): 1 000 cycles, AAD', () async { |
843 | | - await testRounds( |
844 | | - algorithm: AesGcm.with192bits(), |
845 | | - aad: [1, 2, 3], |
846 | | - expectedHex: '' |
847 | | - '76 5d 6c 02 16 56 a0 ef 22 e1 97 5c 4b 81 fc 36\n' |
848 | | - '2b 9c 5a da d9 2d 6c d5 c9 94 c7 a2 c8 73 e7 ab', |
849 | | - ); |
850 | | - }); |
851 | | - |
852 | | - test('AesGcm.with256bits(): 1 000 cycles', () async { |
853 | | - await testRounds( |
854 | | - algorithm: AesGcm.with256bits(), |
855 | | - expectedHex: '' |
856 | | - '2e f9 45 f9 4b 94 43 d9 1a 43 3d c2 e4 40 c0 0f\n' |
857 | | - '20 27 0d 93 3d 47 ae 44 41 29 6e 3c 32 27 97 ef', |
858 | | - ); |
859 | | - }); |
860 | | - |
861 | | - test('AesGcm.with256bits(): 1 000 cycles, AAD', () async { |
862 | | - await testRounds( |
863 | | - algorithm: AesGcm.with256bits(), |
864 | | - aad: [1, 2, 3], |
865 | | - expectedHex: '' |
866 | | - '52 dd c6 e5 07 65 f1 46 7e 1c 5a f5 9f cb 0c 69\n' |
867 | | - '84 11 c1 52 83 08 b5 3b 19 28 d5 79 bd 2f aa c7', |
868 | | - ); |
869 | | - }); |
870 | 751 | } |
0 commit comments