ruby_olm 0.1.1
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.
- 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,461 @@
|
|
|
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/session.hh"
|
|
16
|
+
#include "olm/cipher.h"
|
|
17
|
+
#include "olm/crypto.h"
|
|
18
|
+
#include "olm/account.hh"
|
|
19
|
+
#include "olm/memory.hh"
|
|
20
|
+
#include "olm/message.hh"
|
|
21
|
+
#include "olm/pickle.hh"
|
|
22
|
+
|
|
23
|
+
#include <cstring>
|
|
24
|
+
|
|
25
|
+
namespace {
|
|
26
|
+
|
|
27
|
+
static const std::uint8_t PROTOCOL_VERSION = 0x3;
|
|
28
|
+
|
|
29
|
+
static const std::uint8_t ROOT_KDF_INFO[] = "OLM_ROOT";
|
|
30
|
+
static const std::uint8_t RATCHET_KDF_INFO[] = "OLM_RATCHET";
|
|
31
|
+
static const std::uint8_t CIPHER_KDF_INFO[] = "OLM_KEYS";
|
|
32
|
+
|
|
33
|
+
static const olm::KdfInfo OLM_KDF_INFO = {
|
|
34
|
+
ROOT_KDF_INFO, sizeof(ROOT_KDF_INFO) - 1,
|
|
35
|
+
RATCHET_KDF_INFO, sizeof(RATCHET_KDF_INFO) - 1
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
static const struct _olm_cipher_aes_sha_256 OLM_CIPHER =
|
|
39
|
+
OLM_CIPHER_INIT_AES_SHA_256(CIPHER_KDF_INFO);
|
|
40
|
+
|
|
41
|
+
} // namespace
|
|
42
|
+
|
|
43
|
+
olm::Session::Session(
|
|
44
|
+
) : ratchet(OLM_KDF_INFO, OLM_CIPHER_BASE(&OLM_CIPHER)),
|
|
45
|
+
last_error(OlmErrorCode::OLM_SUCCESS),
|
|
46
|
+
received_message(false) {
|
|
47
|
+
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
std::size_t olm::Session::new_outbound_session_random_length() {
|
|
52
|
+
return CURVE25519_RANDOM_LENGTH * 2;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
std::size_t olm::Session::new_outbound_session(
|
|
57
|
+
olm::Account const & local_account,
|
|
58
|
+
_olm_curve25519_public_key const & identity_key,
|
|
59
|
+
_olm_curve25519_public_key const & one_time_key,
|
|
60
|
+
std::uint8_t const * random, std::size_t random_length
|
|
61
|
+
) {
|
|
62
|
+
if (random_length < new_outbound_session_random_length()) {
|
|
63
|
+
last_error = OlmErrorCode::OLM_NOT_ENOUGH_RANDOM;
|
|
64
|
+
return std::size_t(-1);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
_olm_curve25519_key_pair base_key;
|
|
68
|
+
_olm_crypto_curve25519_generate_key(random, &base_key);
|
|
69
|
+
|
|
70
|
+
_olm_curve25519_key_pair ratchet_key;
|
|
71
|
+
_olm_crypto_curve25519_generate_key(random + CURVE25519_RANDOM_LENGTH, &ratchet_key);
|
|
72
|
+
|
|
73
|
+
_olm_curve25519_key_pair const & alice_identity_key_pair = (
|
|
74
|
+
local_account.identity_keys.curve25519_key
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
received_message = false;
|
|
78
|
+
alice_identity_key = alice_identity_key_pair.public_key;
|
|
79
|
+
alice_base_key = base_key.public_key;
|
|
80
|
+
bob_one_time_key = one_time_key;
|
|
81
|
+
|
|
82
|
+
// Calculate the shared secret S via triple DH
|
|
83
|
+
std::uint8_t secret[3 * CURVE25519_SHARED_SECRET_LENGTH];
|
|
84
|
+
std::uint8_t * pos = secret;
|
|
85
|
+
|
|
86
|
+
_olm_crypto_curve25519_shared_secret(&alice_identity_key_pair, &one_time_key, pos);
|
|
87
|
+
pos += CURVE25519_SHARED_SECRET_LENGTH;
|
|
88
|
+
_olm_crypto_curve25519_shared_secret(&base_key, &identity_key, pos);
|
|
89
|
+
pos += CURVE25519_SHARED_SECRET_LENGTH;
|
|
90
|
+
_olm_crypto_curve25519_shared_secret(&base_key, &one_time_key, pos);
|
|
91
|
+
|
|
92
|
+
ratchet.initialise_as_alice(secret, sizeof(secret), ratchet_key);
|
|
93
|
+
|
|
94
|
+
olm::unset(base_key);
|
|
95
|
+
olm::unset(ratchet_key);
|
|
96
|
+
olm::unset(secret);
|
|
97
|
+
|
|
98
|
+
return std::size_t(0);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
namespace {
|
|
102
|
+
|
|
103
|
+
static bool check_message_fields(
|
|
104
|
+
olm::PreKeyMessageReader & reader, bool have_their_identity_key
|
|
105
|
+
) {
|
|
106
|
+
bool ok = true;
|
|
107
|
+
ok = ok && (have_their_identity_key || reader.identity_key);
|
|
108
|
+
if (reader.identity_key) {
|
|
109
|
+
ok = ok && reader.identity_key_length == CURVE25519_KEY_LENGTH;
|
|
110
|
+
}
|
|
111
|
+
ok = ok && reader.message;
|
|
112
|
+
ok = ok && reader.base_key;
|
|
113
|
+
ok = ok && reader.base_key_length == CURVE25519_KEY_LENGTH;
|
|
114
|
+
ok = ok && reader.one_time_key;
|
|
115
|
+
ok = ok && reader.one_time_key_length == CURVE25519_KEY_LENGTH;
|
|
116
|
+
return ok;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
} // namespace
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
std::size_t olm::Session::new_inbound_session(
|
|
123
|
+
olm::Account & local_account,
|
|
124
|
+
_olm_curve25519_public_key const * their_identity_key,
|
|
125
|
+
std::uint8_t const * one_time_key_message, std::size_t message_length
|
|
126
|
+
) {
|
|
127
|
+
olm::PreKeyMessageReader reader;
|
|
128
|
+
decode_one_time_key_message(reader, one_time_key_message, message_length);
|
|
129
|
+
|
|
130
|
+
if (!check_message_fields(reader, their_identity_key)) {
|
|
131
|
+
last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
|
|
132
|
+
return std::size_t(-1);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (reader.identity_key && their_identity_key) {
|
|
136
|
+
bool same = 0 == std::memcmp(
|
|
137
|
+
their_identity_key->public_key, reader.identity_key, CURVE25519_KEY_LENGTH
|
|
138
|
+
);
|
|
139
|
+
if (!same) {
|
|
140
|
+
last_error = OlmErrorCode::OLM_BAD_MESSAGE_KEY_ID;
|
|
141
|
+
return std::size_t(-1);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
olm::load_array(alice_identity_key.public_key, reader.identity_key);
|
|
146
|
+
olm::load_array(alice_base_key.public_key, reader.base_key);
|
|
147
|
+
olm::load_array(bob_one_time_key.public_key, reader.one_time_key);
|
|
148
|
+
|
|
149
|
+
olm::MessageReader message_reader;
|
|
150
|
+
decode_message(
|
|
151
|
+
message_reader, reader.message, reader.message_length,
|
|
152
|
+
ratchet.ratchet_cipher->ops->mac_length(ratchet.ratchet_cipher)
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
if (!message_reader.ratchet_key
|
|
156
|
+
|| message_reader.ratchet_key_length != CURVE25519_KEY_LENGTH) {
|
|
157
|
+
last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
|
|
158
|
+
return std::size_t(-1);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
_olm_curve25519_public_key ratchet_key;
|
|
162
|
+
olm::load_array(ratchet_key.public_key, message_reader.ratchet_key);
|
|
163
|
+
|
|
164
|
+
olm::OneTimeKey const * our_one_time_key = local_account.lookup_key(
|
|
165
|
+
bob_one_time_key
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
if (!our_one_time_key) {
|
|
169
|
+
last_error = OlmErrorCode::OLM_BAD_MESSAGE_KEY_ID;
|
|
170
|
+
return std::size_t(-1);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
_olm_curve25519_key_pair const & bob_identity_key = (
|
|
174
|
+
local_account.identity_keys.curve25519_key
|
|
175
|
+
);
|
|
176
|
+
_olm_curve25519_key_pair const & bob_one_time_key = our_one_time_key->key;
|
|
177
|
+
|
|
178
|
+
// Calculate the shared secret S via triple DH
|
|
179
|
+
std::uint8_t secret[CURVE25519_SHARED_SECRET_LENGTH * 3];
|
|
180
|
+
std::uint8_t * pos = secret;
|
|
181
|
+
_olm_crypto_curve25519_shared_secret(&bob_one_time_key, &alice_identity_key, pos);
|
|
182
|
+
pos += CURVE25519_SHARED_SECRET_LENGTH;
|
|
183
|
+
_olm_crypto_curve25519_shared_secret(&bob_identity_key, &alice_base_key, pos);
|
|
184
|
+
pos += CURVE25519_SHARED_SECRET_LENGTH;
|
|
185
|
+
_olm_crypto_curve25519_shared_secret(&bob_one_time_key, &alice_base_key, pos);
|
|
186
|
+
|
|
187
|
+
ratchet.initialise_as_bob(secret, sizeof(secret), ratchet_key);
|
|
188
|
+
|
|
189
|
+
olm::unset(secret);
|
|
190
|
+
|
|
191
|
+
return std::size_t(0);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
std::size_t olm::Session::session_id_length() {
|
|
196
|
+
return SHA256_OUTPUT_LENGTH;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
std::size_t olm::Session::session_id(
|
|
201
|
+
std::uint8_t * id, std::size_t id_length
|
|
202
|
+
) {
|
|
203
|
+
if (id_length < session_id_length()) {
|
|
204
|
+
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
|
205
|
+
return std::size_t(-1);
|
|
206
|
+
}
|
|
207
|
+
std::uint8_t tmp[CURVE25519_KEY_LENGTH * 3];
|
|
208
|
+
std::uint8_t * pos = tmp;
|
|
209
|
+
pos = olm::store_array(pos, alice_identity_key.public_key);
|
|
210
|
+
pos = olm::store_array(pos, alice_base_key.public_key);
|
|
211
|
+
pos = olm::store_array(pos, bob_one_time_key.public_key);
|
|
212
|
+
_olm_crypto_sha256(tmp, sizeof(tmp), id);
|
|
213
|
+
return session_id_length();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
bool olm::Session::matches_inbound_session(
|
|
217
|
+
_olm_curve25519_public_key const * their_identity_key,
|
|
218
|
+
std::uint8_t const * one_time_key_message, std::size_t message_length
|
|
219
|
+
) {
|
|
220
|
+
olm::PreKeyMessageReader reader;
|
|
221
|
+
decode_one_time_key_message(reader, one_time_key_message, message_length);
|
|
222
|
+
|
|
223
|
+
if (!check_message_fields(reader, their_identity_key)) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
bool same = true;
|
|
228
|
+
if (reader.identity_key) {
|
|
229
|
+
same = same && 0 == std::memcmp(
|
|
230
|
+
reader.identity_key, alice_identity_key.public_key, CURVE25519_KEY_LENGTH
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
if (their_identity_key) {
|
|
234
|
+
same = same && 0 == std::memcmp(
|
|
235
|
+
their_identity_key->public_key, alice_identity_key.public_key,
|
|
236
|
+
CURVE25519_KEY_LENGTH
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
same = same && 0 == std::memcmp(
|
|
240
|
+
reader.base_key, alice_base_key.public_key, CURVE25519_KEY_LENGTH
|
|
241
|
+
);
|
|
242
|
+
same = same && 0 == std::memcmp(
|
|
243
|
+
reader.one_time_key, bob_one_time_key.public_key, CURVE25519_KEY_LENGTH
|
|
244
|
+
);
|
|
245
|
+
return same;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
olm::MessageType olm::Session::encrypt_message_type() {
|
|
250
|
+
if (received_message) {
|
|
251
|
+
return olm::MessageType::MESSAGE;
|
|
252
|
+
} else {
|
|
253
|
+
return olm::MessageType::PRE_KEY;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
std::size_t olm::Session::encrypt_message_length(
|
|
259
|
+
std::size_t plaintext_length
|
|
260
|
+
) {
|
|
261
|
+
std::size_t message_length = ratchet.encrypt_output_length(
|
|
262
|
+
plaintext_length
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
if (received_message) {
|
|
266
|
+
return message_length;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return encode_one_time_key_message_length(
|
|
270
|
+
CURVE25519_KEY_LENGTH,
|
|
271
|
+
CURVE25519_KEY_LENGTH,
|
|
272
|
+
CURVE25519_KEY_LENGTH,
|
|
273
|
+
message_length
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
std::size_t olm::Session::encrypt_random_length() {
|
|
279
|
+
return ratchet.encrypt_random_length();
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
std::size_t olm::Session::encrypt(
|
|
284
|
+
std::uint8_t const * plaintext, std::size_t plaintext_length,
|
|
285
|
+
std::uint8_t const * random, std::size_t random_length,
|
|
286
|
+
std::uint8_t * message, std::size_t message_length
|
|
287
|
+
) {
|
|
288
|
+
if (message_length < encrypt_message_length(plaintext_length)) {
|
|
289
|
+
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
|
290
|
+
return std::size_t(-1);
|
|
291
|
+
}
|
|
292
|
+
std::uint8_t * message_body;
|
|
293
|
+
std::size_t message_body_length = ratchet.encrypt_output_length(
|
|
294
|
+
plaintext_length
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
if (received_message) {
|
|
298
|
+
message_body = message;
|
|
299
|
+
} else {
|
|
300
|
+
olm::PreKeyMessageWriter writer;
|
|
301
|
+
encode_one_time_key_message(
|
|
302
|
+
writer,
|
|
303
|
+
PROTOCOL_VERSION,
|
|
304
|
+
CURVE25519_KEY_LENGTH,
|
|
305
|
+
CURVE25519_KEY_LENGTH,
|
|
306
|
+
CURVE25519_KEY_LENGTH,
|
|
307
|
+
message_body_length,
|
|
308
|
+
message
|
|
309
|
+
);
|
|
310
|
+
olm::store_array(writer.one_time_key, bob_one_time_key.public_key);
|
|
311
|
+
olm::store_array(writer.identity_key, alice_identity_key.public_key);
|
|
312
|
+
olm::store_array(writer.base_key, alice_base_key.public_key);
|
|
313
|
+
message_body = writer.message;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
std::size_t result = ratchet.encrypt(
|
|
317
|
+
plaintext, plaintext_length,
|
|
318
|
+
random, random_length,
|
|
319
|
+
message_body, message_body_length
|
|
320
|
+
);
|
|
321
|
+
|
|
322
|
+
if (result == std::size_t(-1)) {
|
|
323
|
+
last_error = ratchet.last_error;
|
|
324
|
+
ratchet.last_error = OlmErrorCode::OLM_SUCCESS;
|
|
325
|
+
return result;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
std::size_t olm::Session::decrypt_max_plaintext_length(
|
|
333
|
+
MessageType message_type,
|
|
334
|
+
std::uint8_t const * message, std::size_t message_length
|
|
335
|
+
) {
|
|
336
|
+
std::uint8_t const * message_body;
|
|
337
|
+
std::size_t message_body_length;
|
|
338
|
+
if (message_type == olm::MessageType::MESSAGE) {
|
|
339
|
+
message_body = message;
|
|
340
|
+
message_body_length = message_length;
|
|
341
|
+
} else {
|
|
342
|
+
olm::PreKeyMessageReader reader;
|
|
343
|
+
decode_one_time_key_message(reader, message, message_length);
|
|
344
|
+
if (!reader.message) {
|
|
345
|
+
last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
|
|
346
|
+
return std::size_t(-1);
|
|
347
|
+
}
|
|
348
|
+
message_body = reader.message;
|
|
349
|
+
message_body_length = reader.message_length;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
std::size_t result = ratchet.decrypt_max_plaintext_length(
|
|
353
|
+
message_body, message_body_length
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
if (result == std::size_t(-1)) {
|
|
357
|
+
last_error = ratchet.last_error;
|
|
358
|
+
ratchet.last_error = OlmErrorCode::OLM_SUCCESS;
|
|
359
|
+
}
|
|
360
|
+
return result;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
std::size_t olm::Session::decrypt(
|
|
365
|
+
olm::MessageType message_type,
|
|
366
|
+
std::uint8_t const * message, std::size_t message_length,
|
|
367
|
+
std::uint8_t * plaintext, std::size_t max_plaintext_length
|
|
368
|
+
) {
|
|
369
|
+
std::uint8_t const * message_body;
|
|
370
|
+
std::size_t message_body_length;
|
|
371
|
+
if (message_type == olm::MessageType::MESSAGE) {
|
|
372
|
+
message_body = message;
|
|
373
|
+
message_body_length = message_length;
|
|
374
|
+
} else {
|
|
375
|
+
olm::PreKeyMessageReader reader;
|
|
376
|
+
decode_one_time_key_message(reader, message, message_length);
|
|
377
|
+
if (!reader.message) {
|
|
378
|
+
last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
|
|
379
|
+
return std::size_t(-1);
|
|
380
|
+
}
|
|
381
|
+
message_body = reader.message;
|
|
382
|
+
message_body_length = reader.message_length;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
std::size_t result = ratchet.decrypt(
|
|
386
|
+
message_body, message_body_length, plaintext, max_plaintext_length
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
if (result == std::size_t(-1)) {
|
|
390
|
+
last_error = ratchet.last_error;
|
|
391
|
+
ratchet.last_error = OlmErrorCode::OLM_SUCCESS;
|
|
392
|
+
return result;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
received_message = true;
|
|
396
|
+
return result;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
namespace {
|
|
400
|
+
// the master branch writes pickle version 1; the logging_enabled branch writes
|
|
401
|
+
// 0x80000001.
|
|
402
|
+
static const std::uint32_t SESSION_PICKLE_VERSION = 1;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
std::size_t olm::pickle_length(
|
|
406
|
+
Session const & value
|
|
407
|
+
) {
|
|
408
|
+
std::size_t length = 0;
|
|
409
|
+
length += olm::pickle_length(SESSION_PICKLE_VERSION);
|
|
410
|
+
length += olm::pickle_length(value.received_message);
|
|
411
|
+
length += olm::pickle_length(value.alice_identity_key);
|
|
412
|
+
length += olm::pickle_length(value.alice_base_key);
|
|
413
|
+
length += olm::pickle_length(value.bob_one_time_key);
|
|
414
|
+
length += olm::pickle_length(value.ratchet);
|
|
415
|
+
return length;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
std::uint8_t * olm::pickle(
|
|
420
|
+
std::uint8_t * pos,
|
|
421
|
+
Session const & value
|
|
422
|
+
) {
|
|
423
|
+
pos = olm::pickle(pos, SESSION_PICKLE_VERSION);
|
|
424
|
+
pos = olm::pickle(pos, value.received_message);
|
|
425
|
+
pos = olm::pickle(pos, value.alice_identity_key);
|
|
426
|
+
pos = olm::pickle(pos, value.alice_base_key);
|
|
427
|
+
pos = olm::pickle(pos, value.bob_one_time_key);
|
|
428
|
+
pos = olm::pickle(pos, value.ratchet);
|
|
429
|
+
return pos;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
std::uint8_t const * olm::unpickle(
|
|
434
|
+
std::uint8_t const * pos, std::uint8_t const * end,
|
|
435
|
+
Session & value
|
|
436
|
+
) {
|
|
437
|
+
uint32_t pickle_version;
|
|
438
|
+
pos = olm::unpickle(pos, end, pickle_version);
|
|
439
|
+
|
|
440
|
+
bool includes_chain_index;
|
|
441
|
+
switch (pickle_version) {
|
|
442
|
+
case 1:
|
|
443
|
+
includes_chain_index = false;
|
|
444
|
+
break;
|
|
445
|
+
|
|
446
|
+
case 0x80000001UL:
|
|
447
|
+
includes_chain_index = true;
|
|
448
|
+
break;
|
|
449
|
+
|
|
450
|
+
default:
|
|
451
|
+
value.last_error = OlmErrorCode::OLM_UNKNOWN_PICKLE_VERSION;
|
|
452
|
+
return end;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
pos = olm::unpickle(pos, end, value.received_message);
|
|
456
|
+
pos = olm::unpickle(pos, end, value.alice_identity_key);
|
|
457
|
+
pos = olm::unpickle(pos, end, value.alice_base_key);
|
|
458
|
+
pos = olm::unpickle(pos, end, value.bob_one_time_key);
|
|
459
|
+
pos = olm::unpickle(pos, end, value.ratchet, includes_chain_index);
|
|
460
|
+
return pos;
|
|
461
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: sha256.c
|
|
3
|
+
* Author: Brad Conte (brad AT bradconte.com)
|
|
4
|
+
* Copyright:
|
|
5
|
+
* Disclaimer: This code is presented "as is" without any guarantees.
|
|
6
|
+
* Details: Implementation of the SHA-256 hashing algorithm.
|
|
7
|
+
SHA-256 is one of the three algorithms in the SHA2
|
|
8
|
+
specification. The others, SHA-384 and SHA-512, are not
|
|
9
|
+
offered in this implementation.
|
|
10
|
+
Algorithm specification can be found here:
|
|
11
|
+
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
|
|
12
|
+
This implementation uses little endian byte order.
|
|
13
|
+
*********************************************************************/
|
|
14
|
+
|
|
15
|
+
/*************************** HEADER FILES ***************************/
|
|
16
|
+
#include <stdlib.h>
|
|
17
|
+
#include <memory.h>
|
|
18
|
+
#include <string.h>
|
|
19
|
+
#include "sha256.h"
|
|
20
|
+
|
|
21
|
+
/****************************** MACROS ******************************/
|
|
22
|
+
#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
|
|
23
|
+
#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
|
|
24
|
+
|
|
25
|
+
#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
|
|
26
|
+
#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
|
|
27
|
+
#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
|
|
28
|
+
#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
|
|
29
|
+
#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
|
|
30
|
+
#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
|
|
31
|
+
|
|
32
|
+
/**************************** VARIABLES *****************************/
|
|
33
|
+
static const WORD k[64] = {
|
|
34
|
+
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
|
|
35
|
+
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
|
|
36
|
+
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
|
|
37
|
+
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
|
|
38
|
+
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
|
|
39
|
+
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
|
|
40
|
+
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
|
|
41
|
+
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/*********************** FUNCTION DEFINITIONS ***********************/
|
|
45
|
+
void sha256_transform(SHA256_CTX *ctx, const BYTE data[])
|
|
46
|
+
{
|
|
47
|
+
WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
|
|
48
|
+
|
|
49
|
+
for (i = 0, j = 0; i < 16; ++i, j += 4)
|
|
50
|
+
m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
|
|
51
|
+
for ( ; i < 64; ++i)
|
|
52
|
+
m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
|
|
53
|
+
|
|
54
|
+
a = ctx->state[0];
|
|
55
|
+
b = ctx->state[1];
|
|
56
|
+
c = ctx->state[2];
|
|
57
|
+
d = ctx->state[3];
|
|
58
|
+
e = ctx->state[4];
|
|
59
|
+
f = ctx->state[5];
|
|
60
|
+
g = ctx->state[6];
|
|
61
|
+
h = ctx->state[7];
|
|
62
|
+
|
|
63
|
+
for (i = 0; i < 64; ++i) {
|
|
64
|
+
t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
|
|
65
|
+
t2 = EP0(a) + MAJ(a,b,c);
|
|
66
|
+
h = g;
|
|
67
|
+
g = f;
|
|
68
|
+
f = e;
|
|
69
|
+
e = d + t1;
|
|
70
|
+
d = c;
|
|
71
|
+
c = b;
|
|
72
|
+
b = a;
|
|
73
|
+
a = t1 + t2;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
ctx->state[0] += a;
|
|
77
|
+
ctx->state[1] += b;
|
|
78
|
+
ctx->state[2] += c;
|
|
79
|
+
ctx->state[3] += d;
|
|
80
|
+
ctx->state[4] += e;
|
|
81
|
+
ctx->state[5] += f;
|
|
82
|
+
ctx->state[6] += g;
|
|
83
|
+
ctx->state[7] += h;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
void sha256_init(SHA256_CTX *ctx)
|
|
87
|
+
{
|
|
88
|
+
ctx->datalen = 0;
|
|
89
|
+
ctx->bitlen = 0;
|
|
90
|
+
ctx->state[0] = 0x6a09e667;
|
|
91
|
+
ctx->state[1] = 0xbb67ae85;
|
|
92
|
+
ctx->state[2] = 0x3c6ef372;
|
|
93
|
+
ctx->state[3] = 0xa54ff53a;
|
|
94
|
+
ctx->state[4] = 0x510e527f;
|
|
95
|
+
ctx->state[5] = 0x9b05688c;
|
|
96
|
+
ctx->state[6] = 0x1f83d9ab;
|
|
97
|
+
ctx->state[7] = 0x5be0cd19;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)
|
|
101
|
+
{
|
|
102
|
+
WORD i;
|
|
103
|
+
|
|
104
|
+
for (i = 0; i < len; ++i) {
|
|
105
|
+
ctx->data[ctx->datalen] = data[i];
|
|
106
|
+
ctx->datalen++;
|
|
107
|
+
if (ctx->datalen == 64) {
|
|
108
|
+
sha256_transform(ctx, ctx->data);
|
|
109
|
+
ctx->bitlen += 512;
|
|
110
|
+
ctx->datalen = 0;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
void sha256_final(SHA256_CTX *ctx, BYTE hash[])
|
|
116
|
+
{
|
|
117
|
+
WORD i;
|
|
118
|
+
|
|
119
|
+
i = ctx->datalen;
|
|
120
|
+
|
|
121
|
+
// Pad whatever data is left in the buffer.
|
|
122
|
+
if (ctx->datalen < 56) {
|
|
123
|
+
ctx->data[i++] = 0x80;
|
|
124
|
+
while (i < 56)
|
|
125
|
+
ctx->data[i++] = 0x00;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
ctx->data[i++] = 0x80;
|
|
129
|
+
while (i < 64)
|
|
130
|
+
ctx->data[i++] = 0x00;
|
|
131
|
+
sha256_transform(ctx, ctx->data);
|
|
132
|
+
memset(ctx->data, 0, 56);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Append to the padding the total message's length in bits and transform.
|
|
136
|
+
ctx->bitlen += ctx->datalen * 8;
|
|
137
|
+
ctx->data[63] = ctx->bitlen;
|
|
138
|
+
ctx->data[62] = ctx->bitlen >> 8;
|
|
139
|
+
ctx->data[61] = ctx->bitlen >> 16;
|
|
140
|
+
ctx->data[60] = ctx->bitlen >> 24;
|
|
141
|
+
ctx->data[59] = ctx->bitlen >> 32;
|
|
142
|
+
ctx->data[58] = ctx->bitlen >> 40;
|
|
143
|
+
ctx->data[57] = ctx->bitlen >> 48;
|
|
144
|
+
ctx->data[56] = ctx->bitlen >> 56;
|
|
145
|
+
sha256_transform(ctx, ctx->data);
|
|
146
|
+
|
|
147
|
+
// Since this implementation uses little endian byte ordering and SHA uses big endian,
|
|
148
|
+
// reverse all the bytes when copying the final state to the output hash.
|
|
149
|
+
for (i = 0; i < 4; ++i) {
|
|
150
|
+
hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
|
|
151
|
+
hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
|
|
152
|
+
hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
|
|
153
|
+
hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
|
|
154
|
+
hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
|
|
155
|
+
hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
|
|
156
|
+
hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
|
|
157
|
+
hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
|
|
16
|
+
#include "olm/utility.hh"
|
|
17
|
+
#include "olm/crypto.h"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
olm::Utility::Utility(
|
|
21
|
+
) : last_error(OlmErrorCode::OLM_SUCCESS) {
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
size_t olm::Utility::sha256_length() {
|
|
26
|
+
return SHA256_OUTPUT_LENGTH;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
size_t olm::Utility::sha256(
|
|
31
|
+
std::uint8_t const * input, std::size_t input_length,
|
|
32
|
+
std::uint8_t * output, std::size_t output_length
|
|
33
|
+
) {
|
|
34
|
+
if (output_length < sha256_length()) {
|
|
35
|
+
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
|
36
|
+
return std::size_t(-1);
|
|
37
|
+
}
|
|
38
|
+
_olm_crypto_sha256(input, input_length, output);
|
|
39
|
+
return SHA256_OUTPUT_LENGTH;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
size_t olm::Utility::ed25519_verify(
|
|
44
|
+
_olm_ed25519_public_key const & key,
|
|
45
|
+
std::uint8_t const * message, std::size_t message_length,
|
|
46
|
+
std::uint8_t const * signature, std::size_t signature_length
|
|
47
|
+
) {
|
|
48
|
+
if (signature_length < ED25519_SIGNATURE_LENGTH) {
|
|
49
|
+
last_error = OlmErrorCode::OLM_BAD_MESSAGE_MAC;
|
|
50
|
+
return std::size_t(-1);
|
|
51
|
+
}
|
|
52
|
+
if (!_olm_crypto_ed25519_verify(&key, message, message_length, signature)) {
|
|
53
|
+
last_error = OlmErrorCode::OLM_BAD_MESSAGE_MAC;
|
|
54
|
+
return std::size_t(-1);
|
|
55
|
+
}
|
|
56
|
+
return std::size_t(0);
|
|
57
|
+
}
|