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,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
|
+
};
|
@@ -0,0 +1,299 @@
|
|
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/crypto.h"
|
16
|
+
#include "olm/memory.hh"
|
17
|
+
|
18
|
+
#include <cstring>
|
19
|
+
|
20
|
+
extern "C" {
|
21
|
+
|
22
|
+
#include "crypto-algorithms/aes.h"
|
23
|
+
#include "crypto-algorithms/sha256.h"
|
24
|
+
|
25
|
+
}
|
26
|
+
|
27
|
+
#include "ed25519/src/ed25519.h"
|
28
|
+
#include "curve25519-donna.h"
|
29
|
+
|
30
|
+
namespace {
|
31
|
+
|
32
|
+
static const std::uint8_t CURVE25519_BASEPOINT[32] = {9};
|
33
|
+
static const std::size_t AES_KEY_SCHEDULE_LENGTH = 60;
|
34
|
+
static const std::size_t AES_KEY_BITS = 8 * AES256_KEY_LENGTH;
|
35
|
+
static const std::size_t AES_BLOCK_LENGTH = 16;
|
36
|
+
static const std::size_t SHA256_BLOCK_LENGTH = 64;
|
37
|
+
static const std::uint8_t HKDF_DEFAULT_SALT[32] = {};
|
38
|
+
|
39
|
+
|
40
|
+
template<std::size_t block_size>
|
41
|
+
inline static void xor_block(
|
42
|
+
std::uint8_t * block,
|
43
|
+
std::uint8_t const * input
|
44
|
+
) {
|
45
|
+
for (std::size_t i = 0; i < block_size; ++i) {
|
46
|
+
block[i] ^= input[i];
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
|
51
|
+
inline static void hmac_sha256_key(
|
52
|
+
std::uint8_t const * input_key, std::size_t input_key_length,
|
53
|
+
std::uint8_t * hmac_key
|
54
|
+
) {
|
55
|
+
std::memset(hmac_key, 0, SHA256_BLOCK_LENGTH);
|
56
|
+
if (input_key_length > SHA256_BLOCK_LENGTH) {
|
57
|
+
::SHA256_CTX context;
|
58
|
+
::sha256_init(&context);
|
59
|
+
::sha256_update(&context, input_key, input_key_length);
|
60
|
+
::sha256_final(&context, hmac_key);
|
61
|
+
} else {
|
62
|
+
std::memcpy(hmac_key, input_key, input_key_length);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
|
67
|
+
inline static void hmac_sha256_init(
|
68
|
+
::SHA256_CTX * context,
|
69
|
+
std::uint8_t const * hmac_key
|
70
|
+
) {
|
71
|
+
std::uint8_t i_pad[SHA256_BLOCK_LENGTH];
|
72
|
+
std::memcpy(i_pad, hmac_key, SHA256_BLOCK_LENGTH);
|
73
|
+
for (std::size_t i = 0; i < SHA256_BLOCK_LENGTH; ++i) {
|
74
|
+
i_pad[i] ^= 0x36;
|
75
|
+
}
|
76
|
+
::sha256_init(context);
|
77
|
+
::sha256_update(context, i_pad, SHA256_BLOCK_LENGTH);
|
78
|
+
olm::unset(i_pad);
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
inline static void hmac_sha256_final(
|
83
|
+
::SHA256_CTX * context,
|
84
|
+
std::uint8_t const * hmac_key,
|
85
|
+
std::uint8_t * output
|
86
|
+
) {
|
87
|
+
std::uint8_t o_pad[SHA256_BLOCK_LENGTH + SHA256_OUTPUT_LENGTH];
|
88
|
+
std::memcpy(o_pad, hmac_key, SHA256_BLOCK_LENGTH);
|
89
|
+
for (std::size_t i = 0; i < SHA256_BLOCK_LENGTH; ++i) {
|
90
|
+
o_pad[i] ^= 0x5C;
|
91
|
+
}
|
92
|
+
::sha256_final(context, o_pad + SHA256_BLOCK_LENGTH);
|
93
|
+
::SHA256_CTX final_context;
|
94
|
+
::sha256_init(&final_context);
|
95
|
+
::sha256_update(&final_context, o_pad, sizeof(o_pad));
|
96
|
+
::sha256_final(&final_context, output);
|
97
|
+
olm::unset(final_context);
|
98
|
+
olm::unset(o_pad);
|
99
|
+
}
|
100
|
+
|
101
|
+
} // namespace
|
102
|
+
|
103
|
+
void _olm_crypto_curve25519_generate_key(
|
104
|
+
uint8_t const * random_32_bytes,
|
105
|
+
struct _olm_curve25519_key_pair *key_pair
|
106
|
+
) {
|
107
|
+
std::memcpy(
|
108
|
+
key_pair->private_key.private_key, random_32_bytes,
|
109
|
+
CURVE25519_KEY_LENGTH
|
110
|
+
);
|
111
|
+
::curve25519_donna(
|
112
|
+
key_pair->public_key.public_key,
|
113
|
+
key_pair->private_key.private_key,
|
114
|
+
CURVE25519_BASEPOINT
|
115
|
+
);
|
116
|
+
}
|
117
|
+
|
118
|
+
|
119
|
+
void _olm_crypto_curve25519_shared_secret(
|
120
|
+
const struct _olm_curve25519_key_pair *our_key,
|
121
|
+
const struct _olm_curve25519_public_key * their_key,
|
122
|
+
std::uint8_t * output
|
123
|
+
) {
|
124
|
+
::curve25519_donna(output, our_key->private_key.private_key, their_key->public_key);
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
void _olm_crypto_ed25519_generate_key(
|
129
|
+
std::uint8_t const * random_32_bytes,
|
130
|
+
struct _olm_ed25519_key_pair *key_pair
|
131
|
+
) {
|
132
|
+
::ed25519_create_keypair(
|
133
|
+
key_pair->public_key.public_key, key_pair->private_key.private_key,
|
134
|
+
random_32_bytes
|
135
|
+
);
|
136
|
+
}
|
137
|
+
|
138
|
+
|
139
|
+
void _olm_crypto_ed25519_sign(
|
140
|
+
const struct _olm_ed25519_key_pair *our_key,
|
141
|
+
std::uint8_t const * message, std::size_t message_length,
|
142
|
+
std::uint8_t * output
|
143
|
+
) {
|
144
|
+
::ed25519_sign(
|
145
|
+
output,
|
146
|
+
message, message_length,
|
147
|
+
our_key->public_key.public_key,
|
148
|
+
our_key->private_key.private_key
|
149
|
+
);
|
150
|
+
}
|
151
|
+
|
152
|
+
|
153
|
+
int _olm_crypto_ed25519_verify(
|
154
|
+
const struct _olm_ed25519_public_key *their_key,
|
155
|
+
std::uint8_t const * message, std::size_t message_length,
|
156
|
+
std::uint8_t const * signature
|
157
|
+
) {
|
158
|
+
return 0 != ::ed25519_verify(
|
159
|
+
signature,
|
160
|
+
message, message_length,
|
161
|
+
their_key->public_key
|
162
|
+
);
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
std::size_t _olm_crypto_aes_encrypt_cbc_length(
|
167
|
+
std::size_t input_length
|
168
|
+
) {
|
169
|
+
return input_length + AES_BLOCK_LENGTH - input_length % AES_BLOCK_LENGTH;
|
170
|
+
}
|
171
|
+
|
172
|
+
|
173
|
+
void _olm_crypto_aes_encrypt_cbc(
|
174
|
+
_olm_aes256_key const *key,
|
175
|
+
_olm_aes256_iv const *iv,
|
176
|
+
std::uint8_t const * input, std::size_t input_length,
|
177
|
+
std::uint8_t * output
|
178
|
+
) {
|
179
|
+
std::uint32_t key_schedule[AES_KEY_SCHEDULE_LENGTH];
|
180
|
+
::aes_key_setup(key->key, key_schedule, AES_KEY_BITS);
|
181
|
+
std::uint8_t input_block[AES_BLOCK_LENGTH];
|
182
|
+
std::memcpy(input_block, iv->iv, AES_BLOCK_LENGTH);
|
183
|
+
while (input_length >= AES_BLOCK_LENGTH) {
|
184
|
+
xor_block<AES_BLOCK_LENGTH>(input_block, input);
|
185
|
+
::aes_encrypt(input_block, output, key_schedule, AES_KEY_BITS);
|
186
|
+
std::memcpy(input_block, output, AES_BLOCK_LENGTH);
|
187
|
+
input += AES_BLOCK_LENGTH;
|
188
|
+
output += AES_BLOCK_LENGTH;
|
189
|
+
input_length -= AES_BLOCK_LENGTH;
|
190
|
+
}
|
191
|
+
std::size_t i = 0;
|
192
|
+
for (; i < input_length; ++i) {
|
193
|
+
input_block[i] ^= input[i];
|
194
|
+
}
|
195
|
+
for (; i < AES_BLOCK_LENGTH; ++i) {
|
196
|
+
input_block[i] ^= AES_BLOCK_LENGTH - input_length;
|
197
|
+
}
|
198
|
+
::aes_encrypt(input_block, output, key_schedule, AES_KEY_BITS);
|
199
|
+
olm::unset(key_schedule);
|
200
|
+
olm::unset(input_block);
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
std::size_t _olm_crypto_aes_decrypt_cbc(
|
205
|
+
_olm_aes256_key const *key,
|
206
|
+
_olm_aes256_iv const *iv,
|
207
|
+
std::uint8_t const * input, std::size_t input_length,
|
208
|
+
std::uint8_t * output
|
209
|
+
) {
|
210
|
+
std::uint32_t key_schedule[AES_KEY_SCHEDULE_LENGTH];
|
211
|
+
::aes_key_setup(key->key, key_schedule, AES_KEY_BITS);
|
212
|
+
std::uint8_t block1[AES_BLOCK_LENGTH];
|
213
|
+
std::uint8_t block2[AES_BLOCK_LENGTH];
|
214
|
+
std::memcpy(block1, iv->iv, AES_BLOCK_LENGTH);
|
215
|
+
for (std::size_t i = 0; i < input_length; i += AES_BLOCK_LENGTH) {
|
216
|
+
std::memcpy(block2, &input[i], AES_BLOCK_LENGTH);
|
217
|
+
::aes_decrypt(&input[i], &output[i], key_schedule, AES_KEY_BITS);
|
218
|
+
xor_block<AES_BLOCK_LENGTH>(&output[i], block1);
|
219
|
+
std::memcpy(block1, block2, AES_BLOCK_LENGTH);
|
220
|
+
}
|
221
|
+
olm::unset(key_schedule);
|
222
|
+
olm::unset(block1);
|
223
|
+
olm::unset(block2);
|
224
|
+
std::size_t padding = output[input_length - 1];
|
225
|
+
return (padding > input_length) ? std::size_t(-1) : (input_length - padding);
|
226
|
+
}
|
227
|
+
|
228
|
+
|
229
|
+
void _olm_crypto_sha256(
|
230
|
+
std::uint8_t const * input, std::size_t input_length,
|
231
|
+
std::uint8_t * output
|
232
|
+
) {
|
233
|
+
::SHA256_CTX context;
|
234
|
+
::sha256_init(&context);
|
235
|
+
::sha256_update(&context, input, input_length);
|
236
|
+
::sha256_final(&context, output);
|
237
|
+
olm::unset(context);
|
238
|
+
}
|
239
|
+
|
240
|
+
|
241
|
+
void _olm_crypto_hmac_sha256(
|
242
|
+
std::uint8_t const * key, std::size_t key_length,
|
243
|
+
std::uint8_t const * input, std::size_t input_length,
|
244
|
+
std::uint8_t * output
|
245
|
+
) {
|
246
|
+
std::uint8_t hmac_key[SHA256_BLOCK_LENGTH];
|
247
|
+
::SHA256_CTX context;
|
248
|
+
hmac_sha256_key(key, key_length, hmac_key);
|
249
|
+
hmac_sha256_init(&context, hmac_key);
|
250
|
+
::sha256_update(&context, input, input_length);
|
251
|
+
hmac_sha256_final(&context, hmac_key, output);
|
252
|
+
olm::unset(hmac_key);
|
253
|
+
olm::unset(context);
|
254
|
+
}
|
255
|
+
|
256
|
+
|
257
|
+
void _olm_crypto_hkdf_sha256(
|
258
|
+
std::uint8_t const * input, std::size_t input_length,
|
259
|
+
std::uint8_t const * salt, std::size_t salt_length,
|
260
|
+
std::uint8_t const * info, std::size_t info_length,
|
261
|
+
std::uint8_t * output, std::size_t output_length
|
262
|
+
) {
|
263
|
+
::SHA256_CTX context;
|
264
|
+
std::uint8_t hmac_key[SHA256_BLOCK_LENGTH];
|
265
|
+
std::uint8_t step_result[SHA256_OUTPUT_LENGTH];
|
266
|
+
std::size_t bytes_remaining = output_length;
|
267
|
+
std::uint8_t iteration = 1;
|
268
|
+
if (!salt) {
|
269
|
+
salt = HKDF_DEFAULT_SALT;
|
270
|
+
salt_length = sizeof(HKDF_DEFAULT_SALT);
|
271
|
+
}
|
272
|
+
/* Extract */
|
273
|
+
hmac_sha256_key(salt, salt_length, hmac_key);
|
274
|
+
hmac_sha256_init(&context, hmac_key);
|
275
|
+
::sha256_update(&context, input, input_length);
|
276
|
+
hmac_sha256_final(&context, hmac_key, step_result);
|
277
|
+
hmac_sha256_key(step_result, SHA256_OUTPUT_LENGTH, hmac_key);
|
278
|
+
|
279
|
+
/* Expand */
|
280
|
+
hmac_sha256_init(&context, hmac_key);
|
281
|
+
::sha256_update(&context, info, info_length);
|
282
|
+
::sha256_update(&context, &iteration, 1);
|
283
|
+
hmac_sha256_final(&context, hmac_key, step_result);
|
284
|
+
while (bytes_remaining > SHA256_OUTPUT_LENGTH) {
|
285
|
+
std::memcpy(output, step_result, SHA256_OUTPUT_LENGTH);
|
286
|
+
output += SHA256_OUTPUT_LENGTH;
|
287
|
+
bytes_remaining -= SHA256_OUTPUT_LENGTH;
|
288
|
+
iteration ++;
|
289
|
+
hmac_sha256_init(&context, hmac_key);
|
290
|
+
::sha256_update(&context, step_result, SHA256_OUTPUT_LENGTH);
|
291
|
+
::sha256_update(&context, info, info_length);
|
292
|
+
::sha256_update(&context, &iteration, 1);
|
293
|
+
hmac_sha256_final(&context, hmac_key, step_result);
|
294
|
+
}
|
295
|
+
std::memcpy(output, step_result, bytes_remaining);
|
296
|
+
olm::unset(context);
|
297
|
+
olm::unset(hmac_key);
|
298
|
+
olm::unset(step_result);
|
299
|
+
}
|