Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_aes.py: 54%

39 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 07:03 +0000

1###### Coverage stub 

2import atexit 

3import coverage 

4cov = coverage.coverage(data_file='.coverage', cover_pylib=True) 

5cov.start() 

6# Register an exist handler that will print coverage 

7def exit_handler(): 

8 cov.stop() 

9 cov.save() 

10atexit.register(exit_handler) 

11####### End of coverage stub 

12#!/usr/bin/python3 

13# Copyright 2022 Google LLC 

14# 

15# Licensed under the Apache License, Version 2.0 (the "License"); 

16# you may not use this file except in compliance with the License. 

17# You may obtain a copy of the License at 

18# 

19# http://www.apache.org/licenses/LICENSE-2.0 

20# 

21# Unless required by applicable law or agreed to in writing, software 

22# distributed under the License is distributed on an "AS IS" BASIS, 

23# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

24# See the License for the specific language governing permissions and 

25# limitations under the License. 

26 

27import atheris 

28import sys 

29 

30with atheris.instrument_imports(): 

31 from Crypto.Cipher import AES 

32 

33 

34@atheris.instrument_func 

35def TestOneInput(data): 

36 if len(data) < 40: 

37 return 

38 fdp = atheris.FuzzedDataProvider(data) 

39 key = fdp.ConsumeBytes(16) 

40 IV = fdp.ConsumeBytes(16) 

41 enc_data = fdp.ConsumeBytes(atheris.ALL_REMAINING) 

42 

43 # All modes: https://github.com/pycrypto/pycrypto/blob/7acba5f3a6ff10f1424c309d0d34d2b713233019/lib/Crypto/Cipher/AES.py#L183 

44 # minus CTR, ECB, PGP (not supported) 

45 modes = [ 

46 AES.MODE_CBC, 

47 AES.MODE_CFB, 

48 AES.MODE_OFB, 

49 AES.MODE_OPENPGP, 

50 AES.MODE_CCM, 

51 AES.MODE_EAX, 

52 AES.MODE_SIV, 

53 AES.MODE_GCM 

54 ] 

55 for mode in modes: 

56 try: 

57 obj = AES.new(key, mode, IV) 

58 except ValueError as e: 

59 if not ( 

60 "Key cannot be the null string" in str(e) or 

61 "Length of parameter" in str(e) 

62 ): 

63 raise e 

64 return 

65 

66 try: 

67 ciphertext = obj.encrypt(enc_data) 

68 except ValueError as e: 

69 if not "Input strings must be a multiple of 16 in length" in str(e): 

70 raise e 

71 

72 

73def main(): 

74 atheris.instrument_all() 

75 atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True) 

76 atheris.Fuzz() 

77 

78if __name__ == "__main__": 

79 main()