Beispiel #1
0
def load_libsodium():
    global loaded, libsodium, buf

    libsodium = util.find_library('sodium', 'crypto_stream_salsa20_xor_ic',
                                  'libsodium')
    if libsodium is None:
        raise Exception('libsodium not found')

    libsodium.crypto_stream_salsa20_xor_ic.restype = c_int
    libsodium.crypto_stream_salsa20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                       c_ulonglong, c_char_p,
                                                       c_ulonglong, c_char_p)
    libsodium.crypto_stream_chacha20_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                        c_ulonglong, c_char_p,
                                                        c_ulonglong, c_char_p)

    try:
        libsodium.crypto_stream_chacha20_ietf_xor_ic.restype = c_int
        libsodium.crypto_stream_chacha20_ietf_xor_ic.argtypes = (c_void_p,
                                                                 c_char_p,
                                                                 c_ulonglong,
                                                                 c_char_p,
                                                                 c_ulong,
                                                                 c_char_p)
    except:
        pass

    buf = create_string_buffer(buf_size)
    loaded = True
Beispiel #2
0
def load_openssl(crypto_path=None):
    global loaded, libcrypto, libsodium, buf, ctx_cleanup

    crypto_path = dict(crypto_path) if crypto_path else dict()
    path = crypto_path.get('openssl', None)
    libcrypto = util.find_library(('crypto', 'eay32'), 'EVP_get_cipherbyname',
                                  'libcrypto', path)
    if libcrypto is None:
        raise Exception('libcrypto(OpenSSL) not found with path %s' % path)

    libcrypto.EVP_get_cipherbyname.restype = c_void_p
    libcrypto.EVP_CIPHER_CTX_new.restype = c_void_p

    libcrypto.EVP_CipherInit_ex.argtypes = (c_void_p, c_void_p, c_char_p,
                                            c_char_p, c_char_p, c_int)
    libcrypto.EVP_CIPHER_CTX_ctrl.argtypes = (c_void_p, c_int, c_int, c_void_p)

    libcrypto.EVP_CipherUpdate.argtypes = (c_void_p, c_void_p, c_void_p,
                                           c_char_p, c_int)

    libcrypto.EVP_CipherFinal_ex.argtypes = (c_void_p, c_void_p, c_void_p)

    try:
        libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p, )
        ctx_cleanup = libcrypto.EVP_CIPHER_CTX_cleanup
    except AttributeError:
        libcrypto.EVP_CIPHER_CTX_reset.argtypes = (c_void_p, )
        ctx_cleanup = libcrypto.EVP_CIPHER_CTX_reset
    libcrypto.EVP_CIPHER_CTX_free.argtypes = (c_void_p, )
    if hasattr(libcrypto, 'OpenSSL_add_all_ciphers'):
        libcrypto.OpenSSL_add_all_ciphers()

    buf = create_string_buffer(buf_size)
    loaded = True
Beispiel #3
0
def load_openssl():
    global loaded, libcrypto, buf

    libcrypto = util.find_library(('crypto', 'eay32'), 'EVP_get_cipherbyname',
                                  'libcrypto')
    if libcrypto is None:
        raise Exception('libcrypto(OpenSSL) not found')

    libcrypto.EVP_get_cipherbyname.restype = c_void_p
    libcrypto.EVP_CIPHER_CTX_new.restype = c_void_p

    libcrypto.EVP_CipherInit_ex.argtypes = (c_void_p, c_void_p, c_char_p,
                                            c_char_p, c_char_p, c_int)

    libcrypto.EVP_CipherUpdate.argtypes = (c_void_p, c_void_p, c_void_p,
                                           c_char_p, c_int)

    if hasattr(libcrypto, "EVP_CIPHER_CTX_cleanup"):
        libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p, )
    else:
        libcrypto.EVP_CIPHER_CTX_reset.argtypes = (c_void_p, )
    libcrypto.EVP_CIPHER_CTX_free.argtypes = (c_void_p, )

    libcrypto.RAND_bytes.restype = c_int
    libcrypto.RAND_bytes.argtypes = (c_void_p, c_int)

    if hasattr(libcrypto, 'OpenSSL_add_all_ciphers'):
        libcrypto.OpenSSL_add_all_ciphers()

    buf = create_string_buffer(buf_size)
    loaded = True
Beispiel #4
0
def load_openssl():
    global loaded, libcrypto, buf

    libcrypto = util.find_library(('crypto', 'eay32'),
                                  'EVP_get_cipherbyname',
                                  'libcrypto')
    if libcrypto is None:
        raise Exception('libcrypto(OpenSSL) not found')

    libcrypto.EVP_get_cipherbyname.restype = c_void_p
    libcrypto.EVP_CIPHER_CTX_new.restype = c_void_p

    libcrypto.EVP_CipherInit_ex.argtypes = (c_void_p, c_void_p, c_char_p,
                                            c_char_p, c_char_p, c_int)

    libcrypto.EVP_CipherUpdate.argtypes = (c_void_p, c_void_p, c_void_p,
                                           c_char_p, c_int)

    libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p,)
    libcrypto.EVP_CIPHER_CTX_free.argtypes = (c_void_p,)
    if hasattr(libcrypto, 'OpenSSL_add_all_ciphers'):
        libcrypto.OpenSSL_add_all_ciphers()

    buf = create_string_buffer(buf_size)
    loaded = True
