diff options
Diffstat (limited to 'python/olm')
-rw-r--r-- | python/olm/__init__.py | 1 | ||||
-rwxr-xr-x | python/olm/__main__.py | 12 | ||||
-rw-r--r-- | python/olm/utility.py | 56 |
3 files changed, 69 insertions, 0 deletions
diff --git a/python/olm/__init__.py b/python/olm/__init__.py index 31b29b9..f74cbcb 100644 --- a/python/olm/__init__.py +++ b/python/olm/__init__.py @@ -2,3 +2,4 @@ from .account import Account from .session import Session from .outbound_group_session import OutboundGroupSession from .inbound_group_session import InboundGroupSession +from .utility import ed25519_verify diff --git a/python/olm/__main__.py b/python/olm/__main__.py index 71f018a..2a48b78 100755 --- a/python/olm/__main__.py +++ b/python/olm/__main__.py @@ -344,6 +344,9 @@ def build_arg_parser(): ) export_inbound_group.set_defaults(func=do_export_inbound_group) + ed25519_verify = commands.add_parser("ed25519_verify", + help="Verify an ed25519 signature") + ed25519_verify.set_defaults(func=do_verify_ed25519_signature) return parser @@ -430,6 +433,15 @@ def do_export_inbound_group(args): args.export_file.write(session.export_session(index)) +def do_verify_ed25519_signature(args): + account = Account() + account.create() + message = "A Message".encode("ASCII") + ed25519_key = account.identity_keys()["ed25519"].encode("utf-8") + signature = account.sign(message) + ed25519_verify(ed25519_key, message, signature) + + if __name__ == '__main__': parser = build_arg_parser() args = parser.parse_args() diff --git a/python/olm/utility.py b/python/olm/utility.py new file mode 100644 index 0000000..dac0225 --- /dev/null +++ b/python/olm/utility.py @@ -0,0 +1,56 @@ +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)) |