Example #1
0
def aead_chacha20poly1305_encrypt(key, input, nonce):
    key = bytes.fromhex(key)
    ad = nonce = bytes.fromhex(nonce)
    input = input.encode()
    output_str = pysodium.crypto_aead_chacha20poly1305_encrypt(
        input, ad, nonce, key)
    return output_str.hex()
Example #2
0
def encrypt_aead(key, pt):
    nonce = pysodium.randombytes(12)
    ad = ""
    cipher = pysodium.crypto_aead_chacha20poly1305_encrypt(pt, ad, nonce, key)
    print "Chacha20 Poly1305: Plaintext Len: " + str(
        len(pt)) + " Cipher Len: " + str(len(cipher))
    return nonce + cipher
Example #3
0
def simpleTest():
    key = "AAAAAAAAaaaaaaaaAAAAAAAAaaaaaaaa"
    input_ = "ItsNotATumor"

    print "Key:", key, "(", len(key), " bytes)"
    print "Input:", input_, " (", len(input_), " bytes)"

    nonce = pysodium.randombytes(12)
    ad = "1234"
    print "Nonce:", nonce, "(", len(nonce), " bytes)"
    print "Additional Data: ", ad, " (", len(ad), " bytes)"
    cipher = pysodium.crypto_aead_chacha20poly1305_encrypt(
        input_, ad, nonce, key)
    print "Cipher:", cipher, " (", len(cipher), " bytes)"
    print "Network Packet:", cipher, ad, nonce, " (", len(cipher) + len(
        ad) + len(nonce), " bytes)"

    try:
        plaintext = pysodium.crypto_aead_chacha20poly1305_decrypt(
            cipher, ad, nonce, key)
    except Exception:
        print "Failed to verify"
    else:
        print "Verified and Decrypted."
        print "Plaintext:", plaintext
Example #4
0
    def constructSTSResponse(self, mType, addr, exchangeData):
        data = None
        addrStr = sts_utility.addrToString(addr)
        if addrStr in STS.STSConnectionStates.keys():

            if STS.STSConnectionStates[addrStr][
                    'cSuite'] == 1 or STS.STSConnectionStates[addrStr][
                        'cSuite'] == 4:
                nonce = pysodium.randombytes(
                    pysodium.crypto_aead_chacha20poly1305_ietf_NONCEBYTES)
                m = struct.pack('>B', mType) + struct.pack('>I', len(nonce))
                # T = struct.pack('>Q', int(time.time()))
                encrypData = pysodium.crypto_aead_chacha20poly1305_ietf_encrypt(
                    exchangeData, m, nonce,
                    STS.STSConnectionStates[addrStr]['session_key'][:32])
                data = m + nonce + encrypData

            elif STS.STSConnectionStates[addrStr][
                    'cSuite'] == 2 or STS.STSConnectionStates[addrStr][
                        'cSuite'] == 5:
                nonce = pysodium.randombytes(
                    pysodium.crypto_aead_chacha20poly1305_NONCEBYTES)
                m = struct.pack('>B', mType) + struct.pack('>I', len(nonce))
                # T = struct.pack('>Q', int(time.time()))
                encrypData = pysodium.crypto_aead_chacha20poly1305_encrypt(
                    exchangeData, m, nonce,
                    STS.STSConnectionStates[addrStr]['session_key'][:32])
                data = m + nonce + encrypData
        # print(binascii.hexlify(T))
        # print(binascii.hexlify(pysodium.crypto_aead_chacha20poly1305_ietf_decrypt(encrypData, m, nonce, myTX[:32])))
        return data
Example #5
0
 def test_aead_chacha20poly1305(self):
     key = binascii.unhexlify(b"4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd1100a1007")
     input_ = binascii.unhexlify(b"86d09974840bded2a5ca")
     nonce = binascii.unhexlify(b"cd7cf67be39c794a")
     ad = binascii.unhexlify(b"87e229d4500845a079c0")
     output = pysodium.crypto_aead_chacha20poly1305_encrypt(input_, ad, nonce, key)
     self.assertEqual(binascii.unhexlify(b"e3e446f7ede9a19b62a4677dabf4e3d24b876bb284753896e1d6"), output)
     output = pysodium.crypto_aead_chacha20poly1305_decrypt(output, ad, nonce, key)
     self.assertEqual(output, input_)
Example #6
0
 def test_aead_chacha20poly1305(self):
     key = binascii.unhexlify(b"4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd1100a1007")
     input_ = binascii.unhexlify(b"86d09974840bded2a5ca")
     nonce = binascii.unhexlify(b"cd7cf67be39c794a")
     ad = binascii.unhexlify(b"87e229d4500845a079c0")
     output = pysodium.crypto_aead_chacha20poly1305_encrypt(input_, ad, nonce, key)
     self.assertEqual(binascii.unhexlify(b"e3e446f7ede9a19b62a4677dabf4e3d24b876bb284753896e1d6"), output)
     output = pysodium.crypto_aead_chacha20poly1305_decrypt(output, ad, nonce, key)
     self.assertEqual(output, input_)