Beispiel #5
0
def load_sodium(path=None):
    """
    Load libsodium helpers for nonce increment
    :return: None
    """
    global libsodium, sodium_loaded

    libsodium = util.find_library('sodium', 'sodium_increment',
                                  'libsodium', path)
    if libsodium is None:
        print('load libsodium failed with path %s' % path)
        return

    if libsodium.sodium_init() < 0:
        libsodium = None
        print('sodium init failed')
        return

    libsodium.sodium_increment.restype = c_void_p
    libsodium.sodium_increment.argtypes = (
        c_void_p, c_int
    )

    sodium_loaded = True
    return
Beispiel #6
0
def load_openssl(crypto_path=None):
    global loaded, libcrypto, libsodium, buf, ctx_cleanup

    crypto_path = dict(crypto_path) if crypto_path else dict()
    path = crypto_path.get('openssl', None)
    libcrypto = util.find_library(('crypto', 'eay32'),
                                  'EVP_get_cipherbyname',
                                  'libcrypto', path)
    if libcrypto is None:
        raise Exception('libcrypto(OpenSSL) not found with path %s' % path)

    libcrypto.EVP_get_cipherbyname.restype = c_void_p
    libcrypto.EVP_CIPHER_CTX_new.restype = c_void_p

    libcrypto.EVP_CipherInit_ex.argtypes = (c_void_p, c_void_p, c_char_p,
                                            c_char_p, c_char_p, c_int)
    libcrypto.EVP_CIPHER_CTX_ctrl.argtypes = (c_void_p, c_int, c_int, c_void_p)

    libcrypto.EVP_CipherUpdate.argtypes = (c_void_p, c_void_p, c_void_p,
                                           c_char_p, c_int)

    libcrypto.EVP_CipherFinal_ex.argtypes = (c_void_p, c_void_p, c_void_p)

    try:
        libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p,)
        ctx_cleanup = libcrypto.EVP_CIPHER_CTX_cleanup
    except AttributeError:
        libcrypto.EVP_CIPHER_CTX_reset.argtypes = (c_void_p,)
        ctx_cleanup = libcrypto.EVP_CIPHER_CTX_reset
    libcrypto.EVP_CIPHER_CTX_free.argtypes = (c_void_p,)
    if hasattr(libcrypto, 'OpenSSL_add_all_ciphers'):
        libcrypto.OpenSSL_add_all_ciphers()

    buf = create_string_buffer(buf_size)
    loaded = True
Beispiel #7
0
def load_libsodium():
    global loaded, libsodium, buf

    libsodium = util.find_library('sodium', 'crypto_stream_salsa20_xor_ic',
                                  'libsodium')
    if libsodium is None:
        raise Exception('libsodium not found')

    libsodium.crypto_stream_salsa20_xor_ic.restype = c_int
    libsodium.crypto_stream_salsa20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                       c_ulonglong,
                                                       c_char_p, c_ulonglong,
                                                       c_char_p)
    libsodium.crypto_stream_chacha20_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                        c_ulonglong,
                                                        c_char_p, c_ulonglong,
                                                        c_char_p)

    try:
        libsodium.crypto_stream_chacha20_ietf_xor_ic.restype = c_int
        libsodium.crypto_stream_chacha20_ietf_xor_ic.argtypes = (c_void_p, c_char_p,
                                                        c_ulonglong,
                                                        c_char_p, c_ulong,
                                                        c_char_p)
    except:
        pass

    buf = create_string_buffer(buf_size)
    loaded = True
Beispiel #8
0
def load_libsodium():
    global loaded, libsodium, buf

    libsodium = util.find_library('sodium', 'crypto_stream_salsa20_xor_ic',
                                  'libsodium')
    if libsodium is None:
        raise Exception('libsodium not found')

    libsodium.crypto_stream_salsa20_xor_ic.restype = c_int
    libsodium.crypto_stream_salsa20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                       c_ulonglong, c_char_p,
                                                       c_ulonglong, c_char_p)
    libsodium.crypto_stream_chacha20_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                        c_ulonglong, c_char_p,
                                                        c_ulonglong, c_char_p)

    try:
        libsodium.crypto_stream_chacha20_ietf_xor_ic.restype = c_int
        libsodium.crypto_stream_chacha20_ietf_xor_ic.argtypes = (c_void_p,
                                                                 c_char_p,
                                                                 c_ulonglong,
                                                                 c_char_p,
                                                                 c_ulong,
                                                                 c_char_p)
    except:
        logging.info("ChaCha20 IETF not support.")
        pass

    try:
        libsodium.crypto_stream_xsalsa20_xor_ic.restype = c_int
        libsodium.crypto_stream_xsalsa20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                            c_ulonglong,
                                                            c_char_p,
                                                            c_ulonglong,
                                                            c_char_p)
    except:
        logging.info("XSalsa20 not support.")
        pass

    try:
        libsodium.crypto_stream_xchacha20_xor_ic.restype = c_int
        libsodium.crypto_stream_xchacha20_xor_ic.argtypes = (c_void_p,
                                                             c_char_p,
                                                             c_ulonglong,
                                                             c_char_p,
                                                             c_ulonglong,
                                                             c_char_p)
    except:
        logging.info(
            "XChaCha20 not support. XChaCha20 only support since libsodium v1.0.12"
        )
        pass

    buf = create_string_buffer(buf_size)
    loaded = True
