python3-olm 3.2.18__cp314-cp314-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
olm/sas.py ADDED
@@ -0,0 +1,276 @@
1
+ # -*- coding: utf-8 -*-
2
+ # libolm python bindings
3
+ # Copyright © 2019 Damir Jelić <poljar@termina.org.uk>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ """libolm SAS module.
18
+
19
+ This module contains functions to perform key verification using the Short
20
+ Authentication String (SAS) method.
21
+
22
+ Examples:
23
+ >>> sas = Sas()
24
+ >>> bob_key = "3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08"
25
+ >>> message = "Hello world!"
26
+ >>> extra_info = "MAC"
27
+ >>> sas_alice.set_their_pubkey(bob_key)
28
+ >>> sas_alice.calculate_mac(message, extra_info)
29
+ >>> sas_alice.generate_bytes(extra_info, 5)
30
+
31
+ """
32
+
33
+ from builtins import bytes
34
+ from functools import wraps
35
+ from typing import Optional
36
+
37
+ from _libolm import ffi, lib
38
+
39
+ from ._compat import URANDOM, to_bytearray, to_bytes
40
+ from ._finalize import track_for_finalization
41
+
42
+
43
+ def _clear_sas(sas):
44
+ # type: (ffi.cdata) -> None
45
+ lib.olm_clear_sas(sas)
46
+
47
+
48
+ class OlmSasError(Exception):
49
+ """libolm Sas error exception."""
50
+
51
+
52
+ class Sas(object):
53
+ """libolm Short Authenticaton String (SAS) class."""
54
+
55
+ def __init__(self, other_users_pubkey=None):
56
+ # type: (Optional[str]) -> None
57
+ """Create a new SAS object.
58
+
59
+ Args:
60
+ other_users_pubkey(str, optional): The other users public key, this
61
+ key is necesary to generate bytes for the authentication string
62
+ as well as to calculate the MAC.
63
+
64
+ Raises OlmSasError on failure.
65
+
66
+ """
67
+ self._buf = ffi.new("char[]", lib.olm_sas_size())
68
+ self._sas = lib.olm_sas(self._buf)
69
+ track_for_finalization(self, self._sas, _clear_sas)
70
+
71
+ random_length = lib.olm_create_sas_random_length(self._sas)
72
+ random = URANDOM(random_length)
73
+
74
+ self._create_sas(random, random_length)
75
+
76
+ if other_users_pubkey:
77
+ self.set_their_pubkey(other_users_pubkey)
78
+
79
+ def _create_sas(self, buffer, buffer_length):
80
+ self._check_error(
81
+ lib.olm_create_sas(
82
+ self._sas,
83
+ ffi.from_buffer(buffer),
84
+ buffer_length
85
+ )
86
+ )
87
+
88
+ def _check_error(self, ret):
89
+ # type: (int) -> None
90
+ if ret != lib.olm_error():
91
+ return
92
+
93
+ last_error = ffi.string((lib.olm_sas_last_error(self._sas))).decode()
94
+
95
+ raise OlmSasError(last_error)
96
+
97
+ @property
98
+ def pubkey(self):
99
+ # type: () -> str
100
+ """Get the public key for the SAS object.
101
+
102
+ This returns the public key of the SAS object that can then be shared
103
+ with another user to perform the authentication process.
104
+
105
+ Raises OlmSasError on failure.
106
+
107
+ """
108
+ pubkey_length = lib.olm_sas_pubkey_length(self._sas)
109
+ pubkey_buffer = ffi.new("char[]", pubkey_length)
110
+
111
+ self._check_error(
112
+ lib.olm_sas_get_pubkey(self._sas, pubkey_buffer, pubkey_length)
113
+ )
114
+
115
+ return ffi.unpack(pubkey_buffer, pubkey_length).decode()
116
+
117
+ @property
118
+ def other_key_set(self):
119
+ # type: () -> bool
120
+ """Check if the other user's pubkey has been set.
121
+ """
122
+ return lib.olm_sas_is_their_key_set(self._sas) == 1
123
+
124
+ def set_their_pubkey(self, key):
125
+ # type: (str) -> None
126
+ """Set the public key of the other user.
127
+
128
+ This sets the public key of the other user, it needs to be set before
129
+ bytes can be generated for the authentication string and a MAC can be
130
+ calculated.
131
+
132
+ Args:
133
+ key (str): The other users public key.
134
+
135
+ Raises OlmSasError on failure.
136
+
137
+ """
138
+ byte_key = to_bytearray(key)
139
+
140
+ self._check_error(
141
+ lib.olm_sas_set_their_key(
142
+ self._sas,
143
+ ffi.from_buffer(byte_key),
144
+ len(byte_key)
145
+ )
146
+ )
147
+
148
+ def generate_bytes(self, extra_info, length):
149
+ # type: (str, int) -> bytes
150
+ """Generate bytes to use for the short authentication string.
151
+
152
+ Args:
153
+ extra_info (str): Extra information to mix in when generating the
154
+ bytes.
155
+ length (int): The number of bytes to generate.
156
+
157
+ Raises OlmSasError if the other users persons public key isn't set or
158
+ an internal Olm error happens.
159
+
160
+ """
161
+ if length < 1:
162
+ raise ValueError("The length needs to be a positive integer value")
163
+
164
+ byte_info = to_bytearray(extra_info)
165
+ out_buffer = ffi.new("char[]", length)
166
+
167
+ self._check_error(
168
+ lib.olm_sas_generate_bytes(
169
+ self._sas,
170
+ ffi.from_buffer(byte_info),
171
+ len(byte_info),
172
+ out_buffer,
173
+ length
174
+ )
175
+ )
176
+
177
+ return ffi.unpack(out_buffer, length)
178
+
179
+ def calculate_mac(self, message, extra_info):
180
+ # type: (str, str) -> str
181
+ """Generate a message authentication code based on the shared secret.
182
+
183
+ Args:
184
+ message (str): The message to produce the authentication code for.
185
+ extra_info (str): Extra information to mix in when generating the
186
+ MAC
187
+
188
+ Raises OlmSasError on failure.
189
+
190
+ """
191
+ byte_message = to_bytes(message)
192
+ byte_info = to_bytes(extra_info)
193
+
194
+ mac_length = lib.olm_sas_mac_length(self._sas)
195
+ mac_buffer = ffi.new("char[]", mac_length)
196
+
197
+ self._check_error(
198
+ lib.olm_sas_calculate_mac(
199
+ self._sas,
200
+ ffi.from_buffer(byte_message),
201
+ len(byte_message),
202
+ ffi.from_buffer(byte_info),
203
+ len(byte_info),
204
+ mac_buffer,
205
+ mac_length
206
+ )
207
+ )
208
+ return ffi.unpack(mac_buffer, mac_length).decode()
209
+
210
+ def calculate_mac_fixed_base64(self, message, extra_info):
211
+ # type: (str, str) -> str
212
+ """Generate a message authentication code based on the shared secret.
213
+
214
+ This function uses a fixed base64 encoding that is compatible with
215
+ other base64 implementations.
216
+
217
+ Args:
218
+ message (str): The message to produce the authentication code for.
219
+ extra_info (str): Extra information to mix in when generating the
220
+ MAC
221
+
222
+ Raises OlmSasError on failure.
223
+
224
+ """
225
+ byte_message = to_bytes(message)
226
+ byte_info = to_bytes(extra_info)
227
+
228
+ mac_length = lib.olm_sas_mac_length(self._sas)
229
+ mac_buffer = ffi.new("char[]", mac_length)
230
+
231
+ self._check_error(
232
+ lib.olm_sas_calculate_mac_fixed_base64(
233
+ self._sas,
234
+ ffi.from_buffer(byte_message),
235
+ len(byte_message),
236
+ ffi.from_buffer(byte_info),
237
+ len(byte_info),
238
+ mac_buffer,
239
+ mac_length
240
+ )
241
+ )
242
+ return ffi.unpack(mac_buffer, mac_length).decode()
243
+
244
+ def calculate_mac_long_kdf(self, message, extra_info):
245
+ # type: (str, str) -> str
246
+ """Generate a message authentication code based on the shared secret.
247
+
248
+ This function should not be used unless compatibility with an older
249
+ non-tagged Olm version is required.
250
+
251
+ Args:
252
+ message (str): The message to produce the authentication code for.
253
+ extra_info (str): Extra information to mix in when generating the
254
+ MAC
255
+
256
+ Raises OlmSasError on failure.
257
+
258
+ """
259
+ byte_message = to_bytes(message)
260
+ byte_info = to_bytes(extra_info)
261
+
262
+ mac_length = lib.olm_sas_mac_length(self._sas)
263
+ mac_buffer = ffi.new("char[]", mac_length)
264
+
265
+ self._check_error(
266
+ lib.olm_sas_calculate_mac_long_kdf(
267
+ self._sas,
268
+ ffi.from_buffer(byte_message),
269
+ len(byte_message),
270
+ ffi.from_buffer(byte_info),
271
+ len(byte_info),
272
+ mac_buffer,
273
+ mac_length
274
+ )
275
+ )
276
+ return ffi.unpack(mac_buffer, mac_length).decode()