From 39ba5a7dacb5d8ca4d52600e96a49ad46936238c Mon Sep 17 00:00:00 2001 From: cyfraeviolae Date: Wed, 24 Aug 2022 16:15:46 -0400 Subject: work --- aesgcmanalysis.py | 2 +- app.py | 46 ++++++++++--- static/styles.css | 32 +++++++++ templates/nonce-reuse.html | 160 ++++++++++++++++++++++++--------------------- 4 files changed, 156 insertions(+), 84 deletions(-) diff --git a/aesgcmanalysis.py b/aesgcmanalysis.py index 0b81522..5123c63 100644 --- a/aesgcmanalysis.py +++ b/aesgcmanalysis.py @@ -517,7 +517,7 @@ def compute_forbidden_polynomial(aad1, aad2, c1, c2, mac1, mac2): b2 = bytes_to_gf128(bs2[i*16:(i+1)*16]) f.append(gf128_add(b1, b2)) f.append(gf128_add(bytes_to_gf128(mac1), bytes_to_gf128(mac2))) - return list(reversed(f)) + return collapse(list(reversed(f))) def nonce_reuse_recover_secrets(nonce, aad1, aad2, c1, c2, mac1, mac2): f = compute_forbidden_polynomial(aad1, aad2, c1, c2, mac1, mac2) diff --git a/app.py b/app.py index 2177e9c..d573efc 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,9 @@ import binascii from flask import Flask, render_template, request, redirect, url_for +from flask_wtf import FlaskForm +from wtforms import StringField +from wtforms.validators import DataRequired, Length, ValidationError from aesgcmanalysis import xor, gmac, gcm_encrypt, nonce_reuse_recover_secrets, gf128_to_bytes @@ -10,18 +13,41 @@ app = Flask(__name__) def index(): return render_template('index.html') +def hex_check(form, field): + if len(field.data) % 2 != 0: + raise ValidationError(f'not valid hex; must have even length') + if not all(c in '1234567890abcdef' for c in field.data): + raise ValidationError(f'not valid hex; contains non-hex character') + +def not_equal_to(other): + def helper(form, field): + print(other, form['m1'], field) + if other not in form: + return + if form[other].data == field.data: + raise ValidationError(f'must not be equal to {other}') + return helper + +class NonceReuseForm(FlaskForm): + key = StringField('key', validators=[DataRequired(), Length(min=32, max=32), hex_check]) + nonce = StringField('nonce', validators=[DataRequired(), Length(min=24, max=24), hex_check]) + m1 = StringField('first message', validators=[DataRequired(), Length(min=1, max=64)]) + m2 = StringField('second message', validators=[DataRequired(), Length(min=1, max=64), not_equal_to('m1')]) + mf = StringField('forged message', validators=[DataRequired(), Length(min=1, max=64)]) + @app.route('/nonce-reuse', methods=['GET', 'POST']) def nonce_reuse(): - key = nonce = c_forged = macs = None - m1 = m2 = mf = '' - if request.method == 'POST': - key = binascii.unhexlify(request.form['key']) - nonce = binascii.unhexlify(request.form['nonce']) - m1 = request.form['m1'] - m2 = request.form['m2'] - mf = request.form['mf'] - c_forged, macs = solve(key, nonce, bytes(m1, 'ascii'), bytes(m2, 'ascii'), bytes(mf, 'ascii')) - return render_template('nonce-reuse.html', key=key, nonce=nonce, m1=m1, m2=m2, mf=mf, c_forged=c_forged, macs=macs) + form = NonceReuseForm(meta={'csrf': False}) + key = nonce = None + m1 = m2 = mf = c_forged = '' + macs = None + if form.is_submitted(): + key, nonce, m1, m2, mf = form.key.data, form.nonce.data, form.m1.data, form.m2.data, form.mf.data + if form.validate(): + skey = binascii.unhexlify(key) + snonce = binascii.unhexlify(nonce) + c_forged, macs = solve(skey, snonce, bytes(m1, 'utf-8'), bytes(m2, 'utf-8'), bytes(mf, 'utf-8')) + return render_template('nonce-reuse.html', form=form, key=key, nonce=nonce, m1=m1, m2=m2, mf=mf, c_forged=c_forged, macs=macs) def solve(k, nonce, m1, m2, mf): aad1 = aad2 = b"" diff --git a/static/styles.css b/static/styles.css index 6360fc1..9688050 100644 --- a/static/styles.css +++ b/static/styles.css @@ -14,3 +14,35 @@ ul { margin-top: 5px; } + +.inner-ul { + margin-top: initial; +} + +pre { + white-space: pre-wrap; +} + +details[open=""] { + border: 1px dotted darkslategrey; + padding: 1em; +} + +input[type="text"] { + width: 50%; + min-width: 350px; +} + +.errors { + border-left: 2px crimson solid; + padding-left: 1em; +} + +.solution { + border-left: 2px #289528 solid; + padding-left: 1em; +} + +code { + word-wrap: anywhere; +} diff --git a/templates/nonce-reuse.html b/templates/nonce-reuse.html index 9761955..2637d50 100644 --- a/templates/nonce-reuse.html +++ b/templates/nonce-reuse.html @@ -31,6 +31,88 @@ Forbidden Attack in order to recover the authentication key and forge arbitrary ciphertext.

+
+ {% if form.errors %} +
+ Errors: + +
+ {% endif %} +
+
+ Roseacrucis chooses a key, a nonce, and two messages. He encrypts both messages under the same nonce. +

+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ After intercepting the ciphertexts, you choose a new message to forge under the same key and nonce. +

+ +
+ + +
+ +
+ +
+
+
+
+ +
+
+ {% if macs %} +
+

+ Forged ciphertext: {{ c_forged.hex() }} + {% if macs|length == 1 %} +
+ Forged MAC: {{macs[0][2].hex()}} +
+ Authentication key: {{macs[0][0].hex()}} + {% endif %} +

+ {% if macs|length != 1 %} + Forged MAC candidates: + + {% endif %} +
+ {% endif %}
@@ -84,61 +166,6 @@ in this case, one can check each possibility online.

-
-
-
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- -
-
- {% if macs %} -
-

- Forged ciphertext: {{ c_forged.hex() }} -

- Forged MAC candidates: - -
-
- -
-
-
- {% endif %} -
Show me the code. @@ -148,10 +175,8 @@ from aesgcmanalysis import xor, gmac, g k = b"tlonorbistertius" nonce = b"jorgelborges" -m1 = b"The universe (which others call the Library)" -aad1 = b"The Anatomy of Melancholy" -m2 = b"From any of the hexagons one can see, interminably" -aad2 = b"Letizia Alvarez de Toledo" +m1, aad1 = b"The universe (which others call the Library)", b"" +m2, aad2 = b"From any of the hexagons one can see, interminably", b"" c1, mac1 = gcm_encrypt(k, nonce, aad1, m1) c2, mac2 = gcm_encrypt(k, nonce, aad2, m2) @@ -161,21 +186,10 @@ possible_secrets = nonce_reuse_recover_secrets(nonce, aad1, aad2, c1, c2, mac1, # Forge the ciphertext m_forged = b"As was natural, this inordinate hope" -assert len(m_forged) <= len(m1) -c_forged = xor(c1, xor(m1, m_forged)) -aad_forged = b"You who read me, are You sure of understanding my language?" +c_forged, aad_forged = xor(c1, xor(m1, m_forged)), b"" -# Check possible candidates for authentication key -succeeded = False for h, s in possible_secrets: - mac_forged = gmac(h, s, aad_forged, c_forged) - try: - assert gcm_decrypt(k, nonce, aad_forged, c_forged, mac_forged) == m_forged - succeeded = True - print(c_forged.hex(), mac_forged.hex()) - except AssertionError: - pass -assert succeeded
+ print("MAC candidate": gmac(h, s, aad_forged, c_forged))
Show me the math. @@ -219,7 +233,7 @@ for factor, _ in p.factor():

  • The gcd of two polynomials is unique only up to multiplication by a non-zero constant because “greater” is defined for polynomials in terms of degree. When used in algorithms, gcd refers to the monic gcd, which is unique.
  • -
  • The inverse Frobenius automorphism (i.e., square root) in \(\mathbb{F}_{2^{128}}\) is given by \(\sqrt{x} = x^{2^{127}})\).
  • +
  • The inverse Frobenius automorphism (i.e., square root) in \(\mathbb{F}_{2^{128}}\) is given by \(\sqrt{x} = x^{2^{127}}\).