Beispiel #9
0
def load_sodium():
    """
    Load libsodium helpers for nonce increment
    :return: None
    """
    global libsodium, sodium_loaded

    libsodium = util.find_library('sodium', 'sodium_increment', 'libsodium')
    if libsodium is None:
        return

    if libsodium.sodium_init() < 0:
        libsodium = None
        return

    libsodium.sodium_increment.restype = c_void_p
    libsodium.sodium_increment.argtypes = (c_void_p, c_int)

    sodium_loaded = True
    return
Beispiel #10
0
def load_mbedtls(crypto_path=None):
    global loaded, libmbedtls, buf

    crypto_path = dict(crypto_path) if crypto_path else dict()
    path = crypto_path.get('mbedtls', None)
    libmbedtls = util.find_library('mbedcrypto',
                                   'mbedtls_cipher_init',
                                   'libmbedcrypto', path)
    if libmbedtls is None:
        raise Exception('libmbedcrypto(mbedtls) not found with path %s'
                        % path)

    libmbedtls.mbedtls_cipher_init.restype = None
    libmbedtls.mbedtls_cipher_free.restype = None

    libmbedtls.mbedtls_cipher_info_from_string.restype = c_void_p
    libmbedtls.mbedtls_cipher_info_from_string.argtypes = (c_char_p,)

    libmbedtls.mbedtls_cipher_setup.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_setup.argtypes = (c_void_p, c_void_p)

    libmbedtls.mbedtls_cipher_setkey.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_setkey.argtypes = (
        c_void_p,  # ctx
        c_char_p,  # key
        c_int,     # key_bitlen, not bytes
        c_int      # op: 1 enc, 0 dec, -1 none
    )

    libmbedtls.mbedtls_cipher_set_iv.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_set_iv.argtypes = (
        c_void_p,  # ctx
        c_char_p,  # iv
        c_size_t   # iv_len
    )

    libmbedtls.mbedtls_cipher_reset.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_reset.argtypes = (c_void_p,)  # ctx

    if hasattr(libmbedtls, 'mbedtls_cipher_update_ad'):
        libmbedtls.mbedtls_cipher_update_ad.restype = c_int  # 0 on success
        libmbedtls.mbedtls_cipher_update_ad.argtypes = (
            c_void_p,  # ctx
            c_char_p,  # ad
            c_size_t   # ad_len
        )

    libmbedtls.mbedtls_cipher_update.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_update.argtypes = (
        c_void_p,  # ctx
        c_char_p,  # input
        c_size_t,  # ilen, must be multiple of block size except last one
        c_void_p,  # *output
        c_void_p   # *olen
    )

    libmbedtls.mbedtls_cipher_finish.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_finish.argtypes = (
        c_void_p,  # ctx
        c_void_p,  # *output
        c_void_p   # *olen
    )

    if hasattr(libmbedtls, 'mbedtls_cipher_write_tag'):
        libmbedtls.mbedtls_cipher_write_tag.restype = c_int  # 0 on success
        libmbedtls.mbedtls_cipher_write_tag.argtypes = (
            c_void_p,  # ctx
            c_void_p,  # *tag
            c_size_t   # tag_len
        )
        libmbedtls.mbedtls_cipher_check_tag.restype = c_int  # 0 on success
        libmbedtls.mbedtls_cipher_check_tag.argtypes = (
            c_void_p,  # ctx
            c_char_p,  # tag
            c_size_t   # tag_len
        )

    libmbedtls.mbedtls_cipher_crypt.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_crypt.argtypes = (
        c_void_p,  # ctx
        c_char_p,  # iv
        c_size_t,  # iv_len, = 0 if iv = NULL
        c_char_p,  # input
        c_size_t,  # ilen
        c_void_p,  # *output, no less than ilen + block_size
        c_void_p   # *olen
    )

    if hasattr(libmbedtls, 'mbedtls_cipher_auth_encrypt'):
        libmbedtls.mbedtls_cipher_auth_encrypt.restype = c_int  # 0 on success
        libmbedtls.mbedtls_cipher_auth_encrypt.argtypes = (
            c_void_p,  # ctx
            c_char_p,  # iv
            c_size_t,  # iv_len
            c_char_p,  # ad
            c_size_t,  # ad_len
            c_char_p,  # input
            c_size_t,  # ilen
            c_void_p,  # *output, no less than ilen + block_size
            c_void_p,  # *olen
            c_void_p,  # *tag
            c_size_t   # tag_len
        )
        libmbedtls.mbedtls_cipher_auth_decrypt.restype = c_int  # 0 on success
        libmbedtls.mbedtls_cipher_auth_decrypt.argtypes = (
            c_void_p,  # ctx
            c_char_p,  # iv
            c_size_t,  # iv_len
            c_char_p,  # ad
            c_size_t,  # ad_len
            c_char_p,  # input
            c_size_t,  # ilen
            c_void_p,  # *output, no less than ilen + block_size
            c_void_p,  # *olen
            c_char_p,  # tag
            c_size_t,  # tag_len
        )

    buf = create_string_buffer(buf_size)
    loaded = True
