librats 1.0.2 → 2.2.0

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 (319) hide show
  1. package/README.md +145 -331
  2. package/binding.gyp +16 -3
  3. package/lib/index.d.ts +288 -696
  4. package/lib/index.js +407 -44
  5. package/native-src/3rdparty/android/ifaddrs-android.c +1 -0
  6. package/native-src/3rdparty/android/ifaddrs-android.h +1 -0
  7. package/native-src/CMakeLists.txt +404 -179
  8. package/native-src/LICENSE +1 -1
  9. package/native-src/src/librats/bindings/rats.cpp +762 -0
  10. package/native-src/src/librats/bindings/rats.h +380 -0
  11. package/native-src/src/librats/bittorrent/bencode.cpp +437 -0
  12. package/native-src/src/librats/bittorrent/bencode.h +176 -0
  13. package/native-src/src/librats/bittorrent/bitfield.cpp +97 -0
  14. package/native-src/src/librats/bittorrent/bitfield.h +76 -0
  15. package/native-src/src/librats/bittorrent/byte_io.h +58 -0
  16. package/native-src/src/librats/bittorrent/choker.cpp +25 -0
  17. package/native-src/src/librats/bittorrent/choker.h +46 -0
  18. package/native-src/src/librats/bittorrent/client.cpp +413 -0
  19. package/native-src/src/librats/bittorrent/client.h +227 -0
  20. package/native-src/src/librats/bittorrent/disk_io.cpp +209 -0
  21. package/native-src/src/librats/bittorrent/disk_io.h +150 -0
  22. package/native-src/src/librats/bittorrent/extensions.cpp +191 -0
  23. package/native-src/src/librats/bittorrent/extensions.h +94 -0
  24. package/native-src/src/librats/bittorrent/file_storage.cpp +77 -0
  25. package/native-src/src/librats/bittorrent/file_storage.h +79 -0
  26. package/native-src/src/librats/bittorrent/log.h +42 -0
  27. package/native-src/src/librats/bittorrent/magnet_uri.cpp +98 -0
  28. package/native-src/src/librats/bittorrent/magnet_uri.h +35 -0
  29. package/native-src/src/librats/bittorrent/peer_connection.cpp +502 -0
  30. package/native-src/src/librats/bittorrent/peer_connection.h +194 -0
  31. package/native-src/src/librats/bittorrent/peer_list.cpp +68 -0
  32. package/native-src/src/librats/bittorrent/peer_list.h +75 -0
  33. package/native-src/src/librats/bittorrent/piece_picker.cpp +352 -0
  34. package/native-src/src/librats/bittorrent/piece_picker.h +201 -0
  35. package/native-src/src/librats/bittorrent/reactor.cpp +97 -0
  36. package/native-src/src/librats/bittorrent/reactor.h +89 -0
  37. package/native-src/src/librats/bittorrent/resume_data.cpp +72 -0
  38. package/native-src/src/librats/bittorrent/resume_data.h +41 -0
  39. package/native-src/src/librats/bittorrent/store_buffer.cpp +48 -0
  40. package/native-src/src/librats/bittorrent/store_buffer.h +47 -0
  41. package/native-src/src/librats/bittorrent/torrent.cpp +870 -0
  42. package/native-src/src/librats/bittorrent/torrent.h +260 -0
  43. package/native-src/src/librats/bittorrent/torrent_creator.cpp +129 -0
  44. package/native-src/src/librats/bittorrent/torrent_creator.h +58 -0
  45. package/native-src/src/librats/bittorrent/torrent_info.cpp +314 -0
  46. package/native-src/src/librats/bittorrent/torrent_info.h +118 -0
  47. package/native-src/src/librats/bittorrent/tracker.cpp +374 -0
  48. package/native-src/src/librats/bittorrent/tracker.h +108 -0
  49. package/native-src/src/librats/bittorrent/types.cpp +206 -0
  50. package/native-src/src/librats/bittorrent/types.h +86 -0
  51. package/native-src/src/librats/core/address.cpp +35 -0
  52. package/native-src/src/librats/core/address.h +78 -0
  53. package/native-src/src/librats/core/bytes.h +69 -0
  54. package/native-src/src/librats/core/chained_send_buffer.cpp +172 -0
  55. package/native-src/src/librats/core/chained_send_buffer.h +183 -0
  56. package/native-src/src/librats/core/endpoint_parse.cpp +41 -0
  57. package/native-src/src/librats/core/endpoint_parse.h +31 -0
  58. package/native-src/src/librats/core/event_bus.h +70 -0
  59. package/native-src/src/librats/core/host_endpoint.h +56 -0
  60. package/native-src/src/{io_poller.cpp → librats/core/io_poller.cpp} +520 -65
  61. package/native-src/src/{io_poller.h → librats/core/io_poller.h} +12 -6
  62. package/native-src/src/librats/core/ip_address.cpp +120 -0
  63. package/native-src/src/librats/core/ip_address.h +109 -0
  64. package/native-src/src/librats/core/mpsc_queue.h +47 -0
  65. package/native-src/src/librats/core/notifier.h +74 -0
  66. package/native-src/src/librats/core/receive_buffer.cpp +219 -0
  67. package/native-src/src/librats/core/receive_buffer.h +171 -0
  68. package/native-src/src/librats/core/service_registry.h +58 -0
  69. package/native-src/src/{socket.cpp → librats/core/socket.cpp} +625 -118
  70. package/native-src/src/librats/core/socket.h +496 -0
  71. package/native-src/src/librats/core/timer_queue.h +105 -0
  72. package/native-src/src/librats/core/types.cpp +43 -0
  73. package/native-src/src/librats/core/types.h +103 -0
  74. package/native-src/src/librats/core/wakeup_pipe.h +83 -0
  75. package/native-src/src/{crypto → librats/crypto}/blake2_endian.h +21 -23
  76. package/native-src/src/{crypto → librats/crypto}/blake2b.c +34 -33
  77. package/native-src/src/{crypto → librats/crypto}/blake2b.h +7 -6
  78. package/native-src/src/{crypto → librats/crypto}/blake2s.c +55 -54
  79. package/native-src/src/{crypto → librats/crypto}/blake2s.h +13 -12
  80. package/native-src/src/{crypto → librats/crypto}/chacha.c +22 -21
  81. package/native-src/src/{crypto → librats/crypto}/chacha.h +14 -13
  82. package/native-src/src/{crypto → librats/crypto}/chachapoly.c +56 -56
  83. package/native-src/src/{crypto → librats/crypto}/chachapoly.h +24 -17
  84. package/native-src/src/{crc32.cpp → librats/crypto/crc32.cpp} +1 -1
  85. package/native-src/src/{crc32.h → librats/crypto/crc32.h} +3 -1
  86. package/native-src/src/{crypto → librats/crypto}/curve25519.c +6 -4
  87. package/native-src/src/{crypto → librats/crypto}/curve25519.h +6 -3
  88. package/native-src/src/librats/crypto/hkdf.c +266 -0
  89. package/native-src/src/{crypto → librats/crypto}/hkdf.h +19 -19
  90. package/native-src/src/{noise.cpp → librats/crypto/noise.cpp} +84 -73
  91. package/native-src/src/{noise.h → librats/crypto/noise.h} +18 -8
  92. package/native-src/src/{crypto → librats/crypto}/poly1305.c +47 -46
  93. package/native-src/src/librats/crypto/poly1305.h +37 -0
  94. package/native-src/src/{sha1.cpp → librats/crypto/sha1.cpp} +33 -1
  95. package/native-src/src/{sha1.h → librats/crypto/sha1.h} +14 -6
  96. package/native-src/src/{crypto → librats/crypto}/sha256.c +15 -14
  97. package/native-src/src/{crypto → librats/crypto}/sha256.h +8 -7
  98. package/native-src/src/{crypto → librats/crypto}/sha512.c +15 -14
  99. package/native-src/src/{crypto → librats/crypto}/sha512.h +8 -7
  100. package/native-src/src/librats/dht/announce.cpp +37 -0
  101. package/native-src/src/librats/dht/announce.h +41 -0
  102. package/native-src/src/librats/dht/bep42.cpp +109 -0
  103. package/native-src/src/librats/dht/bep42.h +48 -0
  104. package/native-src/src/librats/dht/dht.cpp +501 -0
  105. package/native-src/src/librats/dht/dht.h +119 -0
  106. package/native-src/src/librats/dht/dht_runner.cpp +103 -0
  107. package/native-src/src/librats/dht/dht_runner.h +71 -0
  108. package/native-src/src/librats/dht/dos_blocker.cpp +42 -0
  109. package/native-src/src/librats/dht/dos_blocker.h +47 -0
  110. package/native-src/src/librats/dht/find_peers.cpp +52 -0
  111. package/native-src/src/librats/dht/find_peers.h +73 -0
  112. package/native-src/src/librats/dht/id.h +167 -0
  113. package/native-src/src/{krpc.cpp → librats/dht/krpc.cpp} +32 -81
  114. package/native-src/src/{krpc.h → librats/dht/krpc.h} +19 -23
  115. package/native-src/src/librats/dht/log.h +38 -0
  116. package/native-src/src/librats/dht/node.cpp +473 -0
  117. package/native-src/src/librats/dht/node.h +164 -0
  118. package/native-src/src/librats/dht/node_entry.h +81 -0
  119. package/native-src/src/librats/dht/observer.h +72 -0
  120. package/native-src/src/librats/dht/persistence.cpp +90 -0
  121. package/native-src/src/librats/dht/persistence.h +32 -0
  122. package/native-src/src/librats/dht/routing_table.cpp +559 -0
  123. package/native-src/src/librats/dht/routing_table.h +185 -0
  124. package/native-src/src/librats/dht/rpc_manager.cpp +127 -0
  125. package/native-src/src/librats/dht/rpc_manager.h +77 -0
  126. package/native-src/src/librats/dht/storage.cpp +92 -0
  127. package/native-src/src/librats/dht/storage.h +74 -0
  128. package/native-src/src/librats/dht/transport.h +27 -0
  129. package/native-src/src/librats/dht/traversal.cpp +326 -0
  130. package/native-src/src/librats/dht/traversal.h +120 -0
  131. package/native-src/src/librats/dht/udp_transport.cpp +49 -0
  132. package/native-src/src/librats/dht/udp_transport.h +51 -0
  133. package/native-src/src/librats/mdns/log.h +22 -0
  134. package/native-src/src/{mdns.cpp → librats/mdns/mdns.cpp} +75 -40
  135. package/native-src/src/{mdns.h → librats/mdns/mdns.h} +9 -8
  136. package/native-src/src/{natpmp.cpp → librats/nat/natpmp.cpp} +12 -9
  137. package/native-src/src/{natpmp.h → librats/nat/natpmp.h} +3 -3
  138. package/native-src/src/{port_mapping.h → librats/nat/port_mapping.h} +3 -2
  139. package/native-src/src/{stun.cpp → librats/nat/stun.cpp} +4 -4
  140. package/native-src/src/{stun.h → librats/nat/stun.h} +1 -1
  141. package/native-src/src/{upnp.cpp → librats/nat/upnp.cpp} +6 -6
  142. package/native-src/src/{upnp.h → librats/nat/upnp.h} +2 -2
  143. package/native-src/src/librats/node/circuit_service.h +84 -0
  144. package/native-src/src/librats/node/config.h +110 -0
  145. package/native-src/src/librats/node/dial_service.h +54 -0
  146. package/native-src/src/librats/node/dialer.cpp +264 -0
  147. package/native-src/src/librats/node/dialer.h +188 -0
  148. package/native-src/src/librats/node/host_events.h +26 -0
  149. package/native-src/src/librats/node/identify.cpp +130 -0
  150. package/native-src/src/librats/node/identify.h +71 -0
  151. package/native-src/src/librats/node/nat_status.cpp +103 -0
  152. package/native-src/src/librats/node/nat_status.h +118 -0
  153. package/native-src/src/librats/node/node.cpp +865 -0
  154. package/native-src/src/librats/node/node.h +344 -0
  155. package/native-src/src/librats/node/node_context.h +33 -0
  156. package/native-src/src/librats/node/peer_network.h +91 -0
  157. package/native-src/src/librats/peer/peer.h +49 -0
  158. package/native-src/src/librats/peer/peer_book.cpp +181 -0
  159. package/native-src/src/librats/peer/peer_book.h +88 -0
  160. package/native-src/src/librats/peer/peer_id.cpp +72 -0
  161. package/native-src/src/librats/peer/peer_id.h +62 -0
  162. package/native-src/src/librats/peer/peer_info.h +37 -0
  163. package/native-src/src/librats/peer/peer_table.cpp +170 -0
  164. package/native-src/src/librats/peer/peer_table.h +148 -0
  165. package/native-src/src/librats/security/handshaker.h +66 -0
  166. package/native-src/src/librats/security/identity.h +43 -0
  167. package/native-src/src/librats/security/noise_security.cpp +122 -0
  168. package/native-src/src/librats/security/noise_security.h +37 -0
  169. package/native-src/src/librats/security/plaintext_security.h +106 -0
  170. package/native-src/src/librats/security/session.h +37 -0
  171. package/native-src/src/{storage.cpp → librats/storage/storage.cpp} +369 -522
  172. package/native-src/src/{storage.h → librats/storage/storage.h} +135 -299
  173. package/native-src/src/librats/subsystems/bittorrent.cpp +211 -0
  174. package/native-src/src/librats/subsystems/bittorrent.h +136 -0
  175. package/native-src/src/librats/subsystems/dht_discovery.cpp +202 -0
  176. package/native-src/src/librats/subsystems/dht_discovery.h +123 -0
  177. package/native-src/src/librats/subsystems/dht_service.h +36 -0
  178. package/native-src/src/librats/subsystems/file_transfer.cpp +972 -0
  179. package/native-src/src/librats/subsystems/file_transfer.h +367 -0
  180. package/native-src/src/librats/subsystems/hole_punch.cpp +605 -0
  181. package/native-src/src/librats/subsystems/hole_punch.h +290 -0
  182. package/native-src/src/librats/subsystems/hole_punch_service.h +38 -0
  183. package/native-src/src/librats/subsystems/mdns_discovery.cpp +66 -0
  184. package/native-src/src/librats/subsystems/mdns_discovery.h +55 -0
  185. package/native-src/src/librats/subsystems/message_json.cpp +112 -0
  186. package/native-src/src/librats/subsystems/message_json.h +88 -0
  187. package/native-src/src/librats/subsystems/peer_exchange.cpp +241 -0
  188. package/native-src/src/librats/subsystems/peer_exchange.h +136 -0
  189. package/native-src/src/librats/subsystems/ping_service.cpp +98 -0
  190. package/native-src/src/librats/subsystems/ping_service.h +66 -0
  191. package/native-src/src/librats/subsystems/port_mapping_service.cpp +192 -0
  192. package/native-src/src/librats/subsystems/port_mapping_service.h +84 -0
  193. package/native-src/src/librats/subsystems/pubsub.cpp +567 -0
  194. package/native-src/src/librats/subsystems/pubsub.h +175 -0
  195. package/native-src/src/librats/subsystems/reconnection.cpp +239 -0
  196. package/native-src/src/librats/subsystems/reconnection.h +126 -0
  197. package/native-src/src/librats/subsystems/relay.cpp +1142 -0
  198. package/native-src/src/librats/subsystems/relay.h +211 -0
  199. package/native-src/src/librats/subsystems/relay_service.h +46 -0
  200. package/native-src/src/librats/transport/connection.cpp +343 -0
  201. package/native-src/src/librats/transport/connection.h +283 -0
  202. package/native-src/src/librats/transport/link.h +96 -0
  203. package/native-src/src/librats/transport/reactor.cpp +588 -0
  204. package/native-src/src/librats/transport/reactor.h +262 -0
  205. package/native-src/src/librats/transport/reactor_pool.h +81 -0
  206. package/native-src/src/librats/transport/relay_link.cpp +208 -0
  207. package/native-src/src/librats/transport/relay_link.h +303 -0
  208. package/native-src/src/librats/transport/tcp_link.cpp +49 -0
  209. package/native-src/src/librats/transport/tcp_link.h +43 -0
  210. package/native-src/src/librats/transport/udp_mux.cpp +617 -0
  211. package/native-src/src/librats/transport/udp_mux.h +363 -0
  212. package/native-src/src/librats/transport/udp_packet.cpp +121 -0
  213. package/native-src/src/librats/transport/udp_packet.h +190 -0
  214. package/native-src/src/librats/transport/udp_stream.cpp +1194 -0
  215. package/native-src/src/librats/transport/udp_stream.h +614 -0
  216. package/native-src/src/librats/util/features.h.in +51 -0
  217. package/native-src/src/{fs.cpp → librats/util/fs.cpp} +51 -3
  218. package/native-src/src/librats/util/fs.h +136 -0
  219. package/native-src/src/librats/util/json.cpp +1002 -0
  220. package/native-src/src/librats/util/json.h +444 -0
  221. package/native-src/src/{logger.cpp → librats/util/logger.cpp} +1 -1
  222. package/native-src/src/{logger.h → librats/util/logger.h} +43 -31
  223. package/native-src/src/{network_monitor.cpp → librats/util/network_monitor.cpp} +12 -4
  224. package/native-src/src/{network_monitor.h → librats/util/network_monitor.h} +2 -1
  225. package/native-src/src/{network_utils.cpp → librats/util/network_utils.cpp} +38 -23
  226. package/native-src/src/{network_utils.h → librats/util/network_utils.h} +15 -8
  227. package/native-src/src/{os.cpp → librats/util/os.cpp} +48 -18
  228. package/native-src/src/librats/util/rats_export.h +69 -0
  229. package/native-src/src/{version.cpp → librats/util/version.cpp} +2 -2
  230. package/native-src/src/{version.h.in → librats/util/version.h.in} +1 -1
  231. package/native-src/src/librats/wire/frame.cpp +76 -0
  232. package/native-src/src/librats/wire/frame.h +111 -0
  233. package/native-src/src/librats/wire/message_router.cpp +45 -0
  234. package/native-src/src/librats/wire/message_router.h +47 -0
  235. package/package.json +5 -4
  236. package/scripts/build-librats.js +1 -0
  237. package/scripts/postinstall.js +3 -3
  238. package/scripts/prepare-package.js +4 -4
  239. package/scripts/verify-installation.js +63 -105
  240. package/src/librats_node.cpp +1067 -1323
  241. package/native-src/src/bencode.cpp +0 -485
  242. package/native-src/src/bencode.h +0 -145
  243. package/native-src/src/bittorrent.cpp +0 -14
  244. package/native-src/src/bittorrent.h +0 -74
  245. package/native-src/src/bt_bitfield.cpp +0 -372
  246. package/native-src/src/bt_bitfield.h +0 -316
  247. package/native-src/src/bt_choker.cpp +0 -228
  248. package/native-src/src/bt_choker.h +0 -147
  249. package/native-src/src/bt_client.cpp +0 -1047
  250. package/native-src/src/bt_client.h +0 -445
  251. package/native-src/src/bt_create_torrent.cpp +0 -677
  252. package/native-src/src/bt_create_torrent.h +0 -473
  253. package/native-src/src/bt_extension.cpp +0 -469
  254. package/native-src/src/bt_extension.h +0 -309
  255. package/native-src/src/bt_file_storage.cpp +0 -261
  256. package/native-src/src/bt_file_storage.h +0 -298
  257. package/native-src/src/bt_handshake.cpp +0 -134
  258. package/native-src/src/bt_handshake.h +0 -157
  259. package/native-src/src/bt_messages.cpp +0 -364
  260. package/native-src/src/bt_messages.h +0 -324
  261. package/native-src/src/bt_network.cpp +0 -1007
  262. package/native-src/src/bt_network.h +0 -417
  263. package/native-src/src/bt_peer_connection.cpp +0 -742
  264. package/native-src/src/bt_peer_connection.h +0 -592
  265. package/native-src/src/bt_piece_picker.cpp +0 -786
  266. package/native-src/src/bt_piece_picker.h +0 -473
  267. package/native-src/src/bt_resume_data.cpp +0 -410
  268. package/native-src/src/bt_resume_data.h +0 -249
  269. package/native-src/src/bt_torrent.cpp +0 -2120
  270. package/native-src/src/bt_torrent.h +0 -641
  271. package/native-src/src/bt_torrent_info.cpp +0 -659
  272. package/native-src/src/bt_torrent_info.h +0 -418
  273. package/native-src/src/bt_types.h +0 -621
  274. package/native-src/src/chained_send_buffer.cpp +0 -75
  275. package/native-src/src/chained_send_buffer.h +0 -137
  276. package/native-src/src/crypto/hkdf.c +0 -266
  277. package/native-src/src/crypto/poly1305.h +0 -36
  278. package/native-src/src/dht.cpp +0 -3311
  279. package/native-src/src/dht.h +0 -717
  280. package/native-src/src/disk_io.cpp +0 -632
  281. package/native-src/src/disk_io.h +0 -315
  282. package/native-src/src/file_transfer.cpp +0 -1415
  283. package/native-src/src/file_transfer.h +0 -286
  284. package/native-src/src/fs.h +0 -108
  285. package/native-src/src/gossipsub.cpp +0 -1139
  286. package/native-src/src/gossipsub.h +0 -403
  287. package/native-src/src/ice.cpp +0 -893
  288. package/native-src/src/ice.h +0 -559
  289. package/native-src/src/json.hpp +0 -25526
  290. package/native-src/src/librats.cpp +0 -2378
  291. package/native-src/src/librats.h +0 -2324
  292. package/native-src/src/librats_bittorrent.cpp +0 -601
  293. package/native-src/src/librats_c.cpp +0 -1557
  294. package/native-src/src/librats_c.h +0 -323
  295. package/native-src/src/librats_discovery.cpp +0 -402
  296. package/native-src/src/librats_encryption.cpp +0 -275
  297. package/native-src/src/librats_file_transfer.cpp +0 -144
  298. package/native-src/src/librats_gossipsub.cpp +0 -289
  299. package/native-src/src/librats_ice.cpp +0 -213
  300. package/native-src/src/librats_log_macros.h +0 -36
  301. package/native-src/src/librats_logging.cpp +0 -173
  302. package/native-src/src/librats_mdns.cpp +0 -166
  303. package/native-src/src/librats_persistence.cpp +0 -796
  304. package/native-src/src/librats_portmap.cpp +0 -419
  305. package/native-src/src/librats_reconnection.cpp +0 -218
  306. package/native-src/src/librats_statistic.cpp +0 -105
  307. package/native-src/src/librats_storage.cpp +0 -189
  308. package/native-src/src/rats_export.h +0 -17
  309. package/native-src/src/receive_buffer.cpp +0 -82
  310. package/native-src/src/receive_buffer.h +0 -127
  311. package/native-src/src/socket.h +0 -228
  312. package/native-src/src/threadmanager.cpp +0 -105
  313. package/native-src/src/threadmanager.h +0 -53
  314. package/native-src/src/tracker.cpp +0 -1264
  315. package/native-src/src/tracker.h +0 -319
  316. package/native-src/src/turn.cpp +0 -762
  317. package/native-src/src/turn.h +0 -460
  318. package/native-src/src/wakeup_pipe.h +0 -60
  319. /package/native-src/src/{os.h → librats/util/os.h} +0 -0
@@ -1,3 +1,4 @@
1
+ /* SPDX-License-Identifier: MIT */
1
2
  /*
2
3
  * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
3
4
  *
@@ -23,7 +24,7 @@
23
24
  /* Essentially the same as D. J. Bernstein's reference implementation
24
25
  but with an option to use SIMD instructions for 4-way parallelism. */
25
26
 
26
- #include "chacha.h"
27
+ #include "librats/crypto/chacha.h"
27
28
  #include <string.h>
28
29
 
29
30
  #define fromLittle(ptr) \
@@ -38,12 +39,12 @@
38
39
  (ptr)[2] = (uint8_t)((v) >> 16), \
39
40
  (ptr)[3] = (uint8_t)((v) >> 24))
40
41
 
41
- #ifdef CHACHA_USE_VECTOR_MATH
42
+ #ifdef RATS_CHACHA_USE_VECTOR_MATH
42
43
 
43
44
  /* Assumption: CPU is little-endian and can perform unaligned
44
45
  32-bit loads and stores */
45
46
  #define fromLittleVec(ptr) \
46
- (ChaChaVectorUInt32){((const uint32_t *)(ptr))[0], \
47
+ (RatsChaChaVectorUInt32){((const uint32_t *)(ptr))[0], \
47
48
  ((const uint32_t *)(ptr))[1], \
48
49
  ((const uint32_t *)(ptr))[2], \
49
50
  ((const uint32_t *)(ptr))[3]}
@@ -71,13 +72,13 @@
71
72
  (b) = leftRotate((b) ^ (c), 7); \
72
73
  } while (0)
