Tags: crypto sss

Rating:

This challenge was a cool introduction to Shamir Secret Sharing (SSS).

### msg.enc


share: (21202245407317581090, 11086299714260406068)
coefficient: 93526756371754197321930622219489764824


## Overview
We are given some parameters : p = 92434467187580489687 , k = 10 , n = 18

At first glance, we can see that the coefficients array (**coeffs**) is initialized with a random secret value, say **s**. self.coeffs = [self.secret]

This value is later used as seed to generate the random AES key which is used to encrypt the flag.

-----

The coeffs array contain repeated md5 hashes of the initial secret value. Of course this hash is converted to an integer. That is:

- element 0 : secret = s
- element 1 : md5(secret) = hs
- element 2 : md5(md5(secret)) = hhs ...

and so on and so forth. This repeats n = 18 times.

By the end of create_pol **coeffs** will contain only the first k=10 hash values because of self.coeffs = self.coeffs[:self.k].

The important part are the two arrays, x_vals and y_vals.

**x** is just some random value < p but **y** is calculated based on the corresponding x value and the coefficients array as below:

y = (s + hs * x + hhs * x^2 + hhhs * x^3 + ... + hhhhhhhhhs * x^9) % p (1)

## Solution

We are given the md5 hash of the secret value. That means that we can hash it, then hash again and again to recover all the coefficients but the secret.

We know all the hash values and just one pair of x, y values. Let's solve (1) for the secret:


secret = (y - hs * x - hhs * x^2 - hhhs * x^3 - ... - hhhhhhhhhs * x^9) % p =
= [y - (hs * x + hhs * x^2 + hhhs * x^3 + ... + hhhhhhhhhs * x^9)] % p


Now we can subtitute for the coefficients that we calculated above, the x and y value to get the secret, find the key and decrypt the flag.

## solve.py

from hashlib import md5
from Crypto.Cipher import AES
from random import randbytes, seed

x, y = 21202245407317581090, 11086299714260406068
hs = 93526756371754197321930622219489764824
k = 10
n = 18
p = 92434467187580489687
coeffs = [hs]

ct = bytes.fromhex('redacted_for_the_sake_of_brevity')

def next_coeff(val):
return int(md5(val.to_bytes(32, byteorder="big")).hexdigest(),16)

def calc_coeffs():
for i in range(1, k-1):
coeffs.append(next_coeff(coeffs[i-1]))

def calc_rhs():
sum = 0
for i in range(len(coeffs)):
sum += coeffs[i] * x**(i+1)
return sum % p

calc_coeffs()
rhs = calc_rhs()

secret = y - rhs
secret %= p

seed(secret)
key = randbytes(16)
cipher = AES.new(key, AES.MODE_ECB)
flag = cipher.decrypt(ct)

print(flag)