Beispiel #11
0
def load_libsodium(crypto_path=None):
    global loaded, libsodium, buf

    crypto_path = dict(crypto_path) if crypto_path else dict()
    path = crypto_path.get('sodium', None)

    if not aead.sodium_loaded:
        aead.load_sodium(path)

    if aead.sodium_loaded:
        libsodium = aead.libsodium
    else:
        print('load libsodium again with path %s' % path)
        libsodium = util.find_library('sodium', 'crypto_stream_salsa20_xor_ic',
                                      'libsodium', path)
        if libsodium is None:
            raise Exception('libsodium not found')

        if libsodium.sodium_init() < 0:
            raise Exception('libsodium init failed')

    libsodium.crypto_stream_salsa20_xor_ic.restype = c_int
    libsodium.crypto_stream_salsa20_xor_ic.argtypes = (
        c_void_p,
        c_char_p,  # cipher output, msg
        c_ulonglong,  # msg len
        c_char_p,
        c_ulonglong,  # nonce, uint64_t initial block counter
        c_char_p  # key
    )
    libsodium.crypto_stream_chacha20_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                        c_ulonglong, c_char_p,
                                                        c_ulonglong, c_char_p)
    if hasattr(libsodium, 'crypto_stream_xchacha20_xor_ic'):
        libsodium.crypto_stream_xchacha20_xor_ic.restype = c_int
        libsodium.crypto_stream_xchacha20_xor_ic.argtypes = (c_void_p,
                                                             c_char_p,
                                                             c_ulonglong,
                                                             c_char_p,
                                                             c_ulonglong,
                                                             c_char_p)
    libsodium.crypto_stream_chacha20_ietf_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_ietf_xor_ic.argtypes = (
        c_void_p,
        c_char_p,
        c_ulonglong,
        c_char_p,
        c_uint,  # uint32_t initial counter
        c_char_p)

    # chacha20-poly1305
    libsodium.crypto_aead_chacha20poly1305_encrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_encrypt.argtypes = (
        c_void_p,
        c_void_p,  # c, clen
        c_char_p,
        c_ulonglong,  # m, mlen
        c_char_p,
        c_ulonglong,  # ad, adlen
        c_char_p,  # nsec, not used
        c_char_p,
        c_char_p  # npub, k
    )
    libsodium.crypto_aead_chacha20poly1305_decrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_decrypt.argtypes = (
        c_void_p,
        c_void_p,  # m, mlen
        c_char_p,  # nsec, not used
        c_char_p,
        c_ulonglong,  # c, clen
        c_char_p,
        c_ulonglong,  # ad, adlen
        c_char_p,
        c_char_p  # npub, k
    )

    # chacha20-ietf-poly1305, same api structure as above
    libsodium.crypto_aead_chacha20poly1305_ietf_encrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_ietf_encrypt.argtypes = (
        c_void_p, c_void_p, c_char_p, c_ulonglong, c_char_p, c_ulonglong,
        c_char_p, c_char_p, c_char_p)
    libsodium.crypto_aead_chacha20poly1305_ietf_decrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_ietf_decrypt.argtypes = (
        c_void_p, c_void_p, c_char_p, c_char_p, c_ulonglong, c_char_p,
        c_ulonglong, c_char_p, c_char_p)

    # xchacha20-ietf-poly1305, same api structure as above
    if hasattr(libsodium, 'crypto_aead_xchacha20poly1305_ietf_encrypt'):
        libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt.restype = c_int
        libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt.argtypes = (
            c_void_p, c_void_p, c_char_p, c_ulonglong, c_char_p, c_ulonglong,
            c_char_p, c_char_p, c_char_p)

        libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt.restype = c_int
        libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt.argtypes = (
            c_void_p, c_void_p, c_char_p, c_char_p, c_ulonglong, c_char_p,
            c_ulonglong, c_char_p, c_char_p)

    # aes-256-gcm, same api structure as above
    libsodium.crypto_aead_aes256gcm_is_available.restype = c_int

    if libsodium.crypto_aead_aes256gcm_is_available():
        libsodium.crypto_aead_aes256gcm_encrypt.restype = c_int
        libsodium.crypto_aead_aes256gcm_encrypt.argtypes = (c_void_p, c_void_p,
                                                            c_char_p,
                                                            c_ulonglong,
                                                            c_char_p,
                                                            c_ulonglong,
                                                            c_char_p, c_char_p,
                                                            c_char_p)
        libsodium.crypto_aead_aes256gcm_decrypt.restype = c_int
        libsodium.crypto_aead_aes256gcm_decrypt.argtypes = (c_void_p, c_void_p,
                                                            c_char_p, c_char_p,
                                                            c_ulonglong,
                                                            c_char_p,
                                                            c_ulonglong,
                                                            c_char_p, c_char_p)

    buf = create_string_buffer(buf_size)
    loaded = True
