Exemplo n.º 1
0
def test_secretbox_wrong_length():
    with pytest.raises(ValueError):
        c.crypto_secretbox(b"", b"", b"")
    with pytest.raises(ValueError):
        c.crypto_secretbox(b"", b"", b"\x00" * c.crypto_secretbox_KEYBYTES)
    with pytest.raises(ValueError):
        c.crypto_secretbox_open(b"", b"", b"")
    with pytest.raises(ValueError):
        c.crypto_secretbox_open(b"", b"",
                                b"\x00" * c.crypto_secretbox_KEYBYTES)
Exemplo n.º 2
0
def test_secretbox_wrong_length():
    with pytest.raises(ValueError):
        c.crypto_secretbox(b"", b"", b"")
    with pytest.raises(ValueError):
        c.crypto_secretbox(b"", b"", b"\x00" * c.crypto_secretbox_KEYBYTES)
    with pytest.raises(ValueError):
        c.crypto_secretbox_open(b"", b"", b"")
    with pytest.raises(ValueError):
        c.crypto_secretbox_open(
            b"", b"", b"\x00" * c.crypto_secretbox_KEYBYTES)
Exemplo n.º 3
0
def secretbox(private, plaintext):
    nonce = base58.b58encode(nacl.utils.random(24))
    ciphertext = base58.b58encode(
        c.crypto_secretbox(plaintext.encode(), base58.b58decode(nonce),
                           base58.b58decode(private)))

    return ciphertext, nonce
def EncryptAndSend(s, res):
    encr_res = nstp_v3_pb2.EncryptedMessage()
    nonce = utils.random(secret.SecretBox.NONCE_SIZE)
    encr_res.ciphertext = crypto_secretbox(res.SerializeToString(), nonce,
                                           dict_session_keys[s][1])
    encr_res.nonce = nonce
    nstp_res = nstp_v3_pb2.NSTPMessage()
    nstp_res.encrypted_message.CopyFrom(encr_res)
    print(nstp_res)
    response_message[s] = append_len(nstp_res.SerializeToString())
Exemplo n.º 5
0
    async def _send_encrypted(self, m: DecryptedMessage):
        """Send an encrypted message."""

        e = NSTPMessage()
        e.encrypted_message.ciphertext = crypto_secretbox(
            m.SerializeToString(), self.writer_nonce, self.writer_key)
        e.encrypted_message.nonce = self.writer_nonce
        sodium_increment(self.writer_nonce)
        message_data = e.SerializeToString()
        self.writer.write(message_data)
        await self.writer.drain()
Exemplo n.º 6
0
def test_secretbox():
    key = b"\x00" * c.crypto_secretbox_KEYBYTES
    msg = b"message"
    nonce = b"\x01" * c.crypto_secretbox_NONCEBYTES
    ct = c.crypto_secretbox(msg, nonce, key)
    assert len(ct) == len(msg) + c.crypto_secretbox_BOXZEROBYTES
    assert tohex(ct) == "3ae84dfb89728737bd6e2c8cacbaf8af3d34cc1666533a"
    msg2 = c.crypto_secretbox_open(ct, nonce, key)
    assert msg2 == msg

    with pytest.raises(CryptoError):
        c.crypto_secretbox_open(
            msg + b"!",
            nonce,
            key,
        )
Exemplo n.º 7
0
def test_secretbox():
    key = b"\x00" * c.crypto_secretbox_KEYBYTES
    msg = b"message"
    nonce = b"\x01" * c.crypto_secretbox_NONCEBYTES
    ct = c.crypto_secretbox(msg, nonce, key)
    assert len(ct) == len(msg) + c.crypto_secretbox_BOXZEROBYTES
    assert tohex(ct) == "3ae84dfb89728737bd6e2c8cacbaf8af3d34cc1666533a"
    msg2 = c.crypto_secretbox_open(ct, nonce, key)
    assert msg2 == msg

    with pytest.raises(CryptoError):
        c.crypto_secretbox_open(
            msg + b"!",
            nonce,
            key,
        )
full_msg = recv_full_msg(len_msg[0], s)
res_msg = nstp_v3_pb2.NSTPMessage()
res_msg.ParseFromString(full_msg)
print(res_msg)
s_hello = nstp_v3_pb2.ServerHello()
s_hello.CopyFrom(res_msg.server_hello)

rx, tx = crypto_kx.crypto_kx_client_session_keys(pk_client, sk_client,
                                                 s_hello.public_key)
#AuthReq
auth_req = nstp_v3_pb2.AuthenticationRequest()
auth_req.username = "******"
auth_req.password = "******"
random_nonce = nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE)
encr_auth_req = nstp_v3_pb2.EncryptedMessage()
encr_auth_req.ciphertext = crypto_secretbox(auth_req.SerializeToString(),
                                            random_nonce, tx)
encr_auth_req.nonce = random_nonce
print(encr_auth_req.ciphertext, encr_auth_req.nonce)
nstp_req = nstp_v3_pb2.NSTPMessage()
nstp_req.encrypted_message.CopyFrom(encr_auth_req)
s.send(append_len(nstp_req.SerializeToString()))

data = s.recv(2)
print("receiving data")
len_msg = struct.unpack('!H', data[:2])
print("length of message to be received {}".format(len_msg[0]))
full_msg = recv_full_msg(len_msg[0], s)
res_msg = nstp_v3_pb2.NSTPMessage()
res_msg.ParseFromString(full_msg)
print(res_msg)