ruby_olm 0.1.1

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