Beispiel #12
0
def load_libsodium():
    global loaded, libsodium, buf

    libsodium = util.find_library('sodium', 'crypto_stream_salsa20_xor_ic',
                                  'libsodium')
    if libsodium is None:
        raise Exception('libsodium not found')

    libsodium.crypto_stream_salsa20_xor_ic.restype = c_int
    libsodium.crypto_stream_salsa20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                       c_ulonglong, c_char_p,
                                                       c_ulonglong, c_char_p)
    libsodium.crypto_stream_chacha20_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                        c_ulonglong, c_char_p,
                                                        c_ulonglong, c_char_p)
    libsodium.crypto_stream_chacha20_ietf_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_ietf_xor_ic.argtypes = (c_void_p,
                                                             c_char_p,
                                                             c_ulonglong,
                                                             c_char_p, c_ulong,
                                                             c_char_p)

    # chacha20-poly1305
    libsodium.crypto_aead_chacha20poly1305_encrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_encrypt.argtypes = (
        c_void_p,
        c_void_p,  # c, clen
        c_char_p,
        c_ulonglong,  # m, mlen
        c_char_p,
        c_ulonglong,  # ad, adlen
        c_char_p,  # nsec, not used
        c_char_p,
        c_char_p  # npub, k
    )
    libsodium.crypto_aead_chacha20poly1305_decrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_decrypt.argtypes = (
        c_void_p,
        c_void_p,  # m, mlen
        c_char_p,  # nsec, not used
        c_char_p,
        c_ulonglong,  # c, clen
        c_char_p,
        c_ulonglong,  # ad, adlen
        c_char_p,
        c_char_p  # npub, k
    )

    # chacha20-ietf-poly1305, same api structure as above
    libsodium.crypto_aead_chacha20poly1305_ietf_encrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_ietf_encrypt.argtypes = (
        c_void_p, c_void_p, c_char_p, c_ulonglong, c_char_p, c_ulonglong,
        c_char_p, c_char_p, c_char_p)
    libsodium.crypto_aead_chacha20poly1305_ietf_decrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_ietf_decrypt.argtypes = (
        c_void_p, c_void_p, c_char_p, c_char_p, c_ulonglong, c_char_p,
        c_ulonglong, c_char_p, c_char_p)

    # xchacha20-ietf-poly1305, same api structure as above
    if hasattr(libsodium, 'crypto_aead_xchacha20poly1305_ietf_encrypt'):
        libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt.restype = c_int
        libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt.argtypes = (
            c_void_p, c_void_p, c_char_p, c_ulonglong, c_char_p, c_ulonglong,
            c_char_p, c_char_p, c_char_p)

        libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt.restype = c_int
        libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt.argtypes = (
            c_void_p, c_void_p, c_char_p, c_char_p, c_ulonglong, c_char_p,
            c_ulonglong, c_char_p, c_char_p)

    libsodium.sodium_increment.restype = c_void_p
    libsodium.sodium_increment.argtypes = (c_void_p, c_int)

    buf = create_string_buffer(buf_size)
    loaded = True