73
74
 
74
- void chacha_keysetup(chacha_ctx *x, const uint8_t *k, uint32_t kbits)
75
+ void rats_chacha_keysetup(rats_chacha_ctx *x, const uint8_t *k, uint32_t kbits)
75
76
  {
76
77
  static const char tag128[] = "expand 16-byte k";
77
78
  static const char tag256[] = "expand 32-byte k";
78
79
  if (kbits == 256) {
79
80
  memcpy(x->input, tag256, 16);
80
- #ifdef CHACHA_USE_VECTOR_MATH
81
+ #ifdef RATS_CHACHA_USE_VECTOR_MATH
81
82
  x->input[1] = fromLittleVec(k);
82
83
  x->input[2] = fromLittleVec(k + 16);
83
84
  #else
@@ -92,7 +93,7 @@ void chacha_keysetup(chacha_ctx *x, const uint8_t *k, uint32_t kbits)
92
93
  #endif
93
94
  } else {
94
95
  memcpy(x->input, tag128, 16);
95
- #ifdef CHACHA_USE_VECTOR_MATH
96
+ #ifdef RATS_CHACHA_USE_VECTOR_MATH
96
97
  x->input[2] = x->input[1] = fromLittleVec(k);
97
98
  #else
98
99
  x->input[8] = x->input[4] = fromLittle(k);
@@ -101,23 +102,23 @@ void chacha_keysetup(chacha_ctx *x, const uint8_t *k, uint32_t kbits)
101
102
  x->input[11] = x->input[7] = fromLittle(k + 12);
102
103
  #endif
103
104
  }
104
- #ifdef CHACHA_USE_VECTOR_MATH
105
- x->input[3] = (ChaChaVectorUInt32){0, 0, 0, 0};
105
+ #ifdef RATS_CHACHA_USE_VECTOR_MATH
106
+ x->input[3] = (RatsChaChaVectorUInt32){0, 0, 0, 0};
106
107
  #else
107
108
  memset(x->input + 12, 0, 16);
108
109
  #endif
109
110
  }
