ruby_olm 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. checksums.yaml +7 -0
  2. data/ext/ruby_olm/ext_lib_olm/ext_account.c +274 -0
  3. data/ext/ruby_olm/ext_lib_olm/ext_lib_olm.c +51 -0
  4. data/ext/ruby_olm/ext_lib_olm/ext_lib_olm.h +13 -0
  5. data/ext/ruby_olm/ext_lib_olm/ext_session.c +363 -0
  6. data/ext/ruby_olm/ext_lib_olm/ext_utility.c +69 -0
  7. data/ext/ruby_olm/ext_lib_olm/extconf.rb +69 -0
  8. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_account.cpp +695 -0
  9. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_account.h +56 -0
  10. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_inbound_group_session.cpp +654 -0
  11. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_inbound_group_session.h +51 -0
  12. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni.h +81 -0
  13. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni_helper.cpp +224 -0
  14. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni_helper.h +30 -0
  15. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_manager.cpp +35 -0
  16. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_manager.h +36 -0
  17. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_outbound_group_session.cpp +563 -0
  18. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_outbound_group_session.h +49 -0
  19. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_pk.cpp +716 -0
  20. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_pk.h +48 -0
  21. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_session.cpp +977 -0
  22. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_session.h +59 -0
  23. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_utility.cpp +236 -0
  24. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_utility.h +40 -0
  25. data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_decode_message.cpp +14 -0
  26. data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_decrypt.cpp +65 -0
  27. data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_group_decrypt.cpp +73 -0
  28. data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_unpickle_account.cpp +14 -0
  29. data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_unpickle_session.cpp +14 -0
  30. data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/include/fuzzing.hh +82 -0
  31. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/account.hh +160 -0
  32. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/base64.h +77 -0
  33. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/base64.hh +63 -0
  34. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/cipher.h +138 -0
  35. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/crypto.h +202 -0
  36. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/error.h +72 -0
  37. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/inbound_group_session.h +235 -0
  38. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/list.hh +119 -0
  39. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/megolm.h +95 -0
  40. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/memory.h +41 -0
  41. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/memory.hh +90 -0
  42. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/message.h +93 -0
  43. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/message.hh +138 -0
  44. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/olm.h +451 -0
  45. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/olm.hh +4 -0
  46. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/outbound_group_session.h +181 -0
  47. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle.h +90 -0
  48. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle.hh +149 -0
  49. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle_encoding.h +76 -0
  50. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pk.h +214 -0
  51. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/ratchet.hh +184 -0
  52. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/session.hh +156 -0
  53. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/utility.hh +61 -0
  54. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes.c +1073 -0
  55. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes.h +123 -0
  56. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes_test.c +276 -0
  57. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour.c +45 -0
  58. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour.h +30 -0
  59. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour_test.c +47 -0
  60. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64.c +135 -0
  61. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64.h +27 -0
  62. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64_test.c +54 -0
  63. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish.c +269 -0
  64. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish.h +32 -0
  65. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish_test.c +68 -0
  66. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des.c +269 -0
  67. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des.h +37 -0
  68. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des_test.c +83 -0
  69. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2.c +104 -0
  70. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2.h +33 -0
  71. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2_test.c +58 -0
  72. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5.c +189 -0
  73. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5.h +34 -0
  74. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5_test.c +60 -0
  75. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13.c +35 -0
  76. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13.h +20 -0
  77. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13_test.c +44 -0
  78. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1.c +149 -0
  79. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1.h +35 -0
  80. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1_test.c +58 -0
  81. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256.c +159 -0
  82. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256.h +34 -0
  83. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256_test.c +61 -0
  84. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/contrib/Curve25519Donna.c +118 -0
  85. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/contrib/Curve25519Donna.h +53 -0
  86. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/curve25519-donna-c64.c +449 -0
  87. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/curve25519-donna.c +860 -0
  88. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/python-src/curve25519/curve25519module.c +105 -0
  89. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/speed-curve25519.c +50 -0
  90. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-curve25519.c +54 -0
  91. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-noncanon.c +39 -0
  92. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-sc-curve25519.c +72 -0
  93. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna.h +18 -0
  94. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/add_scalar.c +56 -0
  95. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ed25519.h +38 -0
  96. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fe.c +1493 -0
  97. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fe.h +41 -0
  98. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fixedint.h +72 -0
  99. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ge.c +467 -0
  100. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ge.h +74 -0
  101. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/key_exchange.c +79 -0
  102. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/keypair.c +16 -0
  103. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/precomp_data.h +1391 -0
  104. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sc.c +814 -0
  105. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sc.h +12 -0
  106. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/seed.c +40 -0
  107. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sha512.c +275 -0
  108. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sha512.h +21 -0
  109. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sign.c +31 -0
  110. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/verify.c +77 -0
  111. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/test.c +150 -0
  112. data/ext/ruby_olm/ext_lib_olm/olm/python/dummy/stddef.h +0 -0
  113. data/ext/ruby_olm/ext_lib_olm/olm/python/dummy/stdint.h +0 -0
  114. data/ext/ruby_olm/ext_lib_olm/olm/src/account.cpp +380 -0
  115. data/ext/ruby_olm/ext_lib_olm/olm/src/base64.cpp +167 -0
  116. data/ext/ruby_olm/ext_lib_olm/olm/src/cipher.cpp +152 -0
  117. data/ext/ruby_olm/ext_lib_olm/olm/src/crypto.cpp +299 -0
  118. data/ext/ruby_olm/ext_lib_olm/olm/src/ed25519.c +22 -0
  119. data/ext/ruby_olm/ext_lib_olm/olm/src/error.c +44 -0
  120. data/ext/ruby_olm/ext_lib_olm/olm/src/inbound_group_session.c +524 -0
  121. data/ext/ruby_olm/ext_lib_olm/olm/src/megolm.c +150 -0
  122. data/ext/ruby_olm/ext_lib_olm/olm/src/memory.cpp +45 -0
  123. data/ext/ruby_olm/ext_lib_olm/olm/src/message.cpp +401 -0
  124. data/ext/ruby_olm/ext_lib_olm/olm/src/olm.cpp +738 -0
  125. data/ext/ruby_olm/ext_lib_olm/olm/src/outbound_group_session.c +363 -0
  126. data/ext/ruby_olm/ext_lib_olm/olm/src/pickle.cpp +242 -0
  127. data/ext/ruby_olm/ext_lib_olm/olm/src/pickle_encoding.c +92 -0
  128. data/ext/ruby_olm/ext_lib_olm/olm/src/pk.cpp +412 -0
  129. data/ext/ruby_olm/ext_lib_olm/olm/src/ratchet.cpp +625 -0
  130. data/ext/ruby_olm/ext_lib_olm/olm/src/session.cpp +462 -0
  131. data/ext/ruby_olm/ext_lib_olm/olm/src/utility.cpp +57 -0
  132. data/ext/ruby_olm/ext_lib_olm/olm/tests/include/unittest.hh +107 -0
  133. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_base64.cpp +70 -0
  134. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_crypto.cpp +246 -0
  135. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_group_session.cpp +329 -0
  136. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_list.cpp +92 -0
  137. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_megolm.cpp +134 -0
  138. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_message.cpp +112 -0
  139. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm.cpp +405 -0
  140. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_decrypt.cpp +90 -0
  141. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_sha256.cpp +20 -0
  142. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_signature.cpp +81 -0
  143. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_using_malloc.cpp +210 -0
  144. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_pk.cpp +166 -0
  145. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_ratchet.cpp +221 -0
  146. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_session.cpp +144 -0
  147. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMAccount.h +51 -0
  148. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMAccount_Private.h +25 -0
  149. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMInboundGroupSession.h +38 -0
  150. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMKit.h +37 -0
  151. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMMessage.h +38 -0
  152. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMOutboundGroupSession.h +32 -0
  153. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkDecryption.h +71 -0
  154. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkEncryption.h +42 -0
  155. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkMessage.h +31 -0
  156. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSerializable.h +29 -0
  157. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSession.h +44 -0
  158. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSession_Private.h +26 -0
  159. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMUtility.h +49 -0
  160. data/ext/ruby_olm/ext_lib_olm/staging/account.cpp +380 -0
  161. data/ext/ruby_olm/ext_lib_olm/staging/aes.c +1073 -0
  162. data/ext/ruby_olm/ext_lib_olm/staging/base64.cpp +167 -0
  163. data/ext/ruby_olm/ext_lib_olm/staging/cipher.cpp +152 -0
  164. data/ext/ruby_olm/ext_lib_olm/staging/crypto.cpp +299 -0
  165. data/ext/ruby_olm/ext_lib_olm/staging/curve25519-donna.c +860 -0
  166. data/ext/ruby_olm/ext_lib_olm/staging/ed25519.c +22 -0
  167. data/ext/ruby_olm/ext_lib_olm/staging/error.c +44 -0
  168. data/ext/ruby_olm/ext_lib_olm/staging/inbound_group_session.c +524 -0
  169. data/ext/ruby_olm/ext_lib_olm/staging/megolm.c +150 -0
  170. data/ext/ruby_olm/ext_lib_olm/staging/memory.cpp +45 -0
  171. data/ext/ruby_olm/ext_lib_olm/staging/message.cpp +401 -0
  172. data/ext/ruby_olm/ext_lib_olm/staging/olm.cpp +738 -0
  173. data/ext/ruby_olm/ext_lib_olm/staging/outbound_group_session.c +363 -0
  174. data/ext/ruby_olm/ext_lib_olm/staging/pickle.cpp +242 -0
  175. data/ext/ruby_olm/ext_lib_olm/staging/pickle_encoding.c +92 -0
  176. data/ext/ruby_olm/ext_lib_olm/staging/pk.cpp +412 -0
  177. data/ext/ruby_olm/ext_lib_olm/staging/ratchet.cpp +625 -0
  178. data/ext/ruby_olm/ext_lib_olm/staging/session.cpp +461 -0
  179. data/ext/ruby_olm/ext_lib_olm/staging/sha256.c +159 -0
  180. data/ext/ruby_olm/ext_lib_olm/staging/utility.cpp +57 -0
  181. data/lib/ruby_olm/account.rb +42 -0
  182. data/lib/ruby_olm/message.rb +6 -0
  183. data/lib/ruby_olm/olm_error.rb +70 -0
  184. data/lib/ruby_olm/olm_message.rb +25 -0
  185. data/lib/ruby_olm/pre_key_message.rb +6 -0
  186. data/lib/ruby_olm/session.rb +16 -0
  187. data/lib/ruby_olm/version.rb +5 -0
  188. data/lib/ruby_olm.rb +10 -0
  189. data/rakefile +18 -0
  190. data/test/examples/test_bob_no_answer.rb +62 -0
  191. data/test/examples/test_exchange.rb +60 -0
  192. data/test/spec/test_account.rb +152 -0
  193. data/test/unit/test_account_methods.rb +85 -0
  194. metadata +282 -0
