toxygen_wrapper/tox_wrapper/toxencryptsave.py

92 lines
4.6 KiB
Python
Raw Normal View History

2022-09-24 06:00:32 +02:00
# -*- mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*-
2022-09-25 09:21:16 +02:00
try:
2023-12-17 00:23:00 +01:00
from tox_wrapper import libtox
import tox_wrapper.toxencryptsave_enums_and_consts as enum
2022-09-25 09:21:16 +02:00
except:
import libtox
2023-12-11 16:06:07 +01:00
import toxencryptsave_enums_and_consts as enum
2022-09-24 06:00:32 +02:00
2023-12-15 03:26:51 +01:00
from typing import Union, Callable
2022-11-17 15:18:51 +01:00
from ctypes import (ArgumentError, byref, c_bool, c_char_p, c_int, c_size_t,
2023-12-15 03:26:51 +01:00
create_string_buffer, Array)
2023-12-17 00:23:00 +01:00
def ToxError(ArgumentError): pass
2022-10-29 20:44:01 +02:00
2022-09-24 06:00:32 +02:00
class ToxEncryptSave:
def __init__(self):
self.libtoxencryptsave = libtox.LibToxEncryptSave()
2023-12-11 15:37:04 +01:00
def is_data_encrypted(self, data: bytes) -> bool:
2022-09-24 06:00:32 +02:00
"""
Checks if given data is encrypted
"""
func = self.libtoxencryptsave.tox_is_data_encrypted
func.restype = c_bool
result = func(c_char_p(bytes(data)))
2023-12-11 15:37:04 +01:00
return bool(result)
2022-09-24 06:00:32 +02:00
2023-12-14 21:46:56 +01:00
def pass_encrypt(self, data: bytes, password: Union[str,bytes]) -> bytes:
2022-09-24 06:00:32 +02:00
"""
Encrypts the given data with the given password.
:return: output array
"""
2023-12-11 16:06:07 +01:00
out = create_string_buffer(len(data) + enum.TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
2022-09-24 06:00:32 +02:00
tox_err_encryption = c_int()
2023-12-14 21:46:56 +01:00
assert password
2023-12-11 15:37:04 +01:00
if type(password) != bytes:
password = bytes(password, 'utf-8')
2022-09-24 06:00:32 +02:00
self.libtoxencryptsave.tox_pass_encrypt(c_char_p(data),
c_size_t(len(data)),
2023-12-11 15:37:04 +01:00
c_char_p(password),
2022-09-24 06:00:32 +02:00
c_size_t(len(password)),
out,
byref(tox_err_encryption))
tox_err_encryption = tox_err_encryption.value
2023-12-11 16:06:07 +01:00
if tox_err_encryption == enum.TOX_ERR_ENCRYPTION['OK']:
2023-12-14 21:46:56 +01:00
return bytes(out[:])
2023-12-13 01:57:28 +01:00
if tox_err_encryption == enum.TOX_ERR_ENCRYPTION['NULL']:
2022-09-24 06:00:32 +02:00
raise ArgumentError('Some input data, or maybe the output pointer, was null.')
2023-12-13 01:57:28 +01:00
if tox_err_encryption == enum.TOX_ERR_ENCRYPTION['KEY_DERIVATION_FAILED']:
2022-09-24 06:00:32 +02:00
raise RuntimeError('The crypto lib was unable to derive a key from the given passphrase, which is usually a'
' lack of memory issue. The functions accepting keys do not produce this error.')
2023-12-13 01:57:28 +01:00
if tox_err_encryption == enum.TOX_ERR_ENCRYPTION['FAILED']:
2022-09-24 06:00:32 +02:00
raise RuntimeError('The encryption itself failed.')
2023-12-13 01:57:28 +01:00
raise ToxError('The function did not return OK.')
2022-09-24 06:00:32 +02:00
2023-12-14 21:46:56 +01:00
def pass_decrypt(self, data: bytes, password: Union[str,bytes]) -> bytes:
2022-09-24 06:00:32 +02:00
"""
Decrypts the given data with the given password.
:return: output array
"""
2023-12-12 06:20:05 +01:00
out = create_string_buffer(len(data) - enum.TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
2022-09-24 06:00:32 +02:00
tox_err_decryption = c_int()
2023-12-14 21:46:56 +01:00
assert password
2023-12-11 16:06:07 +01:00
if type(password) != bytes:
password = bytes(password, 'utf-8')
2022-09-24 06:00:32 +02:00
self.libtoxencryptsave.tox_pass_decrypt(c_char_p(bytes(data)),
c_size_t(len(data)),
2023-12-11 16:06:07 +01:00
c_char_p(password),
2022-09-24 06:00:32 +02:00
c_size_t(len(password)),
out,
byref(tox_err_decryption))
tox_err_decryption = tox_err_decryption.value
2023-12-11 16:06:07 +01:00
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['OK']:
2023-12-11 15:37:04 +01:00
return bytes(out[:])
2023-12-14 21:46:56 +01:00
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['NULL']:
2022-09-24 06:00:32 +02:00
raise ArgumentError('Some input data, or maybe the output pointer, was null.')
2023-12-14 21:46:56 +01:00
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['INVALID_LENGTH']:
2022-09-24 06:00:32 +02:00
raise ArgumentError('The input data was shorter than TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes')
2023-12-14 21:46:56 +01:00
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['BAD_FORMAT']:
2022-09-24 06:00:32 +02:00
raise ArgumentError('The input data is missing the magic number (i.e. wasn\'t created by this module, or is'
' corrupted)')
2023-12-14 21:46:56 +01:00
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['KEY_DERIVATION_FAILED']:
2022-09-24 06:00:32 +02:00
raise RuntimeError('The crypto lib was unable to derive a key from the given passphrase, which is usually a'
' lack of memory issue. The functions accepting keys do not produce this error.')
2023-12-14 21:46:56 +01:00
if tox_err_decryption == enum.TOX_ERR_DECRYPTION['FAILED']:
2022-09-24 06:00:32 +02:00
raise RuntimeError('The encrypted byte array could not be decrypted. Either the data was corrupt or the '
'password/key was incorrect.')
2023-12-14 21:46:56 +01:00
raise ToxError('The function did not return OK.')