Created
May 19, 2018 15:08
-
-
Save ykm11/1dd27536dcbf645e2883a478f3e0962b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import binascii | |
def plus(x, y): | |
return (x + y) & 0xffffffff | |
#return (x + y) % 2^32 | |
def lrotate(x, n): | |
l = (x << n) & 0xffffffff | |
r = (x >> (32-n)) & 0xffffffff | |
return l + r | |
def QuarterRound(a, b, c, d): | |
a = plus(a, b); d ^= a; d = lrotate(d, 16) | |
c = plus(c, d); b ^= c; b = lrotate(b, 12) | |
a = plus(a, b); d ^= a; d = lrotate(d, 8) | |
c = plus(c, d); b ^= c; b = lrotate(b, 7) | |
return a, b, c, d | |
def chacha20(text, key, nonce, cnt=0): | |
## initialize ## | |
const = [0x64787061, 0x6e642033, 0x322d6279, 0x7465206b,] | |
count = [0 + cnt] | |
state = const + key + count + nonce | |
for _ in range(10): | |
# Columns | |
state[0], state[4], state[8], state[12] = QuarterRound(state[0], state[4], state[8], state[12]) | |
state[1], state[5], state[9], state[13] = QuarterRound(state[1], state[5], state[9], state[13]) | |
state[2], state[6], state[10], state[14] = QuarterRound(state[2], state[6], state[10], state[14]) | |
state[3], state[7], state[11], state[15] = QuarterRound(state[3], state[7], state[11], state[15]) | |
# Diagonal | |
state[0], state[5], state[10], state[15] = QuarterRound(state[0], state[5], state[10], state[15]) | |
state[1], state[6], state[11], state[12] = QuarterRound(state[1], state[6], state[11], state[12]) | |
state[2], state[7], state[8], state[13] = QuarterRound(state[2], state[7], state[8], state[13]) | |
state[3], state[4], state[9], state[14] = QuarterRound(state[3], state[4], state[9], state[14]) | |
results = [0]*16 | |
for i in range(16): | |
results[i] = text[i] ^ state[i] | |
return results, state | |
def make_key(): | |
chrs = "abcdef1234567890" | |
key = [0]*8 | |
for i in range(8): | |
k = "" | |
for _ in range(8): | |
k += chrs[random.randint(0, len(chrs)-1)] | |
key[i] = int(k, 16) | |
return key | |
## Test ## | |
text = [random.randint(114,514) for _ in range(16)] | |
print(text) | |
key = make_key() | |
nonce = [0, 0, 0] | |
## Confirmation ## | |
enc, _ = chacha20(text, nonce, key) | |
print(enc) | |
dec, _ = chacha20(enc, nonce, key) | |
print(dec) | |
print(text == dec) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import binascii | |
bytes_to_int = lambda x:int(binascii.hexlify(x), 16) | |
str_to_int = lambda x:int(binascii.hexlify(x.encode()), 16) | |
def make_array(s): | |
return [ s[16*i:16*(i+1)] for i in range((len(s)+15)//16) ] | |
def make_coef(array): | |
r = [] | |
for s in array: | |
s = (s << 8) + 1 | |
len_s = len(bin(s)[2:]) | |
if len_s < 136: | |
s <<= (136-len_s) | |
r.append(s) | |
return r | |
def f(coefs, r, s): | |
MOD = 2**130 - 5 | |
x = 0 | |
for i, Ci in enumerate(coefs[::-1]): | |
x += (Ci % MOD)*pow(r, i+1, MOD) % MOD | |
return (x + s) & 0xffffffffffffffffffffffffffffffff | |
text = b"Surely Everyone is someone's HERO" | |
dt = make_array(text) | |
dt1 = map(bytes_to_int, dt) | |
coefs = make_coef(dt1) | |
r = 114514 | |
s = 364364 | |
x = f(coefs, r, s) | |
print(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment