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,363 @@
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
+ #include "olm/outbound_group_session.h"
17
+
18
+ #include <string.h>
19
+
20
+ #include "olm/base64.h"
21
+ #include "olm/cipher.h"
22
+ #include "olm/crypto.h"
23
+ #include "olm/error.h"
24
+ #include "olm/megolm.h"
25
+ #include "olm/memory.h"
26
+ #include "olm/message.h"
27
+ #include "olm/pickle.h"
28
+ #include "olm/pickle_encoding.h"
29
+
30
+ #define OLM_PROTOCOL_VERSION 3
31
+ #define GROUP_SESSION_ID_LENGTH ED25519_PUBLIC_KEY_LENGTH
32
+ #define PICKLE_VERSION 1
33
+ #define SESSION_KEY_VERSION 2
34
+
35
+ struct OlmOutboundGroupSession {
36
+ /** the Megolm ratchet providing the encryption keys */
37
+ Megolm ratchet;
38
+
39
+ /** The ed25519 keypair used for signing the messages */
40
+ struct _olm_ed25519_key_pair signing_key;
41
+
42
+ enum OlmErrorCode last_error;
43
+ };
44
+
45
+
46
+ size_t olm_outbound_group_session_size(void) {
47
+ return sizeof(OlmOutboundGroupSession);
48
+ }
49
+
50
+ OlmOutboundGroupSession * olm_outbound_group_session(
51
+ void *memory
52
+ ) {
53
+ OlmOutboundGroupSession *session = memory;
54
+ olm_clear_outbound_group_session(session);
55
+ return session;
56
+ }
57
+
58
+ const char *olm_outbound_group_session_last_error(
59
+ const OlmOutboundGroupSession *session
60
+ ) {
61
+ return _olm_error_to_string(session->last_error);
62
+ }
63
+
64
+ size_t olm_clear_outbound_group_session(
65
+ OlmOutboundGroupSession *session
66
+ ) {
67
+ _olm_unset(session, sizeof(OlmOutboundGroupSession));
68
+ return sizeof(OlmOutboundGroupSession);
69
+ }
70
+
71
+ static size_t raw_pickle_length(
72
+ const OlmOutboundGroupSession *session
73
+ ) {
74
+ size_t length = 0;
75
+ length += _olm_pickle_uint32_length(PICKLE_VERSION);
76
+ length += megolm_pickle_length(&(session->ratchet));
77
+ length += _olm_pickle_ed25519_key_pair_length(&(session->signing_key));
78
+ return length;
79
+ }
80
+
81
+ size_t olm_pickle_outbound_group_session_length(
82
+ const OlmOutboundGroupSession *session
83
+ ) {
84
+ return _olm_enc_output_length(raw_pickle_length(session));
85
+ }
86
+
87
+ size_t olm_pickle_outbound_group_session(
88
+ OlmOutboundGroupSession *session,
89
+ void const * key, size_t key_length,
90
+ void * pickled, size_t pickled_length
91
+ ) {
92
+ size_t raw_length = raw_pickle_length(session);
93
+ uint8_t *pos;
94
+
95
+ if (pickled_length < _olm_enc_output_length(raw_length)) {
96
+ session->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL;
97
+ return (size_t)-1;
98
+ }
99
+
100
+ pos = _olm_enc_output_pos(pickled, raw_length);
101
+ pos = _olm_pickle_uint32(pos, PICKLE_VERSION);
102
+ pos = megolm_pickle(&(session->ratchet), pos);
103
+ pos = _olm_pickle_ed25519_key_pair(pos, &(session->signing_key));
104
+
105
+ return _olm_enc_output(key, key_length, pickled, raw_length);
106
+ }
107
+
108
+ size_t olm_unpickle_outbound_group_session(
109
+ OlmOutboundGroupSession *session,
110
+ void const * key, size_t key_length,
111
+ void * pickled, size_t pickled_length
112
+ ) {
113
+ const uint8_t *pos;
114
+ const uint8_t *end;
115
+ uint32_t pickle_version;
116
+
117
+ size_t raw_length = _olm_enc_input(
118
+ key, key_length, pickled, pickled_length, &(session->last_error)
119
+ );
120
+ if (raw_length == (size_t)-1) {
121
+ return raw_length;
122
+ }
123
+
124
+ pos = pickled;
125
+ end = pos + raw_length;
126
+ pos = _olm_unpickle_uint32(pos, end, &pickle_version);
127
+ if (pickle_version != PICKLE_VERSION) {
128
+ session->last_error = OLM_UNKNOWN_PICKLE_VERSION;
129
+ return (size_t)-1;
130
+ }
131
+ pos = megolm_unpickle(&(session->ratchet), pos, end);
132
+ pos = _olm_unpickle_ed25519_key_pair(pos, end, &(session->signing_key));
133
+
134
+ if (end != pos) {
135
+ /* We had the wrong number of bytes in the input. */
136
+ session->last_error = OLM_CORRUPTED_PICKLE;
137
+ return (size_t)-1;
138
+ }
139
+
140
+ return pickled_length;
141
+ }
142
+
143
+
144
+ size_t olm_init_outbound_group_session_random_length(
145
+ const OlmOutboundGroupSession *session
146
+ ) {
147
+ /* we need data to initialize the megolm ratchet, plus some more for the
148
+ * session id.
149
+ */
150
+ return MEGOLM_RATCHET_LENGTH +
151
+ ED25519_RANDOM_LENGTH;
152
+ }
153
+
154
+ size_t olm_init_outbound_group_session(
155
+ OlmOutboundGroupSession *session,
156
+ uint8_t *random, size_t random_length
157
+ ) {
158
+ const uint8_t *random_ptr = random;
159
+
160
+ if (random_length < olm_init_outbound_group_session_random_length(session)) {
161
+ /* Insufficient random data for new session */
162
+ session->last_error = OLM_NOT_ENOUGH_RANDOM;
163
+ return (size_t)-1;
164
+ }
165
+
166
+ megolm_init(&(session->ratchet), random_ptr, 0);
167
+ random_ptr += MEGOLM_RATCHET_LENGTH;
168
+
169
+ _olm_crypto_ed25519_generate_key(random_ptr, &(session->signing_key));
170
+ random_ptr += ED25519_RANDOM_LENGTH;
171
+
172
+ _olm_unset(random, random_length);
173
+ return 0;
174
+ }
175
+
176
+ static size_t raw_message_length(
177
+ OlmOutboundGroupSession *session,
178
+ size_t plaintext_length)
179
+ {
180
+ size_t ciphertext_length, mac_length;
181
+
182
+ ciphertext_length = megolm_cipher->ops->encrypt_ciphertext_length(
183
+ megolm_cipher, plaintext_length
184
+ );
185
+
186
+ mac_length = megolm_cipher->ops->mac_length(megolm_cipher);
187
+
188
+ return _olm_encode_group_message_length(
189
+ session->ratchet.counter,
190
+ ciphertext_length, mac_length, ED25519_SIGNATURE_LENGTH
191
+ );
192
+ }
193
+
194
+ size_t olm_group_encrypt_message_length(
195
+ OlmOutboundGroupSession *session,
196
+ size_t plaintext_length
197
+ ) {
198
+ size_t message_length = raw_message_length(session, plaintext_length);
199
+ return _olm_encode_base64_length(message_length);
200
+ }
201
+
202
+ /** write an un-base64-ed message to the buffer */
203
+ static size_t _encrypt(
204
+ OlmOutboundGroupSession *session, uint8_t const * plaintext, size_t plaintext_length,
205
+ uint8_t * buffer
206
+ ) {
207
+ size_t ciphertext_length, mac_length, message_length;
208
+ size_t result;
209
+ uint8_t *ciphertext_ptr;
210
+
211
+ ciphertext_length = megolm_cipher->ops->encrypt_ciphertext_length(
212
+ megolm_cipher,
213
+ plaintext_length
214
+ );
215
+
216
+ mac_length = megolm_cipher->ops->mac_length(megolm_cipher);
217
+
218
+ /* first we build the message structure, then we encrypt
219
+ * the plaintext into it.
220
+ */
221
+ message_length = _olm_encode_group_message(
222
+ OLM_PROTOCOL_VERSION,
223
+ session->ratchet.counter,
224
+ ciphertext_length,
225
+ buffer,
226
+ &ciphertext_ptr);
227
+
228
+ message_length += mac_length;
229
+
230
+ result = megolm_cipher->ops->encrypt(
231
+ megolm_cipher,
232
+ megolm_get_data(&(session->ratchet)), MEGOLM_RATCHET_LENGTH,
233
+ plaintext, plaintext_length,
234
+ ciphertext_ptr, ciphertext_length,
235
+ buffer, message_length
236
+ );
237
+
238
+ if (result == (size_t)-1) {
239
+ return result;
240
+ }
241
+
242
+ megolm_advance(&(session->ratchet));
243
+
244
+ /* sign the whole thing with the ed25519 key. */
245
+ _olm_crypto_ed25519_sign(
246
+ &(session->signing_key),
247
+ buffer, message_length,
248
+ buffer + message_length
249
+ );
250
+
251
+ return result;
252
+ }
253
+
254
+ size_t olm_group_encrypt(
255
+ OlmOutboundGroupSession *session,
256
+ uint8_t const * plaintext, size_t plaintext_length,
257
+ uint8_t * message, size_t max_message_length
258
+ ) {
259
+ size_t rawmsglen;
260
+ size_t result;
261
+ uint8_t *message_pos;
262
+
263
+ rawmsglen = raw_message_length(session, plaintext_length);
264
+
265
+ if (max_message_length < _olm_encode_base64_length(rawmsglen)) {
266
+ session->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL;
267
+ return (size_t)-1;
268
+ }
269
+
270
+ /* we construct the message at the end of the buffer, so that
271
+ * we have room to base64-encode it once we're done.
272
+ */
273
+ message_pos = message + _olm_encode_base64_length(rawmsglen) - rawmsglen;
274
+
275
+ /* write the message, and encrypt it, at message_pos */
276
+ result = _encrypt(session, plaintext, plaintext_length, message_pos);
277
+ if (result == (size_t)-1) {
278
+ return result;
279
+ }
280
+
281
+ /* bas64-encode it */
282
+ return _olm_encode_base64(
283
+ message_pos, rawmsglen, message
284
+ );
285
+ }
286
+
287
+
288
+ size_t olm_outbound_group_session_id_length(
289
+ const OlmOutboundGroupSession *session
290
+ ) {
291
+ return _olm_encode_base64_length(GROUP_SESSION_ID_LENGTH);
292
+ }
293
+
294
+ size_t olm_outbound_group_session_id(
295
+ OlmOutboundGroupSession *session,
296
+ uint8_t * id, size_t id_length
297
+ ) {
298
+ if (id_length < olm_outbound_group_session_id_length(session)) {
299
+ session->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL;
300
+ return (size_t)-1;
301
+ }
302
+
303
+ return _olm_encode_base64(
304
+ session->signing_key.public_key.public_key, GROUP_SESSION_ID_LENGTH, id
305
+ );
306
+ }
307
+
308
+ uint32_t olm_outbound_group_session_message_index(
309
+ OlmOutboundGroupSession *session
310
+ ) {
311
+ return session->ratchet.counter;
312
+ }
313
+
314
+ #define SESSION_KEY_RAW_LENGTH \
315
+ (1 + 4 + MEGOLM_RATCHET_LENGTH + ED25519_PUBLIC_KEY_LENGTH\
316
+ + ED25519_SIGNATURE_LENGTH)
317
+
318
+ size_t olm_outbound_group_session_key_length(
319
+ const OlmOutboundGroupSession *session
320
+ ) {
321
+ return _olm_encode_base64_length(SESSION_KEY_RAW_LENGTH);
322
+ }
323
+
324
+ size_t olm_outbound_group_session_key(
325
+ OlmOutboundGroupSession *session,
326
+ uint8_t * key, size_t key_length
327
+ ) {
328
+ uint8_t *raw;
329
+ uint8_t *ptr;
330
+ size_t encoded_length = olm_outbound_group_session_key_length(session);
331
+
332
+ if (key_length < encoded_length) {
333
+ session->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL;
334
+ return (size_t)-1;
335
+ }
336
+
337
+ /* put the raw data at the end of the output buffer. */
338
+ raw = ptr = key + encoded_length - SESSION_KEY_RAW_LENGTH;
339
+ *ptr++ = SESSION_KEY_VERSION;
340
+
341
+ uint32_t counter = session->ratchet.counter;
342
+ // Encode counter as a big endian 32-bit number.
343
+ for (unsigned i = 0; i < 4; i++) {
344
+ *ptr++ = 0xFF & (counter >> 24); counter <<= 8;
345
+ }
346
+
347
+ memcpy(ptr, megolm_get_data(&session->ratchet), MEGOLM_RATCHET_LENGTH);
348
+ ptr += MEGOLM_RATCHET_LENGTH;
349
+
350
+ memcpy(
351
+ ptr, session->signing_key.public_key.public_key,
352
+ ED25519_PUBLIC_KEY_LENGTH
353
+ );
354
+ ptr += ED25519_PUBLIC_KEY_LENGTH;
355
+
356
+ /* sign the whole thing with the ed25519 key. */
357
+ _olm_crypto_ed25519_sign(
358
+ &(session->signing_key),
359
+ raw, ptr - raw, ptr
360
+ );
361
+
362
+ return _olm_encode_base64(raw, SESSION_KEY_RAW_LENGTH, key);
363
+ }
@@ -0,0 +1,242 @@
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/pickle.hh"
16
+ #include "olm/pickle.h"
17
+
18
+ std::uint8_t * olm::pickle(
19
+ std::uint8_t * pos,
20
+ std::uint32_t value
21
+ ) {
22
+ pos += 4;
23
+ for (unsigned i = 4; i--;) { *(--pos) = value; value >>= 8; }
24
+ return pos + 4;
25
+ }
26
+
27
+
28
+ std::uint8_t const * olm::unpickle(
29
+ std::uint8_t const * pos, std::uint8_t const * end,
30
+ std::uint32_t & value
31
+ ) {
32
+ value = 0;
33
+ if (end < pos + 4) return end;
34
+ for (unsigned i = 4; i--;) { value <<= 8; value |= *(pos++); }
35
+ return pos;
36
+ }
37
+
38
+ std::uint8_t * olm::pickle(
39
+ std::uint8_t * pos,
40
+ bool value
41
+ ) {
42
+ *(pos++) = value ? 1 : 0;
43
+ return pos;
44
+ }
45
+
46
+ std::uint8_t const * olm::unpickle(
47
+ std::uint8_t const * pos, std::uint8_t const * end,
48
+ bool & value
49
+ ) {
50
+ if (pos == end) return end;
51
+ value = *(pos++);
52
+ return pos;
53
+ }
54
+
55
+ std::uint8_t * olm::pickle_bytes(
56
+ std::uint8_t * pos,
57
+ std::uint8_t const * bytes, std::size_t bytes_length
58
+ ) {
59
+ std::memcpy(pos, bytes, bytes_length);
60
+ return pos + bytes_length;
61
+ }
62
+
63
+ std::uint8_t const * olm::unpickle_bytes(
64
+ std::uint8_t const * pos, std::uint8_t const * end,
65
+ std::uint8_t * bytes, std::size_t bytes_length
66
+ ) {
67
+ if (end < pos + bytes_length) return end;
68
+ std::memcpy(bytes, pos, bytes_length);
69
+ return pos + bytes_length;
70
+ }
71
+
72
+
73
+ std::size_t olm::pickle_length(
74
+ const _olm_curve25519_public_key & value
75
+ ) {
76
+ return sizeof(value.public_key);
77
+ }
78
+
79
+
80
+ std::uint8_t * olm::pickle(
81
+ std::uint8_t * pos,
82
+ const _olm_curve25519_public_key & value
83
+ ) {
84
+ pos = olm::pickle_bytes(
85
+ pos, value.public_key, sizeof(value.public_key)
86
+ );
87
+ return pos;
88
+ }
89
+
90
+
91
+ std::uint8_t const * olm::unpickle(
92
+ std::uint8_t const * pos, std::uint8_t const * end,
93
+ _olm_curve25519_public_key & value
94
+ ) {
95
+ pos = olm::unpickle_bytes(
96
+ pos, end, value.public_key, sizeof(value.public_key)
97
+ );
98
+ return pos;
99
+
100
+ }
101
+
102
+
103
+ std::size_t olm::pickle_length(
104
+ const _olm_curve25519_key_pair & value
105
+ ) {
106
+ return sizeof(value.public_key.public_key)
107
+ + sizeof(value.private_key.private_key);
108
+ }
109
+
110
+
111
+ std::uint8_t * olm::pickle(
112
+ std::uint8_t * pos,
113
+ const _olm_curve25519_key_pair & value
114
+ ) {
115
+ pos = olm::pickle_bytes(
116
+ pos, value.public_key.public_key,
117
+ sizeof(value.public_key.public_key)
118
+ );
119
+ pos = olm::pickle_bytes(
120
+ pos, value.private_key.private_key,
121
+ sizeof(value.private_key.private_key)
122
+ );
123
+ return pos;
124
+ }
125
+
126
+
127
+ std::uint8_t const * olm::unpickle(
128
+ std::uint8_t const * pos, std::uint8_t const * end,
129
+ _olm_curve25519_key_pair & value
130
+ ) {
131
+ pos = olm::unpickle_bytes(
132
+ pos, end, value.public_key.public_key,
133
+ sizeof(value.public_key.public_key)
134
+ );
135
+ pos = olm::unpickle_bytes(
136
+ pos, end, value.private_key.private_key,
137
+ sizeof(value.private_key.private_key)
138
+ );
139
+ return pos;
140
+ }
141
+
142
+ ////// pickle.h implementations
143
+
144
+ std::size_t _olm_pickle_ed25519_public_key_length(
145
+ const _olm_ed25519_public_key * value
146
+ ) {
147
+ return sizeof(value->public_key);
148
+ }
149
+
150
+
151
+ std::uint8_t * _olm_pickle_ed25519_public_key(
152
+ std::uint8_t * pos,
153
+ const _olm_ed25519_public_key *value
154
+ ) {
155
+ pos = olm::pickle_bytes(
156
+ pos, value->public_key, sizeof(value->public_key)
157
+ );
158
+ return pos;
159
+ }
160
+
161
+
162
+ std::uint8_t const * _olm_unpickle_ed25519_public_key(
163
+ std::uint8_t const * pos, std::uint8_t const * end,
164
+ _olm_ed25519_public_key * value
165
+ ) {
166
+ pos = olm::unpickle_bytes(
167
+ pos, end, value->public_key, sizeof(value->public_key)
168
+ );
169
+ return pos;
170
+ }
171
+
172
+
173
+ std::size_t _olm_pickle_ed25519_key_pair_length(
174
+ const _olm_ed25519_key_pair *value
175
+ ) {
176
+ return sizeof(value->public_key.public_key)
177
+ + sizeof(value->private_key.private_key);
178
+ }
179
+
180
+
181
+ std::uint8_t * _olm_pickle_ed25519_key_pair(
182
+ std::uint8_t * pos,
183
+ const _olm_ed25519_key_pair *value
184
+ ) {
185
+ pos = olm::pickle_bytes(
186
+ pos, value->public_key.public_key,
187
+ sizeof(value->public_key.public_key)
188
+ );
189
+ pos = olm::pickle_bytes(
190
+ pos, value->private_key.private_key,
191
+ sizeof(value->private_key.private_key)
192
+ );
193
+ return pos;
194
+ }
195
+
196
+
197
+ std::uint8_t const * _olm_unpickle_ed25519_key_pair(
198
+ std::uint8_t const * pos, std::uint8_t const * end,
199
+ _olm_ed25519_key_pair *value
200
+ ) {
201
+ pos = olm::unpickle_bytes(
202
+ pos, end, value->public_key.public_key,
203
+ sizeof(value->public_key.public_key)
204
+ );
205
+ pos = olm::unpickle_bytes(
206
+ pos, end, value->private_key.private_key,
207
+ sizeof(value->private_key.private_key)
208
+ );
209
+ return pos;
210
+ }
211
+
212
+ uint8_t * _olm_pickle_uint32(uint8_t * pos, uint32_t value) {
213
+ return olm::pickle(pos, value);
214
+ }
215
+
216
+ uint8_t const * _olm_unpickle_uint32(
217
+ uint8_t const * pos, uint8_t const * end,
218
+ uint32_t *value
219
+ ) {
220
+ return olm::unpickle(pos, end, *value);
221
+ }
222
+
223
+ uint8_t * _olm_pickle_bool(uint8_t * pos, int value) {
224
+ return olm::pickle(pos, (bool)value);
225
+ }
226
+
227
+ uint8_t const * _olm_unpickle_bool(
228
+ uint8_t const * pos, uint8_t const * end,
229
+ int *value
230
+ ) {
231
+ return olm::unpickle(pos, end, *reinterpret_cast<bool *>(value));
232
+ }
233
+
234
+ uint8_t * _olm_pickle_bytes(uint8_t * pos, uint8_t const * bytes,
235
+ size_t bytes_length) {
236
+ return olm::pickle_bytes(pos, bytes, bytes_length);
237
+ }
238
+
239
+ uint8_t const * _olm_unpickle_bytes(uint8_t const * pos, uint8_t const * end,
240
+ uint8_t * bytes, size_t bytes_length) {
241
+ return olm::unpickle_bytes(pos, end, bytes, bytes_length);
242
+ }
@@ -0,0 +1,92 @@
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
+ #include "olm/pickle_encoding.h"
17
+
18
+ #include "olm/base64.h"
19
+ #include "olm/cipher.h"
20
+ #include "olm/olm.h"
21
+
22
+ static const struct _olm_cipher_aes_sha_256 PICKLE_CIPHER =
23
+ OLM_CIPHER_INIT_AES_SHA_256("Pickle");
24
+
25
+ size_t _olm_enc_output_length(
26
+ size_t raw_length
27
+ ) {
28
+ const struct _olm_cipher *cipher = OLM_CIPHER_BASE(&PICKLE_CIPHER);
29
+ size_t length = cipher->ops->encrypt_ciphertext_length(cipher, raw_length);
30
+ length += cipher->ops->mac_length(cipher);
31
+ return _olm_encode_base64_length(length);
32
+ }
33
+
34
+ uint8_t * _olm_enc_output_pos(
35
+ uint8_t * output,
36
+ size_t raw_length
37
+ ) {
38
+ const struct _olm_cipher *cipher = OLM_CIPHER_BASE(&PICKLE_CIPHER);
39
+ size_t length = cipher->ops->encrypt_ciphertext_length(cipher, raw_length);
40
+ length += cipher->ops->mac_length(cipher);
41
+ return output + _olm_encode_base64_length(length) - length;
42
+ }
43
+
44
+ size_t _olm_enc_output(
45
+ uint8_t const * key, size_t key_length,
46
+ uint8_t * output, size_t raw_length
47
+ ) {
48
+ const struct _olm_cipher *cipher = OLM_CIPHER_BASE(&PICKLE_CIPHER);
49
+ size_t ciphertext_length = cipher->ops->encrypt_ciphertext_length(
50
+ cipher, raw_length
51
+ );
52
+ size_t length = ciphertext_length + cipher->ops->mac_length(cipher);
53
+ size_t base64_length = _olm_encode_base64_length(length);
54
+ uint8_t * raw_output = output + base64_length - length;
55
+ cipher->ops->encrypt(
56
+ cipher,
57
+ key, key_length,
58
+ raw_output, raw_length,
59
+ raw_output, ciphertext_length,
60
+ raw_output, length
61
+ );
62
+ _olm_encode_base64(raw_output, length, output);
63
+ return base64_length;
64
+ }
65
+
66
+
67
+ size_t _olm_enc_input(uint8_t const * key, size_t key_length,
68
+ uint8_t * input, size_t b64_length,
69
+ enum OlmErrorCode * last_error
70
+ ) {
71
+ size_t enc_length = _olm_decode_base64_length(b64_length);
72
+ if (enc_length == (size_t)-1) {
73
+ if (last_error) {
74
+ *last_error = OLM_INVALID_BASE64;
75
+ }
76
+ return (size_t)-1;
77
+ }
78
+ _olm_decode_base64(input, b64_length, input);
79
+ const struct _olm_cipher *cipher = OLM_CIPHER_BASE(&PICKLE_CIPHER);
80
+ size_t raw_length = enc_length - cipher->ops->mac_length(cipher);
81
+ size_t result = cipher->ops->decrypt(
82
+ cipher,
83
+ key, key_length,
84
+ input, enc_length,
85
+ input, raw_length,
86
+ input, raw_length
87
+ );
88
+ if (result == (size_t)-1 && last_error) {
89
+ *last_error = OLM_BAD_ACCOUNT_KEY;
90
+ }
91
+ return result;
92
+ }