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.
- package/README.md +145 -331
- package/binding.gyp +16 -3
- package/lib/index.d.ts +288 -696
- package/lib/index.js +407 -44
- package/native-src/3rdparty/android/ifaddrs-android.c +1 -0
- package/native-src/3rdparty/android/ifaddrs-android.h +1 -0
- package/native-src/CMakeLists.txt +404 -179
- package/native-src/LICENSE +1 -1
- package/native-src/src/librats/bindings/rats.cpp +762 -0
- package/native-src/src/librats/bindings/rats.h +380 -0
- package/native-src/src/librats/bittorrent/bencode.cpp +437 -0
- package/native-src/src/librats/bittorrent/bencode.h +176 -0
- package/native-src/src/librats/bittorrent/bitfield.cpp +97 -0
- package/native-src/src/librats/bittorrent/bitfield.h +76 -0
- package/native-src/src/librats/bittorrent/byte_io.h +58 -0
- package/native-src/src/librats/bittorrent/choker.cpp +25 -0
- package/native-src/src/librats/bittorrent/choker.h +46 -0
- package/native-src/src/librats/bittorrent/client.cpp +413 -0
- package/native-src/src/librats/bittorrent/client.h +227 -0
- package/native-src/src/librats/bittorrent/disk_io.cpp +209 -0
- package/native-src/src/librats/bittorrent/disk_io.h +150 -0
- package/native-src/src/librats/bittorrent/extensions.cpp +191 -0
- package/native-src/src/librats/bittorrent/extensions.h +94 -0
- package/native-src/src/librats/bittorrent/file_storage.cpp +77 -0
- package/native-src/src/librats/bittorrent/file_storage.h +79 -0
- package/native-src/src/librats/bittorrent/log.h +42 -0
- package/native-src/src/librats/bittorrent/magnet_uri.cpp +98 -0
- package/native-src/src/librats/bittorrent/magnet_uri.h +35 -0
- package/native-src/src/librats/bittorrent/peer_connection.cpp +502 -0
- package/native-src/src/librats/bittorrent/peer_connection.h +194 -0
- package/native-src/src/librats/bittorrent/peer_list.cpp +68 -0
- package/native-src/src/librats/bittorrent/peer_list.h +75 -0
- package/native-src/src/librats/bittorrent/piece_picker.cpp +352 -0
- package/native-src/src/librats/bittorrent/piece_picker.h +201 -0
- package/native-src/src/librats/bittorrent/reactor.cpp +97 -0
- package/native-src/src/librats/bittorrent/reactor.h +89 -0
- package/native-src/src/librats/bittorrent/resume_data.cpp +72 -0
- package/native-src/src/librats/bittorrent/resume_data.h +41 -0
- package/native-src/src/librats/bittorrent/store_buffer.cpp +48 -0
- package/native-src/src/librats/bittorrent/store_buffer.h +47 -0
- package/native-src/src/librats/bittorrent/torrent.cpp +870 -0
- package/native-src/src/librats/bittorrent/torrent.h +260 -0
- package/native-src/src/librats/bittorrent/torrent_creator.cpp +129 -0
- package/native-src/src/librats/bittorrent/torrent_creator.h +58 -0
- package/native-src/src/librats/bittorrent/torrent_info.cpp +314 -0
- package/native-src/src/librats/bittorrent/torrent_info.h +118 -0
- package/native-src/src/librats/bittorrent/tracker.cpp +374 -0
- package/native-src/src/librats/bittorrent/tracker.h +108 -0
- package/native-src/src/librats/bittorrent/types.cpp +206 -0
- package/native-src/src/librats/bittorrent/types.h +86 -0
- package/native-src/src/librats/core/address.cpp +35 -0
- package/native-src/src/librats/core/address.h +78 -0
- package/native-src/src/librats/core/bytes.h +69 -0
- package/native-src/src/librats/core/chained_send_buffer.cpp +172 -0
- package/native-src/src/librats/core/chained_send_buffer.h +183 -0
- package/native-src/src/librats/core/endpoint_parse.cpp +41 -0
- package/native-src/src/librats/core/endpoint_parse.h +31 -0
- package/native-src/src/librats/core/event_bus.h +70 -0
- package/native-src/src/librats/core/host_endpoint.h +56 -0
- package/native-src/src/{io_poller.cpp → librats/core/io_poller.cpp} +520 -65
- package/native-src/src/{io_poller.h → librats/core/io_poller.h} +12 -6
- package/native-src/src/librats/core/ip_address.cpp +120 -0
- package/native-src/src/librats/core/ip_address.h +109 -0
- package/native-src/src/librats/core/mpsc_queue.h +47 -0
- package/native-src/src/librats/core/notifier.h +74 -0
- package/native-src/src/librats/core/receive_buffer.cpp +219 -0
- package/native-src/src/librats/core/receive_buffer.h +171 -0
- package/native-src/src/librats/core/service_registry.h +58 -0
- package/native-src/src/{socket.cpp → librats/core/socket.cpp} +625 -118
- package/native-src/src/librats/core/socket.h +496 -0
- package/native-src/src/librats/core/timer_queue.h +105 -0
- package/native-src/src/librats/core/types.cpp +43 -0
- package/native-src/src/librats/core/types.h +103 -0
- package/native-src/src/librats/core/wakeup_pipe.h +83 -0
- package/native-src/src/{crypto → librats/crypto}/blake2_endian.h +21 -23
- package/native-src/src/{crypto → librats/crypto}/blake2b.c +34 -33
- package/native-src/src/{crypto → librats/crypto}/blake2b.h +7 -6
- package/native-src/src/{crypto → librats/crypto}/blake2s.c +55 -54
- package/native-src/src/{crypto → librats/crypto}/blake2s.h +13 -12
- package/native-src/src/{crypto → librats/crypto}/chacha.c +22 -21
- package/native-src/src/{crypto → librats/crypto}/chacha.h +14 -13
- package/native-src/src/{crypto → librats/crypto}/chachapoly.c +56 -56
- package/native-src/src/{crypto → librats/crypto}/chachapoly.h +24 -17
- package/native-src/src/{crc32.cpp → librats/crypto/crc32.cpp} +1 -1
- package/native-src/src/{crc32.h → librats/crypto/crc32.h} +3 -1
- package/native-src/src/{crypto → librats/crypto}/curve25519.c +6 -4
- package/native-src/src/{crypto → librats/crypto}/curve25519.h +6 -3
- package/native-src/src/librats/crypto/hkdf.c +266 -0
- package/native-src/src/{crypto → librats/crypto}/hkdf.h +19 -19
- package/native-src/src/{noise.cpp → librats/crypto/noise.cpp} +84 -73
- package/native-src/src/{noise.h → librats/crypto/noise.h} +18 -8
- package/native-src/src/{crypto → librats/crypto}/poly1305.c +47 -46
- package/native-src/src/librats/crypto/poly1305.h +37 -0
- package/native-src/src/{sha1.cpp → librats/crypto/sha1.cpp} +33 -1
- package/native-src/src/{sha1.h → librats/crypto/sha1.h} +14 -6
- package/native-src/src/{crypto → librats/crypto}/sha256.c +15 -14
- package/native-src/src/{crypto → librats/crypto}/sha256.h +8 -7
- package/native-src/src/{crypto → librats/crypto}/sha512.c +15 -14
- package/native-src/src/{crypto → librats/crypto}/sha512.h +8 -7
- package/native-src/src/librats/dht/announce.cpp +37 -0
- package/native-src/src/librats/dht/announce.h +41 -0
- package/native-src/src/librats/dht/bep42.cpp +109 -0
- package/native-src/src/librats/dht/bep42.h +48 -0
- package/native-src/src/librats/dht/dht.cpp +501 -0
- package/native-src/src/librats/dht/dht.h +119 -0
- package/native-src/src/librats/dht/dht_runner.cpp +103 -0
- package/native-src/src/librats/dht/dht_runner.h +71 -0
- package/native-src/src/librats/dht/dos_blocker.cpp +42 -0
- package/native-src/src/librats/dht/dos_blocker.h +47 -0
- package/native-src/src/librats/dht/find_peers.cpp +52 -0
- package/native-src/src/librats/dht/find_peers.h +73 -0
- package/native-src/src/librats/dht/id.h +167 -0
- package/native-src/src/{krpc.cpp → librats/dht/krpc.cpp} +32 -81
- package/native-src/src/{krpc.h → librats/dht/krpc.h} +19 -23
- package/native-src/src/librats/dht/log.h +38 -0
- package/native-src/src/librats/dht/node.cpp +473 -0
- package/native-src/src/librats/dht/node.h +164 -0
- package/native-src/src/librats/dht/node_entry.h +81 -0
- package/native-src/src/librats/dht/observer.h +72 -0
- package/native-src/src/librats/dht/persistence.cpp +90 -0
- package/native-src/src/librats/dht/persistence.h +32 -0
- package/native-src/src/librats/dht/routing_table.cpp +559 -0
- package/native-src/src/librats/dht/routing_table.h +185 -0
- package/native-src/src/librats/dht/rpc_manager.cpp +127 -0
- package/native-src/src/librats/dht/rpc_manager.h +77 -0
- package/native-src/src/librats/dht/storage.cpp +92 -0
- package/native-src/src/librats/dht/storage.h +74 -0
- package/native-src/src/librats/dht/transport.h +27 -0
- package/native-src/src/librats/dht/traversal.cpp +326 -0
- package/native-src/src/librats/dht/traversal.h +120 -0
- package/native-src/src/librats/dht/udp_transport.cpp +49 -0
- package/native-src/src/librats/dht/udp_transport.h +51 -0
- package/native-src/src/librats/mdns/log.h +22 -0
- package/native-src/src/{mdns.cpp → librats/mdns/mdns.cpp} +75 -40
- package/native-src/src/{mdns.h → librats/mdns/mdns.h} +9 -8
- package/native-src/src/{natpmp.cpp → librats/nat/natpmp.cpp} +12 -9
- package/native-src/src/{natpmp.h → librats/nat/natpmp.h} +3 -3
- package/native-src/src/{port_mapping.h → librats/nat/port_mapping.h} +3 -2
- package/native-src/src/{stun.cpp → librats/nat/stun.cpp} +4 -4
- package/native-src/src/{stun.h → librats/nat/stun.h} +1 -1
- package/native-src/src/{upnp.cpp → librats/nat/upnp.cpp} +6 -6
- package/native-src/src/{upnp.h → librats/nat/upnp.h} +2 -2
- package/native-src/src/librats/node/circuit_service.h +84 -0
- package/native-src/src/librats/node/config.h +110 -0
- package/native-src/src/librats/node/dial_service.h +54 -0
- package/native-src/src/librats/node/dialer.cpp +264 -0
- package/native-src/src/librats/node/dialer.h +188 -0
- package/native-src/src/librats/node/host_events.h +26 -0
- package/native-src/src/librats/node/identify.cpp +130 -0
- package/native-src/src/librats/node/identify.h +71 -0
- package/native-src/src/librats/node/nat_status.cpp +103 -0
- package/native-src/src/librats/node/nat_status.h +118 -0
- package/native-src/src/librats/node/node.cpp +865 -0
- package/native-src/src/librats/node/node.h +344 -0
- package/native-src/src/librats/node/node_context.h +33 -0
- package/native-src/src/librats/node/peer_network.h +91 -0
- package/native-src/src/librats/peer/peer.h +49 -0
- package/native-src/src/librats/peer/peer_book.cpp +181 -0
- package/native-src/src/librats/peer/peer_book.h +88 -0
- package/native-src/src/librats/peer/peer_id.cpp +72 -0
- package/native-src/src/librats/peer/peer_id.h +62 -0
- package/native-src/src/librats/peer/peer_info.h +37 -0
- package/native-src/src/librats/peer/peer_table.cpp +170 -0
- package/native-src/src/librats/peer/peer_table.h +148 -0
- package/native-src/src/librats/security/handshaker.h +66 -0
- package/native-src/src/librats/security/identity.h +43 -0
- package/native-src/src/librats/security/noise_security.cpp +122 -0
- package/native-src/src/librats/security/noise_security.h +37 -0
- package/native-src/src/librats/security/plaintext_security.h +106 -0
- package/native-src/src/librats/security/session.h +37 -0
- package/native-src/src/{storage.cpp → librats/storage/storage.cpp} +369 -522
- package/native-src/src/{storage.h → librats/storage/storage.h} +135 -299
- package/native-src/src/librats/subsystems/bittorrent.cpp +211 -0
- package/native-src/src/librats/subsystems/bittorrent.h +136 -0
- package/native-src/src/librats/subsystems/dht_discovery.cpp +202 -0
- package/native-src/src/librats/subsystems/dht_discovery.h +123 -0
- package/native-src/src/librats/subsystems/dht_service.h +36 -0
- package/native-src/src/librats/subsystems/file_transfer.cpp +972 -0
- package/native-src/src/librats/subsystems/file_transfer.h +367 -0
- package/native-src/src/librats/subsystems/hole_punch.cpp +605 -0
- package/native-src/src/librats/subsystems/hole_punch.h +290 -0
- package/native-src/src/librats/subsystems/hole_punch_service.h +38 -0
- package/native-src/src/librats/subsystems/mdns_discovery.cpp +66 -0
- package/native-src/src/librats/subsystems/mdns_discovery.h +55 -0
- package/native-src/src/librats/subsystems/message_json.cpp +112 -0
- package/native-src/src/librats/subsystems/message_json.h +88 -0
- package/native-src/src/librats/subsystems/peer_exchange.cpp +241 -0
- package/native-src/src/librats/subsystems/peer_exchange.h +136 -0
- package/native-src/src/librats/subsystems/ping_service.cpp +98 -0
- package/native-src/src/librats/subsystems/ping_service.h +66 -0
- package/native-src/src/librats/subsystems/port_mapping_service.cpp +192 -0
- package/native-src/src/librats/subsystems/port_mapping_service.h +84 -0
- package/native-src/src/librats/subsystems/pubsub.cpp +567 -0
- package/native-src/src/librats/subsystems/pubsub.h +175 -0
- package/native-src/src/librats/subsystems/reconnection.cpp +239 -0
- package/native-src/src/librats/subsystems/reconnection.h +126 -0
- package/native-src/src/librats/subsystems/relay.cpp +1142 -0
- package/native-src/src/librats/subsystems/relay.h +211 -0
- package/native-src/src/librats/subsystems/relay_service.h +46 -0
- package/native-src/src/librats/transport/connection.cpp +343 -0
- package/native-src/src/librats/transport/connection.h +283 -0
- package/native-src/src/librats/transport/link.h +96 -0
- package/native-src/src/librats/transport/reactor.cpp +588 -0
- package/native-src/src/librats/transport/reactor.h +262 -0
- package/native-src/src/librats/transport/reactor_pool.h +81 -0
- package/native-src/src/librats/transport/relay_link.cpp +208 -0
- package/native-src/src/librats/transport/relay_link.h +303 -0
- package/native-src/src/librats/transport/tcp_link.cpp +49 -0
- package/native-src/src/librats/transport/tcp_link.h +43 -0
- package/native-src/src/librats/transport/udp_mux.cpp +617 -0
- package/native-src/src/librats/transport/udp_mux.h +363 -0
- package/native-src/src/librats/transport/udp_packet.cpp +121 -0
- package/native-src/src/librats/transport/udp_packet.h +190 -0
- package/native-src/src/librats/transport/udp_stream.cpp +1194 -0
- package/native-src/src/librats/transport/udp_stream.h +614 -0
- package/native-src/src/librats/util/features.h.in +51 -0
- package/native-src/src/{fs.cpp → librats/util/fs.cpp} +51 -3
- package/native-src/src/librats/util/fs.h +136 -0
- package/native-src/src/librats/util/json.cpp +1002 -0
- package/native-src/src/librats/util/json.h +444 -0
- package/native-src/src/{logger.cpp → librats/util/logger.cpp} +1 -1
- package/native-src/src/{logger.h → librats/util/logger.h} +43 -31
- package/native-src/src/{network_monitor.cpp → librats/util/network_monitor.cpp} +12 -4
- package/native-src/src/{network_monitor.h → librats/util/network_monitor.h} +2 -1
- package/native-src/src/{network_utils.cpp → librats/util/network_utils.cpp} +38 -23
- package/native-src/src/{network_utils.h → librats/util/network_utils.h} +15 -8
- package/native-src/src/{os.cpp → librats/util/os.cpp} +48 -18
- package/native-src/src/librats/util/rats_export.h +69 -0
- package/native-src/src/{version.cpp → librats/util/version.cpp} +2 -2
- package/native-src/src/{version.h.in → librats/util/version.h.in} +1 -1
- package/native-src/src/librats/wire/frame.cpp +76 -0
- package/native-src/src/librats/wire/frame.h +111 -0
- package/native-src/src/librats/wire/message_router.cpp +45 -0
- package/native-src/src/librats/wire/message_router.h +47 -0
- package/package.json +5 -4
- package/scripts/build-librats.js +1 -0
- package/scripts/postinstall.js +3 -3
- package/scripts/prepare-package.js +4 -4
- package/scripts/verify-installation.js +63 -105
- package/src/librats_node.cpp +1067 -1323
- package/native-src/src/bencode.cpp +0 -485
- package/native-src/src/bencode.h +0 -145
- package/native-src/src/bittorrent.cpp +0 -14
- package/native-src/src/bittorrent.h +0 -74
- package/native-src/src/bt_bitfield.cpp +0 -372
- package/native-src/src/bt_bitfield.h +0 -316
- package/native-src/src/bt_choker.cpp +0 -228
- package/native-src/src/bt_choker.h +0 -147
- package/native-src/src/bt_client.cpp +0 -1047
- package/native-src/src/bt_client.h +0 -445
- package/native-src/src/bt_create_torrent.cpp +0 -677
- package/native-src/src/bt_create_torrent.h +0 -473
- package/native-src/src/bt_extension.cpp +0 -469
- package/native-src/src/bt_extension.h +0 -309
- package/native-src/src/bt_file_storage.cpp +0 -261
- package/native-src/src/bt_file_storage.h +0 -298
- package/native-src/src/bt_handshake.cpp +0 -134
- package/native-src/src/bt_handshake.h +0 -157
- package/native-src/src/bt_messages.cpp +0 -364
- package/native-src/src/bt_messages.h +0 -324
- package/native-src/src/bt_network.cpp +0 -1007
- package/native-src/src/bt_network.h +0 -417
- package/native-src/src/bt_peer_connection.cpp +0 -742
- package/native-src/src/bt_peer_connection.h +0 -592
- package/native-src/src/bt_piece_picker.cpp +0 -786
- package/native-src/src/bt_piece_picker.h +0 -473
- package/native-src/src/bt_resume_data.cpp +0 -410
- package/native-src/src/bt_resume_data.h +0 -249
- package/native-src/src/bt_torrent.cpp +0 -2120
- package/native-src/src/bt_torrent.h +0 -641
- package/native-src/src/bt_torrent_info.cpp +0 -659
- package/native-src/src/bt_torrent_info.h +0 -418
- package/native-src/src/bt_types.h +0 -621
- package/native-src/src/chained_send_buffer.cpp +0 -75
- package/native-src/src/chained_send_buffer.h +0 -137
- package/native-src/src/crypto/hkdf.c +0 -266
- package/native-src/src/crypto/poly1305.h +0 -36
- package/native-src/src/dht.cpp +0 -3311
- package/native-src/src/dht.h +0 -717
- package/native-src/src/disk_io.cpp +0 -632
- package/native-src/src/disk_io.h +0 -315
- package/native-src/src/file_transfer.cpp +0 -1415
- package/native-src/src/file_transfer.h +0 -286
- package/native-src/src/fs.h +0 -108
- package/native-src/src/gossipsub.cpp +0 -1139
- package/native-src/src/gossipsub.h +0 -403
- package/native-src/src/ice.cpp +0 -893
- package/native-src/src/ice.h +0 -559
- package/native-src/src/json.hpp +0 -25526
- package/native-src/src/librats.cpp +0 -2378
- package/native-src/src/librats.h +0 -2324
- package/native-src/src/librats_bittorrent.cpp +0 -601
- package/native-src/src/librats_c.cpp +0 -1557
- package/native-src/src/librats_c.h +0 -323
- package/native-src/src/librats_discovery.cpp +0 -402
- package/native-src/src/librats_encryption.cpp +0 -275
- package/native-src/src/librats_file_transfer.cpp +0 -144
- package/native-src/src/librats_gossipsub.cpp +0 -289
- package/native-src/src/librats_ice.cpp +0 -213
- package/native-src/src/librats_log_macros.h +0 -36
- package/native-src/src/librats_logging.cpp +0 -173
- package/native-src/src/librats_mdns.cpp +0 -166
- package/native-src/src/librats_persistence.cpp +0 -796
- package/native-src/src/librats_portmap.cpp +0 -419
- package/native-src/src/librats_reconnection.cpp +0 -218
- package/native-src/src/librats_statistic.cpp +0 -105
- package/native-src/src/librats_storage.cpp +0 -189
- package/native-src/src/rats_export.h +0 -17
- package/native-src/src/receive_buffer.cpp +0 -82
- package/native-src/src/receive_buffer.h +0 -127
- package/native-src/src/socket.h +0 -228
- package/native-src/src/threadmanager.cpp +0 -105
- package/native-src/src/threadmanager.h +0 -53
- package/native-src/src/tracker.cpp +0 -1264
- package/native-src/src/tracker.h +0 -319
- package/native-src/src/turn.cpp +0 -762
- package/native-src/src/turn.h +0 -460
- package/native-src/src/wakeup_pipe.h +0 -60
- /package/native-src/src/{os.h → librats/util/os.h} +0 -0
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
#include <memory>
|
|
17
17
|
#include <string>
|
|
18
18
|
|
|
19
|
-
namespace
|
|
19
|
+
namespace librats {
|
|
20
20
|
|
|
21
21
|
/* Constants */
|
|
22
22
|
constexpr size_t NOISE_KEY_SIZE = 32;
|
|
@@ -25,6 +25,12 @@ constexpr size_t NOISE_DH_SIZE = 32;
|
|
|
25
25
|
constexpr size_t NOISE_TAG_SIZE = 16;
|
|
26
26
|
constexpr size_t NOISE_MAX_MESSAGE_SIZE = 65535;
|
|
27
27
|
|
|
28
|
+
/* Returned by decrypt_with_ad / decrypt_and_hash / NoiseSession::decrypt on
|
|
29
|
+
* authentication failure. A successful decryption returns the plaintext length,
|
|
30
|
+
* which may be 0 for an empty authenticated message, so failure needs a distinct
|
|
31
|
+
* sentinel that cannot be confused with a valid (possibly zero) length. */
|
|
32
|
+
constexpr size_t NOISE_DECRYPT_FAILED = static_cast<size_t>(-1);
|
|
33
|
+
|
|
28
34
|
/* Protocol name for Noise_XX_25519_ChaChaPoly_SHA256 */
|
|
29
35
|
constexpr const char* NOISE_PROTOCOL_NAME = "Noise_XX_25519_ChaChaPoly_SHA256";
|
|
30
36
|
|
|
@@ -61,7 +67,7 @@ public:
|
|
|
61
67
|
/* Get current nonce value */
|
|
62
68
|
uint64_t get_nonce() const { return nonce_; }
|
|
63
69
|
|
|
64
|
-
/* Set nonce (for testing
|
|
70
|
+
/* Set nonce (for testing) */
|
|
65
71
|
void set_nonce(uint64_t n) { nonce_ = n; }
|
|
66
72
|
|
|
67
73
|
/**
|
|
@@ -94,9 +100,6 @@ public:
|
|
|
94
100
|
uint8_t* plaintext
|
|
95
101
|
);
|
|
96
102
|
|
|
97
|
-
/* Rekey the cipher state */
|
|
98
|
-
void rekey();
|
|
99
|
-
|
|
100
103
|
/* Clear sensitive data */
|
|
101
104
|
void clear();
|
|
102
105
|
|
|
@@ -145,9 +148,12 @@ public:
|
|
|
145
148
|
* @param ciphertext Input ciphertext
|
|
146
149
|
* @param ct_len Length of ciphertext
|
|
147
150
|
* @param plaintext Output buffer
|
|
151
|
+
* @param plaintext_cap Capacity of the output buffer in bytes; decryption is
|
|
152
|
+
* refused (returns 0) if the resulting plaintext would not fit. This
|
|
153
|
+
* guards a fixed-size payload buffer against an oversized message.
|
|
148
154
|
* @return Length of plaintext on success, 0 on failure
|
|
149
155
|
*/
|
|
150
|
-
size_t decrypt_and_hash(const uint8_t* ciphertext, size_t ct_len, uint8_t* plaintext);
|
|
156
|
+
size_t decrypt_and_hash(const uint8_t* ciphertext, size_t ct_len, uint8_t* plaintext, size_t plaintext_cap);
|
|
151
157
|
|
|
152
158
|
/**
|
|
153
159
|
* Split into two CipherState objects for transport phase
|
|
@@ -323,8 +329,12 @@ public:
|
|
|
323
329
|
* Start a new session
|
|
324
330
|
* @param is_initiator true if initiating the connection
|
|
325
331
|
* @param static_keypair Optional pre-generated static key pair
|
|
332
|
+
* @param prologue Optional prologue mixed into the handshake hash; both peers
|
|
333
|
+
* must supply identical prologue bytes or the handshake fails
|
|
334
|
+
* @param prologue_len Length of prologue
|
|
326
335
|
*/
|
|
327
|
-
NoiseError start(bool is_initiator, const NoiseKeyPair* static_keypair = nullptr
|
|
336
|
+
NoiseError start(bool is_initiator, const NoiseKeyPair* static_keypair = nullptr,
|
|
337
|
+
const uint8_t* prologue = nullptr, size_t prologue_len = 0);
|
|
328
338
|
|
|
329
339
|
/**
|
|
330
340
|
* Process handshake - call repeatedly until is_handshake_complete()
|
|
@@ -402,6 +412,6 @@ void noise_generate_keypair(NoiseKeyPair& keypair);
|
|
|
402
412
|
void noise_derive_public_key(const uint8_t private_key[NOISE_DH_SIZE],
|
|
403
413
|
uint8_t public_key[NOISE_DH_SIZE]);
|
|
404
414
|
|
|
405
|
-
} /* namespace
|
|
415
|
+
} /* namespace librats */
|
|
406
416
|
|
|
407
417
|
#endif /* RATS_NOISE_H */
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MIT */
|
|
1
2
|
/*
|
|
2
3
|
* poly1305-donna
|
|
3
4
|
* https://github.com/floodyberry/poly1305-donna
|
|
4
5
|
* MIT license / public domain
|
|
5
6
|
*/
|
|
6
7
|
|
|
7
|
-
#include "poly1305.h"
|
|
8
|
+
#include "librats/crypto/poly1305.h"
|
|
8
9
|
|
|
9
10
|
/* Auto detect between 32bit / 64bit */
|
|
10
11
|
#if (defined(__SIZEOF_INT128__) && defined(__LP64__)) || \
|
|
@@ -29,17 +30,17 @@
|
|
|
29
30
|
#define POLY1305_NOINLINE
|
|
30
31
|
#endif
|
|
31
32
|
|
|
32
|
-
#define
|
|
33
|
+
#define rats_poly1305_block_size 16
|
|
33
34
|
|
|
34
35
|
/* 17 + sizeof(size_t) + 14*sizeof(unsigned long) */
|
|
35
|
-
typedef struct
|
|
36
|
+
typedef struct rats_poly1305_state_internal_t {
|
|
36
37
|
unsigned long r[5];
|
|
37
38
|
unsigned long h[5];
|
|
38
39
|
unsigned long pad[4];
|
|
39
40
|
size_t leftover;
|
|
40
|
-
unsigned char buffer[
|
|
41
|
+
unsigned char buffer[rats_poly1305_block_size];
|
|
41
42
|
unsigned char final;
|
|
42
|
-
}
|
|
43
|
+
} rats_poly1305_state_internal_t;
|
|
43
44
|
|
|
44
45
|
/* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in little endian */
|
|
45
46
|
static unsigned long
|
|
@@ -61,8 +62,8 @@ U32TO8(unsigned char *p, unsigned long v) {
|
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
void
|
|
64
|
-
|
|
65
|
-
|
|
65
|
+
rats_poly1305_init(rats_poly1305_context *ctx, const unsigned char key[32]) {
|
|
66
|
+
rats_poly1305_state_internal_t *st = (rats_poly1305_state_internal_t *)ctx;
|
|
66
67
|
|
|
67
68
|
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
|
|
68
69
|
st->r[0] = (U8TO32(&key[ 0]) ) & 0x3ffffff;
|
|
@@ -89,7 +90,7 @@ poly1305_init(poly1305_context *ctx, const unsigned char key[32]) {
|
|
|
89
90
|
}
|
|
90
91
|
|
|
91
92
|
static void
|
|
92
|
-
|
|
93
|
+
rats_poly1305_blocks(rats_poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) {
|
|
93
94
|
const unsigned long hibit = (st->final) ? 0 : (1UL << 24); /* 1 << 128 */
|
|
94
95
|
unsigned long r0,r1,r2,r3,r4;
|
|
95
96
|
unsigned long s1,s2,s3,s4;
|
|
@@ -114,7 +115,7 @@ poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t by
|
|
|
114
115
|
h3 = st->h[3];
|
|
115
116
|
h4 = st->h[4];
|
|
116
117
|
|
|
117
|
-
while (bytes >=
|
|
118
|
+
while (bytes >= rats_poly1305_block_size) {
|
|
118
119
|
/* h += m[i] */
|
|
119
120
|
h0 += (U8TO32(m+ 0) ) & 0x3ffffff;
|
|
120
121
|
h1 += (U8TO32(m+ 3) >> 2) & 0x3ffffff;
|
|
@@ -138,8 +139,8 @@ poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t by
|
|
|
138
139
|
h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff;
|
|
139
140
|
h1 += c;
|
|
140
141
|
|
|
141
|
-
m +=
|
|
142
|
-
bytes -=
|
|
142
|
+
m += rats_poly1305_block_size;
|
|
143
|
+
bytes -= rats_poly1305_block_size;
|
|
143
144
|
}
|
|
144
145
|
|
|
145
146
|
st->h[0] = h0;
|
|
@@ -150,8 +151,8 @@ poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t by
|
|
|
150
151
|
}
|
|
151
152
|
|
|
152
153
|
POLY1305_NOINLINE void
|
|
153
|
-
|
|
154
|
-
|
|
154
|
+
rats_poly1305_finish(rats_poly1305_context *ctx, unsigned char mac[16]) {
|
|
155
|
+
rats_poly1305_state_internal_t *st = (rats_poly1305_state_internal_t *)ctx;
|
|
155
156
|
unsigned long h0,h1,h2,h3,h4,c;
|
|
156
157
|
unsigned long g0,g1,g2,g3,g4;
|
|
157
158
|
unsigned long long f;
|
|
@@ -161,10 +162,10 @@ poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) {
|
|
|
161
162
|
if (st->leftover) {
|
|
162
163
|
size_t i = st->leftover;
|
|
163
164
|
st->buffer[i++] = 1;
|
|
164
|
-
for (; i <
|
|
165
|
+
for (; i < rats_poly1305_block_size; i++)
|
|
165
166
|
st->buffer[i] = 0;
|
|
166
167
|
st->final = 1;
|
|
167
|
-
|
|
168
|
+
rats_poly1305_blocks(st, st->buffer, rats_poly1305_block_size);
|
|
168
169
|
}
|
|
169
170
|
|
|
170
171
|
/* fully carry h */
|
|
@@ -273,17 +274,17 @@ poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) {
|
|
|
273
274
|
#define POLY1305_NOINLINE __attribute__((noinline))
|
|
274
275
|
#endif
|
|
275
276
|
|
|
276
|
-
#define
|
|
277
|
+
#define rats_poly1305_block_size 16
|
|
277
278
|
|
|
278
279
|
/* 17 + sizeof(size_t) + 8*sizeof(unsigned long long) */
|
|
279
|
-
typedef struct
|
|
280
|
+
typedef struct rats_poly1305_state_internal_t {
|
|
280
281
|
unsigned long long r[3];
|
|
281
282
|
unsigned long long h[3];
|
|
282
283
|
unsigned long long pad[2];
|
|
283
284
|
size_t leftover;
|
|
284
|
-
unsigned char buffer[
|
|
285
|
+
unsigned char buffer[rats_poly1305_block_size];
|
|
285
286
|
unsigned char final;
|
|
286
|
-
}
|
|
287
|
+
} rats_poly1305_state_internal_t;
|
|
287
288
|
|
|
288
289
|
/* interpret eight 8 bit unsigned integers as a 64 bit unsigned integer in little endian */
|
|
289
290
|
static unsigned long long
|
|
@@ -313,8 +314,8 @@ U64TO8(unsigned char *p, unsigned long long v) {
|
|
|
313
314
|
}
|
|
314
315
|
|
|
315
316
|
void
|
|
316
|
-
|
|
317
|
-
|
|
317
|
+
rats_poly1305_init(rats_poly1305_context *ctx, const unsigned char key[32]) {
|
|
318
|
+
rats_poly1305_state_internal_t *st = (rats_poly1305_state_internal_t *)ctx;
|
|
318
319
|
unsigned long long t0,t1;
|
|
319
320
|
|
|
320
321
|
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
|
|
@@ -339,7 +340,7 @@ poly1305_init(poly1305_context *ctx, const unsigned char key[32]) {
|
|
|
339
340
|
}
|
|
340
341
|
|
|
341
342
|
static void
|
|
342
|
-
|
|
343
|
+
rats_poly1305_blocks(rats_poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) {
|
|
343
344
|
const unsigned long long hibit = (st->final) ? 0 : ((unsigned long long)1 << 40); /* 1 << 128 */
|
|
344
345
|
unsigned long long r0,r1,r2;
|
|
345
346
|
unsigned long long s1,s2;
|
|
@@ -358,7 +359,7 @@ poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t by
|
|
|
358
359
|
s1 = r1 * (5 << 2);
|
|
359
360
|
s2 = r2 * (5 << 2);
|
|
360
361
|
|
|
361
|
-
while (bytes >=
|
|
362
|
+
while (bytes >= rats_poly1305_block_size) {
|
|
362
363
|
unsigned long long t0,t1;
|
|
363
364
|
|
|
364
365
|
/* h += m[i] */
|
|
@@ -381,8 +382,8 @@ poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t by
|
|
|
381
382
|
h0 += c * 5; c = (h0 >> 44); h0 = h0 & 0xfffffffffffULL;
|
|
382
383
|
h1 += c;
|
|
383
384
|
|
|
384
|
-
m +=
|
|
385
|
-
bytes -=
|
|
385
|
+
m += rats_poly1305_block_size;
|
|
386
|
+
bytes -= rats_poly1305_block_size;
|
|
386
387
|
}
|
|
387
388
|
|
|
388
389
|
st->h[0] = h0;
|
|
@@ -392,8 +393,8 @@ poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t by
|
|
|
392
393
|
|
|
393
394
|
|
|
394
395
|
POLY1305_NOINLINE void
|
|
395
|
-
|
|
396
|
-
|
|
396
|
+
rats_poly1305_finish(rats_poly1305_context *ctx, unsigned char mac[16]) {
|
|
397
|
+
rats_poly1305_state_internal_t *st = (rats_poly1305_state_internal_t *)ctx;
|
|
397
398
|
unsigned long long h0,h1,h2,c;
|
|
398
399
|
unsigned long long g0,g1,g2;
|
|
399
400
|
unsigned long long t0,t1;
|
|
@@ -402,10 +403,10 @@ poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) {
|
|
|
402
403
|
if (st->leftover) {
|
|
403
404
|
size_t i = st->leftover;
|
|
404
405
|
st->buffer[i] = 1;
|
|
405
|
-
for (i = i + 1; i <
|
|
406
|
+
for (i = i + 1; i < rats_poly1305_block_size; i++)
|
|
406
407
|
st->buffer[i] = 0;
|
|
407
408
|
st->final = 1;
|
|
408
|
-
|
|
409
|
+
rats_poly1305_blocks(st, st->buffer, rats_poly1305_block_size);
|
|
409
410
|
}
|
|
410
411
|
|
|
411
412
|
/* fully carry h */
|
|
@@ -465,13 +466,13 @@ poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) {
|
|
|
465
466
|
#endif /* POLY1305_64BIT */
|
|
466
467
|
|
|
467
468
|
void
|
|
468
|
-
|
|
469
|
-
|
|
469
|
+
rats_poly1305_update(rats_poly1305_context *ctx, const unsigned char *m, size_t bytes) {
|
|
470
|
+
rats_poly1305_state_internal_t *st = (rats_poly1305_state_internal_t *)ctx;
|
|
470
471
|
size_t i;
|
|
471
472
|
|
|
472
473
|
/* handle leftover */
|
|
473
474
|
if (st->leftover) {
|
|
474
|
-
size_t want = (
|
|
475
|
+
size_t want = (rats_poly1305_block_size - st->leftover);
|
|
475
476
|
if (want > bytes)
|
|
476
477
|
want = bytes;
|
|
477
478
|
for (i = 0; i < want; i++)
|
|
@@ -479,16 +480,16 @@ poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes) {
|
|
|
479
480
|
bytes -= want;
|
|
480
481
|
m += want;
|
|
481
482
|
st->leftover += want;
|
|
482
|
-
if (st->leftover <
|
|
483
|
+
if (st->leftover < rats_poly1305_block_size)
|
|
483
484
|
return;
|
|
484
|
-
|
|
485
|
+
rats_poly1305_blocks(st, st->buffer, rats_poly1305_block_size);
|
|
485
486
|
st->leftover = 0;
|
|
486
487
|
}
|
|
487
488
|
|
|
488
489
|
/* process full blocks */
|
|
489
|
-
if (bytes >=
|
|
490
|
-
size_t want = (bytes & ~(
|
|
491
|
-
|
|
490
|
+
if (bytes >= rats_poly1305_block_size) {
|
|
491
|
+
size_t want = (bytes & ~(rats_poly1305_block_size - 1));
|
|
492
|
+
rats_poly1305_blocks(st, m, want);
|
|
492
493
|
m += want;
|
|
493
494
|
bytes -= want;
|
|
494
495
|
}
|
|
@@ -502,15 +503,15 @@ poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes) {
|
|
|
502
503
|
}
|
|
503
504
|
|
|
504
505
|
void
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
506
|
+
rats_poly1305_auth(unsigned char mac[16], const unsigned char *m, size_t bytes, const unsigned char key[32]) {
|
|
507
|
+
rats_poly1305_context ctx;
|
|
508
|
+
rats_poly1305_init(&ctx, key);
|
|
509
|
+
rats_poly1305_update(&ctx, m, bytes);
|
|
510
|
+
rats_poly1305_finish(&ctx, mac);
|
|
510
511
|
}
|
|
511
512
|
|
|
512
513
|
int
|
|
513
|
-
|
|
514
|
+
rats_poly1305_verify(const unsigned char mac1[16], const unsigned char mac2[16]) {
|
|
514
515
|
size_t i;
|
|
515
516
|
unsigned int dif = 0;
|
|
516
517
|
for (i = 0; i < 16; i++)
|
|
@@ -522,7 +523,7 @@ poly1305_verify(const unsigned char mac1[16], const unsigned char mac2[16]) {
|
|
|
522
523
|
|
|
523
524
|
/* test a few basic operations */
|
|
524
525
|
int
|
|
525
|
-
|
|
526
|
+
rats_poly1305_power_on_self_test(void) {
|
|
526
527
|
/* example from nacl */
|
|
527
528
|
static const unsigned char nacl_key[32] = {
|
|
528
529
|
0xee,0xa6,0xa7,0x25,0x1c,0x1e,0x72,0x91,
|
|
@@ -559,8 +560,8 @@ poly1305_power_on_self_test(void) {
|
|
|
559
560
|
unsigned char mac[16];
|
|
560
561
|
int result = 1;
|
|
561
562
|
|
|
562
|
-
|
|
563
|
-
result &=
|
|
563
|
+
rats_poly1305_auth(mac, nacl_msg, sizeof(nacl_msg), nacl_key);
|
|
564
|
+
result &= rats_poly1305_verify(nacl_mac, mac);
|
|
564
565
|
|
|
565
566
|
return result;
|
|
566
567
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MIT */
|
|
2
|
+
/*
|
|
3
|
+
* poly1305-donna
|
|
4
|
+
* https://github.com/floodyberry/poly1305-donna
|
|
5
|
+
* MIT license / public domain
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
#ifndef LIBRATS_POLY1305_H
|
|
9
|
+
#define LIBRATS_POLY1305_H
|
|
10
|
+
|
|
11
|
+
#include <stddef.h>
|
|
12
|
+
|
|
13
|
+
#ifdef __cplusplus
|
|
14
|
+
extern "C" {
|
|
15
|
+
#endif
|
|
16
|
+
|
|
17
|
+
#define RATS_POLY1305_KEY_SIZE 32
|
|
18
|
+
#define RATS_POLY1305_TAG_SIZE 16
|
|
19
|
+
|
|
20
|
+
typedef struct rats_poly1305_context {
|
|
21
|
+
size_t aligner;
|
|
22
|
+
unsigned char opaque[136];
|
|
23
|
+
} rats_poly1305_context;
|
|
24
|
+
|
|
25
|
+
void rats_poly1305_init(rats_poly1305_context *ctx, const unsigned char key[32]);
|
|
26
|
+
void rats_poly1305_update(rats_poly1305_context *ctx, const unsigned char *m, size_t bytes);
|
|
27
|
+
void rats_poly1305_finish(rats_poly1305_context *ctx, unsigned char mac[16]);
|
|
28
|
+
void rats_poly1305_auth(unsigned char mac[16], const unsigned char *m, size_t bytes, const unsigned char key[32]);
|
|
29
|
+
|
|
30
|
+
int rats_poly1305_verify(const unsigned char mac1[16], const unsigned char mac2[16]);
|
|
31
|
+
int rats_poly1305_power_on_self_test(void);
|
|
32
|
+
|
|
33
|
+
#ifdef __cplusplus
|
|
34
|
+
}
|
|
35
|
+
#endif
|
|
36
|
+
|
|
37
|
+
#endif /* LIBRATS_POLY1305_H */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#include "sha1.h"
|
|
1
|
+
#include "librats/crypto/sha1.h"
|
|
2
2
|
#include <iomanip>
|
|
3
3
|
#include <sstream>
|
|
4
4
|
#include <cstring>
|
|
@@ -148,6 +148,38 @@ std::string SHA1::finalize() {
|
|
|
148
148
|
return result.str();
|
|
149
149
|
}
|
|
150
150
|
|
|
151
|
+
std::array<uint8_t, 20> SHA1::finalize_bytes() {
|
|
152
|
+
if (!finalized) {
|
|
153
|
+
// Same padding as finalize(): append 0x80, zero-fill to 56 mod 64, then
|
|
154
|
+
// the 64-bit big-endian bit length.
|
|
155
|
+
uint64_t bit_length = total_length * 8;
|
|
156
|
+
update(0x80);
|
|
157
|
+
while (buffer_length != 56) {
|
|
158
|
+
update(0x00);
|
|
159
|
+
}
|
|
160
|
+
for (int i = 7; i >= 0; i--) {
|
|
161
|
+
update(static_cast<uint8_t>(bit_length >> (i * 8)));
|
|
162
|
+
}
|
|
163
|
+
finalized = true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
std::array<uint8_t, 20> out{};
|
|
167
|
+
const uint32_t h[5] = {h0, h1, h2, h3, h4};
|
|
168
|
+
for (int i = 0; i < 5; i++) {
|
|
169
|
+
out[i * 4] = static_cast<uint8_t>(h[i] >> 24);
|
|
170
|
+
out[i * 4 + 1] = static_cast<uint8_t>(h[i] >> 16);
|
|
171
|
+
out[i * 4 + 2] = static_cast<uint8_t>(h[i] >> 8);
|
|
172
|
+
out[i * 4 + 3] = static_cast<uint8_t>(h[i]);
|
|
173
|
+
}
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
std::array<uint8_t, 20> SHA1::hash_raw(const uint8_t* data, size_t length) {
|
|
178
|
+
SHA1 hasher;
|
|
179
|
+
hasher.update(data, length);
|
|
180
|
+
return hasher.finalize_bytes();
|
|
181
|
+
}
|
|
182
|
+
|
|
151
183
|
std::string SHA1::hash(const std::string& input) {
|
|
152
184
|
SHA1 hasher;
|
|
153
185
|
hasher.update(input);
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
#include <string>
|
|
4
4
|
#include <vector>
|
|
5
|
+
#include <array>
|
|
5
6
|
#include <cstdint>
|
|
6
7
|
|
|
7
8
|
namespace librats {
|
|
@@ -9,25 +10,32 @@ namespace librats {
|
|
|
9
10
|
class SHA1 {
|
|
10
11
|
public:
|
|
11
12
|
SHA1();
|
|
12
|
-
|
|
13
|
+
|
|
13
14
|
// Process a single byte
|
|
14
15
|
void update(uint8_t byte);
|
|
15
|
-
|
|
16
|
+
|
|
16
17
|
// Process a buffer
|
|
17
18
|
void update(const uint8_t* data, size_t length);
|
|
18
|
-
|
|
19
|
+
|
|
19
20
|
// Process a string
|
|
20
21
|
void update(const std::string& str);
|
|
21
|
-
|
|
22
|
+
|
|
22
23
|
// Get the final hash as a hex string
|
|
23
24
|
std::string finalize();
|
|
24
|
-
|
|
25
|
+
|
|
26
|
+
// Get the final hash as the raw 20-byte digest. Idempotent: may be called
|
|
27
|
+
// after finalize() (and vice versa) — the digest is retained once computed.
|
|
28
|
+
std::array<uint8_t, 20> finalize_bytes();
|
|
29
|
+
|
|
25
30
|
// Convenience function to hash a string directly
|
|
26
31
|
static std::string hash(const std::string& input);
|
|
27
|
-
|
|
32
|
+
|
|
28
33
|
// Convenience function to hash a vector of bytes directly
|
|
29
34
|
static std::string hash_bytes(const std::vector<uint8_t>& input);
|
|
30
35
|
|
|
36
|
+
// Convenience: hash a buffer to the raw 20-byte digest in one call.
|
|
37
|
+
static std::array<uint8_t, 20> hash_raw(const uint8_t* data, size_t length);
|
|
38
|
+
|
|
31
39
|
private:
|
|
32
40
|
void process_block();
|
|
33
41
|
void reset();
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MIT */
|
|
1
2
|
/*
|
|
2
3
|
* Copyright (C) 2016 Southern Storm Software, Pty Ltd.
|
|
3
4
|
*
|
|
@@ -20,10 +21,10 @@
|
|
|
20
21
|
* DEALINGS IN THE SOFTWARE.
|
|
21
22
|
*/
|
|
22
23
|
|
|
23
|
-
#include "sha256.h"
|
|
24
|
+
#include "librats/crypto/sha256.h"
|
|
24
25
|
#include <string.h>
|
|
25
26
|
|
|
26
|
-
void
|
|
27
|
+
void rats_sha256_reset(rats_sha256_context_t *context)
|
|
27
28
|
{
|
|
28
29
|
context->h[0] = 0x6a09e667;
|
|
29
30
|
context->h[1] = 0xbb67ae85;
|
|
@@ -39,7 +40,7 @@ void sha256_reset(sha256_context_t *context)
|
|
|
39
40
|
|
|
40
41
|
#define rightRotate(v, n) (((v) >> (n)) | ((v) << (32 - (n))))
|
|
41
42
|
|
|
42
|
-
static void
|
|
43
|
+
static void rats_sha256_transform(rats_sha256_context_t *context, const uint8_t *m)
|
|
43
44
|
{
|
|
44
45
|
static uint32_t const k[64] = {
|
|
45
46
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
|
@@ -126,12 +127,12 @@ static void sha256_transform(sha256_context_t *context, const uint8_t *m)
|
|
|
126
127
|
context->h[7] += h;
|
|
127
128
|
}
|
|
128
129
|
|
|
129
|
-
void
|
|
130
|
+
void rats_sha256_update(rats_sha256_context_t *context, const void *data, size_t size)
|
|
130
131
|
{
|
|
131
132
|
const uint8_t *d = (const uint8_t *)data;
|
|
132
133
|
while (size > 0) {
|
|
133
134
|
if (context->posn == 0 && size >= 64) {
|
|
134
|
-
|
|
135
|
+
rats_sha256_transform(context, d);
|
|
135
136
|
d += 64;
|
|
136
137
|
size -= 64;
|
|
137
138
|
context->length += 64 * 8;
|
|
@@ -142,7 +143,7 @@ void sha256_update(sha256_context_t *context, const void *data, size_t size)
|
|
|
142
143
|
memcpy(context->m + context->posn, d, temp);
|
|
143
144
|
context->posn += (uint8_t)temp;
|
|
144
145
|
if (context->posn >= 64) {
|
|
145
|
-
|
|
146
|
+
rats_sha256_transform(context, context->m);
|
|
146
147
|
context->posn = 0;
|
|
147
148
|
}
|
|
148
149
|
d += temp;
|
|
@@ -160,7 +161,7 @@ static void write_be32(uint8_t *out, uint32_t value)
|
|
|
160
161
|
out[3] = (uint8_t)value;
|
|
161
162
|
}
|
|
162
163
|
|
|
163
|
-
void
|
|
164
|
+
void rats_sha256_finish(rats_sha256_context_t *context, uint8_t *hash)
|
|
164
165
|
{
|
|
165
166
|
uint8_t posn = context->posn;
|
|
166
167
|
if (posn <= (64 - 9)) {
|
|
@@ -169,21 +170,21 @@ void sha256_finish(sha256_context_t *context, uint8_t *hash)
|
|
|
169
170
|
} else {
|
|
170
171
|
context->m[posn] = 0x80;
|
|
171
172
|
memset(context->m + posn + 1, 0, 64 - (posn + 1));
|
|
172
|
-
|
|
173
|
+
rats_sha256_transform(context, context->m);
|
|
173
174
|
memset(context->m, 0, 64 - 8);
|
|
174
175
|
}
|
|
175
176
|
write_be32(context->m + 64 - 8, (uint32_t)(context->length >> 32));
|
|
176
177
|
write_be32(context->m + 64 - 4, (uint32_t)context->length);
|
|
177
|
-
|
|
178
|
+
rats_sha256_transform(context, context->m);
|
|
178
179
|
context->posn = 0;
|
|
179
180
|
for (posn = 0; posn < 8; ++posn)
|
|
180
181
|
write_be32(hash + posn * 4, context->h[posn]);
|
|
181
182
|
}
|
|
182
183
|
|
|
183
|
-
void
|
|
184
|
+
void rats_sha256_hash(uint8_t *hash, const void *data, size_t size)
|
|
184
185
|
{
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
186
|
+
rats_sha256_context_t context;
|
|
187
|
+
rats_sha256_reset(&context);
|
|
188
|
+
rats_sha256_update(&context, data, size);
|
|
189
|
+
rats_sha256_finish(&context, hash);
|
|
189
190
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MIT */
|
|
1
2
|
/*
|
|
2
3
|
* Copyright (C) 2016 Southern Storm Software, Pty Ltd.
|
|
3
4
|
*
|
|
@@ -30,8 +31,8 @@
|
|
|
30
31
|
extern "C" {
|
|
31
32
|
#endif
|
|
32
33
|
|
|
33
|
-
#define
|
|
34
|
-
#define
|
|
34
|
+
#define RATS_SHA256_HASH_SIZE 32
|
|
35
|
+
#define RATS_SHA256_BLOCK_SIZE 64
|
|
35
36
|
|
|
36
37
|
typedef struct
|
|
37
38
|
{
|
|
@@ -40,12 +41,12 @@ typedef struct
|
|
|
40
41
|
uint64_t length;
|
|
41
42
|
uint8_t posn;
|
|
42
43
|
|
|
43
|
-
}
|
|
44
|
+
} rats_sha256_context_t;
|
|
44
45
|
|
|
45
|
-
void
|
|
46
|
-
void
|
|
47
|
-
void
|
|
48
|
-
void
|
|
46
|
+
void rats_sha256_reset(rats_sha256_context_t *context);
|
|
47
|
+
void rats_sha256_update(rats_sha256_context_t *context, const void *data, size_t size);
|
|
48
|
+
void rats_sha256_finish(rats_sha256_context_t *context, uint8_t *hash);
|
|
49
|
+
void rats_sha256_hash(uint8_t *hash, const void *data, size_t size);
|
|
49
50
|
|
|
50
51
|
#ifdef __cplusplus
|
|
51
52
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MIT */
|
|
1
2
|
/*
|
|
2
3
|
* Copyright (C) 2016 Southern Storm Software, Pty Ltd.
|
|
3
4
|
*
|
|
@@ -20,10 +21,10 @@
|
|
|
20
21
|
* DEALINGS IN THE SOFTWARE.
|
|
21
22
|
*/
|
|
22
23
|
|
|
23
|
-
#include "sha512.h"
|
|
24
|
+
#include "librats/crypto/sha512.h"
|
|
24
25
|
#include <string.h>
|
|
25
26
|
|
|
26
|
-
void
|
|
27
|
+
void rats_sha512_reset(rats_sha512_context_t *context)
|
|
27
28
|
{
|
|
28
29
|
static uint64_t const hash_start[8] = {
|
|
29
30
|
0x6A09E667F3BCC908ULL, 0xBB67AE8584CAA73BULL, 0x3C6EF372FE94F82BULL,
|
|
@@ -37,7 +38,7 @@ void sha512_reset(sha512_context_t *context)
|
|
|
37
38
|
|
|
38
39
|
#define rightRotate(v, n) (((v) >> (n)) | ((v) << (64 - (n))))
|
|
39
40
|
|
|
40
|
-
static void
|
|
41
|
+
static void rats_sha512_transform(rats_sha512_context_t *context, const uint8_t *m)
|
|
41
42
|
{
|
|
42
43
|
static uint64_t const k[80] = {
|
|
43
44
|
0x428A2F98D728AE22ULL, 0x7137449123EF65CDULL, 0xB5C0FBCFEC4D3B2FULL,
|
|
@@ -139,12 +140,12 @@ static void sha512_transform(sha512_context_t *context, const uint8_t *m)
|
|
|
139
140
|
context->h[7] += h;
|
|
140
141
|
}
|
|
141
142
|
|
|
142
|
-
void
|
|
143
|
+
void rats_sha512_update(rats_sha512_context_t *context, const void *data, size_t size)
|
|
143
144
|
{
|
|
144
145
|
const uint8_t *d = (const uint8_t *)data;
|
|
145
146
|
while (size > 0) {
|
|
146
147
|
if (context->posn == 0 && size >= 128) {
|
|
147
|
-
|
|
148
|
+
rats_sha512_transform(context, d);
|
|
148
149
|
d += 128;
|
|
149
150
|
size -= 128;
|
|
150
151
|
context->length += 128 * 8;
|
|
@@ -155,7 +156,7 @@ void sha512_update(sha512_context_t *context, const void *data, size_t size)
|
|
|
155
156
|
memcpy(context->m + context->posn, d, temp);
|
|
156
157
|
context->posn += (uint8_t)temp;
|
|
157
158
|
if (context->posn >= 128) {
|
|
158
|
-
|
|
159
|
+
rats_sha512_transform(context, context->m);
|
|
159
160
|
context->posn = 0;
|
|
160
161
|
}
|
|
161
162
|
d += temp;
|
|
@@ -177,7 +178,7 @@ static void write_be64(uint8_t *out, uint64_t value)
|
|
|
177
178
|
out[7] = (uint8_t)value;
|
|
178
179
|
}
|
|
179
180
|
|
|
180
|
-
void
|
|
181
|
+
void rats_sha512_finish(rats_sha512_context_t *context, uint8_t *hash)
|
|
181
182
|
{
|
|
182
183
|
uint8_t posn = context->posn;
|
|
183
184
|
if (posn <= (128 - 17)) {
|
|
@@ -186,21 +187,21 @@ void sha512_finish(sha512_context_t *context, uint8_t *hash)
|
|
|
186
187
|
} else {
|
|
187
188
|
context->m[posn] = 0x80;
|
|
188
189
|
memset(context->m + posn + 1, 0, 128 - (posn + 1));
|
|
189
|
-
|
|
190
|
+
rats_sha512_transform(context, context->m);
|
|
190
191
|
memset(context->m, 0, 128 - 16);
|
|
191
192
|
}
|
|
192
193
|
write_be64(context->m + 128 - 16, 0);
|
|
193
194
|
write_be64(context->m + 128 - 8, context->length);
|
|
194
|
-
|
|
195
|
+
rats_sha512_transform(context, context->m);
|
|
195
196
|
context->posn = 0;
|
|
196
197
|
for (posn = 0; posn < 8; ++posn)
|
|
197
198
|
write_be64(hash + posn * 8, context->h[posn]);
|
|
198
199
|
}
|
|
199
200
|
|
|
200
|
-
void
|
|
201
|
+
void rats_sha512_hash(uint8_t *hash, const void *data, size_t size)
|
|
201
202
|
{
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
203
|
+
rats_sha512_context_t context;
|
|
204
|
+
rats_sha512_reset(&context);
|
|
205
|
+
rats_sha512_update(&context, data, size);
|
|
206
|
+
rats_sha512_finish(&context, hash);
|
|
206
207
|
}
|