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 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")
Esempio n. 3
0
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")
Esempio n. 4
0
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")
Esempio n. 5
0
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 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")
Esempio n. 7
0
def scalarmult_test():
        """
        """

        for i in range(0, 10):

                alicesk = nacl.randombytes(nacl.crypto_scalarmult_SCALARBYTES)
                alicepk = nacl.crypto_scalarmult_base(alicesk);

                bobsk = nacl.randombytes(nacl.crypto_scalarmult_SCALARBYTES)
                bobpk = nacl.crypto_scalarmult_base(bobsk);

                alicek = nacl.crypto_scalarmult(alicesk, bobpk)
                bobk   = nacl.crypto_scalarmult(bobsk, alicepk)

                if nacl.crypto_scalarmult(alicesk, bobpk) != nacl.crypto_scalarmult(bobsk, alicepk):
                        raise ValueError("crypto_scalarmult problem")
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 scalarmult_test():
    """
        """

    for i in range(0, 10):

        alicesk = nacl.randombytes(nacl.crypto_scalarmult_SCALARBYTES)
        alicepk = nacl.crypto_scalarmult_base(alicesk)

        bobsk = nacl.randombytes(nacl.crypto_scalarmult_SCALARBYTES)
        bobpk = nacl.crypto_scalarmult_base(bobsk)

        alicek = nacl.crypto_scalarmult(alicesk, bobpk)
        bobk = nacl.crypto_scalarmult(bobsk, alicepk)

        if nacl.crypto_scalarmult(alicesk, bobpk) != nacl.crypto_scalarmult(
                bobsk, alicepk):
            raise ValueError("crypto_scalarmult problem")
Esempio n. 10
0
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")
Esempio n. 11
0
def stream_test():
    """
        """

    mlen = 0
    while 1:
        mlen = mlen + 1 + int(mlen / 16)

        if mlen > 10000:
            break

        m = nacl.randombytes(mlen)
        n = nacl.randombytes(nacl.crypto_stream_NONCEBYTES)
        k = nacl.randombytes(nacl.crypto_stream_KEYBYTES)
        c = nacl.crypto_stream_xor(m, n, k)
        m1 = nacl.crypto_stream_xor(c, n, k)

        if m != m1:
            raise ValueError("crypto_stream_xor problem")
Esempio n. 12
0
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")
Esempio n. 13
0
def scalarmult_bad_test():
        """
        """

        sk = nacl.randombytes(nacl.crypto_scalarmult_SCALARBYTES)
        pk = nacl.crypto_scalarmult_base(sk);
        if len(pk) != nacl.crypto_scalarmult_BYTES:
                raise ValueError("invalid crypto_scalarmult_base output length")

        k = nacl.crypto_scalarmult(sk, pk)
        if len(k) != nacl.crypto_scalarmult_BYTES:
                raise ValueError("invalid crypto_scalarmult output length")

        ss = (
                nacl.randombytes(nacl.crypto_scalarmult_SCALARBYTES + 1),
                nacl.randombytes(nacl.crypto_scalarmult_SCALARBYTES - 1),
                0
        )
        pp = (
                nacl.randombytes(nacl.crypto_scalarmult_BYTES + 1),
                nacl.randombytes(nacl.crypto_scalarmult_BYTES - 1),
                0
        )
        for s in ss:
                try:
                        pk = nacl.crypto_scalarmult_base(s);
                except:
                        pass
                else:
                        raise Exception("crypto_scalarmult_base accepts incorrect input data")
                try:
                        k = nacl.crypto_scalarmult(s, pk);
                except:
                        pass
                else:
                        raise Exception("crypto_scalarmult accepts incorrect input data")
        for p in pp:
                try:
                        k = nacl.crypto_scalarmult(sk, p);
                except:
                        pass
                else:
                        raise Exception("crypto_scalarmult accepts incorrect input data")
Esempio n. 14
0
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")
Esempio n. 15
0
def stream_test():
        """
        """


        mlen = 0
        while 1:
                mlen = mlen + 1 + int(mlen / 16)

                if  mlen > 10000:
                        break

                m = nacl.randombytes(mlen)
                n = nacl.randombytes(nacl.crypto_stream_NONCEBYTES)
                k = nacl.randombytes(nacl.crypto_stream_KEYBYTES)
                c = nacl.crypto_stream_xor(m, n, k)
                m1 = nacl.crypto_stream_xor(c, n, k)

                if m != m1:
                        raise ValueError("crypto_stream_xor problem")
Esempio n. 16
0
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")
Esempio n. 17
0
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")
Esempio n. 18
0
def scalarmult_bad_test():
    """
        """

    sk = nacl.randombytes(nacl.crypto_scalarmult_SCALARBYTES)
    pk = nacl.crypto_scalarmult_base(sk)
    if len(pk) != nacl.crypto_scalarmult_BYTES:
        raise ValueError("invalid crypto_scalarmult_base output length")

    k = nacl.crypto_scalarmult(sk, pk)
    if len(k) != nacl.crypto_scalarmult_BYTES:
        raise ValueError("invalid crypto_scalarmult output length")

    ss = (nacl.randombytes(nacl.crypto_scalarmult_SCALARBYTES + 1),
          nacl.randombytes(nacl.crypto_scalarmult_SCALARBYTES - 1), 0)
    pp = (nacl.randombytes(nacl.crypto_scalarmult_BYTES + 1),
          nacl.randombytes(nacl.crypto_scalarmult_BYTES - 1), 0)
    for s in ss:
        try:
            pk = nacl.crypto_scalarmult_base(s)
        except:
            pass
        else:
            raise Exception(
                "crypto_scalarmult_base accepts incorrect input data")
        try:
            k = nacl.crypto_scalarmult(s, pk)
        except:
            pass
        else:
            raise Exception("crypto_scalarmult accepts incorrect input data")
    for p in pp:
        try:
            k = nacl.crypto_scalarmult(sk, p)
        except:
            pass
        else:
            raise Exception("crypto_scalarmult accepts incorrect input data")
