From b8491dd7d6f3060c78b64c995a1ad643b72e9544 Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Mon, 22 Dec 2014 17:08:33 +0100 Subject: [PATCH] Working cxfreeze version on windows Wont run because we have to port Pynacl code to libnacl --- lib/nacl/__init__.py | 33 ------ lib/nacl/_lib/__init__.py | 76 ------------- lib/nacl/_lib/crypto_box.h | 43 ------- lib/nacl/_lib/crypto_hash.h | 29 ----- lib/nacl/_lib/crypto_scalarmult.h | 20 ---- lib/nacl/_lib/crypto_secretbox.h | 28 ----- lib/nacl/_lib/crypto_sign.h | 33 ------ lib/nacl/_lib/randombytes.h | 16 --- lib/nacl/_lib/sodium_core.h | 16 --- lib/nacl/c/__init__.py | 94 ---------------- lib/nacl/c/crypto_box.py | 180 ------------------------------ lib/nacl/c/crypto_hash.py | 63 ----------- lib/nacl/c/crypto_scalarmult.py | 54 --------- lib/nacl/c/crypto_secretbox.py | 77 ------------- lib/nacl/c/crypto_sign.py | 102 ----------------- lib/nacl/c/randombytes.py | 30 ----- lib/nacl/c/sodium_core.py | 26 ----- lib/nacl/encoding.py | 90 --------------- lib/nacl/exceptions.py | 27 ----- lib/nacl/hash.py | 26 ----- lib/nacl/public.py | 130 --------------------- lib/nacl/secret.py | 76 ------------- lib/nacl/signing.py | 166 --------------------------- lib/nacl/utils.py | 50 --------- lib/ucoinpy/documents/__init__.py | 2 - lib/ucoinpy/key/__init__.py | 9 +- setup.py | 4 +- src/cutecoin/core/__init__.py | 0 src/cutecoin/core/app.py | 1 - src/cutecoin/core/config.py | 1 - src/cutecoin/core/wallet.py | 3 +- 31 files changed, 7 insertions(+), 1498 deletions(-) delete mode 100644 lib/nacl/__init__.py delete mode 100644 lib/nacl/_lib/__init__.py delete mode 100644 lib/nacl/_lib/crypto_box.h delete mode 100644 lib/nacl/_lib/crypto_hash.h delete mode 100644 lib/nacl/_lib/crypto_scalarmult.h delete mode 100644 lib/nacl/_lib/crypto_secretbox.h delete mode 100644 lib/nacl/_lib/crypto_sign.h delete mode 100644 lib/nacl/_lib/randombytes.h delete mode 100644 lib/nacl/_lib/sodium_core.h delete mode 100644 lib/nacl/c/__init__.py delete mode 100644 lib/nacl/c/crypto_box.py delete mode 100644 lib/nacl/c/crypto_hash.py delete mode 100644 lib/nacl/c/crypto_scalarmult.py delete mode 100644 lib/nacl/c/crypto_secretbox.py delete mode 100644 lib/nacl/c/crypto_sign.py delete mode 100644 lib/nacl/c/randombytes.py delete mode 100644 lib/nacl/c/sodium_core.py delete mode 100644 lib/nacl/encoding.py delete mode 100644 lib/nacl/exceptions.py delete mode 100644 lib/nacl/hash.py delete mode 100644 lib/nacl/public.py delete mode 100644 lib/nacl/secret.py delete mode 100644 lib/nacl/signing.py delete mode 100644 lib/nacl/utils.py create mode 100644 src/cutecoin/core/__init__.py diff --git a/lib/nacl/__init__.py b/lib/nacl/__init__.py deleted file mode 100644 index 0b0bb415..00000000 --- a/lib/nacl/__init__.py +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -__all__ = [ - "__title__", "__summary__", "__uri__", "__version__", "__author__", - "__email__", "__license__", "__copyright__", -] - -__title__ = "PyNaCl" -__summary__ = ("Python binding to the Networking and Cryptography (NaCl) " - "library") -__uri__ = "https://github.com/pyca/pynacl/" - -__version__ = "0.3.0" - -__author__ = "Donald Stufft" -__email__ = "donald@stufft.io" - -__license__ = "Apache License 2.0" -__copyright__ = "Copyright 2013 Donald Stufft and individual contributors" diff --git a/lib/nacl/_lib/__init__.py b/lib/nacl/_lib/__init__.py deleted file mode 100644 index f82496ea..00000000 --- a/lib/nacl/_lib/__init__.py +++ /dev/null @@ -1,76 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -from __future__ import absolute_import, division, print_function - -import glob -import os.path - -from cffi import FFI -from cffi.verifier import Verifier - - -__all__ = ["ffi"] - - -HEADERS = glob.glob( - os.path.join(os.path.abspath(os.path.dirname(__file__)), "*.h") -) - - -# Build our FFI instance -ffi = FFI() - - -# Add all of our header files, but sort first for consistency of the -# hash that CFFI generates and uses in the .so filename (the order of -# glob() results cannot be relied on) -for header in sorted(HEADERS): - with open(header, "r") as hfile: - ffi.cdef(hfile.read()) - - -# TODO: Can we use the ABI of libsodium for this instead? -ffi.verifier = Verifier( - ffi, - - "#include <sodium.h>", - - # We need to link to the sodium library - libraries=["sodium"], - - # Our ext_package is nacl so look for it - ext_package="nacl._lib", -) - - -class Library(object): - - def __init__(self, ffi): - self.ffi = ffi - self._lib = None - - # This prevents the compile_module() from being called, the module - # should have been compiled by setup.py - def _compile_module(*args, **kwargs): - raise RuntimeError("Cannot compile module during runtime") - self.ffi.verifier.compile_module = _compile_module - - def __getattr__(self, name): - if self._lib is None: - self._lib = self.ffi.verifier.load_library() - - # redirect attribute access to the underlying lib - return getattr(self._lib, name) - -lib = Library(ffi) diff --git a/lib/nacl/_lib/crypto_box.h b/lib/nacl/_lib/crypto_box.h deleted file mode 100644 index e7d59f76..00000000 --- a/lib/nacl/_lib/crypto_box.h +++ /dev/null @@ -1,43 +0,0 @@ -/* Copyright 2013 Donald Stufft and individual contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -size_t crypto_box_secretkeybytes(); -size_t crypto_box_publickeybytes(); -size_t crypto_box_zerobytes(); -size_t crypto_box_boxzerobytes(); -size_t crypto_box_noncebytes(); -size_t crypto_box_beforenmbytes(); - - -int crypto_box_keypair(unsigned char *pk, unsigned char *sk); - -int crypto_box(unsigned char *c, const unsigned char *m, - unsigned long long mlen, const unsigned char *n, - const unsigned char *pk, const unsigned char *sk); - -int crypto_box_open(unsigned char *m, const unsigned char *c, - unsigned long long clen, const unsigned char *n, - const unsigned char *pk, const unsigned char *sk); - -int crypto_box_beforenm(unsigned char *k, const unsigned char *pk, - const unsigned char *sk); - -int crypto_box_afternm(unsigned char *c, const unsigned char *m, - unsigned long long mlen, const unsigned char *n, - const unsigned char *k); - -int crypto_box_open_afternm(unsigned char *m, const unsigned char *c, - unsigned long long clen, const unsigned char *n, - const unsigned char *k); diff --git a/lib/nacl/_lib/crypto_hash.h b/lib/nacl/_lib/crypto_hash.h deleted file mode 100644 index 9ca64737..00000000 --- a/lib/nacl/_lib/crypto_hash.h +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright 2013 Donald Stufft and individual contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -// size_t crypto_hash_bytes(); -size_t crypto_hash_sha256_bytes(); -size_t crypto_hash_sha512_bytes(); - - -int crypto_hash(unsigned char *out, const unsigned char *in, - unsigned long long inlen); - -int crypto_hash_sha256(unsigned char *out, const unsigned char *in, - unsigned long long inlen); - -int crypto_hash_sha512(unsigned char *out, const unsigned char *in, - unsigned long long inlen); diff --git a/lib/nacl/_lib/crypto_scalarmult.h b/lib/nacl/_lib/crypto_scalarmult.h deleted file mode 100644 index e7990389..00000000 --- a/lib/nacl/_lib/crypto_scalarmult.h +++ /dev/null @@ -1,20 +0,0 @@ -/* Copyright 2013 Donald Stufft and individual contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -size_t crypto_scalarmult_bytes(); -size_t crypto_scalarmult_scalarbytes(); - -int crypto_scalarmult_base(unsigned char *q, const unsigned char *n); -int crypto_scalarmult(unsigned char *q, const unsigned char *n, const unsigned char *p); diff --git a/lib/nacl/_lib/crypto_secretbox.h b/lib/nacl/_lib/crypto_secretbox.h deleted file mode 100644 index 88da2a35..00000000 --- a/lib/nacl/_lib/crypto_secretbox.h +++ /dev/null @@ -1,28 +0,0 @@ -/* Copyright 2013 Donald Stufft and individual contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -size_t crypto_secretbox_keybytes(); -size_t crypto_secretbox_noncebytes(); -size_t crypto_secretbox_zerobytes(); -size_t crypto_secretbox_boxzerobytes(); - - -int crypto_secretbox(unsigned char *c, const unsigned char *m, - unsigned long long mlen, const unsigned char *n, - const unsigned char *k); - -int crypto_secretbox_open(unsigned char *m, const unsigned char *c, - unsigned long long clen, const unsigned char *n, - const unsigned char *k); diff --git a/lib/nacl/_lib/crypto_sign.h b/lib/nacl/_lib/crypto_sign.h deleted file mode 100644 index e4015679..00000000 --- a/lib/nacl/_lib/crypto_sign.h +++ /dev/null @@ -1,33 +0,0 @@ -/* Copyright 2013 Donald Stufft and individual contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -size_t crypto_sign_bytes(); -// size_t crypto_sign_seedbytes(); -size_t crypto_sign_publickeybytes(); -size_t crypto_sign_secretkeybytes(); - - -int crypto_sign_keypair(unsigned char *pk, unsigned char *sk); - -int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk, - const unsigned char *seed); - -int crypto_sign(unsigned char *sm, unsigned long long *smlen, - const unsigned char *m, unsigned long long mlen, - const unsigned char *sk); - -int crypto_sign_open(unsigned char *m, unsigned long long *mlen, - const unsigned char *sm, unsigned long long smlen, - const unsigned char *pk); diff --git a/lib/nacl/_lib/randombytes.h b/lib/nacl/_lib/randombytes.h deleted file mode 100644 index 6952fbbe..00000000 --- a/lib/nacl/_lib/randombytes.h +++ /dev/null @@ -1,16 +0,0 @@ -/* Copyright 2013 Donald Stufft and individual contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -void randombytes(unsigned char * const buf, const unsigned long long buf_len); diff --git a/lib/nacl/_lib/sodium_core.h b/lib/nacl/_lib/sodium_core.h deleted file mode 100644 index b0059d5e..00000000 --- a/lib/nacl/_lib/sodium_core.h +++ /dev/null @@ -1,16 +0,0 @@ -/* Copyright 2013 Donald Stufft and individual contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -int sodium_init(); diff --git a/lib/nacl/c/__init__.py b/lib/nacl/c/__init__.py deleted file mode 100644 index 0be71265..00000000 --- a/lib/nacl/c/__init__.py +++ /dev/null @@ -1,94 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -from nacl.c.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 ( - 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 ( - crypto_scalarmult, crypto_scalarmult_BYTES, crypto_scalarmult_SCALARBYTES, - crypto_scalarmult_base -) -from nacl.c.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 ( - 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 - - -__all__ = [ - "crypto_box_SECRETKEYBYTES", - "crypto_box_PUBLICKEYBYTES", - "crypto_box_NONCEBYTES", - "crypto_box_ZEROBYTES", - "crypto_box_BOXZEROBYTES", - "crypto_box_BEFORENMBYTES", - "crypto_box_keypair", - "crypto_box", - "crypto_box_open", - "crypto_box_beforenm", - "crypto_box_afternm", - "crypto_box_open_afternm", - - "crypto_hash_BYTES", - "crypto_hash_sha256_BYTES", - "crypto_hash_sha512_BYTES", - "crypto_hash", - "crypto_hash_sha256", - "crypto_hash_sha512", - - "crypto_scalarmult_BYTES", - "crypto_scalarmult_SCALARBYTES", - "crypto_scalarmult", - "crypto_scalarmult_base", - - "crypto_secretbox_KEYBYTES", - "crypto_secretbox_NONCEBYTES", - "crypto_secretbox_ZEROBYTES", - "crypto_secretbox_BOXZEROBYTES", - "crypto_secretbox", - "crypto_secretbox_open", - - "crypto_sign_BYTES", - "crypto_sign_SEEDBYTES", - "crypto_sign_PUBLICKEYBYTES", - "crypto_sign_SECRETKEYBYTES", - "crypto_sign_keypair", - "crypto_sign_seed_keypair", - "crypto_sign", - "crypto_sign_open", - - "randombytes", - - "sodium_init", -] - -# Initialize Sodium -sodium_init() diff --git a/lib/nacl/c/crypto_box.py b/lib/nacl/c/crypto_box.py deleted file mode 100644 index d9fd4ec7..00000000 --- a/lib/nacl/c/crypto_box.py +++ /dev/null @@ -1,180 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -from nacl._lib import lib -from nacl.exceptions import CryptoError - - -__all__ = ["crypto_box_keypair", "crypto_box"] - - -crypto_box_SECRETKEYBYTES = lib.crypto_box_secretkeybytes() -crypto_box_PUBLICKEYBYTES = lib.crypto_box_publickeybytes() -crypto_box_NONCEBYTES = lib.crypto_box_noncebytes() -crypto_box_ZEROBYTES = lib.crypto_box_zerobytes() -crypto_box_BOXZEROBYTES = lib.crypto_box_boxzerobytes() -crypto_box_BEFORENMBYTES = lib.crypto_box_beforenmbytes() - - -def crypto_box_keypair(): - """ - Returns a randomly generated public and secret key. - - :rtype: (bytes(public_key), bytes(secret_key)) - """ - pk = lib.ffi.new("unsigned char[]", crypto_box_PUBLICKEYBYTES) - sk = lib.ffi.new("unsigned char[]", crypto_box_SECRETKEYBYTES) - - if lib.crypto_box_keypair(pk, sk) != 0: - raise CryptoError("An error occurred trying to generate the keypair") - - return ( - lib.ffi.buffer(pk, crypto_box_PUBLICKEYBYTES)[:], - lib.ffi.buffer(sk, crypto_box_SECRETKEYBYTES)[:], - ) - - -def crypto_box(message, nonce, pk, sk): - """ - Encrypts and returns a message ``message`` using the secret key ``sk``, - public key ``pk``, and the nonce ``nonce``. - - :param message: bytes - :param nonce: bytes - :param pk: bytes - :param sk: bytes - :rtype: bytes - """ - if len(nonce) != crypto_box_NONCEBYTES: - raise ValueError("Invalid nonce size") - - if len(pk) != crypto_box_PUBLICKEYBYTES: - raise ValueError("Invalid public key") - - if len(sk) != crypto_box_SECRETKEYBYTES: - raise ValueError("Invalid secret key") - - padded = (b"\x00" * crypto_box_ZEROBYTES) + message - ciphertext = lib.ffi.new("unsigned char[]", len(padded)) - - if lib.crypto_box(ciphertext, padded, len(padded), nonce, pk, sk) != 0: - raise CryptoError("An error occurred trying to encrypt the message") - - return lib.ffi.buffer(ciphertext, len(padded))[crypto_box_BOXZEROBYTES:] - - -def crypto_box_open(ciphertext, nonce, pk, sk): - """ - Decrypts and returns an encrypted message ``ciphertext``, using the secret - key ``sk``, public key ``pk``, and the nonce ``nonce``. - - :param ciphertext: bytes - :param nonce: bytes - :param pk: bytes - :param sk: bytes - :rtype: bytes - """ - if len(nonce) != crypto_box_NONCEBYTES: - raise ValueError("Invalid nonce size") - - if len(pk) != crypto_box_PUBLICKEYBYTES: - raise ValueError("Invalid public key") - - if len(sk) != crypto_box_SECRETKEYBYTES: - raise ValueError("Invalid secret key") - - padded = (b"\x00" * crypto_box_BOXZEROBYTES) + ciphertext - plaintext = lib.ffi.new("unsigned char[]", len(padded)) - - if lib.crypto_box_open(plaintext, padded, len(padded), nonce, pk, sk) != 0: - raise CryptoError("An error occurred trying to decrypt the message") - - return lib.ffi.buffer(plaintext, len(padded))[crypto_box_ZEROBYTES:] - - -def crypto_box_beforenm(pk, sk): - """ - Computes and returns the shared key for the public key ``pk`` and the - secret key ``sk``. This can be used to speed up operations where the same - set of keys is going to be used multiple times. - - :param pk: bytes - :param sk: bytes - :rtype: bytes - """ - if len(pk) != crypto_box_PUBLICKEYBYTES: - raise ValueError("Invalid public key") - - if len(sk) != crypto_box_SECRETKEYBYTES: - raise ValueError("Invalid secret key") - - k = lib.ffi.new("unsigned char[]", crypto_box_BEFORENMBYTES) - - if lib.crypto_box_beforenm(k, pk, sk) != 0: - raise CryptoError("An error occurred computing the shared key.") - - return lib.ffi.buffer(k, crypto_box_BEFORENMBYTES)[:] - - -def crypto_box_afternm(message, nonce, k): - """ - Encrypts and returns the message ``message`` using the shared key ``k`` and - the nonce ``nonce``. - - :param message: bytes - :param nonce: bytes - :param k: bytes - :rtype: bytes - """ - if len(nonce) != crypto_box_NONCEBYTES: - raise ValueError("Invalid nonce") - - if len(k) != crypto_box_BEFORENMBYTES: - raise ValueError("Invalid shared key") - - padded = b"\x00" * crypto_box_ZEROBYTES + message - ciphertext = lib.ffi.new("unsigned char[]", len(padded)) - - if lib.crypto_box_afternm(ciphertext, padded, len(padded), nonce, k) != 0: - raise CryptoError("An error occurred trying to encrypt the message") - - return lib.ffi.buffer(ciphertext, len(padded))[crypto_box_BOXZEROBYTES:] - - -def crypto_box_open_afternm(ciphertext, nonce, k): - """ - Decrypts and returns the encrypted message ``ciphertext``, using the shared - key ``k`` and the nonce ``nonce``. - - :param ciphertext: bytes - :param nonce: bytes - :param k: bytes - :rtype: bytes - """ - if len(nonce) != crypto_box_NONCEBYTES: - raise ValueError("Invalid nonce") - - if len(k) != crypto_box_BEFORENMBYTES: - raise ValueError("Invalid shared key") - - padded = (b"\x00" * crypto_box_BOXZEROBYTES) + ciphertext - plaintext = lib.ffi.new("unsigned char[]", len(padded)) - - if lib.crypto_box_open_afternm( - plaintext, padded, len(padded), nonce, k) != 0: - raise CryptoError("An error occurred trying to decrypt the message") - - return lib.ffi.buffer(plaintext, len(padded))[crypto_box_ZEROBYTES:] diff --git a/lib/nacl/c/crypto_hash.py b/lib/nacl/c/crypto_hash.py deleted file mode 100644 index eef9a13f..00000000 --- a/lib/nacl/c/crypto_hash.py +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -from nacl._lib import lib -from nacl.exceptions import CryptoError - - -# crypto_hash_BYTES = lib.crypto_hash_bytes() -crypto_hash_BYTES = lib.crypto_hash_sha512_bytes() -crypto_hash_sha256_BYTES = lib.crypto_hash_sha256_bytes() -crypto_hash_sha512_BYTES = lib.crypto_hash_sha512_bytes() - - -def crypto_hash(message): - """ - Hashes and returns the message ``message``. - - :param message: bytes - :rtype: bytes - """ - digest = lib.ffi.new("unsigned char[]", crypto_hash_BYTES) - if lib.crypto_hash(digest, message, len(message)) != 0: - raise CryptoError("Hashing failed") - return lib.ffi.buffer(digest, crypto_hash_BYTES)[:] - - -def crypto_hash_sha256(message): - """ - Hashes and returns the message ``message``. - - :param message: bytes - :rtype: bytes - """ - digest = lib.ffi.new("unsigned char[]", crypto_hash_sha256_BYTES) - if lib.crypto_hash_sha256(digest, message, len(message)) != 0: - raise CryptoError("Hashing failed") - return lib.ffi.buffer(digest, crypto_hash_sha256_BYTES)[:] - - -def crypto_hash_sha512(message): - """ - Hashes and returns the message ``message``. - - :param message: bytes - :rtype: bytes - """ - digest = lib.ffi.new("unsigned char[]", crypto_hash_sha512_BYTES) - if lib.crypto_hash_sha512(digest, message, len(message)) != 0: - raise CryptoError("Hashing failed") - return lib.ffi.buffer(digest, crypto_hash_sha512_BYTES)[:] diff --git a/lib/nacl/c/crypto_scalarmult.py b/lib/nacl/c/crypto_scalarmult.py deleted file mode 100644 index d3f26273..00000000 --- a/lib/nacl/c/crypto_scalarmult.py +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -from nacl._lib import lib - - -crypto_scalarmult_BYTES = lib.crypto_scalarmult_bytes() -crypto_scalarmult_SCALARBYTES = lib.crypto_scalarmult_scalarbytes() - - -def crypto_scalarmult_base(n): - """ - Computes and returns the scalar product of a standard group element and an - integer ``n``. - - :param n: bytes - :rtype: bytes - """ - q = lib.ffi.new("unsigned char[]", crypto_scalarmult_BYTES) - - rc = lib.crypto_scalarmult_base(q, n) - assert rc == 0 - - return lib.ffi.buffer(q, crypto_scalarmult_SCALARBYTES)[:] - - -def crypto_scalarmult(n, p): - """ - Computes and returns the scalar product of the given group element and an - integer ``n``. - - :param p: bytes - :param n: bytes - :rtype: bytes - """ - q = lib.ffi.new("unsigned char[]", crypto_scalarmult_BYTES) - - rc = lib.crypto_scalarmult(q, n, p) - assert rc == 0 - - return lib.ffi.buffer(q, crypto_scalarmult_SCALARBYTES)[:] diff --git a/lib/nacl/c/crypto_secretbox.py b/lib/nacl/c/crypto_secretbox.py deleted file mode 100644 index d5906f20..00000000 --- a/lib/nacl/c/crypto_secretbox.py +++ /dev/null @@ -1,77 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -from nacl._lib import lib -from nacl.exceptions import CryptoError - - -crypto_secretbox_KEYBYTES = lib.crypto_secretbox_keybytes() -crypto_secretbox_NONCEBYTES = lib.crypto_secretbox_noncebytes() -crypto_secretbox_ZEROBYTES = lib.crypto_secretbox_zerobytes() -crypto_secretbox_BOXZEROBYTES = lib.crypto_secretbox_boxzerobytes() - - -def crypto_secretbox(message, nonce, key): - """ - Encrypts and returns the message ``message`` with the secret ``key`` and - the nonce ``nonce``. - - :param message: bytes - :param nonce: bytes - :param key: bytes - :rtype: bytes - """ - if len(key) != crypto_secretbox_KEYBYTES: - raise ValueError("Invalid key") - - if len(nonce) != crypto_secretbox_NONCEBYTES: - raise ValueError("Invalid nonce") - - padded = b"\x00" * crypto_secretbox_ZEROBYTES + message - ciphertext = lib.ffi.new("unsigned char[]", len(padded)) - - if lib.crypto_secretbox(ciphertext, padded, len(padded), nonce, key) != 0: - raise CryptoError("Encryption failed") - - ciphertext = lib.ffi.buffer(ciphertext, len(padded)) - return ciphertext[crypto_secretbox_BOXZEROBYTES:] - - -def crypto_secretbox_open(ciphertext, nonce, key): - """ - Decrypt and returns the encrypted message ``ciphertext`` with the secret - ``key`` and the nonce ``nonce``. - - :param ciphertext: bytes - :param nonce: bytes - :param key: bytes - :rtype: bytes - """ - if len(key) != crypto_secretbox_KEYBYTES: - raise ValueError("Invalid key") - - if len(nonce) != crypto_secretbox_NONCEBYTES: - raise ValueError("Invalid nonce") - - padded = b"\x00" * crypto_secretbox_BOXZEROBYTES + ciphertext - plaintext = lib.ffi.new("unsigned char[]", len(padded)) - - if lib.crypto_secretbox_open( - plaintext, padded, len(padded), nonce, key) != 0: - raise CryptoError("Decryption failed. Ciphertext failed verification") - - plaintext = lib.ffi.buffer(plaintext, len(padded)) - return plaintext[crypto_secretbox_ZEROBYTES:] diff --git a/lib/nacl/c/crypto_sign.py b/lib/nacl/c/crypto_sign.py deleted file mode 100644 index c05e7379..00000000 --- a/lib/nacl/c/crypto_sign.py +++ /dev/null @@ -1,102 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -from nacl._lib import lib -from nacl.exceptions import BadSignatureError, CryptoError - - -crypto_sign_BYTES = lib.crypto_sign_bytes() -# crypto_sign_SEEDBYTES = lib.crypto_sign_seedbytes() -crypto_sign_SEEDBYTES = lib.crypto_sign_secretkeybytes() // 2 -crypto_sign_PUBLICKEYBYTES = lib.crypto_sign_publickeybytes() -crypto_sign_SECRETKEYBYTES = lib.crypto_sign_secretkeybytes() - - -def crypto_sign_keypair(): - """ - Returns a randomly generated public key and secret key. - - :rtype: (bytes(public_key), bytes(secret_key)) - """ - pk = lib.ffi.new("unsigned char[]", crypto_sign_PUBLICKEYBYTES) - sk = lib.ffi.new("unsigned char[]", crypto_sign_SECRETKEYBYTES) - - if lib.crypto_sign_keypair(pk, sk) != 0: - raise CryptoError("An error occurred while generating keypairs") - - return ( - lib.ffi.buffer(pk, crypto_sign_PUBLICKEYBYTES)[:], - lib.ffi.buffer(sk, crypto_sign_SECRETKEYBYTES)[:], - ) - - -def crypto_sign_seed_keypair(seed): - """ - Computes and returns the public key and secret key using the seed ``seed``. - - :param seed: bytes - :rtype: (bytes(public_key), bytes(secret_key)) - """ - if len(seed) != crypto_sign_SEEDBYTES: - raise ValueError("Invalid seed") - - pk = lib.ffi.new("unsigned char[]", crypto_sign_PUBLICKEYBYTES) - sk = lib.ffi.new("unsigned char[]", crypto_sign_SECRETKEYBYTES) - - if lib.crypto_sign_seed_keypair(pk, sk, seed) != 0: - raise CryptoError("An error occurred while generating keypairs") - - return ( - lib.ffi.buffer(pk, crypto_sign_PUBLICKEYBYTES)[:], - lib.ffi.buffer(sk, crypto_sign_SECRETKEYBYTES)[:], - ) - - -def crypto_sign(message, sk): - """ - Signs the message ``message`` using the secret key ``sk`` and returns the - signed message. - - :param message: bytes - :param sk: bytes - :rtype: bytes - """ - signed = lib.ffi.new("unsigned char[]", len(message) + crypto_sign_BYTES) - signed_len = lib.ffi.new("unsigned long long *") - - if lib.crypto_sign(signed, signed_len, message, len(message), sk) != 0: - raise CryptoError("Failed to sign the message") - - return lib.ffi.buffer(signed, signed_len[0])[:] - - -def crypto_sign_open(signed, pk): - """ - Verifies the signature of the signed message ``signed`` using the public - key ``pkg`` and returns the unsigned message. - - :param signed: bytes - :param pk: bytes - :rtype: bytes - """ - message = lib.ffi.new("unsigned char[]", len(signed)) - message_len = lib.ffi.new("unsigned long long *") - - if lib.crypto_sign_open( - message, message_len, signed, len(signed), pk) != 0: - raise BadSignatureError("Signature was forged or corrupt") - - return lib.ffi.buffer(message, message_len[0])[:] diff --git a/lib/nacl/c/randombytes.py b/lib/nacl/c/randombytes.py deleted file mode 100644 index a28cd881..00000000 --- a/lib/nacl/c/randombytes.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -from nacl._lib import lib - - -def randombytes(size): - """ - Returns ``size`` number of random bytes from a cryptographically secure - random source. - - :param size: int - :rtype: bytes - """ - buf = lib.ffi.new("unsigned char[]", size) - lib.randombytes(buf, size) - return lib.ffi.buffer(buf, size)[:] diff --git a/lib/nacl/c/sodium_core.py b/lib/nacl/c/sodium_core.py deleted file mode 100644 index 51d3a8f6..00000000 --- a/lib/nacl/c/sodium_core.py +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -from __future__ import absolute_import, division, print_function - -from nacl._lib import lib -from nacl.exceptions import CryptoError - - -def sodium_init(): - """ - Initializes sodium, picking the best implementations available for this - machine. - """ - if lib.sodium_init() != 0: - raise CryptoError("Could not initialize sodium") diff --git a/lib/nacl/encoding.py b/lib/nacl/encoding.py deleted file mode 100644 index 8c771ac8..00000000 --- a/lib/nacl/encoding.py +++ /dev/null @@ -1,90 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -import base64 -import binascii - - -class RawEncoder(object): - - @staticmethod - def encode(data): - return data - - @staticmethod - def decode(data): - return data - - -class HexEncoder(object): - - @staticmethod - def encode(data): - return binascii.hexlify(data) - - @staticmethod - def decode(data): - return binascii.unhexlify(data) - - -class Base16Encoder(object): - - @staticmethod - def encode(data): - return base64.b16encode(data) - - @staticmethod - def decode(data): - return base64.b16decode(data) - - -class Base32Encoder(object): - - @staticmethod - def encode(data): - return base64.b32encode(data) - - @staticmethod - def decode(data): - return base64.b32decode(data) - - -class Base64Encoder(object): - - @staticmethod - def encode(data): - return base64.b64encode(data) - - @staticmethod - def decode(data): - return base64.b64decode(data) - - -class URLSafeBase64Encoder(object): - - @staticmethod - def encode(data): - return base64.urlsafe_b64encode(data) - - @staticmethod - def decode(data): - return base64.urlsafe_b64decode(data) - - -class Encodable(object): - - def encode(self, encoder=RawEncoder): - return encoder.encode(bytes(self)) diff --git a/lib/nacl/exceptions.py b/lib/nacl/exceptions.py deleted file mode 100644 index 1ba26da6..00000000 --- a/lib/nacl/exceptions.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - - -class CryptoError(Exception): - """ - Base exception for all nacl related errors - """ - - -class BadSignatureError(CryptoError): - """ - Raised when the signature was forged or otherwise corrupt. - """ diff --git a/lib/nacl/hash.py b/lib/nacl/hash.py deleted file mode 100644 index ff4b711e..00000000 --- a/lib/nacl/hash.py +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -import nacl.c -import nacl.encoding - - -def sha256(message, encoder=nacl.encoding.HexEncoder): - return encoder.encode(nacl.c.crypto_hash_sha256(message)) - - -def sha512(message, encoder=nacl.encoding.HexEncoder): - return encoder.encode(nacl.c.crypto_hash_sha512(message)) diff --git a/lib/nacl/public.py b/lib/nacl/public.py deleted file mode 100644 index f2da61cc..00000000 --- a/lib/nacl/public.py +++ /dev/null @@ -1,130 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -from nacl import encoding -import nacl.c -from nacl.utils import EncryptedMessage, StringFixer, random - - -class PublicKey(encoding.Encodable, StringFixer, object): - - SIZE = nacl.c.crypto_box_PUBLICKEYBYTES - - def __init__(self, public_key, encoder=encoding.RawEncoder): - self._public_key = encoder.decode(public_key) - - if len(self._public_key) != self.SIZE: - raise ValueError("The public key must be exactly %s bytes long" % - self.SIZE) - - def __bytes__(self): - return self._public_key - - -class PrivateKey(encoding.Encodable, StringFixer, object): - - SIZE = nacl.c.crypto_box_SECRETKEYBYTES - - def __init__(self, private_key, encoder=encoding.RawEncoder): - # Decode the secret_key - private_key = encoder.decode(private_key) - - # Verify that our seed is the proper size - if len(private_key) != self.SIZE: - raise ValueError( - "The secret key must be exactly %d bytes long" % self.SIZE) - - raw_public_key = nacl.c.crypto_scalarmult_base(private_key) - - self._private_key = private_key - self.public_key = PublicKey(raw_public_key) - - def __bytes__(self): - return self._private_key - - @classmethod - def generate(cls): - return cls(random(PrivateKey.SIZE), encoder=encoding.RawEncoder) - - -class Box(encoding.Encodable, StringFixer, object): - - NONCE_SIZE = nacl.c.crypto_box_NONCEBYTES - - def __init__(self, private_key, public_key): - if private_key and public_key: - self._shared_key = nacl.c.crypto_box_beforenm( - public_key.encode(encoder=encoding.RawEncoder), - private_key.encode(encoder=encoding.RawEncoder), - ) - else: - self._shared_key = None - - def __bytes__(self): - return self._shared_key - - @classmethod - def decode(cls, encoded, encoder=encoding.RawEncoder): - # Create an empty box - box = cls(None, None) - - # Assign our decoded value to the shared key of the box - box._shared_key = encoder.decode(encoded) - - return box - - def encrypt(self, plaintext, nonce, encoder=encoding.RawEncoder): - - if len(nonce) != self.NONCE_SIZE: - raise ValueError("The nonce must be exactly %s bytes long" % - self.NONCE_SIZE) - - ciphertext = nacl.c.crypto_box_afternm( - plaintext, - nonce, - self._shared_key, - ) - - encoded_nonce = encoder.encode(nonce) - encoded_ciphertext = encoder.encode(ciphertext) - - return EncryptedMessage._from_parts( - encoded_nonce, - encoded_ciphertext, - encoder.encode(nonce + ciphertext), - ) - - def decrypt(self, ciphertext, nonce=None, encoder=encoding.RawEncoder): - - # Decode our ciphertext - ciphertext = encoder.decode(ciphertext) - - if nonce is None: - # If we were given the nonce and ciphertext combined, split them. - nonce = ciphertext[:self.NONCE_SIZE] - ciphertext = ciphertext[self.NONCE_SIZE:] - - if len(nonce) != self.NONCE_SIZE: - raise ValueError("The nonce must be exactly %s bytes long" % - self.NONCE_SIZE) - - plaintext = nacl.c.crypto_box_open_afternm( - ciphertext, - nonce, - self._shared_key, - ) - - return plaintext diff --git a/lib/nacl/secret.py b/lib/nacl/secret.py deleted file mode 100644 index a8a7c029..00000000 --- a/lib/nacl/secret.py +++ /dev/null @@ -1,76 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -from nacl import encoding -import nacl.c -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 - - def __init__(self, key, encoder=encoding.RawEncoder): - key = encoder.decode(key) - - if len(key) != self.KEY_SIZE: - raise ValueError( - "The key must be exactly %s bytes long" % - self.KEY_SIZE, - ) - - self._key = key - - def __bytes__(self): - return self._key - - def encrypt(self, plaintext, nonce, encoder=encoding.RawEncoder): - - if len(nonce) != self.NONCE_SIZE: - raise ValueError( - "The nonce must be exactly %s bytes long" % self.NONCE_SIZE, - ) - - ciphertext = nacl.c.crypto_secretbox(plaintext, nonce, self._key) - - encoded_nonce = encoder.encode(nonce) - encoded_ciphertext = encoder.encode(ciphertext) - - return EncryptedMessage._from_parts( - encoded_nonce, - encoded_ciphertext, - encoder.encode(nonce + ciphertext), - ) - - def decrypt(self, ciphertext, nonce=None, encoder=encoding.RawEncoder): - - # Decode our ciphertext - ciphertext = encoder.decode(ciphertext) - - if nonce is None: - # If we were given the nonce and ciphertext combined, split them. - nonce = ciphertext[:self.NONCE_SIZE] - ciphertext = ciphertext[self.NONCE_SIZE:] - - if len(nonce) != self.NONCE_SIZE: - raise ValueError( - "The nonce must be exactly %s bytes long" % self.NONCE_SIZE, - ) - - plaintext = nacl.c.crypto_secretbox_open(ciphertext, nonce, self._key) - - return plaintext diff --git a/lib/nacl/signing.py b/lib/nacl/signing.py deleted file mode 100644 index 76ce0a4c..00000000 --- a/lib/nacl/signing.py +++ /dev/null @@ -1,166 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -import six - -from nacl import encoding -import nacl.c -from nacl.utils import StringFixer, random - - -class SignedMessage(six.binary_type): - """ - A bytes subclass that holds a messaged that has been signed by a - :class:`SigningKey`. - """ - - @classmethod - def _from_parts(cls, signature, message, combined): - obj = cls(combined) - obj._signature = signature - obj._message = message - return obj - - @property - def signature(self): - """ - The signature contained within the :class:`SignedMessage`. - """ - return self._signature - - @property - def message(self): - """ - The message contained within the :class:`SignedMessage`. - """ - return self._message - - -class VerifyKey(encoding.Encodable, StringFixer, object): - """ - The public key counterpart to an Ed25519 SigningKey for producing digital - signatures. - - :param key: [:class:`bytes`] Serialized Ed25519 public key - :param encoder: A class that is able to decode the `key` - """ - - def __init__(self, key, encoder=encoding.RawEncoder): - # Decode the key - key = encoder.decode(key) - - if len(key) != nacl.c.crypto_sign_PUBLICKEYBYTES: - raise ValueError( - "The key must be exactly %s bytes long" % - nacl.c.crypto_sign_PUBLICKEYBYTES, - ) - - self._key = key - - def __bytes__(self): - return self._key - - def verify(self, smessage, signature=None, encoder=encoding.RawEncoder): - """ - Verifies the signature of a signed message, returning the message - if it has not been tampered with else raising - :class:`~nacl.signing.BadSignatureError`. - - :param smessage: [:class:`bytes`] Either the original messaged or a - signature and message concated together. - :param signature: [:class:`bytes`] If an unsigned message is given for - smessage then the detached signature must be provded. - :param encoder: A class that is able to decode the secret message and - signature. - :rtype: :class:`bytes` - """ - if signature is not None: - # If we were given the message and signature separately, combine - # them. - smessage = signature + smessage - - # Decode the signed message - smessage = encoder.decode(smessage) - - return nacl.c.crypto_sign_open(smessage, self._key) - - -class SigningKey(encoding.Encodable, StringFixer, object): - """ - Private key for producing digital signatures using the Ed25519 algorithm. - - Signing keys are produced from a 32-byte (256-bit) random seed value. This - value can be passed into the :class:`~nacl.signing.SigningKey` as a - :func:`bytes` whose length is 32. - - .. warning:: This **must** be protected and remain secret. Anyone who knows - the value of your :class:`~nacl.signing.SigningKey` or it's seed can - masquerade as you. - - :param seed: [:class:`bytes`] Random 32-byte value (i.e. private key) - :param encoder: A class that is able to decode the seed - - :ivar: verify_key: [:class:`~nacl.signing.VerifyKey`] The verify - (i.e. public) key that corresponds with this signing key. - """ - - def __init__(self, seed, encoder=encoding.RawEncoder): - # Decode the seed - seed = encoder.decode(seed) - - # Verify that our seed is the proper size - if len(seed) != nacl.c.crypto_sign_SEEDBYTES: - raise ValueError( - "The seed must be exactly %d bytes long" % - nacl.c.crypto_sign_SEEDBYTES - ) - - public_key, secret_key = nacl.c.crypto_sign_seed_keypair(seed) - - self._seed = seed - self._signing_key = secret_key - self.verify_key = VerifyKey(public_key) - - def __bytes__(self): - return self._seed - - @classmethod - def generate(cls): - """ - Generates a random :class:`~nacl.signing.SingingKey` object. - - :rtype: :class:`~nacl.signing.SigningKey` - """ - return cls( - random(nacl.c.crypto_sign_SEEDBYTES), - encoder=encoding.RawEncoder, - ) - - def sign(self, message, encoder=encoding.RawEncoder): - """ - Sign a message using this key. - - :param message: [:class:`bytes`] The data to be signed. - :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) - - signature = encoder.encode(raw_signed[:nacl.c.crypto_sign_BYTES]) - message = encoder.encode(raw_signed[nacl.c.crypto_sign_BYTES:]) - signed = encoder.encode(raw_signed) - - return SignedMessage._from_parts(signature, message, signed) diff --git a/lib/nacl/utils.py b/lib/nacl/utils.py deleted file mode 100644 index b4b7ca92..00000000 --- a/lib/nacl/utils.py +++ /dev/null @@ -1,50 +0,0 @@ -# Copyright 2013 Donald Stufft and individual contributors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import, division, print_function - -import six - -import nacl.c - - -class EncryptedMessage(six.binary_type): - - @classmethod - def _from_parts(cls, nonce, ciphertext, combined): - obj = cls(combined) - obj._nonce = nonce - obj._ciphertext = ciphertext - return obj - - @property - def nonce(self): - return self._nonce - - @property - def ciphertext(self): - return self._ciphertext - - -class StringFixer(object): - - def __str__(self): - if six.PY3: - return self.__unicode__() - else: - return self.__bytes__() - - -def random(size=32): - return nacl.c.randombytes(size) diff --git a/lib/ucoinpy/documents/__init__.py b/lib/ucoinpy/documents/__init__.py index 14a7f83d..dcd251f8 100644 --- a/lib/ucoinpy/documents/__init__.py +++ b/lib/ucoinpy/documents/__init__.py @@ -6,8 +6,6 @@ Created on 3 déc. 2014 import base58 import re from ..key import Base58Encoder -from nacl.encoding import Base64Encoder - class Document: re_version = re.compile("Version: ([0-9]+)\n") diff --git a/lib/ucoinpy/key/__init__.py b/lib/ucoinpy/key/__init__.py index 51cb7221..45f8e8b6 100644 --- a/lib/ucoinpy/key/__init__.py +++ b/lib/ucoinpy/key/__init__.py @@ -6,9 +6,8 @@ Ucoin public and private keys import base58 import base64 -import scrypt -from nacl.signing import SigningKey as NaclSigningKey -from nacl.encoding import Base64Encoder +from .scrypt import hash +from libnacl.sign import Signer as NaclSigningKey SEED_LENGTH = 32 # Length of the key @@ -21,12 +20,12 @@ SCRYPT_PARAMS = {'N': 4096, class SigningKey(NaclSigningKey): def __init__(self, salt, password): - seed = scrypt.hash(password, salt, + seed = hash(password, salt, SCRYPT_PARAMS['N'], SCRYPT_PARAMS['r'], SCRYPT_PARAMS['p'], SEED_LENGTH) super().__init__(seed) - self.pubkey = self.verify_key.encode(encoder=Base58Encoder) + self.pubkey = Base58Encoder.encoder(self.vk) class Base58Encoder(object): diff --git a/setup.py b/setup.py index bb9a463e..bc06def1 100644 --- a/setup.py +++ b/setup.py @@ -12,9 +12,9 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'lib'))) sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'src'))) print(sys.path) -includes = ["sip", "re", "json", "logging", "hashlib", "os", "urllib", "ucoinpy", "requests"] +includes = ["sip", "re", "json", "logging", "hashlib", "os", "urllib", "ucoinpy", "requests", "cutecoin.core"] excludes = [] -packages = ["nacl", "nacl.c", "scrypt"] +packages = ["libnacl"] includefiles = [] options = {"path": sys.path, diff --git a/src/cutecoin/core/__init__.py b/src/cutecoin/core/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/cutecoin/core/app.py b/src/cutecoin/core/app.py index 396aa7ab..7c9caddb 100644 --- a/src/cutecoin/core/app.py +++ b/src/cutecoin/core/app.py @@ -8,7 +8,6 @@ import os import logging import json import tarfile -import gnupg from cutecoin.core import config from cutecoin.tools.exceptions import NameAlreadyExists, BadAccountFile diff --git a/src/cutecoin/core/config.py b/src/cutecoin/core/config.py index 7365757c..1d56c4f4 100644 --- a/src/cutecoin/core/config.py +++ b/src/cutecoin/core/config.py @@ -7,7 +7,6 @@ Created on 7 févr. 2014 import logging from optparse import OptionParser from os import environ, path -import gnupg if "XDG_CONFIG_HOME" in environ: diff --git a/src/cutecoin/core/wallet.py b/src/cutecoin/core/wallet.py index 129e0730..605200b4 100644 --- a/src/cutecoin/core/wallet.py +++ b/src/cutecoin/core/wallet.py @@ -6,7 +6,6 @@ Created on 1 févr. 2014 from ucoinpy import PROTOCOL_VERSION from ucoinpy.api import bma -from nacl.encoding import Base64Encoder from ucoinpy.documents.transaction import InputSource, OutputSource, Transaction from ucoinpy.key import SigningKey from ..tools.exceptions import NotEnoughMoneyError @@ -118,7 +117,7 @@ class Wallet(object): key = SigningKey("{0}{1}".format(salt, self.walletid), password) logging.debug("Sender pubkey:{0}".format(key.pubkey)) - signing = key.sign(bytes(tx.raw(), 'ascii'), Base64Encoder) + signing = key.signature(bytes(tx.raw(), 'ascii')) logging.debug("Signature : {0}".format(str(signing.signature))) tx.signatures = [str(signing.signature, 'ascii')] logging.debug("Transaction : {0}".format(tx.signed_raw())) -- GitLab