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,159 @@
1
+ /*********************************************************************
2
+ * Filename: sha256.c
3
+ * Author: Brad Conte (brad AT bradconte.com)
4
+ * Copyright:
5
+ * Disclaimer: This code is presented "as is" without any guarantees.
6
+ * Details: Implementation of the SHA-256 hashing algorithm.
7
+ SHA-256 is one of the three algorithms in the SHA2
8
+ specification. The others, SHA-384 and SHA-512, are not
9
+ offered in this implementation.
10
+ Algorithm specification can be found here:
11
+ * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
12
+ This implementation uses little endian byte order.
13
+ *********************************************************************/
14
+
15
+ /*************************** HEADER FILES ***************************/
16
+ #include <stdlib.h>
17
+ #include <memory.h>
18
+ #include <string.h>
19
+ #include "sha256.h"
20
+
21
+ /****************************** MACROS ******************************/
22
+ #define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
23
+ #define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
24
+
25
+ #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
26
+ #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
27
+ #define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
28
+ #define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
29
+ #define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
30
+ #define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
31
+
32
+ /**************************** VARIABLES *****************************/
33
+ static const WORD k[64] = {
34
+ 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
35
+ 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
36
+ 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
37
+ 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
38
+ 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
39
+ 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
40
+ 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
41
+ 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
42
+ };
43
+
44
+ /*********************** FUNCTION DEFINITIONS ***********************/
45
+ void sha256_transform(SHA256_CTX *ctx, const BYTE data[])
46
+ {
47
+ WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
48
+
49
+ for (i = 0, j = 0; i < 16; ++i, j += 4)
50
+ m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
51
+ for ( ; i < 64; ++i)
52
+ m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
53
+
54
+ a = ctx->state[0];
55
+ b = ctx->state[1];
56
+ c = ctx->state[2];
57
+ d = ctx->state[3];
58
+ e = ctx->state[4];
59
+ f = ctx->state[5];
60
+ g = ctx->state[6];
61
+ h = ctx->state[7];
62
+
63
+ for (i = 0; i < 64; ++i) {
64
+ t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
65
+ t2 = EP0(a) + MAJ(a,b,c);
66
+ h = g;
67
+ g = f;
68
+ f = e;
69
+ e = d + t1;
70
+ d = c;
71
+ c = b;
72
+ b = a;
73
+ a = t1 + t2;
74
+ }
75
+
76
+ ctx->state[0] += a;
77
+ ctx->state[1] += b;
78
+ ctx->state[2] += c;
79
+ ctx->state[3] += d;
80
+ ctx->state[4] += e;
81
+ ctx->state[5] += f;
82
+ ctx->state[6] += g;
83
+ ctx->state[7] += h;
84
+ }
85
+
86
+ void sha256_init(SHA256_CTX *ctx)
87
+ {
88
+ ctx->datalen = 0;
89
+ ctx->bitlen = 0;
90
+ ctx->state[0] = 0x6a09e667;
91
+ ctx->state[1] = 0xbb67ae85;
92
+ ctx->state[2] = 0x3c6ef372;
93
+ ctx->state[3] = 0xa54ff53a;
94
+ ctx->state[4] = 0x510e527f;
95
+ ctx->state[5] = 0x9b05688c;
96
+ ctx->state[6] = 0x1f83d9ab;
97
+ ctx->state[7] = 0x5be0cd19;
98
+ }
99
+
100
+ void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)
101
+ {
102
+ WORD i;
103
+
104
+ for (i = 0; i < len; ++i) {
105
+ ctx->data[ctx->datalen] = data[i];
106
+ ctx->datalen++;
107
+ if (ctx->datalen == 64) {
108
+ sha256_transform(ctx, ctx->data);
109
+ ctx->bitlen += 512;
110
+ ctx->datalen = 0;
111
+ }
112
+ }
113
+ }
114
+
115
+ void sha256_final(SHA256_CTX *ctx, BYTE hash[])
116
+ {
117
+ WORD i;
118
+
119
+ i = ctx->datalen;
120
+
121
+ // Pad whatever data is left in the buffer.
122
+ if (ctx->datalen < 56) {
123
+ ctx->data[i++] = 0x80;
124
+ while (i < 56)
125
+ ctx->data[i++] = 0x00;
126
+ }
127
+ else {
128
+ ctx->data[i++] = 0x80;
129
+ while (i < 64)
130
+ ctx->data[i++] = 0x00;
131
+ sha256_transform(ctx, ctx->data);
132
+ memset(ctx->data, 0, 56);
133
+ }
134
+
135
+ // Append to the padding the total message's length in bits and transform.
136
+ ctx->bitlen += ctx->datalen * 8;
137
+ ctx->data[63] = ctx->bitlen;
138
+ ctx->data[62] = ctx->bitlen >> 8;
139
+ ctx->data[61] = ctx->bitlen >> 16;
140
+ ctx->data[60] = ctx->bitlen >> 24;
141
+ ctx->data[59] = ctx->bitlen >> 32;
142
+ ctx->data[58] = ctx->bitlen >> 40;
143
+ ctx->data[57] = ctx->bitlen >> 48;
144
+ ctx->data[56] = ctx->bitlen >> 56;
145
+ sha256_transform(ctx, ctx->data);
146
+
147
+ // Since this implementation uses little endian byte ordering and SHA uses big endian,
148
+ // reverse all the bytes when copying the final state to the output hash.
149
+ for (i = 0; i < 4; ++i) {
150
+ hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
151
+ hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
152
+ hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
153
+ hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
154
+ hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
155
+ hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
156
+ hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
157
+ hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
158
+ }
159
+ }
@@ -0,0 +1,34 @@
1
+ /*********************************************************************
2
+ * Filename: sha256.h
3
+ * Author: Brad Conte (brad AT bradconte.com)
4
+ * Copyright:
5
+ * Disclaimer: This code is presented "as is" without any guarantees.
6
+ * Details: Defines the API for the corresponding SHA1 implementation.
7
+ *********************************************************************/
8
+
9
+ #ifndef SHA256_H
10
+ #define SHA256_H
11
+
12
+ /*************************** HEADER FILES ***************************/
13
+ #include <stddef.h>
14
+
15
+ /****************************** MACROS ******************************/
16
+ #define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest
17
+
18
+ /**************************** DATA TYPES ****************************/
19
+ typedef unsigned char BYTE; // 8-bit byte
20
+ typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
21
+
22
+ typedef struct {
23
+ BYTE data[64];
24
+ WORD datalen;
25
+ unsigned long long bitlen;
26
+ WORD state[8];
27
+ } SHA256_CTX;
28
+
29
+ /*********************** FUNCTION DECLARATIONS **********************/
30
+ void sha256_init(SHA256_CTX *ctx);
31
+ void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len);
32
+ void sha256_final(SHA256_CTX *ctx, BYTE hash[]);
33
+
34
+ #endif // SHA256_H
@@ -0,0 +1,61 @@
1
+ /*********************************************************************
2
+ * Filename: sha256.c
3
+ * Author: Brad Conte (brad AT bradconte.com)
4
+ * Copyright:
5
+ * Disclaimer: This code is presented "as is" without any guarantees.
6
+ * Details: Performs known-answer tests on the corresponding SHA1
7
+ implementation. These tests do not encompass the full
8
+ range of available test vectors, however, if the tests
9
+ pass it is very, very likely that the code is correct
10
+ and was compiled properly. This code also serves as
11
+ example usage of the functions.
12
+ *********************************************************************/
13
+
14
+ /*************************** HEADER FILES ***************************/
15
+ #include <stdio.h>
16
+ #include <memory.h>
17
+ #include <string.h>
18
+ #include "sha256.h"
19
+
20
+ /*********************** FUNCTION DEFINITIONS ***********************/
21
+ int sha256_test()
22
+ {
23
+ BYTE text1[] = {"abc"};
24
+ BYTE text2[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"};
25
+ BYTE text3[] = {"aaaaaaaaaa"};
26
+ BYTE hash1[SHA256_BLOCK_SIZE] = {0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea,0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23,
27
+ 0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c,0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad};
28
+ BYTE hash2[SHA256_BLOCK_SIZE] = {0x24,0x8d,0x6a,0x61,0xd2,0x06,0x38,0xb8,0xe5,0xc0,0x26,0x93,0x0c,0x3e,0x60,0x39,
29
+ 0xa3,0x3c,0xe4,0x59,0x64,0xff,0x21,0x67,0xf6,0xec,0xed,0xd4,0x19,0xdb,0x06,0xc1};
30
+ BYTE hash3[SHA256_BLOCK_SIZE] = {0xcd,0xc7,0x6e,0x5c,0x99,0x14,0xfb,0x92,0x81,0xa1,0xc7,0xe2,0x84,0xd7,0x3e,0x67,
31
+ 0xf1,0x80,0x9a,0x48,0xa4,0x97,0x20,0x0e,0x04,0x6d,0x39,0xcc,0xc7,0x11,0x2c,0xd0};
32
+ BYTE buf[SHA256_BLOCK_SIZE];
33
+ SHA256_CTX ctx;
34
+ int idx;
35
+ int pass = 1;
36
+
37
+ sha256_init(&ctx);
38
+ sha256_update(&ctx, text1, strlen(text1));
39
+ sha256_final(&ctx, buf);
40
+ pass = pass && !memcmp(hash1, buf, SHA256_BLOCK_SIZE);
41
+
42
+ sha256_init(&ctx);
43
+ sha256_update(&ctx, text2, strlen(text2));
44
+ sha256_final(&ctx, buf);
45
+ pass = pass && !memcmp(hash2, buf, SHA256_BLOCK_SIZE);
46
+
47
+ sha256_init(&ctx);
48
+ for (idx = 0; idx < 100000; ++idx)
49
+ sha256_update(&ctx, text3, strlen(text3));
50
+ sha256_final(&ctx, buf);
51
+ pass = pass && !memcmp(hash3, buf, SHA256_BLOCK_SIZE);
52
+
53
+ return(pass);
54
+ }
55
+
56
+ int main()
57
+ {
58
+ printf("SHA-256 tests: %s\n", sha256_test() ? "SUCCEEDED" : "FAILEd");
59
+
60
+ return(0);
61
+ }
@@ -0,0 +1,118 @@
1
+ /*
2
+ James Robson
3
+ Public domain.
4
+ */
5
+
6
+ #include "Curve25519Donna.h"
7
+ #include <stdio.h>
8
+ #include <stdlib.h>
9
+
10
+ extern void curve25519_donna(unsigned char *output, const unsigned char *a,
11
+ const unsigned char *b);
12
+
13
+ unsigned char*
14
+ as_unsigned_char_array(JNIEnv* env, jbyteArray array, int* len);
15
+
16
+ jbyteArray as_byte_array(JNIEnv* env, unsigned char* buf, int len);
17
+
18
+
19
+ jbyteArray as_byte_array(JNIEnv* env, unsigned char* buf, int len) {
20
+ jbyteArray array = (*env)->NewByteArray(env, len);
21
+ (*env)->SetByteArrayRegion(env, array, 0, len, (jbyte*)buf);
22
+
23
+ //int i;
24
+ //for (i = 0;i < len;++i) printf("%02x",(unsigned int) buf[i]); printf(" ");
25
+ //printf("\n");
26
+
27
+ return array;
28
+ }
29
+
30
+ unsigned char*
31
+ as_unsigned_char_array(JNIEnv* env, jbyteArray array, int* len) {
32
+
33
+ *len = (*env)->GetArrayLength(env, array);
34
+ unsigned char* buf = (unsigned char*)calloc(*len+1, sizeof(char));
35
+ (*env)->GetByteArrayRegion (env, array, 0, *len, (jbyte*)buf);
36
+ return buf;
37
+
38
+ }
39
+
40
+ JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_curve25519Donna
41
+ (JNIEnv *env, jobject obj, jbyteArray a, jbyteArray b) {
42
+
43
+ unsigned char o[32] = {0};
44
+ int l1, l2;
45
+ unsigned char* a1 = as_unsigned_char_array(env, a, &l1);
46
+ unsigned char* b1 = as_unsigned_char_array(env, b, &l2);
47
+
48
+ if ( !(l1 == 32 && l2 == 32) ) {
49
+ fprintf(stderr, "Error, must be length 32");
50
+ return NULL;
51
+ }
52
+
53
+
54
+ curve25519_donna(o, (const unsigned char*)a1, (const unsigned char*)b1);
55
+
56
+ free(a1);
57
+ free(b1);
58
+
59
+ return as_byte_array(env, (unsigned char*)o, 32);
60
+ }
61
+
62
+ JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_makePrivate
63
+ (JNIEnv *env, jobject obj, jbyteArray secret) {
64
+
65
+ int len;
66
+ unsigned char* k = as_unsigned_char_array(env, secret, &len);
67
+
68
+ if (len != 32) {
69
+ fprintf(stderr, "Error, must be length 32");
70
+ return NULL;
71
+ }
72
+
73
+ k[0] &= 248;
74
+ k[31] &= 127;
75
+ k[31] |= 64;
76
+ return as_byte_array(env, k, 32);
77
+ }
78
+
79
+ JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_getPublic
80
+ (JNIEnv *env, jobject obj, jbyteArray privkey) {
81
+
82
+ int len;
83
+ unsigned char* private = as_unsigned_char_array(env, privkey, &len);
84
+
85
+ if (len != 32) {
86
+ fprintf(stderr, "Error, must be length 32");
87
+ return NULL;
88
+ }
89
+
90
+ unsigned char pubkey[32];
91
+ unsigned char basepoint[32] = {9};
92
+
93
+ curve25519_donna(pubkey, private, basepoint);
94
+ return as_byte_array(env, (unsigned char*)pubkey, 32);
95
+ }
96
+
97
+ JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_makeSharedSecret
98
+ (JNIEnv *env, jobject obj, jbyteArray privkey, jbyteArray their_pubkey) {
99
+
100
+ unsigned char shared_secret[32];
101
+
102
+ int l1, l2;
103
+ unsigned char* private = as_unsigned_char_array(env, privkey, &l1);
104
+ unsigned char* pubkey = as_unsigned_char_array(env, their_pubkey, &l2);
105
+
106
+ if ( !(l1 == 32 && l2 == 32) ) {
107
+ fprintf(stderr, "Error, must be length 32");
108
+ return NULL;
109
+ }
110
+
111
+ curve25519_donna(shared_secret, private, pubkey);
112
+ return as_byte_array(env, (unsigned char*)shared_secret, 32);
113
+ }
114
+
115
+ JNIEXPORT void JNICALL Java_Curve25519Donna_helowrld
116
+ (JNIEnv *env, jobject obj) {
117
+ printf("helowrld\n");
118
+ }
@@ -0,0 +1,53 @@
1
+ /* DO NOT EDIT THIS FILE - it is machine generated */
2
+ #include <jni.h>
3
+ /* Header for class Curve25519Donna */
4
+
5
+ #ifndef _Included_Curve25519Donna
6
+ #define _Included_Curve25519Donna
7
+ #ifdef __cplusplus
8
+ extern "C" {
9
+ #endif
10
+ /*
11
+ * Class: Curve25519Donna
12
+ * Method: curve25519Donna
13
+ * Signature: ([B[B)[B
14
+ */
15
+ JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_curve25519Donna
16
+ (JNIEnv *, jobject, jbyteArray, jbyteArray);
17
+
18
+ /*
19
+ * Class: Curve25519Donna
20
+ * Method: makePrivate
21
+ * Signature: ([B)[B
22
+ */
23
+ JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_makePrivate
24
+ (JNIEnv *, jobject, jbyteArray);
25
+
26
+ /*
27
+ * Class: Curve25519Donna
28
+ * Method: getPublic
29
+ * Signature: ([B)[B
30
+ */
31
+ JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_getPublic
32
+ (JNIEnv *, jobject, jbyteArray);
33
+
34
+ /*
35
+ * Class: Curve25519Donna
36
+ * Method: makeSharedSecret
37
+ * Signature: ([B[B)[B
38
+ */
39
+ JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_makeSharedSecret
40
+ (JNIEnv *, jobject, jbyteArray, jbyteArray);
41
+
42
+ /*
43
+ * Class: Curve25519Donna
44
+ * Method: helowrld
45
+ * Signature: ()V
46
+ */
47
+ JNIEXPORT void JNICALL Java_Curve25519Donna_helowrld
48
+ (JNIEnv *, jobject);
49
+
50
+ #ifdef __cplusplus
51
+ }
52
+ #endif
53
+ #endif