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