agenttestgen.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python3
  2. import sys
  3. assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
  4. def generate():
  5. import hashlib
  6. print("""\
  7. # DO NOT EDIT DIRECTLY! Autogenerated by agenttestgen.py
  8. #
  9. # To regenerate, run
  10. # python3 agenttestgen.py > agenttestdata.py
  11. #
  12. # agenttestgen.py depends on the testcrypt system, so you must also
  13. # have built testcrypt in the parent directory, or else set
  14. # PUTTY_TESTCRYPT to point at a working implementation of it.
  15. """)
  16. from testcrypt import (rsa_generate, dsa_generate, ecdsa_generate,
  17. eddsa_generate, random_clear, random_queue,
  18. ssh_key_public_blob, ssh_key_openssh_blob,
  19. ssh_key_sign, rsa1_generate, rsa_ssh1_encrypt,
  20. rsa_ssh1_public_blob, rsa_ssh1_private_blob_agent,
  21. mp_from_bytes_be)
  22. from agenttest import (Key2, TestSig2, test_message_to_sign,
  23. Key1, test_session_id)
  24. import ssh
  25. keygen2 = [
  26. ('RSA-1024', lambda: rsa_generate(1024, False),
  27. (ssh.SSH_AGENT_RSA_SHA2_256, ssh.SSH_AGENT_RSA_SHA2_512)),
  28. ('DSA-1024', lambda: dsa_generate(1024)),
  29. ('ECDSA-p256', lambda: ecdsa_generate(256)),
  30. ('Ed25519', lambda: eddsa_generate(256)),
  31. ]
  32. keys2 = []
  33. for record in keygen2:
  34. if len(record) == 2:
  35. record += ((),)
  36. comment, genfn, flaglist = record
  37. flaglist = (0,) + flaglist
  38. random_clear()
  39. random_queue(b''.join(hashlib.sha512('{}{:d}'.format(comment, j)
  40. .encode('ASCII')).digest()
  41. for j in range(1000)))
  42. key = genfn()
  43. sigs = [TestSig2(flags, ssh_key_sign(key, test_message_to_sign, flags))
  44. for flags in flaglist]
  45. keys2.append(Key2(comment.encode("ASCII"),
  46. ssh_key_public_blob(key),
  47. sigs,
  48. ssh_key_openssh_blob(key)))
  49. print("def key2examples(Key2, TestSig2):\n return {!r}".format(keys2))
  50. keygen1 = [
  51. ('RSA-1024a', 1024),
  52. ('RSA-1024b', 1024),
  53. ('RSA-768c', 768),
  54. ('RSA-768d', 768),
  55. ]
  56. keys1 = []
  57. for comment, bits in keygen1:
  58. random_clear()
  59. random_queue(b''.join(hashlib.sha512('{}{:d}'.format(comment, j)
  60. .encode('ASCII')).digest()
  61. for j in range(1000)))
  62. key = rsa1_generate(bits)
  63. preimage = b'Test128BitRSA1ChallengeCleartext'
  64. assert len(preimage) == 32
  65. challenge_bytes = rsa_ssh1_encrypt(preimage, key)
  66. assert len(challenge_bytes) > 0
  67. challenge = int(mp_from_bytes_be(challenge_bytes))
  68. response = hashlib.md5(preimage + test_session_id).digest()
  69. keys1.append(Key1(comment.encode("ASCII"),
  70. rsa_ssh1_public_blob(key, 'exponent_first'),
  71. challenge, response,
  72. rsa_ssh1_private_blob_agent(key)))
  73. print("def key1examples(Key1):\n return {!r}".format(keys1))
  74. if __name__ == "__main__":
  75. generate()