Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Code

442 views
import hashlib from datetime import datetime bitmask = (2^(30*8) - 1); #internal state s_i = None; #additional input second = datetime.now().second; microsecond = datetime.now().microsecond; counter = 0; pid = os.getpid(); def Dual_EC_DRBG(P, Q, h_adin=0, s_0=None): global s_i; if(s_0 == None): s_0 = int(floor((2^16-1)*random())); if(s_i == None): s_i = s_0; t_i = s_i ^^ h_adin; s_i = (t_i*P)[0].lift(); r_i = (s_i*Q)[0].lift(); r_i = r_i & bitmask; return r_i; def Random_Generator(P, Q, byte, h_adin=0): result = 0; req = (byte/30).ceil(); for i in range(req): if(i == 0): result = (result << (30*8)) | Dual_EC_DRBG(P, Q, h_adin) else: result = (result << (30*8)) | Dual_EC_DRBG(P, Q) result = result >> ((30*req - byte)*8) return result; def Get_H_Adin(): global second; global microsecond; global counter; global pid; second = datetime.now().second; microsecond = datetime.now().microsecond; counter = counter + 1; pid = os.getpid(); adin = (second << (12*8)) | (microsecond << (8*8)) | (counter << (4*8)) | pid; h = hashlib.sha256(); h.update(str(adin)); return int(h.hexdigest(), 16); def Get_Internal_State(P, Q, p, b, curve, r, d): result = []; r_1 = r >> (len(hex(r))*4 - 30*8); r_2 = r & (2^(len(hex(r))*4 - 30*8) - 1); for i in range(2^16): mb = i << (30*8); x_cand = mb | r_1; y = Mod(x_cand^3 - 3*x_cand + b, p); if(y.is_square()): y_cand = y.sqrt(); try: R = curve(x_cand, y_cand); s_cand = (d*R)[0].lift(); r_cand = (s_cand*Q)[0].lift(); r_cand = r_cand & bitmask; if((hex(r_cand).startswith(hex(r_2))) or (hex(r_2).startswith(hex(r_cand)))): result.append(s_cand); except: continue; return result; def Predict_Next(P, Q, byte, s_cand, h_adin): result = 0; req = (byte/30).ceil(); for i in range(req): if(i == 0): t_cand = s_cand ^^ h_adin; s_cand = (t_cand*P)[0].lift(); r_cand = (s_cand*Q)[0].lift(); r_cand = r_cand & bitmask; result = (result << (30*8)) | r_cand else: s_cand = (s_cand*P)[0].lift(); r_cand = (s_cand*Q)[0].lift(); r_cand = r_cand & bitmask; result = (result << (30*8)) | r_cand result = result >> ((30*req - byte)*8) return result; #Curve P-256 p = 115792089210356248762697446949407573530086143415290314195533631308867097853951; n = 115792089210356248762697446949407573529996955224135760342422259061068512044369; b = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b; Px = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296; Py = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5; #Qx = 0xc97445f45cdef9f0d3e05e1e585fc297235b82b5be8ff3efca67c59852018192; #Qy = 0xb28ef557ba31dfcbdd21ac46e2a91e3c304f44cb87058ada2cb815151e610046; # y^2 = x^3 - 3*x + b (mod p) curve = EllipticCurve(GF(p), [0, 0, 0, -3, b]); print curve; P = curve(Px, Py); #Q = curve(Qx, Qy); #Backdoor d = 5; order = P.additive_order(); e = inverse_mod(d, order); #P = d*Q; Q = e*P; print "P = ", P; print "Q = ", Q; h_adin = Get_H_Adin(); print "h_adin0 = ", hex(h_adin); r = Random_Generator(P, Q, 32, h_adin); print "r1 = ", hex(r); #predictable additional input h_adin = Get_H_Adin(); print "h_adin1 = ", hex(h_adin); %time s = Get_Internal_State(P, Q, p, b, curve, r, d); for i in range(len(s)): print "s = ", hex(s[i]); %time rp = Predict_Next(P, Q, 60, s[i], h_adin); print "predict = ", hex(rp); r = Random_Generator(P, Q, 60, h_adin); print "r2 + r3 = ", hex(r);
Elliptic Curve defined by y^2 = x^3 + 115792089210356248762697446949407573530086143415290314195533631308867097853948*x + 41058363725152142129326129780047268409114441015993725554835256314039467401291 over Finite Field of size 115792089210356248762697446949407573530086143415290314195533631308867097853951 P = (48439561293906451759052585252797914202762949526041747995844080717082404635286 : 36134250956749795798585127919587881956611106672985015071877198253568414405109 : 1) Q = (100222093819885759857726245131128697024676897724593576735535145416600847521071 : 112705950327624587511154978849178363127000253898669394213565898066000545039919 : 1) h_adin0 = 0xc0050358a0265f8f9280786ce7341a8420129ed09c691f9cbbbf3d72e03934c6L r1 = 74d4b04a6c67c5d7854a07623e0a8ee2f186bdb0b427f34969f087472fb19175 h_adin1 = 0x4585455f188dfb0551eed1264dc9b7e315594d5a827dcb72bc4eb0d3421097acL CPU time: 683.62 s, Wall time: 767.68 s s = eb2c26696e42c1217d47c7d6fac4e1c7e75819038f6c05351f931f52b1f5c6ce CPU time: 0.06 s, Wall time: 0.06 s predict = f52435ee8b25d2169d76ced73474ebde908d206bcb741b2516e22d12564edd5b06c1b90a4d0a3c9e66178e4feec730ed75858c15ed7d9567905622b3 r2 + r3 = f52435ee8b25d2169d76ced73474ebde908d206bcb741b2516e22d12564edd5b06c1b90a4d0a3c9e66178e4feec730ed75858c15ed7d9567905622b3