Example #1
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
Example #2
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
Example #3
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
Example #4
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