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,134 @@
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
+ #include "olm/megolm.h"
16
+ #include "olm/memory.hh"
17
+
18
+ #include "unittest.hh"
19
+
20
+
21
+ int main() {
22
+
23
+ std::uint8_t random_bytes[] =
24
+ "0123456789ABCDEF0123456789ABCDEF"
25
+ "0123456789ABCDEF0123456789ABCDEF"
26
+ "0123456789ABCDEF0123456789ABCDEF"
27
+ "0123456789ABCDEF0123456789ABCDEF";
28
+
29
+ {
30
+ TestCase test_case("Megolm::advance");
31
+
32
+ Megolm mr;
33
+
34
+ megolm_init(&mr, random_bytes, 0);
35
+ // single-step advance
36
+ megolm_advance(&mr);
37
+ const std::uint8_t expected1[] = {
38
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
39
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
40
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
41
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
42
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
43
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
44
+ 0xba, 0x9c, 0xd9, 0x55, 0x74, 0x1d, 0x1c, 0x16, 0x23, 0x23, 0xec, 0x82, 0x5e, 0x7c, 0x5c, 0xe8,
45
+ 0x89, 0xbb, 0xb4, 0x23, 0xa1, 0x8f, 0x23, 0x82, 0x8f, 0xb2, 0x09, 0x0d, 0x6e, 0x2a, 0xf8, 0x6a
46
+ };
47
+ assert_equals(1U, mr.counter);
48
+ assert_equals(expected1, megolm_get_data(&mr), MEGOLM_RATCHET_LENGTH);
49
+
50
+ // repeat with complex advance
51
+ megolm_init(&mr, random_bytes, 0);
52
+ megolm_advance_to(&mr, 1);
53
+ assert_equals(1U, mr.counter);
54
+ assert_equals(expected1, megolm_get_data(&mr), MEGOLM_RATCHET_LENGTH);
55
+
56
+ megolm_advance_to(&mr, 0x1000000);
57
+ const std::uint8_t expected2[] = {
58
+ 0x54, 0x02, 0x2d, 0x7d, 0xc0, 0x29, 0x8e, 0x16, 0x37, 0xe2, 0x1c, 0x97, 0x15, 0x30, 0x92, 0xf9,
59
+ 0x33, 0xc0, 0x56, 0xff, 0x74, 0xfe, 0x1b, 0x92, 0x2d, 0x97, 0x1f, 0x24, 0x82, 0xc2, 0x85, 0x9c,
60
+ 0x70, 0x04, 0xc0, 0x1e, 0xe4, 0x9b, 0xd6, 0xef, 0xe0, 0x07, 0x35, 0x25, 0xaf, 0x9b, 0x16, 0x32,
61
+ 0xc5, 0xbe, 0x72, 0x6d, 0x12, 0x34, 0x9c, 0xc5, 0xbd, 0x47, 0x2b, 0xdc, 0x2d, 0xf6, 0x54, 0x0f,
62
+ 0x31, 0x12, 0x59, 0x11, 0x94, 0xfd, 0xa6, 0x17, 0xe5, 0x68, 0xc6, 0x83, 0x10, 0x1e, 0xae, 0xcd,
63
+ 0x7e, 0xdd, 0xd6, 0xde, 0x1f, 0xbc, 0x07, 0x67, 0xae, 0x34, 0xda, 0x1a, 0x09, 0xa5, 0x4e, 0xab,
64
+ 0xba, 0x9c, 0xd9, 0x55, 0x74, 0x1d, 0x1c, 0x16, 0x23, 0x23, 0xec, 0x82, 0x5e, 0x7c, 0x5c, 0xe8,
65
+ 0x89, 0xbb, 0xb4, 0x23, 0xa1, 0x8f, 0x23, 0x82, 0x8f, 0xb2, 0x09, 0x0d, 0x6e, 0x2a, 0xf8, 0x6a,
66
+ };
67
+ assert_equals(0x1000000U, mr.counter);
68
+ assert_equals(expected2, megolm_get_data(&mr), MEGOLM_RATCHET_LENGTH);
69
+
70
+ megolm_advance_to(&mr, 0x1041506);
71
+ const std::uint8_t expected3[] = {
72
+ 0x54, 0x02, 0x2d, 0x7d, 0xc0, 0x29, 0x8e, 0x16, 0x37, 0xe2, 0x1c, 0x97, 0x15, 0x30, 0x92, 0xf9,
73
+ 0x33, 0xc0, 0x56, 0xff, 0x74, 0xfe, 0x1b, 0x92, 0x2d, 0x97, 0x1f, 0x24, 0x82, 0xc2, 0x85, 0x9c,
74
+ 0x55, 0x58, 0x8d, 0xf5, 0xb7, 0xa4, 0x88, 0x78, 0x42, 0x89, 0x27, 0x86, 0x81, 0x64, 0x58, 0x9f,
75
+ 0x36, 0x63, 0x44, 0x7b, 0x51, 0xed, 0xc3, 0x59, 0x5b, 0x03, 0x6c, 0xa6, 0x04, 0xc4, 0x6d, 0xcd,
76
+ 0x5c, 0x54, 0x85, 0x0b, 0xfa, 0x98, 0xa1, 0xfd, 0x79, 0xa9, 0xdf, 0x1c, 0xbe, 0x8f, 0xc5, 0x68,
77
+ 0x19, 0x37, 0xd3, 0x0c, 0x85, 0xc8, 0xc3, 0x1f, 0x7b, 0xb8, 0x28, 0x81, 0x6c, 0xf9, 0xff, 0x3b,
78
+ 0x95, 0x6c, 0xbf, 0x80, 0x7e, 0x65, 0x12, 0x6a, 0x49, 0x55, 0x8d, 0x45, 0xc8, 0x4a, 0x2e, 0x4c,
79
+ 0xd5, 0x6f, 0x03, 0xe2, 0x44, 0x16, 0xb9, 0x8e, 0x1c, 0xfd, 0x97, 0xc2, 0x06, 0xaa, 0x90, 0x7a
80
+ };
81
+ assert_equals(0x1041506U, mr.counter);
82
+ assert_equals(expected3, megolm_get_data(&mr), MEGOLM_RATCHET_LENGTH);
83
+ }
84
+
85
+ {
86
+ TestCase test_case("Megolm::advance wraparound");
87
+
88
+ Megolm mr1, mr2;
89
+
90
+ megolm_init(&mr1, random_bytes, 0xffffffffUL);
91
+ megolm_advance_to(&mr1, 0x1000000);
92
+ assert_equals(0x1000000U, mr1.counter);
93
+
94
+ megolm_init(&mr2, random_bytes, 0);
95
+ megolm_advance_to(&mr2, 0x2000000);
96
+ assert_equals(0x2000000U, mr2.counter);
97
+
98
+ assert_equals(megolm_get_data(&mr2), megolm_get_data(&mr1), MEGOLM_RATCHET_LENGTH);
99
+ }
100
+
101
+ {
102
+ TestCase test_case("Megolm::advance overflow by one");
103
+
104
+ Megolm mr1, mr2;
105
+
106
+ megolm_init(&mr1, random_bytes, 0xffffffffUL);
107
+ megolm_advance_to(&mr1, 0x0);
108
+ assert_equals(0x0U, mr1.counter);
109
+
110
+ megolm_init(&mr2, random_bytes, 0xffffffffUL);
111
+ megolm_advance(&mr2);
112
+ assert_equals(0x0U, mr2.counter);
113
+
114
+ assert_equals(megolm_get_data(&mr2), megolm_get_data(&mr1), MEGOLM_RATCHET_LENGTH);
115
+ }
116
+
117
+ {
118
+ TestCase test_case("Megolm::advance overflow");
119
+
120
+ Megolm mr1, mr2;
121
+
122
+ megolm_init(&mr1, random_bytes, 0x1UL);
123
+ megolm_advance_to(&mr1, 0x80000000UL);
124
+ megolm_advance_to(&mr1, 0x0);
125
+ assert_equals(0x0U, mr1.counter);
126
+
127
+ megolm_init(&mr2, random_bytes, 0x1UL);
128
+ megolm_advance_to(&mr2, 0x0UL);
129
+ assert_equals(0x0U, mr2.counter);
130
+
131
+ assert_equals(megolm_get_data(&mr2), megolm_get_data(&mr1), MEGOLM_RATCHET_LENGTH);
132
+ }
133
+
134
+ }
@@ -0,0 +1,112 @@
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
+ #include "unittest.hh"
17
+
18
+ int main() {
19
+
20
+ std::uint8_t message1[36] = "\x03\x10\x01\n\nratchetkey\"\nciphertexthmacsha2";
21
+ std::uint8_t message2[36] = "\x03\n\nratchetkey\x10\x01\"\nciphertexthmacsha2";
22
+ std::uint8_t ratchetkey[11] = "ratchetkey";
23
+ std::uint8_t ciphertext[11] = "ciphertext";
24
+ std::uint8_t hmacsha2[9] = "hmacsha2";
25
+
26
+ { /* Message decode test */
27
+
28
+ TestCase test_case("Message decode test");
29
+
30
+ olm::MessageReader reader;
31
+ olm::decode_message(reader, message1, 35, 8);
32
+
33
+ assert_equals(std::uint8_t(3), reader.version);
34
+ assert_equals(true, reader.has_counter);
35
+ assert_equals(std::uint32_t(1), reader.counter);
36
+ assert_equals(std::size_t(10), reader.ratchet_key_length);
37
+ assert_equals(std::size_t(10), reader.ciphertext_length);
38
+
39
+ assert_equals(ratchetkey, reader.ratchet_key, 10);
40
+ assert_equals(ciphertext, reader.ciphertext, 10);
41
+
42
+
43
+ } /* Message decode test */
44
+
45
+ { /* Message encode test */
46
+
47
+ TestCase test_case("Message encode test");
48
+
49
+ std::size_t length = olm::encode_message_length(1, 10, 10, 8);
50
+ assert_equals(std::size_t(35), length);
51
+
52
+ std::uint8_t output[length];
53
+
54
+ olm::MessageWriter writer;
55
+ olm::encode_message(writer, 3, 1, 10, 10, output);
56
+
57
+ std::memcpy(writer.ratchet_key, ratchetkey, 10);
58
+ std::memcpy(writer.ciphertext, ciphertext, 10);
59
+ std::memcpy(output + length - 8, hmacsha2, 8);
60
+
61
+ assert_equals(message2, output, 35);
62
+
63
+ } /* Message encode test */
64
+
65
+
66
+ { /* group message encode test */
67
+
68
+ TestCase test_case("Group message encode test");
69
+
70
+ size_t length = _olm_encode_group_message_length(200, 10, 8, 64);
71
+ size_t expected_length = 1 + (1+2) + (2+10) + 8 + 64;
72
+ assert_equals(expected_length, length);
73
+
74
+ uint8_t output[50];
75
+ uint8_t *ciphertext_ptr;
76
+
77
+ _olm_encode_group_message(
78
+ 3,
79
+ 200, // counter
80
+ 10, // ciphertext length
81
+ output,
82
+ &ciphertext_ptr
83
+ );
84
+
85
+ uint8_t expected[] =
86
+ "\x03"
87
+ "\x08\xC8\x01"
88
+ "\x12\x0A";
89
+
90
+ assert_equals(expected, output, sizeof(expected)-1);
91
+ assert_equals(output+sizeof(expected)-1, ciphertext_ptr);
92
+ } /* group message encode test */
93
+
94
+ {
95
+ TestCase test_case("Group message decode test");
96
+
97
+ struct _OlmDecodeGroupMessageResults results;
98
+ std::uint8_t message[] =
99
+ "\x03"
100
+ "\x08\xC8\x01"
101
+ "\x12\x0A" "ciphertext"
102
+ "hmacsha2"
103
+ "ed25519signature";
104
+
105
+ _olm_decode_group_message(message, sizeof(message)-1, 8, 16, &results);
106
+ assert_equals(std::uint8_t(3), results.version);
107
+ assert_equals(1, results.has_message_index);
108
+ assert_equals(std::uint32_t(200), results.message_index);
109
+ assert_equals(std::size_t(10), results.ciphertext_length);
110
+ assert_equals(ciphertext, results.ciphertext, 10);
111
+ } /* group message decode test */
112
+ }
@@ -0,0 +1,405 @@
1
+ #include "olm/olm.h"
2
+ #include "unittest.hh"
3
+
4
+ #include <cstddef>
5
+ #include <cstdint>
6
+ #include <cstring>
7
+
8
+ struct MockRandom {
9
+ MockRandom(std::uint8_t tag, std::uint8_t offset = 0)
10
+ : tag(tag), current(offset) {}
11
+ void operator()(
12
+ std::uint8_t * bytes, std::size_t length
13
+ ) {
14
+ while (length > 32) {
15
+ bytes[0] = tag;
16
+ std::memset(bytes + 1, current, 31);
17
+ length -= 32;
18
+ bytes += 32;
19
+ current += 1;
20
+ }
21
+ if (length) {
22
+ bytes[0] = tag;
23
+ std::memset(bytes + 1, current, length - 1);
24
+ current += 1;
25
+ }
26
+ }
27
+ std::uint8_t tag;
28
+ std::uint8_t current;
29
+ };
30
+
31
+ int main() {
32
+
33
+ { /** Pickle account test */
34
+
35
+ TestCase test_case("Pickle account test");
36
+ MockRandom mock_random('P');
37
+
38
+
39
+ std::uint8_t account_buffer[::olm_account_size()];
40
+ ::OlmAccount *account = ::olm_account(account_buffer);
41
+ std::uint8_t random[::olm_create_account_random_length(account)];
42
+ mock_random(random, sizeof(random));
43
+ ::olm_create_account(account, random, sizeof(random));
44
+ std::uint8_t ot_random[::olm_account_generate_one_time_keys_random_length(
45
+ account, 42
46
+ )];
47
+ mock_random(ot_random, sizeof(ot_random));
48
+ ::olm_account_generate_one_time_keys(account, 42, ot_random, sizeof(ot_random));
49
+
50
+ std::size_t pickle_length = ::olm_pickle_account_length(account);
51
+ std::uint8_t pickle1[pickle_length];
52
+ std::size_t res = ::olm_pickle_account(account, "secret_key", 10, pickle1, pickle_length);
53
+ assert_equals(pickle_length, res);
54
+
55
+ std::uint8_t pickle2[pickle_length];
56
+ std::memcpy(pickle2, pickle1, pickle_length);
57
+
58
+ std::uint8_t account_buffer2[::olm_account_size()];
59
+ ::OlmAccount *account2 = ::olm_account(account_buffer2);
60
+ assert_not_equals(std::size_t(-1), ::olm_unpickle_account(
61
+ account2, "secret_key", 10, pickle2, pickle_length
62
+ ));
63
+ assert_equals(pickle_length, ::olm_pickle_account_length(account2));
64
+ res = ::olm_pickle_account(account2, "secret_key", 10, pickle2, pickle_length);
65
+ assert_equals(pickle_length, res);
66
+
67
+ assert_equals(pickle1, pickle2, pickle_length);
68
+ }
69
+
70
+
71
+ {
72
+ TestCase test_case("Old account unpickle test");
73
+
74
+ // this uses the old pickle format, which did not use enough space
75
+ // for the Ed25519 key. We should reject it.
76
+ std::uint8_t pickle[] =
77
+ "x3h9er86ygvq56pM1yesdAxZou4ResPQC9Rszk/fhEL9JY/umtZ2N/foL/SUgVXS"
78
+ "v0IxHHZTafYjDdzJU9xr8dQeBoOTGfV9E/lCqDGBnIlu7SZndqjEKXtzGyQr4sP4"
79
+ "K/A/8TOu9iK2hDFszy6xETiousHnHgh2ZGbRUh4pQx+YMm8ZdNZeRnwFGLnrWyf9"
80
+ "O5TmXua1FcU";
81
+
82
+ std::uint8_t account_buffer[::olm_account_size()];
83
+ ::OlmAccount *account = ::olm_account(account_buffer);
84
+ assert_equals(
85
+ std::size_t(-1),
86
+ ::olm_unpickle_account(
87
+ account, "", 0, pickle, sizeof(pickle)-1
88
+ )
89
+ );
90
+ assert_equals(
91
+ std::string("BAD_LEGACY_ACCOUNT_PICKLE"),
92
+ std::string(::olm_account_last_error(account))
93
+ );
94
+ }
95
+
96
+
97
+ { /** Pickle session test */
98
+
99
+ TestCase test_case("Pickle session test");
100
+ MockRandom mock_random('P');
101
+
102
+ std::uint8_t account_buffer[::olm_account_size()];
103
+ ::OlmAccount *account = ::olm_account(account_buffer);
104
+ std::uint8_t random[::olm_create_account_random_length(account)];
105
+ mock_random(random, sizeof(random));
106
+ ::olm_create_account(account, random, sizeof(random));
107
+
108
+ std::uint8_t session_buffer[::olm_session_size()];
109
+ ::OlmSession *session = ::olm_session(session_buffer);
110
+ std::uint8_t identity_key[32];
111
+ std::uint8_t one_time_key[32];
112
+ mock_random(identity_key, sizeof(identity_key));
113
+ mock_random(one_time_key, sizeof(one_time_key));
114
+ std::uint8_t random2[::olm_create_outbound_session_random_length(session)];
115
+ mock_random(random2, sizeof(random2));
116
+
117
+ ::olm_create_outbound_session(
118
+ session, account,
119
+ identity_key, sizeof(identity_key),
120
+ one_time_key, sizeof(one_time_key),
121
+ random2, sizeof(random2)
122
+ );
123
+
124
+
125
+ std::size_t pickle_length = ::olm_pickle_session_length(session);
126
+ std::uint8_t pickle1[pickle_length];
127
+ std::size_t res = ::olm_pickle_session(session, "secret_key", 10, pickle1, pickle_length);
128
+ assert_equals(pickle_length, res);
129
+
130
+ std::uint8_t pickle2[pickle_length];
131
+ std::memcpy(pickle2, pickle1, pickle_length);
132
+
133
+ std::uint8_t session_buffer2[::olm_session_size()];
134
+ ::OlmSession *session2 = ::olm_session(session_buffer2);
135
+ assert_not_equals(std::size_t(-1), ::olm_unpickle_session(
136
+ session2, "secret_key", 10, pickle2, pickle_length
137
+ ));
138
+ assert_equals(pickle_length, ::olm_pickle_session_length(session2));
139
+ res = ::olm_pickle_session(session2, "secret_key", 10, pickle2, pickle_length);
140
+ assert_equals(pickle_length, res);
141
+
142
+ assert_equals(pickle1, pickle2, pickle_length);
143
+ }
144
+
145
+ { /** Loopback test */
146
+
147
+ TestCase test_case("Loopback test");
148
+ MockRandom mock_random_a('A', 0x00);
149
+ MockRandom mock_random_b('B', 0x80);
150
+
151
+ std::uint8_t a_account_buffer[::olm_account_size()];
152
+ ::OlmAccount *a_account = ::olm_account(a_account_buffer);
153
+ std::uint8_t a_random[::olm_create_account_random_length(a_account)];
154
+ mock_random_a(a_random, sizeof(a_random));
155
+ ::olm_create_account(a_account, a_random, sizeof(a_random));
156
+
157
+ std::uint8_t b_account_buffer[::olm_account_size()];
158
+ ::OlmAccount *b_account = ::olm_account(b_account_buffer);
159
+ std::uint8_t b_random[::olm_create_account_random_length(b_account)];
160
+ mock_random_b(b_random, sizeof(b_random));
161
+ ::olm_create_account(b_account, b_random, sizeof(b_random));
162
+ std::uint8_t o_random[::olm_account_generate_one_time_keys_random_length(
163
+ b_account, 42
164
+ )];
165
+ mock_random_b(o_random, sizeof(o_random));
166
+ ::olm_account_generate_one_time_keys(b_account, 42, o_random, sizeof(o_random));
167
+
168
+ std::uint8_t a_id_keys[::olm_account_identity_keys_length(a_account)];
169
+ ::olm_account_identity_keys(a_account, a_id_keys, sizeof(a_id_keys));
170
+
171
+ std::uint8_t b_id_keys[::olm_account_identity_keys_length(b_account)];
172
+ std::uint8_t b_ot_keys[::olm_account_one_time_keys_length(b_account)];
173
+ ::olm_account_identity_keys(b_account, b_id_keys, sizeof(b_id_keys));
174
+ ::olm_account_one_time_keys(b_account, b_ot_keys, sizeof(b_ot_keys));
175
+
176
+ std::uint8_t a_session_buffer[::olm_session_size()];
177
+ ::OlmSession *a_session = ::olm_session(a_session_buffer);
178
+ std::uint8_t a_rand[::olm_create_outbound_session_random_length(a_session)];
179
+ mock_random_a(a_rand, sizeof(a_rand));
180
+ assert_not_equals(std::size_t(-1), ::olm_create_outbound_session(
181
+ a_session, a_account,
182
+ b_id_keys + 15, 43, // B's curve25519 identity key
183
+ b_ot_keys + 25, 43, // B's curve25519 one time key
184
+ a_rand, sizeof(a_rand)
185
+ ));
186
+
187
+ std::uint8_t plaintext[] = "Hello, World";
188
+ std::uint8_t message_1[::olm_encrypt_message_length(a_session, 12)];
189
+ std::uint8_t a_message_random[::olm_encrypt_random_length(a_session)];
190
+ mock_random_a(a_message_random, sizeof(a_message_random));
191
+ assert_equals(std::size_t(0), ::olm_encrypt_message_type(a_session));
192
+ assert_not_equals(std::size_t(-1), ::olm_encrypt(
193
+ a_session,
194
+ plaintext, 12,
195
+ a_message_random, sizeof(a_message_random),
196
+ message_1, sizeof(message_1)
197
+ ));
198
+
199
+
200
+ std::uint8_t tmp_message_1[sizeof(message_1)];
201
+ std::memcpy(tmp_message_1, message_1, sizeof(message_1));
202
+ std::uint8_t b_session_buffer[::olm_account_size()];
203
+ ::OlmSession *b_session = ::olm_session(b_session_buffer);
204
+ ::olm_create_inbound_session(
205
+ b_session, b_account, tmp_message_1, sizeof(message_1)
206
+ );
207
+
208
+ // Check that the inbound session matches the message it was created from.
209
+ std::memcpy(tmp_message_1, message_1, sizeof(message_1));
210
+ assert_equals(std::size_t(1), ::olm_matches_inbound_session(
211
+ b_session,
212
+ tmp_message_1, sizeof(message_1)
213
+ ));
214
+
215
+ // Check that the inbound session matches the key this message is supposed
216
+ // to be from.
217
+ std::memcpy(tmp_message_1, message_1, sizeof(message_1));
218
+ assert_equals(std::size_t(1), ::olm_matches_inbound_session_from(
219
+ b_session,
220
+ a_id_keys + 15, 43, // A's curve125519 identity key.
221
+ tmp_message_1, sizeof(message_1)
222
+ ));
223
+
224
+ // Check that the inbound session isn't from a different user.
225
+ std::memcpy(tmp_message_1, message_1, sizeof(message_1));
226
+ assert_equals(std::size_t(0), ::olm_matches_inbound_session_from(
227
+ b_session,
228
+ b_id_keys + 15, 43, // B's curve25519 identity key.
229
+ tmp_message_1, sizeof(message_1)
230
+ ));
231
+
232
+ // Check that we can decrypt the message.
233
+ std::memcpy(tmp_message_1, message_1, sizeof(message_1));
234
+ std::uint8_t plaintext_1[::olm_decrypt_max_plaintext_length(
235
+ b_session, 0, tmp_message_1, sizeof(message_1)
236
+ )];
237
+ std::memcpy(tmp_message_1, message_1, sizeof(message_1));
238
+ assert_equals(std::size_t(12), ::olm_decrypt(
239
+ b_session, 0,
240
+ tmp_message_1, sizeof(message_1),
241
+ plaintext_1, sizeof(plaintext_1)
242
+ ));
243
+
244
+ assert_equals(plaintext, plaintext_1, 12);
245
+
246
+ std::uint8_t message_2[::olm_encrypt_message_length(b_session, 12)];
247
+ std::uint8_t b_message_random[::olm_encrypt_random_length(b_session)];
248
+ mock_random_b(b_message_random, sizeof(b_message_random));
249
+ assert_equals(std::size_t(1), ::olm_encrypt_message_type(b_session));
250
+ assert_not_equals(std::size_t(-1), ::olm_encrypt(
251
+ b_session,
252
+ plaintext, 12,
253
+ b_message_random, sizeof(b_message_random),
254
+ message_2, sizeof(message_2)
255
+ ));
256
+
257
+ std::uint8_t tmp_message_2[sizeof(message_2)];
258
+ std::memcpy(tmp_message_2, message_2, sizeof(message_2));
259
+ std::uint8_t plaintext_2[::olm_decrypt_max_plaintext_length(
260
+ a_session, 1, tmp_message_2, sizeof(message_2)
261
+ )];
262
+ std::memcpy(tmp_message_2, message_2, sizeof(message_2));
263
+ assert_equals(std::size_t(12), ::olm_decrypt(
264
+ a_session, 1,
265
+ tmp_message_2, sizeof(message_2),
266
+ plaintext_2, sizeof(plaintext_2)
267
+ ));
268
+
269
+ assert_equals(plaintext, plaintext_2, 12);
270
+
271
+ std::memcpy(tmp_message_2, message_2, sizeof(message_2));
272
+ assert_equals(std::size_t(-1), ::olm_decrypt(
273
+ a_session, 1,
274
+ tmp_message_2, sizeof(message_2),
275
+ plaintext_2, sizeof(plaintext_2)
276
+ ));
277
+
278
+ std::uint8_t a_session_id[::olm_session_id_length(a_session)];
279
+ assert_not_equals(std::size_t(-1), ::olm_session_id(
280
+ a_session, a_session_id, sizeof(a_session_id)
281
+ ));
282
+
283
+ std::uint8_t b_session_id[::olm_session_id_length(b_session)];
284
+ assert_not_equals(std::size_t(-1), ::olm_session_id(
285
+ b_session, b_session_id, sizeof(b_session_id)
286
+ ));
287
+
288
+ assert_equals(sizeof(a_session_id), sizeof(b_session_id));
289
+ assert_equals(a_session_id, b_session_id, sizeof(b_session_id));
290
+
291
+ }
292
+
293
+ { /** More messages test */
294
+
295
+ TestCase test_case("More messages test");
296
+ MockRandom mock_random_a('A', 0x00);
297
+ MockRandom mock_random_b('B', 0x80);
298
+
299
+ std::uint8_t a_account_buffer[::olm_account_size()];
300
+ ::OlmAccount *a_account = ::olm_account(a_account_buffer);
301
+ std::uint8_t a_random[::olm_create_account_random_length(a_account)];
302
+ mock_random_a(a_random, sizeof(a_random));
303
+ ::olm_create_account(a_account, a_random, sizeof(a_random));
304
+
305
+ std::uint8_t b_account_buffer[::olm_account_size()];
306
+ ::OlmAccount *b_account = ::olm_account(b_account_buffer);
307
+ std::uint8_t b_random[::olm_create_account_random_length(b_account)];
308
+ mock_random_b(b_random, sizeof(b_random));
309
+ ::olm_create_account(b_account, b_random, sizeof(b_random));
310
+ std::uint8_t o_random[::olm_account_generate_one_time_keys_random_length(
311
+ b_account, 42
312
+ )];
313
+ mock_random_b(o_random, sizeof(o_random));
314
+ ::olm_account_generate_one_time_keys(b_account, 42, o_random, sizeof(o_random));
315
+
316
+ std::uint8_t b_id_keys[::olm_account_identity_keys_length(b_account)];
317
+ std::uint8_t b_ot_keys[::olm_account_one_time_keys_length(b_account)];
318
+ ::olm_account_identity_keys(b_account, b_id_keys, sizeof(b_id_keys));
319
+ ::olm_account_one_time_keys(b_account, b_ot_keys, sizeof(b_ot_keys));
320
+
321
+ std::uint8_t a_session_buffer[::olm_session_size()];
322
+ ::OlmSession *a_session = ::olm_session(a_session_buffer);
323
+ std::uint8_t a_rand[::olm_create_outbound_session_random_length(a_session)];
324
+ mock_random_a(a_rand, sizeof(a_rand));
325
+ assert_not_equals(std::size_t(-1), ::olm_create_outbound_session(
326
+ a_session, a_account,
327
+ b_id_keys + 15, 43,
328
+ b_ot_keys + 25, 43,
329
+ a_rand, sizeof(a_rand)
330
+ ));
331
+
332
+ std::uint8_t plaintext[] = "Hello, World";
333
+ std::uint8_t message_1[::olm_encrypt_message_length(a_session, 12)];
334
+ std::uint8_t a_message_random[::olm_encrypt_random_length(a_session)];
335
+ mock_random_a(a_message_random, sizeof(a_message_random));
336
+ assert_equals(std::size_t(0), ::olm_encrypt_message_type(a_session));
337
+ assert_not_equals(std::size_t(-1), ::olm_encrypt(
338
+ a_session,
339
+ plaintext, 12,
340
+ a_message_random, sizeof(a_message_random),
341
+ message_1, sizeof(message_1)
342
+ ));
343
+
344
+ std::uint8_t tmp_message_1[sizeof(message_1)];
345
+ std::memcpy(tmp_message_1, message_1, sizeof(message_1));
346
+ std::uint8_t b_session_buffer[::olm_account_size()];
347
+ ::OlmSession *b_session = ::olm_session(b_session_buffer);
348
+ ::olm_create_inbound_session(
349
+ b_session, b_account, tmp_message_1, sizeof(message_1)
350
+ );
351
+
352
+ std::memcpy(tmp_message_1, message_1, sizeof(message_1));
353
+ std::uint8_t plaintext_1[::olm_decrypt_max_plaintext_length(
354
+ b_session, 0, tmp_message_1, sizeof(message_1)
355
+ )];
356
+ std::memcpy(tmp_message_1, message_1, sizeof(message_1));
357
+ assert_equals(std::size_t(12), ::olm_decrypt(
358
+ b_session, 0,
359
+ tmp_message_1, sizeof(message_1),
360
+ plaintext_1, sizeof(plaintext_1)
361
+ ));
362
+
363
+ for (unsigned i = 0; i < 8; ++i) {
364
+ {
365
+ std::uint8_t msg_a[::olm_encrypt_message_length(a_session, 12)];
366
+ std::uint8_t rnd_a[::olm_encrypt_random_length(a_session)];
367
+ mock_random_a(rnd_a, sizeof(rnd_a));
368
+ std::size_t type_a = ::olm_encrypt_message_type(a_session);
369
+ assert_not_equals(std::size_t(-1), ::olm_encrypt(
370
+ a_session, plaintext, 12, rnd_a, sizeof(rnd_a), msg_a, sizeof(msg_a)
371
+ ));
372
+
373
+ std::uint8_t tmp_a[sizeof(msg_a)];
374
+ std::memcpy(tmp_a, msg_a, sizeof(msg_a));
375
+ std::uint8_t out_a[::olm_decrypt_max_plaintext_length(
376
+ b_session, type_a, tmp_a, sizeof(tmp_a)
377
+ )];
378
+ std::memcpy(tmp_a, msg_a, sizeof(msg_a));
379
+ assert_equals(std::size_t(12), ::olm_decrypt(
380
+ b_session, type_a, msg_a, sizeof(msg_a), out_a, sizeof(out_a)
381
+ ));
382
+ }
383
+ {
384
+ std::uint8_t msg_b[::olm_encrypt_message_length(b_session, 12)];
385
+ std::uint8_t rnd_b[::olm_encrypt_random_length(b_session)];
386
+ mock_random_b(rnd_b, sizeof(rnd_b));
387
+ std::size_t type_b = ::olm_encrypt_message_type(b_session);
388
+ assert_not_equals(std::size_t(-1), ::olm_encrypt(
389
+ b_session, plaintext, 12, rnd_b, sizeof(rnd_b), msg_b, sizeof(msg_b)
390
+ ));
391
+
392
+ std::uint8_t tmp_b[sizeof(msg_b)];
393
+ std::memcpy(tmp_b, msg_b, sizeof(msg_b));
394
+ std::uint8_t out_b[::olm_decrypt_max_plaintext_length(
395
+ a_session, type_b, tmp_b, sizeof(tmp_b)
396
+ )];
397
+ std::memcpy(tmp_b, msg_b, sizeof(msg_b));
398
+ assert_equals(std::size_t(12), ::olm_decrypt(
399
+ a_session, type_b, msg_b, sizeof(msg_b), out_b, sizeof(out_b)
400
+ ));
401
+ }
402
+ }
403
+ }
404
+
405
+ }