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,12 @@
1
+ #ifndef SC_H
2
+ #define SC_H
3
+
4
+ /*
5
+ The set of scalars is \Z/l
6
+ where l = 2^252 + 27742317777372353535851937790883648493.
7
+ */
8
+
9
+ void sc_reduce(unsigned char *s);
10
+ void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, const unsigned char *c);
11
+
12
+ #endif
@@ -0,0 +1,40 @@
1
+ #include "ed25519.h"
2
+
3
+ #ifndef ED25519_NO_SEED
4
+
5
+ #ifdef _WIN32
6
+ #include <Windows.h>
7
+ #include <Wincrypt.h>
8
+ #else
9
+ #include <stdio.h>
10
+ #endif
11
+
12
+ int ed25519_create_seed(unsigned char *seed) {
13
+ #ifdef _WIN32
14
+ HCRYPTPROV prov;
15
+
16
+ if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
17
+ return 1;
18
+ }
19
+
20
+ if (!CryptGenRandom(prov, 32, seed)) {
21
+ CryptReleaseContext(prov, 0);
22
+ return 1;
23
+ }
24
+
25
+ CryptReleaseContext(prov, 0);
26
+ #else
27
+ FILE *f = fopen("/dev/urandom", "rb");
28
+
29
+ if (f == NULL) {
30
+ return 1;
31
+ }
32
+
33
+ fread(seed, 1, 32, f);
34
+ fclose(f);
35
+ #endif
36
+
37
+ return 0;
38
+ }
39
+
40
+ #endif
@@ -0,0 +1,275 @@
1
+ /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2
+ *
3
+ * LibTomCrypt is a library that provides various cryptographic
4
+ * algorithms in a highly modular and flexible manner.
5
+ *
6
+ * The library is free for all purposes without any express
7
+ * guarantee it works.
8
+ *
9
+ * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
10
+ */
11
+
12
+ #include "fixedint.h"
13
+ #include "sha512.h"
14
+
15
+ /* the K array */
16
+ static const uint64_t K[80] = {
17
+ UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd),
18
+ UINT64_C(0xb5c0fbcfec4d3b2f), UINT64_C(0xe9b5dba58189dbbc),
19
+ UINT64_C(0x3956c25bf348b538), UINT64_C(0x59f111f1b605d019),
20
+ UINT64_C(0x923f82a4af194f9b), UINT64_C(0xab1c5ed5da6d8118),
21
+ UINT64_C(0xd807aa98a3030242), UINT64_C(0x12835b0145706fbe),
22
+ UINT64_C(0x243185be4ee4b28c), UINT64_C(0x550c7dc3d5ffb4e2),
23
+ UINT64_C(0x72be5d74f27b896f), UINT64_C(0x80deb1fe3b1696b1),
24
+ UINT64_C(0x9bdc06a725c71235), UINT64_C(0xc19bf174cf692694),
25
+ UINT64_C(0xe49b69c19ef14ad2), UINT64_C(0xefbe4786384f25e3),
26
+ UINT64_C(0x0fc19dc68b8cd5b5), UINT64_C(0x240ca1cc77ac9c65),
27
+ UINT64_C(0x2de92c6f592b0275), UINT64_C(0x4a7484aa6ea6e483),
28
+ UINT64_C(0x5cb0a9dcbd41fbd4), UINT64_C(0x76f988da831153b5),
29
+ UINT64_C(0x983e5152ee66dfab), UINT64_C(0xa831c66d2db43210),
30
+ UINT64_C(0xb00327c898fb213f), UINT64_C(0xbf597fc7beef0ee4),
31
+ UINT64_C(0xc6e00bf33da88fc2), UINT64_C(0xd5a79147930aa725),
32
+ UINT64_C(0x06ca6351e003826f), UINT64_C(0x142929670a0e6e70),
33
+ UINT64_C(0x27b70a8546d22ffc), UINT64_C(0x2e1b21385c26c926),
34
+ UINT64_C(0x4d2c6dfc5ac42aed), UINT64_C(0x53380d139d95b3df),
35
+ UINT64_C(0x650a73548baf63de), UINT64_C(0x766a0abb3c77b2a8),
36
+ UINT64_C(0x81c2c92e47edaee6), UINT64_C(0x92722c851482353b),
37
+ UINT64_C(0xa2bfe8a14cf10364), UINT64_C(0xa81a664bbc423001),
38
+ UINT64_C(0xc24b8b70d0f89791), UINT64_C(0xc76c51a30654be30),
39
+ UINT64_C(0xd192e819d6ef5218), UINT64_C(0xd69906245565a910),
40
+ UINT64_C(0xf40e35855771202a), UINT64_C(0x106aa07032bbd1b8),
41
+ UINT64_C(0x19a4c116b8d2d0c8), UINT64_C(0x1e376c085141ab53),
42
+ UINT64_C(0x2748774cdf8eeb99), UINT64_C(0x34b0bcb5e19b48a8),
43
+ UINT64_C(0x391c0cb3c5c95a63), UINT64_C(0x4ed8aa4ae3418acb),
44
+ UINT64_C(0x5b9cca4f7763e373), UINT64_C(0x682e6ff3d6b2b8a3),
45
+ UINT64_C(0x748f82ee5defb2fc), UINT64_C(0x78a5636f43172f60),
46
+ UINT64_C(0x84c87814a1f0ab72), UINT64_C(0x8cc702081a6439ec),
47
+ UINT64_C(0x90befffa23631e28), UINT64_C(0xa4506cebde82bde9),
48
+ UINT64_C(0xbef9a3f7b2c67915), UINT64_C(0xc67178f2e372532b),
49
+ UINT64_C(0xca273eceea26619c), UINT64_C(0xd186b8c721c0c207),
50
+ UINT64_C(0xeada7dd6cde0eb1e), UINT64_C(0xf57d4f7fee6ed178),
51
+ UINT64_C(0x06f067aa72176fba), UINT64_C(0x0a637dc5a2c898a6),
52
+ UINT64_C(0x113f9804bef90dae), UINT64_C(0x1b710b35131c471b),
53
+ UINT64_C(0x28db77f523047d84), UINT64_C(0x32caab7b40c72493),
54
+ UINT64_C(0x3c9ebe0a15c9bebc), UINT64_C(0x431d67c49c100d4c),
55
+ UINT64_C(0x4cc5d4becb3e42b6), UINT64_C(0x597f299cfc657e2a),
56
+ UINT64_C(0x5fcb6fab3ad6faec), UINT64_C(0x6c44198c4a475817)
57
+ };
58
+
59
+ /* Various logical functions */
60
+
61
+ #define ROR64c(x, y) \
62
+ ( ((((x)&UINT64_C(0xFFFFFFFFFFFFFFFF))>>((uint64_t)(y)&UINT64_C(63))) | \
63
+ ((x)<<((uint64_t)(64-((y)&UINT64_C(63)))))) & UINT64_C(0xFFFFFFFFFFFFFFFF))
64
+
65
+ #define STORE64H(x, y) \
66
+ { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
67
+ (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
68
+ (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
69
+ (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
70
+
71
+ #define LOAD64H(x, y) \
72
+ { x = (((uint64_t)((y)[0] & 255))<<56)|(((uint64_t)((y)[1] & 255))<<48) | \
73
+ (((uint64_t)((y)[2] & 255))<<40)|(((uint64_t)((y)[3] & 255))<<32) | \
74
+ (((uint64_t)((y)[4] & 255))<<24)|(((uint64_t)((y)[5] & 255))<<16) | \
75
+ (((uint64_t)((y)[6] & 255))<<8)|(((uint64_t)((y)[7] & 255))); }
76
+
77
+
78
+ #define Ch(x,y,z) (z ^ (x & (y ^ z)))
79
+ #define Maj(x,y,z) (((x | y) & z) | (x & y))
80
+ #define S(x, n) ROR64c(x, n)
81
+ #define R(x, n) (((x) &UINT64_C(0xFFFFFFFFFFFFFFFF))>>((uint64_t)n))
82
+ #define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
83
+ #define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
84
+ #define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
85
+ #define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
86
+ #ifndef MIN
87
+ #define MIN(x, y) ( ((x)<(y))?(x):(y) )
88
+ #endif
89
+
90
+ /* compress 1024-bits */
91
+ static int sha512_compress(sha512_context *md, unsigned char *buf)
92
+ {
93
+ uint64_t S[8], W[80], t0, t1;
94
+ int i;
95
+
96
+ /* copy state into S */
97
+ for (i = 0; i < 8; i++) {
98
+ S[i] = md->state[i];
99
+ }
100
+
101
+ /* copy the state into 1024-bits into W[0..15] */
102
+ for (i = 0; i < 16; i++) {
103
+ LOAD64H(W[i], buf + (8*i));
104
+ }
105
+
106
+ /* fill W[16..79] */
107
+ for (i = 16; i < 80; i++) {
108
+ W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
109
+ }
110
+
111
+ /* Compress */
112
+ #define RND(a,b,c,d,e,f,g,h,i) \
113
+ t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
114
+ t1 = Sigma0(a) + Maj(a, b, c);\
115
+ d += t0; \
116
+ h = t0 + t1;
117
+
118
+ for (i = 0; i < 80; i += 8) {
119
+ RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0);
120
+ RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1);
121
+ RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2);
122
+ RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3);
123
+ RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4);
124
+ RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5);
125
+ RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6);
126
+ RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7);
127
+ }
128
+
129
+ #undef RND
130
+
131
+
132
+
133
+ /* feedback */
134
+ for (i = 0; i < 8; i++) {
135
+ md->state[i] = md->state[i] + S[i];
136
+ }
137
+
138
+ return 0;
139
+ }
140
+
141
+
142
+ /**
143
+ Initialize the hash state
144
+ @param md The hash state you wish to initialize
145
+ @return 0 if successful
146
+ */
147
+ int sha512_init(sha512_context * md) {
148
+ if (md == NULL) return 1;
149
+
150
+ md->curlen = 0;
151
+ md->length = 0;
152
+ md->state[0] = UINT64_C(0x6a09e667f3bcc908);
153
+ md->state[1] = UINT64_C(0xbb67ae8584caa73b);
154
+ md->state[2] = UINT64_C(0x3c6ef372fe94f82b);
155
+ md->state[3] = UINT64_C(0xa54ff53a5f1d36f1);
156
+ md->state[4] = UINT64_C(0x510e527fade682d1);
157
+ md->state[5] = UINT64_C(0x9b05688c2b3e6c1f);
158
+ md->state[6] = UINT64_C(0x1f83d9abfb41bd6b);
159
+ md->state[7] = UINT64_C(0x5be0cd19137e2179);
160
+
161
+ return 0;
162
+ }
163
+
164
+ /**
165
+ Process a block of memory though the hash
166
+ @param md The hash state
167
+ @param in The data to hash
168
+ @param inlen The length of the data (octets)
169
+ @return 0 if successful
170
+ */
171
+ int sha512_update (sha512_context * md, const unsigned char *in, size_t inlen)
172
+ {
173
+ size_t n;
174
+ size_t i;
175
+ int err;
176
+ if (md == NULL) return 1;
177
+ if (in == NULL) return 1;
178
+ if (md->curlen > sizeof(md->buf)) {
179
+ return 1;
180
+ }
181
+ while (inlen > 0) {
182
+ if (md->curlen == 0 && inlen >= 128) {
183
+ if ((err = sha512_compress (md, (unsigned char *)in)) != 0) {
184
+ return err;
185
+ }
186
+ md->length += 128 * 8;
187
+ in += 128;
188
+ inlen -= 128;
189
+ } else {
190
+ n = MIN(inlen, (128 - md->curlen));
191
+
192
+ for (i = 0; i < n; i++) {
193
+ md->buf[i + md->curlen] = in[i];
194
+ }
195
+
196
+
197
+ md->curlen += n;
198
+ in += n;
199
+ inlen -= n;
200
+ if (md->curlen == 128) {
201
+ if ((err = sha512_compress (md, md->buf)) != 0) {
202
+ return err;
203
+ }
204
+ md->length += 8*128;
205
+ md->curlen = 0;
206
+ }
207
+ }
208
+ }
209
+ return 0;
210
+ }
211
+
212
+ /**
213
+ Terminate the hash to get the digest
214
+ @param md The hash state
215
+ @param out [out] The destination of the hash (64 bytes)
216
+ @return 0 if successful
217
+ */
218
+ int sha512_final(sha512_context * md, unsigned char *out)
219
+ {
220
+ int i;
221
+
222
+ if (md == NULL) return 1;
223
+ if (out == NULL) return 1;
224
+
225
+ if (md->curlen >= sizeof(md->buf)) {
226
+ return 1;
227
+ }
228
+
229
+ /* increase the length of the message */
230
+ md->length += md->curlen * UINT64_C(8);
231
+
232
+ /* append the '1' bit */
233
+ md->buf[md->curlen++] = (unsigned char)0x80;
234
+
235
+ /* if the length is currently above 112 bytes we append zeros
236
+ * then compress. Then we can fall back to padding zeros and length
237
+ * encoding like normal.
238
+ */
239
+ if (md->curlen > 112) {
240
+ while (md->curlen < 128) {
241
+ md->buf[md->curlen++] = (unsigned char)0;
242
+ }
243
+ sha512_compress(md, md->buf);
244
+ md->curlen = 0;
245
+ }
246
+
247
+ /* pad upto 120 bytes of zeroes
248
+ * note: that from 112 to 120 is the 64 MSB of the length. We assume that you won't hash
249
+ * > 2^64 bits of data... :-)
250
+ */
251
+ while (md->curlen < 120) {
252
+ md->buf[md->curlen++] = (unsigned char)0;
253
+ }
254
+
255
+ /* store length */
256
+ STORE64H(md->length, md->buf+120);
257
+ sha512_compress(md, md->buf);
258
+
259
+ /* copy output */
260
+ for (i = 0; i < 8; i++) {
261
+ STORE64H(md->state[i], out+(8*i));
262
+ }
263
+
264
+ return 0;
265
+ }
266
+
267
+ int sha512(const unsigned char *message, size_t message_len, unsigned char *out)
268
+ {
269
+ sha512_context ctx;
270
+ int ret;
271
+ if ((ret = sha512_init(&ctx))) return ret;
272
+ if ((ret = sha512_update(&ctx, message, message_len))) return ret;
273
+ if ((ret = sha512_final(&ctx, out))) return ret;
274
+ return 0;
275
+ }
@@ -0,0 +1,21 @@
1
+ #ifndef SHA512_H
2
+ #define SHA512_H
3
+
4
+ #include <stddef.h>
5
+
6
+ #include "fixedint.h"
7
+
8
+ /* state */
9
+ typedef struct sha512_context_ {
10
+ uint64_t length, state[8];
11
+ size_t curlen;
12
+ unsigned char buf[128];
13
+ } sha512_context;
14
+
15
+
16
+ int sha512_init(sha512_context * md);
17
+ int sha512_final(sha512_context * md, unsigned char *out);
18
+ int sha512_update(sha512_context * md, const unsigned char *in, size_t inlen);
19
+ int sha512(const unsigned char *message, size_t message_len, unsigned char *out);
20
+
21
+ #endif
@@ -0,0 +1,31 @@
1
+ #include "ed25519.h"
2
+ #include "sha512.h"
3
+ #include "ge.h"
4
+ #include "sc.h"
5
+
6
+
7
+ void ed25519_sign(unsigned char *signature, const unsigned char *message, size_t message_len, const unsigned char *public_key, const unsigned char *private_key) {
8
+ sha512_context hash;
9
+ unsigned char hram[64];
10
+ unsigned char r[64];
11
+ ge_p3 R;
12
+
13
+
14
+ sha512_init(&hash);
15
+ sha512_update(&hash, private_key + 32, 32);
16
+ sha512_update(&hash, message, message_len);
17
+ sha512_final(&hash, r);
18
+
19
+ sc_reduce(r);
20
+ ge_scalarmult_base(&R, r);
21
+ ge_p3_tobytes(signature, &R);
22
+
23
+ sha512_init(&hash);
24
+ sha512_update(&hash, signature, 32);
25
+ sha512_update(&hash, public_key, 32);
26
+ sha512_update(&hash, message, message_len);
27
+ sha512_final(&hash, hram);
28
+
29
+ sc_reduce(hram);
30
+ sc_muladd(signature + 32, hram, private_key, r);
31
+ }
@@ -0,0 +1,77 @@
1
+ #include "ed25519.h"
2
+ #include "sha512.h"
3
+ #include "ge.h"
4
+ #include "sc.h"
5
+
6
+ static int consttime_equal(const unsigned char *x, const unsigned char *y) {
7
+ unsigned char r = 0;
8
+
9
+ r = x[0] ^ y[0];
10
+ #define F(i) r |= x[i] ^ y[i]
11
+ F(1);
12
+ F(2);
13
+ F(3);
14
+ F(4);
15
+ F(5);
16
+ F(6);
17
+ F(7);
18
+ F(8);
19
+ F(9);
20
+ F(10);
21
+ F(11);
22
+ F(12);
23
+ F(13);
24
+ F(14);
25
+ F(15);
26
+ F(16);
27
+ F(17);
28
+ F(18);
29
+ F(19);
30
+ F(20);
31
+ F(21);
32
+ F(22);
33
+ F(23);
34
+ F(24);
35
+ F(25);
36
+ F(26);
37
+ F(27);
38
+ F(28);
39
+ F(29);
40
+ F(30);
41
+ F(31);
42
+ #undef F
43
+
44
+ return !r;
45
+ }
46
+
47
+ int ed25519_verify(const unsigned char *signature, const unsigned char *message, size_t message_len, const unsigned char *public_key) {
48
+ unsigned char h[64];
49
+ unsigned char checker[32];
50
+ sha512_context hash;
51
+ ge_p3 A;
52
+ ge_p2 R;
53
+
54
+ if (signature[63] & 224) {
55
+ return 0;
56
+ }
57
+
58
+ if (ge_frombytes_negate_vartime(&A, public_key) != 0) {
59
+ return 0;
60
+ }
61
+
62
+ sha512_init(&hash);
63
+ sha512_update(&hash, signature, 32);
64
+ sha512_update(&hash, public_key, 32);
65
+ sha512_update(&hash, message, message_len);
66
+ sha512_final(&hash, h);
67
+
68
+ sc_reduce(h);
69
+ ge_double_scalarmult_vartime(&R, h, &A, signature + 32);
70
+ ge_tobytes(checker, &R);
71
+
72
+ if (!consttime_equal(checker, signature)) {
73
+ return 0;
74
+ }
75
+
76
+ return 1;
77
+ }
@@ -0,0 +1,150 @@
1
+ #include <stdlib.h>
2
+ #include <stdio.h>
3
+ #include <string.h>
4
+ #include <time.h>
5
+
6
+ /* #define ED25519_DLL */
7
+ #include "src/ed25519.h"
8
+
9
+ #include "src/ge.h"
10
+ #include "src/sc.h"
11
+
12
+
13
+ int main() {
14
+ unsigned char public_key[32], private_key[64], seed[32], scalar[32];
15
+ unsigned char other_public_key[32], other_private_key[64];
16
+ unsigned char shared_secret[32], other_shared_secret[32];
17
+ unsigned char signature[64];
18
+
19
+ clock_t start;
20
+ clock_t end;
21
+ int i;
22
+
23
+ const unsigned char message[] = "Hello, world!";
24
+ const int message_len = strlen((char*) message);
25
+
26
+ /* create a random seed, and a keypair out of that seed */
27
+ ed25519_create_seed(seed);
28
+ ed25519_create_keypair(public_key, private_key, seed);
29
+
30
+ /* create signature on the message with the keypair */
31
+ ed25519_sign(signature, message, message_len, public_key, private_key);
32
+
33
+ /* verify the signature */
34
+ if (ed25519_verify(signature, message, message_len, public_key)) {
35
+ printf("valid signature\n");
36
+ } else {
37
+ printf("invalid signature\n");
38
+ }
39
+
40
+ /* create scalar and add it to the keypair */
41
+ ed25519_create_seed(scalar);
42
+ ed25519_add_scalar(public_key, private_key, scalar);
43
+
44
+ /* create signature with the new keypair */
45
+ ed25519_sign(signature, message, message_len, public_key, private_key);
46
+
47
+ /* verify the signature with the new keypair */
48
+ if (ed25519_verify(signature, message, message_len, public_key)) {
49
+ printf("valid signature\n");
50
+ } else {
51
+ printf("invalid signature\n");
52
+ }
53
+
54
+ /* make a slight adjustment and verify again */
55
+ signature[44] ^= 0x10;
56
+ if (ed25519_verify(signature, message, message_len, public_key)) {
57
+ printf("did not detect signature change\n");
58
+ } else {
59
+ printf("correctly detected signature change\n");
60
+ }
61
+
62
+ /* generate two keypairs for testing key exchange */
63
+ ed25519_create_seed(seed);
64
+ ed25519_create_keypair(public_key, private_key, seed);
65
+ ed25519_create_seed(seed);
66
+ ed25519_create_keypair(other_public_key, other_private_key, seed);
67
+
68
+ /* create two shared secrets - from both perspectives - and check if they're equal */
69
+ ed25519_key_exchange(shared_secret, other_public_key, private_key);
70
+ ed25519_key_exchange(other_shared_secret, public_key, other_private_key);
71
+
72
+ for (i = 0; i < 32; ++i) {
73
+ if (shared_secret[i] != other_shared_secret[i]) {
74
+ printf("key exchange was incorrect\n");
75
+ break;
76
+ }
77
+ }
78
+
79
+ if (i == 32) {
80
+ printf("key exchange was correct\n");
81
+ }
82
+
83
+ /* test performance */
84
+ printf("testing seed generation performance: ");
85
+ start = clock();
86
+ for (i = 0; i < 10000; ++i) {
87
+ ed25519_create_seed(seed);
88
+ }
89
+ end = clock();
90
+
91
+ printf("%fus per seed\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
92
+
93
+
94
+ printf("testing key generation performance: ");
95
+ start = clock();
96
+ for (i = 0; i < 10000; ++i) {
97
+ ed25519_create_keypair(public_key, private_key, seed);
98
+ }
99
+ end = clock();
100
+
101
+ printf("%fus per keypair\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
102
+
103
+ printf("testing sign performance: ");
104
+ start = clock();
105
+ for (i = 0; i < 10000; ++i) {
106
+ ed25519_sign(signature, message, message_len, public_key, private_key);
107
+ }
108
+ end = clock();
109
+
110
+ printf("%fus per signature\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
111
+
112
+ printf("testing verify performance: ");
113
+ start = clock();
114
+ for (i = 0; i < 10000; ++i) {
115
+ ed25519_verify(signature, message, message_len, public_key);
116
+ }
117
+ end = clock();
118
+
119
+ printf("%fus per signature\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
120
+
121
+
122
+ printf("testing keypair scalar addition performance: ");
123
+ start = clock();
124
+ for (i = 0; i < 10000; ++i) {
125
+ ed25519_add_scalar(public_key, private_key, scalar);
126
+ }
127
+ end = clock();
128
+
129
+ printf("%fus per keypair\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
130
+
131
+ printf("testing public key scalar addition performance: ");
132
+ start = clock();
133
+ for (i = 0; i < 10000; ++i) {
134
+ ed25519_add_scalar(public_key, NULL, scalar);
135
+ }
136
+ end = clock();
137
+
138
+ printf("%fus per key\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
139
+
140
+ printf("testing key exchange performance: ");
141
+ start = clock();
142
+ for (i = 0; i < 10000; ++i) {
143
+ ed25519_key_exchange(shared_secret, other_public_key, private_key);
144
+ }
145
+ end = clock();
146
+
147
+ printf("%fus per shared secret\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
148
+
149
+ return 0;
150
+ }