Skip to content
Snippets Groups Projects
Commit 660a7ba6 authored by Brian Warner's avatar Brian Warner
Browse files

fix raw crypto_secretbox API

parent 7ab615aa
No related branches found
No related tags found
No related merge requests found
...@@ -23,14 +23,14 @@ crypto_secretbox_ZEROBYTES = lib.crypto_secretbox_zerobytes() ...@@ -23,14 +23,14 @@ crypto_secretbox_ZEROBYTES = lib.crypto_secretbox_zerobytes()
crypto_secretbox_BOXZEROBYTES = lib.crypto_secretbox_boxzerobytes() crypto_secretbox_BOXZEROBYTES = lib.crypto_secretbox_boxzerobytes()
def crypto_secretbox(key, message, nonce): def crypto_secretbox(message, nonce, key):
""" """
Encrypts and returns the message ``message`` with the secret ``key`` and Encrypts and returns the message ``message`` with the secret ``key`` and
the nonce ``nonce``. the nonce ``nonce``.
:param key: bytes
:param message: bytes :param message: bytes
:param nonce: bytes :param nonce: bytes
:param key: bytes
:rtype: bytes :rtype: bytes
""" """
if len(key) != crypto_secretbox_KEYBYTES: if len(key) != crypto_secretbox_KEYBYTES:
...@@ -49,14 +49,14 @@ def crypto_secretbox(key, message, nonce): ...@@ -49,14 +49,14 @@ def crypto_secretbox(key, message, nonce):
return ciphertext[crypto_secretbox_BOXZEROBYTES:] return ciphertext[crypto_secretbox_BOXZEROBYTES:]
def crypto_secretbox_open(key, ciphertext, nonce): def crypto_secretbox_open(ciphertext, nonce, key):
""" """
Decrypt and returns the encrypted message ``ciphertext`` with the secret Decrypt and returns the encrypted message ``ciphertext`` with the secret
``key`` and the nonce ``nonce``. ``key`` and the nonce ``nonce``.
:param key: bytes
:param ciphertext: bytes :param ciphertext: bytes
:param nonce: bytes :param nonce: bytes
:param key: bytes
:rtype: bytes :rtype: bytes
""" """
if len(key) != crypto_secretbox_KEYBYTES: if len(key) != crypto_secretbox_KEYBYTES:
......
...@@ -78,7 +78,7 @@ class SecretBox(encoding.Encodable, StringFixer, object): ...@@ -78,7 +78,7 @@ class SecretBox(encoding.Encodable, StringFixer, object):
"The nonce must be exactly %s bytes long" % self.NONCE_SIZE, "The nonce must be exactly %s bytes long" % self.NONCE_SIZE,
) )
ciphertext = nacl.c.crypto_secretbox(self._key, plaintext, nonce) ciphertext = nacl.c.crypto_secretbox(plaintext, nonce, self._key)
encoded_nonce = encoder.encode(nonce) encoded_nonce = encoder.encode(nonce)
encoded_ciphertext = encoder.encode(ciphertext) encoded_ciphertext = encoder.encode(ciphertext)
...@@ -113,6 +113,6 @@ class SecretBox(encoding.Encodable, StringFixer, object): ...@@ -113,6 +113,6 @@ class SecretBox(encoding.Encodable, StringFixer, object):
"The nonce must be exactly %s bytes long" % self.NONCE_SIZE, "The nonce must be exactly %s bytes long" % self.NONCE_SIZE,
) )
plaintext = nacl.c.crypto_secretbox_open(self._key, ciphertext, nonce) plaintext = nacl.c.crypto_secretbox_open(ciphertext, nonce, self._key)
return plaintext return plaintext
...@@ -37,12 +37,10 @@ def test_secretbox(): ...@@ -37,12 +37,10 @@ def test_secretbox():
key = "\x00" * c.crypto_secretbox_KEYBYTES key = "\x00" * c.crypto_secretbox_KEYBYTES
msg = "message" msg = "message"
nonce = "\x01" * c.crypto_secretbox_NONCEBYTES nonce = "\x01" * c.crypto_secretbox_NONCEBYTES
# TODO: NaCl is secretbox(msg,nonce,key) ct = c.crypto_secretbox(msg, nonce, key)
ct = c.crypto_secretbox(key, msg, nonce)
assert len(ct) == len(msg) + c.crypto_secretbox_BOXZEROBYTES assert len(ct) == len(msg) + c.crypto_secretbox_BOXZEROBYTES
assert hexlify(ct) == "3ae84dfb89728737bd6e2c8cacbaf8af3d34cc1666533a" assert hexlify(ct) == "3ae84dfb89728737bd6e2c8cacbaf8af3d34cc1666533a"
# TODO: NaCl is secretbox_open(ct,nonce,key) msg2 = c.crypto_secretbox_open(ct, nonce, key)
msg2 = c.crypto_secretbox_open(key, ct, nonce)
assert msg2 == msg assert msg2 == msg
def test_box(): def test_box():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment