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,412 @@
1
+ /* Copyright 2018 New Vector 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/pk.h"
16
+ #include "olm/cipher.h"
17
+ #include "olm/crypto.h"
18
+ #include "olm/ratchet.hh"
19
+ #include "olm/error.h"
20
+ #include "olm/memory.hh"
21
+ #include "olm/base64.hh"
22
+ #include "olm/pickle_encoding.h"
23
+ #include "olm/pickle.hh"
24
+
25
+ static const std::size_t MAC_LENGTH = 8;
26
+
27
+ const struct _olm_cipher_aes_sha_256 olm_pk_cipher_aes_sha256 =
28
+ OLM_CIPHER_INIT_AES_SHA_256("");
29
+ const struct _olm_cipher *olm_pk_cipher =
30
+ OLM_CIPHER_BASE(&olm_pk_cipher_aes_sha256);
31
+
32
+ extern "C" {
33
+
34
+ struct OlmPkEncryption {
35
+ OlmErrorCode last_error;
36
+ _olm_curve25519_public_key recipient_key;
37
+ };
38
+
39
+ const char * olm_pk_encryption_last_error(
40
+ OlmPkEncryption * encryption
41
+ ) {
42
+ auto error = encryption->last_error;
43
+ return _olm_error_to_string(error);
44
+ }
45
+
46
+ size_t olm_pk_encryption_size(void) {
47
+ return sizeof(OlmPkEncryption);
48
+ }
49
+
50
+ OlmPkEncryption *olm_pk_encryption(
51
+ void * memory
52
+ ) {
53
+ olm::unset(memory, sizeof(OlmPkEncryption));
54
+ return new(memory) OlmPkEncryption;
55
+ }
56
+
57
+ size_t olm_clear_pk_encryption(
58
+ OlmPkEncryption *encryption
59
+ ) {
60
+ /* Clear the memory backing the encryption */
61
+ olm::unset(encryption, sizeof(OlmPkEncryption));
62
+ /* Initialise a fresh encryption object in case someone tries to use it */
63
+ new(encryption) OlmPkEncryption();
64
+ return sizeof(OlmPkEncryption);
65
+ }
66
+
67
+ size_t olm_pk_encryption_set_recipient_key (
68
+ OlmPkEncryption *encryption,
69
+ void const * key, size_t key_length
70
+ ) {
71
+ if (key_length < olm_pk_key_length()) {
72
+ encryption->last_error =
73
+ OlmErrorCode::OLM_INPUT_BUFFER_TOO_SMALL;
74
+ return std::size_t(-1);
75
+ }
76
+ olm::decode_base64(
77
+ (const uint8_t*)key,
78
+ olm_pk_key_length(),
79
+ (uint8_t *)encryption->recipient_key.public_key
80
+ );
81
+ return 0;
82
+ }
83
+
84
+ size_t olm_pk_ciphertext_length(
85
+ OlmPkEncryption *encryption,
86
+ size_t plaintext_length
87
+ ) {
88
+ return olm::encode_base64_length(
89
+ _olm_cipher_aes_sha_256_ops.encrypt_ciphertext_length(olm_pk_cipher, plaintext_length)
90
+ );
91
+ }
92
+
93
+ size_t olm_pk_mac_length(
94
+ OlmPkEncryption *encryption
95
+ ) {
96
+ return olm::encode_base64_length(_olm_cipher_aes_sha_256_ops.mac_length(olm_pk_cipher));
97
+ }
98
+
99
+ size_t olm_pk_encrypt_random_length(
100
+ OlmPkEncryption *encryption
101
+ ) {
102
+ return CURVE25519_KEY_LENGTH;
103
+ }
104
+
105
+ size_t olm_pk_encrypt(
106
+ OlmPkEncryption *encryption,
107
+ void const * plaintext, size_t plaintext_length,
108
+ void * ciphertext, size_t ciphertext_length,
109
+ void * mac, size_t mac_length,
110
+ void * ephemeral_key, size_t ephemeral_key_size,
111
+ void * random, size_t random_length
112
+ ) {
113
+ if (ciphertext_length
114
+ < olm_pk_ciphertext_length(encryption, plaintext_length)
115
+ || mac_length
116
+ < _olm_cipher_aes_sha_256_ops.mac_length(olm_pk_cipher)
117
+ || ephemeral_key_size
118
+ < olm_pk_key_length()) {
119
+ encryption->last_error =
120
+ OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
121
+ return std::size_t(-1);
122
+ }
123
+ if (random_length < olm_pk_encrypt_random_length(encryption)) {
124
+ encryption->last_error =
125
+ OlmErrorCode::OLM_NOT_ENOUGH_RANDOM;
126
+ return std::size_t(-1);
127
+ }
128
+
129
+ _olm_curve25519_key_pair ephemeral_keypair;
130
+ _olm_crypto_curve25519_generate_key((uint8_t *) random, &ephemeral_keypair);
131
+ olm::encode_base64(
132
+ (const uint8_t *)ephemeral_keypair.public_key.public_key,
133
+ CURVE25519_KEY_LENGTH,
134
+ (uint8_t *)ephemeral_key
135
+ );
136
+
137
+ olm::SharedKey secret;
138
+ _olm_crypto_curve25519_shared_secret(&ephemeral_keypair, &encryption->recipient_key, secret);
139
+ size_t raw_ciphertext_length =
140
+ _olm_cipher_aes_sha_256_ops.encrypt_ciphertext_length(olm_pk_cipher, plaintext_length);
141
+ uint8_t *ciphertext_pos = (uint8_t *) ciphertext + ciphertext_length - raw_ciphertext_length;
142
+ uint8_t raw_mac[MAC_LENGTH];
143
+ size_t result = _olm_cipher_aes_sha_256_ops.encrypt(
144
+ olm_pk_cipher,
145
+ secret, sizeof(secret),
146
+ (const uint8_t *) plaintext, plaintext_length,
147
+ (uint8_t *) ciphertext_pos, raw_ciphertext_length,
148
+ (uint8_t *) raw_mac, MAC_LENGTH
149
+ );
150
+ if (result != std::size_t(-1)) {
151
+ olm::encode_base64(raw_mac, MAC_LENGTH, (uint8_t *)mac);
152
+ olm::encode_base64(ciphertext_pos, raw_ciphertext_length, (uint8_t *)ciphertext);
153
+ }
154
+ return result;
155
+ }
156
+
157
+ struct OlmPkDecryption {
158
+ OlmErrorCode last_error;
159
+ _olm_curve25519_key_pair key_pair;
160
+ };
161
+
162
+ const char * olm_pk_decryption_last_error(
163
+ OlmPkDecryption * decryption
164
+ ) {
165
+ auto error = decryption->last_error;
166
+ return _olm_error_to_string(error);
167
+ }
168
+
169
+ size_t olm_pk_decryption_size(void) {
170
+ return sizeof(OlmPkDecryption);
171
+ }
172
+
173
+ OlmPkDecryption *olm_pk_decryption(
174
+ void * memory
175
+ ) {
176
+ olm::unset(memory, sizeof(OlmPkDecryption));
177
+ return new(memory) OlmPkDecryption;
178
+ }
179
+
180
+ size_t olm_clear_pk_decryption(
181
+ OlmPkDecryption *decryption
182
+ ) {
183
+ /* Clear the memory backing the decryption */
184
+ olm::unset(decryption, sizeof(OlmPkDecryption));
185
+ /* Initialise a fresh decryption object in case someone tries to use it */
186
+ new(decryption) OlmPkDecryption();
187
+ return sizeof(OlmPkDecryption);
188
+ }
189
+
190
+ size_t olm_pk_private_key_length(void) {
191
+ return CURVE25519_KEY_LENGTH;
192
+ }
193
+
194
+ size_t olm_pk_generate_key_random_length(void) {
195
+ return olm_pk_private_key_length();
196
+ }
197
+
198
+ size_t olm_pk_key_length(void) {
199
+ return olm::encode_base64_length(CURVE25519_KEY_LENGTH);
200
+ }
201
+
202
+ size_t olm_pk_key_from_private(
203
+ OlmPkDecryption * decryption,
204
+ void * pubkey, size_t pubkey_length,
205
+ void * privkey, size_t privkey_length
206
+ ) {
207
+ if (pubkey_length < olm_pk_key_length()) {
208
+ decryption->last_error =
209
+ OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
210
+ return std::size_t(-1);
211
+ }
212
+ if (privkey_length < olm_pk_private_key_length()) {
213
+ decryption->last_error =
214
+ OlmErrorCode::OLM_INPUT_BUFFER_TOO_SMALL;
215
+ return std::size_t(-1);
216
+ }
217
+
218
+ _olm_crypto_curve25519_generate_key((uint8_t *) privkey, &decryption->key_pair);
219
+ olm::encode_base64(
220
+ (const uint8_t *)decryption->key_pair.public_key.public_key,
221
+ CURVE25519_KEY_LENGTH,
222
+ (uint8_t *)pubkey
223
+ );
224
+ return 0;
225
+ }
226
+
227
+ size_t olm_pk_generate_key(
228
+ OlmPkDecryption * decryption,
229
+ void * pubkey, size_t pubkey_length,
230
+ void * privkey, size_t privkey_length
231
+ ) {
232
+ return olm_pk_key_from_private(decryption, pubkey, pubkey_length, privkey, privkey_length);
233
+ }
234
+
235
+ namespace {
236
+ static const std::uint32_t PK_DECRYPTION_PICKLE_VERSION = 1;
237
+
238
+ static std::size_t pickle_length(
239
+ OlmPkDecryption const & value
240
+ ) {
241
+ std::size_t length = 0;
242
+ length += olm::pickle_length(PK_DECRYPTION_PICKLE_VERSION);
243
+ length += olm::pickle_length(value.key_pair);
244
+ return length;
245
+ }
246
+
247
+
248
+ static std::uint8_t * pickle(
249
+ std::uint8_t * pos,
250
+ OlmPkDecryption const & value
251
+ ) {
252
+ pos = olm::pickle(pos, PK_DECRYPTION_PICKLE_VERSION);
253
+ pos = olm::pickle(pos, value.key_pair);
254
+ return pos;
255
+ }
256
+
257
+
258
+ static std::uint8_t const * unpickle(
259
+ std::uint8_t const * pos, std::uint8_t const * end,
260
+ OlmPkDecryption & value
261
+ ) {
262
+ uint32_t pickle_version;
263
+ pos = olm::unpickle(pos, end, pickle_version);
264
+
265
+ switch (pickle_version) {
266
+ case 1:
267
+ break;
268
+
269
+ default:
270
+ value.last_error = OlmErrorCode::OLM_UNKNOWN_PICKLE_VERSION;
271
+ return end;
272
+ }
273
+
274
+ pos = olm::unpickle(pos, end, value.key_pair);
275
+ return pos;
276
+ }
277
+ }
278
+
279
+ size_t olm_pickle_pk_decryption_length(
280
+ OlmPkDecryption * decryption
281
+ ) {
282
+ return _olm_enc_output_length(pickle_length(*decryption));
283
+ }
284
+
285
+ size_t olm_pickle_pk_decryption(
286
+ OlmPkDecryption * decryption,
287
+ void const * key, size_t key_length,
288
+ void *pickled, size_t pickled_length
289
+ ) {
290
+ OlmPkDecryption & object = *decryption;
291
+ std::size_t raw_length = pickle_length(object);
292
+ if (pickled_length < _olm_enc_output_length(raw_length)) {
293
+ object.last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
294
+ return std::size_t(-1);
295
+ }
296
+ pickle(_olm_enc_output_pos(reinterpret_cast<std::uint8_t *>(pickled), raw_length), object);
297
+ return _olm_enc_output(
298
+ reinterpret_cast<std::uint8_t const *>(key), key_length,
299
+ reinterpret_cast<std::uint8_t *>(pickled), raw_length
300
+ );
301
+ }
302
+
303
+ size_t olm_unpickle_pk_decryption(
304
+ OlmPkDecryption * decryption,
305
+ void const * key, size_t key_length,
306
+ void *pickled, size_t pickled_length,
307
+ void *pubkey, size_t pubkey_length
308
+ ) {
309
+ OlmPkDecryption & object = *decryption;
310
+ if (pubkey != NULL && pubkey_length < olm_pk_key_length()) {
311
+ object.last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
312
+ return std::size_t(-1);
313
+ }
314
+ std::uint8_t * const pos = reinterpret_cast<std::uint8_t *>(pickled);
315
+ std::size_t raw_length = _olm_enc_input(
316
+ reinterpret_cast<std::uint8_t const *>(key), key_length,
317
+ pos, pickled_length, &object.last_error
318
+ );
319
+ if (raw_length == std::size_t(-1)) {
320
+ return std::size_t(-1);
321
+ }
322
+ std::uint8_t * const end = pos + raw_length;
323
+ /* On success unpickle will return (pos + raw_length). If unpickling
324
+ * terminates too soon then it will return a pointer before
325
+ * (pos + raw_length). On error unpickle will return (pos + raw_length + 1).
326
+ */
327
+ if (end != unpickle(pos, end + 1, object)) {
328
+ if (object.last_error == OlmErrorCode::OLM_SUCCESS) {
329
+ object.last_error = OlmErrorCode::OLM_CORRUPTED_PICKLE;
330
+ }
331
+ return std::size_t(-1);
332
+ }
333
+ if (pubkey != NULL) {
334
+ olm::encode_base64(
335
+ (const uint8_t *)object.key_pair.public_key.public_key,
336
+ CURVE25519_KEY_LENGTH,
337
+ (uint8_t *)pubkey
338
+ );
339
+ }
340
+ return pickled_length;
341
+ }
342
+
343
+ size_t olm_pk_max_plaintext_length(
344
+ OlmPkDecryption * decryption,
345
+ size_t ciphertext_length
346
+ ) {
347
+ return _olm_cipher_aes_sha_256_ops.decrypt_max_plaintext_length(
348
+ olm_pk_cipher, olm::decode_base64_length(ciphertext_length)
349
+ );
350
+ }
351
+
352
+ size_t olm_pk_decrypt(
353
+ OlmPkDecryption * decryption,
354
+ void const * ephemeral_key, size_t ephemeral_key_length,
355
+ void const * mac, size_t mac_length,
356
+ void * ciphertext, size_t ciphertext_length,
357
+ void * plaintext, size_t max_plaintext_length
358
+ ) {
359
+ if (max_plaintext_length
360
+ < olm_pk_max_plaintext_length(decryption, ciphertext_length)) {
361
+ decryption->last_error =
362
+ OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
363
+ return std::size_t(-1);
364
+ }
365
+
366
+ struct _olm_curve25519_public_key ephemeral;
367
+ olm::decode_base64(
368
+ (const uint8_t*)ephemeral_key, ephemeral_key_length,
369
+ (uint8_t *)ephemeral.public_key
370
+ );
371
+ olm::SharedKey secret;
372
+ _olm_crypto_curve25519_shared_secret(&decryption->key_pair, &ephemeral, secret);
373
+ uint8_t raw_mac[MAC_LENGTH];
374
+ olm::decode_base64((const uint8_t*)mac, olm::encode_base64_length(MAC_LENGTH), raw_mac);
375
+ size_t raw_ciphertext_length = olm::decode_base64_length(ciphertext_length);
376
+ olm::decode_base64((const uint8_t *)ciphertext, ciphertext_length, (uint8_t *)ciphertext);
377
+ size_t result = _olm_cipher_aes_sha_256_ops.decrypt(
378
+ olm_pk_cipher,
379
+ secret, sizeof(secret),
380
+ (uint8_t *) raw_mac, MAC_LENGTH,
381
+ (const uint8_t *) ciphertext, raw_ciphertext_length,
382
+ (uint8_t *) plaintext, max_plaintext_length
383
+ );
384
+ if (result == std::size_t(-1)) {
385
+ // we already checked the buffer sizes, so the only error that decrypt
386
+ // will return is if the MAC is incorrect
387
+ decryption->last_error =
388
+ OlmErrorCode::OLM_BAD_MESSAGE_MAC;
389
+ return std::size_t(-1);
390
+ } else {
391
+ return result;
392
+ }
393
+ }
394
+
395
+ size_t olm_pk_get_private_key(
396
+ OlmPkDecryption * decryption,
397
+ void *private_key, size_t private_key_length
398
+ ) {
399
+ if (private_key_length < olm_pk_private_key_length()) {
400
+ decryption->last_error =
401
+ OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
402
+ return std::size_t(-1);
403
+ }
404
+ std::memcpy(
405
+ private_key,
406
+ decryption->key_pair.private_key.private_key,
407
+ olm_pk_private_key_length()
408
+ );
409
+ return olm_pk_private_key_length();
410
+ }
411
+
412
+ }