ruby_olm 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/ruby_olm/ext_lib_olm/ext_account.c +274 -0
- data/ext/ruby_olm/ext_lib_olm/ext_lib_olm.c +51 -0
- data/ext/ruby_olm/ext_lib_olm/ext_lib_olm.h +13 -0
- data/ext/ruby_olm/ext_lib_olm/ext_session.c +363 -0
- data/ext/ruby_olm/ext_lib_olm/ext_utility.c +69 -0
- data/ext/ruby_olm/ext_lib_olm/extconf.rb +69 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_account.cpp +695 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_account.h +56 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_inbound_group_session.cpp +654 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_inbound_group_session.h +51 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni.h +81 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni_helper.cpp +224 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni_helper.h +30 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_manager.cpp +35 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_manager.h +36 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_outbound_group_session.cpp +563 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_outbound_group_session.h +49 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_pk.cpp +716 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_pk.h +48 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_session.cpp +977 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_session.h +59 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_utility.cpp +236 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_utility.h +40 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_decode_message.cpp +14 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_decrypt.cpp +65 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_group_decrypt.cpp +73 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_unpickle_account.cpp +14 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_unpickle_session.cpp +14 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/include/fuzzing.hh +82 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/account.hh +160 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/base64.h +77 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/base64.hh +63 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/cipher.h +138 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/crypto.h +202 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/error.h +72 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/inbound_group_session.h +235 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/list.hh +119 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/megolm.h +95 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/memory.h +41 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/memory.hh +90 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/message.h +93 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/message.hh +138 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/olm.h +451 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/olm.hh +4 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/outbound_group_session.h +181 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle.h +90 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle.hh +149 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle_encoding.h +76 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pk.h +214 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/ratchet.hh +184 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/session.hh +156 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/utility.hh +61 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes.c +1073 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes.h +123 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes_test.c +276 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour.c +45 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour.h +30 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour_test.c +47 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64.c +135 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64.h +27 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64_test.c +54 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish.c +269 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish.h +32 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish_test.c +68 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des.c +269 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des.h +37 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des_test.c +83 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2.c +104 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2.h +33 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2_test.c +58 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5.c +189 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5.h +34 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5_test.c +60 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13.c +35 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13.h +20 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13_test.c +44 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1.c +149 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1.h +35 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1_test.c +58 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256.c +159 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256.h +34 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256_test.c +61 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/contrib/Curve25519Donna.c +118 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/contrib/Curve25519Donna.h +53 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/curve25519-donna-c64.c +449 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/curve25519-donna.c +860 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/python-src/curve25519/curve25519module.c +105 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/speed-curve25519.c +50 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-curve25519.c +54 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-noncanon.c +39 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-sc-curve25519.c +72 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna.h +18 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/add_scalar.c +56 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ed25519.h +38 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fe.c +1493 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fe.h +41 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fixedint.h +72 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ge.c +467 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ge.h +74 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/key_exchange.c +79 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/keypair.c +16 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/precomp_data.h +1391 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sc.c +814 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sc.h +12 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/seed.c +40 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sha512.c +275 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sha512.h +21 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sign.c +31 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/verify.c +77 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/test.c +150 -0
- data/ext/ruby_olm/ext_lib_olm/olm/python/dummy/stddef.h +0 -0
- data/ext/ruby_olm/ext_lib_olm/olm/python/dummy/stdint.h +0 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/account.cpp +380 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/base64.cpp +167 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/cipher.cpp +152 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/crypto.cpp +299 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/ed25519.c +22 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/error.c +44 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/inbound_group_session.c +524 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/megolm.c +150 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/memory.cpp +45 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/message.cpp +401 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/olm.cpp +738 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/outbound_group_session.c +363 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/pickle.cpp +242 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/pickle_encoding.c +92 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/pk.cpp +412 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/ratchet.cpp +625 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/session.cpp +462 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/utility.cpp +57 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/include/unittest.hh +107 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_base64.cpp +70 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_crypto.cpp +246 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_group_session.cpp +329 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_list.cpp +92 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_megolm.cpp +134 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_message.cpp +112 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm.cpp +405 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_decrypt.cpp +90 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_sha256.cpp +20 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_signature.cpp +81 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_using_malloc.cpp +210 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_pk.cpp +166 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_ratchet.cpp +221 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_session.cpp +144 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMAccount.h +51 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMAccount_Private.h +25 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMInboundGroupSession.h +38 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMKit.h +37 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMMessage.h +38 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMOutboundGroupSession.h +32 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkDecryption.h +71 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkEncryption.h +42 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkMessage.h +31 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSerializable.h +29 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSession.h +44 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSession_Private.h +26 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMUtility.h +49 -0
- data/ext/ruby_olm/ext_lib_olm/staging/account.cpp +380 -0
- data/ext/ruby_olm/ext_lib_olm/staging/aes.c +1073 -0
- data/ext/ruby_olm/ext_lib_olm/staging/base64.cpp +167 -0
- data/ext/ruby_olm/ext_lib_olm/staging/cipher.cpp +152 -0
- data/ext/ruby_olm/ext_lib_olm/staging/crypto.cpp +299 -0
- data/ext/ruby_olm/ext_lib_olm/staging/curve25519-donna.c +860 -0
- data/ext/ruby_olm/ext_lib_olm/staging/ed25519.c +22 -0
- data/ext/ruby_olm/ext_lib_olm/staging/error.c +44 -0
- data/ext/ruby_olm/ext_lib_olm/staging/inbound_group_session.c +524 -0
- data/ext/ruby_olm/ext_lib_olm/staging/megolm.c +150 -0
- data/ext/ruby_olm/ext_lib_olm/staging/memory.cpp +45 -0
- data/ext/ruby_olm/ext_lib_olm/staging/message.cpp +401 -0
- data/ext/ruby_olm/ext_lib_olm/staging/olm.cpp +738 -0
- data/ext/ruby_olm/ext_lib_olm/staging/outbound_group_session.c +363 -0
- data/ext/ruby_olm/ext_lib_olm/staging/pickle.cpp +242 -0
- data/ext/ruby_olm/ext_lib_olm/staging/pickle_encoding.c +92 -0
- data/ext/ruby_olm/ext_lib_olm/staging/pk.cpp +412 -0
- data/ext/ruby_olm/ext_lib_olm/staging/ratchet.cpp +625 -0
- data/ext/ruby_olm/ext_lib_olm/staging/session.cpp +461 -0
- data/ext/ruby_olm/ext_lib_olm/staging/sha256.c +159 -0
- data/ext/ruby_olm/ext_lib_olm/staging/utility.cpp +57 -0
- data/lib/ruby_olm/account.rb +42 -0
- data/lib/ruby_olm/message.rb +6 -0
- data/lib/ruby_olm/olm_error.rb +70 -0
- data/lib/ruby_olm/olm_message.rb +25 -0
- data/lib/ruby_olm/pre_key_message.rb +6 -0
- data/lib/ruby_olm/session.rb +16 -0
- data/lib/ruby_olm/version.rb +5 -0
- data/lib/ruby_olm.rb +10 -0
- data/rakefile +18 -0
- data/test/examples/test_bob_no_answer.rb +62 -0
- data/test/examples/test_exchange.rb +60 -0
- data/test/spec/test_account.rb +152 -0
- data/test/unit/test_account_methods.rb +85 -0
- metadata +282 -0
@@ -0,0 +1,401 @@
|
|
1
|
+
/* Copyright 2015-2016 OpenMarket Ltd
|
2
|
+
*
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
* you may not use this file except in compliance with the License.
|
5
|
+
* You may obtain a copy of the License at
|
6
|
+
*
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
*
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
* See the License for the specific language governing permissions and
|
13
|
+
* limitations under the License.
|
14
|
+
*/
|
15
|
+
#include "olm/message.hh"
|
16
|
+
|
17
|
+
#include "olm/memory.hh"
|
18
|
+
|
19
|
+
namespace {
|
20
|
+
|
21
|
+
template<typename T>
|
22
|
+
static std::size_t varint_length(
|
23
|
+
T value
|
24
|
+
) {
|
25
|
+
std::size_t result = 1;
|
26
|
+
while (value >= 128U) {
|
27
|
+
++result;
|
28
|
+
value >>= 7;
|
29
|
+
}
|
30
|
+
return result;
|
31
|
+
}
|
32
|
+
|
33
|
+
|
34
|
+
template<typename T>
|
35
|
+
static std::uint8_t * varint_encode(
|
36
|
+
std::uint8_t * output,
|
37
|
+
T value
|
38
|
+
) {
|
39
|
+
while (value >= 128U) {
|
40
|
+
*(output++) = (0x7F & value) | 0x80;
|
41
|
+
value >>= 7;
|
42
|
+
}
|
43
|
+
(*output++) = value;
|
44
|
+
return output;
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
template<typename T>
|
49
|
+
static T varint_decode(
|
50
|
+
std::uint8_t const * varint_start,
|
51
|
+
std::uint8_t const * varint_end
|
52
|
+
) {
|
53
|
+
T value = 0;
|
54
|
+
if (varint_end == varint_start) {
|
55
|
+
return 0;
|
56
|
+
}
|
57
|
+
do {
|
58
|
+
value <<= 7;
|
59
|
+
value |= 0x7F & *(--varint_end);
|
60
|
+
} while (varint_end != varint_start);
|
61
|
+
return value;
|
62
|
+
}
|
63
|
+
|
64
|
+
|
65
|
+
static std::uint8_t const * varint_skip(
|
66
|
+
std::uint8_t const * input,
|
67
|
+
std::uint8_t const * input_end
|
68
|
+
) {
|
69
|
+
while (input != input_end) {
|
70
|
+
std::uint8_t tmp = *(input++);
|
71
|
+
if ((tmp & 0x80) == 0) {
|
72
|
+
return input;
|
73
|
+
}
|
74
|
+
}
|
75
|
+
return input;
|
76
|
+
}
|
77
|
+
|
78
|
+
|
79
|
+
static std::size_t varstring_length(
|
80
|
+
std::size_t string_length
|
81
|
+
) {
|
82
|
+
return varint_length(string_length) + string_length;
|
83
|
+
}
|
84
|
+
|
85
|
+
static std::size_t const VERSION_LENGTH = 1;
|
86
|
+
static std::uint8_t const RATCHET_KEY_TAG = 012;
|
87
|
+
static std::uint8_t const COUNTER_TAG = 020;
|
88
|
+
static std::uint8_t const CIPHERTEXT_TAG = 042;
|
89
|
+
|
90
|
+
static std::uint8_t * encode(
|
91
|
+
std::uint8_t * pos,
|
92
|
+
std::uint8_t tag,
|
93
|
+
std::uint32_t value
|
94
|
+
) {
|
95
|
+
*(pos++) = tag;
|
96
|
+
return varint_encode(pos, value);
|
97
|
+
}
|
98
|
+
|
99
|
+
static std::uint8_t * encode(
|
100
|
+
std::uint8_t * pos,
|
101
|
+
std::uint8_t tag,
|
102
|
+
std::uint8_t * & value, std::size_t value_length
|
103
|
+
) {
|
104
|
+
*(pos++) = tag;
|
105
|
+
pos = varint_encode(pos, value_length);
|
106
|
+
value = pos;
|
107
|
+
return pos + value_length;
|
108
|
+
}
|
109
|
+
|
110
|
+
static std::uint8_t const * decode(
|
111
|
+
std::uint8_t const * pos, std::uint8_t const * end,
|
112
|
+
std::uint8_t tag,
|
113
|
+
std::uint32_t & value, bool & has_value
|
114
|
+
) {
|
115
|
+
if (pos != end && *pos == tag) {
|
116
|
+
++pos;
|
117
|
+
std::uint8_t const * value_start = pos;
|
118
|
+
pos = varint_skip(pos, end);
|
119
|
+
value = varint_decode<std::uint32_t>(value_start, pos);
|
120
|
+
has_value = true;
|
121
|
+
}
|
122
|
+
return pos;
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
static std::uint8_t const * decode(
|
127
|
+
std::uint8_t const * pos, std::uint8_t const * end,
|
128
|
+
std::uint8_t tag,
|
129
|
+
std::uint8_t const * & value, std::size_t & value_length
|
130
|
+
) {
|
131
|
+
if (pos != end && *pos == tag) {
|
132
|
+
++pos;
|
133
|
+
std::uint8_t const * len_start = pos;
|
134
|
+
pos = varint_skip(pos, end);
|
135
|
+
std::size_t len = varint_decode<std::size_t>(len_start, pos);
|
136
|
+
if (len > std::size_t(end - pos)) return end;
|
137
|
+
value = pos;
|
138
|
+
value_length = len;
|
139
|
+
pos += len;
|
140
|
+
}
|
141
|
+
return pos;
|
142
|
+
}
|
143
|
+
|
144
|
+
static std::uint8_t const * skip_unknown(
|
145
|
+
std::uint8_t const * pos, std::uint8_t const * end
|
146
|
+
) {
|
147
|
+
if (pos != end) {
|
148
|
+
uint8_t tag = *pos;
|
149
|
+
if ((tag & 0x7) == 0) {
|
150
|
+
pos = varint_skip(pos, end);
|
151
|
+
pos = varint_skip(pos, end);
|
152
|
+
} else if ((tag & 0x7) == 2) {
|
153
|
+
pos = varint_skip(pos, end);
|
154
|
+
std::uint8_t const * len_start = pos;
|
155
|
+
pos = varint_skip(pos, end);
|
156
|
+
std::size_t len = varint_decode<std::size_t>(len_start, pos);
|
157
|
+
if (len > std::size_t(end - pos)) return end;
|
158
|
+
pos += len;
|
159
|
+
} else {
|
160
|
+
return end;
|
161
|
+
}
|
162
|
+
}
|
163
|
+
return pos;
|
164
|
+
}
|
165
|
+
|
166
|
+
} // namespace
|
167
|
+
|
168
|
+
|
169
|
+
std::size_t olm::encode_message_length(
|
170
|
+
std::uint32_t counter,
|
171
|
+
std::size_t ratchet_key_length,
|
172
|
+
std::size_t ciphertext_length,
|
173
|
+
std::size_t mac_length
|
174
|
+
) {
|
175
|
+
std::size_t length = VERSION_LENGTH;
|
176
|
+
length += 1 + varstring_length(ratchet_key_length);
|
177
|
+
length += 1 + varint_length(counter);
|
178
|
+
length += 1 + varstring_length(ciphertext_length);
|
179
|
+
length += mac_length;
|
180
|
+
return length;
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
void olm::encode_message(
|
185
|
+
olm::MessageWriter & writer,
|
186
|
+
std::uint8_t version,
|
187
|
+
std::uint32_t counter,
|
188
|
+
std::size_t ratchet_key_length,
|
189
|
+
std::size_t ciphertext_length,
|
190
|
+
std::uint8_t * output
|
191
|
+
) {
|
192
|
+
std::uint8_t * pos = output;
|
193
|
+
*(pos++) = version;
|
194
|
+
pos = encode(pos, RATCHET_KEY_TAG, writer.ratchet_key, ratchet_key_length);
|
195
|
+
pos = encode(pos, COUNTER_TAG, counter);
|
196
|
+
pos = encode(pos, CIPHERTEXT_TAG, writer.ciphertext, ciphertext_length);
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
void olm::decode_message(
|
201
|
+
olm::MessageReader & reader,
|
202
|
+
std::uint8_t const * input, std::size_t input_length,
|
203
|
+
std::size_t mac_length
|
204
|
+
) {
|
205
|
+
std::uint8_t const * pos = input;
|
206
|
+
std::uint8_t const * end = input + input_length - mac_length;
|
207
|
+
std::uint8_t const * unknown = nullptr;
|
208
|
+
|
209
|
+
reader.input = input;
|
210
|
+
reader.input_length = input_length;
|
211
|
+
reader.has_counter = false;
|
212
|
+
reader.ratchet_key = nullptr;
|
213
|
+
reader.ratchet_key_length = 0;
|
214
|
+
reader.ciphertext = nullptr;
|
215
|
+
reader.ciphertext_length = 0;
|
216
|
+
|
217
|
+
if (input_length < mac_length) return;
|
218
|
+
|
219
|
+
if (pos == end) return;
|
220
|
+
reader.version = *(pos++);
|
221
|
+
|
222
|
+
while (pos != end) {
|
223
|
+
unknown = pos;
|
224
|
+
pos = decode(
|
225
|
+
pos, end, RATCHET_KEY_TAG,
|
226
|
+
reader.ratchet_key, reader.ratchet_key_length
|
227
|
+
);
|
228
|
+
pos = decode(
|
229
|
+
pos, end, COUNTER_TAG,
|
230
|
+
reader.counter, reader.has_counter
|
231
|
+
);
|
232
|
+
pos = decode(
|
233
|
+
pos, end, CIPHERTEXT_TAG,
|
234
|
+
reader.ciphertext, reader.ciphertext_length
|
235
|
+
);
|
236
|
+
if (unknown == pos) {
|
237
|
+
pos = skip_unknown(pos, end);
|
238
|
+
}
|
239
|
+
}
|
240
|
+
}
|
241
|
+
|
242
|
+
|
243
|
+
namespace {
|
244
|
+
|
245
|
+
static std::uint8_t const ONE_TIME_KEY_ID_TAG = 012;
|
246
|
+
static std::uint8_t const BASE_KEY_TAG = 022;
|
247
|
+
static std::uint8_t const IDENTITY_KEY_TAG = 032;
|
248
|
+
static std::uint8_t const MESSAGE_TAG = 042;
|
249
|
+
|
250
|
+
} // namespace
|
251
|
+
|
252
|
+
|
253
|
+
std::size_t olm::encode_one_time_key_message_length(
|
254
|
+
std::size_t one_time_key_length,
|
255
|
+
std::size_t identity_key_length,
|
256
|
+
std::size_t base_key_length,
|
257
|
+
std::size_t message_length
|
258
|
+
) {
|
259
|
+
std::size_t length = VERSION_LENGTH;
|
260
|
+
length += 1 + varstring_length(one_time_key_length);
|
261
|
+
length += 1 + varstring_length(identity_key_length);
|
262
|
+
length += 1 + varstring_length(base_key_length);
|
263
|
+
length += 1 + varstring_length(message_length);
|
264
|
+
return length;
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
void olm::encode_one_time_key_message(
|
269
|
+
olm::PreKeyMessageWriter & writer,
|
270
|
+
std::uint8_t version,
|
271
|
+
std::size_t identity_key_length,
|
272
|
+
std::size_t base_key_length,
|
273
|
+
std::size_t one_time_key_length,
|
274
|
+
std::size_t message_length,
|
275
|
+
std::uint8_t * output
|
276
|
+
) {
|
277
|
+
std::uint8_t * pos = output;
|
278
|
+
*(pos++) = version;
|
279
|
+
pos = encode(pos, ONE_TIME_KEY_ID_TAG, writer.one_time_key, one_time_key_length);
|
280
|
+
pos = encode(pos, BASE_KEY_TAG, writer.base_key, base_key_length);
|
281
|
+
pos = encode(pos, IDENTITY_KEY_TAG, writer.identity_key, identity_key_length);
|
282
|
+
pos = encode(pos, MESSAGE_TAG, writer.message, message_length);
|
283
|
+
}
|
284
|
+
|
285
|
+
|
286
|
+
void olm::decode_one_time_key_message(
|
287
|
+
PreKeyMessageReader & reader,
|
288
|
+
std::uint8_t const * input, std::size_t input_length
|
289
|
+
) {
|
290
|
+
std::uint8_t const * pos = input;
|
291
|
+
std::uint8_t const * end = input + input_length;
|
292
|
+
std::uint8_t const * unknown = nullptr;
|
293
|
+
|
294
|
+
reader.one_time_key = nullptr;
|
295
|
+
reader.one_time_key_length = 0;
|
296
|
+
reader.identity_key = nullptr;
|
297
|
+
reader.identity_key_length = 0;
|
298
|
+
reader.base_key = nullptr;
|
299
|
+
reader.base_key_length = 0;
|
300
|
+
reader.message = nullptr;
|
301
|
+
reader.message_length = 0;
|
302
|
+
|
303
|
+
if (pos == end) return;
|
304
|
+
reader.version = *(pos++);
|
305
|
+
|
306
|
+
while (pos != end) {
|
307
|
+
unknown = pos;
|
308
|
+
pos = decode(
|
309
|
+
pos, end, ONE_TIME_KEY_ID_TAG,
|
310
|
+
reader.one_time_key, reader.one_time_key_length
|
311
|
+
);
|
312
|
+
pos = decode(
|
313
|
+
pos, end, BASE_KEY_TAG,
|
314
|
+
reader.base_key, reader.base_key_length
|
315
|
+
);
|
316
|
+
pos = decode(
|
317
|
+
pos, end, IDENTITY_KEY_TAG,
|
318
|
+
reader.identity_key, reader.identity_key_length
|
319
|
+
);
|
320
|
+
pos = decode(
|
321
|
+
pos, end, MESSAGE_TAG,
|
322
|
+
reader.message, reader.message_length
|
323
|
+
);
|
324
|
+
if (unknown == pos) {
|
325
|
+
pos = skip_unknown(pos, end);
|
326
|
+
}
|
327
|
+
}
|
328
|
+
}
|
329
|
+
|
330
|
+
|
331
|
+
|
332
|
+
static const std::uint8_t GROUP_MESSAGE_INDEX_TAG = 010;
|
333
|
+
static const std::uint8_t GROUP_CIPHERTEXT_TAG = 022;
|
334
|
+
|
335
|
+
size_t _olm_encode_group_message_length(
|
336
|
+
uint32_t message_index,
|
337
|
+
size_t ciphertext_length,
|
338
|
+
size_t mac_length,
|
339
|
+
size_t signature_length
|
340
|
+
) {
|
341
|
+
size_t length = VERSION_LENGTH;
|
342
|
+
length += 1 + varint_length(message_index);
|
343
|
+
length += 1 + varstring_length(ciphertext_length);
|
344
|
+
length += mac_length;
|
345
|
+
length += signature_length;
|
346
|
+
return length;
|
347
|
+
}
|
348
|
+
|
349
|
+
|
350
|
+
size_t _olm_encode_group_message(
|
351
|
+
uint8_t version,
|
352
|
+
uint32_t message_index,
|
353
|
+
size_t ciphertext_length,
|
354
|
+
uint8_t *output,
|
355
|
+
uint8_t **ciphertext_ptr
|
356
|
+
) {
|
357
|
+
std::uint8_t * pos = output;
|
358
|
+
|
359
|
+
*(pos++) = version;
|
360
|
+
pos = encode(pos, GROUP_MESSAGE_INDEX_TAG, message_index);
|
361
|
+
pos = encode(pos, GROUP_CIPHERTEXT_TAG, *ciphertext_ptr, ciphertext_length);
|
362
|
+
return pos-output;
|
363
|
+
}
|
364
|
+
|
365
|
+
void _olm_decode_group_message(
|
366
|
+
const uint8_t *input, size_t input_length,
|
367
|
+
size_t mac_length, size_t signature_length,
|
368
|
+
struct _OlmDecodeGroupMessageResults *results
|
369
|
+
) {
|
370
|
+
std::uint8_t const * pos = input;
|
371
|
+
std::size_t trailer_length = mac_length + signature_length;
|
372
|
+
std::uint8_t const * end = input + input_length - trailer_length;
|
373
|
+
std::uint8_t const * unknown = nullptr;
|
374
|
+
|
375
|
+
bool has_message_index = false;
|
376
|
+
results->message_index = 0;
|
377
|
+
results->ciphertext = nullptr;
|
378
|
+
results->ciphertext_length = 0;
|
379
|
+
|
380
|
+
if (input_length < trailer_length) return;
|
381
|
+
|
382
|
+
if (pos == end) return;
|
383
|
+
results->version = *(pos++);
|
384
|
+
|
385
|
+
while (pos != end) {
|
386
|
+
unknown = pos;
|
387
|
+
pos = decode(
|
388
|
+
pos, end, GROUP_MESSAGE_INDEX_TAG,
|
389
|
+
results->message_index, has_message_index
|
390
|
+
);
|
391
|
+
pos = decode(
|
392
|
+
pos, end, GROUP_CIPHERTEXT_TAG,
|
393
|
+
results->ciphertext, results->ciphertext_length
|
394
|
+
);
|
395
|
+
if (unknown == pos) {
|
396
|
+
pos = skip_unknown(pos, end);
|
397
|
+
}
|
398
|
+
}
|
399
|
+
|
400
|
+
results->has_message_index = (int)has_message_index;
|
401
|
+
}
|