prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  KRACK module requires python-cryptography
  When `python-cryptography` or `python3-cryptography` packages are not installed, the KRACK module cannot be loaded.
  
  ```
  >>> load_module("krack")
  ERROR: Loading module scapy.modules.krack
  Traceback (most recent call last):
    File "/home/ria/scapy/scapy/main.py", line 150, in _load
      mod = importlib.import_module(module)
    File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
    File "<frozen importlib._bootstrap>", line 983, in _find_and_load
    File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 728, in exec_module
    File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
    File "/home/ria/scapy/scapy/modules/krack/__init__.py", line 28, in <module>
      from scapy.modules.krack.automaton import KrackAP  # noqa: F401
    File "/home/ria/scapy/scapy/modules/krack/automaton.py", line 7, in <module>
      from cryptography.hazmat.primitives import hashes
  ModuleNotFoundError: No module named 'cryptography'
  ```
  
  Calling @commial to assess whether the module could still offer functionalities with a `crypto_valid` around some blocks. But at first glance I believe a missing `cryptography` should completely prevent importing the module.
  
  https://github.com/secdev/scapy/blob/a58e1b90a704c394216a0b5a864a50931754bdf7/scapy/modules/krack/automaton.py#L6-L10
  
  https://github.com/secdev/scapy/blob/a58e1b90a704c394216a0b5a864a50931754bdf7/scapy/modules/krack/crypto.py#L6-L9
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `scapy/modules/krack/__init__.py`
  Content:
  ```
  1 """Module implementing Krack Attack on client, as a custom WPA Access Point
  2 
  3 More details on the attack can be found on https://www.krackattacks.com/
  4 
  5 Example of use (from the scapy shell):
  6 >>> load_module("krack")
  7 >>> KrackAP(
  8     iface="mon0",               # A monitor interface
  9     ap_mac='11:22:33:44:55:66', # MAC (BSSID) to use
  10     ssid="TEST_KRACK",          # SSID
  11     passphrase="testtest",      # Associated passphrase
  12 ).run()
  13 
  14 Then, on the target device, connect to "TEST_KRACK" using "testtest" as the
  15 passphrase.
  16 The output logs will indicate if one of the vulnerability have been triggered.
  17 
  18 Outputs for vulnerable devices:
  19 - IV re-use!! Client seems to be vulnerable to handshake 3/4 replay
  20   (CVE-2017-13077)
  21 - Broadcast packet accepted twice!! (CVE-2017-13080)
  22 - Client has installed an all zero encryption key (TK)!!
  23 
  24 For patched devices:
  25 - Client is likely not vulnerable to CVE-2017-13080
  26 """
  27 
  28 from scapy.modules.krack.automaton import KrackAP  # noqa: F401
  29
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/scapy/modules/krack/__init__.py b/scapy/modules/krack/__init__.py
  --- a/scapy/modules/krack/__init__.py
  +++ b/scapy/modules/krack/__init__.py
  @@ -1,5 +1,7 @@
   """Module implementing Krack Attack on client, as a custom WPA Access Point
   
  +Requires the python cryptography package v1.7+. See https://cryptography.io/
  +
   More details on the attack can be found on https://www.krackattacks.com/
   
   Example of use (from the scapy shell):
  @@ -25,4 +27,10 @@
   - Client is likely not vulnerable to CVE-2017-13080
   """
   
  -from scapy.modules.krack.automaton import KrackAP  # noqa: F401
  +from scapy.config import conf
  +
  +if conf.crypto_valid:
  +    from scapy.modules.krack.automaton import KrackAP  # noqa: F401
  +else:
  +    raise ImportError("Cannot import Krack module due to missing dependency. "
  +                      "Please install python{3}-cryptography v1.7+.")
  
verification_info: {"golden_diff": "diff --git a/scapy/modules/krack/__init__.py b/scapy/modules/krack/__init__.py\n--- a/scapy/modules/krack/__init__.py\n+++ b/scapy/modules/krack/__init__.py\n@@ -1,5 +1,7 @@\n \"\"\"Module implementing Krack Attack on client, as a custom WPA Access Point\n \n+Requires the python cryptography package v1.7+. See https://cryptography.io/\n+\n More details on the attack can be found on https://www.krackattacks.com/\n \n Example of use (from the scapy shell):\n@@ -25,4 +27,10 @@\n - Client is likely not vulnerable to CVE-2017-13080\n \"\"\"\n \n-from scapy.modules.krack.automaton import KrackAP  # noqa: F401\n+from scapy.config import conf\n+\n+if conf.crypto_valid:\n+    from scapy.modules.krack.automaton import KrackAP  # noqa: F401\n+else:\n+    raise ImportError(\"Cannot import Krack module due to missing dependency. \"\n+                      \"Please install python{3}-cryptography v1.7+.\")\n", "issue": "KRACK module requires python-cryptography\nWhen `python-cryptography` or `python3-cryptography` packages are not installed, the KRACK module cannot be loaded.\r\n\r\n```\r\n>>> load_module(\"krack\")\r\nERROR: Loading module scapy.modules.krack\r\nTraceback (most recent call last):\r\n  File \"/home/ria/scapy/scapy/main.py\", line 150, in _load\r\n    mod = importlib.import_module(module)\r\n  File \"/usr/lib/python3.7/importlib/__init__.py\", line 127, in import_module\r\n    return _bootstrap._gcd_import(name[level:], package, level)\r\n  File \"<frozen importlib._bootstrap>\", line 1006, in _gcd_import\r\n  File \"<frozen importlib._bootstrap>\", line 983, in _find_and_load\r\n  File \"<frozen importlib._bootstrap>\", line 967, in _find_and_load_unlocked\r\n  File \"<frozen importlib._bootstrap>\", line 677, in _load_unlocked\r\n  File \"<frozen importlib._bootstrap_external>\", line 728, in exec_module\r\n  File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\r\n  File \"/home/ria/scapy/scapy/modules/krack/__init__.py\", line 28, in <module>\r\n    from scapy.modules.krack.automaton import KrackAP  # noqa: F401\r\n  File \"/home/ria/scapy/scapy/modules/krack/automaton.py\", line 7, in <module>\r\n    from cryptography.hazmat.primitives import hashes\r\nModuleNotFoundError: No module named 'cryptography'\r\n```\r\n\r\nCalling @commial to assess whether the module could still offer functionalities with a `crypto_valid` around some blocks. But at first glance I believe a missing `cryptography` should completely prevent importing the module.\r\n\r\nhttps://github.com/secdev/scapy/blob/a58e1b90a704c394216a0b5a864a50931754bdf7/scapy/modules/krack/automaton.py#L6-L10\r\n\r\nhttps://github.com/secdev/scapy/blob/a58e1b90a704c394216a0b5a864a50931754bdf7/scapy/modules/krack/crypto.py#L6-L9\n", "before_files": [{"content": "\"\"\"Module implementing Krack Attack on client, as a custom WPA Access Point\n\nMore details on the attack can be found on https://www.krackattacks.com/\n\nExample of use (from the scapy shell):\n>>> load_module(\"krack\")\n>>> KrackAP(\n    iface=\"mon0\",               # A monitor interface\n    ap_mac='11:22:33:44:55:66', # MAC (BSSID) to use\n    ssid=\"TEST_KRACK\",          # SSID\n    passphrase=\"testtest\",      # Associated passphrase\n).run()\n\nThen, on the target device, connect to \"TEST_KRACK\" using \"testtest\" as the\npassphrase.\nThe output logs will indicate if one of the vulnerability have been triggered.\n\nOutputs for vulnerable devices:\n- IV re-use!! Client seems to be vulnerable to handshake 3/4 replay\n  (CVE-2017-13077)\n- Broadcast packet accepted twice!! (CVE-2017-13080)\n- Client has installed an all zero encryption key (TK)!!\n\nFor patched devices:\n- Client is likely not vulnerable to CVE-2017-13080\n\"\"\"\n\nfrom scapy.modules.krack.automaton import KrackAP  # noqa: F401\n", "path": "scapy/modules/krack/__init__.py"}], "after_files": [{"content": "\"\"\"Module implementing Krack Attack on client, as a custom WPA Access Point\n\nRequires the python cryptography package v1.7+. See https://cryptography.io/\n\nMore details on the attack can be found on https://www.krackattacks.com/\n\nExample of use (from the scapy shell):\n>>> load_module(\"krack\")\n>>> KrackAP(\n    iface=\"mon0\",               # A monitor interface\n    ap_mac='11:22:33:44:55:66', # MAC (BSSID) to use\n    ssid=\"TEST_KRACK\",          # SSID\n    passphrase=\"testtest\",      # Associated passphrase\n).run()\n\nThen, on the target device, connect to \"TEST_KRACK\" using \"testtest\" as the\npassphrase.\nThe output logs will indicate if one of the vulnerability have been triggered.\n\nOutputs for vulnerable devices:\n- IV re-use!! Client seems to be vulnerable to handshake 3/4 replay\n  (CVE-2017-13077)\n- Broadcast packet accepted twice!! (CVE-2017-13080)\n- Client has installed an all zero encryption key (TK)!!\n\nFor patched devices:\n- Client is likely not vulnerable to CVE-2017-13080\n\"\"\"\n\nfrom scapy.config import conf\n\nif conf.crypto_valid:\n    from scapy.modules.krack.automaton import KrackAP  # noqa: F401\nelse:\n    raise ImportError(\"Cannot import Krack module due to missing dependency. \"\n                      \"Please install python{3}-cryptography v1.7+.\")\n", "path": "scapy/modules/krack/__init__.py"}]}