Esempio n. 19
0
def hash_base_test():
    """
        """

    x = nacl.crypto_hash(nacl.randombytes(1))
    if (len(x) != nacl.crypto_hash_BYTES):
        raise ValueError("invalid crypto_hash output length")

    try:
        x = nacl.crypto_hash(0)
    except:
        pass
    else:
        raise Exception("crypto_hash accepts bad input data")
Esempio n. 20
0
def hash_base_test():
        """
        """

        x = nacl.crypto_hash(nacl.randombytes(1))
        if (len(x) != nacl.crypto_hash_BYTES):
                raise ValueError("invalid crypto_hash output length")
                
        try:
                x = nacl.crypto_hash(0)
        except:
                pass
        else:
                raise Exception("crypto_hash accepts bad input data")
Esempio n. 21
0
def stream_bad_test():
    """
        """

    n = nacl.randombytes(nacl.crypto_stream_NONCEBYTES)
    k = nacl.randombytes(nacl.crypto_stream_KEYBYTES)
    clen = 1
    m = nacl.randombytes(clen)

    bad = []
    tmp = {"clen": clen, "m": m, "k": k, "n": n}
    tmp["n"] = nacl.randombytes(nacl.crypto_stream_NONCEBYTES + 1)
    bad.append(tmp)
    tmp = {"clen": clen, "m": m, "k": k, "n": n}
    tmp["n"] = nacl.randombytes(nacl.crypto_stream_NONCEBYTES - 1)
    bad.append(tmp)
    tmp = {"clen": clen, "m": m, "k": k, "n": n}
    tmp["n"] = 0
    bad.append(tmp)
    tmp = {"clen": clen, "m": m, "k": k, "n": n}
    tmp["k"] = nacl.randombytes(nacl.crypto_stream_KEYBYTES + 1)
    bad.append(tmp)
    tmp = {"clen": clen, "m": m, "k": k, "n": n}
    tmp["k"] = nacl.randombytes(nacl.crypto_stream_KEYBYTES - 1)
    bad.append(tmp)
    tmp = {"clen": clen, "m": m, "k": k, "n": n}
    tmp["k"] = 0
    bad.append(tmp)
    tmp = {"clen": clen, "m": m, "k": k, "n": n}
    tmp["m"] = 0
    tmp["clen"] = -1
    bad.append(tmp)
    tmp = {"clen": clen, "m": m, "k": k, "n": n}
    tmp["m"] = 0
    tmp["clen"] = m
    bad.append(tmp)

    for tmp in bad:

        try:
            nacl.crypto_stream_xor(tmp["m"], tmp["n"], tmp["k"])
        except:
            pass
        else:
            raise Exception("crypto_stream_xor accepts incorrect input data")

        try:
            nacl.crypto_stream(tmp["clen"], tmp["n"], tmp["k"])
        except:
            pass
        else:
            raise Exception("crypto_stream accepts incorrect input data")
Esempio n. 22
0
def stream_bad_test():
        """
        """

        n = nacl.randombytes(nacl.crypto_stream_NONCEBYTES);
        k = nacl.randombytes(nacl.crypto_stream_KEYBYTES);
        clen = 1
        m = nacl.randombytes(clen);

        bad = []
        tmp = {"clen":clen, "m":m, "k":k, "n":n}
        tmp["n"] = nacl.randombytes(nacl.crypto_stream_NONCEBYTES + 1)
        bad.append(tmp)
        tmp = {"clen":clen, "m":m, "k":k, "n":n}
        tmp["n"] = nacl.randombytes(nacl.crypto_stream_NONCEBYTES - 1)
        bad.append(tmp)
        tmp = {"clen":clen, "m":m, "k":k, "n":n}
        tmp["n"] = 0
        bad.append(tmp)
        tmp = {"clen":clen, "m":m, "k":k, "n":n}
        tmp["k"] = nacl.randombytes(nacl.crypto_stream_KEYBYTES + 1)
        bad.append(tmp)
        tmp = {"clen":clen, "m":m, "k":k, "n":n}
        tmp["k"] = nacl.randombytes(nacl.crypto_stream_KEYBYTES - 1)
        bad.append(tmp)
        tmp = {"clen":clen, "m":m, "k":k, "n":n}
        tmp["k"] = 0
        bad.append(tmp)
        tmp = {"clen":clen, "m":m, "k":k, "n":n}
        tmp["m"] = 0
        tmp["clen"] = -1
        bad.append(tmp)
        tmp = {"clen":clen, "m":m, "k":k, "n":n}
        tmp["m"] = 0
        tmp["clen"] = m
        bad.append(tmp)

        for tmp in bad:

                try:
                        nacl.crypto_stream_xor(tmp["m"], tmp["n"], tmp["k"])
                except:
                        pass
                else:
                        raise Exception("crypto_stream_xor accepts incorrect input data")

                try:
                        nacl.crypto_stream(tmp["clen"], tmp["n"], tmp["k"])
                except:
                        pass
                else:
                        raise Exception("crypto_stream accepts incorrect input data")
Esempio n. 23
0
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")
Esempio n. 24
0
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")
Esempio n. 25
0
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")
Esempio n. 26
0
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")