Skip to content

Commit

Permalink
Merge pull request #125 from warner/rename-c
Browse files Browse the repository at this point in the history
Rename nacl.c to nacl.bindings, to avoid silent failures from the API change
  • Loading branch information
reaperhulk committed Mar 4, 2015
2 parents 4e8b74c + 89d2eb6 commit 2dee726
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 45 deletions.
8 changes: 5 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ Changes

* 0.3.0: the low-level API (`nacl.c.*`) has been changed to match the
upstream NaCl C/C++ conventions (as well as those of other NaCl bindings).
The order of arguments and return values has changed significantly. If you
have code which calls these functions (e.g. `nacl.c.crypto_box_keypair()`),
you must review the new docstrings and update your code to match the new
The order of arguments and return values has changed significantly. To
avoid silent failures, `nacl.c` has been removed, and replaced with
`nacl.bindings` (with the new argument ordering). If you have code which
calls these functions (e.g. `nacl.c.crypto_box_keypair()`), you must review
the new docstrings and update your code/imports to match the new
conventions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def run(self):
packages=[
"nacl",
"nacl._lib",
"nacl.c",
"nacl.bindings",
],
package_data={"nacl._lib": ["*.h"]},

Expand Down
14 changes: 7 additions & 7 deletions src/nacl/c/__init__.py → src/nacl/bindings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@

from __future__ import absolute_import, division, print_function

