From 2fccf44015dfb27865ddb50ed66afdedbd4e03e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Sun, 8 Jul 2018 12:19:15 +0200 Subject: python: Remove the python bindings. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Damir Jelić --- python/olm/utility.py | 56 --------------------------------------------------- 1 file changed, 56 deletions(-) delete mode 100644 python/olm/utility.py (limited to 'python/olm/utility.py') diff --git a/python/olm/utility.py b/python/olm/utility.py deleted file mode 100644 index dac0225..0000000 --- a/python/olm/utility.py +++ /dev/null @@ -1,56 +0,0 @@ -from ._base import lib, c_void_p, c_size_t, c_char_p, \ - create_string_buffer, ERR, OlmError - -lib.olm_utility_size.argtypes = [] -lib.olm_utility_size.restype = c_size_t - -lib.olm_utility.argtypes = [c_void_p] -lib.olm_utility.restype = c_void_p - -lib.olm_utility_last_error.argtypes = [c_void_p] -lib.olm_utility_last_error.restype = c_char_p - - -def utility_errcheck(res, func, args): - if res == ERR: - raise OlmError("%s: %s" % ( - func.__name__, lib.olm_utility_last_error(args[0]) - )) - return res - - -def utility_function(func, *types): - func.argtypes = (c_void_p,) + types - func.restypes = c_size_t - func.errcheck = utility_errcheck - -utility_function( - lib.olm_ed25519_verify, - c_void_p, c_size_t, # key, key_length - c_void_p, c_size_t, # message, message_length - c_void_p, c_size_t, # signature, signature_length -) - - -class Utility(object): - def __init__(self): - self.buf = create_string_buffer(lib.olm_utility_size()) - self.ptr = lib.olm_utility(self.buf) - -_utility = None - - -def ed25519_verify(key, message, signature): - """ Verify an ed25519 signature. Raises an OlmError if verification fails. - Args: - key(bytes): The ed25519 public key used for signing. - message(bytes): The signed message. - signature(bytes): The message signature. - """ - global _utility - if not _utility: - _utility = Utility() - lib.olm_ed25519_verify(_utility.ptr, - key, len(key), - message, len(message), - signature, len(signature)) -- cgit v1.2.3 From e3d66733712e161d9287ea3f0116e5b57477b0d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Sun, 8 Jul 2018 12:19:16 +0200 Subject: python: Import improved python bindings. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit imports the python bindings from: https://github.com/poljar/python-olm The bindings are imported at commit c44b145818520d69eaaa350fb95afcb846125e0f Minor modifications were made while importing: - Removed travis config - Removed Arch Linux PKGBUILD - Removed the html docs, they can be rebuild by running make html in the docs folder - Slightly modified the README The new bindings feature some improvements over the old ones: - Python 2 and 3 support - Automatic memory management - Automatic memory clearing before it is freed - Type signatures via the python typing module - Full test coverage - Properties are utilized where it makes sense (e.g. account.id) Signed-off-by: Damir Jelić --- python/olm/utility.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 python/olm/utility.py (limited to 'python/olm/utility.py') diff --git a/python/olm/utility.py b/python/olm/utility.py new file mode 100644 index 0000000..838cf3f --- /dev/null +++ b/python/olm/utility.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- +# libolm python bindings +# Copyright © 2015-2017 OpenMarket Ltd +# Copyright © 2018 Damir Jelić +"""libolm Utility module. + +This module contains utilities for olm. +It only contains the ed25519_verify function for signature verification. + +Examples: + >>> alice = Account() + + >>> message = "Test" + >>> signature = alice.sign(message) + >>> signing_key = alice.identity_keys["ed25519"] + + >>> ed25519_verify(signing_key, message, signature) + +""" + +# pylint: disable=redefined-builtin,unused-import +from typing import AnyStr, Type + +# pylint: disable=no-name-in-module +from _libolm import ffi, lib # type: ignore + +from ._compat import to_bytes +from ._finalize import track_for_finalization + + +def _clear_utility(utility): # pragma: no cover + # type: (ffi.cdata) -> None + lib.olm_clear_utility(utility) + + +class OlmVerifyError(Exception): + """libolm signature verification exception.""" + + +class _Utility(object): + # pylint: disable=too-few-public-methods + """libolm Utility class.""" + + _buf = None + _utility = None + + @classmethod + def _allocate(cls): + # type: (Type[_Utility]) -> None + cls._buf = ffi.new("char[]", lib.olm_utility_size()) + cls._utility = lib.olm_utility(cls._buf) + track_for_finalization(cls, cls._utility, _clear_utility) + + @classmethod + def _check_error(cls, ret): + # type: (int) -> None + if ret != lib.olm_error(): + return + + raise OlmVerifyError("{}".format( + ffi.string(lib.olm_utility_last_error( + cls._utility)).decode("utf-8"))) + + @classmethod + def _ed25519_verify(cls, key, message, signature): + # type: (Type[_Utility], AnyStr, AnyStr, AnyStr) -> None + if not cls._utility: + cls._allocate() + + byte_key = to_bytes(key) + byte_message = to_bytes(message) + byte_signature = to_bytes(signature) + + cls._check_error( + lib.olm_ed25519_verify(cls._utility, byte_key, len(byte_key), + byte_message, len(byte_message), + byte_signature, len(byte_signature))) + + +def ed25519_verify(key, message, signature): + # type: (AnyStr, AnyStr, AnyStr) -> None + """Verify an ed25519 signature. + + Raises an OlmVerifyError if verification fails. + + Args: + key(str): The ed25519 public key used for signing. + message(str): The signed message. + signature(bytes): The message signature. + """ + return _Utility._ed25519_verify(key, message, signature) -- cgit v1.2.3 From 019ff702a0f8e8015a70b03a5370d518c1b1dafb Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Mon, 15 Oct 2018 13:54:14 -0400 Subject: add license headers to python bindings --- python/olm/utility.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'python/olm/utility.py') diff --git a/python/olm/utility.py b/python/olm/utility.py index 838cf3f..1c5c41d 100644 --- a/python/olm/utility.py +++ b/python/olm/utility.py @@ -2,6 +2,18 @@ # libolm python bindings # Copyright © 2015-2017 OpenMarket Ltd # Copyright © 2018 Damir Jelić +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. """libolm Utility module. This module contains utilities for olm. -- cgit v1.2.3 From 5ef6a844d6fd3d58d1eb85dcd188ac6b6baa3fbe Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Tue, 16 Oct 2018 00:31:56 -0400 Subject: overwrite buffers that may contain sensitive data also reduce the amount of memory copying that we do --- python/olm/utility.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'python/olm/utility.py') diff --git a/python/olm/utility.py b/python/olm/utility.py index 1c5c41d..0a64128 100644 --- a/python/olm/utility.py +++ b/python/olm/utility.py @@ -36,7 +36,7 @@ from typing import AnyStr, Type # pylint: disable=no-name-in-module from _libolm import ffi, lib # type: ignore -from ._compat import to_bytes +from ._compat import to_bytearray, to_bytes from ._finalize import track_for_finalization @@ -80,13 +80,20 @@ class _Utility(object): cls._allocate() byte_key = to_bytes(key) - byte_message = to_bytes(message) + byte_message = to_bytearray(message) byte_signature = to_bytes(signature) - cls._check_error( - lib.olm_ed25519_verify(cls._utility, byte_key, len(byte_key), - byte_message, len(byte_message), - byte_signature, len(byte_signature))) + try: + cls._check_error( + lib.olm_ed25519_verify(cls._utility, byte_key, len(byte_key), + ffi.from_buffer(byte_message), + len(byte_message), + byte_signature, len(byte_signature))) + finally: + # clear out copies of the message, which may be a plaintext + if byte_message is not message: + for i in range(0, len(byte_message)): + byte_message[i] = 0 def ed25519_verify(key, message, signature): -- cgit v1.2.3