omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / tls / test / chacha / gen_diff.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # gen_diff.py: test tooling only. A seeded random differential corpus for driver.om, with expected |
| 4 | # values from the python `cryptography` package (OpenSSL), an implementation independent of ours. |
| 5 | # Lengths cover 0..300 bytes (every block-boundary residue), random keys, nonces, counters, aad. |
| 6 | # Usage: python3 gen_diff.py SEED COUNT > OUT |
| 7 | import random |
| 8 | import sys |
| 9 | |
| 10 | from cryptography.hazmat.primitives.ciphers import Cipher, algorithms |
| 11 | from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305 |
| 12 | from cryptography.hazmat.primitives.poly1305 import Poly1305 |
| 13 | |
| 14 | seed, count = int(sys.argv[1]), int(sys.argv[2]) |
| 15 | rng = random.Random(seed) |
| 16 | |
| 17 | |
| 18 | def rb(n): |
| 19 | return bytes(rng.getrandbits(8) for _ in range(n)) |
| 20 | |
| 21 | |
| 22 | def hx(b): |
| 23 | return b.hex() or "-" |
| 24 | |
| 25 | |
| 26 | print("# gen_diff.py seed %d count %d (oracle: python cryptography)" % (seed, count)) |
| 27 | for i in range(count): |
| 28 | n = i % 301 |
| 29 | key, nonce, msg = rb(32), rb(12), rb(n) |
| 30 | nb = (n + 63) // 64 |
| 31 | ctr = rng.choice([0, 1, rng.getrandbits(32) % (0x100000001 - nb), 0x100000000 - nb if nb else 0xffffffff]) |
| 32 | ks = Cipher(algorithms.ChaCha20(key, ctr.to_bytes(4, "little") + nonce), None).encryptor() |
| 33 | print("C %s %d %s %s 0" % (hx(key + nonce), ctr, hx(msg), hx(ks.update(msg)))) |
| 34 | print("P %s %s %s 0" % (hx(key), hx(msg), hx(Poly1305.generate_tag(key, msg)))) |
| 35 | aad = rb(rng.choice([0, 1, 12, 15, 16, 17, n])) |
| 36 | print("A %s %s %s %s 0" % (hx(key + nonce), hx(aad), hx(msg), |
| 37 | hx(ChaCha20Poly1305(key).encrypt(nonce, msg, aad)))) |