Example #7
0
def udpServe(key=b'this is my key value!',addr=('localhost',8080)) :
    s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    nonce = pysodium.randombytes(8)
    ciphertext = pysodium.crypto_aead_chacha20poly1305_encrypt(b'this is my key value!',None,nonce,key)
    plaintext = pysodium.crypto_aead_chacha20poly1305_decrypt(ciphertext,None,nonce,key)
    print(plaintext)
    print(ciphertext)
    print(reprlib.repr(ciphertext))
    print(nonce)
    print(ciphertext+nonce)
    s.sendto(ciphertext+nonce,addr)
    s.close()
Example #8
0
 def test_aead_chacha20poly1305(self):
     key = binascii.unhexlify(b"4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd1100a1007")
     input_ = binascii.unhexlify(b"86d09974840bded2a5ca")
     nonce = binascii.unhexlify(b"cd7cf67be39c794a")
     ct_common = b"e3e446f7ede9a19b62a4"
     for ad, ct in [
             (binascii.unhexlify(b"87e229d4500845a079c0"), b"677dabf4e3d24b876bb284753896e1d6"),
             (None,                                        b"69e7789bcd954e658ed38423e23161dc"),
     ]:
         output = pysodium.crypto_aead_chacha20poly1305_encrypt(input_, ad, nonce, key)
         self.assertEqual(binascii.unhexlify(ct_common + ct), output)
         output = pysodium.crypto_aead_chacha20poly1305_decrypt(output, ad, nonce, key)
         self.assertEqual(output, input_)
Example #9
0
 def test_aead_chacha20poly1305(self):
     key = binascii.unhexlify(b"4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd1100a1007")
     input_ = binascii.unhexlify(b"86d09974840bded2a5ca")
     nonce = binascii.unhexlify(b"cd7cf67be39c794a")
     ct_common = b"e3e446f7ede9a19b62a4"
     for ad, ct in [
             (binascii.unhexlify(b"87e229d4500845a079c0"), b"677dabf4e3d24b876bb284753896e1d6"),
             (None,                                        b"69e7789bcd954e658ed38423e23161dc"),
     ]:
         output = pysodium.crypto_aead_chacha20poly1305_encrypt(input_, ad, nonce, key)
         self.assertEqual(binascii.unhexlify(ct_common + ct), output)
         output = pysodium.crypto_aead_chacha20poly1305_decrypt(output, ad, nonce, key)
         self.assertEqual(output, input_)
Example #10
0
def twolayers():
    key = "AAAAAAAAaaaaaaaaAAAAAAAAaaaaaaaa"
    input_ = "ItsNotATumor"

    print "Key:", key, "(", len(key), " bytes)"
    print "Input:", input_, " (", len(input_), " bytes)"

    nonce = pysodium.randombytes(12)
    ad = "1234"
    print "Nonce:", nonce, "(", len(nonce), " bytes)"
    print "Additional Data: ", ad, " (", len(ad), " bytes)"
    cipher = pysodium.crypto_aead_chacha20poly1305_encrypt(
        input_, ad, nonce, key)
    print "Cipher:", cipher, " (", len(cipher), " bytes)"
    print "Cipher1:", cipher, ad, nonce, " (", len(cipher) + len(ad) + len(
        nonce), " bytes)"

    key2 = "BBBBBBBBbbbbbbbbBBBBBBBBbbbbbbbb"
    nonce2 = pysodium.randombytes(12)
    cipher2 = pysodium.crypto_aead_chacha20poly1305_encrypt(
        cipher, ad, nonce2, key2)
    print "Ciper2:", cipher2, " (", len(cipher2), " bytes)"

    try:
        cipher1 = pysodium.crypto_aead_chacha20poly1305_decrypt(
            cipher2, ad, nonce2, key2)
    except Exception:
        print "Failed to verify outer layer."
    else:
        print "Verified and Decrypted Outer Layer."
        try:
            plaintext = pysodium.crypto_aead_chacha20poly1305_decrypt(
                cipher1, ad, nonce, key)
        except Exception:
            print "Failed to verify inner layer."
        else:
            print "Plaintext: ", plaintext
Example #11
0
def test_pysodium():
    from xaal.lib import messages
    import pysodium

    payload = "FooBar".encode("utf-8")
    ad = '[]'

    data = messages.build_timestamp()
    nonce = messages.build_nonce(data)
    key = tools.pass2key("My Friend Goo")

    dump_hex("Payload", payload)
    dump_hex("Key", key)

    ciph = pysodium.crypto_aead_chacha20poly1305_encrypt(
        payload, ad, nonce, key)
    dump_hex("Ciph", ciph)

    pjson = pysodium.crypto_aead_chacha20poly1305_decrypt(ciph, ad, nonce, key)
    print(pjson)
Example #12
0
def crypt_aed_encrypt(data, keystr):
    key = binascii.unhexlify(binary_to_hex(pysodium.crypto_hash_sha256(keystr)))
    nonce = binascii.unhexlify(b"cd7cf67be39c7977")
    ad = ""
    rtn = pysodium.crypto_aead_chacha20poly1305_encrypt(data, ad, nonce, key)
    return rtn
Example #13
0
def encrypt_box(box_key, box_number, plaintext_header_packed):
    r = pysodium.crypto_aead_chacha20poly1305_encrypt(
        bytearray_to_char_array(plaintext_header_packed), None,
        u64_to_be(box_number), box_key)
    return r
Example #14
0
def encrypt(k, data):
    n = rand(pysodium.crypto_aead_chacha20poly1305_NONCEBYTES)
    return n + pysodium.crypto_aead_chacha20poly1305_encrypt(data, None, n, k)