def onetimeauth_test(): """ """ return mlen = 0 while 1: mlen = mlen + 1 + int(mlen / 16) if mlen > 10000: break k = nacl.randombytes(nacl.crypto_onetimeauth_KEYBYTES) m = nacl.randombytes(mlen) a = nacl.crypto_onetimeauth(m, k) nacl.crypto_onetimeauth_verify(a, m, k) if mlen < 1: continue a1 = flip_bit(a) try: nacl.crypto_onetimeauth_verify(a1, m, k) except: pass else: raise ValueError("forgery")
def sign_test(): """ """ mlen = 0 while 1: mlen = mlen + 1 + int(mlen / 16) if mlen > 10000: break (pk, sk) = nacl.crypto_sign_keypair() m = nacl.randombytes(mlen) sm = nacl.crypto_sign(m, sk) t = nacl.crypto_sign_open(sm, pk) sm1 = flip_bit(sm) try: t1 = nacl.crypto_sign_open(sm1, pk) except: pass else: raise ValueError("forgery") if m != t: raise ValueError("crypto_sign_open does not match contents")
def secretbox_bad_test(): """ """ n = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES); k = nacl.randombytes(nacl.crypto_secretbox_KEYBYTES); m = nacl.randombytes(1); c = nacl.crypto_secretbox(m, n, k) #save exception string cx = flip_bit(c) exc_string = "" try: nacl.crypto_secretbox_open(cx, n, k) except: exc_string = exc() bad = [] tmp = {"c":c, "m":m, "k":k, "n":n} tmp["n"] = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES + 1) bad.append(tmp) tmp = {"c":c, "m":m, "k":k, "n":n} tmp["n"] = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES - 1) bad.append(tmp) tmp = {"c":c, "m":m, "k":k, "n":n} tmp["n"] = 0 bad.append(tmp) tmp = {"c":c, "m":m, "k":k, "n":n} tmp["k"] = nacl.randombytes(nacl.crypto_secretbox_KEYBYTES + 1) bad.append(tmp) tmp = {"c":c, "m":m, "k":k, "n":n} tmp["k"] = nacl.randombytes(nacl.crypto_secretbox_KEYBYTES - 1) bad.append(tmp) tmp = {"c":c, "m":m, "k":k, "n":n} tmp["k"] = 0; bad.append(tmp) tmp = {"c":c, "m":m, "k":k, "n":n} tmp["m"] = 0 tmp["c"] = 0 bad.append(tmp) for tmp in bad: try: nacl.crypto_secretbox(tmp["m"], tmp["n"], tmp["k"]) except: pass else: raise Exception("crypto_secretbox accepts incorrect input data") try: nacl.crypto_secretbox_open(tmp["c"], tmp["n"], tmp["k"]) except: if exc_string == exc(): raise else: raise Exception("crypto_secretbox accepts incorrect input data")
def secretbox_bad_test(): """ """ n = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES) k = nacl.randombytes(nacl.crypto_secretbox_KEYBYTES) m = nacl.randombytes(1) c = nacl.crypto_secretbox(m, n, k) #save exception string cx = flip_bit(c) exc_string = "" try: nacl.crypto_secretbox_open(cx, n, k) except: exc_string = exc() bad = [] tmp = {"c": c, "m": m, "k": k, "n": n} tmp["n"] = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES + 1) bad.append(tmp) tmp = {"c": c, "m": m, "k": k, "n": n} tmp["n"] = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES - 1) bad.append(tmp) tmp = {"c": c, "m": m, "k": k, "n": n} tmp["n"] = 0 bad.append(tmp) tmp = {"c": c, "m": m, "k": k, "n": n} tmp["k"] = nacl.randombytes(nacl.crypto_secretbox_KEYBYTES + 1) bad.append(tmp) tmp = {"c": c, "m": m, "k": k, "n": n} tmp["k"] = nacl.randombytes(nacl.crypto_secretbox_KEYBYTES - 1) bad.append(tmp) tmp = {"c": c, "m": m, "k": k, "n": n} tmp["k"] = 0 bad.append(tmp) tmp = {"c": c, "m": m, "k": k, "n": n} tmp["m"] = 0 tmp["c"] = 0 bad.append(tmp) for tmp in bad: try: nacl.crypto_secretbox(tmp["m"], tmp["n"], tmp["k"]) except: pass else: raise Exception("crypto_secretbox accepts incorrect input data") try: nacl.crypto_secretbox_open(tmp["c"], tmp["n"], tmp["k"]) except: if exc_string == exc(): raise else: raise Exception("crypto_secretbox accepts incorrect input data")
def sign_bad_test(): """ """ (pk, sk) = nacl.crypto_sign_keypair() if len(pk) != nacl.crypto_sign_PUBLICKEYBYTES: raise ValueError("invalid crypto_sign_keypair public-key length") if len(sk) != nacl.crypto_sign_SECRETKEYBYTES: raise ValueError("invalid crypto_sign_keypair secret-key length") m = nacl.randombytes(1) sm = nacl.crypto_sign(m, sk) #save exception string smx = flip_bit(sm) exc_string = "" try: nacl.crypto_sign_open(smx, pk) except: exc_string = exc() ss = (nacl.randombytes(nacl.crypto_sign_SECRETKEYBYTES + 1), nacl.randombytes(nacl.crypto_sign_SECRETKEYBYTES - 1), 0) pp = (nacl.randombytes(nacl.crypto_sign_PUBLICKEYBYTES + 1), nacl.randombytes(nacl.crypto_sign_PUBLICKEYBYTES - 1), 0) for s in ss: try: sm = nacl.crypto_sign(m, s) except: pass else: raise Exception("crypto_sign accepts incorrect input data") for p in pp: try: nacl.crypto_sign_open(sm, p) except: if exc_string == exc(): raise else: raise Exception("crypto_sign_open accepts incorrect input data") try: sm = nacl.crypto_sign(0, sk) except: pass else: raise Exception("crypto_sign accepts incorrect input data") try: nacl.crypto_sign_open(0, pk) except: if exc_string == exc(): raise else: raise Exception("crypto_sign_open accepts incorrect input data")
def box_test2(): """ """ mlen = 0 while 1: mlen = mlen + 1 + int(mlen / 16) if mlen > 10000: break (alicepk,alicesk) = nacl.crypto_box_keypair(); (bobpk,bobsk) = nacl.crypto_box_keypair(); n = nacl.randombytes(nacl.crypto_box_NONCEBYTES); m = nacl.randombytes(mlen); bobk = nacl.crypto_box_beforenm(alicepk, bobsk) alicek = nacl.crypto_box_beforenm(bobpk, alicesk) c = nacl.crypto_box_afternm(m,n,bobk) m1 = nacl.crypto_box_open_afternm(c, n, alicek) if m != m1: raise ValueError("bad decryption") n1 = flip_bit(n) try: m1 = nacl.crypto_box_open_afternm(c, n1, alicek) except: pass else: raise ValueError("forgery") c1 = flip_bit(c) try: m1 = nacl.crypto_box_open_afternm(c1, n, alicek) except: pass else: raise ValueError("forgery")
def secretbox_test(): """ """ mlen = 0 while 1: mlen = mlen + 1 + int(mlen / 16) if mlen > 10000: break n = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES); k = nacl.randombytes(nacl.crypto_secretbox_KEYBYTES); m = nacl.randombytes(mlen); c = nacl.crypto_secretbox(m, n, k) m1 = nacl.crypto_secretbox_open(c, n, k) if m != m1: raise ValueError("bad decryption") n1 = flip_bit(n) try: m1 = nacl.crypto_secretbox_open(c, n1, k) except: pass else: print(hexlify(n)) print(hexlify(n1)) raise ValueError("forgery") c1 = flip_bit(c) try: m1 = nacl.crypto_secretbox_open(c1, n, k) except: pass else: raise ValueError("forgery")
def secretbox_test(): """ """ mlen = 0 while 1: mlen = mlen + 1 + int(mlen / 16) if mlen > 10000: break n = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES) k = nacl.randombytes(nacl.crypto_secretbox_KEYBYTES) m = nacl.randombytes(mlen) c = nacl.crypto_secretbox(m, n, k) m1 = nacl.crypto_secretbox_open(c, n, k) if m != m1: raise ValueError("bad decryption") n1 = flip_bit(n) try: m1 = nacl.crypto_secretbox_open(c, n1, k) except: pass else: print(hexlify(n)) print(hexlify(n1)) raise ValueError("forgery") c1 = flip_bit(c) try: m1 = nacl.crypto_secretbox_open(c1, n, k) except: pass else: raise ValueError("forgery")
def onetimeauth_bad_test(): """ """ k = nacl.randombytes(nacl.crypto_onetimeauth_KEYBYTES) m = nacl.randombytes(1) a = nacl.crypto_onetimeauth(m, k) #save exception string exc_string = "" ax = flip_bit(a) try: a = nacl.crypto_onetimeauth(ax, k) except: exc_string = exc() bad = [] tmp = {"k": k, "m": m, "a": a} tmp["k"] = nacl.randombytes(nacl.crypto_onetimeauth_KEYBYTES + 1) bad.append(tmp) tmp = {"k": k, "m": m, "a": a} tmp["k"] = nacl.randombytes(nacl.crypto_onetimeauth_KEYBYTES - 1) bad.append(tmp) tmp = {"k": k, "m": m, "a": a} tmp["k"] = 0 bad.append(tmp) tmp = {"k": k, "m": m, "a": a} tmp["m"] = 0 tmp["a"] = 0 bad.append(tmp) for tmp in bad: try: nacl.crypto_onetimeauth(tmp["m"], tmp["k"]) except: pass else: raise Exception("crypto_onetimeauth accepts incorrect input data") try: nacl.crypto_onetimeauth_open(tmp["a"], tmp["k"]) except: if exc_string == exc(): raise else: raise Exception( "crypto_onetimeauth_open accepts incorrect input data")
def onetimeauth_bad_test(): """ """ k = nacl.randombytes(nacl.crypto_onetimeauth_KEYBYTES) m = nacl.randombytes(1) a = nacl.crypto_onetimeauth(m, k) #save exception string exc_string = "" ax = flip_bit(a) try: a = nacl.crypto_onetimeauth(ax, k) except: exc_string = exc() bad = [] tmp = {"k":k, "m":m, "a":a} tmp["k"] = nacl.randombytes(nacl.crypto_onetimeauth_KEYBYTES + 1) bad.append(tmp) tmp = {"k":k, "m":m, "a":a} tmp["k"] = nacl.randombytes(nacl.crypto_onetimeauth_KEYBYTES - 1) bad.append(tmp) tmp = {"k":k, "m":m, "a":a} tmp["k"] = 0 bad.append(tmp) tmp = {"k":k, "m":m, "a":a} tmp["m"] = 0 tmp["a"] = 0 bad.append(tmp) for tmp in bad: try: nacl.crypto_onetimeauth(tmp["m"], tmp["k"]) except: pass else: raise Exception("crypto_onetimeauth accepts incorrect input data") try: nacl.crypto_onetimeauth_open(tmp["a"], tmp["k"]) except: if exc_string == exc(): raise else: raise Exception("crypto_onetimeauth_open accepts incorrect input data")
def verify_32_test(): """ """ for x in range(0, 10): x = nacl.randombytes(nacl.crypto_verify_32_BYTES) y = x nacl.crypto_verify_32(x, y) y1 = flip_bit(y) try: nacl.crypto_verify_32(x, y1) except ValueError: pass else: raise ValueError("forgery")
def box_bad_test(): """ """ (pk, sk) = nacl.crypto_box_keypair() if len(pk) != nacl.crypto_box_PUBLICKEYBYTES: raise ValueError("invalid crypto_box_keypair public-key length") if len(sk) != nacl.crypto_box_SECRETKEYBYTES: raise ValueError("invalid crypto_box_keypair secret-key length") n = nacl.randombytes(nacl.crypto_box_NONCEBYTES); m = nacl.randombytes(1); c = nacl.crypto_box(m, n, pk, sk) #save exception string cx = flip_bit(c) exc_string = "" try: nacl.crypto_box_open(cx, n, pk, sk) except: exc_string = exc() bad = [] tmp = {"c":c, "m":m, "pk":pk, "sk":sk, "n":n} tmp["n"] = nacl.randombytes(nacl.crypto_box_NONCEBYTES + 1) bad.append(tmp) tmp = {"c":c, "m":m, "pk":pk, "sk":sk, "n":n} tmp["n"] = nacl.randombytes(nacl.crypto_box_NONCEBYTES - 1) bad.append(tmp) tmp = {"c":c, "m":m, "pk":pk, "sk":sk, "n":n} tmp["n"] = 0 bad.append(tmp) tmp = {"m":m, "pk":pk, "sk":sk, "n":n} tmp = {"c":c, "m":m, "pk":pk, "sk":sk, "n":n} tmp["pk"] = nacl.randombytes(nacl.crypto_box_PUBLICKEYBYTES + 1) bad.append(tmp) tmp = {"c":c, "m":m, "pk":pk, "sk":sk, "n":n} tmp["pk"] = nacl.randombytes(nacl.crypto_box_PUBLICKEYBYTES - 1) bad.append(tmp) tmp = {"c":c, "m":m, "pk":pk, "sk":sk, "n":n} tmp["pk"] = 0 bad.append(tmp) tmp = {"c":c, "m":m, "pk":pk, "sk":sk, "n":n} tmp["sk"] = nacl.randombytes(nacl.crypto_box_SECRETKEYBYTES + 1) bad.append(tmp) tmp = {"c":c, "m":m, "pk":pk, "sk":sk, "n":n} tmp["sk"] = nacl.randombytes(nacl.crypto_box_SECRETKEYBYTES - 1) bad.append(tmp) tmp = {"c":c, "m":m, "pk":pk, "sk":sk, "n":n} tmp["sk"] = 0 bad.append(tmp) tmp = {"c":c, "m":m, "pk":pk, "sk":sk, "n":n} tmp["m"] = 0 tmp["c"] = 0 bad.append(tmp) for tmp in bad: try: nacl.crypto_box(tmp["m"], tmp["n"], tmp["pk"], tmp["sk"]) except: pass else: raise Exception("crypto_box accepts incorrect input data") try: nacl.crypto_box_open(tmp["c"], tmp["n"], tmp["pk"], tmp["sk"]) except: if exc_string == exc(): raise else: raise Exception("crypto_box_open accepts incorrect input data") try: k = nacl.crypto_box_beforenm(tmp["pk"], tmp["sk"]) if len(k) != nacl.crypto_box_BEFORENMBYTES: raise ValueError("invalid crypto_box_beforenm beforenm-key length") nacl.crypto_box_afternm(tmp["m"], tmp["n"], k) except: pass else: raise Exception("crypto_box_afternm accepts incorrect input data") try: k = nacl.crypto_box_beforenm(tmp["pk"], tmp["sk"]) if len(k) != nacl.crypto_box_BEFORENMBYTES: raise ValueError("invalid crypto_box_beforenm beforenm-key length") nacl.crypto_box_open_afternm(tmp["c"], tmp["n"], k) except: if exc_string == exc(): raise else: raise Exception("crypto_box_open_afternm accepts incorrect input data")
def sign_bad_test(): """ """ (pk, sk) = nacl.crypto_sign_keypair() if len(pk) != nacl.crypto_sign_PUBLICKEYBYTES: raise ValueError("invalid crypto_sign_keypair public-key length") if len(sk) != nacl.crypto_sign_SECRETKEYBYTES: raise ValueError("invalid crypto_sign_keypair secret-key length") m = nacl.randombytes(1) sm = nacl.crypto_sign(m, sk) #save exception string smx = flip_bit(sm) exc_string = "" try: nacl.crypto_sign_open(smx, pk) except: exc_string = exc() ss = ( nacl.randombytes(nacl.crypto_sign_SECRETKEYBYTES + 1), nacl.randombytes(nacl.crypto_sign_SECRETKEYBYTES - 1), 0 ) pp = ( nacl.randombytes(nacl.crypto_sign_PUBLICKEYBYTES + 1), nacl.randombytes(nacl.crypto_sign_PUBLICKEYBYTES - 1), 0 ) for s in ss: try: sm = nacl.crypto_sign(m, s) except: pass else: raise Exception("crypto_sign accepts incorrect input data") for p in pp: try: nacl.crypto_sign_open(sm, p) except: if exc_string == exc(): raise else: raise Exception("crypto_sign_open accepts incorrect input data") try: sm = nacl.crypto_sign(0, sk) except: pass else: raise Exception("crypto_sign accepts incorrect input data") try: nacl.crypto_sign_open(0, pk) except: if exc_string == exc(): raise else: raise Exception("crypto_sign_open accepts incorrect input data")