aboutsummaryrefslogtreecommitdiff
path: root/src/test/ope_ref.py
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-05-10 08:46:36 -0400
committerNick Mathewson <nickm@torproject.org>2018-07-17 15:57:46 -0400
commit3a45f6ffe95d4c51e4ad4e14f468feb3f4bd6b1e (patch)
tree955117d03238f618d9f472ef884edbca26c05cdd /src/test/ope_ref.py
parent860b9a991879c5be2b32cf98766adf5fdd349d41 (diff)
downloadtor-3a45f6ffe95d4c51e4ad4e14f468feb3f4bd6b1e.tar.gz
tor-3a45f6ffe95d4c51e4ad4e14f468feb3f4bd6b1e.zip
Implementation for a simple order-preserving encryption scheme.
This is meant for use when encrypting the current time within the period in order to get a monotonically increasing revision counter without actually revealing our view of the time. This scheme is far from the most state-of-the-art: don't use it for anything else without careful analysis by somebody much smarter than I am. See ticket #25552 for some rationale for this logic.
Diffstat (limited to 'src/test/ope_ref.py')
-rw-r--r--src/test/ope_ref.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/test/ope_ref.py b/src/test/ope_ref.py
new file mode 100644
index 0000000000..3677e57a61
--- /dev/null
+++ b/src/test/ope_ref.py
@@ -0,0 +1,40 @@
+#!/usr/bin/python3
+# Copyright 2018, The Tor Project, Inc. See LICENSE for licensing info.
+
+# Reference implementation for our rudimentary OPE code, used to
+# generate test vectors. See crypto_ope.c for more details.
+
+from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
+from cryptography.hazmat.primitives.ciphers.algorithms import AES
+from cryptography.hazmat.backends import default_backend
+
+from binascii import a2b_hex
+
+#randomly generated and values.
+KEY = a2b_hex(
+ "19e05891d55232c08c2cad91d612fdb9cbd6691949a0742434a76c80bc6992fe")
+PTS = [ 121132, 82283, 72661, 72941, 123122, 12154, 121574, 11391, 65845,
+ 86301, 61284, 70505, 30438, 60150, 114800, 109403, 21893, 123569,
+ 95617, 48561, 53334, 92746, 7110, 9612, 106958, 46889, 87790, 68878,
+ 47917, 121128, 108602, 28217, 69498, 63870, 57542, 122148, 46254,
+ 42850, 92661, 57720]
+
+IV = b'\x00' * 16
+
+backend = default_backend()
+
+def words():
+ cipher = Cipher(algorithms.AES(KEY), modes.CTR(IV), backend=backend)
+ e = cipher.encryptor()
+ while True:
+ v = e.update(b'\x00\x00')
+ yield v[0] + 256 * v[1] + 1
+
+def encrypt(n):
+ return sum(w for w, _ in zip(words(), range(n)))
+
+def example(n):
+ return ' {{ {}, UINT64_C({}) }},'.format(n, encrypt(n))
+
+for v in PTS:
+ print(example(v))