ruby_olm 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,160 @@
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_ACCOUNT_HH_
16
+ #define OLM_ACCOUNT_HH_
17
+
18
+ #include "olm/list.hh"
19
+ #include "olm/crypto.h"
20
+ #include "olm/error.h"
21
+
22
+ #include <cstdint>
23
+
24
+ namespace olm {
25
+
26
+
27
+ struct IdentityKeys {
28
+ _olm_ed25519_key_pair ed25519_key;
29
+ _olm_curve25519_key_pair curve25519_key;
30
+ };
31
+
32
+ struct OneTimeKey {
33
+ std::uint32_t id;
34
+ bool published;
35
+ _olm_curve25519_key_pair key;
36
+ };
37
+
38
+
39
+ static std::size_t const MAX_ONE_TIME_KEYS = 100;
40
+
41
+
42
+ struct Account {
43
+ Account();
44
+ IdentityKeys identity_keys;
45
+ List<OneTimeKey, MAX_ONE_TIME_KEYS> one_time_keys;
46
+ std::uint32_t next_one_time_key_id;
47
+ OlmErrorCode last_error;
48
+
49
+ /** Number of random bytes needed to create a new account */
50
+ std::size_t new_account_random_length();
51
+
52
+ /** Create a new account. Returns std::size_t(-1) on error. If the number of
53
+ * random bytes is too small then last_error will be NOT_ENOUGH_RANDOM */
54
+ std::size_t new_account(
55
+ uint8_t const * random, std::size_t random_length
56
+ );
57
+
58
+ /** Number of bytes needed to output the identity keys for this account */
59
+ std::size_t get_identity_json_length();
60
+
61
+ /** Output the identity keys for this account as JSON in the following
62
+ * format:
63
+ *
64
+ * {"curve25519":"<43 base64 characters>"
65
+ * ,"ed25519":"<43 base64 characters>"
66
+ * }
67
+ *
68
+ *
69
+ * Returns the size of the JSON written or std::size_t(-1) on error.
70
+ * If the buffer is too small last_error will be OUTPUT_BUFFER_TOO_SMALL. */
71
+ std::size_t get_identity_json(
72
+ std::uint8_t * identity_json, std::size_t identity_json_length
73
+ );
74
+
75
+ /**
76
+ * The length of an ed25519 signature in bytes.
77
+ */
78
+ std::size_t signature_length();
79
+
80
+ /**
81
+ * Signs a message with the ed25519 key for this account.
82
+ */
83
+ std::size_t sign(
84
+ std::uint8_t const * message, std::size_t message_length,
85
+ std::uint8_t * signature, std::size_t signature_length
86
+ );
87
+
88
+ /** Number of bytes needed to output the one time keys for this account */
89
+ std::size_t get_one_time_keys_json_length();
90
+
91
+ /** Output the one time keys that haven't been published yet as JSON:
92
+ *
93
+ * {"curve25519":
94
+ * ["<6 byte key id>":"<43 base64 characters>"
95
+ * ,"<6 byte key id>":"<43 base64 characters>"
96
+ * ...
97
+ * ]
98
+ * }
99
+ *
100
+ * Returns the size of the JSON written or std::size_t(-1) on error.
101
+ * If the buffer is too small last_error will be OUTPUT_BUFFER_TOO_SMALL.
102
+ */
103
+ std::size_t get_one_time_keys_json(
104
+ std::uint8_t * one_time_json, std::size_t one_time_json_length
105
+ );
106
+
107
+ /** Mark the current list of one_time_keys as being published. They
108
+ * will no longer be returned by get_one_time_keys_json_length(). */
109
+ std::size_t mark_keys_as_published();
110
+
111
+ /** The largest number of one time keys this account can store. */
112
+ std::size_t max_number_of_one_time_keys();
113
+
114
+ /** The number of random bytes needed to generate a given number of new one
115
+ * time keys. */
116
+ std::size_t generate_one_time_keys_random_length(
117
+ std::size_t number_of_keys
118
+ );
119
+
120
+ /** Generates a number of new one time keys. If the total number of keys
121
+ * stored by this account exceeds max_number_of_one_time_keys() then the
122
+ * old keys are discarded. Returns std::size_t(-1) on error. If the number
123
+ * of random bytes is too small then last_error will be NOT_ENOUGH_RANDOM */
124
+ std::size_t generate_one_time_keys(
125
+ std::size_t number_of_keys,
126
+ std::uint8_t const * random, std::size_t random_length
127
+ );
128
+
129
+ /** Lookup a one time key with the given public key */
130
+ OneTimeKey const * lookup_key(
131
+ _olm_curve25519_public_key const & public_key
132
+ );
133
+
134
+ /** Remove a one time key with the given public key */
135
+ std::size_t remove_key(
136
+ _olm_curve25519_public_key const & public_key
137
+ );
138
+ };
139
+
140
+
141
+ std::size_t pickle_length(
142
+ Account const & value
143
+ );
144
+
145
+
146
+ std::uint8_t * pickle(
147
+ std::uint8_t * pos,
148
+ Account const & value
149
+ );
150
+
151
+
152
+ std::uint8_t const * unpickle(
153
+ std::uint8_t const * pos, std::uint8_t const * end,
154
+ Account & value
155
+ );
156
+
157
+
158
+ } // namespace olm
159
+
160
+ #endif /* OLM_ACCOUNT_HH_ */
@@ -0,0 +1,77 @@
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
+
16
+ /* C bindings for base64 functions */
17
+
18
+
19
+ #ifndef OLM_BASE64_H_
20
+ #define OLM_BASE64_H_
21
+
22
+ #include <stddef.h>
23
+ #include <stdint.h>
24
+
25
+ #ifdef __cplusplus
26
+ extern "C" {
27
+ #endif
28
+
29
+
30
+ /**
31
+ * The number of bytes of unpadded base64 needed to encode a length of input.
32
+ */
33
+ size_t _olm_encode_base64_length(
34
+ size_t input_length
35
+ );
36
+
37
+ /**
38
+ * Encode the raw input as unpadded base64.
39
+ * Writes encode_base64_length(input_length) bytes to the output buffer.
40
+ * The input can overlap with the last three quarters of the output buffer.
41
+ * That is, the input pointer may be output + output_length - input_length.
42
+ *
43
+ * Returns number of bytes encoded
44
+ */
45
+ size_t _olm_encode_base64(
46
+ uint8_t const * input, size_t input_length,
47
+ uint8_t * output
48
+ );
49
+
50
+ /**
51
+ * The number of bytes of raw data a length of unpadded base64 will encode to.
52
+ * Returns size_t(-1) if the length is not a valid length for base64.
53
+ */
54
+ size_t _olm_decode_base64_length(
55
+ size_t input_length
56
+ );
57
+
58
+ /**
59
+ * Decodes the unpadded base64 input to raw bytes.
60
+ * Writes decode_base64_length(input_length) bytes to the output buffer.
61
+ * The output can overlap with the first three quarters of the input buffer.
62
+ * That is, the input pointers and output pointer may be the same.
63
+ *
64
+ * Returns number of bytes decoded
65
+ */
66
+ size_t _olm_decode_base64(
67
+ uint8_t const * input, size_t input_length,
68
+ uint8_t * output
69
+ );
70
+
71
+
72
+ #ifdef __cplusplus
73
+ } // extern "C"
74
+ #endif
75
+
76
+
77
+ #endif /* OLM_BASE64_H_ */
@@ -0,0 +1,63 @@
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_BASE64_HH_
16
+ #define OLM_BASE64_HH_
17
+
18
+ #include <cstddef>
19
+ #include <cstdint>
20
+
21
+ namespace olm {
22
+
23
+ /**
24
+ * The number of bytes of unpadded base64 needed to encode a length of input.
25
+ */
26
+ std::size_t encode_base64_length(
27
+ std::size_t input_length
28
+ );
29
+
30
+ /**
31
+ * Encode the raw input as unpadded base64.
32
+ * Writes encode_base64_length(input_length) bytes to the output buffer.
33
+ * The input can overlap with the last three quarters of the output buffer.
34
+ * That is, the input pointer may be output + output_length - input_length.
35
+ */
36
+ std::uint8_t * encode_base64(
37
+ std::uint8_t const * input, std::size_t input_length,
38
+ std::uint8_t * output
39
+ );
40
+
41
+ /**
42
+ * The number of bytes of raw data a length of unpadded base64 will encode to.
43
+ * Returns std::size_t(-1) if the length is not a valid length for base64.
44
+ */
45
+ std::size_t decode_base64_length(
46
+ std::size_t input_length
47
+ );
48
+
49
+ /**
50
+ * Decodes the unpadded base64 input to raw bytes.
51
+ * Writes decode_base64_length(input_length) bytes to the output buffer.
52
+ * The output can overlap with the first three quarters of the input buffer.
53
+ * That is, the input pointers and output pointer may be the same.
54
+ */
55
+ std::uint8_t const * decode_base64(
56
+ std::uint8_t const * input, std::size_t input_length,
57
+ std::uint8_t * output
58
+ );
59
+
60
+ } // namespace olm
61
+
62
+
63
+ #endif /* OLM_BASE64_HH_ */
@@ -0,0 +1,138 @@
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 OLM_CIPHER_H_
17
+ #define OLM_CIPHER_H_
18
+
19
+ #include <stdint.h>
20
+ #include <stdlib.h>
21
+
22
+ #ifdef __cplusplus
23
+ extern "C" {
24
+ #endif
25
+
26
+ struct _olm_cipher;
27
+
28
+ struct _olm_cipher_ops {
29
+ /**
30
+ * Returns the length of the message authentication code that will be
31
+ * appended to the output.
32
+ */
33
+ size_t (*mac_length)(const struct _olm_cipher *cipher);
34
+
35
+ /**
36
+ * Returns the length of cipher-text for a given length of plain-text.
37
+ */
38
+ size_t (*encrypt_ciphertext_length)(
39
+ const struct _olm_cipher *cipher,
40
+ size_t plaintext_length
41
+ );
42
+
43
+ /*
44
+ * Encrypts the plain-text into the output buffer and authenticates the
45
+ * contents of the output buffer covering both cipher-text and any other
46
+ * associated data in the output buffer.
47
+ *
48
+ * |---------------------------------------output_length-->|
49
+ * output |--ciphertext_length-->| |---mac_length-->|
50
+ * ciphertext
51
+ *
52
+ * The plain-text pointers and cipher-text pointers may be the same.
53
+ *
54
+ * Returns size_t(-1) if the length of the cipher-text or the output
55
+ * buffer is too small. Otherwise returns the length of the output buffer.
56
+ */
57
+ size_t (*encrypt)(
58
+ const struct _olm_cipher *cipher,
59
+ uint8_t const * key, size_t key_length,
60
+ uint8_t const * plaintext, size_t plaintext_length,
61
+ uint8_t * ciphertext, size_t ciphertext_length,
62
+ uint8_t * output, size_t output_length
63
+ );
64
+
65
+ /**
66
+ * Returns the maximum length of plain-text that a given length of
67
+ * cipher-text can contain.
68
+ */
69
+ size_t (*decrypt_max_plaintext_length)(
70
+ const struct _olm_cipher *cipher,
71
+ size_t ciphertext_length
72
+ );
73
+
74
+ /**
75
+ * Authenticates the input and decrypts the cipher-text into the plain-text
76
+ * buffer.
77
+ *
78
+ * |----------------------------------------input_length-->|
79
+ * input |--ciphertext_length-->| |---mac_length-->|
80
+ * ciphertext
81
+ *
82
+ * The plain-text pointers and cipher-text pointers may be the same.
83
+ *
84
+ * Returns size_t(-1) if the length of the plain-text buffer is too
85
+ * small or if the authentication check fails. Otherwise returns the length
86
+ * of the plain text.
87
+ */
88
+ size_t (*decrypt)(
89
+ const struct _olm_cipher *cipher,
90
+ uint8_t const * key, size_t key_length,
91
+ uint8_t const * input, size_t input_length,
92
+ uint8_t const * ciphertext, size_t ciphertext_length,
93
+ uint8_t * plaintext, size_t max_plaintext_length
94
+ );
95
+ };
96
+
97
+ struct _olm_cipher {
98
+ const struct _olm_cipher_ops *ops;
99
+ /* cipher-specific fields follow */
100
+ };
101
+
102
+ struct _olm_cipher_aes_sha_256 {
103
+ struct _olm_cipher base_cipher;
104
+
105
+ /** context string for the HKDF used for deriving the AES256 key, HMAC key,
106
+ * and AES IV, from the key material passed to encrypt/decrypt.
107
+ */
108
+ uint8_t const * kdf_info;
109
+
110
+ /** length of context string kdf_info */
111
+ size_t kdf_info_length;
112
+ };
113
+
114
+ extern const struct _olm_cipher_ops _olm_cipher_aes_sha_256_ops;
115
+
116
+ /**
117
+ * get an initializer for an instance of struct _olm_cipher_aes_sha_256.
118
+ *
119
+ * To use it, declare:
120
+ *
121
+ * struct _olm_cipher_aes_sha_256 MY_CIPHER =
122
+ * OLM_CIPHER_INIT_AES_SHA_256("MY_KDF");
123
+ * struct _olm_cipher *cipher = OLM_CIPHER_BASE(&MY_CIPHER);
124
+ */
125
+ #define OLM_CIPHER_INIT_AES_SHA_256(KDF_INFO) { \
126
+ /*.base_cipher = */{ &_olm_cipher_aes_sha_256_ops },\
127
+ /*.kdf_info = */(uint8_t *)(KDF_INFO), \
128
+ /*.kdf_info_length = */sizeof(KDF_INFO) - 1 \
129
+ }
130
+ #define OLM_CIPHER_BASE(CIPHER) \
131
+ (&((CIPHER)->base_cipher))
132
+
133
+
134
+ #ifdef __cplusplus
135
+ } /* extern "C" */
136
+ #endif
137
+
138
+ #endif /* OLM_CIPHER_H_ */
@@ -0,0 +1,202 @@
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
+ /* C-compatible crpyto utility functions. At some point all of crypto.hh will
17
+ * move here.
18
+ */
19
+
20
+ #ifndef OLM_CRYPTO_H_
21
+ #define OLM_CRYPTO_H_
22
+
23
+ #include <stdint.h>
24
+ #include <stdlib.h>
25
+
26
+ #ifdef __cplusplus
27
+ extern "C" {
28
+ #endif
29
+
30
+ /** length of a sha256 hash */
31
+ #define SHA256_OUTPUT_LENGTH 32
32
+
33
+ /** length of a public or private Curve25519 key */
34
+ #define CURVE25519_KEY_LENGTH 32
35
+
36
+ /** length of the shared secret created by a Curve25519 ECDH operation */
37
+ #define CURVE25519_SHARED_SECRET_LENGTH 32
38
+
39
+ /** amount of random data required to create a Curve25519 keypair */
40
+ #define CURVE25519_RANDOM_LENGTH CURVE25519_KEY_LENGTH
41
+
42
+ /** length of a public Ed25519 key */
43
+ #define ED25519_PUBLIC_KEY_LENGTH 32
44
+
45
+ /** length of a private Ed25519 key */
46
+ #define ED25519_PRIVATE_KEY_LENGTH 64
47
+
48
+ /** amount of random data required to create a Ed25519 keypair */
49
+ #define ED25519_RANDOM_LENGTH 32
50
+
51
+ /** length of an Ed25519 signature */
52
+ #define ED25519_SIGNATURE_LENGTH 64
53
+
54
+ /** length of an aes256 key */
55
+ #define AES256_KEY_LENGTH 32
56
+
57
+ /** length of an aes256 initialisation vector */
58
+ #define AES256_IV_LENGTH 16
59
+
60
+ struct _olm_aes256_key {
61
+ uint8_t key[AES256_KEY_LENGTH];
62
+ };
63
+
64
+ struct _olm_aes256_iv {
65
+ uint8_t iv[AES256_IV_LENGTH];
66
+ };
67
+
68
+
69
+ struct _olm_curve25519_public_key {
70
+ uint8_t public_key[CURVE25519_KEY_LENGTH];
71
+ };
72
+
73
+ struct _olm_curve25519_private_key {
74
+ uint8_t private_key[CURVE25519_KEY_LENGTH];
75
+ };
76
+
77
+ struct _olm_curve25519_key_pair {
78
+ struct _olm_curve25519_public_key public_key;
79
+ struct _olm_curve25519_private_key private_key;
80
+ };
81
+
82
+ struct _olm_ed25519_public_key {
83
+ uint8_t public_key[ED25519_PUBLIC_KEY_LENGTH];
84
+ };
85
+
86
+ struct _olm_ed25519_private_key {
87
+ uint8_t private_key[ED25519_PRIVATE_KEY_LENGTH];
88
+ };
89
+
90
+ struct _olm_ed25519_key_pair {
91
+ struct _olm_ed25519_public_key public_key;
92
+ struct _olm_ed25519_private_key private_key;
93
+ };
94
+
95
+
96
+ /** The length of output the aes_encrypt_cbc function will write */
97
+ size_t _olm_crypto_aes_encrypt_cbc_length(
98
+ size_t input_length
99
+ );
100
+
101
+ /** Encrypts the input using AES256 in CBC mode with PKCS#7 padding.
102
+ * The output buffer must be big enough to hold the output including padding */
103
+ void _olm_crypto_aes_encrypt_cbc(
104
+ const struct _olm_aes256_key *key,
105
+ const struct _olm_aes256_iv *iv,
106
+ const uint8_t *input, size_t input_length,
107
+ uint8_t *output
108
+ );
109
+
110
+ /** Decrypts the input using AES256 in CBC mode. The output buffer must be at
111
+ * least the same size as the input buffer. Returns the length of the plaintext
112
+ * without padding on success or std::size_t(-1) if the padding is invalid.
113
+ */
114
+ size_t _olm_crypto_aes_decrypt_cbc(
115
+ const struct _olm_aes256_key *key,
116
+ const struct _olm_aes256_iv *iv,
117
+ uint8_t const * input, size_t input_length,
118
+ uint8_t * output
119
+ );
120
+
121
+
122
+ /** Computes SHA-256 of the input. The output buffer must be a least
123
+ * SHA256_OUTPUT_LENGTH (32) bytes long. */
124
+ void _olm_crypto_sha256(
125
+ uint8_t const * input, size_t input_length,
126
+ uint8_t * output
127
+ );
128
+
129
+ /** HMAC: Keyed-Hashing for Message Authentication
130
+ * http://tools.ietf.org/html/rfc2104
131
+ * Computes HMAC-SHA-256 of the input for the key. The output buffer must
132
+ * be at least SHA256_OUTPUT_LENGTH (32) bytes long. */
133
+ void _olm_crypto_hmac_sha256(
134
+ uint8_t const * key, size_t key_length,
135
+ uint8_t const * input, size_t input_length,
136
+ uint8_t * output
137
+ );
138
+
139
+
140
+ /** HMAC-based Key Derivation Function (HKDF)
141
+ * https://tools.ietf.org/html/rfc5869
142
+ * Derives key material from the input bytes. */
143
+ void _olm_crypto_hkdf_sha256(
144
+ uint8_t const * input, size_t input_length,
145
+ uint8_t const * info, size_t info_length,
146
+ uint8_t const * salt, size_t salt_length,
147
+ uint8_t * output, size_t output_length
148
+ );
149
+
150
+
151
+ /** Generate a curve25519 key pair
152
+ * random_32_bytes should be CURVE25519_RANDOM_LENGTH (32) bytes long.
153
+ */
154
+ void _olm_crypto_curve25519_generate_key(
155
+ uint8_t const * random_32_bytes,
156
+ struct _olm_curve25519_key_pair *output
157
+ );
158
+
159
+
160
+ /** Create a shared secret using our private key and their public key.
161
+ * The output buffer must be at least CURVE25519_SHARED_SECRET_LENGTH (32) bytes long.
162
+ */
163
+ void _olm_crypto_curve25519_shared_secret(
164
+ const struct _olm_curve25519_key_pair *our_key,
165
+ const struct _olm_curve25519_public_key *their_key,
166
+ uint8_t * output
167
+ );
168
+
169
+ /** Generate an ed25519 key pair
170
+ * random_32_bytes should be ED25519_RANDOM_LENGTH (32) bytes long.
171
+ */
172
+ void _olm_crypto_ed25519_generate_key(
173
+ uint8_t const * random_bytes,
174
+ struct _olm_ed25519_key_pair *output
175
+ );
176
+
177
+ /** Signs the message using our private key.
178
+ *
179
+ * The output buffer must be at least ED25519_SIGNATURE_LENGTH (64) bytes
180
+ * long. */
181
+ void _olm_crypto_ed25519_sign(
182
+ const struct _olm_ed25519_key_pair *our_key,
183
+ const uint8_t * message, size_t message_length,
184
+ uint8_t * output
185
+ );
186
+
187
+ /** Verify an ed25519 signature
188
+ * The signature input buffer must be ED25519_SIGNATURE_LENGTH (64) bytes long.
189
+ * Returns non-zero if the signature is valid. */
190
+ int _olm_crypto_ed25519_verify(
191
+ const struct _olm_ed25519_public_key *their_key,
192
+ const uint8_t * message, size_t message_length,
193
+ const uint8_t * signature
194
+ );
195
+
196
+
197
+
198
+ #ifdef __cplusplus
199
+ } // extern "C"
200
+ #endif
201
+
202
+ #endif /* OLM_CRYPTO_H_ */