Beispiel #13
0
def load_mbedtls(crypto_path=None):
    global loaded, libmbedtls, buf

    crypto_path = dict(crypto_path) if crypto_path else dict()
    path = crypto_path.get('mbedtls', None)
    libmbedtls = util.find_library('mbedcrypto', 'mbedtls_cipher_init',
                                   'libmbedcrypto', path)
    if libmbedtls is None:
        raise Exception('libmbedcrypto(mbedtls) not found with path %s' % path)

    libmbedtls.mbedtls_cipher_init.restype = None
    libmbedtls.mbedtls_cipher_free.restype = None

    libmbedtls.mbedtls_cipher_info_from_string.restype = c_void_p
    libmbedtls.mbedtls_cipher_info_from_string.argtypes = (c_char_p, )

    libmbedtls.mbedtls_cipher_setup.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_setup.argtypes = (c_void_p, c_void_p)

    libmbedtls.mbedtls_cipher_setkey.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_setkey.argtypes = (
        c_void_p,  # ctx
        c_char_p,  # key
        c_int,  # key_bitlen, not bytes
        c_int  # op: 1 enc, 0 dec, -1 none
    )

    libmbedtls.mbedtls_cipher_set_iv.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_set_iv.argtypes = (
        c_void_p,  # ctx
        c_char_p,  # iv
        c_size_t  # iv_len
    )

    libmbedtls.mbedtls_cipher_reset.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_reset.argtypes = (c_void_p, )  # ctx

    if hasattr(libmbedtls, 'mbedtls_cipher_update_ad'):
        libmbedtls.mbedtls_cipher_update_ad.restype = c_int  # 0 on success
        libmbedtls.mbedtls_cipher_update_ad.argtypes = (
            c_void_p,  # ctx
            c_char_p,  # ad
            c_size_t  # ad_len
        )

    libmbedtls.mbedtls_cipher_update.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_update.argtypes = (
        c_void_p,  # ctx
        c_char_p,  # input
        c_size_t,  # ilen, must be multiple of block size except last one
        c_void_p,  # *output
        c_void_p  # *olen
    )

    libmbedtls.mbedtls_cipher_finish.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_finish.argtypes = (
        c_void_p,  # ctx
        c_void_p,  # *output
        c_void_p  # *olen
    )

    if hasattr(libmbedtls, 'mbedtls_cipher_write_tag'):
        libmbedtls.mbedtls_cipher_write_tag.restype = c_int  # 0 on success
        libmbedtls.mbedtls_cipher_write_tag.argtypes = (
            c_void_p,  # ctx
            c_void_p,  # *tag
            c_size_t  # tag_len
        )
        libmbedtls.mbedtls_cipher_check_tag.restype = c_int  # 0 on success
        libmbedtls.mbedtls_cipher_check_tag.argtypes = (
            c_void_p,  # ctx
            c_char_p,  # tag
            c_size_t  # tag_len
        )

    libmbedtls.mbedtls_cipher_crypt.restype = c_int  # 0 on success
    libmbedtls.mbedtls_cipher_crypt.argtypes = (
        c_void_p,  # ctx
        c_char_p,  # iv
        c_size_t,  # iv_len, = 0 if iv = NULL
        c_char_p,  # input
        c_size_t,  # ilen
        c_void_p,  # *output, no less than ilen + block_size
        c_void_p  # *olen
    )

    if hasattr(libmbedtls, 'mbedtls_cipher_auth_encrypt'):
        libmbedtls.mbedtls_cipher_auth_encrypt.restype = c_int  # 0 on success
        libmbedtls.mbedtls_cipher_auth_encrypt.argtypes = (
            c_void_p,  # ctx
            c_char_p,  # iv
            c_size_t,  # iv_len
            c_char_p,  # ad
            c_size_t,  # ad_len
            c_char_p,  # input
            c_size_t,  # ilen
            c_void_p,  # *output, no less than ilen + block_size
            c_void_p,  # *olen
            c_void_p,  # *tag
            c_size_t  # tag_len
        )
        libmbedtls.mbedtls_cipher_auth_decrypt.restype = c_int  # 0 on success
        libmbedtls.mbedtls_cipher_auth_decrypt.argtypes = (
            c_void_p,  # ctx
            c_char_p,  # iv
            c_size_t,  # iv_len
            c_char_p,  # ad
            c_size_t,  # ad_len
            c_char_p,  # input
            c_size_t,  # ilen
            c_void_p,  # *output, no less than ilen + block_size
            c_void_p,  # *olen
            c_char_p,  # tag
            c_size_t,  # tag_len
        )

    buf = create_string_buffer(buf_size)
    loaded = True
Beispiel #14
0
def load_libsodium():
    global loaded, libsodium, buf

    if not aead.sodium_loaded:
        aead.load_sodium()

    if aead.sodium_loaded:
        libsodium = aead.libsodium
    else:
        print('load libsodium again')
        libsodium = util.find_library('sodium', 'crypto_stream_salsa20_xor_ic',
                                      'libsodium')
        if libsodium is None:
            raise Exception('libsodium not found')

        if libsodium.sodium_init() < 0:
            raise Exception('libsodium init failed')

    libsodium.crypto_stream_salsa20_xor_ic.restype = c_int
    libsodium.crypto_stream_salsa20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                       c_ulonglong,
                                                       c_char_p, c_ulonglong,
                                                       c_char_p)
    libsodium.crypto_stream_chacha20_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                        c_ulonglong,
                                                        c_char_p, c_ulonglong,
                                                        c_char_p)
    libsodium.crypto_stream_chacha20_ietf_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_ietf_xor_ic.argtypes = (c_void_p,
                                                             c_char_p,
                                                             c_ulonglong,
                                                             c_char_p,
                                                             c_ulong,
                                                             c_char_p)

    # chacha20-poly1305
    libsodium.crypto_aead_chacha20poly1305_encrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_encrypt.argtypes = (
        c_void_p, c_void_p,     # c, clen
        c_char_p, c_ulonglong,  # m, mlen
        c_char_p, c_ulonglong,  # ad, adlen
        c_char_p,               # nsec, not used
        c_char_p, c_char_p      # npub, k
    )
    libsodium.crypto_aead_chacha20poly1305_decrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_decrypt.argtypes = (
        c_void_p, c_void_p,     # m, mlen
        c_char_p,               # nsec, not used
        c_char_p, c_ulonglong,  # c, clen
        c_char_p, c_ulonglong,  # ad, adlen
        c_char_p, c_char_p      # npub, k
    )

    # chacha20-ietf-poly1305, same api structure as above
    libsodium.crypto_aead_chacha20poly1305_ietf_encrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_ietf_encrypt.argtypes = (
        c_void_p, c_void_p,
        c_char_p, c_ulonglong,
        c_char_p, c_ulonglong,
        c_char_p,
        c_char_p, c_char_p
    )
    libsodium.crypto_aead_chacha20poly1305_ietf_decrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_ietf_decrypt.argtypes = (
        c_void_p, c_void_p,
        c_char_p,
        c_char_p, c_ulonglong,
        c_char_p, c_ulonglong,
        c_char_p, c_char_p
    )

    # xchacha20-ietf-poly1305, same api structure as above
    if hasattr(libsodium, 'crypto_aead_xchacha20poly1305_ietf_encrypt'):
        libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt.restype = c_int
        libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt.argtypes = (
            c_void_p, c_void_p,
            c_char_p, c_ulonglong,
            c_char_p, c_ulonglong,
            c_char_p,
            c_char_p, c_char_p
        )

        libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt.restype = c_int
        libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt.argtypes = (
            c_void_p, c_void_p,
            c_char_p,
            c_char_p, c_ulonglong,
            c_char_p, c_ulonglong,
            c_char_p, c_char_p
        )

    # aes-256-gcm, same api structure as above
    libsodium.crypto_aead_aes256gcm_is_available.restype = c_int

    if libsodium.crypto_aead_aes256gcm_is_available():
        libsodium.crypto_aead_aes256gcm_encrypt.restype = c_int
        libsodium.crypto_aead_aes256gcm_encrypt.argtypes = (
            c_void_p, c_void_p,
            c_char_p, c_ulonglong,
            c_char_p, c_ulonglong,
            c_char_p,
            c_char_p, c_char_p
        )
        libsodium.crypto_aead_aes256gcm_decrypt.restype = c_int
        libsodium.crypto_aead_aes256gcm_decrypt.argtypes = (
            c_void_p, c_void_p,
            c_char_p,
            c_char_p, c_ulonglong,
            c_char_p, c_ulonglong,
            c_char_p, c_char_p
        )

    buf = create_string_buffer(buf_size)
    loaded = True
