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,184 @@
1
+ /* Copyright 2015 OpenMarket Ltd
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+ #include <cstdint>
17
+
18
+ #include "olm/crypto.h"
19
+ #include "olm/list.hh"
20
+ #include "olm/error.h"
21
+
22
+ struct _olm_cipher;
23
+
24
+ namespace olm {
25
+
26
+ /** length of a shared key: the root key R(i), chain key C(i,j), and message key
27
+ * M(i,j)). They are all only used to stuff into HMACs, so could be any length
28
+ * for that. The chain key and message key are both derived from SHA256
29
+ * operations, so their length is determined by that. */
30
+ const std::size_t OLM_SHARED_KEY_LENGTH = SHA256_OUTPUT_LENGTH;
31
+
32
+ typedef std::uint8_t SharedKey[OLM_SHARED_KEY_LENGTH];
33
+
34
+ struct ChainKey {
35
+ std::uint32_t index;
36
+ SharedKey key;
37
+ };
38
+
39
+ struct MessageKey {
40
+ std::uint32_t index;
41
+ SharedKey key;
42
+ };
43
+
44
+
45
+ struct SenderChain {
46
+ _olm_curve25519_key_pair ratchet_key;
47
+ ChainKey chain_key;
48
+ };
49
+
50
+
51
+ struct ReceiverChain {
52
+ _olm_curve25519_public_key ratchet_key;
53
+ ChainKey chain_key;
54
+ };
55
+
56
+
57
+ struct SkippedMessageKey {
58
+ _olm_curve25519_public_key ratchet_key;
59
+ MessageKey message_key;
60
+ };
61
+
62
+
63
+ static std::size_t const MAX_RECEIVER_CHAINS = 5;
64
+ static std::size_t const MAX_SKIPPED_MESSAGE_KEYS = 40;
65
+
66
+
67
+ struct KdfInfo {
68
+ std::uint8_t const * root_info;
69
+ std::size_t root_info_length;
70
+ std::uint8_t const * ratchet_info;
71
+ std::size_t ratchet_info_length;
72
+ };
73
+
74
+
75
+ struct Ratchet {
76
+
77
+ Ratchet(
78
+ KdfInfo const & kdf_info,
79
+ _olm_cipher const *ratchet_cipher
80
+ );
81
+
82
+ /** A some strings identifying the application to feed into the KDF. */
83
+ KdfInfo const & kdf_info;
84
+
85
+ /** The AEAD cipher to use for encrypting messages. */
86
+ _olm_cipher const *ratchet_cipher;
87
+
88
+ /** The last error that happened encrypting or decrypting a message. */
89
+ OlmErrorCode last_error;
90
+
91
+ /** The root key is used to generate chain keys from the ephemeral keys.
92
+ * A new root_key derived each time a new chain is started. */
93
+ SharedKey root_key;
94
+
95
+ /** The sender chain is used to send messages. Each time a new ephemeral
96
+ * key is received from the remote server we generate a new sender chain
97
+ * with a new empheral key when we next send a message. */
98
+ List<SenderChain, 1> sender_chain;
99
+
100
+ /** The receiver chain is used to decrypt received messages. We store the
101
+ * last few chains so we can decrypt any out of order messages we haven't
102
+ * received yet. */
103
+ List<ReceiverChain, MAX_RECEIVER_CHAINS> receiver_chains;
104
+
105
+ /** List of message keys we've skipped over when advancing the receiver
106
+ * chain. */
107
+ List<SkippedMessageKey, MAX_SKIPPED_MESSAGE_KEYS> skipped_message_keys;
108
+
109
+ /** Initialise the session using a shared secret and the public part of the
110
+ * remote's first ratchet key */
111
+ void initialise_as_bob(
112
+ std::uint8_t const * shared_secret, std::size_t shared_secret_length,
113
+ _olm_curve25519_public_key const & their_ratchet_key
114
+ );
115
+
116
+ /** Initialise the session using a shared secret and the public/private key
117
+ * pair for the first ratchet key */
118
+ void initialise_as_alice(
119
+ std::uint8_t const * shared_secret, std::size_t shared_secret_length,
120
+ _olm_curve25519_key_pair const & our_ratchet_key
121
+ );
122
+
123
+ /** The number of bytes of output the encrypt method will write for
124
+ * a given message length. */
125
+ std::size_t encrypt_output_length(
126
+ std::size_t plaintext_length
127
+ );
128
+
129
+ /** The number of bytes of random data the encrypt method will need to
130
+ * encrypt a message. This will be 32 bytes if the session needs to
131
+ * generate a new ephemeral key, or will be 0 bytes otherwise.*/
132
+ std::size_t encrypt_random_length();
133
+
134
+ /** Encrypt some plain-text. Returns the length of the encrypted message
135
+ * or std::size_t(-1) on failure. On failure last_error will be set with
136
+ * an error code. The last_error will be NOT_ENOUGH_RANDOM if the number
137
+ * of random bytes is too small. The last_error will be
138
+ * OUTPUT_BUFFER_TOO_SMALL if the output buffer is too small. */
139
+ std::size_t encrypt(
140
+ std::uint8_t const * plaintext, std::size_t plaintext_length,
141
+ std::uint8_t const * random, std::size_t random_length,
142
+ std::uint8_t * output, std::size_t max_output_length
143
+ );
144
+
145
+ /** An upper bound on the number of bytes of plain-text the decrypt method
146
+ * will write for a given input message length. */
147
+ std::size_t decrypt_max_plaintext_length(
148
+ std::uint8_t const * input, std::size_t input_length
149
+ );
150
+
151
+ /** Decrypt a message. Returns the length of the decrypted plain-text or
152
+ * std::size_t(-1) on failure. On failure last_error will be set with an
153
+ * error code. The last_error will be OUTPUT_BUFFER_TOO_SMALL if the
154
+ * plain-text buffer is too small. The last_error will be
155
+ * BAD_MESSAGE_VERSION if the message was encrypted with an unsupported
156
+ * version of the protocol. The last_error will be BAD_MESSAGE_FORMAT if
157
+ * the message headers could not be decoded. The last_error will be
158
+ * BAD_MESSAGE_MAC if the message could not be verified */
159
+ std::size_t decrypt(
160
+ std::uint8_t const * input, std::size_t input_length,
161
+ std::uint8_t * plaintext, std::size_t max_plaintext_length
162
+ );
163
+ };
164
+
165
+
166
+ std::size_t pickle_length(
167
+ Ratchet const & value
168
+ );
169
+
170
+
171
+ std::uint8_t * pickle(
172
+ std::uint8_t * pos,
173
+ Ratchet const & value
174
+ );
175
+
176
+
177
+ std::uint8_t const * unpickle(
178
+ std::uint8_t const * pos, std::uint8_t const * end,
179
+ Ratchet & value,
180
+ bool includes_chain_index
181
+ );
182
+
183
+
184
+ } // namespace olm
@@ -0,0 +1,156 @@
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
+ #ifndef OLM_SESSION_HH_
16
+ #define OLM_SESSION_HH_
17
+
18
+ #include "olm/ratchet.hh"
19
+
20
+ namespace olm {
21
+
22
+ struct Account;
23
+
24
+ enum struct MessageType {
25
+ PRE_KEY = 0,
26
+ MESSAGE = 1,
27
+ };
28
+
29
+ struct Session {
30
+
31
+ Session();
32
+
33
+ Ratchet ratchet;
34
+ OlmErrorCode last_error;
35
+
36
+ bool received_message;
37
+
38
+ _olm_curve25519_public_key alice_identity_key;
39
+ _olm_curve25519_public_key alice_base_key;
40
+ _olm_curve25519_public_key bob_one_time_key;
41
+
42
+ /** The number of random bytes that are needed to create a new outbound
43
+ * session. This will be 64 bytes since two ephemeral keys are needed. */
44
+ std::size_t new_outbound_session_random_length();
45
+
46
+ /** Start a new outbound session. Returns std::size_t(-1) on failure. On
47
+ * failure last_error will be set with an error code. The last_error will be
48
+ * NOT_ENOUGH_RANDOM if the number of random bytes was too small. */
49
+ std::size_t new_outbound_session(
50
+ Account const & local_account,
51
+ _olm_curve25519_public_key const & identity_key,
52
+ _olm_curve25519_public_key const & one_time_key,
53
+ std::uint8_t const * random, std::size_t random_length
54
+ );
55
+
56
+ /** Start a new inbound session from a pre-key message.
57
+ * Returns std::size_t(-1) on failure. On failure last_error will be set
58
+ * with an error code. The last_error will be BAD_MESSAGE_FORMAT if
59
+ * the message headers could not be decoded. */
60
+ std::size_t new_inbound_session(
61
+ Account & local_account,
62
+ _olm_curve25519_public_key const * their_identity_key,
63
+ std::uint8_t const * pre_key_message, std::size_t message_length
64
+ );
65
+
66
+ /** The number of bytes written by session_id() */
67
+ std::size_t session_id_length();
68
+
69
+ /** An identifier for this session. Generated by hashing the public keys
70
+ * used to create the session. Returns the length of the session id on
71
+ * success or std::size_t(-1) on failure. On failure last_error will be set
72
+ * with an error code. The last_error will be OUTPUT_BUFFER_TOO_SMALL if
73
+ * the id buffer was too small. */
74
+ std::size_t session_id(
75
+ std::uint8_t * id, std::size_t id_length
76
+ );
77
+
78
+ /** True if this session can be used to decode an inbound pre-key message.
79
+ * This can be used to test whether a pre-key message should be decoded
80
+ * with an existing session or if a new session will need to be created.
81
+ * Returns true if the session is the same. Returns false if either the
82
+ * session does not match or the pre-key message could not be decoded.
83
+ */
84
+ bool matches_inbound_session(
85
+ _olm_curve25519_public_key const * their_identity_key,
86
+ std::uint8_t const * pre_key_message, std::size_t message_length
87
+ );
88
+
89
+ /** Whether the next message will be a pre-key message or a normal message.
90
+ * An outbound session will send pre-key messages until it receives a
91
+ * message with a ratchet key. */
92
+ MessageType encrypt_message_type();
93
+
94
+ std::size_t encrypt_message_length(
95
+ std::size_t plaintext_length
96
+ );
97
+
98
+ /** The number of bytes of random data the encrypt method will need to
99
+ * encrypt a message. This will be 32 bytes if the session needs to
100
+ * generate a new ephemeral key, or will be 0 bytes otherwise. */
101
+ std::size_t encrypt_random_length();
102
+
103
+ /** Encrypt some plain-text. Returns the length of the encrypted message
104
+ * or std::size_t(-1) on failure. On failure last_error will be set with
105
+ * an error code. The last_error will be NOT_ENOUGH_RANDOM if the number
106
+ * of random bytes is too small. The last_error will be
107
+ * OUTPUT_BUFFER_TOO_SMALL if the output buffer is too small. */
108
+ std::size_t encrypt(
109
+ std::uint8_t const * plaintext, std::size_t plaintext_length,
110
+ std::uint8_t const * random, std::size_t random_length,
111
+ std::uint8_t * message, std::size_t message_length
112
+ );
113
+
114
+ /** An upper bound on the number of bytes of plain-text the decrypt method
115
+ * will write for a given input message length. */
116
+ std::size_t decrypt_max_plaintext_length(
117
+ MessageType message_type,
118
+ std::uint8_t const * message, std::size_t message_length
119
+ );
120
+
121
+ /** Decrypt a message. Returns the length of the decrypted plain-text or
122
+ * std::size_t(-1) on failure. On failure last_error will be set with an
123
+ * error code. The last_error will be OUTPUT_BUFFER_TOO_SMALL if the
124
+ * plain-text buffer is too small. The last_error will be
125
+ * BAD_MESSAGE_VERSION if the message was encrypted with an unsupported
126
+ * version of the protocol. The last_error will be BAD_MESSAGE_FORMAT if
127
+ * the message headers could not be decoded. The last_error will be
128
+ * BAD_MESSAGE_MAC if the message could not be verified */
129
+ std::size_t decrypt(
130
+ MessageType message_type,
131
+ std::uint8_t const * message, std::size_t message_length,
132
+ std::uint8_t * plaintext, std::size_t max_plaintext_length
133
+ );
134
+ };
135
+
136
+
137
+ std::size_t pickle_length(
138
+ Session const & value
139
+ );
140
+
141
+
142
+ std::uint8_t * pickle(
143
+ std::uint8_t * pos,
144
+ Session const & value
145
+ );
146
+
147
+
148
+ std::uint8_t const * unpickle(
149
+ std::uint8_t const * pos, std::uint8_t const * end,
150
+ Session & value
151
+ );
152
+
153
+
154
+ } // namespace olm
155
+
156
+ #endif /* OLM_SESSION_HH_ */
@@ -0,0 +1,61 @@
1
+ /* Copyright 2015 OpenMarket Ltd
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+ #ifndef UTILITY_HH_
17
+ #define UTILITY_HH_
18
+
19
+ #include "olm/error.h"
20
+
21
+ #include <cstddef>
22
+ #include <cstdint>
23
+
24
+ struct _olm_ed25519_public_key;
25
+
26
+ namespace olm {
27
+
28
+ struct Utility {
29
+
30
+ Utility();
31
+
32
+ OlmErrorCode last_error;
33
+
34
+ /** The length of a SHA-256 hash in bytes. */
35
+ std::size_t sha256_length();
36
+
37
+ /** Compute a SHA-256 hash. Returns the length of the SHA-256 hash in bytes
38
+ * on success. Returns std::size_t(-1) on failure. On failure last_error
39
+ * will be set with an error code. If the output buffer was too small then
40
+ * last error will be OUTPUT_BUFFER_TOO_SMALL. */
41
+ std::size_t sha256(
42
+ std::uint8_t const * input, std::size_t input_length,
43
+ std::uint8_t * output, std::size_t output_length
44
+ );
45
+
46
+ /** Verify a ed25519 signature. Returns std::size_t(0) on success. Returns
47
+ * std::size_t(-1) on failure or if the signature was invalid. On failure
48
+ * last_error will be set with an error code. If the signature was too short
49
+ * or was not a valid signature then last_error will be BAD_MESSAGE_MAC. */
50
+ std::size_t ed25519_verify(
51
+ _olm_ed25519_public_key const & key,
52
+ std::uint8_t const * message, std::size_t message_length,
53
+ std::uint8_t const * signature, std::size_t signature_length
54
+ );
55
+
56
+ };
57
+
58
+
59
+ } // namespace olm
60
+
61
+ #endif /* UTILITY_HH_ */