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,380 @@
|
|
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/account.hh"
|
16
|
+
#include "olm/base64.hh"
|
17
|
+
#include "olm/pickle.h"
|
18
|
+
#include "olm/pickle.hh"
|
19
|
+
#include "olm/memory.hh"
|
20
|
+
|
21
|
+
olm::Account::Account(
|
22
|
+
) : next_one_time_key_id(0),
|
23
|
+
last_error(OlmErrorCode::OLM_SUCCESS) {
|
24
|
+
}
|
25
|
+
|
26
|
+
|
27
|
+
olm::OneTimeKey const * olm::Account::lookup_key(
|
28
|
+
_olm_curve25519_public_key const & public_key
|
29
|
+
) {
|
30
|
+
for (olm::OneTimeKey const & key : one_time_keys) {
|
31
|
+
if (olm::array_equal(key.key.public_key.public_key, public_key.public_key)) {
|
32
|
+
return &key;
|
33
|
+
}
|
34
|
+
}
|
35
|
+
return 0;
|
36
|
+
}
|
37
|
+
|
38
|
+
std::size_t olm::Account::remove_key(
|
39
|
+
_olm_curve25519_public_key const & public_key
|
40
|
+
) {
|
41
|
+
OneTimeKey * i;
|
42
|
+
for (i = one_time_keys.begin(); i != one_time_keys.end(); ++i) {
|
43
|
+
if (olm::array_equal(i->key.public_key.public_key, public_key.public_key)) {
|
44
|
+
std::uint32_t id = i->id;
|
45
|
+
one_time_keys.erase(i);
|
46
|
+
return id;
|
47
|
+
}
|
48
|
+
}
|
49
|
+
return std::size_t(-1);
|
50
|
+
}
|
51
|
+
|
52
|
+
std::size_t olm::Account::new_account_random_length() {
|
53
|
+
return ED25519_RANDOM_LENGTH + CURVE25519_RANDOM_LENGTH;
|
54
|
+
}
|
55
|
+
|
56
|
+
std::size_t olm::Account::new_account(
|
57
|
+
uint8_t const * random, std::size_t random_length
|
58
|
+
) {
|
59
|
+
if (random_length < new_account_random_length()) {
|
60
|
+
last_error = OlmErrorCode::OLM_NOT_ENOUGH_RANDOM;
|
61
|
+
return std::size_t(-1);
|
62
|
+
}
|
63
|
+
|
64
|
+
_olm_crypto_ed25519_generate_key(random, &identity_keys.ed25519_key);
|
65
|
+
random += ED25519_RANDOM_LENGTH;
|
66
|
+
_olm_crypto_curve25519_generate_key(random, &identity_keys.curve25519_key);
|
67
|
+
|
68
|
+
return 0;
|
69
|
+
}
|
70
|
+
|
71
|
+
namespace {
|
72
|
+
|
73
|
+
uint8_t KEY_JSON_ED25519[] = "\"ed25519\":";
|
74
|
+
uint8_t KEY_JSON_CURVE25519[] = "\"curve25519\":";
|
75
|
+
|
76
|
+
template<typename T>
|
77
|
+
static std::uint8_t * write_string(
|
78
|
+
std::uint8_t * pos,
|
79
|
+
T const & value
|
80
|
+
) {
|
81
|
+
std::memcpy(pos, value, sizeof(T) - 1);
|
82
|
+
return pos + (sizeof(T) - 1);
|
83
|
+
}
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
std::size_t olm::Account::get_identity_json_length() {
|
89
|
+
std::size_t length = 0;
|
90
|
+
length += 1; /* { */
|
91
|
+
length += sizeof(KEY_JSON_CURVE25519) - 1;
|
92
|
+
length += 1; /* " */
|
93
|
+
length += olm::encode_base64_length(
|
94
|
+
sizeof(identity_keys.curve25519_key.public_key)
|
95
|
+
);
|
96
|
+
length += 2; /* ", */
|
97
|
+
length += sizeof(KEY_JSON_ED25519) - 1;
|
98
|
+
length += 1; /* " */
|
99
|
+
length += olm::encode_base64_length(
|
100
|
+
sizeof(identity_keys.ed25519_key.public_key)
|
101
|
+
);
|
102
|
+
length += 2; /* "} */
|
103
|
+
return length;
|
104
|
+
}
|
105
|
+
|
106
|
+
|
107
|
+
std::size_t olm::Account::get_identity_json(
|
108
|
+
std::uint8_t * identity_json, std::size_t identity_json_length
|
109
|
+
) {
|
110
|
+
std::uint8_t * pos = identity_json;
|
111
|
+
size_t expected_length = get_identity_json_length();
|
112
|
+
|
113
|
+
if (identity_json_length < expected_length) {
|
114
|
+
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
115
|
+
return std::size_t(-1);
|
116
|
+
}
|
117
|
+
|
118
|
+
*(pos++) = '{';
|
119
|
+
pos = write_string(pos, KEY_JSON_CURVE25519);
|
120
|
+
*(pos++) = '\"';
|
121
|
+
pos = olm::encode_base64(
|
122
|
+
identity_keys.curve25519_key.public_key.public_key,
|
123
|
+
sizeof(identity_keys.curve25519_key.public_key.public_key),
|
124
|
+
pos
|
125
|
+
);
|
126
|
+
*(pos++) = '\"'; *(pos++) = ',';
|
127
|
+
pos = write_string(pos, KEY_JSON_ED25519);
|
128
|
+
*(pos++) = '\"';
|
129
|
+
pos = olm::encode_base64(
|
130
|
+
identity_keys.ed25519_key.public_key.public_key,
|
131
|
+
sizeof(identity_keys.ed25519_key.public_key.public_key),
|
132
|
+
pos
|
133
|
+
);
|
134
|
+
*(pos++) = '\"'; *(pos++) = '}';
|
135
|
+
return pos - identity_json;
|
136
|
+
}
|
137
|
+
|
138
|
+
|
139
|
+
std::size_t olm::Account::signature_length(
|
140
|
+
) {
|
141
|
+
return ED25519_SIGNATURE_LENGTH;
|
142
|
+
}
|
143
|
+
|
144
|
+
|
145
|
+
std::size_t olm::Account::sign(
|
146
|
+
std::uint8_t const * message, std::size_t message_length,
|
147
|
+
std::uint8_t * signature, std::size_t signature_length
|
148
|
+
) {
|
149
|
+
if (signature_length < this->signature_length()) {
|
150
|
+
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
151
|
+
return std::size_t(-1);
|
152
|
+
}
|
153
|
+
_olm_crypto_ed25519_sign(
|
154
|
+
&identity_keys.ed25519_key, message, message_length, signature
|
155
|
+
);
|
156
|
+
return this->signature_length();
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
std::size_t olm::Account::get_one_time_keys_json_length(
|
161
|
+
) {
|
162
|
+
std::size_t length = 0;
|
163
|
+
bool is_empty = true;
|
164
|
+
for (auto const & key : one_time_keys) {
|
165
|
+
if (key.published) {
|
166
|
+
continue;
|
167
|
+
}
|
168
|
+
is_empty = false;
|
169
|
+
length += 2; /* {" */
|
170
|
+
length += olm::encode_base64_length(_olm_pickle_uint32_length(key.id));
|
171
|
+
length += 3; /* ":" */
|
172
|
+
length += olm::encode_base64_length(sizeof(key.key.public_key));
|
173
|
+
length += 1; /* " */
|
174
|
+
}
|
175
|
+
if (is_empty) {
|
176
|
+
length += 1; /* { */
|
177
|
+
}
|
178
|
+
length += 3; /* }{} */
|
179
|
+
length += sizeof(KEY_JSON_CURVE25519) - 1;
|
180
|
+
return length;
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
std::size_t olm::Account::get_one_time_keys_json(
|
185
|
+
std::uint8_t * one_time_json, std::size_t one_time_json_length
|
186
|
+
) {
|
187
|
+
std::uint8_t * pos = one_time_json;
|
188
|
+
if (one_time_json_length < get_one_time_keys_json_length()) {
|
189
|
+
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
190
|
+
return std::size_t(-1);
|
191
|
+
}
|
192
|
+
*(pos++) = '{';
|
193
|
+
pos = write_string(pos, KEY_JSON_CURVE25519);
|
194
|
+
std::uint8_t sep = '{';
|
195
|
+
for (auto const & key : one_time_keys) {
|
196
|
+
if (key.published) {
|
197
|
+
continue;
|
198
|
+
}
|
199
|
+
*(pos++) = sep;
|
200
|
+
*(pos++) = '\"';
|
201
|
+
std::uint8_t key_id[_olm_pickle_uint32_length(key.id)];
|
202
|
+
_olm_pickle_uint32(key_id, key.id);
|
203
|
+
pos = olm::encode_base64(key_id, sizeof(key_id), pos);
|
204
|
+
*(pos++) = '\"'; *(pos++) = ':'; *(pos++) = '\"';
|
205
|
+
pos = olm::encode_base64(
|
206
|
+
key.key.public_key.public_key, sizeof(key.key.public_key.public_key), pos
|
207
|
+
);
|
208
|
+
*(pos++) = '\"';
|
209
|
+
sep = ',';
|
210
|
+
}
|
211
|
+
if (sep != ',') {
|
212
|
+
/* The list was empty */
|
213
|
+
*(pos++) = sep;
|
214
|
+
}
|
215
|
+
*(pos++) = '}';
|
216
|
+
*(pos++) = '}';
|
217
|
+
return pos - one_time_json;
|
218
|
+
}
|
219
|
+
|
220
|
+
|
221
|
+
std::size_t olm::Account::mark_keys_as_published(
|
222
|
+
) {
|
223
|
+
std::size_t count = 0;
|
224
|
+
for (auto & key : one_time_keys) {
|
225
|
+
if (!key.published) {
|
226
|
+
key.published = true;
|
227
|
+
count++;
|
228
|
+
}
|
229
|
+
}
|
230
|
+
return count;
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
std::size_t olm::Account::max_number_of_one_time_keys(
|
235
|
+
) {
|
236
|
+
return olm::MAX_ONE_TIME_KEYS;
|
237
|
+
}
|
238
|
+
|
239
|
+
std::size_t olm::Account::generate_one_time_keys_random_length(
|
240
|
+
std::size_t number_of_keys
|
241
|
+
) {
|
242
|
+
return CURVE25519_RANDOM_LENGTH * number_of_keys;
|
243
|
+
}
|
244
|
+
|
245
|
+
std::size_t olm::Account::generate_one_time_keys(
|
246
|
+
std::size_t number_of_keys,
|
247
|
+
std::uint8_t const * random, std::size_t random_length
|
248
|
+
) {
|
249
|
+
if (random_length < generate_one_time_keys_random_length(number_of_keys)) {
|
250
|
+
last_error = OlmErrorCode::OLM_NOT_ENOUGH_RANDOM;
|
251
|
+
return std::size_t(-1);
|
252
|
+
}
|
253
|
+
for (unsigned i = 0; i < number_of_keys; ++i) {
|
254
|
+
OneTimeKey & key = *one_time_keys.insert(one_time_keys.begin());
|
255
|
+
key.id = ++next_one_time_key_id;
|
256
|
+
key.published = false;
|
257
|
+
_olm_crypto_curve25519_generate_key(random, &key.key);
|
258
|
+
random += CURVE25519_RANDOM_LENGTH;
|
259
|
+
}
|
260
|
+
return number_of_keys;
|
261
|
+
}
|
262
|
+
|
263
|
+
namespace olm {
|
264
|
+
|
265
|
+
static std::size_t pickle_length(
|
266
|
+
olm::IdentityKeys const & value
|
267
|
+
) {
|
268
|
+
size_t length = 0;
|
269
|
+
length += _olm_pickle_ed25519_key_pair_length(&value.ed25519_key);
|
270
|
+
length += olm::pickle_length(value.curve25519_key);
|
271
|
+
return length;
|
272
|
+
}
|
273
|
+
|
274
|
+
|
275
|
+
static std::uint8_t * pickle(
|
276
|
+
std::uint8_t * pos,
|
277
|
+
olm::IdentityKeys const & value
|
278
|
+
) {
|
279
|
+
pos = _olm_pickle_ed25519_key_pair(pos, &value.ed25519_key);
|
280
|
+
pos = olm::pickle(pos, value.curve25519_key);
|
281
|
+
return pos;
|
282
|
+
}
|
283
|
+
|
284
|
+
|
285
|
+
static std::uint8_t const * unpickle(
|
286
|
+
std::uint8_t const * pos, std::uint8_t const * end,
|
287
|
+
olm::IdentityKeys & value
|
288
|
+
) {
|
289
|
+
pos = _olm_unpickle_ed25519_key_pair(pos, end, &value.ed25519_key);
|
290
|
+
pos = olm::unpickle(pos, end, value.curve25519_key);
|
291
|
+
return pos;
|
292
|
+
}
|
293
|
+
|
294
|
+
|
295
|
+
static std::size_t pickle_length(
|
296
|
+
olm::OneTimeKey const & value
|
297
|
+
) {
|
298
|
+
std::size_t length = 0;
|
299
|
+
length += olm::pickle_length(value.id);
|
300
|
+
length += olm::pickle_length(value.published);
|
301
|
+
length += olm::pickle_length(value.key);
|
302
|
+
return length;
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
static std::uint8_t * pickle(
|
307
|
+
std::uint8_t * pos,
|
308
|
+
olm::OneTimeKey const & value
|
309
|
+
) {
|
310
|
+
pos = olm::pickle(pos, value.id);
|
311
|
+
pos = olm::pickle(pos, value.published);
|
312
|
+
pos = olm::pickle(pos, value.key);
|
313
|
+
return pos;
|
314
|
+
}
|
315
|
+
|
316
|
+
|
317
|
+
static std::uint8_t const * unpickle(
|
318
|
+
std::uint8_t const * pos, std::uint8_t const * end,
|
319
|
+
olm::OneTimeKey & value
|
320
|
+
) {
|
321
|
+
pos = olm::unpickle(pos, end, value.id);
|
322
|
+
pos = olm::unpickle(pos, end, value.published);
|
323
|
+
pos = olm::unpickle(pos, end, value.key);
|
324
|
+
return pos;
|
325
|
+
}
|
326
|
+
|
327
|
+
} // namespace olm
|
328
|
+
|
329
|
+
namespace {
|
330
|
+
// pickle version 1 used only 32 bytes for the ed25519 private key.
|
331
|
+
// Any keys thus used should be considered compromised.
|
332
|
+
static const std::uint32_t ACCOUNT_PICKLE_VERSION = 2;
|
333
|
+
}
|
334
|
+
|
335
|
+
|
336
|
+
std::size_t olm::pickle_length(
|
337
|
+
olm::Account const & value
|
338
|
+
) {
|
339
|
+
std::size_t length = 0;
|
340
|
+
length += olm::pickle_length(ACCOUNT_PICKLE_VERSION);
|
341
|
+
length += olm::pickle_length(value.identity_keys);
|
342
|
+
length += olm::pickle_length(value.one_time_keys);
|
343
|
+
length += olm::pickle_length(value.next_one_time_key_id);
|
344
|
+
return length;
|
345
|
+
}
|
346
|
+
|
347
|
+
|
348
|
+
std::uint8_t * olm::pickle(
|
349
|
+
std::uint8_t * pos,
|
350
|
+
olm::Account const & value
|
351
|
+
) {
|
352
|
+
pos = olm::pickle(pos, ACCOUNT_PICKLE_VERSION);
|
353
|
+
pos = olm::pickle(pos, value.identity_keys);
|
354
|
+
pos = olm::pickle(pos, value.one_time_keys);
|
355
|
+
pos = olm::pickle(pos, value.next_one_time_key_id);
|
356
|
+
return pos;
|
357
|
+
}
|
358
|
+
|
359
|
+
|
360
|
+
std::uint8_t const * olm::unpickle(
|
361
|
+
std::uint8_t const * pos, std::uint8_t const * end,
|
362
|
+
olm::Account & value
|
363
|
+
) {
|
364
|
+
uint32_t pickle_version;
|
365
|
+
pos = olm::unpickle(pos, end, pickle_version);
|
366
|
+
switch (pickle_version) {
|
367
|
+
case ACCOUNT_PICKLE_VERSION:
|
368
|
+
break;
|
369
|
+
case 1:
|
370
|
+
value.last_error = OlmErrorCode::OLM_BAD_LEGACY_ACCOUNT_PICKLE;
|
371
|
+
return end;
|
372
|
+
default:
|
373
|
+
value.last_error = OlmErrorCode::OLM_UNKNOWN_PICKLE_VERSION;
|
374
|
+
return end;
|
375
|
+
}
|
376
|
+
pos = olm::unpickle(pos, end, value.identity_keys);
|
377
|
+
pos = olm::unpickle(pos, end, value.one_time_keys);
|
378
|
+
pos = olm::unpickle(pos, end, value.next_one_time_key_id);
|
379
|
+
return pos;
|
380
|
+
}
|
@@ -0,0 +1,167 @@
|
|
1
|
+
/* Copyright 2015 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/base64.h"
|
16
|
+
#include "olm/base64.hh"
|
17
|
+
|
18
|
+
namespace {
|
19
|
+
|
20
|
+
static const std::uint8_t ENCODE_BASE64[64] = {
|
21
|
+
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
|
22
|
+
0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
|
23
|
+
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
|
24
|
+
0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
|
25
|
+
0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
|
26
|
+
0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
|
27
|
+
0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
|
28
|
+
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F,
|
29
|
+
};
|
30
|
+
|
31
|
+
static const std::uint8_t E = -1;
|
32
|
+
|
33
|
+
static const std::uint8_t DECODE_BASE64[128] = {
|
34
|
+
/* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF */
|
35
|
+
E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,
|
36
|
+
E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,
|
37
|
+
E, E, E, E, E, E, E, E, E, E, E, 62, E, E, E, 63,
|
38
|
+
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, E, E, E, E, E, E,
|
39
|
+
E, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
40
|
+
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, E, E, E, E, E,
|
41
|
+
E, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
42
|
+
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, E, E, E, E, E,
|
43
|
+
};
|
44
|
+
|
45
|
+
} // namespace
|
46
|
+
|
47
|
+
|
48
|
+
std::size_t olm::encode_base64_length(
|
49
|
+
std::size_t input_length
|
50
|
+
) {
|
51
|
+
return 4 * ((input_length + 2) / 3) + (input_length + 2) % 3 - 2;
|
52
|
+
}
|
53
|
+
|
54
|
+
std::uint8_t * olm::encode_base64(
|
55
|
+
std::uint8_t const * input, std::size_t input_length,
|
56
|
+
std::uint8_t * output
|
57
|
+
) {
|
58
|
+
std::uint8_t const * end = input + (input_length / 3) * 3;
|
59
|
+
std::uint8_t const * pos = input;
|
60
|
+
while (pos != end) {
|
61
|
+
unsigned value = pos[0];
|
62
|
+
value <<= 8; value |= pos[1];
|
63
|
+
value <<= 8; value |= pos[2];
|
64
|
+
pos += 3;
|
65
|
+
output[3] = ENCODE_BASE64[value & 0x3F];
|
66
|
+
value >>= 6; output[2] = ENCODE_BASE64[value & 0x3F];
|
67
|
+
value >>= 6; output[1] = ENCODE_BASE64[value & 0x3F];
|
68
|
+
value >>= 6; output[0] = ENCODE_BASE64[value];
|
69
|
+
output += 4;
|
70
|
+
}
|
71
|
+
unsigned remainder = input + input_length - pos;
|
72
|
+
std::uint8_t * result = output;
|
73
|
+
if (remainder) {
|
74
|
+
unsigned value = pos[0];
|
75
|
+
if (remainder == 2) {
|
76
|
+
value <<= 8; value |= pos[1];
|
77
|
+
value <<= 2;
|
78
|
+
output[2] = ENCODE_BASE64[value & 0x3F];
|
79
|
+
value >>= 6;
|
80
|
+
result += 3;
|
81
|
+
} else {
|
82
|
+
value <<= 4;
|
83
|
+
result += 2;
|
84
|
+
}
|
85
|
+
output[1] = ENCODE_BASE64[value & 0x3F];
|
86
|
+
value >>= 6;
|
87
|
+
output[0] = ENCODE_BASE64[value];
|
88
|
+
}
|
89
|
+
return result;
|
90
|
+
}
|
91
|
+
|
92
|
+
|
93
|
+
std::size_t olm::decode_base64_length(
|
94
|
+
std::size_t input_length
|
95
|
+
) {
|
96
|
+
if (input_length % 4 == 1) {
|
97
|
+
return std::size_t(-1);
|
98
|
+
} else {
|
99
|
+
return 3 * ((input_length + 2) / 4) + (input_length + 2) % 4 - 2;
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
std::uint8_t const * olm::decode_base64(
|
105
|
+
std::uint8_t const * input, std::size_t input_length,
|
106
|
+
std::uint8_t * output
|
107
|
+
) {
|
108
|
+
std::uint8_t const * end = input + (input_length / 4) * 4;
|
109
|
+
std::uint8_t const * pos = input;
|
110
|
+
while (pos != end) {
|
111
|
+
unsigned value = DECODE_BASE64[pos[0] & 0x7F];
|
112
|
+
value <<= 6; value |= DECODE_BASE64[pos[1] & 0x7F];
|
113
|
+
value <<= 6; value |= DECODE_BASE64[pos[2] & 0x7F];
|
114
|
+
value <<= 6; value |= DECODE_BASE64[pos[3] & 0x7F];
|
115
|
+
pos += 4;
|
116
|
+
output[2] = value;
|
117
|
+
value >>= 8; output[1] = value;
|
118
|
+
value >>= 8; output[0] = value;
|
119
|
+
output += 3;
|
120
|
+
}
|
121
|
+
unsigned remainder = input + input_length - pos;
|
122
|
+
if (remainder) {
|
123
|
+
unsigned value = DECODE_BASE64[pos[0] & 0x7F];
|
124
|
+
value <<= 6; value |= DECODE_BASE64[pos[1] & 0x7F];
|
125
|
+
if (remainder == 3) {
|
126
|
+
value <<= 6; value |= DECODE_BASE64[pos[2] & 0x7F];
|
127
|
+
value >>= 2;
|
128
|
+
output[1] = value;
|
129
|
+
value >>= 8;
|
130
|
+
} else {
|
131
|
+
value >>= 4;
|
132
|
+
}
|
133
|
+
output[0] = value;
|
134
|
+
}
|
135
|
+
return input + input_length;
|
136
|
+
}
|
137
|
+
|
138
|
+
|
139
|
+
// implementations of base64.h
|
140
|
+
|
141
|
+
size_t _olm_encode_base64_length(
|
142
|
+
size_t input_length
|
143
|
+
) {
|
144
|
+
return olm::encode_base64_length(input_length);
|
145
|
+
}
|
146
|
+
|
147
|
+
size_t _olm_encode_base64(
|
148
|
+
uint8_t const * input, size_t input_length,
|
149
|
+
uint8_t * output
|
150
|
+
) {
|
151
|
+
uint8_t * r = olm::encode_base64(input, input_length, output);
|
152
|
+
return r - output;
|
153
|
+
}
|
154
|
+
|
155
|
+
size_t _olm_decode_base64_length(
|
156
|
+
size_t input_length
|
157
|
+
) {
|
158
|
+
return olm::decode_base64_length(input_length);
|
159
|
+
}
|
160
|
+
|
161
|
+
size_t _olm_decode_base64(
|
162
|
+
uint8_t const * input, size_t input_length,
|
163
|
+
uint8_t * output
|
164
|
+
) {
|
165
|
+
olm::decode_base64(input, input_length, output);
|
166
|
+
return olm::decode_base64_length(input_length);
|
167
|
+
}
|
@@ -0,0 +1,152 @@
|
|
1
|
+
/* Copyright 2015 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/cipher.h"
|
16
|
+
#include "olm/crypto.h"
|
17
|
+
#include "olm/memory.hh"
|
18
|
+
#include <cstring>
|
19
|
+
|
20
|
+
const std::size_t HMAC_KEY_LENGTH = 32;
|
21
|
+
|
22
|
+
namespace {
|
23
|
+
|
24
|
+
struct DerivedKeys {
|
25
|
+
_olm_aes256_key aes_key;
|
26
|
+
std::uint8_t mac_key[HMAC_KEY_LENGTH];
|
27
|
+
_olm_aes256_iv aes_iv;
|
28
|
+
};
|
29
|
+
|
30
|
+
|
31
|
+
static void derive_keys(
|
32
|
+
std::uint8_t const * kdf_info, std::size_t kdf_info_length,
|
33
|
+
std::uint8_t const * key, std::size_t key_length,
|
34
|
+
DerivedKeys & keys
|
35
|
+
) {
|
36
|
+
std::uint8_t derived_secrets[
|
37
|
+
AES256_KEY_LENGTH + HMAC_KEY_LENGTH + AES256_IV_LENGTH
|
38
|
+
];
|
39
|
+
_olm_crypto_hkdf_sha256(
|
40
|
+
key, key_length,
|
41
|
+
nullptr, 0,
|
42
|
+
kdf_info, kdf_info_length,
|
43
|
+
derived_secrets, sizeof(derived_secrets)
|
44
|
+
);
|
45
|
+
std::uint8_t const * pos = derived_secrets;
|
46
|
+
pos = olm::load_array(keys.aes_key.key, pos);
|
47
|
+
pos = olm::load_array(keys.mac_key, pos);
|
48
|
+
pos = olm::load_array(keys.aes_iv.iv, pos);
|
49
|
+
olm::unset(derived_secrets);
|
50
|
+
}
|
51
|
+
|
52
|
+
static const std::size_t MAC_LENGTH = 8;
|
53
|
+
|
54
|
+
size_t aes_sha_256_cipher_mac_length(const struct _olm_cipher *cipher) {
|
55
|
+
return MAC_LENGTH;
|
56
|
+
}
|
57
|
+
|
58
|
+
size_t aes_sha_256_cipher_encrypt_ciphertext_length(
|
59
|
+
const struct _olm_cipher *cipher, size_t plaintext_length
|
60
|
+
) {
|
61
|
+
return _olm_crypto_aes_encrypt_cbc_length(plaintext_length);
|
62
|
+
}
|
63
|
+
|
64
|
+
size_t aes_sha_256_cipher_encrypt(
|
65
|
+
const struct _olm_cipher *cipher,
|
66
|
+
uint8_t const * key, size_t key_length,
|
67
|
+
uint8_t const * plaintext, size_t plaintext_length,
|
68
|
+
uint8_t * ciphertext, size_t ciphertext_length,
|
69
|
+
uint8_t * output, size_t output_length
|
70
|
+
) {
|
71
|
+
auto *c = reinterpret_cast<const _olm_cipher_aes_sha_256 *>(cipher);
|
72
|
+
|
73
|
+
if (ciphertext_length
|
74
|
+
< aes_sha_256_cipher_encrypt_ciphertext_length(cipher, plaintext_length)
|
75
|
+
|| output_length < MAC_LENGTH) {
|
76
|
+
return std::size_t(-1);
|
77
|
+
}
|
78
|
+
|
79
|
+
struct DerivedKeys keys;
|
80
|
+
std::uint8_t mac[SHA256_OUTPUT_LENGTH];
|
81
|
+
|
82
|
+
derive_keys(c->kdf_info, c->kdf_info_length, key, key_length, keys);
|
83
|
+
|
84
|
+
_olm_crypto_aes_encrypt_cbc(
|
85
|
+
&keys.aes_key, &keys.aes_iv, plaintext, plaintext_length, ciphertext
|
86
|
+
);
|
87
|
+
|
88
|
+
_olm_crypto_hmac_sha256(
|
89
|
+
keys.mac_key, HMAC_KEY_LENGTH, output, output_length - MAC_LENGTH, mac
|
90
|
+
);
|
91
|
+
|
92
|
+
std::memcpy(output + output_length - MAC_LENGTH, mac, MAC_LENGTH);
|
93
|
+
|
94
|
+
olm::unset(keys);
|
95
|
+
return output_length;
|
96
|
+
}
|
97
|
+
|
98
|
+
|
99
|
+
size_t aes_sha_256_cipher_decrypt_max_plaintext_length(
|
100
|
+
const struct _olm_cipher *cipher,
|
101
|
+
size_t ciphertext_length
|
102
|
+
) {
|
103
|
+
return ciphertext_length;
|
104
|
+
}
|
105
|
+
|
106
|
+
size_t aes_sha_256_cipher_decrypt(
|
107
|
+
const struct _olm_cipher *cipher,
|
108
|
+
uint8_t const * key, size_t key_length,
|
109
|
+
uint8_t const * input, size_t input_length,
|
110
|
+
uint8_t const * ciphertext, size_t ciphertext_length,
|
111
|
+
uint8_t * plaintext, size_t max_plaintext_length
|
112
|
+
) {
|
113
|
+
if (max_plaintext_length
|
114
|
+
< aes_sha_256_cipher_decrypt_max_plaintext_length(cipher, ciphertext_length)
|
115
|
+
|| input_length < MAC_LENGTH) {
|
116
|
+
return std::size_t(-1);
|
117
|
+
}
|
118
|
+
|
119
|
+
auto *c = reinterpret_cast<const _olm_cipher_aes_sha_256 *>(cipher);
|
120
|
+
|
121
|
+
DerivedKeys keys;
|
122
|
+
std::uint8_t mac[SHA256_OUTPUT_LENGTH];
|
123
|
+
|
124
|
+
derive_keys(c->kdf_info, c->kdf_info_length, key, key_length, keys);
|
125
|
+
|
126
|
+
_olm_crypto_hmac_sha256(
|
127
|
+
keys.mac_key, HMAC_KEY_LENGTH, input, input_length - MAC_LENGTH, mac
|
128
|
+
);
|
129
|
+
|
130
|
+
std::uint8_t const * input_mac = input + input_length - MAC_LENGTH;
|
131
|
+
if (!olm::is_equal(input_mac, mac, MAC_LENGTH)) {
|
132
|
+
olm::unset(keys);
|
133
|
+
return std::size_t(-1);
|
134
|
+
}
|
135
|
+
|
136
|
+
std::size_t plaintext_length = _olm_crypto_aes_decrypt_cbc(
|
137
|
+
&keys.aes_key, &keys.aes_iv, ciphertext, ciphertext_length, plaintext
|
138
|
+
);
|
139
|
+
|
140
|
+
olm::unset(keys);
|
141
|
+
return plaintext_length;
|
142
|
+
}
|
143
|
+
|
144
|
+
} // namespace
|
145
|
+
|
146
|
+
const struct _olm_cipher_ops _olm_cipher_aes_sha_256_ops = {
|
147
|
+
aes_sha_256_cipher_mac_length,
|
148
|
+
aes_sha_256_cipher_encrypt_ciphertext_length,
|
149
|
+
aes_sha_256_cipher_encrypt,
|
150
|
+
aes_sha_256_cipher_decrypt_max_plaintext_length,
|
151
|
+
aes_sha_256_cipher_decrypt,
|
152
|
+
};
|