pysodium-0.7.0-0/0000755000175000017500000000000013235077010012250 5ustar ss00000000000000pysodium-0.7.0-0/setup.cfg0000644000175000017500000000004613235077010014071 0ustar ss00000000000000[egg_info] tag_build = tag_date = 0 pysodium-0.7.0-0/pysodium/0000755000175000017500000000000013235077010014121 5ustar ss00000000000000pysodium-0.7.0-0/pysodium/__init__.py0000755000175000017500000016332213226634022016246 0ustar ss00000000000000#!/usr/bin/env python2 """ Wrapper for libsodium library Copyright (c) 2013-2014, Marsiske Stefan. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import ctypes import ctypes.util sodium = ctypes.cdll.LoadLibrary(ctypes.util.find_library('sodium') or ctypes.util.find_library('libsodium')) if not sodium._name: raise ValueError('Unable to find libsodium') sodium.sodium_version_string.restype = ctypes.c_char_p try: sodium_major = int(sodium.sodium_version_string().decode('utf8').split('.')[0]) sodium_minor = int(sodium.sodium_version_string().decode('utf8').split('.')[1]) sodium_patch = int(sodium.sodium_version_string().decode('utf8').split('.')[2]) except (IndexError, ValueError): raise ValueError('Unable to parse version string from libsodium') def sodium_version_check(major, minor, patch): """Check if the current libsodium version is greater or equal to the supplied one """ if major > sodium_major: return False if major == sodium_major and minor > sodium_minor: return False if major == sodium_major and minor == sodium_minor and patch > sodium_patch: return False return True def sodium_version(major, minor, patch): def decorator(func): def wrapper(*args, **kwargs): if sodium_version_check(major, minor, patch) == False: raise ValueError('Unavailable in this libsodium version') return func(*args, **kwargs) return wrapper return decorator def encode_strings(func): """ This decorator forces the encoding of str function parameters to UTF-8 to elliminate the differences between Python 3.x and Python 2.x. The only caveat is that bytes and str are both str types in Python 2.x so it is possible for the encode() function to fail. It is OK for us to accept that failure, hence the pass in the except block. Use this decorator on any functions that can take strings as parameters such as crypto_pwhash(). """ def wrapper(*args, **kwargs): largs = [] for arg in args: if isinstance(arg, str): try: arg = arg.encode(encoding='utf-8') except: pass largs.append(arg) for k in kwargs.keys(): if isinstance(kwargs[k], str): try: kwargs[k] = kwargs[k].encode(encoding='utf-8') except: pass return func(*largs, **kwargs) return wrapper sodium.crypto_pwhash_scryptsalsa208sha256_strprefix.restype = ctypes.c_char_p crypto_auth_KEYBYTES = sodium.crypto_auth_keybytes() crypto_auth_BYTES = sodium.crypto_auth_bytes() crypto_box_NONCEBYTES = sodium.crypto_box_noncebytes() crypto_box_BEFORENMBYTES = sodium.crypto_box_beforenmbytes() crypto_box_PUBLICKEYBYTES = sodium.crypto_box_publickeybytes() crypto_box_SECRETKEYBYTES = sodium.crypto_box_secretkeybytes() crypto_box_ZEROBYTES = sodium.crypto_box_zerobytes() crypto_box_BOXZEROBYTES = sodium.crypto_box_boxzerobytes() crypto_box_MACBYTES = sodium.crypto_box_macbytes() crypto_box_SEALBYTES = sodium.crypto_box_sealbytes() crypto_box_SEEDBYTES = sodium.crypto_box_seedbytes() crypto_secretbox_KEYBYTES = sodium.crypto_secretbox_keybytes() crypto_secretbox_NONCEBYTES = sodium.crypto_secretbox_noncebytes() crypto_secretbox_ZEROBYTES = sodium.crypto_secretbox_zerobytes() crypto_secretbox_BOXZEROBYTES = sodium.crypto_secretbox_boxzerobytes() crypto_secretbox_MACBYTES = sodium.crypto_secretbox_macbytes() crypto_sign_PUBLICKEYBYTES = sodium.crypto_sign_publickeybytes() crypto_sign_SECRETKEYBYTES = sodium.crypto_sign_secretkeybytes() crypto_sign_SEEDBYTES = sodium.crypto_sign_seedbytes() crypto_sign_BYTES = sodium.crypto_sign_bytes() crypto_sign_ed25519_SECRETKEYBYTES = sodium.crypto_sign_ed25519_secretkeybytes() crypto_sign_ed25519_PUBLICKEYBYTES = sodium.crypto_sign_ed25519_publickeybytes() crypto_stream_KEYBYTES = sodium.crypto_stream_keybytes() crypto_stream_NONCEBYTES = sodium.crypto_stream_noncebytes() crypto_stream_chacha20_NONCEBYTES = sodium.crypto_stream_chacha20_noncebytes() crypto_stream_chacha20_KEYBYTES = sodium.crypto_stream_chacha20_keybytes() crypto_generichash_KEYBYTES_MAX = sodium.crypto_generichash_keybytes_max() crypto_generichash_BYTES = sodium.crypto_generichash_bytes() crypto_generichash_BYTES_MIN = sodium.crypto_generichash_bytes_min() crypto_generichash_BYTES_MAX = sodium.crypto_generichash_bytes_max() crypto_generichash_STATEBYTES = sodium.crypto_generichash_statebytes() crypto_scalarmult_curve25519_BYTES = sodium.crypto_scalarmult_curve25519_bytes() crypto_scalarmult_BYTES = sodium.crypto_scalarmult_bytes() crypto_scalarmult_SCALARBYTES = sodium.crypto_scalarmult_curve25519_scalarbytes() crypto_generichash_blake2b_KEYBYTES_MAX = sodium.crypto_generichash_blake2b_keybytes_max() crypto_generichash_blake2b_BYTES = sodium.crypto_generichash_blake2b_bytes() crypto_generichash_blake2b_BYTES_MIN = sodium.crypto_generichash_blake2b_bytes_min() crypto_generichash_blake2b_BYTES_MAX = sodium.crypto_generichash_blake2b_bytes_max() crypto_generichash_blake2b_SALTBYTES = sodium.crypto_generichash_blake2b_saltbytes() crypto_generichash_blake2b_PERSONALBYTES = sodium.crypto_generichash_blake2b_personalbytes() crypto_pwhash_scryptsalsa208sha256_SALTBYTES = sodium.crypto_pwhash_scryptsalsa208sha256_saltbytes() crypto_pwhash_scryptsalsa208sha256_STRBYTES = sodium.crypto_pwhash_scryptsalsa208sha256_strbytes() crypto_pwhash_scryptsalsa208sha256_STRPREFIX = sodium.crypto_pwhash_scryptsalsa208sha256_strprefix() crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE = sodium.crypto_pwhash_scryptsalsa208sha256_opslimit_interactive() crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE = sodium.crypto_pwhash_scryptsalsa208sha256_memlimit_interactive() crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE = sodium.crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive() crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE = sodium.crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive() crypto_hash_sha256_BYTES = sodium.crypto_hash_sha256_bytes() crypto_hash_sha512_BYTES = sodium.crypto_hash_sha512_bytes() crypto_aead_chacha20poly1305_KEYBYTES = sodium.crypto_aead_chacha20poly1305_keybytes() crypto_aead_chacha20poly1305_NPUBBYTES = sodium.crypto_aead_chacha20poly1305_npubbytes() crypto_aead_chacha20poly1305_NONCEBYTES = crypto_aead_chacha20poly1305_NPUBBYTES crypto_aead_chacha20poly1305_ABYTES = sodium.crypto_aead_chacha20poly1305_abytes() if sodium_version_check(1, 0, 9): crypto_aead_chacha20poly1305_ietf_KEYBYTES = sodium.crypto_aead_chacha20poly1305_ietf_keybytes() crypto_aead_chacha20poly1305_ietf_NPUBBYTES = sodium.crypto_aead_chacha20poly1305_ietf_npubbytes() crypto_pwhash_SALTBYTES = sodium.crypto_pwhash_saltbytes() crypto_pwhash_STRBYTES = sodium.crypto_pwhash_strbytes() sodium.crypto_pwhash_bytes_max.restype=ctypes.c_uint sodium.crypto_pwhash_opslimit_max.restype=ctypes.c_uint sodium.crypto_pwhash_memlimit_max.restype=ctypes.c_uint sodium.crypto_pwhash_passwd_max.restype=ctypes.c_uint crypto_pwhash_BYTES_MAX = sodium.crypto_pwhash_bytes_max() crypto_pwhash_BYTES_MIN = sodium.crypto_pwhash_bytes_min() crypto_pwhash_MEMLIMIT_MAX = sodium.crypto_pwhash_memlimit_max() crypto_pwhash_MEMLIMIT_MIN = sodium.crypto_pwhash_memlimit_min() crypto_pwhash_OPSLIMIT_MAX = sodium.crypto_pwhash_opslimit_max() crypto_pwhash_OPSLIMIT_MIN = sodium.crypto_pwhash_opslimit_min() crypto_pwhash_PASSWD_MAX = sodium.crypto_pwhash_passwd_max() crypto_pwhash_PASSWD_MIN = sodium.crypto_pwhash_passwd_min() crypto_pwhash_OPSLIMIT_INTERACTIVE = sodium.crypto_pwhash_opslimit_interactive() crypto_pwhash_MEMLIMIT_INTERACTIVE = sodium.crypto_pwhash_memlimit_interactive() crypto_pwhash_OPSLIMIT_MODERATE = sodium.crypto_pwhash_opslimit_moderate() crypto_pwhash_MEMLIMIT_MODERATE = sodium.crypto_pwhash_memlimit_moderate() crypto_pwhash_OPSLIMIT_SENSITIVE = sodium.crypto_pwhash_opslimit_sensitive() crypto_pwhash_MEMLIMIT_SENSITIVE = sodium.crypto_pwhash_memlimit_sensitive() crypto_pwhash_ALG_DEFAULT = sodium.crypto_pwhash_alg_default() crypto_pwhash_ALG_ARGON2I13 = sodium.crypto_pwhash_alg_argon2i13() crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE = sodium.crypto_pwhash_argon2i_opslimit_interactive() crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE = sodium.crypto_pwhash_argon2i_memlimit_interactive() crypto_pwhash_argon2i_OPSLIMIT_MODERATE = sodium.crypto_pwhash_argon2i_opslimit_moderate() crypto_pwhash_argon2i_MEMLIMIT_MODERATE = sodium.crypto_pwhash_argon2i_memlimit_moderate() crypto_pwhash_argon2i_OPSLIMIT_SENSITIVE = sodium.crypto_pwhash_argon2i_opslimit_sensitive() crypto_pwhash_argon2i_MEMLIMIT_SENSITIVE = sodium.crypto_pwhash_argon2i_memlimit_sensitive() else: crypto_pwhash_ALG_DEFAULT = None crypto_aead_chacha20poly1305_ietf_KEYBYTES = 32 crypto_aead_chacha20poly1305_ietf_NPUBBYTES = 12 crypto_aead_chacha20poly1305_ietf_NONCEBYTES = crypto_aead_chacha20poly1305_ietf_NPUBBYTES if sodium_version_check(1, 0, 12): crypto_kx_PUBLICKEYBYTES = sodium.crypto_kx_publickeybytes() crypto_kx_SECRETKEYBYTES = sodium.crypto_kx_secretkeybytes() crypto_kx_SESSIONKEYBYTES = sodium.crypto_kx_sessionkeybytes() crypto_aead_xchacha20poly1305_ietf_KEYBYTES = sodium.crypto_aead_xchacha20poly1305_ietf_keybytes() crypto_aead_xchacha20poly1305_ietf_NPUBBYTES = sodium.crypto_aead_xchacha20poly1305_ietf_npubbytes() crypto_aead_xchacha20poly1305_ietf_NONCEBYTES = crypto_aead_xchacha20poly1305_ietf_NPUBBYTES crypto_aead_xchacha20poly1305_ietf_ABYTES = sodium.crypto_aead_xchacha20poly1305_ietf_abytes() sodium.crypto_pwhash_scryptsalsa208sha256_bytes_max.restype=ctypes.c_uint sodium.crypto_pwhash_scryptsalsa208sha256_opslimit_max.restype=ctypes.c_uint sodium.crypto_pwhash_scryptsalsa208sha256_memlimit_max.restype=ctypes.c_ulonglong sodium.crypto_pwhash_scryptsalsa208sha256_passwd_max.restype=ctypes.c_uint crypto_pwhash_scryptsalsa208sha256_BYTES_MAX = sodium.crypto_pwhash_scryptsalsa208sha256_bytes_max() crypto_pwhash_scryptsalsa208sha256_BYTES_MIN = sodium.crypto_pwhash_scryptsalsa208sha256_bytes_min() crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX = sodium.crypto_pwhash_scryptsalsa208sha256_memlimit_max() crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN = sodium.crypto_pwhash_scryptsalsa208sha256_memlimit_min() crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX = sodium.crypto_pwhash_scryptsalsa208sha256_opslimit_max() crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN = sodium.crypto_pwhash_scryptsalsa208sha256_opslimit_min() crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX = sodium.crypto_pwhash_scryptsalsa208sha256_passwd_max() crypto_pwhash_scryptsalsa208sha256_PASSWD_MIN = sodium.crypto_pwhash_scryptsalsa208sha256_passwd_min() else: crypto_pwhash_scryptsalsa208sha256_BYTES_MIN = 16 crypto_pwhash_scryptsalsa208sha256_BYTES_MAX = 4294967264 crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX = 68719476736 crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN = 16777216 crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN = 32768 crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX =4294967295 crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX = 4294967295 crypto_pwhash_scryptsalsa208sha256_PASSWD_MIN = 0 if sodium_version_check(1, 0, 13): crypto_pwhash_ALG_ARGON2ID13 = sodium.crypto_pwhash_alg_argon2id13() crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE = sodium.crypto_pwhash_argon2id_opslimit_interactive() crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE = sodium.crypto_pwhash_argon2id_memlimit_interactive() crypto_pwhash_argon2id_OPSLIMIT_MODERATE = sodium.crypto_pwhash_argon2id_opslimit_moderate() crypto_pwhash_argon2id_MEMLIMIT_MODERATE = sodium.crypto_pwhash_argon2id_memlimit_moderate() crypto_pwhash_argon2id_OPSLIMIT_SENSITIVE = sodium.crypto_pwhash_argon2id_opslimit_sensitive() crypto_pwhash_argon2id_MEMLIMIT_SENSITIVE = sodium.crypto_pwhash_argon2id_memlimit_sensitive() if sodium_version_check(1, 0, 15): crypto_secretstream_xchacha20poly1305_STATEBYTES = sodium.crypto_secretstream_xchacha20poly1305_statebytes() crypto_secretstream_xchacha20poly1305_ABYTES = sodium.crypto_secretstream_xchacha20poly1305_abytes() crypto_secretstream_xchacha20poly1305_HEADERBYTES = sodium.crypto_secretstream_xchacha20poly1305_headerbytes() crypto_secretstream_xchacha20poly1305_KEYBYTES = sodium.crypto_secretstream_xchacha20poly1305_keybytes() crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX = sodium.crypto_secretstream_xchacha20poly1305_messagebytes_max() crypto_secretstream_xchacha20poly1305_TAG_MESSAGE = sodium.crypto_secretstream_xchacha20poly1305_tag_message() crypto_secretstream_xchacha20poly1305_TAG_PUSH = sodium.crypto_secretstream_xchacha20poly1305_tag_push() crypto_secretstream_xchacha20poly1305_TAG_REKEY = sodium.crypto_secretstream_xchacha20poly1305_tag_rekey() crypto_secretstream_xchacha20poly1305_TAG_FINAL = sodium.crypto_secretstream_xchacha20poly1305_tag_final() sodium_init = sodium.sodium_init class CryptoSignState(ctypes.Structure): _pack_ = 1 _fields_ = [ ('state', ctypes.c_uint64 * 8), ('count', ctypes.c_uint64 * 2), ('buf', ctypes.c_uint8 * 128) ] def __check(code): if code != 0: raise ValueError def pad_buf(buf, length, name = 'buf'): buflen = len(buf) if buflen > length: raise ValueError("Cannot pad %s (len: %d - expected %d or less)" % (name, buflen, length)) padding = length - buflen if padding > 0: return buf + b"\x00"*padding else: return buf def crypto_scalarmult_curve25519(n, p): if None in (n,p): raise ValueError("invalid parameters") if len(n) != crypto_scalarmult_SCALARBYTES: raise ValueError("truncated scalar") if len(p) != crypto_scalarmult_BYTES: raise ValueError("truncated point") buf = ctypes.create_string_buffer(crypto_scalarmult_BYTES) __check(sodium.crypto_scalarmult_curve25519(buf, n, p)) return buf.raw def crypto_scalarmult_curve25519_base(n): if n is None: raise ValueError("invalid parameters") if len(n) != crypto_scalarmult_SCALARBYTES: raise ValueError("truncated scalar") buf = ctypes.create_string_buffer(crypto_scalarmult_BYTES) __check(sodium.crypto_scalarmult_curve25519_base(ctypes.byref(buf), n)) return buf.raw # crypto_stream_chacha20_xor(unsigned char *c, const unsigned char *m, unsigned long long mlen, const unsigned char *n, const unsigned char *k) def crypto_stream_chacha20_xor(message, nonce, key): if len(nonce) != crypto_stream_chacha20_NONCEBYTES: raise ValueError("truncated nonce") if len(key) != crypto_stream_chacha20_KEYBYTES: raise ValueError("truncated key") mlen = ctypes.c_longlong(len(message)) c = ctypes.create_string_buffer(len(message)) __check(sodium.crypto_stream_chacha20_xor(c, message, mlen, nonce, key)) return c.raw # crypto_aead_chacha20poly1305_encrypt(unsigned char *c, unsigned long long *clen, const unsigned char *m, unsigned long long mlen, const unsigned char *ad, unsigned long long adlen, const unsigned char *nsec, const unsigned char *npub, const unsigned char *k); def crypto_aead_chacha20poly1305_encrypt(message, ad, nonce, key): if len(nonce) != crypto_aead_chacha20poly1305_NONCEBYTES: raise ValueError("truncated nonce") if len(key) != crypto_aead_chacha20poly1305_KEYBYTES: raise ValueError("truncated key") mlen = ctypes.c_ulonglong(len(message)) adlen = ctypes.c_ulonglong(len(ad)) if ad is not None else ctypes.c_ulonglong(0) c = ctypes.create_string_buffer(mlen.value + 16) clen = ctypes.c_ulonglong(0) __check(sodium.crypto_aead_chacha20poly1305_encrypt(c, ctypes.byref(clen), message, mlen, ad, adlen, None, nonce, key)) return c.raw # crypto_aead_chacha20poly1305_decrypt(unsigned char *m, unsigned long long *mlen, unsigned char *nsec, const unsigned char *c, unsigned long long clen, const unsigned char *ad, unsigned long long adlen, const unsigned char *npub, const unsigned char *k) def crypto_aead_chacha20poly1305_decrypt(ciphertext, ad, nonce, key): if len(nonce) != crypto_aead_chacha20poly1305_NONCEBYTES: raise ValueError("truncated nonce") if len(key) != crypto_aead_chacha20poly1305_KEYBYTES: raise ValueError("truncated key") m = ctypes.create_string_buffer(len(ciphertext) - 16) mlen = ctypes.c_ulonglong(0) clen = ctypes.c_ulonglong(len(ciphertext)) adlen = ctypes.c_ulonglong(len(ad)) if ad is not None else ctypes.c_ulonglong(0) __check(sodium.crypto_aead_chacha20poly1305_decrypt(m, ctypes.byref(mlen), None, ciphertext, clen, ad, adlen, nonce, key)) return m.raw # crypto_aead_chacha20poly1305_encrypt_detached(unsigned char *c, unsigned char *mac, unsigned long long *maclen_p, const unsigned char *m, unsigned long long mlen, const unsigned char *ad, unsigned long long adlen, const unsigned char *nsec, const unsigned char *npub, const unsigned char *k) @sodium_version(1, 0, 9) def crypto_aead_chacha20poly1305_encrypt_detached(message, ad, nonce, key): """ Return ciphertext, mac tag """ if len(nonce) != crypto_aead_chacha20poly1305_NONCEBYTES: raise ValueError("truncated nonce") if len(key) != crypto_aead_chacha20poly1305_KEYBYTES: raise ValueError("truncated key") mlen = ctypes.c_ulonglong(len(message)) adlen = ctypes.c_ulonglong(len(ad)) if ad is not None else ctypes.c_ulonglong(0) c = ctypes.create_string_buffer(mlen.value) maclen_p = ctypes.c_ulonglong(crypto_aead_chacha20poly1305_ABYTES) mac = ctypes.create_string_buffer(maclen_p.value) __check(sodium.crypto_aead_chacha20poly1305_encrypt_detached(c, mac, ctypes.byref(maclen_p), message, mlen, ad, adlen, None, nonce, key)) return c.raw, mac.raw # crypto_aead_chacha20poly1305_decrypt_detached(unsigned char *m, unsigned char *nsec, const unsigned char *c, unsigned long long clen, const unsigned char *mac, const unsigned char *ad, unsigned long long adlen, const unsigned char *npub, const unsigned char *k) @sodium_version(1, 0, 9) def crypto_aead_chacha20poly1305_decrypt_detached(ciphertext, mac, ad, nonce, key): """ Return message if successful or -1 (ValueError) if not successful""" if len(nonce) != crypto_aead_chacha20poly1305_NONCEBYTES: raise ValueError("truncated nonce") if len(key) != crypto_aead_chacha20poly1305_KEYBYTES: raise ValueError("truncated key") if len(mac) != crypto_aead_chacha20poly1305_ABYTES: raise ValueError("mac length != %i" % crypto_aead_chacha20poly1305_ABYTES) clen = ctypes.c_ulonglong(len(ciphertext)) m = ctypes.create_string_buffer(clen.value) adlen = ctypes.c_ulonglong(len(ad)) if ad is not None else ctypes.c_ulonglong(0) __check(sodium.crypto_aead_chacha20poly1305_decrypt_detached(m, None, ciphertext, clen, mac, ad, adlen, nonce, key)) return m.raw # crypto_aead_chacha20poly1305_ietf_encrypt(unsigned char *c, unsigned long long *clen_p, const unsigned char *m, unsigned long long mlen, const unsigned char *ad, unsigned long long adlen, const unsigned char *nsec, const unsigned char *npub, const unsigned char *k) @sodium_version(1, 0, 4) def crypto_aead_chacha20poly1305_ietf_encrypt(message, ad, nonce, key): if len(nonce) != crypto_aead_chacha20poly1305_ietf_NONCEBYTES: raise ValueError("truncated nonce") if len(key) != crypto_aead_chacha20poly1305_ietf_KEYBYTES: raise ValueError("truncated key") mlen = ctypes.c_ulonglong(len(message)) adlen = ctypes.c_ulonglong(len(ad)) if ad is not None else ctypes.c_ulonglong(0) c = ctypes.create_string_buffer(mlen.value + 16) clen = ctypes.c_ulonglong(0) __check(sodium.crypto_aead_chacha20poly1305_ietf_encrypt(c, ctypes.byref(clen), message, mlen, ad, adlen, None, nonce, key)) return c.raw # crypto_aead_chacha20poly1305_ietf_decrypt(unsigned char *m, unsigned long long *mlen, unsigned char *nsec, const unsigned char *c, unsigned long long clen, const unsigned char *ad, unsigned long long adlen, const unsigned char *npub, const unsigned char *k) @sodium_version(1, 0, 4) def crypto_aead_chacha20poly1305_ietf_decrypt(ciphertext, ad, nonce, key): if len(nonce) != crypto_aead_chacha20poly1305_ietf_NONCEBYTES: raise ValueError("truncated nonce") if len(key) != crypto_aead_chacha20poly1305_ietf_KEYBYTES: raise ValueError("truncated key") m = ctypes.create_string_buffer(len(ciphertext) - 16) mlen = ctypes.c_ulonglong(0) clen = ctypes.c_ulonglong(len(ciphertext)) adlen = ctypes.c_ulonglong(len(ad)) if ad is not None else ctypes.c_ulonglong(0) __check(sodium.crypto_aead_chacha20poly1305_ietf_decrypt(m, ctypes.byref(mlen), None, ciphertext, clen, ad, adlen, nonce, key)) return m.raw #crypto_aead_xchacha20poly1305_ietf_encrypt(ciphertext, &ciphertext_len, # message, message_len, # additional_data, additional_data_len, # null, nonce, key); @sodium_version(1, 0, 12) def crypto_aead_xchacha20poly1305_ietf_encrypt(message, ad, nonce, key): if len(nonce) != crypto_aead_xchacha20poly1305_ietf_NPUBBYTES: raise ValueError("truncated nonce") if len(key) != crypto_aead_xchacha20poly1305_ietf_KEYBYTES: raise ValueError("truncated key") mlen = ctypes.c_ulonglong(len(message)) adlen = ctypes.c_ulonglong(len(ad)) if ad is not None else ctypes.c_ulonglong(0) c = ctypes.create_string_buffer(mlen.value + 16) clen = ctypes.c_ulonglong(0) __check(sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(c, ctypes.byref(clen), message, mlen, ad, adlen, None, nonce, key)) return c.raw #crypto_aead_xchacha20poly1305_ietf_decrypt(decrypted, &decrypted_len, # null, # ciphertext, ciphertext_len, # additional_data, additional_data_len, # nonce, key); @sodium_version(1, 0, 12) def crypto_aead_xchacha20poly1305_ietf_decrypt(ciphertext, ad, nonce, key): if len(nonce) != crypto_aead_xchacha20poly1305_ietf_NPUBBYTES: raise ValueError("truncated nonce") if len(key) != crypto_aead_xchacha20poly1305_ietf_KEYBYTES: raise ValueError("truncated key") m = ctypes.create_string_buffer(len(ciphertext) - 16) mlen = ctypes.c_ulonglong(0) clen = ctypes.c_ulonglong(len(ciphertext)) adlen = ctypes.c_ulonglong(len(ad)) if ad is not None else ctypes.c_ulonglong(0) __check(sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(m, ctypes.byref(mlen), None, ciphertext, clen, ad, adlen, nonce, key)) return m.raw # crypto_auth(unsigned char *out, const unsigned char *in, unsigned long long inlen, const unsigned char *k) def crypto_auth(m, k=b''): if m is None: raise ValueError("invalid parameters") buf = ctypes.create_string_buffer(crypto_auth_BYTES) __check(sodium.crypto_auth(buf, m, ctypes.c_ulonglong(len(m)), k)) return buf.raw # crypto_auth_verify(const unsigned char *h, const unsigned char *in, unsigned long long inlen, const unsigned char *k) def crypto_auth_verify(h, m, k=b''): if h is None or m is None: raise ValueError("invalid parameters") if len(h) != crypto_auth_BYTES: raise ValueError("invalid tag") __check(sodium.crypto_auth_verify(h, m, ctypes.c_ulonglong(len(m)), k)) # crypto_generichash(unsigned char *out, size_t outlen, const unsigned char *in, unsigned long long inlen, const unsigned char *key, size_t keylen) def crypto_generichash(m, k=b'', outlen=crypto_generichash_BYTES): buf = ctypes.create_string_buffer(outlen) __check(sodium.crypto_generichash(buf, ctypes.c_size_t(outlen), m, ctypes.c_ulonglong(len(m)), k, ctypes.c_size_t(len(k)))) return buf.raw # crypto_generichash_init(crypto_generichash_state *state, const unsigned char *key, const size_t keylen, const size_t outlen); def crypto_generichash_init(outlen=crypto_generichash_BYTES, k=b''): state = ctypes.create_string_buffer(crypto_generichash_STATEBYTES) __check(sodium.crypto_generichash_init(ctypes.byref(state), k, ctypes.c_size_t(len(k)), ctypes.c_size_t(outlen))) return state # crypto_generichash_update(crypto_generichash_state *state, const unsigned char *in, unsigned long long inlen); def crypto_generichash_update(state, m): if len(state) != crypto_generichash_STATEBYTES: raise ValueError("invalid state") __check(sodium.crypto_generichash_update(ctypes.byref(state), m, ctypes.c_ulonglong(len(m)))) return state # crypto_generichash_final(crypto_generichash_state *state, unsigned char *out, const size_t outlen); def crypto_generichash_final(state, outlen=crypto_generichash_BYTES): if len(state) != crypto_generichash_STATEBYTES: raise ValueError("invalid state") buf = ctypes.create_string_buffer(outlen) __check(sodium.crypto_generichash_final(ctypes.byref(state), buf, ctypes.c_size_t(outlen))) return buf.raw def crypto_generichash_blake2b_salt_personal(message, outlen = crypto_generichash_blake2b_BYTES, key = b'', salt = b'', personal = b''): keylen = len(key) if keylen != 0 and not crypto_generichash_blake2b_BYTES_MIN <= keylen <= crypto_generichash_blake2b_KEYBYTES_MAX: raise ValueError("%d <= len(key) <= %d - %d received" % (crypto_generichash_blake2b_BYTES_MIN, crypto_generichash_blake2b_KEYBYTES_MAX, keylen)) salt = pad_buf(salt, crypto_generichash_blake2b_SALTBYTES, 'salt') personal = pad_buf(personal, crypto_generichash_blake2b_PERSONALBYTES, 'personal') buf = ctypes.create_string_buffer(outlen) outlen = ctypes.c_size_t(outlen) inlen = ctypes.c_ulonglong(len(message)) keylen = ctypes.c_size_t(keylen) __check(sodium.crypto_generichash_blake2b_salt_personal(buf, outlen, message, inlen, key, keylen, salt, personal)) return buf.raw def randombytes(size): buf = ctypes.create_string_buffer(size) sodium.randombytes(buf, ctypes.c_ulonglong(size)) return buf.raw def crypto_box_keypair(): pk = ctypes.create_string_buffer(crypto_box_PUBLICKEYBYTES) sk = ctypes.create_string_buffer(crypto_box_SECRETKEYBYTES) __check(sodium.crypto_box_keypair(pk, sk)) return pk.raw, sk.raw # int crypto_box_seed_keypair(unsigned char *pk, unsigned char *sk, # const unsigned char *seed); def crypto_box_seed_keypair(seed): if seed is None: raise ValueError("invalid parameters") if len(seed) != crypto_box_SEEDBYTES: raise ValueError("invalid seed size") pk = ctypes.create_string_buffer(crypto_box_PUBLICKEYBYTES) sk = ctypes.create_string_buffer(crypto_box_SECRETKEYBYTES) __check(sodium.crypto_box_seed_keypair(pk, sk, seed)) return pk.raw, sk.raw def crypto_box_beforenm(pk, sk): if pk is None or sk is None: raise ValueError("invalid parameters") if len(pk) != crypto_box_PUBLICKEYBYTES: raise ValueError("pk incorrect size") if len(sk) != crypto_box_SECRETKEYBYTES: raise ValueError("sk incorrect size") c = ctypes.create_string_buffer(crypto_secretbox_KEYBYTES) __check(sodium.crypto_box_beforenm(c, pk, sk)) return c.raw def crypto_box(msg, nonce, pk, sk): if None in (msg, nonce, pk, sk): raise ValueError("invalid parameters") if len(pk) != crypto_box_PUBLICKEYBYTES: raise ValueError("pk incorrect size") if len(sk) != crypto_box_SECRETKEYBYTES: raise ValueError("sk incorrect size") if len(nonce) != crypto_box_NONCEBYTES: raise ValueError("nonce incorrect size") c = ctypes.create_string_buffer(crypto_box_MACBYTES + len(msg)) __check(sodium.crypto_box_easy(c, msg, ctypes.c_ulonglong(len(msg)), nonce, pk, sk)) return c.raw def crypto_box_afternm(msg, nonce, k): if None in (msg, nonce, k): raise ValueError("invalid parameters") if len(k) != crypto_box_BEFORENMBYTES: raise ValueError("k incorrect size") if len(nonce) != crypto_box_NONCEBYTES: raise ValueError("nonce incorrect size") c = ctypes.create_string_buffer(crypto_box_MACBYTES + len(msg)) __check(sodium.crypto_box_easy_afternm(c, msg, ctypes.c_ulonglong(len(msg)), nonce, k)) return c.raw def crypto_box_open(c, nonce, pk, sk): if None in (c, nonce, pk, sk): raise ValueError("invalid parameters") if len(pk) != crypto_box_PUBLICKEYBYTES: raise ValueError("pk incorrect size") if len(sk) != crypto_box_SECRETKEYBYTES: raise ValueError("sk incorrect size") if len(nonce) != crypto_box_NONCEBYTES: raise ValueError("nonce incorrect size") msg = ctypes.create_string_buffer(len(c) - crypto_box_MACBYTES) __check(sodium.crypto_box_open_easy(msg, c, ctypes.c_ulonglong(len(c)), nonce, pk, sk)) return msg.raw def crypto_box_open_afternm(c, nonce, k): if None in (c, nonce, k): raise ValueError("invalid parameters") if len(k) != crypto_box_BEFORENMBYTES: raise ValueError("k incorrect size") if len(nonce) != crypto_box_NONCEBYTES: raise ValueError("nonce incorrect size") msg = ctypes.create_string_buffer(len(c) - crypto_box_MACBYTES) __check(sodium.crypto_box_open_easy_afternm(msg, c, ctypes.c_ulonglong(len(c)), nonce, k)) return msg.raw def crypto_secretbox(msg, nonce, k): if None in (msg, nonce, k): raise ValueError("invalid parameters") if len(k) != crypto_secretbox_KEYBYTES: raise ValueError("k incorrect size") if len(nonce) != crypto_box_NONCEBYTES: raise ValueError("nonce incorrect size") padded = b"\x00" * crypto_secretbox_ZEROBYTES + msg c = ctypes.create_string_buffer(len(padded)) __check(sodium.crypto_secretbox(c, padded, ctypes.c_ulonglong(len(padded)), nonce, k)) return c.raw[crypto_secretbox_BOXZEROBYTES:] def crypto_secretbox_open(c, nonce, k): if None in (c, nonce, k): raise ValueError("invalid parameters") if len(k) != crypto_secretbox_KEYBYTES: raise ValueError("k incorrect size") if len(nonce) != crypto_box_NONCEBYTES: raise ValueError("nonce incorrect size") padded = b"\x00" * crypto_secretbox_BOXZEROBYTES + c msg = ctypes.create_string_buffer(len(padded)) __check(sodium.crypto_secretbox_open(msg, padded, ctypes.c_ulonglong(len(padded)), nonce, k)) return msg.raw[crypto_secretbox_ZEROBYTES:] # int crypto_box_seal(unsigned char *c, const unsigned char *m, # unsigned long long mlen, const unsigned char *pk); @sodium_version(1, 0, 3) def crypto_box_seal(msg, k): if msg is None or k is None: raise ValueError("invalid parameters") if len(k) != crypto_box_PUBLICKEYBYTES: raise ValueError("k incorrect size") c = ctypes.create_string_buffer(len(msg)+crypto_box_SEALBYTES) __check(sodium.crypto_box_seal(c, msg, ctypes.c_ulonglong(len(msg)), k)) return c.raw # int crypto_box_seal_open(unsigned char *m, const unsigned char *c, # unsigned long long clen, # const unsigned char *pk, const unsigned char *sk); @sodium_version(1, 0, 3) def crypto_box_seal_open(c, pk, sk): if None in (c, pk, sk): raise ValueError("invalid parameters") if len(pk) != crypto_box_PUBLICKEYBYTES: raise ValueError("pk incorrect size") if len(sk) != crypto_box_SECRETKEYBYTES: raise ValueError("sk incorrect size") msg = ctypes.create_string_buffer(len(c)-crypto_box_SEALBYTES) __check(sodium.crypto_box_seal_open(msg, c, ctypes.c_ulonglong(len(c)), pk, sk)) return msg.raw # int crypto_box_detached(unsigned char *c, unsigned char *mac, # const unsigned char *m, unsigned long long mlen, # const unsigned char *n, const unsigned char *pk, # const unsigned char *sk); def crypto_box_detached(msg, nonce, pk, sk): if None in (msg, nonce, pk, sk): raise ValueError("invalid parameters") if len(pk) != crypto_box_PUBLICKEYBYTES: raise ValueError("pk incorrect size") if len(sk) != crypto_box_SECRETKEYBYTES: raise ValueError("sk incorrect size") if len(nonce) != crypto_box_NONCEBYTES: raise ValueError("nonce incorrect size") c = ctypes.create_string_buffer(len(msg)) mac = ctypes.create_string_buffer(crypto_box_MACBYTES) __check(sodium.crypto_box_detached(c, mac, msg, ctypes.c_ulonglong(len(msg)), nonce, pk, sk)) return c.raw, mac.raw # int crypto_box_open_detached(unsigned char *m, const unsigned char *c, # const unsigned char *mac, # unsigned long long clen, # const unsigned char *n, # const unsigned char *pk, # const unsigned char *sk); def crypto_box_open_detached(c, mac, nonce, pk, sk): if None in (c, mac, nonce, pk, sk): raise ValueError("invalid parameters") if len(pk) != crypto_box_PUBLICKEYBYTES: raise ValueError("pk incorrect size") if len(sk) != crypto_box_SECRETKEYBYTES: raise ValueError("sk incorrect size") if len(nonce) != crypto_box_NONCEBYTES: raise ValueError("nonce incorrect size") msg = ctypes.create_string_buffer(len(c)) __check(sodium.crypto_box_open_detached(msg, c, mac, ctypes.c_ulonglong(len(c)), nonce, pk, sk)) return msg.raw # void crypto_secretstream_xchacha20poly1305_keygen (unsigned char k[crypto_secretstream_xchacha20poly1305_KEYBYTES]) @sodium_version(1, 0, 15) def crypto_secretstream_xchacha20poly1305_keygen(): key = ctypes.create_string_buffer(crypto_secretstream_xchacha20poly1305_KEYBYTES) sodium.crypto_secretstream_xchacha20poly1305_keygen(ctypes.byref(key)) return key.raw # int crypto_secretstream_xchacha20poly1305_init_push(crypto_secretstream_xchacha20poly1305_state *state, # unsigned char out[crypto_secretstream_xchacha20poly1305_HEADERBYTES], # const unsigned char k[crypto_secretstream_xchacha20poly1305_KEYBYTES]) @sodium_version(1, 0, 15) def crypto_secretstream_xchacha20poly1305_init_push(key): if key == None: raise ValueError("invalid parameters") if not (len(key) == crypto_secretstream_xchacha20poly1305_KEYBYTES): raise ValueError("Truncated key") state = ctypes.create_string_buffer(crypto_secretstream_xchacha20poly1305_STATEBYTES) header = ctypes.create_string_buffer(crypto_secretstream_xchacha20poly1305_HEADERBYTES) __check(sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header, key)) return state.raw, header.raw # int crypto_secretstream_xchacha20poly1305_init_pull(crypto_secretstream_xchacha20poly1305_state *state, # const unsigned char in[crypto_secretstream_xchacha20poly1305_HEADERBYTES], # const unsigned char k[crypto_secretstream_xchacha20poly1305_KEYBYTES]) @sodium_version(1, 0, 15) def crypto_secretstream_xchacha20poly1305_init_pull(header, key): if None in (header, key): raise ValueError("invalid parameters") if not (len(header) == crypto_secretstream_xchacha20poly1305_HEADERBYTES): raise ValueError("Truncated header") if not (len(key) == crypto_secretstream_xchacha20poly1305_KEYBYTES): raise ValueError("Truncated key") state = ctypes.create_string_buffer(crypto_secretstream_xchacha20poly1305_STATEBYTES) __check(sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header, key)) return state.raw #void crypto_secretstream_xchacha20poly1305_rekey (crypto_secretstream_xchacha20poly1305_state *state) @sodium_version(1, 0, 15) def crypto_secretstream_xchacha20poly1305_rekey(state): if state == None: raise ValueError("invalid parameters") if not (len(state) == crypto_secretstream_xchacha20poly1305_STATEBYTES): raise ValueError("Truncated state") sodium.crypto_secretstream_xchacha20poly1305_rekey(state) #int crypto_secretstream_xchacha20poly1305_push (crypto_secretstream_xchacha20poly1305_state *state, # unsigned char *out, # unsigned long long *outlen_p, # const unsigned char *m, # unsigned long long mlen, # const unsigned char *ad, # unsigned long long adlen, # unsigned char tag) @sodium_version(1, 0, 15) def crypto_secretstream_xchacha20poly1305_push(state, message, ad, tag): if None in (state, message): raise ValueError("invalid parameters") if not (len(state) == crypto_secretstream_xchacha20poly1305_STATEBYTES): raise ValueError("Truncated state") mlen = ctypes.c_ulonglong(len(message)) adlen = ctypes.c_ulonglong(len(ad)) if ad is not None else ctypes.c_ulonglong(0) c = ctypes.create_string_buffer(mlen.value + crypto_secretstream_xchacha20poly1305_ABYTES) clen = ctypes.c_ulonglong(0) __check(sodium.crypto_secretstream_xchacha20poly1305_push( state, # crypto_secretstream_xchacha20poly1305_state *state, c, # unsigned char *out ctypes.byref(clen), # unsigned long long *outlen_p, message, # const unsigned char *m, mlen, # unsigned long long mlen, ad, # const unsigned char *ad, adlen, # unsigned long long adlen, tag)) # unsigned char tag) return c.raw #crypto_secretstream_xchacha20poly1305_pull (crypto_secretstream_xchacha20poly1305_state *state, # unsigned char *m, # unsigned long long *mlen_p, # unsigned char *tag_p, # const unsigned char *in, # unsigned long long inlen, # const unsigned char *ad, # unsigned long long adlen) @sodium_version(1, 0, 15) def crypto_secretstream_xchacha20poly1305_pull(state, ciphertext, ad): if None in (state, ciphertext): raise ValueError("invalid parameters") if not (len(state) == crypto_secretstream_xchacha20poly1305_STATEBYTES): raise ValueError("Truncated state") if len(ciphertext) < crypto_secretstream_xchacha20poly1305_ABYTES: raise ValueError("truncated cyphertext") m = ctypes.create_string_buffer(len(ciphertext) - crypto_secretstream_xchacha20poly1305_ABYTES) mlen = ctypes.c_ulonglong(0) tag = ctypes.c_ubyte(0) clen = ctypes.c_ulonglong(len(ciphertext)) adlen = ctypes.c_ulonglong(len(ad)) if ad is not None else ctypes.c_ulonglong(0) __check(sodium.crypto_secretstream_xchacha20poly1305_pull( state, m, # char *m, ctypes.byref(mlen), # long long *mlen_p, ctypes.byref(tag), # char *tag_p, ciphertext, # unsigned char *in, clen, # long long inlen, ad, # unsigned char *ad, adlen # long long adlen) )) return m.raw, tag.value def crypto_sign_keypair(): pk = ctypes.create_string_buffer(crypto_sign_PUBLICKEYBYTES) sk = ctypes.create_string_buffer(crypto_sign_SECRETKEYBYTES) __check(sodium.crypto_sign_keypair(pk, sk)) return pk.raw, sk.raw def crypto_sign_seed_keypair(seed): if len(seed) != crypto_sign_SEEDBYTES: raise ValueError("invalid seed size") pk = ctypes.create_string_buffer(crypto_sign_PUBLICKEYBYTES) sk = ctypes.create_string_buffer(crypto_sign_SECRETKEYBYTES) __check(sodium.crypto_sign_seed_keypair(pk, sk, seed)) return pk.raw, sk.raw def crypto_sign(m, sk): if m is None or sk is None: raise ValueError("invalid parameters") if not (len(sk) == crypto_sign_SECRETKEYBYTES): raise ValueError('Truncated secret key') smsg = ctypes.create_string_buffer(len(m) + crypto_sign_BYTES) smsglen = ctypes.c_ulonglong() __check(sodium.crypto_sign(smsg, ctypes.byref(smsglen), m, ctypes.c_ulonglong(len(m)), sk)) return smsg.raw def crypto_sign_detached(m, sk): if m is None or sk is None: raise ValueError("invalid parameters") if not (len(sk) == crypto_sign_SECRETKEYBYTES): raise ValueError('Truncated secret key') sig = ctypes.create_string_buffer(crypto_sign_BYTES) # second parm is for output of signature len (optional, ignored if NULL) __check(sodium.crypto_sign_detached(sig, ctypes.c_void_p(0), m, ctypes.c_ulonglong(len(m)), sk)) return sig.raw def crypto_sign_open(sm, pk): if sm is None or pk is None: raise ValueError("invalid parameters") if not (len(pk) == crypto_sign_PUBLICKEYBYTES): raise ValueError('Truncated public key') msg = ctypes.create_string_buffer(len(sm)) msglen = ctypes.c_ulonglong() __check(sodium.crypto_sign_open(msg, ctypes.byref(msglen), sm, ctypes.c_ulonglong(len(sm)), pk)) return msg.raw[:msglen.value] def crypto_sign_verify_detached(sig, msg, pk): if None in (sig, msg, pk): raise ValueError if len(sig) != crypto_sign_BYTES: raise ValueError("invalid sign") if not (len(pk) == crypto_sign_PUBLICKEYBYTES): raise ValueError('Truncated public key') __check(sodium.crypto_sign_verify_detached(sig, msg, ctypes.c_ulonglong(len(msg)), pk)) # crypto_sign_init(crypto_sign_state *state); @sodium_version(1, 0, 12) def crypto_sign_init(): state = CryptoSignState() __check(sodium.crypto_sign_init(ctypes.byref(state))) return state # crypto_sign_update(crypto_sign_state *state, const unsigned char *m, unsigned long long mlen); @sodium_version(1, 0, 12) def crypto_sign_update(state, m): if(not isinstance(state, CryptoSignState)): raise TypeError("state is not CryptoSignState") if m is None: raise ValueError("invalid parameters") __check(sodium.crypto_sign_update(ctypes.byref(state), m, ctypes.c_ulonglong(len(m)))) # crypto_sign_final_create(crypto_sign_state *state, unsigned char *sig, unsigned long long *siglen_p, const unsigned char *sk); @sodium_version(1, 0, 12) def crypto_sign_final_create(state, sk): if(not isinstance(state, CryptoSignState)): raise TypeError("state is not CryptoSignState") if sk is None: raise ValueError("invalid parameters") if len(sk) != crypto_sign_SECRETKEYBYTES: raise ValueError("invalid secret key") buf = ctypes.create_string_buffer(crypto_sign_BYTES) __check(sodium.crypto_sign_final_create(ctypes.byref(state), buf, ctypes.c_void_p(0), sk)) return buf.raw # crypto_sign_final_verify(crypto_sign_state *state, unsigned char *sig, const unsigned char *sk); @sodium_version(1, 0, 12) def crypto_sign_final_verify(state, sig, pk): if(not isinstance(state, CryptoSignState)): raise TypeError("state is not CryptoSignState") if None in (sig, pk): raise ValueError("invalid parameters") if len(sig) != crypto_sign_BYTES: raise ValueError("invalid signature") if len(pk) != crypto_sign_PUBLICKEYBYTES: raise ValueError("invalid public key") __check(sodium.crypto_sign_final_verify(ctypes.byref(state), sig, pk)) # int crypto_stream_salsa20(unsigned char *c, unsigned long long clen, # const unsigned char *n, const unsigned char *k); def crypto_stream(cnt, nonce=None, key=None): res = ctypes.create_string_buffer(cnt) if not nonce: nonce = randombytes(crypto_stream_NONCEBYTES) if not key: key = randombytes(crypto_stream_KEYBYTES) __check(sodium.crypto_stream(res, ctypes.c_ulonglong(cnt), nonce, key)) return res.raw # crypto_stream_salsa20_xor(unsigned char *c, const unsigned char *m, unsigned long long mlen, # const unsigned char *n, const unsigned char *k) def crypto_stream_xor(msg, cnt, nonce, key): res = ctypes.create_string_buffer(cnt) if len(nonce) != crypto_stream_NONCEBYTES: raise ValueError("invalid nonce") if len(key) != crypto_stream_KEYBYTES: raise ValueError("invalid key") __check(sodium.crypto_stream_xor(res, msg, ctypes.c_ulonglong(cnt), nonce, key)) return res.raw def crypto_sign_pk_to_box_pk(pk): if pk is None: raise ValueError if not (len(pk) == crypto_sign_PUBLICKEYBYTES): raise ValueError('Truncated public key') res = ctypes.create_string_buffer(crypto_box_PUBLICKEYBYTES) __check(sodium.crypto_sign_ed25519_pk_to_curve25519(ctypes.byref(res), pk)) return res.raw def crypto_sign_sk_to_box_sk(sk): if sk is None: raise ValueError if not (len(sk) == crypto_sign_SECRETKEYBYTES): raise ValueError('Truncated secret key') res = ctypes.create_string_buffer(crypto_box_SECRETKEYBYTES) __check(sodium.crypto_sign_ed25519_sk_to_curve25519(ctypes.byref(res), sk)) return res.raw def crypto_sign_sk_to_seed(sk): if sk is None: raise ValueError if not (len(sk) == crypto_sign_SECRETKEYBYTES): raise ValueError('Truncated secret key') seed = ctypes.create_string_buffer(crypto_sign_SEEDBYTES) __check(sodium.crypto_sign_ed25519_sk_to_seed(ctypes.byref(seed), sk)) return seed.raw # int crypto_pwhash(unsigned char * const out, # unsigned long long outlen, # const char * const passwd, # unsigned long long passwdlen, # const unsigned char * const salt, # unsigned long long opslimit, # size_t memlimit, int alg); @sodium_version(1, 0, 9) @encode_strings def crypto_pwhash(outlen, passwd, salt, opslimit, memlimit, alg=crypto_pwhash_ALG_DEFAULT): if None in (outlen, passwd, salt, opslimit, memlimit): raise ValueError("invalid parameters") if len(salt) != crypto_pwhash_SALTBYTES: raise ValueError("invalid salt") if not (crypto_pwhash_BYTES_MIN <= outlen <= crypto_pwhash_BYTES_MAX): raise ValueError("invalid hash len") if not (crypto_pwhash_PASSWD_MIN <= len(passwd) <= crypto_pwhash_PASSWD_MAX): raise ValueError("invalid passwd len") if not (crypto_pwhash_OPSLIMIT_MIN <= opslimit <= crypto_pwhash_OPSLIMIT_MAX): raise ValueError("invalid opslimit") if not (crypto_pwhash_MEMLIMIT_MIN <= memlimit <= crypto_pwhash_MEMLIMIT_MAX): raise ValueError("invalid memlimit") out = ctypes.create_string_buffer(outlen) __check(sodium.crypto_pwhash(ctypes.byref(out), ctypes.c_ulonglong(outlen), passwd, ctypes.c_ulonglong(len(passwd)), salt, ctypes.c_ulonglong(opslimit), ctypes.c_size_t(memlimit), ctypes.c_int(alg))) return out.raw # int crypto_pwhash_str(char out[crypto_pwhash_STRBYTES], # const char * const passwd, # unsigned long long passwdlen, # unsigned long long opslimit, # size_t memlimit); @sodium_version(1, 0, 9) @encode_strings def crypto_pwhash_str(passwd, opslimit, memlimit): if None in (passwd, opslimit, memlimit): raise ValueError("invalid parameters") if not (crypto_pwhash_PASSWD_MIN <= len(passwd) <= crypto_pwhash_PASSWD_MAX): raise ValueError("invalid passwd len") if not (crypto_pwhash_OPSLIMIT_MIN <= opslimit <= crypto_pwhash_OPSLIMIT_MAX): raise ValueError("invalid opslimit") if not (crypto_pwhash_MEMLIMIT_MIN <= memlimit <= crypto_pwhash_MEMLIMIT_MAX): raise ValueError("invalid memlimit") out = ctypes.create_string_buffer(crypto_pwhash_STRBYTES) __check(sodium.crypto_pwhash_str(ctypes.byref(out), passwd, ctypes.c_ulonglong(len(passwd)), ctypes.c_ulonglong(opslimit), ctypes.c_size_t(memlimit))) return out.raw # int crypto_pwhash_str_verify(const char str[crypto_pwhash_STRBYTES], # const char * const passwd, # unsigned long long passwdlen); @sodium_version(1, 0, 9) @encode_strings def crypto_pwhash_str_verify(pstr, passwd): if None in (pstr, passwd) or len(pstr) != crypto_pwhash_STRBYTES: raise ValueError("invalid parameters") if not (crypto_pwhash_PASSWD_MIN < len(passwd) <= crypto_pwhash_PASSWD_MAX): raise ValueError("invalid passwd len") return sodium.crypto_pwhash_str_verify(pstr, passwd, ctypes.c_ulonglong(len(passwd))) == 0 # int crypto_pwhash_scryptsalsa208sha256(unsigned char * const out, # unsigned long long outlen, # const char * const passwd, # unsigned long long passwdlen, # const unsigned char * const salt, # unsigned long long opslimit, # size_t memlimit); def crypto_pwhash_scryptsalsa208sha256(outlen, passwd, salt, opslimit, memlimit): if None in (outlen, passwd, salt, opslimit, memlimit): raise ValueError if len(salt) != crypto_pwhash_scryptsalsa208sha256_SALTBYTES: raise ValueError("invalid salt") if not (crypto_pwhash_scryptsalsa208sha256_BYTES_MIN <= outlen <= crypto_pwhash_scryptsalsa208sha256_BYTES_MAX): raise ValueError("invalid hash len") if not (crypto_pwhash_scryptsalsa208sha256_PASSWD_MIN <= len(passwd) <= crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX): raise ValueError("invalid passwd len") if not (crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN <= opslimit <= crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX): raise ValueError("invalid opslimit") if not (crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN <= memlimit <= crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX): raise ValueError("invalid memlimit") out = ctypes.create_string_buffer(outlen) __check(sodium.crypto_pwhash_scryptsalsa208sha256(out, ctypes.c_ulonglong(outlen), passwd, ctypes.c_ulonglong(len(passwd)), salt, ctypes.c_ulonglong(opslimit), ctypes.c_size_t(memlimit))) return out.raw # int crypto_pwhash_scryptsalsa208sha256_str(char out[crypto_pwhash_scryptsalsa208sha256_STRBYTES], # const char * const passwd, # unsigned long long passwdlen, # unsigned long long opslimit, # size_t memlimit); def crypto_pwhash_scryptsalsa208sha256_str(passwd, opslimit, memlimit): if None in (passwd, opslimit, memlimit): raise ValueError if not (crypto_pwhash_scryptsalsa208sha256_PASSWD_MIN <= len(passwd) <= crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX): raise ValueError("invalid passwd len") if not (crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN <= opslimit <= crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX): raise ValueError("invalid opslimit") if not (crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN <= memlimit <= crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX): raise ValueError("invalid memlimit") out = ctypes.create_string_buffer(crypto_pwhash_scryptsalsa208sha256_STRBYTES) __check(sodium.crypto_pwhash_scryptsalsa208sha256_str(out, passwd, ctypes.c_ulonglong(len(passwd)), ctypes.c_ulonglong(opslimit), ctypes.c_size_t(memlimit))) return out.raw #int crypto_pwhash_scryptsalsa208sha256_str_verify(const char str[crypto_pwhash_scryptsalsa208sha256_STRBYTES], # const char * const passwd, # unsigned long long passwdlen); def crypto_pwhash_scryptsalsa208sha256_str_verify(stored, passwd): if stored is None or passwd is None: raise ValueError if not (crypto_pwhash_scryptsalsa208sha256_PASSWD_MIN <= len(passwd) <= crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX): raise ValueError("invalid passwd len") if len(stored) != crypto_pwhash_scryptsalsa208sha256_STRBYTES: raise ValueError('invalid str size') __check(sodium.crypto_pwhash_scryptsalsa208sha256_str_verify(stored, passwd, ctypes.c_ulonglong(len(passwd)))) # int crypto_sign_ed25519_sk_to_pk(unsigned char *pk, const unsigned char *sk) def crypto_sign_sk_to_pk(sk): if sk is None or len(sk) != crypto_sign_ed25519_SECRETKEYBYTES: raise ValueError res = ctypes.create_string_buffer(crypto_sign_ed25519_PUBLICKEYBYTES) __check(sodium.crypto_sign_ed25519_sk_to_pk(ctypes.byref(res), sk)) return res.raw # int crypto_hash_sha256(unsigned char *out, const unsigned char *in, # unsigned long long inlen); def crypto_hash_sha256(message): if message is None: raise ValueError("invalid parameters") out = ctypes.create_string_buffer(crypto_hash_sha256_BYTES) __check(sodium.crypto_hash_sha256(out, message, ctypes.c_ulonglong(len(message)))) return out.raw # int crypto_hash_sha512(unsigned char *out, const unsigned char *in, # unsigned long long inlen); def crypto_hash_sha512(message): if message is None: raise ValueError("invalid parameters") out = ctypes.create_string_buffer(crypto_hash_sha512_BYTES) __check(sodium.crypto_hash_sha512(out, message, ctypes.c_ulonglong(len(message)))) return out.raw # int crypto_kx_keypair(unsigned char pk[crypto_kx_PUBLICKEYBYTES], # unsigned char sk[crypto_kx_SECRETKEYBYTES]); @sodium_version(1, 0, 12) def crypto_kx_keypair(): pk = ctypes.create_string_buffer(crypto_kx_PUBLICKEYBYTES) sk = ctypes.create_string_buffer(crypto_kx_SECRETKEYBYTES) __check(sodium.crypto_kx_keypair(pk, sk)) return pk.raw, sk.raw # int crypto_kx_client_session_keys(unsigned char rx[crypto_kx_SESSIONKEYBYTES], # unsigned char tx[crypto_kx_SESSIONKEYBYTES], # const unsigned char client_pk[crypto_kx_PUBLICKEYBYTES], # const unsigned char client_sk[crypto_kx_SECRETKEYBYTES], # const unsigned char server_pk[crypto_kx_PUBLICKEYBYTES]); @sodium_version(1, 0, 12) def crypto_kx_client_session_keys(client_pk, client_sk, server_pk): if None in (client_pk, client_sk, server_pk): raise ValueError("invalid parameters") if not (len(client_pk) == crypto_kx_PUBLICKEYBYTES): raise ValueError("Invalid client public key") if not (len(client_sk) == crypto_kx_SECRETKEYBYTES): raise ValueError("Invalid client secret key") if not (len(server_pk) == crypto_kx_PUBLICKEYBYTES): raise ValueError("Invalid server public key") rx = ctypes.create_string_buffer(crypto_kx_SESSIONKEYBYTES) tx = ctypes.create_string_buffer(crypto_kx_SESSIONKEYBYTES) __check(sodium.crypto_kx_client_session_keys(rx, tx, client_pk, client_sk, server_pk)) return rx.raw, tx.raw # int crypto_kx_server_session_keys(unsigned char rx[crypto_kx_SESSIONKEYBYTES], # unsigned char tx[crypto_kx_SESSIONKEYBYTES], # const unsigned char server_pk[crypto_kx_PUBLICKEYBYTES], # const unsigned char server_sk[crypto_kx_SECRETKEYBYTES], # const unsigned char client_pk[crypto_kx_PUBLICKEYBYTES]); @sodium_version(1, 0, 12) def crypto_kx_server_session_keys(server_pk, server_sk, client_pk): if None in (server_pk, server_sk, client_pk): raise ValueError("invalid parameters") if not (len(server_pk) == crypto_kx_PUBLICKEYBYTES): raise ValueError("Invalid server public key") if not (len(server_sk) == crypto_kx_SECRETKEYBYTES): raise ValueError("Invalid server secret key") if not (len(client_pk) == crypto_kx_PUBLICKEYBYTES): raise ValueError("Invalid client public key") rx = ctypes.create_string_buffer(crypto_kx_SESSIONKEYBYTES) tx = ctypes.create_string_buffer(crypto_kx_SESSIONKEYBYTES) __check(sodium.crypto_kx_server_session_keys(rx, tx, server_pk, server_sk, client_pk)) return rx.raw, tx.raw pysodium-0.7.0-0/pysodium.egg-info/0000755000175000017500000000000013235077010015613 5ustar ss00000000000000pysodium-0.7.0-0/pysodium.egg-info/SOURCES.txt0000644000175000017500000000034213235077010017476 0ustar ss00000000000000AUTHORS MANIFEST.in README.md setup.py pysodium/__init__.py pysodium.egg-info/PKG-INFO pysodium.egg-info/SOURCES.txt pysodium.egg-info/dependency_links.txt pysodium.egg-info/top_level.txt test/__init__.py test/test_pysodium.pypysodium-0.7.0-0/pysodium.egg-info/dependency_links.txt0000644000175000017500000000000113235077010021661 0ustar ss00000000000000 pysodium-0.7.0-0/pysodium.egg-info/PKG-INFO0000644000175000017500000002230113235077010016706 0ustar ss00000000000000Metadata-Version: 1.1 Name: pysodium Version: 0.7.0-0 Summary: python libsodium wrapper Home-page: https://github.com/stef/pysodium Author: Stefan Marsiske Author-email: s@ctrlc.hu License: BSD Description: This is a very simple wrapper around libsodium masquerading as nacl. [![Build Status](https://travis-ci.org/stef/pysodium.svg?branch=master)](https://travis-ci.org/stef/pysodium) This wrapper requires a pre-installed libsodium from: https://github.com/jedisct1/libsodium then it provides access to the following functions: ``` crypto_aead_chacha20poly1305_decrypt(ciphertext, ad, nonce, key) crypto_aead_chacha20poly1305_decrypt_detached(ciphertext, mac, ad, nonce, key) crypto_aead_chacha20poly1305_encrypt_detached(message, ad, nonce, key) crypto_aead_chacha20poly1305_encrypt(message, ad, nonce, key) crypto_aead_chacha20poly1305_ietf_decrypt(ciphertext, ad, nonce, key) crypto_aead_chacha20poly1305_ietf_encrypt(message, ad, nonce, key) crypto_aead_xchacha20poly1305_ietf_decrypt(ciphertext, ad, nonce, key) crypto_aead_xchacha20poly1305_ietf_encrypt(message, ad, nonce, key) crypto_auth(message, key) crypto_auth_verify(tag, message, key) crypto_box_afternm(msg, nonce, k) crypto_box_beforenm(pk, sk) crypto_box_detached(msg, nonce, pk, sk) crypto_box_keypair() crypto_box(msg, nonce, pk, sk) crypto_box_open_afternm(c, nonce, k) crypto_box_open(c, nonce, pk, sk) crypto_box_open_detached(c, mac, nonce, pk, sk) crypto_box_seal(msg, pk) crypto_box_seal_open(c, pk, sk) crypto_box_seed_keypair(seed) crypto_generichash_blake2b_salt_personal(message, outlen = crypto_generichash_blake2b_BYTES, key = b'', salt = b'', personal = b'') crypto_generichash_final(state, outlen=crypto_generichash_BYTES) crypto_generichash_init(outlen=crypto_generichash_BYTES, k=b'') crypto_generichash(m, k=b'', outlen=crypto_generichash_BYTES) crypto_generichash_update(state, m) crypto_hash_sha256(message) crypto_hash_sha512(message) crypto_kx_client_session_keys(client_pk, client_sk, server_pk) crypto_kx_keypair() crypto_kx_server_session_keys(server_pk, server_sk, client_pk) crypto_pwhash(outlen, passwd, salt, opslimit, memlimit, alg) crypto_pwhash_scryptsalsa208sha256(outlen, passwd, salt, opslimit, memlimit) crypto_pwhash_scryptsalsa208sha256_str(passwd, opslimit, memlimit) crypto_pwhash_scryptsalsa208sha256_str_verify(stored, passwd) crypto_pwhash_str(passwd, opslimit, memlimit) crypto_pwhash_str_verify(pstr, passwd) crypto_scalarmult_curve25519_base(n) crypto_scalarmult_curve25519(n, p) crypto_secretbox(msg, nonce, k) crypto_secretbox_open(c, nonce, k) crypto_secretstream_xchacha20poly1305_keygen(): crypto_secretstream_xchacha20poly1305_init_push(key): crypto_secretstream_xchacha20poly1305_init_pull(header, key): crypto_secretstream_xchacha20poly1305_rekey(state): crypto_secretstream_xchacha20poly1305_push(state, message, ad, tag): crypto_secretstream_xchacha20poly1305_pull(state, ciphertext, ad): crypto_sign_init() crypto_sign_update(state, m) crypto_sign_final_create(state, sk) crypto_sign_final_verify(state, sig, pk) crypto_sign_detached(m, sk) crypto_sign_keypair() crypto_sign(m, sk) crypto_sign_open(sm, pk) crypto_sign_pk_to_box_pk(pk) crypto_sign_seed_keypair(seed) crypto_sign_sk_to_box_sk(sk) crypto_sign_sk_to_pk(sk) crypto_sign_sk_to_seed(sk) crypto_sign_verify_detached(sig, msg, pk) crypto_stream_chacha20_xor(message, nonce, key) crypto_stream(cnt, nonce=None, key=None) crypto_stream_xor(msg, cnt, nonce=None, key=None) randombytes(size) ``` Constants: ``` crypto_aead_chacha20poly1305_ABYTES crypto_aead_chacha20poly1305_KEYBYTES crypto_aead_chacha20poly1305_NPUBBYTES crypto_aead_chacha20poly1305_ietf_KEYBYTES crypto_aead_chacha20poly1305_ietf_NPUBBYTES crypto_aead_xchacha20poly1305_ietf_KEYBYTES crypto_aead_xchacha20poly1305_ietf_NPUBBYTES crypto_aead_xchacha20poly1305_ietf_ABYTES crypto_auth_BYTES crypto_auth_KEYBYTES crypto_box_BEFORENMBYTES crypto_box_BOXZEROBYTES crypto_box_MACBYTES crypto_box_NONCEBYTES crypto_box_PUBLICKEYBYTES crypto_box_SEALBYTES crypto_box_SECRETKEYBYTES crypto_box_SEEDBYTES crypto_box_ZEROBYTES crypto_generichash_KEYBYTES_MAX crypto_generichash_BYTES crypto_generichash_BYTES_MAX crypto_generichash_BYTES_MIN crypto_generichash_STATEBYTES crypto_generichash_blake2b_BYTES crypto_generichash_blake2b_BYTES_MAX crypto_generichash_blake2b_BYTES_MIN crypto_generichash_blake2b_KEYBYTES_MAX crypto_generichash_blake2b_PERSONALBYTES crypto_generichash_blake2b_SALTBYTES crypto_hash_sha256_BYTES crypto_hash_sha512_BYTES crypto_kx_PUBLICKEYBYTES crypto_kx_SECRETKEYBYTES crypto_kx_SESSIONKEYBYTES crypto_pwhash_ALG_DEFAULT crypto_pwhash_ALG_ARGON2I13 crypto_pwhash_ALG_ARGON2ID13 crypto_pwhash_BYTES_MAX crypto_pwhash_BYTES_MIN crypto_pwhash_MEMLIMIT_MAX crypto_pwhash_MEMLIMIT_MIN crypto_pwhash_MEMLIMIT_INTERACTIVE crypto_pwhash_MEMLIMIT_MODERATE crypto_pwhash_MEMLIMIT_SENSITIVE crypto_pwhash_OPSLIMIT_MAX crypto_pwhash_OPSLIMIT_MIN crypto_pwhash_OPSLIMIT_INTERACTIVE crypto_pwhash_OPSLIMIT_MODERATE crypto_pwhash_OPSLIMIT_SENSITIVE crypto_pwhash_PASSWD_MAX crypto_pwhash_PASSWD_MIN crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE crypto_pwhash_argon2i_MEMLIMIT_MODERATE crypto_pwhash_argon2i_MEMLIMIT_SENSITIVE crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE crypto_pwhash_argon2i_OPSLIMIT_MODERATE crypto_pwhash_argon2i_OPSLIMIT_SENSITIVE crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE crypto_pwhash_argon2id_MEMLIMIT_MODERATE crypto_pwhash_argon2id_MEMLIMIT_SENSITIVE crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE crypto_pwhash_argon2id_OPSLIMIT_MODERATE crypto_pwhash_argon2id_OPSLIMIT_SENSITIVE crypto_pwhash_SALTBYTES crypto_pwhash_STRBYTES crypto_pwhash_scryptsalsa208sha256_BYTES_MAX crypto_pwhash_scryptsalsa208sha256_BYTES_MIN crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX crypto_pwhash_scryptsalsa208sha256_PASSWD_MIN crypto_pwhash_scryptsalsa208sha256_SALTBYTES crypto_pwhash_scryptsalsa208sha256_STRBYTES crypto_pwhash_scryptsalsa208sha256_STRPREFIX crypto_scalarmult_BYTES crypto_scalarmult_SCALARBYTES crypto_scalarmult_curve25519_BYTES crypto_secretbox_BOXZEROBYTES crypto_secretbox_KEYBYTES crypto_secretbox_KEYBYTES crypto_secretbox_MACBYTES crypto_secretbox_NONCEBYTES crypto_secretbox_ZEROBYTES crypto_secretstream_xchacha20poly1305_STATEBYTES crypto_secretstream_xchacha20poly1305_ABYTES crypto_secretstream_xchacha20poly1305_HEADERBYTES crypto_secretstream_xchacha20poly1305_KEYBYTES crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX crypto_secretstream_xchacha20poly1305_TAG_MESSAGE crypto_secretstream_xchacha20poly1305_TAG_PUSH crypto_secretstream_xchacha20poly1305_TAG_REKEY crypto_secretstream_xchacha20poly1305_TAG_FINAL crypto_sign_BYTES crypto_sign_PUBLICKEYBYTES crypto_sign_SECRETKEYBYTES crypto_sign_SEEDBYTES crypto_sign_ed25519_PUBLICKEYBYTES crypto_sign_ed25519_SECRETKEYBYTES crypto_stream_KEYBYTES crypto_stream_NONCEBYTES crypto_stream_chacha20_NONCEBYTES crypto_stream_chacha20_KEYBYTES ``` Note most of the the `*_easy` functions are not implemented as the "non-easy" functions provide already the "easy" interface, which hides the placement of buffers in memory, which makes little sense in python, so this wrapper handles this. Keywords: cryptography API NaCl libsodium Platform: UNKNOWN Classifier: Development Status :: 4 - Beta Classifier: License :: OSI Approved :: BSD License Classifier: Topic :: Security :: Cryptography Classifier: Topic :: Security Requires: libsodium pysodium-0.7.0-0/pysodium.egg-info/top_level.txt0000644000175000017500000000001613235077010020342 0ustar ss00000000000000pysodium test pysodium-0.7.0-0/MANIFEST.in0000644000175000017500000000004212620663534014013 0ustar ss00000000000000include README.md include AUTHORS pysodium-0.7.0-0/AUTHORS0000644000175000017500000000034513134624245013330 0ustar ss00000000000000Stefan Marsiske s@ctrlc.hu Bulat Gaifullin Maximilian Heß J Bell Cale Black Jan Varho Henri Sivonen Wes Young namelessjon Emmanuel Vadot Kevin DeLoach Andreas Bogk Fredrick Brennan iachievedit Tacitus Aedifex venzen Blaz Bregar pysodium-0.7.0-0/README.md0000644000175000017500000001626713225666642013561 0ustar ss00000000000000This is a very simple wrapper around libsodium masquerading as nacl. [![Build Status](https://travis-ci.org/stef/pysodium.svg?branch=master)](https://travis-ci.org/stef/pysodium) This wrapper requires a pre-installed libsodium from: https://github.com/jedisct1/libsodium then it provides access to the following functions: ``` crypto_aead_chacha20poly1305_decrypt(ciphertext, ad, nonce, key) crypto_aead_chacha20poly1305_decrypt_detached(ciphertext, mac, ad, nonce, key) crypto_aead_chacha20poly1305_encrypt_detached(message, ad, nonce, key) crypto_aead_chacha20poly1305_encrypt(message, ad, nonce, key) crypto_aead_chacha20poly1305_ietf_decrypt(ciphertext, ad, nonce, key) crypto_aead_chacha20poly1305_ietf_encrypt(message, ad, nonce, key) crypto_aead_xchacha20poly1305_ietf_decrypt(ciphertext, ad, nonce, key) crypto_aead_xchacha20poly1305_ietf_encrypt(message, ad, nonce, key) crypto_auth(message, key) crypto_auth_verify(tag, message, key) crypto_box_afternm(msg, nonce, k) crypto_box_beforenm(pk, sk) crypto_box_detached(msg, nonce, pk, sk) crypto_box_keypair() crypto_box(msg, nonce, pk, sk) crypto_box_open_afternm(c, nonce, k) crypto_box_open(c, nonce, pk, sk) crypto_box_open_detached(c, mac, nonce, pk, sk) crypto_box_seal(msg, pk) crypto_box_seal_open(c, pk, sk) crypto_box_seed_keypair(seed) crypto_generichash_blake2b_salt_personal(message, outlen = crypto_generichash_blake2b_BYTES, key = b'', salt = b'', personal = b'') crypto_generichash_final(state, outlen=crypto_generichash_BYTES) crypto_generichash_init(outlen=crypto_generichash_BYTES, k=b'') crypto_generichash(m, k=b'', outlen=crypto_generichash_BYTES) crypto_generichash_update(state, m) crypto_hash_sha256(message) crypto_hash_sha512(message) crypto_kx_client_session_keys(client_pk, client_sk, server_pk) crypto_kx_keypair() crypto_kx_server_session_keys(server_pk, server_sk, client_pk) crypto_pwhash(outlen, passwd, salt, opslimit, memlimit, alg) crypto_pwhash_scryptsalsa208sha256(outlen, passwd, salt, opslimit, memlimit) crypto_pwhash_scryptsalsa208sha256_str(passwd, opslimit, memlimit) crypto_pwhash_scryptsalsa208sha256_str_verify(stored, passwd) crypto_pwhash_str(passwd, opslimit, memlimit) crypto_pwhash_str_verify(pstr, passwd) crypto_scalarmult_curve25519_base(n) crypto_scalarmult_curve25519(n, p) crypto_secretbox(msg, nonce, k) crypto_secretbox_open(c, nonce, k) crypto_secretstream_xchacha20poly1305_keygen(): crypto_secretstream_xchacha20poly1305_init_push(key): crypto_secretstream_xchacha20poly1305_init_pull(header, key): crypto_secretstream_xchacha20poly1305_rekey(state): crypto_secretstream_xchacha20poly1305_push(state, message, ad, tag): crypto_secretstream_xchacha20poly1305_pull(state, ciphertext, ad): crypto_sign_init() crypto_sign_update(state, m) crypto_sign_final_create(state, sk) crypto_sign_final_verify(state, sig, pk) crypto_sign_detached(m, sk) crypto_sign_keypair() crypto_sign(m, sk) crypto_sign_open(sm, pk) crypto_sign_pk_to_box_pk(pk) crypto_sign_seed_keypair(seed) crypto_sign_sk_to_box_sk(sk) crypto_sign_sk_to_pk(sk) crypto_sign_sk_to_seed(sk) crypto_sign_verify_detached(sig, msg, pk) crypto_stream_chacha20_xor(message, nonce, key) crypto_stream(cnt, nonce=None, key=None) crypto_stream_xor(msg, cnt, nonce=None, key=None) randombytes(size) ``` Constants: ``` crypto_aead_chacha20poly1305_ABYTES crypto_aead_chacha20poly1305_KEYBYTES crypto_aead_chacha20poly1305_NPUBBYTES crypto_aead_chacha20poly1305_ietf_KEYBYTES crypto_aead_chacha20poly1305_ietf_NPUBBYTES crypto_aead_xchacha20poly1305_ietf_KEYBYTES crypto_aead_xchacha20poly1305_ietf_NPUBBYTES crypto_aead_xchacha20poly1305_ietf_ABYTES crypto_auth_BYTES crypto_auth_KEYBYTES crypto_box_BEFORENMBYTES crypto_box_BOXZEROBYTES crypto_box_MACBYTES crypto_box_NONCEBYTES crypto_box_PUBLICKEYBYTES crypto_box_SEALBYTES crypto_box_SECRETKEYBYTES crypto_box_SEEDBYTES crypto_box_ZEROBYTES crypto_generichash_KEYBYTES_MAX crypto_generichash_BYTES crypto_generichash_BYTES_MAX crypto_generichash_BYTES_MIN crypto_generichash_STATEBYTES crypto_generichash_blake2b_BYTES crypto_generichash_blake2b_BYTES_MAX crypto_generichash_blake2b_BYTES_MIN crypto_generichash_blake2b_KEYBYTES_MAX crypto_generichash_blake2b_PERSONALBYTES crypto_generichash_blake2b_SALTBYTES crypto_hash_sha256_BYTES crypto_hash_sha512_BYTES crypto_kx_PUBLICKEYBYTES crypto_kx_SECRETKEYBYTES crypto_kx_SESSIONKEYBYTES crypto_pwhash_ALG_DEFAULT crypto_pwhash_ALG_ARGON2I13 crypto_pwhash_ALG_ARGON2ID13 crypto_pwhash_BYTES_MAX crypto_pwhash_BYTES_MIN crypto_pwhash_MEMLIMIT_MAX crypto_pwhash_MEMLIMIT_MIN crypto_pwhash_MEMLIMIT_INTERACTIVE crypto_pwhash_MEMLIMIT_MODERATE crypto_pwhash_MEMLIMIT_SENSITIVE crypto_pwhash_OPSLIMIT_MAX crypto_pwhash_OPSLIMIT_MIN crypto_pwhash_OPSLIMIT_INTERACTIVE crypto_pwhash_OPSLIMIT_MODERATE crypto_pwhash_OPSLIMIT_SENSITIVE crypto_pwhash_PASSWD_MAX crypto_pwhash_PASSWD_MIN crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE crypto_pwhash_argon2i_MEMLIMIT_MODERATE crypto_pwhash_argon2i_MEMLIMIT_SENSITIVE crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE crypto_pwhash_argon2i_OPSLIMIT_MODERATE crypto_pwhash_argon2i_OPSLIMIT_SENSITIVE crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE crypto_pwhash_argon2id_MEMLIMIT_MODERATE crypto_pwhash_argon2id_MEMLIMIT_SENSITIVE crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE crypto_pwhash_argon2id_OPSLIMIT_MODERATE crypto_pwhash_argon2id_OPSLIMIT_SENSITIVE crypto_pwhash_SALTBYTES crypto_pwhash_STRBYTES crypto_pwhash_scryptsalsa208sha256_BYTES_MAX crypto_pwhash_scryptsalsa208sha256_BYTES_MIN crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX crypto_pwhash_scryptsalsa208sha256_PASSWD_MIN crypto_pwhash_scryptsalsa208sha256_SALTBYTES crypto_pwhash_scryptsalsa208sha256_STRBYTES crypto_pwhash_scryptsalsa208sha256_STRPREFIX crypto_scalarmult_BYTES crypto_scalarmult_SCALARBYTES crypto_scalarmult_curve25519_BYTES crypto_secretbox_BOXZEROBYTES crypto_secretbox_KEYBYTES crypto_secretbox_KEYBYTES crypto_secretbox_MACBYTES crypto_secretbox_NONCEBYTES crypto_secretbox_ZEROBYTES crypto_secretstream_xchacha20poly1305_STATEBYTES crypto_secretstream_xchacha20poly1305_ABYTES crypto_secretstream_xchacha20poly1305_HEADERBYTES crypto_secretstream_xchacha20poly1305_KEYBYTES crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX crypto_secretstream_xchacha20poly1305_TAG_MESSAGE crypto_secretstream_xchacha20poly1305_TAG_PUSH crypto_secretstream_xchacha20poly1305_TAG_REKEY crypto_secretstream_xchacha20poly1305_TAG_FINAL crypto_sign_BYTES crypto_sign_PUBLICKEYBYTES crypto_sign_SECRETKEYBYTES crypto_sign_SEEDBYTES crypto_sign_ed25519_PUBLICKEYBYTES crypto_sign_ed25519_SECRETKEYBYTES crypto_stream_KEYBYTES crypto_stream_NONCEBYTES crypto_stream_chacha20_NONCEBYTES crypto_stream_chacha20_KEYBYTES ``` Note most of the the `*_easy` functions are not implemented as the "non-easy" functions provide already the "easy" interface, which hides the placement of buffers in memory, which makes little sense in python, so this wrapper handles this. pysodium-0.7.0-0/PKG-INFO0000644000175000017500000002230113235077010013343 0ustar ss00000000000000Metadata-Version: 1.1 Name: pysodium Version: 0.7.0-0 Summary: python libsodium wrapper Home-page: https://github.com/stef/pysodium Author: Stefan Marsiske Author-email: s@ctrlc.hu License: BSD Description: This is a very simple wrapper around libsodium masquerading as nacl. [![Build Status](https://travis-ci.org/stef/pysodium.svg?branch=master)](https://travis-ci.org/stef/pysodium) This wrapper requires a pre-installed libsodium from: https://github.com/jedisct1/libsodium then it provides access to the following functions: ``` crypto_aead_chacha20poly1305_decrypt(ciphertext, ad, nonce, key) crypto_aead_chacha20poly1305_decrypt_detached(ciphertext, mac, ad, nonce, key) crypto_aead_chacha20poly1305_encrypt_detached(message, ad, nonce, key) crypto_aead_chacha20poly1305_encrypt(message, ad, nonce, key) crypto_aead_chacha20poly1305_ietf_decrypt(ciphertext, ad, nonce, key) crypto_aead_chacha20poly1305_ietf_encrypt(message, ad, nonce, key) crypto_aead_xchacha20poly1305_ietf_decrypt(ciphertext, ad, nonce, key) crypto_aead_xchacha20poly1305_ietf_encrypt(message, ad, nonce, key) crypto_auth(message, key) crypto_auth_verify(tag, message, key) crypto_box_afternm(msg, nonce, k) crypto_box_beforenm(pk, sk) crypto_box_detached(msg, nonce, pk, sk) crypto_box_keypair() crypto_box(msg, nonce, pk, sk) crypto_box_open_afternm(c, nonce, k) crypto_box_open(c, nonce, pk, sk) crypto_box_open_detached(c, mac, nonce, pk, sk) crypto_box_seal(msg, pk) crypto_box_seal_open(c, pk, sk) crypto_box_seed_keypair(seed) crypto_generichash_blake2b_salt_personal(message, outlen = crypto_generichash_blake2b_BYTES, key = b'', salt = b'', personal = b'') crypto_generichash_final(state, outlen=crypto_generichash_BYTES) crypto_generichash_init(outlen=crypto_generichash_BYTES, k=b'') crypto_generichash(m, k=b'', outlen=crypto_generichash_BYTES) crypto_generichash_update(state, m) crypto_hash_sha256(message) crypto_hash_sha512(message) crypto_kx_client_session_keys(client_pk, client_sk, server_pk) crypto_kx_keypair() crypto_kx_server_session_keys(server_pk, server_sk, client_pk) crypto_pwhash(outlen, passwd, salt, opslimit, memlimit, alg) crypto_pwhash_scryptsalsa208sha256(outlen, passwd, salt, opslimit, memlimit) crypto_pwhash_scryptsalsa208sha256_str(passwd, opslimit, memlimit) crypto_pwhash_scryptsalsa208sha256_str_verify(stored, passwd) crypto_pwhash_str(passwd, opslimit, memlimit) crypto_pwhash_str_verify(pstr, passwd) crypto_scalarmult_curve25519_base(n) crypto_scalarmult_curve25519(n, p) crypto_secretbox(msg, nonce, k) crypto_secretbox_open(c, nonce, k) crypto_secretstream_xchacha20poly1305_keygen(): crypto_secretstream_xchacha20poly1305_init_push(key): crypto_secretstream_xchacha20poly1305_init_pull(header, key): crypto_secretstream_xchacha20poly1305_rekey(state): crypto_secretstream_xchacha20poly1305_push(state, message, ad, tag): crypto_secretstream_xchacha20poly1305_pull(state, ciphertext, ad): crypto_sign_init() crypto_sign_update(state, m) crypto_sign_final_create(state, sk) crypto_sign_final_verify(state, sig, pk) crypto_sign_detached(m, sk) crypto_sign_keypair() crypto_sign(m, sk) crypto_sign_open(sm, pk) crypto_sign_pk_to_box_pk(pk) crypto_sign_seed_keypair(seed) crypto_sign_sk_to_box_sk(sk) crypto_sign_sk_to_pk(sk) crypto_sign_sk_to_seed(sk) crypto_sign_verify_detached(sig, msg, pk) crypto_stream_chacha20_xor(message, nonce, key) crypto_stream(cnt, nonce=None, key=None) crypto_stream_xor(msg, cnt, nonce=None, key=None) randombytes(size) ``` Constants: ``` crypto_aead_chacha20poly1305_ABYTES crypto_aead_chacha20poly1305_KEYBYTES crypto_aead_chacha20poly1305_NPUBBYTES crypto_aead_chacha20poly1305_ietf_KEYBYTES crypto_aead_chacha20poly1305_ietf_NPUBBYTES crypto_aead_xchacha20poly1305_ietf_KEYBYTES crypto_aead_xchacha20poly1305_ietf_NPUBBYTES crypto_aead_xchacha20poly1305_ietf_ABYTES crypto_auth_BYTES crypto_auth_KEYBYTES crypto_box_BEFORENMBYTES crypto_box_BOXZEROBYTES crypto_box_MACBYTES crypto_box_NONCEBYTES crypto_box_PUBLICKEYBYTES crypto_box_SEALBYTES crypto_box_SECRETKEYBYTES crypto_box_SEEDBYTES crypto_box_ZEROBYTES crypto_generichash_KEYBYTES_MAX crypto_generichash_BYTES crypto_generichash_BYTES_MAX crypto_generichash_BYTES_MIN crypto_generichash_STATEBYTES crypto_generichash_blake2b_BYTES crypto_generichash_blake2b_BYTES_MAX crypto_generichash_blake2b_BYTES_MIN crypto_generichash_blake2b_KEYBYTES_MAX crypto_generichash_blake2b_PERSONALBYTES crypto_generichash_blake2b_SALTBYTES crypto_hash_sha256_BYTES crypto_hash_sha512_BYTES crypto_kx_PUBLICKEYBYTES crypto_kx_SECRETKEYBYTES crypto_kx_SESSIONKEYBYTES crypto_pwhash_ALG_DEFAULT crypto_pwhash_ALG_ARGON2I13 crypto_pwhash_ALG_ARGON2ID13 crypto_pwhash_BYTES_MAX crypto_pwhash_BYTES_MIN crypto_pwhash_MEMLIMIT_MAX crypto_pwhash_MEMLIMIT_MIN crypto_pwhash_MEMLIMIT_INTERACTIVE crypto_pwhash_MEMLIMIT_MODERATE crypto_pwhash_MEMLIMIT_SENSITIVE crypto_pwhash_OPSLIMIT_MAX crypto_pwhash_OPSLIMIT_MIN crypto_pwhash_OPSLIMIT_INTERACTIVE crypto_pwhash_OPSLIMIT_MODERATE crypto_pwhash_OPSLIMIT_SENSITIVE crypto_pwhash_PASSWD_MAX crypto_pwhash_PASSWD_MIN crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE crypto_pwhash_argon2i_MEMLIMIT_MODERATE crypto_pwhash_argon2i_MEMLIMIT_SENSITIVE crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE crypto_pwhash_argon2i_OPSLIMIT_MODERATE crypto_pwhash_argon2i_OPSLIMIT_SENSITIVE crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE crypto_pwhash_argon2id_MEMLIMIT_MODERATE crypto_pwhash_argon2id_MEMLIMIT_SENSITIVE crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE crypto_pwhash_argon2id_OPSLIMIT_MODERATE crypto_pwhash_argon2id_OPSLIMIT_SENSITIVE crypto_pwhash_SALTBYTES crypto_pwhash_STRBYTES crypto_pwhash_scryptsalsa208sha256_BYTES_MAX crypto_pwhash_scryptsalsa208sha256_BYTES_MIN crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX crypto_pwhash_scryptsalsa208sha256_PASSWD_MIN crypto_pwhash_scryptsalsa208sha256_SALTBYTES crypto_pwhash_scryptsalsa208sha256_STRBYTES crypto_pwhash_scryptsalsa208sha256_STRPREFIX crypto_scalarmult_BYTES crypto_scalarmult_SCALARBYTES crypto_scalarmult_curve25519_BYTES crypto_secretbox_BOXZEROBYTES crypto_secretbox_KEYBYTES crypto_secretbox_KEYBYTES crypto_secretbox_MACBYTES crypto_secretbox_NONCEBYTES crypto_secretbox_ZEROBYTES crypto_secretstream_xchacha20poly1305_STATEBYTES crypto_secretstream_xchacha20poly1305_ABYTES crypto_secretstream_xchacha20poly1305_HEADERBYTES crypto_secretstream_xchacha20poly1305_KEYBYTES crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX crypto_secretstream_xchacha20poly1305_TAG_MESSAGE crypto_secretstream_xchacha20poly1305_TAG_PUSH crypto_secretstream_xchacha20poly1305_TAG_REKEY crypto_secretstream_xchacha20poly1305_TAG_FINAL crypto_sign_BYTES crypto_sign_PUBLICKEYBYTES crypto_sign_SECRETKEYBYTES crypto_sign_SEEDBYTES crypto_sign_ed25519_PUBLICKEYBYTES crypto_sign_ed25519_SECRETKEYBYTES crypto_stream_KEYBYTES crypto_stream_NONCEBYTES crypto_stream_chacha20_NONCEBYTES crypto_stream_chacha20_KEYBYTES ``` Note most of the the `*_easy` functions are not implemented as the "non-easy" functions provide already the "easy" interface, which hides the placement of buffers in memory, which makes little sense in python, so this wrapper handles this. Keywords: cryptography API NaCl libsodium Platform: UNKNOWN Classifier: Development Status :: 4 - Beta Classifier: License :: OSI Approved :: BSD License Classifier: Topic :: Security :: Cryptography Classifier: Topic :: Security Requires: libsodium pysodium-0.7.0-0/test/0000755000175000017500000000000013235077010013227 5ustar ss00000000000000pysodium-0.7.0-0/test/__init__.py0000644000175000017500000000000113131217774015337 0ustar ss00000000000000 pysodium-0.7.0-0/test/test_pysodium.py0000755000175000017500000006641313225666642016544 0ustar ss00000000000000#!/usr/bin/env python2 """ Wrapper for libsodium library Copyright (c) 2013-2014, Marsiske Stefan. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import pysodium import unittest import binascii class TestPySodium(unittest.TestCase): def test_crypto_stream(self): pysodium.crypto_stream(8) pysodium.crypto_stream(16) pysodium.crypto_stream(32) def test_crypto_stream_xor(self): nonce = b'\x00' * pysodium.crypto_stream_NONCEBYTES key = b'\x00' * pysodium.crypto_stream_KEYBYTES pysodium.crypto_stream_xor(b'howdy', len(b'howdy'), nonce, key) pysodium.crypto_stream_xor(b'howdy' * 16, len(b'howdy') * 16, nonce, key) def test_crypto_generichash(self): r=pysodium.crypto_generichash(b'howdy') pysodium.crypto_generichash(b'howdy', outlen=4) r6=pysodium.crypto_generichash(b'howdy', outlen=6) pysodium.crypto_generichash(b'howdy', outlen=8) state = pysodium.crypto_generichash_init() pysodium.crypto_generichash_update(state, b'howdy') r1=pysodium.crypto_generichash_final(state) state = pysodium.crypto_generichash_init(outlen=6) pysodium.crypto_generichash_update(state, b'howdy') r61=pysodium.crypto_generichash_final(state, outlen=6) self.assertEqual(r, r1) self.assertEqual(r6, r61) def test_crypto_box_pk_from_sk(self): pk1, sk = pysodium.crypto_box_keypair() pk2 = pysodium.crypto_scalarmult_curve25519_base(sk) self.assertEqual(pk1, pk2) def test_crypto_box_seal(self): if not pysodium.sodium_version_check(1, 0, 3): return pk, sk = pysodium.crypto_box_keypair() c = pysodium.crypto_box_seal(b"howdy", pk) self.assertEqual(pysodium.crypto_box_seal_open(c, pk, sk), b'howdy') def test_crypto_box_open(self): m = b"howdy" pk, sk = pysodium.crypto_box_keypair() n = pysodium.randombytes(pysodium.crypto_box_NONCEBYTES) c = pysodium.crypto_box(m, n, pk, sk) plaintext = pysodium.crypto_box_open(c, n, pk, sk) self.assertEqual(m, plaintext) def test_crypto_box_open_afternm(self): m = b"howdy" pk, sk = pysodium.crypto_box_keypair() k = pysodium.crypto_box_beforenm(pk, sk) n = pysodium.randombytes(pysodium.crypto_box_NONCEBYTES) c = pysodium.crypto_box_afternm(m, n, k) self.assertEqual(c, c) plaintext = pysodium.crypto_box_open_afternm(c, n, k) self.assertEqual(m, plaintext) def test_crypto_box_open_detached(self): pk, sk = pysodium.crypto_box_keypair() n = pysodium.randombytes(pysodium.crypto_box_NONCEBYTES) c, mac = pysodium.crypto_box_detached(b"howdy", n, pk, sk) r = pysodium.crypto_box_open_detached(c, mac, n, pk, sk) self.assertEqual(r, b"howdy") changed = b"\0"*len(c) self.assertRaises(ValueError, pysodium.crypto_box_open_detached,changed, mac, n, pk, sk) def test_crypto_secretbox_open(self): k = pysodium.randombytes(pysodium.crypto_secretbox_KEYBYTES) n = pysodium.randombytes(pysodium.crypto_secretbox_NONCEBYTES) c = pysodium.crypto_secretbox(b"howdy", n, k) pysodium.crypto_secretbox_open(c, n, k) def test_crypto_secretstream_xchacha20poly1305_keygen(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() self.assertEqual(len(key), 32) # The following 3 tests verify that no exceptions are raised. Cannot check these # in any more detail as doing so would require internal knowledge of the state and # header structures, which may change. This can be assumed to be correct as long # as 'pull' test passes, it's decrypted values matches the original plain text. def test_crypto_secretstream_xchacha20poly1305_init_push(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) def test_crypto_secretstream_xchacha20poly1305_init_pull(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key) def test_crypto_secretstream_xchacha20poly1305_push(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", None, 0) #---- def test_crypto_secretstream_xchacha20poly1305_pull(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key) msg, tag = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext, None) self.assertEqual(msg, b"howdy") self.assertEqual(tag, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) def test_crypto_secretstream_xchacha20poly1305_pull_changed_ad(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", b"some data", pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key) self.assertRaises(ValueError, pysodium.crypto_secretstream_xchacha20poly1305_pull, state2, ciphertext, b"different data") def test_crypto_secretstream_xchacha20poly1305_pull_incorrect_key(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) bad_key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, bad_key) self.assertRaises(ValueError, pysodium.crypto_secretstream_xchacha20poly1305_pull, state2, ciphertext, None) def test_crypto_secretstream_xchacha20poly1305_pull_multiple(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"Correct Horse Battery Staple", None, 0) ciphertext2 = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) # Verify decryption state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key) msg, tag = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext, None) msg2, tag2 = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext2, None) self.assertEqual(msg, b"Correct Horse Battery Staple") self.assertEqual(tag, 0) self.assertEqual(msg2, b"howdy") self.assertEqual(tag2, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) def test_crypto_secretstream_xchacha20poly1305_pull_corrupted(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) ad = 'additional data' ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"Correct Horse Battery Staple", ad, 0) # Verify error is raised if cypher text is changed state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key) self.assertRaises(ValueError, pysodium.crypto_secretstream_xchacha20poly1305_pull, state2, ciphertext + 'this is a corruption'.encode(), ad) # Verify error is raised if additional data is changed ad2 = 'this is not the same' state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key) self.assertRaises(ValueError, pysodium.crypto_secretstream_xchacha20poly1305_pull, state2, ciphertext, ad2) def test_crypto_secretstream_xchacha20poly1305_rekey(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) # Encrypt two messages with intermediate re-key ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"Correct Horse Battery Staple", None, 0) pysodium.crypto_secretstream_xchacha20poly1305_rekey(state) ciphertext2 = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) # Verify by decrypting them state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key) msg, tag = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext, None) pysodium.crypto_secretstream_xchacha20poly1305_rekey(state2) msg2, tag2 = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext2, None) self.assertEqual(msg, b"Correct Horse Battery Staple") self.assertEqual(tag, 0) self.assertEqual(msg2, b"howdy") self.assertEqual(tag2, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) def test_crypto_secretstream_xchacha20poly1305_missing_rekey(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) # Encrypt two messages with intermediate re-key ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"Correct Horse Battery Staple", None, 0) pysodium.crypto_secretstream_xchacha20poly1305_rekey(state) ciphertext2 = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key) msg, tag = pysodium.crypto_secretstream_xchacha20poly1305_pull(state2, ciphertext, None) # re-key should be here, so following call should fail self.assertRaises(ValueError, pysodium.crypto_secretstream_xchacha20poly1305_pull, state2, ciphertext2, None) def test_crypto_secretstream_xchacha20poly1305_out_of_order_messeges(self): if not pysodium.sodium_version_check(1, 0, 15): return key = pysodium.crypto_secretstream_xchacha20poly1305_keygen() state, header = pysodium.crypto_secretstream_xchacha20poly1305_init_push(key) ciphertext = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"Correct Horse Battery Staple", None, 0) ciphertext2 = pysodium.crypto_secretstream_xchacha20poly1305_push(state, b"howdy", None, pysodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) # Decrypting the second message first should fail state2 = pysodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key) self.assertRaises(ValueError, pysodium.crypto_secretstream_xchacha20poly1305_pull, state2, ciphertext2, None) def test_test_crypto_scalarmult_curve25519_base(self): s = pysodium.crypto_scalarmult_curve25519_base(pysodium.randombytes(pysodium.crypto_scalarmult_BYTES)) r = pysodium.crypto_scalarmult_curve25519_base(pysodium.randombytes(pysodium.crypto_scalarmult_BYTES)) pysodium.crypto_scalarmult_curve25519(s, r) def test_crypto_sign_open(self): pk, sk = pysodium.crypto_sign_keypair() signed = pysodium.crypto_sign(b'howdy', sk) changed = signed[:pysodium.crypto_sign_BYTES] + b'0' + signed[pysodium.crypto_sign_BYTES + 1:] pysodium.crypto_sign_open(signed, pk) self.assertRaises(ValueError, pysodium.crypto_sign_open, changed, pk) def test_crypto_sign_seed_keypair(self): seed = pysodium.crypto_generichash(b'howdy', outlen=pysodium.crypto_sign_SEEDBYTES) pk, sk = pysodium.crypto_sign_seed_keypair(seed) pk2, sk2 = pysodium.crypto_sign_seed_keypair(seed) self.assertEqual(pk, pk2) self.assertEqual(sk, sk2) 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_) def test_aead_chacha20poly1305_detached(self): if not pysodium.sodium_version_check(1, 0, 9): return key = binascii.unhexlify(b"4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd1100a1007") input_ = binascii.unhexlify(b"86d09974840bded2a5ca") nonce = binascii.unhexlify(b"cd7cf67be39c794a") for ad, ct in [ (binascii.unhexlify(b"87e229d4500845a079c0"), b"677dabf4e3d24b876bb284753896e1d6"), (None, b"69e7789bcd954e658ed38423e23161dc"), ]: output, mac = pysodium.crypto_aead_chacha20poly1305_encrypt_detached(input_, ad, nonce, key) self.assertEqual(binascii.unhexlify(b"e3e446f7ede9a19b62a4"), output) self.assertEqual(binascii.unhexlify(ct), mac) output = pysodium.crypto_aead_chacha20poly1305_decrypt_detached(output, mac, ad, nonce, key) self.assertEqual(output, input_) def test_aead_chacha20poly1305_ietf(self): if not pysodium.sodium_version_check(1, 0, 4): return key = binascii.unhexlify(b"4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd1100a1007") input_ = binascii.unhexlify(b"86d09974840bded2a5ca") nonce = binascii.unhexlify(b"cd7cf67be39c794acd7cf67b") for ad in [binascii.unhexlify(b"87e229d4500845a079c0"), None]: output = pysodium.crypto_aead_chacha20poly1305_ietf_encrypt(input_, ad, nonce, key) output = pysodium.crypto_aead_chacha20poly1305_ietf_decrypt(output, ad, nonce, key) self.assertEqual(output, input_) def test_aead_xchacha20poly1305_ietf(self): if not pysodium.sodium_version_check(1, 0, 12): return key = binascii.unhexlify(b"4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd1100a1007") input_ = binascii.unhexlify(b"86d09974840bded2a5ca") nonce = binascii.unhexlify(b"cd7cf67be39c794acd7cf67bcd7cf67be39c794acd7cf67b") for ad in [binascii.unhexlify(b"87e229d4500845a079c0"), None]: output = pysodium.crypto_aead_xchacha20poly1305_ietf_encrypt(input_, ad, nonce, key) output = pysodium.crypto_aead_xchacha20poly1305_ietf_decrypt(output, ad, nonce, key) self.assertEqual(output, input_) def test_crypto_stream_chacha20_xor(self): key = binascii.unhexlify(b"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f") nonce = binascii.unhexlify(b"0001020304050607") input_ = b'\x00' * 256 output = pysodium.crypto_stream_chacha20_xor(input_, nonce, key) self.assertEqual(binascii.unhexlify(b"f798a189f195e66982105ffb640bb7757f579da31602fc93ec01ac56f85ac3c134a4547b733b46413042c9440049176905d3be59ea1c53f15916155c2be8241a38008b9a26bc35941e2444177c8ade6689de95264986d95889fb60e84629c9bd9a5acb1cc118be563eb9b3a4a472f82e09a7e778492b562ef7130e88dfe031c79db9d4f7c7a899151b9a475032b63fc385245fe054e3dd5a97a5f576fe064025d3ce042c566ab2c507b138db853e3d6959660996546cc9c4a6eafdc777c040d70eaf46f76dad3979e5c5360c3317166a1c894c94a371876a94df7628fe4eaaf2ccb27d5aaae0ad7ad0f9d4b6ad3b54098746d4524d38407a6deb3ab78fab78c9"), output) def test_crypto_blake2b(self): message = binascii.unhexlify(b'54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67') key = binascii.unhexlify(b'000102030405060708090a0b0c0d0e0f') # Test vectors generated from the blake2b reference implementation self.assertEqual(pysodium.crypto_generichash_blake2b_salt_personal(message, key = key), binascii.unhexlify(b'0f44eda51dba98442425d486b89962647c1a6e0e8b98a93e7090bf849a5156da')) self.assertEqual(pysodium.crypto_generichash_blake2b_salt_personal(message, personal = key), binascii.unhexlify(b'8c68aecca4b50e91aebaf8c53bde15b68c01b13d571a772fcb8b432affa52a7c')) self.assertEqual(pysodium.crypto_generichash_blake2b_salt_personal(message, salt = key), binascii.unhexlify(b'43b7feaa91019d0d5b492357fb923211af827d6126af28ccc1874e70bc2177f8')) self.assertEqual(pysodium.crypto_generichash_blake2b_salt_personal(message, personal = key[0:8]), binascii.unhexlify(b'31353589b3f179cda74387fbe1deca94f004661f05cde2295a16c0a8d8ead79b')) self.assertEqual(pysodium.crypto_generichash_blake2b_salt_personal(message, salt = key[0:8]), binascii.unhexlify(b'11c29bf7b91b8500a463f27e215dc83afdb71ed5e959f0847e339769c4835fc7')) self.assertEqual(pysodium.crypto_generichash_blake2b_salt_personal(message, personal = key, key = key), binascii.unhexlify(b'5a0b3db4bf2dab71485211447fc2014391228cc6c1acd2f3031050a9a32ca407')) def test_crypto_pwhash(self): if not pysodium.sodium_version_check(1, 0, 9): return pw = "Correct Horse Battery Staple" salt = binascii.unhexlify(b'0f58b94c7a369fd8a9a7083e4cd75266') out = pysodium.crypto_pwhash(pysodium.crypto_auth_KEYBYTES, pw, salt, pysodium.crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE, pysodium.crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE, pysodium.crypto_pwhash_ALG_ARGON2I13) self.assertEqual(binascii.hexlify(out), b'79db3095517c7358449d84ee3b2f81f0e9907fbd4e0bae4e0bcc6c79821427dc') def test_crypto_pwhash_storage(self): if not pysodium.sodium_version_check(1, 0, 9): return pw = "Correct Horse Battery Staple" pstr = pysodium.crypto_pwhash_str(pw, pysodium.crypto_pwhash_OPSLIMIT_INTERACTIVE, pysodium.crypto_pwhash_MEMLIMIT_INTERACTIVE) self.assertTrue(pysodium.crypto_pwhash_str_verify(pstr, pw)) def test_crypto_pwhash_scryptsalsa208sha256(self): passwd = b'Correct Horse Battery Staple' other_passwd = b'correct horse battery staple' salt = binascii.unhexlify(b'4206baae5578933d7cfb315b1c257cc7af162965a91a74ccbb1cfa1d747eb691') other_salt = binascii.unhexlify(b'4206baae5578933d7cfb315b1c257cc7af162965a91a74ccbb1cfa1d747eb692') # Use very small limits to avoid burning resources in CI mem_limit = 16777216 ops_limit = 32768 key16 = pysodium.crypto_pwhash_scryptsalsa208sha256(16, passwd, salt, ops_limit, mem_limit) self.assertEqual(len(key16), 16) self.assertEqual(key16, b'U\x18aL\xcf\xc9\xa3\xf6(\x8f\xed)\xeej8\xdf') key = pysodium.crypto_pwhash_scryptsalsa208sha256(32, passwd, salt, ops_limit, mem_limit) self.assertEqual(len(key), 32) self.assertEqual(key, b'U\x18aL\xcf\xc9\xa3\xf6(\x8f\xed)\xeej8\xdfG\x82hf+vu\xcd\x9c\xdb\xbcmt\xf7\xf1\x10') self.assertNotEqual(key, pysodium.crypto_pwhash_scryptsalsa208sha256(32, passwd, other_salt, ops_limit, mem_limit)) self.assertNotEqual(key, pysodium.crypto_pwhash_scryptsalsa208sha256(32, other_passwd, salt, ops_limit, mem_limit)) def test_crypto_pwhash_scryptsalsa208sha256_str(self): passwd = b'Correct Horse Battery Staple' # Use very small limits to avoid burning resources in CI mem_limit = 16777216 ops_limit = 32768 storage_string = pysodium.crypto_pwhash_scryptsalsa208sha256_str(passwd, ops_limit, mem_limit) self.assertTrue(storage_string.startswith(pysodium.crypto_pwhash_scryptsalsa208sha256_STRPREFIX)) self.assertNotEqual(storage_string, pysodium.crypto_pwhash_scryptsalsa208sha256_str(passwd, ops_limit, mem_limit), "Each call should compute a new random salt.") def test_crypto_pwhash_scryptsalsa208sha256_str_verify(self): passwd = b'Correct Horse Battery Staple' other_passwd = b'correct horse battery staple' # Use very small limits to avoid burning resources in CI mem_limit = 16777216 ops_limit = 32768 storage_string = pysodium.crypto_pwhash_scryptsalsa208sha256_str(passwd, ops_limit, mem_limit) pysodium.crypto_pwhash_scryptsalsa208sha256_str_verify(storage_string, passwd) self.assertRaises(ValueError, pysodium.crypto_pwhash_scryptsalsa208sha256_str_verify, storage_string, other_passwd) self.assertRaises(ValueError, pysodium.crypto_pwhash_scryptsalsa208sha256_str_verify, storage_string[:-1], passwd) self.assertRaises(ValueError, pysodium.crypto_pwhash_scryptsalsa208sha256_str_verify, storage_string + b'a', passwd) def test_crypto_sign_sk_to_pk(self): pk, sk = pysodium.crypto_sign_keypair() pk2 = pysodium.crypto_sign_sk_to_pk(sk) self.assertEqual(pk, pk2) def test_crypto_sign_sk_to_seed(self): seed1 = pysodium.crypto_generichash(b'howdy', outlen=pysodium.crypto_sign_SEEDBYTES) _, sk = pysodium.crypto_sign_seed_keypair(seed1) seed2 = pysodium.crypto_sign_sk_to_seed(sk) self.assertEqual(seed1, seed2) def test_AsymCrypto_With_Seeded_Keypair(self): msg = b"correct horse battery staple" nonce = pysodium.randombytes(pysodium.crypto_box_NONCEBYTES) pk, sk = pysodium.crypto_box_seed_keypair(b"\x00" * pysodium.crypto_box_SEEDBYTES) c = pysodium.crypto_box(msg, nonce, pk, sk) m = pysodium.crypto_box_open(c, nonce, pk, sk) self.assertEqual(msg, m) def test_crypto_hash_sha256(self): self.assertEqual(self.byteHashToString(pysodium.crypto_hash_sha256(b"test")), "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08") self.assertEqual(self.byteHashToString(pysodium.crypto_hash_sha256(b"howdy")), "0f1128046248f83dc9b9ab187e16fad0ff596128f1524d05a9a77c4ad932f10a") self.assertEqual(self.byteHashToString(pysodium.crypto_hash_sha256(b"Correct Horse Battery Staple")), "af139fa284364215adfa49c889ab7feddc5e5d1c52512ffb2cfc9baeb67f220e") self.assertEqual(self.byteHashToString(pysodium.crypto_hash_sha256(b"pysodium")), "0a53ef9bc1bea173118a42bbbe8300abb6bbef83139046940e9593d9559a5df7") self.assertEqual(self.byteHashToString(pysodium.crypto_hash_sha256(b"\x80")), "76be8b528d0075f7aae98d6fa57a6d3c83ae480a8469e668d7b0af968995ac71") def test_crypto_hash_sha512(self): self.assertEqual(self.byteHashToString(pysodium.crypto_hash_sha512(b"test")), "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff") self.assertEqual(self.byteHashToString(pysodium.crypto_hash_sha512(b"howdy")), "905caca5c4685f296c5491d38660d7720ee87bef08f829332e905593522907674de8490de46c969d2c585b40af40439b387562d6f776023507753d1a9554ebbb") self.assertEqual(self.byteHashToString(pysodium.crypto_hash_sha512(b"Correct Horse Battery Staple")), "0675070bda47bef936f0b65ae721d90f82ca137841df4d7cae27776501ae4b446ab926d64dc1d282c8758ac0eb02cc4aa11b2452d4f8ffeb795023b797fe2b80") self.assertEqual(self.byteHashToString(pysodium.crypto_hash_sha512(b"pysodium")), "ecbc6f4ffdb6e6dcbe6e6beecf0b8e05c11b0cc8a56f2b4098cd613585749fcca5ed1cfda3518e33a5d2c63746ee2857ff6857b9a2eeda4cc208c1e7fd89cc17") self.assertEqual(self.byteHashToString(pysodium.crypto_hash_sha512(b"\x80")), "dfe8ef54110b3324d3b889035c95cfb80c92704614bf76f17546ad4f4b08218a630e16da7df34766a975b3bb85b01df9e99a4ec0a1d0ec3de6bed7b7a40b2f10") def byteHashToString(self, input): import sys result = "" for i in range(0, len(input)): if sys.version_info.major == 3: tmp = str(hex(ord(chr(input[i]))))[2:] else: tmp = str(hex(ord(input[i])))[2:] if len(tmp) is 1: tmp = "0" + tmp result += tmp return result def test_crypto_auth(self): sk = pysodium.randombytes(pysodium.crypto_auth_KEYBYTES) tag = pysodium.crypto_auth("howdy", sk) pysodium.crypto_auth_verify(tag, "howdy", sk) def test_crypto_kx(self): if not pysodium.sodium_version_check(1, 0, 12): return client_pk, client_sk = pysodium.crypto_kx_keypair() server_pk, server_sk = pysodium.crypto_kx_keypair() crx, ctx = pysodium.crypto_kx_client_session_keys(client_pk, client_sk, server_pk) srx, stx = pysodium.crypto_kx_server_session_keys(server_pk, server_sk, client_pk) self.assertEqual(crx, stx) self.assertEqual(ctx, srx) if __name__ == '__main__': unittest.main() pysodium-0.7.0-0/setup.py0000644000175000017500000000165313235077006013774 0ustar ss00000000000000import os from setuptools import setup, find_packages # Utility function to read the README file. # Used for the long_description. It's nice, because now 1) we have a top level # README file and 2) it's easier to type in the README file than to put a raw # string in below ... def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read() setup( name="pysodium", version="0.7.0_0", author="Stefan Marsiske", author_email="s@ctrlc.hu", description="python libsodium wrapper", license="BSD", keywords="cryptography API NaCl libsodium", url="https://github.com/stef/pysodium", packages=find_packages(), long_description=read('README.md'), requires=["libsodium"], classifiers=["Development Status :: 4 - Beta", "License :: OSI Approved :: BSD License", "Topic :: Security :: Cryptography", "Topic :: Security"], )