from nacl.c.crypto_box import (
from nacl.bindings.crypto_box import (
crypto_box, crypto_box_BEFORENMBYTES, crypto_box_BOXZEROBYTES,
crypto_box_NONCEBYTES, crypto_box_PUBLICKEYBYTES,
crypto_box_SECRETKEYBYTES, crypto_box_ZEROBYTES, crypto_box_afternm,
crypto_box_beforenm, crypto_box_keypair, crypto_box_open,
crypto_box_open_afternm,
)
from nacl.c.crypto_hash import (
from nacl.bindings.crypto_hash import (
crypto_hash, crypto_hash_BYTES, crypto_hash_sha256,
crypto_hash_sha256_BYTES, crypto_hash_sha512, crypto_hash_sha512_BYTES,
)
from nacl.c.crypto_scalarmult import (
from nacl.bindings.crypto_scalarmult import (
crypto_scalarmult, crypto_scalarmult_BYTES, crypto_scalarmult_SCALARBYTES,
crypto_scalarmult_base
)
from nacl.c.crypto_secretbox import (
from nacl.bindings.crypto_secretbox import (
crypto_secretbox, crypto_secretbox_BOXZEROBYTES, crypto_secretbox_KEYBYTES,
crypto_secretbox_NONCEBYTES, crypto_secretbox_ZEROBYTES,
crypto_secretbox_open
)
from nacl.c.crypto_sign import (
from nacl.bindings.crypto_sign import (
crypto_sign, crypto_sign_BYTES, crypto_sign_PUBLICKEYBYTES,
crypto_sign_SECRETKEYBYTES, crypto_sign_SEEDBYTES, crypto_sign_keypair,
crypto_sign_open, crypto_sign_seed_keypair,
)
from nacl.c.randombytes import randombytes
from nacl.c.sodium_core import sodium_init
from nacl.bindings.randombytes import randombytes
from nacl.bindings.sodium_core import sodium_init


__all__ = [
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/nacl/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

from __future__ import absolute_import, division, print_function

import nacl.c
import nacl.bindings
import nacl.encoding


def sha256(message, encoder=nacl.encoding.HexEncoder):
return encoder.encode(nacl.c.crypto_hash_sha256(message))
return encoder.encode(nacl.bindings.crypto_hash_sha256(message))


def sha512(message, encoder=nacl.encoding.HexEncoder):
return encoder.encode(nacl.c.crypto_hash_sha512(message))
return encoder.encode(nacl.bindings.crypto_hash_sha512(message))
16 changes: 8 additions & 8 deletions src/nacl/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
from __future__ import absolute_import, division, print_function

from nacl import encoding
import nacl.c
import nacl.bindings
from nacl.utils import EncryptedMessage, StringFixer, random


class PublicKey(encoding.Encodable, StringFixer, object):

SIZE = nacl.c.crypto_box_PUBLICKEYBYTES
SIZE = nacl.bindings.crypto_box_PUBLICKEYBYTES

def __init__(self, public_key, encoder=encoding.RawEncoder):
self._public_key = encoder.decode(public_key)
Expand All @@ -36,7 +36,7 @@ def __bytes__(self):

class PrivateKey(encoding.Encodable, StringFixer, object):

SIZE = nacl.c.crypto_box_SECRETKEYBYTES
SIZE = nacl.bindings.crypto_box_SECRETKEYBYTES

def __init__(self, private_key, encoder=encoding.RawEncoder):
# Decode the secret_key
Expand All @@ -47,7 +47,7 @@ def __init__(self, private_key, encoder=encoding.RawEncoder):
raise ValueError(
"The secret key must be exactly %d bytes long" % self.SIZE)

raw_public_key = nacl.c.crypto_scalarmult_base(private_key)
raw_public_key = nacl.bindings.crypto_scalarmult_base(private_key)

self._private_key = private_key
self.public_key = PublicKey(raw_public_key)
Expand All @@ -62,11 +62,11 @@ def generate(cls):

class Box(encoding.Encodable, StringFixer, object):

NONCE_SIZE = nacl.c.crypto_box_NONCEBYTES
NONCE_SIZE = nacl.bindings.crypto_box_NONCEBYTES

def __init__(self, private_key, public_key):
if private_key and public_key:
self._shared_key = nacl.c.crypto_box_beforenm(
self._shared_key = nacl.bindings.crypto_box_beforenm(
public_key.encode(encoder=encoding.RawEncoder),
private_key.encode(encoder=encoding.RawEncoder),
)
Expand All @@ -92,7 +92,7 @@ def encrypt(self, plaintext, nonce, encoder=encoding.RawEncoder):
raise ValueError("The nonce must be exactly %s bytes long" %
self.NONCE_SIZE)

ciphertext = nacl.c.crypto_box_afternm(
ciphertext = nacl.bindings.crypto_box_afternm(
plaintext,
nonce,
self._shared_key,
Expand Down Expand Up @@ -121,7 +121,7 @@ def decrypt(self, ciphertext, nonce=None, encoder=encoding.RawEncoder):
raise ValueError("The nonce must be exactly %s bytes long" %
self.NONCE_SIZE)

plaintext = nacl.c.crypto_box_open_afternm(
plaintext = nacl.bindings.crypto_box_open_afternm(
ciphertext,
nonce,
self._shared_key,
Expand Down
12 changes: 7 additions & 5 deletions src/nacl/secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
from __future__ import absolute_import, division, print_function

from nacl import encoding
import nacl.c
import nacl.bindings
from nacl.utils import EncryptedMessage, StringFixer


class SecretBox(encoding.Encodable, StringFixer, object):

KEY_SIZE = nacl.c.crypto_secretbox_KEYBYTES
NONCE_SIZE = nacl.c.crypto_secretbox_NONCEBYTES
KEY_SIZE = nacl.bindings.crypto_secretbox_KEYBYTES
NONCE_SIZE = nacl.bindings.crypto_secretbox_NONCEBYTES

def __init__(self, key, encoder=encoding.RawEncoder):
key = encoder.decode(key)
Expand All @@ -45,7 +45,8 @@ def encrypt(self, plaintext, nonce, encoder=encoding.RawEncoder):
"The nonce must be exactly %s bytes long" % self.NONCE_SIZE,
)

ciphertext = nacl.c.crypto_secretbox(plaintext, nonce, self._key)
ciphertext = nacl.bindings.crypto_secretbox(plaintext,
nonce, self._key)

encoded_nonce = encoder.encode(nonce)
encoded_ciphertext = encoder.encode(ciphertext)
Expand All @@ -71,6 +72,7 @@ def decrypt(self, ciphertext, nonce=None, encoder=encoding.RawEncoder):
"The nonce must be exactly %s bytes long" % self.NONCE_SIZE,
)

plaintext = nacl.c.crypto_secretbox_open(ciphertext, nonce, self._key)
plaintext = nacl.bindings.crypto_secretbox_open(ciphertext,
nonce, self._key)

return plaintext
23 changes: 12 additions & 11 deletions src/nacl/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import six

from nacl import encoding
import nacl.c
import nacl.bindings
from nacl.utils import StringFixer, random


Expand Down Expand Up @@ -62,10 +62,10 @@ def __init__(self, key, encoder=encoding.RawEncoder):
# Decode the key
key = encoder.decode(key)

if len(key) != nacl.c.crypto_sign_PUBLICKEYBYTES:
if len(key) != nacl.bindings.crypto_sign_PUBLICKEYBYTES:
raise ValueError(
"The key must be exactly %s bytes long" %
nacl.c.crypto_sign_PUBLICKEYBYTES,
nacl.bindings.crypto_sign_PUBLICKEYBYTES,
)

self._key = key
Expand Down Expand Up @@ -95,7 +95,7 @@ def verify(self, smessage, signature=None, encoder=encoding.RawEncoder):
# Decode the signed message
smessage = encoder.decode(smessage)

return nacl.c.crypto_sign_open(smessage, self._key)
return nacl.bindings.crypto_sign_open(smessage, self._key)


class SigningKey(encoding.Encodable, StringFixer, object):
Expand All @@ -122,13 +122,13 @@ def __init__(self, seed, encoder=encoding.RawEncoder):
seed = encoder.decode(seed)

# Verify that our seed is the proper size
if len(seed) != nacl.c.crypto_sign_SEEDBYTES:
if len(seed) != nacl.bindings.crypto_sign_SEEDBYTES:
raise ValueError(
"The seed must be exactly %d bytes long" %
nacl.c.crypto_sign_SEEDBYTES
nacl.bindings.crypto_sign_SEEDBYTES
)

public_key, secret_key = nacl.c.crypto_sign_seed_keypair(seed)
public_key, secret_key = nacl.bindings.crypto_sign_seed_keypair(seed)

self._seed = seed
self._signing_key = secret_key
Expand All @@ -145,7 +145,7 @@ def generate(cls):
:rtype: :class:`~nacl.signing.SigningKey`
"""
return cls(
random(nacl.c.crypto_sign_SEEDBYTES),
random(nacl.bindings.crypto_sign_SEEDBYTES),
encoder=encoding.RawEncoder,
)

Expand All @@ -157,10 +157,11 @@ def sign(self, message, encoder=encoding.RawEncoder):
:param encoder: A class that is used to encode the signed message.
:rtype: :class:`~nacl.signing.SignedMessage`
"""
raw_signed = nacl.c.crypto_sign(message, self._signing_key)
raw_signed = nacl.bindings.crypto_sign(message, self._signing_key)

signature = encoder.encode(raw_signed[:nacl.c.crypto_sign_BYTES])
message = encoder.encode(raw_signed[nacl.c.crypto_sign_BYTES:])
crypto_sign_BYTES = nacl.bindings.crypto_sign_BYTES
signature = encoder.encode(raw_signed[:crypto_sign_BYTES])
message = encoder.encode(raw_signed[crypto_sign_BYTES:])
signed = encoder.encode(raw_signed)

return SignedMessage._from_parts(signature, message, signed)
4 changes: 2 additions & 2 deletions src/nacl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import six

import nacl.c
import nacl.bindings


class EncryptedMessage(six.binary_type):
Expand Down Expand Up @@ -47,4 +47,4 @@ def __str__(self):


def random(size=32):
return nacl.c.randombytes(size)
return nacl.bindings.randombytes(size)
2 changes: 1 addition & 1 deletion tests/test_raw.py → tests/test_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import pytest

from nacl import c
from nacl import bindings as c
from nacl.exceptions import CryptoError


Expand Down
9 changes: 5 additions & 4 deletions tests/test_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import pytest

from nacl.bindings import crypto_sign_PUBLICKEYBYTES, crypto_sign_SEEDBYTES
import nacl.encoding
import nacl.exceptions
import nacl.signing
Expand Down Expand Up @@ -55,8 +56,8 @@ def test_wrong_length(self):
nacl.signing.SigningKey(b"")

def test_bytes(self):
k = nacl.signing.SigningKey(b"\x00" * nacl.c.crypto_sign_SEEDBYTES)
assert bytes(k) == b"\x00" * nacl.c.crypto_sign_SEEDBYTES
k = nacl.signing.SigningKey(b"\x00" * crypto_sign_SEEDBYTES)
assert bytes(k) == b"\x00" * crypto_sign_SEEDBYTES

@pytest.mark.parametrize("seed", [
b"77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a",
Expand Down Expand Up @@ -92,8 +93,8 @@ def test_wrong_length(self):
nacl.signing.VerifyKey(b"")

def test_bytes(self):
k = nacl.signing.VerifyKey(b"\x00" * nacl.c.crypto_sign_PUBLICKEYBYTES)
assert bytes(k) == b"\x00" * nacl.c.crypto_sign_PUBLICKEYBYTES
k = nacl.signing.VerifyKey(b"\x00" * crypto_sign_PUBLICKEYBYTES)
assert bytes(k) == b"\x00" * crypto_sign_PUBLICKEYBYTES

@pytest.mark.parametrize(
("public_key", "signed", "message", "signature"),
Expand Down

0 comments on commit 2dee726

Please sign in to comment.