@@ -0,0 +1,269 @@
1
+ /*********************************************************************
2
+ * Filename: des.c
3
+ * Author: Brad Conte (brad AT radconte.com)
4
+ * Copyright:
5
+ * Disclaimer: This code is presented "as is" without any guarantees.
6
+ * Details: Implementation of the DES encryption algorithm.
7
+ Modes of operation (such as CBC) are not included.
8
+ The formal NIST algorithm specification can be found here:
9
+ * http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
10
+ *********************************************************************/
11
+
12
+ /*************************** HEADER FILES ***************************/
13
+ #include <stdlib.h>
14
+ #include <memory.h>
15
+ #include "des.h"
16
+
17
+ /****************************** MACROS ******************************/
18
+ // Obtain bit "b" from the left and shift it "c" places from the right
19
+ #define BITNUM(a,b,c) (((a[(b)/8] >> (7 - (b%8))) & 0x01) << (c))
20
+ #define BITNUMINTR(a,b,c) ((((a) >> (31 - (b))) & 0x00000001) << (c))
21
+ #define BITNUMINTL(a,b,c) ((((a) << (b)) & 0x80000000) >> (c))
22
+
23
+ // This macro converts a 6 bit block with the S-Box row defined as the first and last
24
+ // bits to a 6 bit block with the row defined by the first two bits.
25
+ #define SBOXBIT(a) (((a) & 0x20) | (((a) & 0x1f) >> 1) | (((a) & 0x01) << 4))
26
+
27
+ /**************************** VARIABLES *****************************/
28
+ static const BYTE sbox1[64] = {
29
+ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
30
+ 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
31
+ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
32
+ 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
33
+ };
34
+ static const BYTE sbox2[64] = {
35
+ 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
36
+ 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
37
+ 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
38
+ 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
39
+ };
40
+ static const BYTE sbox3[64] = {
41
+ 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
42
+ 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
43
+ 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
44
+ 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
45
+ };
46
+ static const BYTE sbox4[64] = {
47
+ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
48
+ 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
49
+ 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
50
+ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
51
+ };
52
+ static const BYTE sbox5[64] = {
53
+ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
54
+ 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
55
+ 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
56
+ 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
57
+ };
58
+ static const BYTE sbox6[64] = {
59
+ 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
60
+ 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
61
+ 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
62
+ 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
63
+ };
64
+ static const BYTE sbox7[64] = {
65
+ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
66
+ 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
67
+ 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
68
+ 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
69
+ };
70
+ static const BYTE sbox8[64] = {
71
+ 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
72
+ 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
73
+ 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
74
+ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
75
+ };
76
+
77
+ /*********************** FUNCTION DEFINITIONS ***********************/
78
+ // Initial (Inv)Permutation step
79
+ void IP(WORD state[], const BYTE in[])
80
+ {
81
+ state[0] = BITNUM(in,57,31) | BITNUM(in,49,30) | BITNUM(in,41,29) | BITNUM(in,33,28) |
82
+ BITNUM(in,25,27) | BITNUM(in,17,26) | BITNUM(in,9,25) | BITNUM(in,1,24) |
83
+ BITNUM(in,59,23) | BITNUM(in,51,22) | BITNUM(in,43,21) | BITNUM(in,35,20) |
84
+ BITNUM(in,27,19) | BITNUM(in,19,18) | BITNUM(in,11,17) | BITNUM(in,3,16) |
85
+ BITNUM(in,61,15) | BITNUM(in,53,14) | BITNUM(in,45,13) | BITNUM(in,37,12) |
86
+ BITNUM(in,29,11) | BITNUM(in,21,10) | BITNUM(in,13,9) | BITNUM(in,5,8) |
87
+ BITNUM(in,63,7) | BITNUM(in,55,6) | BITNUM(in,47,5) | BITNUM(in,39,4) |
88
+ BITNUM(in,31,3) | BITNUM(in,23,2) | BITNUM(in,15,1) | BITNUM(in,7,0);
89
+
90
+ state[1] = BITNUM(in,56,31) | BITNUM(in,48,30) | BITNUM(in,40,29) | BITNUM(in,32,28) |
91
+ BITNUM(in,24,27) | BITNUM(in,16,26) | BITNUM(in,8,25) | BITNUM(in,0,24) |
92
+ BITNUM(in,58,23) | BITNUM(in,50,22) | BITNUM(in,42,21) | BITNUM(in,34,20) |
93
+ BITNUM(in,26,19) | BITNUM(in,18,18) | BITNUM(in,10,17) | BITNUM(in,2,16) |
94
+ BITNUM(in,60,15) | BITNUM(in,52,14) | BITNUM(in,44,13) | BITNUM(in,36,12) |
95
+ BITNUM(in,28,11) | BITNUM(in,20,10) | BITNUM(in,12,9) | BITNUM(in,4,8) |
96
+ BITNUM(in,62,7) | BITNUM(in,54,6) | BITNUM(in,46,5) | BITNUM(in,38,4) |
97
+ BITNUM(in,30,3) | BITNUM(in,22,2) | BITNUM(in,14,1) | BITNUM(in,6,0);
98
+ }
99
+
100
+ void InvIP(WORD state[], BYTE in[])
101
+ {
102
+ in[0] = BITNUMINTR(state[1],7,7) | BITNUMINTR(state[0],7,6) | BITNUMINTR(state[1],15,5) |
103
+ BITNUMINTR(state[0],15,4) | BITNUMINTR(state[1],23,3) | BITNUMINTR(state[0],23,2) |
104
+ BITNUMINTR(state[1],31,1) | BITNUMINTR(state[0],31,0);
105
+
106
+ in[1] = BITNUMINTR(state[1],6,7) | BITNUMINTR(state[0],6,6) | BITNUMINTR(state[1],14,5) |
107
+ BITNUMINTR(state[0],14,4) | BITNUMINTR(state[1],22,3) | BITNUMINTR(state[0],22,2) |
108
+ BITNUMINTR(state[1],30,1) | BITNUMINTR(state[0],30,0);
109
+
110
+ in[2] = BITNUMINTR(state[1],5,7) | BITNUMINTR(state[0],5,6) | BITNUMINTR(state[1],13,5) |
111
+ BITNUMINTR(state[0],13,4) | BITNUMINTR(state[1],21,3) | BITNUMINTR(state[0],21,2) |
112
+ BITNUMINTR(state[1],29,1) | BITNUMINTR(state[0],29,0);
113
+
114
+ in[3] = BITNUMINTR(state[1],4,7) | BITNUMINTR(state[0],4,6) | BITNUMINTR(state[1],12,5) |
115
+ BITNUMINTR(state[0],12,4) | BITNUMINTR(state[1],20,3) | BITNUMINTR(state[0],20,2) |
116
+ BITNUMINTR(state[1],28,1) | BITNUMINTR(state[0],28,0);
117
+
118
+ in[4] = BITNUMINTR(state[1],3,7) | BITNUMINTR(state[0],3,6) | BITNUMINTR(state[1],11,5) |
119
+ BITNUMINTR(state[0],11,4) | BITNUMINTR(state[1],19,3) | BITNUMINTR(state[0],19,2) |
120
+ BITNUMINTR(state[1],27,1) | BITNUMINTR(state[0],27,0);
121
+
122
+ in[5] = BITNUMINTR(state[1],2,7) | BITNUMINTR(state[0],2,6) | BITNUMINTR(state[1],10,5) |
123
+ BITNUMINTR(state[0],10,4) | BITNUMINTR(state[1],18,3) | BITNUMINTR(state[0],18,2) |
124
+ BITNUMINTR(state[1],26,1) | BITNUMINTR(state[0],26,0);
125
+
126
+ in[6] = BITNUMINTR(state[1],1,7) | BITNUMINTR(state[0],1,6) | BITNUMINTR(state[1],9,5) |
127
+ BITNUMINTR(state[0],9,4) | BITNUMINTR(state[1],17,3) | BITNUMINTR(state[0],17,2) |
128
+ BITNUMINTR(state[1],25,1) | BITNUMINTR(state[0],25,0);
129
+
130
+ in[7] = BITNUMINTR(state[1],0,7) | BITNUMINTR(state[0],0,6) | BITNUMINTR(state[1],8,5) |
131
+ BITNUMINTR(state[0],8,4) | BITNUMINTR(state[1],16,3) | BITNUMINTR(state[0],16,2) |
132
+ BITNUMINTR(state[1],24,1) | BITNUMINTR(state[0],24,0);
133
+ }
134
+
135
+ WORD f(WORD state, const BYTE key[])
136
+ {
137
+ BYTE lrgstate[6]; //,i;
138
+ WORD t1,t2;
139
+
140
+ // Expantion Permutation
141
+ t1 = BITNUMINTL(state,31,0) | ((state & 0xf0000000) >> 1) | BITNUMINTL(state,4,5) |
142
+ BITNUMINTL(state,3,6) | ((state & 0x0f000000) >> 3) | BITNUMINTL(state,8,11) |
143
+ BITNUMINTL(state,7,12) | ((state & 0x00f00000) >> 5) | BITNUMINTL(state,12,17) |
144
+ BITNUMINTL(state,11,18) | ((state & 0x000f0000) >> 7) | BITNUMINTL(state,16,23);
145
+
146
+ t2 = BITNUMINTL(state,15,0) | ((state & 0x0000f000) << 15) | BITNUMINTL(state,20,5) |
147
+ BITNUMINTL(state,19,6) | ((state & 0x00000f00) << 13) | BITNUMINTL(state,24,11) |
148
+ BITNUMINTL(state,23,12) | ((state & 0x000000f0) << 11) | BITNUMINTL(state,28,17) |
149
+ BITNUMINTL(state,27,18) | ((state & 0x0000000f) << 9) | BITNUMINTL(state,0,23);
150
+
151
+ lrgstate[0] = (t1 >> 24) & 0x000000ff;
152
+ lrgstate[1] = (t1 >> 16) & 0x000000ff;
153
+ lrgstate[2] = (t1 >> 8) & 0x000000ff;
154
+ lrgstate[3] = (t2 >> 24) & 0x000000ff;
155
+ lrgstate[4] = (t2 >> 16) & 0x000000ff;
156
+ lrgstate[5] = (t2 >> 8) & 0x000000ff;
157
+
158
+ // Key XOR
159
+ lrgstate[0] ^= key[0];
160
+ lrgstate[1] ^= key[1];
161
+ lrgstate[2] ^= key[2];
162
+ lrgstate[3] ^= key[3];
163
+ lrgstate[4] ^= key[4];
164
+ lrgstate[5] ^= key[5];
165
+
166
+ // S-Box Permutation
167
+ state = (sbox1[SBOXBIT(lrgstate[0] >> 2)] << 28) |
168
+ (sbox2[SBOXBIT(((lrgstate[0] & 0x03) << 4) | (lrgstate[1] >> 4))] << 24) |
169
+ (sbox3[SBOXBIT(((lrgstate[1] & 0x0f) << 2) | (lrgstate[2] >> 6))] << 20) |
170
+ (sbox4[SBOXBIT(lrgstate[2] & 0x3f)] << 16) |
171
+ (sbox5[SBOXBIT(lrgstate[3] >> 2)] << 12) |
172
+ (sbox6[SBOXBIT(((lrgstate[3] & 0x03) << 4) | (lrgstate[4] >> 4))] << 8) |
173
+ (sbox7[SBOXBIT(((lrgstate[4] & 0x0f) << 2) | (lrgstate[5] >> 6))] << 4) |
174
+ sbox8[SBOXBIT(lrgstate[5] & 0x3f)];
175
+
176
+ // P-Box Permutation
177
+ state = BITNUMINTL(state,15,0) | BITNUMINTL(state,6,1) | BITNUMINTL(state,19,2) |
178
+ BITNUMINTL(state,20,3) | BITNUMINTL(state,28,4) | BITNUMINTL(state,11,5) |
179
+ BITNUMINTL(state,27,6) | BITNUMINTL(state,16,7) | BITNUMINTL(state,0,8) |
180
+ BITNUMINTL(state,14,9) | BITNUMINTL(state,22,10) | BITNUMINTL(state,25,11) |
181
+ BITNUMINTL(state,4,12) | BITNUMINTL(state,17,13) | BITNUMINTL(state,30,14) |
182
+ BITNUMINTL(state,9,15) | BITNUMINTL(state,1,16) | BITNUMINTL(state,7,17) |
183
+ BITNUMINTL(state,23,18) | BITNUMINTL(state,13,19) | BITNUMINTL(state,31,20) |
184
+ BITNUMINTL(state,26,21) | BITNUMINTL(state,2,22) | BITNUMINTL(state,8,23) |
185
+ BITNUMINTL(state,18,24) | BITNUMINTL(state,12,25) | BITNUMINTL(state,29,26) |
186
+ BITNUMINTL(state,5,27) | BITNUMINTL(state,21,28) | BITNUMINTL(state,10,29) |
187
+ BITNUMINTL(state,3,30) | BITNUMINTL(state,24,31);
188
+
189
+ // Return the final state value
190
+ return(state);
191
+ }
192
+
193
+ void des_key_setup(const BYTE key[], BYTE schedule[][6], DES_MODE mode)
194
+ {
195
+ WORD i, j, to_gen, C, D;
196
+ const WORD key_rnd_shift[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
197
+ const WORD key_perm_c[28] = {56,48,40,32,24,16,8,0,57,49,41,33,25,17,
198
+ 9,1,58,50,42,34,26,18,10,2,59,51,43,35};
199
+ const WORD key_perm_d[28] = {62,54,46,38,30,22,14,6,61,53,45,37,29,21,
200
+ 13,5,60,52,44,36,28,20,12,4,27,19,11,3};
201
+ const WORD key_compression[48] = {13,16,10,23,0,4,2,27,14,5,20,9,
202
+ 22,18,11,3,25,7,15,6,26,19,12,1,
203
+ 40,51,30,36,46,54,29,39,50,44,32,47,
204
+ 43,48,38,55,33,52,45,41,49,35,28,31};
205
+
206
+ // Permutated Choice #1 (copy the key in, ignoring parity bits).
207
+ for (i = 0, j = 31, C = 0; i < 28; ++i, --j)
208
+ C |= BITNUM(key,key_perm_c[i],j);
209
+ for (i = 0, j = 31, D = 0; i < 28; ++i, --j)
210
+ D |= BITNUM(key,key_perm_d[i],j);
211
+
212
+ // Generate the 16 subkeys.
213
+ for (i = 0; i < 16; ++i) {
214
+ C = ((C << key_rnd_shift[i]) | (C >> (28-key_rnd_shift[i]))) & 0xfffffff0;
215
+ D = ((D << key_rnd_shift[i]) | (D >> (28-key_rnd_shift[i]))) & 0xfffffff0;
216
+
217
+ // Decryption subkeys are reverse order of encryption subkeys so
218
+ // generate them in reverse if the key schedule is for decryption useage.
219
+ if (mode == DES_DECRYPT)
220
+ to_gen = 15 - i;
221
+ else /*(if mode == DES_ENCRYPT)*/
222
+ to_gen = i;
223
+ // Initialize the array
224
+ for (j = 0; j < 6; ++j)
225
+ schedule[to_gen][j] = 0;
226
+ for (j = 0; j < 24; ++j)
227
+ schedule[to_gen][j/8] |= BITNUMINTR(C,key_compression[j],7 - (j%8));
228
+ for ( ; j < 48; ++j)
229
+ schedule[to_gen][j/8] |= BITNUMINTR(D,key_compression[j] - 28,7 - (j%8));
230
+ }
231
+ }
232
+
233
+ void des_crypt(const BYTE in[], BYTE out[], const BYTE key[][6])
234
+ {
235
+ WORD state[2],idx,t;
236
+
237
+ IP(state,in);
238
+
239
+ for (idx=0; idx < 15; ++idx) {
240
+ t = state[1];
241
+ state[1] = f(state[1],key[idx]) ^ state[0];
242
+ state[0] = t;
243
+ }
244
+ // Perform the final loop manually as it doesn't switch sides
245
+ state[0] = f(state[1],key[15]) ^ state[0];
246
+
247
+ InvIP(state,out);
248
+ }
249
+
250
+ void three_des_key_setup(const BYTE key[], BYTE schedule[][16][6], DES_MODE mode)
251
+ {
252
+ if (mode == DES_ENCRYPT) {
253
+ des_key_setup(&key[0],schedule[0],mode);
254
+ des_key_setup(&key[8],schedule[1],!mode);
255
+ des_key_setup(&key[16],schedule[2],mode);
256
+ }
257
+ else /*if (mode == DES_DECRYPT*/ {
258
+ des_key_setup(&key[16],schedule[0],mode);
259
+ des_key_setup(&key[8],schedule[1],!mode);
260
+ des_key_setup(&key[0],schedule[2],mode);
261
+ }
262
+ }
263
+
264
+ void three_des_crypt(const BYTE in[], BYTE out[], const BYTE key[][16][6])
265
+ {
266
+ des_crypt(in,out,key[0]);
267
+ des_crypt(out,out,key[1]);
268
+ des_crypt(out,out,key[2]);
269
+ }
@@ -0,0 +1,37 @@
1
+ /*********************************************************************
2
+ * Filename: des.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 DES implementation.
7
+ Note that encryption and decryption are defined by how
8
+ the key setup is performed, the actual en/de-cryption is
9
+ performed by the same function.
10
+ *********************************************************************/
11
+
12
+ #ifndef DES_H
13
+ #define DESH
14
+
15
+ /*************************** HEADER FILES ***************************/
16
+ #include <stddef.h>
17
+
18
+ /****************************** MACROS ******************************/
19
+ #define DES_BLOCK_SIZE 8 // DES operates on 8 bytes at a time
20
+
21
+ /**************************** DATA TYPES ****************************/
22
+ typedef unsigned char BYTE; // 8-bit byte
23
+ typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
24
+
25
+ typedef enum {
26
+ DES_ENCRYPT,
27
+ DES_DECRYPT
28
+ } DES_MODE;
29
+
30
+ /*********************** FUNCTION DECLARATIONS **********************/
31
+ void des_key_setup(const BYTE key[], BYTE schedule[][6], DES_MODE mode);
32
+ void des_crypt(const BYTE in[], BYTE out[], const BYTE key[][6]);
33
+
34
+ void three_des_key_setup(const BYTE key[], BYTE schedule[][16][6], DES_MODE mode);
35
+ void three_des_crypt(const BYTE in[], BYTE out[], const BYTE key[][16][6]);
36
+
37
+ #endif // DES_H
@@ -0,0 +1,83 @@
1
+ /*********************************************************************
2
+ * Filename: des_test.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 DES
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 "des.h"
18
+
19
+ /*********************** FUNCTION DEFINITIONS ***********************/
20
+ int des_test()
21
+ {
22
+ BYTE pt1[DES_BLOCK_SIZE] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xE7};
23
+ BYTE pt2[DES_BLOCK_SIZE] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
24
+ BYTE pt3[DES_BLOCK_SIZE] = {0x54,0x68,0x65,0x20,0x71,0x75,0x66,0x63};
25
+ BYTE ct1[DES_BLOCK_SIZE] = {0xc9,0x57,0x44,0x25,0x6a,0x5e,0xd3,0x1d};
26
+ BYTE ct2[DES_BLOCK_SIZE] = {0x85,0xe8,0x13,0x54,0x0f,0x0a,0xb4,0x05};
27
+ BYTE ct3[DES_BLOCK_SIZE] = {0xc9,0x57,0x44,0x25,0x6a,0x5e,0xd3,0x1d};
28
+ BYTE ct4[DES_BLOCK_SIZE] = {0xA8,0x26,0xFD,0x8C,0xE5,0x3B,0x85,0x5F};
29
+ BYTE key1[DES_BLOCK_SIZE] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
30
+ BYTE key2[DES_BLOCK_SIZE] = {0x13,0x34,0x57,0x79,0x9B,0xBC,0xDF,0xF1};
31
+ BYTE three_key1[DES_BLOCK_SIZE * 3] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
32
+ 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
33
+ 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
34
+ BYTE three_key2[DES_BLOCK_SIZE * 3] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
35
+ 0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,
36
+ 0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,0x23};
37
+
38
+ BYTE schedule[16][6];
39
+ BYTE three_schedule[3][16][6];
40
+ BYTE buf[DES_BLOCK_SIZE];
41
+ int pass = 1;
42
+
43
+ des_key_setup(key1, schedule, DES_ENCRYPT);
44
+ des_crypt(pt1, buf, schedule);
45
+ pass = pass && !memcmp(ct1, buf, DES_BLOCK_SIZE);
46
+
47
+ des_key_setup(key1, schedule, DES_DECRYPT);
48
+ des_crypt(ct1, buf, schedule);
49
+ pass = pass && !memcmp(pt1, buf, DES_BLOCK_SIZE);
50
+
51
+ des_key_setup(key2, schedule, DES_ENCRYPT);
52
+ des_crypt(pt2, buf, schedule);
53
+ pass = pass && !memcmp(ct2, buf, DES_BLOCK_SIZE);
54
+
55
+ des_key_setup(key2, schedule, DES_DECRYPT);
56
+ des_crypt(ct2, buf, schedule);
57
+ pass = pass && !memcmp(pt2, buf, DES_BLOCK_SIZE);
58
+
59
+ three_des_key_setup(three_key1, three_schedule, DES_ENCRYPT);
60
+ three_des_crypt(pt1, buf, three_schedule);
61
+ pass = pass && !memcmp(ct3, buf, DES_BLOCK_SIZE);
62
+
63
+ three_des_key_setup(three_key1, three_schedule, DES_DECRYPT);
64
+ three_des_crypt(ct3, buf, three_schedule);
65
+ pass = pass && !memcmp(pt1, buf, DES_BLOCK_SIZE);
66
+
67
+ three_des_key_setup(three_key2, three_schedule, DES_ENCRYPT);
68
+ three_des_crypt(pt3, buf, three_schedule);
69
+ pass = pass && !memcmp(ct4, buf, DES_BLOCK_SIZE);
70
+
71
+ three_des_key_setup(three_key2, three_schedule, DES_DECRYPT);
72
+ three_des_crypt(ct4, buf, three_schedule);
73
+ pass = pass && !memcmp(pt3, buf, DES_BLOCK_SIZE);
74
+
75
+ return(pass);
76
+ }
77
+
78
+ int main()
79
+ {
80
+ printf("DES test: %s\n", des_test() ? "SUCCEEDED" : "FAILED");
81
+
82
+ return(0);
83
+ }
@@ -0,0 +1,104 @@
1
+ /*********************************************************************
2
+ * Filename: md2.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 MD2 hashing algorithm.
7
+ Algorithm specification can be found here:
8
+ * http://tools.ietf.org/html/rfc1319 .
9
+ Input is little endian byte order.
10
+ *********************************************************************/
11
+
12
+ /*************************** HEADER FILES ***************************/
13
+ #include <stdlib.h>
14
+ #include <memory.h>
15
+ #include "md2.h"
16
+
17
+ /**************************** VARIABLES *****************************/
18
+ static const BYTE s[256] = {
19
+ 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
20
+ 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
21
+ 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
22
+ 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
23
+ 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
24
+ 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
25
+ 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
26
+ 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
27
+ 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
28
+ 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
29
+ 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
30
+ 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
31
+ 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
32
+ 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
33
+ 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
34
+ 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
35
+ 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
36
+ 31, 26, 219, 153, 141, 51, 159, 17, 131, 20
37
+ };
38
+
39
+ /*********************** FUNCTION DEFINITIONS ***********************/
40
+ void md2_transform(MD2_CTX *ctx, BYTE data[])
41
+ {
42
+ int j,k,t;
43
+
44
+ //memcpy(&ctx->state[16], data);
45
+ for (j=0; j < 16; ++j) {
46
+ ctx->state[j + 16] = data[j];
47
+ ctx->state[j + 32] = (ctx->state[j+16] ^ ctx->state[j]);
48
+ }
49
+
50
+ t = 0;
51
+ for (j = 0; j < 18; ++j) {
52
+ for (k = 0; k < 48; ++k) {
53
+ ctx->state[k] ^= s[t];
54
+ t = ctx->state[k];
55
+ }
56
+ t = (t+j) & 0xFF;
57
+ }
58
+
59
+ t = ctx->checksum[15];
60
+ for (j=0; j < 16; ++j) {
61
+ ctx->checksum[j] ^= s[data[j] ^ t];
62
+ t = ctx->checksum[j];
63
+ }
64
+ }
65
+
66
+ void md2_init(MD2_CTX *ctx)
67
+ {
68
+ int i;
69
+
70
+ for (i=0; i < 48; ++i)
71
+ ctx->state[i] = 0;
72
+ for (i=0; i < 16; ++i)
73
+ ctx->checksum[i] = 0;
74
+ ctx->len = 0;
75
+ }
76
+
77
+ void md2_update(MD2_CTX *ctx, const BYTE data[], size_t len)
78
+ {
79
+ size_t i;
80
+
81
+ for (i = 0; i < len; ++i) {
82
+ ctx->data[ctx->len] = data[i];
83
+ ctx->len++;
84
+ if (ctx->len == MD2_BLOCK_SIZE) {
85
+ md2_transform(ctx, ctx->data);
86
+ ctx->len = 0;
87
+ }
88
+ }
89
+ }
90
+
91
+ void md2_final(MD2_CTX *ctx, BYTE hash[])
92
+ {
93
+ int to_pad;
94
+
95
+ to_pad = MD2_BLOCK_SIZE - ctx->len;
96
+
97
+ while (ctx->len < MD2_BLOCK_SIZE)
98
+ ctx->data[ctx->len++] = to_pad;
99
+
100
+ md2_transform(ctx, ctx->data);
101
+ md2_transform(ctx, ctx->checksum);
102
+
103
+ memcpy(hash, ctx->state, MD2_BLOCK_SIZE);
104
+ }
@@ -0,0 +1,33 @@
1
+ /*********************************************************************
2
+ * Filename: md2.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 MD2 implementation.
7
+ *********************************************************************/
8
+
9
+ #ifndef MD2_H
10
+ #define MD2_H
11
+
12
+ /*************************** HEADER FILES ***************************/
13
+ #include <stddef.h>
14
+
15
+ /****************************** MACROS ******************************/
16
+ #define MD2_BLOCK_SIZE 16
17
+
18
+ /**************************** DATA TYPES ****************************/
19
+ typedef unsigned char BYTE; // 8-bit byte
20
+
21
+ typedef struct {
22
+ BYTE data[16];
23
+ BYTE state[48];
24
+ BYTE checksum[16];
25
+ int len;
26
+ } MD2_CTX;
27
+
28
+ /*********************** FUNCTION DECLARATIONS **********************/
29
+ void md2_init(MD2_CTX *ctx);
30
+ void md2_update(MD2_CTX *ctx, const BYTE data[], size_t len);
31
+ void md2_final(MD2_CTX *ctx, BYTE hash[]); // size of hash must be MD2_BLOCK_SIZE
32
+
33
+ #endif // MD2_H
@@ -0,0 +1,58 @@
1
+ /*********************************************************************
2
+ * Filename: md2_test.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 MD2
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 <string.h>
17
+ #include <memory.h>
18
+ #include "md2.h"
19
+
20
+ /*********************** FUNCTION DEFINITIONS ***********************/
21
+ int md2_test()
22
+ {
23
+ BYTE text1[] = {"abc"};
24
+ BYTE text2[] = {"abcdefghijklmnopqrstuvwxyz"};
25
+ BYTE text3_1[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"};
26
+ BYTE text3_2[] = {"fghijklmnopqrstuvwxyz0123456789"};
27
+ BYTE hash1[MD2_BLOCK_SIZE] = {0xda,0x85,0x3b,0x0d,0x3f,0x88,0xd9,0x9b,0x30,0x28,0x3a,0x69,0xe6,0xde,0xd6,0xbb};
28
+ BYTE hash2[MD2_BLOCK_SIZE] = {0x4e,0x8d,0xdf,0xf3,0x65,0x02,0x92,0xab,0x5a,0x41,0x08,0xc3,0xaa,0x47,0x94,0x0b};
29
+ BYTE hash3[MD2_BLOCK_SIZE] = {0xda,0x33,0xde,0xf2,0xa4,0x2d,0xf1,0x39,0x75,0x35,0x28,0x46,0xc3,0x03,0x38,0xcd};
30
+ BYTE buf[16];
31
+ MD2_CTX ctx;
32
+ int pass = 1;
33
+
34
+ md2_init(&ctx);
35
+ md2_update(&ctx, text1, strlen(text1));
36
+ md2_final(&ctx, buf);
37
+ pass = pass && !memcmp(hash1, buf, MD2_BLOCK_SIZE);
38
+
39
+ // Note that the MD2 object can be re-used.
40
+ md2_init(&ctx);
41
+ md2_update(&ctx, text2, strlen(text2));
42
+ md2_final(&ctx, buf);
43
+ pass = pass && !memcmp(hash2, buf, MD2_BLOCK_SIZE);
44
+
45
+ // Note that the data is added in two chunks.
46
+ md2_init(&ctx);
47
+ md2_update(&ctx, text3_1, strlen(text3_1));
48
+ md2_update(&ctx, text3_2, strlen(text3_2));
49
+ md2_final(&ctx, buf);
50
+ pass = pass && !memcmp(hash3, buf, MD2_BLOCK_SIZE);
51
+
52
+ return(pass);
53
+ }
54
+
55
+ int main()
56
+ {
57
+ printf("MD2 tests: %s\n", md2_test() ? "SUCCEEDED" : "FAILED");
58
+ }