aboutsummaryrefslogtreecommitdiff
path: root/python/olm/inbound_group_session.py
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2016-05-25 15:46:50 +0100
committerRichard van der Hoff <richard@matrix.org>2016-05-25 15:46:50 +0100
commit27b57c8d8830ebd37ad304703ccb61390d4b0f36 (patch)
tree752f4c117ccf73c65ebcc853a17759f1ed4a5fa7 /python/olm/inbound_group_session.py
parent54d43010c808b56caa703696a036df18d2b74575 (diff)
parentfae8dacab5233c46f09e7d869afadaead2842609 (diff)
Merge branch 'rav/more_group_chat/1'
Diffstat (limited to 'python/olm/inbound_group_session.py')
-rw-r--r--python/olm/inbound_group_session.py86
1 files changed, 86 insertions, 0 deletions
diff --git a/python/olm/inbound_group_session.py b/python/olm/inbound_group_session.py
new file mode 100644
index 0000000..6c01095
--- /dev/null
+++ b/python/olm/inbound_group_session.py
@@ -0,0 +1,86 @@
+import json
+
+from ._base import *
+
+lib.olm_inbound_group_session_size.argtypes = []
+lib.olm_inbound_group_session_size.restype = c_size_t
+
+lib.olm_inbound_group_session.argtypes = [c_void_p]
+lib.olm_inbound_group_session.restype = c_void_p
+
+lib.olm_inbound_group_session_last_error.argtypes = [c_void_p]
+lib.olm_inbound_group_session_last_error.restype = c_char_p
+
+def inbound_group_session_errcheck(res, func, args):
+ if res == ERR:
+ raise OlmError("%s: %s" % (
+ func.__name__, lib.olm_inbound_group_session_last_error(args[0])
+ ))
+ return res
+
+
+def inbound_group_session_function(func, *types):
+ func.argtypes = (c_void_p,) + types
+ func.restypes = c_size_t
+ func.errcheck = inbound_group_session_errcheck
+
+
+inbound_group_session_function(
+ lib.olm_pickle_inbound_group_session, c_void_p, c_size_t, c_void_p, c_size_t
+)
+inbound_group_session_function(
+ lib.olm_unpickle_inbound_group_session, c_void_p, c_size_t, c_void_p, c_size_t
+)
+
+inbound_group_session_function(
+ lib.olm_init_inbound_group_session, c_uint32, c_void_p, c_size_t
+)
+
+inbound_group_session_function(
+ lib.olm_group_decrypt_max_plaintext_length, c_void_p, c_size_t
+)
+inbound_group_session_function(
+ lib.olm_group_decrypt,
+ c_void_p, c_size_t, # message
+ c_void_p, c_size_t, # plaintext
+)
+
+class InboundGroupSession(object):
+ def __init__(self):
+ self.buf = create_string_buffer(lib.olm_inbound_group_session_size())
+ self.ptr = lib.olm_inbound_group_session(self.buf)
+
+ def pickle(self, key):
+ key_buffer = create_string_buffer(key)
+ pickle_length = lib.olm_pickle_inbound_group_session_length(self.ptr)
+ pickle_buffer = create_string_buffer(pickle_length)
+ lib.olm_pickle_inbound_group_session(
+ self.ptr, key_buffer, len(key), pickle_buffer, pickle_length
+ )
+ return pickle_buffer.raw
+
+ def unpickle(self, key, pickle):
+ key_buffer = create_string_buffer(key)
+ pickle_buffer = create_string_buffer(pickle)
+ lib.olm_unpickle_inbound_group_session(
+ self.ptr, key_buffer, len(key), pickle_buffer, len(pickle)
+ )
+
+ def init(self, message_index, session_key):
+ key_buffer = create_string_buffer(session_key)
+ lib.olm_init_inbound_group_session(
+ self.ptr, message_index, key_buffer, len(session_key)
+ )
+
+ def decrypt(self, message):
+ message_buffer = create_string_buffer(message)
+ max_plaintext_length = lib.olm_group_decrypt_max_plaintext_length(
+ self.ptr, message_buffer, len(message)
+ )
+ plaintext_buffer = create_string_buffer(max_plaintext_length)
+ message_buffer = create_string_buffer(message)
+ plaintext_length = lib.olm_group_decrypt(
+ self.ptr, message_buffer, len(message),
+ plaintext_buffer, max_plaintext_length
+ )
+ return plaintext_buffer.raw[:plaintext_length]