Beispiel #15
0
def load_libsodium(crypto_path=None):
    global loaded, libsodium, buf

    crypto_path = dict(crypto_path) if crypto_path else dict()
    path = crypto_path.get('sodium', None)

    if not aead.sodium_loaded:
        aead.load_sodium(path)

    if aead.sodium_loaded:
        libsodium = aead.libsodium
    else:
        print('load libsodium again with path %s' % path)
        libsodium = util.find_library('sodium', 'crypto_stream_salsa20_xor_ic',
                                      'libsodium', path)
        if libsodium is None:
            raise Exception('libsodium not found')

        if libsodium.sodium_init() < 0:
            raise Exception('libsodium init failed')

    libsodium.crypto_stream_salsa20_xor_ic.restype = c_int
    libsodium.crypto_stream_salsa20_xor_ic.argtypes = (
        c_void_p, c_char_p,  # cipher output, msg
        c_ulonglong,  # msg len
        c_char_p, c_ulonglong,  # nonce, uint64_t initial block counter
        c_char_p  # key
    )
    libsodium.crypto_stream_chacha20_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_xor_ic.argtypes = (
        c_void_p, c_char_p,
        c_ulonglong,
        c_char_p, c_ulonglong,
        c_char_p
    )
    if hasattr(libsodium, 'crypto_stream_xchacha20_xor_ic'):
        libsodium.crypto_stream_xchacha20_xor_ic.restype = c_int
        libsodium.crypto_stream_xchacha20_xor_ic.argtypes = (
            c_void_p, c_char_p,
            c_ulonglong,
            c_char_p, c_ulonglong,
            c_char_p
        )
    libsodium.crypto_stream_chacha20_ietf_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_ietf_xor_ic.argtypes = (
        c_void_p, c_char_p,
        c_ulonglong,
        c_char_p,
        c_uint,  # uint32_t initial counter
        c_char_p
    )

    # chacha20-poly1305
    libsodium.crypto_aead_chacha20poly1305_encrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_encrypt.argtypes = (
        c_void_p, c_void_p,  # c, clen
        c_char_p, c_ulonglong,  # m, mlen
        c_char_p, c_ulonglong,  # ad, adlen
        c_char_p,  # nsec, not used
        c_char_p, c_char_p  # npub, k
    )
    libsodium.crypto_aead_chacha20poly1305_decrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_decrypt.argtypes = (
        c_void_p, c_void_p,  # m, mlen
        c_char_p,  # nsec, not used
        c_char_p, c_ulonglong,  # c, clen
        c_char_p, c_ulonglong,  # ad, adlen
        c_char_p, c_char_p  # npub, k
    )

    # chacha20-ietf-poly1305, same api structure as above
    libsodium.crypto_aead_chacha20poly1305_ietf_encrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_ietf_encrypt.argtypes = (
        c_void_p, c_void_p,
        c_char_p, c_ulonglong,
        c_char_p, c_ulonglong,
        c_char_p,
        c_char_p, c_char_p
    )
    libsodium.crypto_aead_chacha20poly1305_ietf_decrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_ietf_decrypt.argtypes = (
        c_void_p, c_void_p,
        c_char_p,
        c_char_p, c_ulonglong,
        c_char_p, c_ulonglong,
        c_char_p, c_char_p
    )

    # xchacha20-ietf-poly1305, same api structure as above
    if hasattr(libsodium, 'crypto_aead_xchacha20poly1305_ietf_encrypt'):
        libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt.restype = c_int
        libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt.argtypes = (
            c_void_p, c_void_p,
            c_char_p, c_ulonglong,
            c_char_p, c_ulonglong,
            c_char_p,
            c_char_p, c_char_p
        )

        libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt.restype = c_int
        libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt.argtypes = (
            c_void_p, c_void_p,
            c_char_p,
            c_char_p, c_ulonglong,
            c_char_p, c_ulonglong,
            c_char_p, c_char_p
        )

    # aes-256-gcm, same api structure as above
    libsodium.crypto_aead_aes256gcm_is_available.restype = c_int

    if libsodium.crypto_aead_aes256gcm_is_available():
        libsodium.crypto_aead_aes256gcm_encrypt.restype = c_int
        libsodium.crypto_aead_aes256gcm_encrypt.argtypes = (
            c_void_p, c_void_p,
            c_char_p, c_ulonglong,
            c_char_p, c_ulonglong,
            c_char_p,
            c_char_p, c_char_p
        )
        libsodium.crypto_aead_aes256gcm_decrypt.restype = c_int
        libsodium.crypto_aead_aes256gcm_decrypt.argtypes = (
            c_void_p, c_void_p,
            c_char_p,
            c_char_p, c_ulonglong,
            c_char_p, c_ulonglong,
            c_char_p, c_char_p
        )

    buf = create_string_buffer(buf_size)
    loaded = True
