Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_dh.py: 50%

34 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-03-14 06:36 +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 

14# Copyright 2022 Google LLC 

15# 

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

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

18# You may obtain a copy of the License at 

19# 

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

21# 

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

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

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

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

26# limitations under the License. 

27import sys 

28import atheris 

29with atheris.instrument_imports(): 

30 from cryptography.hazmat.primitives import hashes 

31 from cryptography.hazmat.primitives.asymmetric import dh 

32 from cryptography.hazmat.primitives.kdf.hkdf import HKDF 

33 

34def TestInput(input_bytes): 

35 fdp = atheris.FuzzedDataProvider(input_bytes) 

36 

37 try: 

38 parameters = dh.generate_parameters( 

39 generator=(2 if fdp.ConsumeBool() else 5), 

40 key_size=fdp.ConsumeInt(4) 

41 ) 

42 except ValueError as e: 

43 # This can only happen if the key_size isn't valid 

44 return 

45 

46 server_private_key = parameters.generate_private_key() 

47 peer_private_key = parameters.generate_private_key() 

48 server_derived_shared_key = server_private_key.exchange(peer_private_key.public_key()) 

49 peer_derived_shared_key = peer_private_key.exchange(server_private_key.public_key()) 

50 

51 infobytes = fdp.ConsumeBytes(10) 

52 

53 server_derived_key = HKDF( 

54 algorithm=hashes.SHA256(), 

55 length=32, 

56 salt=None, 

57 info=infobytes, 

58 ).derive(server_derived_shared_key) 

59 

60 peer_derived_key = HKDF( 

61 algorithm=hashes.SHA256(), 

62 length=32, 

63 salt=None, 

64 info=infobytes, 

65 ).derive(peer_derived_shared_key) 

66 

67 assert (server_derived_key == peer_derived_key), "Key Derivation Error!!" 

68 

69def main(): 

70 atheris.Setup(sys.argv, TestInput, enable_python_coverage=False) 

71 atheris.instrument_all() 

72 atheris.Fuzz() 

73 

74if __name__ == "__main__": 

75 main()