ope_ref.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/python3
  2. # Copyright 2018-2019, The Tor Project, Inc. See LICENSE for licensing info.
  3. # Reference implementation for our rudimentary OPE code, used to
  4. # generate test vectors. See crypto_ope.c for more details.
  5. from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
  6. from cryptography.hazmat.primitives.ciphers.algorithms import AES
  7. from cryptography.hazmat.backends import default_backend
  8. from binascii import a2b_hex
  9. #randomly generated and values.
  10. KEY = a2b_hex(
  11. "19e05891d55232c08c2cad91d612fdb9cbd6691949a0742434a76c80bc6992fe")
  12. PTS = [ 121132, 82283, 72661, 72941, 123122, 12154, 121574, 11391, 65845,
  13. 86301, 61284, 70505, 30438, 60150, 114800, 109403, 21893, 123569,
  14. 95617, 48561, 53334, 92746, 7110, 9612, 106958, 46889, 87790, 68878,
  15. 47917, 121128, 108602, 28217, 69498, 63870, 57542, 122148, 46254,
  16. 42850, 92661, 57720]
  17. IV = b'\x00' * 16
  18. backend = default_backend()
  19. def words():
  20. cipher = Cipher(algorithms.AES(KEY), modes.CTR(IV), backend=backend)
  21. e = cipher.encryptor()
  22. while True:
  23. v = e.update(b'\x00\x00')
  24. yield v[0] + 256 * v[1] + 1
  25. def encrypt(n):
  26. return sum(w for w, _ in zip(words(), range(n)))
  27. def example(n):
  28. return ' {{ {}, UINT64_C({}) }},'.format(n, encrypt(n))
  29. for v in PTS:
  30. print(example(v))