Beispiel #16
0
def load_libsodium():
    global loaded, libsodium, buf

    if not aead.sodium_loaded:
        aead.load_sodium()

    if aead.sodium_loaded:
        libsodium = aead.libsodium
    else:
        print('load libsodium again')
        libsodium = util.find_library('sodium', 'crypto_stream_salsa20_xor_ic',
                                      'libsodium')
        if libsodium is None:
            raise Exception('libsodium not found')

        if libsodium.sodium_init() < 0:
            raise Exception('libsodium init failed')

    libsodium.crypto_stream_salsa20_xor_ic.restype = c_int
    libsodium.crypto_stream_salsa20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                       c_ulonglong,
                                                       c_char_p, c_ulonglong,
                                                       c_char_p)
    libsodium.crypto_stream_chacha20_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                        c_ulonglong,
                                                        c_char_p, c_ulonglong,
                                                        c_char_p)
    libsodium.crypto_stream_chacha20_ietf_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_ietf_xor_ic.argtypes = (c_void_p,
                                                             c_char_p,
                                                             c_ulonglong,
                                                             c_char_p,
                                                             c_ulong,
                                                             c_char_p)

    # chacha20-poly1305
    libsodium.crypto_aead_chacha20poly1305_encrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_encrypt.argtypes = (
        c_void_p, c_void_p,     # c, clen
        c_char_p, c_ulonglong,  # m, mlen
        c_char_p, c_ulonglong,  # ad, adlen
        c_char_p,               # nsec, not used
        c_char_p, c_char_p      # npub, k
    )
    libsodium.crypto_aead_chacha20poly1305_decrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_decrypt.argtypes = (
        c_void_p, c_void_p,     # m, mlen
        c_char_p,               # nsec, not used
        c_char_p, c_ulonglong,  # c, clen
        c_char_p, c_ulonglong,  # ad, adlen
        c_char_p, c_char_p      # npub, k
    )

    # chacha20-ietf-poly1305, same api structure as above
    libsodium.crypto_aead_chacha20poly1305_ietf_encrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_ietf_encrypt.argtypes = (
        c_void_p, c_void_p,
        c_char_p, c_ulonglong,
        c_char_p, c_ulonglong,
        c_char_p,
        c_char_p, c_char_p
    )
    libsodium.crypto_aead_chacha20poly1305_ietf_decrypt.restype = c_int
    libsodium.crypto_aead_chacha20poly1305_ietf_decrypt.argtypes = (
        c_void_p, c_void_p,
        c_char_p,
        c_char_p, c_ulonglong,
        c_char_p, c_ulonglong,
        c_char_p, c_char_p
    )

    # xchacha20-ietf-poly1305, same api structure as above
    if hasattr(libsodium, 'crypto_aead_xchacha20poly1305_ietf_encrypt'):
        libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt.restype = c_int
        libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt.argtypes = (
            c_void_p, c_void_p,
            c_char_p, c_ulonglong,
            c_char_p, c_ulonglong,
            c_char_p,
            c_char_p, c_char_p
        )

        libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt.restype = c_int
        libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt.argtypes = (
            c_void_p, c_void_p,
            c_char_p,
            c_char_p, c_ulonglong,
            c_char_p, c_ulonglong,
            c_char_p, c_char_p
        )

    # aes-256-gcm, same api structure as above
    libsodium.crypto_aead_aes256gcm_is_available.restype = c_int

    if libsodium.crypto_aead_aes256gcm_is_available():
        libsodium.crypto_aead_aes256gcm_encrypt.restype = c_int
        libsodium.crypto_aead_aes256gcm_encrypt.argtypes = (
            c_void_p, c_void_p,
            c_char_p, c_ulonglong,
            c_char_p, c_ulonglong,
            c_char_p,
            c_char_p, c_char_p
        )
        libsodium.crypto_aead_aes256gcm_decrypt.restype = c_int
        libsodium.crypto_aead_aes256gcm_decrypt.argtypes = (
            c_void_p, c_void_p,
            c_char_p,
            c_char_p, c_ulonglong,
            c_char_p, c_ulonglong,
            c_char_p, c_char_p
        )

    buf = create_string_buffer(buf_size)
    loaded = True