110
111
 
111
- void chacha_ivsetup(chacha_ctx *x, const uint8_t *iv, const uint8_t *counter)
112
+ void rats_chacha_ivsetup(rats_chacha_ctx *x, const uint8_t *iv, const uint8_t *counter)
112
113
  {
113
- #ifdef CHACHA_USE_VECTOR_MATH
114
+ #ifdef RATS_CHACHA_USE_VECTOR_MATH
114
115
  if (counter) {
115
- x->input[3] = (ChaChaVectorUInt32){fromLittle(counter),
116
+ x->input[3] = (RatsChaChaVectorUInt32){fromLittle(counter),
116
117
  fromLittle(counter + 4),
117
118
  fromLittle(iv),
118
119
  fromLittle(iv + 4)};
119
120
  } else {
120
- x->input[3] = (ChaChaVectorUInt32){0, 0, fromLittle(iv), fromLittle(iv + 4)};
121
+ x->input[3] = (RatsChaChaVectorUInt32){0, 0, fromLittle(iv), fromLittle(iv + 4)};
121
122
  }
122
123
  #else
123
124
  if (counter) {
@@ -132,15 +133,15 @@ void chacha_ivsetup(chacha_ctx *x, const uint8_t *iv, const uint8_t *counter)
132
133
  #endif
133
134
  }
134
135
 
135
- #ifdef CHACHA_USE_VECTOR_MATH
136
+ #ifdef RATS_CHACHA_USE_VECTOR_MATH
136
137
 
137
- #define shuffleLeft1(x) (ChaChaVectorUInt32){(x)[1], (x)[2], (x)[3], (x)[0]}
138
- #define shuffleLeft2(x) (ChaChaVectorUInt32){(x)[2], (x)[3], (x)[0], (x)[1]}
139
- #define shuffleLeft3(x) (ChaChaVectorUInt32){(x)[3], (x)[0], (x)[1], (x)[2]}
138
+ #define shuffleLeft1(x) (RatsChaChaVectorUInt32){(x)[1], (x)[2], (x)[3], (x)[0]}
139
+ #define shuffleLeft2(x) (RatsChaChaVectorUInt32){(x)[2], (x)[3], (x)[0], (x)[1]}
140
+ #define shuffleLeft3(x) (RatsChaChaVectorUInt32){(x)[3], (x)[0], (x)[1], (x)[2]}
140
141
 
141
- void chacha_encrypt_bytes(chacha_ctx *x, const uint8_t *m, uint8_t *c, uint32_t bytes)
142
+ void rats_chacha_encrypt_bytes(rats_chacha_ctx *x, const uint8_t *m, uint8_t *c, uint32_t bytes)
142
143
  {
143
- ChaChaVectorUInt32 out0, out1, out2, out3;
144
+ RatsChaChaVectorUInt32 out0, out1, out2, out3;
144
145
  uint32_t index;
145
146
  uint8_t temp[64];
146
147
  uint8_t *t;
@@ -204,9 +205,9 @@ void chacha_encrypt_bytes(chacha_ctx *x, const uint8_t *m, uint8_t *c, uint32_t
204
205
  }
205
206
  }
206
207
 
207
- #else /* !CHACHA_USE_VECTOR_MATH */
208
+ #else /* !RATS_CHACHA_USE_VECTOR_MATH */
208
209
 
209
- void chacha_encrypt_bytes(chacha_ctx *x, const uint8_t *m, uint8_t *c, uint32_t bytes)
210
+ void rats_chacha_encrypt_bytes(rats_chacha_ctx *x, const uint8_t *m, uint8_t *c, uint32_t bytes)
210
211
  {
211
212
  uint32_t in0, in1, in2, in3, in4, in5, in6, in7;
212
213
  uint32_t in8, in9, in10, in11, in12, in13, in14, in15;
@@ -309,4 +310,4 @@ void chacha_encrypt_bytes(chacha_ctx *x, const uint8_t *m, uint8_t *c, uint32_t
309
310
  x->input[13] = in13;
310
311
  }
311
312
 
312
- #endif /* !CHACHA_USE_VECTOR_MATH */
313
+ #endif /* !RATS_CHACHA_USE_VECTOR_MATH */
@@ -1,3 +1,4 @@
1
+ /* SPDX-License-Identifier: MIT */
1
2
  /*
2
3
  * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
3
4
  *
@@ -30,34 +31,34 @@
30
31
  extern "C" {
31
32
  #endif
32
33
 
33
- #define CHACHA_KEY_SIZE 32
34
- #define CHACHA_NONCE_SIZE 8
35
- #define CHACHA_BLOCK_SIZE 64
34
+ #define RATS_CHACHA_KEY_SIZE 32
35
+ #define RATS_CHACHA_NONCE_SIZE 8
36
+ #define RATS_CHACHA_BLOCK_SIZE 64
36
37
 
37
38
  #if defined(__SSE2__) && defined(__GNUC__) && __GNUC__ >= 4
38
- #define CHACHA_USE_VECTOR_MATH 1
39
+ #define RATS_CHACHA_USE_VECTOR_MATH 1
39
40
  #ifdef __clang__
40
- typedef uint32_t ChaChaVectorUInt32 __attribute__((ext_vector_type(4)));
41
+ typedef uint32_t RatsChaChaVectorUInt32 __attribute__((ext_vector_type(4)));
41
42
  #else
42
- typedef uint32_t ChaChaVectorUInt32 __attribute__((__vector_size__(16)));
43
+ typedef uint32_t RatsChaChaVectorUInt32 __attribute__((__vector_size__(16)));
43
44
  #endif
44
45
  #else
45
- #undef CHACHA_USE_VECTOR_MATH
46
+ #undef RATS_CHACHA_USE_VECTOR_MATH
46
47
  #endif
47
48
 
48
49
  typedef struct
49
50
  {
50
- #ifdef CHACHA_USE_VECTOR_MATH
51
- ChaChaVectorUInt32 input[4];
51
+ #ifdef RATS_CHACHA_USE_VECTOR_MATH
52
+ RatsChaChaVectorUInt32 input[4];
52
53
  #else
53
54
  uint32_t input[16];
54
55
  #endif
55
56
 
56
- } chacha_ctx;
57
+ } rats_chacha_ctx;
57
58
 
58
- void chacha_keysetup(chacha_ctx *x, const uint8_t *k, uint32_t kbits);
59
- void chacha_ivsetup(chacha_ctx *x, const uint8_t *iv, const uint8_t *counter);
60
- void chacha_encrypt_bytes(chacha_ctx *x, const uint8_t *m, uint8_t *c, uint32_t bytes);
59
+ void rats_chacha_keysetup(rats_chacha_ctx *x, const uint8_t *k, uint32_t kbits);
60
+ void rats_chacha_ivsetup(rats_chacha_ctx *x, const uint8_t *iv, const uint8_t *counter);
61
+ void rats_chacha_encrypt_bytes(rats_chacha_ctx *x, const uint8_t *m, uint8_t *c, uint32_t bytes);
61
62
 
62
63
  #ifdef __cplusplus
63
64
  }
@@ -3,9 +3,9 @@
3
3
  * Based on RFC 8439
4
4
  */
5
5
 
6
- #include "chachapoly.h"
7
- #include "chacha.h"
8
- #include "poly1305.h"
6
+ #include "librats/crypto/chachapoly.h"
7
+ #include "librats/crypto/chacha.h"
8
+ #include "librats/crypto/poly1305.h"
9
9
  #include <string.h>
10
10
 
11
11
  /* Write 64-bit nonce counter to 12-byte Noise nonce format (currently unused, reserved for future use) */
@@ -57,80 +57,80 @@ static void secure_zero(void *ptr, size_t len) {
57
57
  }
58
58
 
59
59
  /* Pad to 16-byte boundary for Poly1305 */
60
- static void poly1305_pad16(poly1305_context *ctx, size_t data_len) {
60
+ static void rats_poly1305_pad16(rats_poly1305_context *ctx, size_t data_len) {
61
61
  static const uint8_t zeros[16] = {0};
62
62
  size_t pad = (16 - (data_len % 16)) % 16;
63
63
  if (pad > 0) {
64
- poly1305_update(ctx, zeros, pad);
64
+ rats_poly1305_update(ctx, zeros, pad);
65
65
  }
66
66
  }
67
67
 
68
- size_t chachapoly_encrypt(
69
- const uint8_t key[CHACHAPOLY_KEY_SIZE],
70
- const uint8_t nonce[CHACHAPOLY_NONCE_SIZE],
68
+ size_t rats_chachapoly_encrypt(
69
+ const uint8_t key[RATS_CHACHAPOLY_KEY_SIZE],
70
+ const uint8_t nonce[RATS_CHACHAPOLY_NONCE_SIZE],
71
71
  const uint8_t *ad, size_t ad_len,
72
72
  const uint8_t *plaintext, size_t pt_len,
73
73
  uint8_t *ciphertext
74
74
  ) {
75
- chacha_ctx chacha;
76
- poly1305_context poly;
75
+ rats_chacha_ctx chacha;
76
+ rats_poly1305_context poly;
77
77
  uint8_t poly_key[64];
78
78
  uint8_t block0_counter[8] = {0};
79
79
  uint8_t block1_counter[8] = {1, 0, 0, 0, 0, 0, 0, 0};
80
80
  uint8_t len_block[16];
81
81
 
82
82
  /* Generate Poly1305 key from first ChaCha20 block (counter = 0) */
83
- chacha_keysetup(&chacha, key, 256);
84
- chacha_ivsetup(&chacha, nonce + 4, block0_counter);
83
+ rats_chacha_keysetup(&chacha, key, 256);
84
+ rats_chacha_ivsetup(&chacha, nonce + 4, block0_counter);
85
85
  memset(poly_key, 0, 64);
86
- chacha_encrypt_bytes(&chacha, poly_key, poly_key, 64);
86
+ rats_chacha_encrypt_bytes(&chacha, poly_key, poly_key, 64);
87
87
 
88
88
  /* Encrypt plaintext with ChaCha20 (counter = 1) */
89
- chacha_keysetup(&chacha, key, 256);
90
- chacha_ivsetup(&chacha, nonce + 4, block1_counter);
89
+ rats_chacha_keysetup(&chacha, key, 256);
90
+ rats_chacha_ivsetup(&chacha, nonce + 4, block1_counter);
91
91
  if (pt_len > 0) {
92
- chacha_encrypt_bytes(&chacha, plaintext, ciphertext, (uint32_t)pt_len);
92
+ rats_chacha_encrypt_bytes(&chacha, plaintext, ciphertext, (uint32_t)pt_len);
93
93
  }
94
94
 
95
95
  /* Compute Poly1305 tag */
96
- poly1305_init(&poly, poly_key);
96
+ rats_poly1305_init(&poly, poly_key);
97
97
 
98
98
  /* Authenticate AD */
99
99
  if (ad_len > 0) {
100
- poly1305_update(&poly, ad, ad_len);
101
- poly1305_pad16(&poly, ad_len);
100
+ rats_poly1305_update(&poly, ad, ad_len);
101
+ rats_poly1305_pad16(&poly, ad_len);
102
102
  }
103
103
 
104
104
  /* Authenticate ciphertext */
105
105
  if (pt_len > 0) {
106
- poly1305_update(&poly, ciphertext, pt_len);
107
- poly1305_pad16(&poly, pt_len);
106
+ rats_poly1305_update(&poly, ciphertext, pt_len);
107
+ rats_poly1305_pad16(&poly, pt_len);
108
108
  }
109
109
 
110
110
  /* Authenticate lengths */
111
111
  write_le64(len_block, ad_len);
112
112
  write_le64(len_block + 8, pt_len);
113
- poly1305_update(&poly, len_block, 16);
113
+ rats_poly1305_update(&poly, len_block, 16);
114
114
 
115
115
  /* Output tag */
116
- poly1305_finish(&poly, ciphertext + pt_len);
116
+ rats_poly1305_finish(&poly, ciphertext + pt_len);
117
117
 
118
118
  /* Clean up sensitive data */
119
119
  secure_zero(&chacha, sizeof(chacha));
120
120
  secure_zero(poly_key, sizeof(poly_key));
121
121
 
122
- return pt_len + CHACHAPOLY_TAG_SIZE;
122
+ return pt_len + RATS_CHACHAPOLY_TAG_SIZE;
123
123
  }
124
124
 
125
- size_t chachapoly_decrypt(
126
- const uint8_t key[CHACHAPOLY_KEY_SIZE],
127
- const uint8_t nonce[CHACHAPOLY_NONCE_SIZE],
125
+ size_t rats_chachapoly_decrypt(
126
+ const uint8_t key[RATS_CHACHAPOLY_KEY_SIZE],
127
+ const uint8_t nonce[RATS_CHACHAPOLY_NONCE_SIZE],
128
128
  const uint8_t *ad, size_t ad_len,
129
129
  const uint8_t *ciphertext, size_t ct_len,
130
130
  uint8_t *plaintext
131
131
  ) {
132
- chacha_ctx chacha;
133
- poly1305_context poly;
132
+ rats_chacha_ctx chacha;
133
+ rats_poly1305_context poly;
134
134
  uint8_t poly_key[64];
135
135
  uint8_t block0_counter[8] = {0};
136
136
  uint8_t block1_counter[8] = {1, 0, 0, 0, 0, 0, 0, 0};
@@ -138,53 +138,53 @@ size_t chachapoly_decrypt(
138
138
  uint8_t computed_tag[16];
139
139
  size_t pt_len;
140
140
 
141
- if (ct_len < CHACHAPOLY_TAG_SIZE) {
142
- return 0;
141
+ if (ct_len < RATS_CHACHAPOLY_TAG_SIZE) {
142
+ return RATS_CHACHAPOLY_DECRYPT_FAILED;
143
143
  }
144
-
145
- pt_len = ct_len - CHACHAPOLY_TAG_SIZE;
144
+
145
+ pt_len = ct_len - RATS_CHACHAPOLY_TAG_SIZE;
146
146
 
147
147
  /* Generate Poly1305 key from first ChaCha20 block (counter = 0) */
148
- chacha_keysetup(&chacha, key, 256);
149
- chacha_ivsetup(&chacha, nonce + 4, block0_counter);
148
+ rats_chacha_keysetup(&chacha, key, 256);
149
+ rats_chacha_ivsetup(&chacha, nonce + 4, block0_counter);
150
150
  memset(poly_key, 0, 64);
151
- chacha_encrypt_bytes(&chacha, poly_key, poly_key, 64);
151
+ rats_chacha_encrypt_bytes(&chacha, poly_key, poly_key, 64);
152
152
 
153
153
  /* Compute Poly1305 tag */
154
- poly1305_init(&poly, poly_key);
154
+ rats_poly1305_init(&poly, poly_key);
155
155
 
156
156
  /* Authenticate AD */
157
157
  if (ad_len > 0) {
158
- poly1305_update(&poly, ad, ad_len);
159
- poly1305_pad16(&poly, ad_len);
158
+ rats_poly1305_update(&poly, ad, ad_len);
159
+ rats_poly1305_pad16(&poly, ad_len);
160
160
  }
161
161
 
162
162
  /* Authenticate ciphertext */
163
163
  if (pt_len > 0) {
164
- poly1305_update(&poly, ciphertext, pt_len);
165
- poly1305_pad16(&poly, pt_len);
164
+ rats_poly1305_update(&poly, ciphertext, pt_len);
165
+ rats_poly1305_pad16(&poly, pt_len);
166
166
  }
167
167
 
168
168
  /* Authenticate lengths */
169
169
  write_le64(len_block, ad_len);
170
170
  write_le64(len_block + 8, pt_len);
171
- poly1305_update(&poly, len_block, 16);
171
+ rats_poly1305_update(&poly, len_block, 16);
172
172
 
173
173
  /* Compute and verify tag */
174
- poly1305_finish(&poly, computed_tag);
174
+ rats_poly1305_finish(&poly, computed_tag);
175
175
 
176
- if (!secure_compare(computed_tag, ciphertext + pt_len, CHACHAPOLY_TAG_SIZE)) {
176
+ if (!secure_compare(computed_tag, ciphertext + pt_len, RATS_CHACHAPOLY_TAG_SIZE)) {
177
177
  secure_zero(&chacha, sizeof(chacha));
178
178
  secure_zero(poly_key, sizeof(poly_key));
179
179
  secure_zero(computed_tag, sizeof(computed_tag));
180
- return 0; /* Authentication failed */
180
+ return RATS_CHACHAPOLY_DECRYPT_FAILED; /* Authentication failed */
181
181
  }
182
182
 
183
183
  /* Decrypt ciphertext */
184
- chacha_keysetup(&chacha, key, 256);
185
- chacha_ivsetup(&chacha, nonce + 4, block1_counter);
184
+ rats_chacha_keysetup(&chacha, key, 256);
185
+ rats_chacha_ivsetup(&chacha, nonce + 4, block1_counter);
186
186
  if (pt_len > 0) {
187
- chacha_encrypt_bytes(&chacha, ciphertext, plaintext, (uint32_t)pt_len);
187
+ rats_chacha_encrypt_bytes(&chacha, ciphertext, plaintext, (uint32_t)pt_len);
188
188
  }
189
189
 
190
190
  /* Clean up sensitive data */
@@ -195,20 +195,20 @@ size_t chachapoly_decrypt(
195
195
  return pt_len;
196
196
  }
197
197
 
198
- size_t chachapoly_encrypt_inplace(
199
- const uint8_t key[CHACHAPOLY_KEY_SIZE],
200
- const uint8_t nonce[CHACHAPOLY_NONCE_SIZE],
198
+ size_t rats_chachapoly_encrypt_inplace(
199
+ const uint8_t key[RATS_CHACHAPOLY_KEY_SIZE],
200
+ const uint8_t nonce[RATS_CHACHAPOLY_NONCE_SIZE],
201
201
  const uint8_t *ad, size_t ad_len,
202
202
  uint8_t *data, size_t data_len
203
203
  ) {
204
- return chachapoly_encrypt(key, nonce, ad, ad_len, data, data_len, data);
204
+ return rats_chachapoly_encrypt(key, nonce, ad, ad_len, data, data_len, data);
205
205
  }
206
206
 
207
- size_t chachapoly_decrypt_inplace(
208
- const uint8_t key[CHACHAPOLY_KEY_SIZE],
209
- const uint8_t nonce[CHACHAPOLY_NONCE_SIZE],
207
+ size_t rats_chachapoly_decrypt_inplace(
208
+ const uint8_t key[RATS_CHACHAPOLY_KEY_SIZE],
209
+ const uint8_t nonce[RATS_CHACHAPOLY_NONCE_SIZE],
210
210
  const uint8_t *ad, size_t ad_len,
211
211
  uint8_t *data, size_t data_len
212
212
  ) {
213
- return chachapoly_decrypt(key, nonce, ad, ad_len, data, data_len, data);
213
+ return rats_chachapoly_decrypt(key, nonce, ad, ad_len, data, data_len, data);
214
214
  }
@@ -13,9 +13,14 @@
13
13
  extern "C" {
14
14
  #endif
15
15
 
16
- #define CHACHAPOLY_KEY_SIZE 32
17
- #define CHACHAPOLY_NONCE_SIZE 12
18
- #define CHACHAPOLY_TAG_SIZE 16
16
+ #define RATS_CHACHAPOLY_KEY_SIZE 32
17
+ #define RATS_CHACHAPOLY_NONCE_SIZE 12
18
+ #define RATS_CHACHAPOLY_TAG_SIZE 16
19
+
20
+ /* Sentinel returned by the decrypt functions on authentication failure.
21
+ * A successful decryption returns the plaintext length, which may legitimately
22
+ * be 0 (an empty-but-authenticated message), so failure needs a distinct value. */
23
+ #define RATS_CHACHAPOLY_DECRYPT_FAILED ((size_t)-1)
19
24
 
20
25
  /**
21
26
  * Encrypt plaintext with ChaCha20-Poly1305 AEAD
@@ -29,9 +34,9 @@ extern "C" {
29
34
  * @param ciphertext Output buffer (must be at least pt_len + 16 bytes)
30
35
  * @return Length of ciphertext (pt_len + 16) on success, 0 on failure
31
36
  */
32
- size_t chachapoly_encrypt(
33
- const uint8_t key[CHACHAPOLY_KEY_SIZE],
34
- const uint8_t nonce[CHACHAPOLY_NONCE_SIZE],
37
+ size_t rats_chachapoly_encrypt(
38
+ const uint8_t key[RATS_CHACHAPOLY_KEY_SIZE],
39
+ const uint8_t nonce[RATS_CHACHAPOLY_NONCE_SIZE],
35
40
  const uint8_t *ad, size_t ad_len,
36
41
  const uint8_t *plaintext, size_t pt_len,
37
42
  uint8_t *ciphertext
@@ -47,11 +52,12 @@ size_t chachapoly_encrypt(
47
52
  * @param ciphertext Input ciphertext (plaintext + 16-byte tag)
48
53
  * @param ct_len Length of ciphertext (must be >= 16)
49
54
  * @param plaintext Output buffer (must be at least ct_len - 16 bytes)
50
- * @return Length of plaintext (ct_len - 16) on success, 0 on failure
55
+ * @return Length of plaintext (ct_len - 16) on success (may be 0 for an
56
+ * empty authenticated message), RATS_CHACHAPOLY_DECRYPT_FAILED on failure
51
57
  */
52
- size_t chachapoly_decrypt(
53
- const uint8_t key[CHACHAPOLY_KEY_SIZE],
54
- const uint8_t nonce[CHACHAPOLY_NONCE_SIZE],
58
+ size_t rats_chachapoly_decrypt(
59
+ const uint8_t key[RATS_CHACHAPOLY_KEY_SIZE],
60
+ const uint8_t nonce[RATS_CHACHAPOLY_NONCE_SIZE],
55
61
  const uint8_t *ad, size_t ad_len,
56
62
  const uint8_t *ciphertext, size_t ct_len,
57
63
  uint8_t *plaintext
@@ -69,9 +75,9 @@ size_t chachapoly_decrypt(
69
75
  * @param data_len Length of plaintext
70
76
  * @return Length of output (data_len + 16) on success, 0 on failure
71
77
  */
72
- size_t chachapoly_encrypt_inplace(
73
- const uint8_t key[CHACHAPOLY_KEY_SIZE],
74
- const uint8_t nonce[CHACHAPOLY_NONCE_SIZE],
78
+ size_t rats_chachapoly_encrypt_inplace(
79
+ const uint8_t key[RATS_CHACHAPOLY_KEY_SIZE],
80
+ const uint8_t nonce[RATS_CHACHAPOLY_NONCE_SIZE],
75
81
  const uint8_t *ad, size_t ad_len,
76
82
  uint8_t *data, size_t data_len
77
83
  );
@@ -85,11 +91,12 @@ size_t chachapoly_encrypt_inplace(
85
91
  * @param ad_len Length of additional data
86
92
  * @param data Data buffer (ciphertext + tag in, plaintext out)
87
93
  * @param data_len Length of ciphertext including tag (must be >= 16)
88
- * @return Length of plaintext (data_len - 16) on success, 0 on failure
94
+ * @return Length of plaintext (data_len - 16) on success (may be 0),
95
+ * RATS_CHACHAPOLY_DECRYPT_FAILED on failure
89
96
  */
90
- size_t chachapoly_decrypt_inplace(
91
- const uint8_t key[CHACHAPOLY_KEY_SIZE],
92
- const uint8_t nonce[CHACHAPOLY_NONCE_SIZE],
97
+ size_t rats_chachapoly_decrypt_inplace(
98
+ const uint8_t key[RATS_CHACHAPOLY_KEY_SIZE],
99
+ const uint8_t nonce[RATS_CHACHAPOLY_NONCE_SIZE],
93
100
  const uint8_t *ad, size_t ad_len,
94
101
  uint8_t *data, size_t data_len
95
102
  );
@@ -1,4 +1,4 @@
1
- #include "crc32.h"
1
+ #include "librats/crypto/crc32.h"
2
2
 
3
3
  namespace librats {
4
4
 
@@ -1,6 +1,8 @@
1
1
  #ifndef LIBRATS_CRC32_H
2
2
  #define LIBRATS_CRC32_H
3
3
 
4
+ #include "librats/util/rats_export.h"
5
+
4
6
  #include <cstdint>
5
7
  #include <cstddef>
6
8
  #include <vector>
@@ -25,7 +27,7 @@ namespace librats {
25
27
  * uint32_t checksum2 = CRC32::calculate("Hello World", 11);
26
28
  * @endcode
27
29
  */
28
- class CRC32 {
30
+ class RATS_API CRC32 {
29
31
  public:
30
32
  /**
31
33
  * @brief Construct a new CRC32 object with initial state
@@ -1,3 +1,4 @@
1
+ /* SPDX-License-Identifier: BSD-3-Clause */
1
2
  /* Copyright 2008, Google Inc.
2
3
  * All rights reserved.
3
4
  *
@@ -45,11 +46,12 @@
45
46
  * uses many of the tricks described therein. Only the crecip function is taken
46
47
  * from the sample implementation. */
47
48
 
48
- #include "curve25519.h"
49
+ #include "librats/crypto/curve25519.h"
49
50
  #include <string.h>
50
51
 
51
- /* The Curve25519 basepoint */
52
- const uint8_t curve25519_basepoint[32] = {9};
52
+ /* The Curve25519 basepoint. RATS_API here is what puts the symbol in the DLL:
53
+ * unlike functions, exported data needs the attribute on the definition too. */
54
+ RATS_API const uint8_t rats_curve25519_basepoint[32] = {9};
53
55
 
54
56
  #ifdef _MSC_VER
55
57
  #define inline __inline
@@ -844,7 +846,7 @@ crecip(limb *out, const limb *z) {
844
846
  }
845
847
 
846
848
  int
847
- curve25519_donna(u8 *mypublic, const u8 *secret, const u8 *basepoint) {
849
+ rats_curve25519_donna(u8 *mypublic, const u8 *secret, const u8 *basepoint) {
848
850
  limb bp[10], x[10], z[11], zmone[10];
849
851
  uint8_t e[32];
850
852
  int i;
@@ -1,3 +1,4 @@
1
+ /* SPDX-License-Identifier: BSD-3-Clause */
1
2
  /* Copyright 2008, Google Inc.
2
3
  * All rights reserved.
3
4
  *
@@ -37,13 +38,15 @@
37
38
  #ifndef LIBRATS_CURVE25519_H
38
39
  #define LIBRATS_CURVE25519_H
39
40
 
41
+ #include "librats/util/rats_export.h"
42
+
40
43
  #include <stdint.h>
41
44
 
42
45
  #ifdef __cplusplus
43
46
  extern "C" {
44
47
  #endif
45
48
 
46
- #define CURVE25519_KEY_SIZE 32
49
+ #define RATS_CURVE25519_KEY_SIZE 32
47
50
 
48
51
  /**
49
52
  * Computes a Curve25519 shared secret or public key.
@@ -53,13 +56,13 @@ extern "C" {
53
56
  * @param basepoint Public key of the other party, or the Curve25519 basepoint
54
57
  * @return 0 on success
55
58
  */
56
- int curve25519_donna(uint8_t *mypublic, const uint8_t *secret, const uint8_t *basepoint);
59
+ int rats_curve25519_donna(uint8_t *mypublic, const uint8_t *secret, const uint8_t *basepoint);
57
60
 
58
61
  /**
59
62
  * The Curve25519 basepoint (9).
60
63
  * Use this as basepoint to derive a public key from a private key.
61
64
  */
62
- extern const uint8_t curve25519_basepoint[32];
65
+ RATS_API extern const uint8_t rats_curve25519_basepoint[32];
63
66
 
64
67
  #ifdef __cplusplus
65
68
  }