Commit f2b38bc0 authored by Samir Halilcevic's avatar Samir Halilcevic

Remove ripemd and system info, use random bytes instead

parent 840bb47a
...@@ -111,7 +111,6 @@ caf_add_component( ...@@ -111,7 +111,6 @@ caf_add_component(
caf/detail/behavior_stack.cpp caf/detail/behavior_stack.cpp
caf/detail/blocking_behavior.cpp caf/detail/blocking_behavior.cpp
caf/detail/config_consumer.cpp caf/detail/config_consumer.cpp
caf/detail/get_mac_addresses.cpp
caf/detail/get_process_id.cpp caf/detail/get_process_id.cpp
caf/detail/get_root_uuid.cpp caf/detail/get_root_uuid.cpp
caf/detail/glob_match.cpp caf/detail/glob_match.cpp
...@@ -133,7 +132,6 @@ caf_add_component( ...@@ -133,7 +132,6 @@ caf_add_component(
caf/detail/private_thread_pool.cpp caf/detail/private_thread_pool.cpp
caf/detail/rfc3629.cpp caf/detail/rfc3629.cpp
caf/detail/rfc3629.test.cpp caf/detail/rfc3629.test.cpp
caf/detail/ripemd_160.cpp
caf/detail/set_thread_name.cpp caf/detail/set_thread_name.cpp
caf/detail/stream_bridge.cpp caf/detail/stream_bridge.cpp
caf/detail/stringification_inspector.cpp caf/detail/stringification_inspector.cpp
...@@ -277,7 +275,6 @@ caf_add_component( ...@@ -277,7 +275,6 @@ caf_add_component(
detail.parser.read_unsigned_integer detail.parser.read_unsigned_integer
detail.private_thread_pool detail.private_thread_pool
detail.ringbuffer detail.ringbuffer
detail.ripemd_160
detail.type_id_list_builder detail.type_id_list_builder
detail.unique_function detail.unique_function
dictionary dictionary
......
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
#include "caf/detail/get_mac_addresses.hpp"
#include "caf/config.hpp"
#include "caf/detail/scope_guard.hpp"
#if defined(CAF_MACOS) || defined(CAF_BSD) || defined(CAF_IOS)
# include <arpa/inet.h>
# include <cerrno>
# include <cstdio>
# include <cstdlib>
# include <memory>
# include <net/if.h>
# include <net/if_dl.h>
# include <netinet/in.h>
# include <sstream>
# include <sys/ioctl.h>
# include <sys/socket.h>
# include <sys/sysctl.h>
# include <sys/types.h>
# include <iostream>
namespace caf {
namespace detail {
std::vector<iface_info> get_mac_addresses() {
int mib[6];
std::vector<iface_info> result;
mib[0] = CTL_NET;
mib[1] = AF_ROUTE;
mib[2] = 0;
mib[3] = AF_LINK;
mib[4] = NET_RT_IFLIST;
auto indices = if_nameindex();
std::vector<char> buf;
for (auto i = indices; !(i->if_index == 0 && i->if_name == nullptr); ++i) {
mib[5] = static_cast<int>(i->if_index);
size_t len;
if (sysctl(mib, 6, nullptr, &len, nullptr, 0) < 0) {
perror("sysctl 1 error");
exit(3);
}
if (buf.size() < len)
buf.resize(len);
CAF_ASSERT(len > 0);
if (sysctl(mib, 6, buf.data(), &len, nullptr, 0) < 0) {
perror("sysctl 2 error");
exit(5);
}
auto ifm = reinterpret_cast<if_msghdr*>(buf.data());
auto sdl = reinterpret_cast<sockaddr_dl*>(ifm + 1);
constexpr auto mac_addr_len = 6;
if (sdl->sdl_alen != mac_addr_len)
continue;
auto ptr = reinterpret_cast<unsigned char*>(LLADDR(sdl));
auto uctoi = [](unsigned char c) -> unsigned {
return static_cast<unsigned char>(c);
};
std::ostringstream oss;
oss << std::hex;
oss.fill('0');
oss.width(2);
oss << uctoi(*ptr++);
for (auto j = 0; j < mac_addr_len - 1; ++j) {
oss << ":";
oss.width(2);
oss << uctoi(*ptr++);
}
auto addr = oss.str();
if (addr != "00:00:00:00:00:00") {
result.emplace_back(i->if_name, std::move(addr));
}
}
if_freenameindex(indices);
return result;
}
} // namespace detail
} // namespace caf
#elif defined(CAF_LINUX) || defined(CAF_ANDROID) || defined(CAF_CYGWIN)
# include <algorithm>
# include <cctype>
# include <cstring>
# include <fstream>
# include <iostream>
# include <iterator>
# include <net/if.h>
# include <sstream>
# include <stdio.h>
# include <string>
# include <sys/ioctl.h>
# include <sys/socket.h>
# include <sys/types.h>
# include <unistd.h>
# include <vector>
namespace caf::detail {
std::vector<iface_info> get_mac_addresses() {
// get a socket handle
int socktype = SOCK_DGRAM;
# ifdef SOCK_CLOEXEC
socktype |= SOCK_CLOEXEC;
# endif
int sck = socket(AF_INET, socktype, 0);
if (sck < 0) {
perror("socket");
return {};
}
auto g = make_scope_guard([&] { close(sck); });
// query available interfaces
char buf[1024] = {0};
ifconf ifc;
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if (ioctl(sck, SIOCGIFCONF, &ifc) < 0) {
perror("ioctl(SIOCGIFCONF)");
return {};
}
std::vector<iface_info> result;
auto ctoi = [](char c) -> unsigned { return static_cast<unsigned char>(c); };
// iterate through interfaces
auto ifr = ifc.ifc_req;
auto num_ifaces = static_cast<size_t>(ifc.ifc_len) / sizeof(ifreq);
for (size_t i = 0; i < num_ifaces; ++i) {
auto item = &ifr[i];
// get mac address
if (ioctl(sck, SIOCGIFHWADDR, item) < 0) {
perror("ioctl(SIOCGIFHWADDR)");
return {};
}
std::ostringstream oss;
oss << std::hex;
oss.width(2);
oss << ctoi(item->ifr_hwaddr.sa_data[0]);
for (size_t j = 1; j < 6; ++j) {
oss << ":";
oss.width(2);
oss << ctoi(item->ifr_hwaddr.sa_data[j]);
}
auto addr = oss.str();
if (addr != "00:00:00:00:00:00") {
result.push_back({item->ifr_name, std::move(addr)});
}
}
return result;
}
} // namespace caf::detail
#else
// windows
// clang-format off
# include <ws2tcpip.h>
# include <winsock2.h>
# include <iphlpapi.h>
// clang-format on
# include <algorithm>
# include <cctype>
# include <cstdio>
# include <cstdlib>
# include <cstring>
# include <fstream>
# include <iostream>
# include <iterator>
# include <memory>
# include <sstream>
# include <string>
# include <vector>
namespace {
constexpr size_t working_buffer_size = 15 * 1024; // 15kb by default
constexpr size_t max_iterations = 3;
struct c_free {
template <class T>
void operator()(T* ptr) const {
free(ptr);
}
};
} // namespace
namespace caf {
namespace detail {
std::vector<iface_info> get_mac_addresses() {
// result vector
std::vector<iface_info> result;
// flags to pass to GetAdaptersAddresses
ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
// default to unspecified address family (both)
ULONG family = AF_UNSPEC;
// buffer
std::unique_ptr<IP_ADAPTER_ADDRESSES, c_free> addresses;
// init buf size to default, adjusted by GetAdaptersAddresses if needed
ULONG addresses_len = working_buffer_size;
// stores result of latest system call
DWORD res = 0;
// break condition
size_t iterations = 0;
do {
addresses.reset((IP_ADAPTER_ADDRESSES*) malloc(addresses_len));
if (!addresses) {
perror("Memory allocation failed for IP_ADAPTER_ADDRESSES struct");
exit(1);
}
res = GetAdaptersAddresses(family, flags, nullptr, addresses.get(),
&addresses_len);
} while ((res == ERROR_BUFFER_OVERFLOW) && (++iterations < max_iterations));
if (res == NO_ERROR) {
// read hardware addresses from the output we've received
for (auto addr = addresses.get(); addr != nullptr; addr = addr->Next) {
if (addr->PhysicalAddressLength > 0) {
std::ostringstream oss;
oss << std::hex;
oss.width(2);
oss << static_cast<int>(addr->PhysicalAddress[0]);
for (DWORD i = 1; i < addr->PhysicalAddressLength; ++i) {
oss << ":";
oss.width(2);
oss << static_cast<int>(addr->PhysicalAddress[i]);
}
auto hw_addr = oss.str();
if (hw_addr != "00:00:00:00:00:00") {
result.push_back({addr->AdapterName, std::move(hw_addr)});
}
}
}
} else {
if (res == ERROR_NO_DATA) {
perror("No addresses were found for the requested parameters");
} else {
perror("Call to GetAdaptersAddresses failed with error");
}
}
return result;
}
} // namespace detail
} // namespace caf
#endif
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
#pragma once
#include <string>
#include <utility>
#include <vector>
#include "caf/detail/core_export.hpp"
namespace caf::detail {
using iface_info = std::pair<std::string /* interface name */,
std::string /* interface address */>;
CAF_CORE_EXPORT std::vector<iface_info> get_mac_addresses();
} // namespace caf::detail
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
// Based on http://homes.esat.kuleuven.be/~cosicart/ps/AB-9601/rmd160.c;
// original header:
//
// AUTHOR: Antoon Bosselaers, ESAT-COSIC
// DATE: 1 March 1996
// VERSION: 1.0
//
// Copyright (c) Katholieke Universiteit Leuven
// 1996, All Rights Reserved
//
// Conditions for use of the RIPEMD-160 Software
//
// The RIPEMD-160 software is freely available for use under the terms and
// conditions described hereunder, which shall be deemed to be accepted by
// any user of the software and applicable on any use of the software:
//
// 1. K.U.Leuven Department of Electrical Engineering-ESAT/COSIC shall for
// all purposes be considered the owner of the RIPEMD-160 software and of
// all copyright, trade secret, patent or other intellectual property
// rights therein.
// 2. The RIPEMD-160 software is provided on an "as is" basis without
// warranty of any sort, express or implied. K.U.Leuven makes no
// representation that the use of the software will not infringe any
// patent or proprietary right of third parties. User will indemnify
// K.U.Leuven and hold K.U.Leuven harmless from any claims or liabilities
// which may arise as a result of its use of the software. In no
// circumstances K.U.Leuven R&D will be held liable for any deficiency,
// fault or other mishappening with regard to the use or performance of
// the software.
// 3. User agrees to give due credit to K.U.Leuven in scientific
// publications or communications in relation with the use of the
// RIPEMD-160 software as follows: RIPEMD-160 software written by
// Antoon Bosselaers,
// available at http://www.esat.kuleuven.be/~cosicart/ps/AB-9601/.
#include "caf/detail/ripemd_160.hpp"
#include <cstring>
namespace {
// typedef 8 and 32 bit types, resp.
// adapt these, if necessary, for your operating system and compiler
using byte = unsigned char;
using dword = uint32_t;
static_assert(sizeof(dword) == sizeof(unsigned), "platform not supported");
// macro definitions
// collect four bytes into one word:
#define BYTES_TO_DWORD(strptr) \
((static_cast<dword>(*((strptr) + 3)) << 24) \
| (static_cast<dword>(*((strptr) + 2)) << 16) \
| (static_cast<dword>(*((strptr) + 1)) << 8) \
| (static_cast<dword>(*(strptr))))
// ROL(x, n) cyclically rotates x over n bits to the left
// x must be of an unsigned 32 bits type and 0 <= n < 32.
#define ROL(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
// the five basic functions F(), G() and H()
#define F(x, y, z) ((x) ^ (y) ^ (z))
#define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
#define H(x, y, z) (((x) | ~(y)) ^ (z))
#define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
#define J(x, y, z) ((x) ^ ((y) | ~(z)))
// the ten basic operations FF() through III()
#define FF(a, b, c, d, e, x, s) \
{ \
(a) += F((b), (c), (d)) + (x); \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define GG(a, b, c, d, e, x, s) \
{ \
(a) += G((b), (c), (d)) + (x) + 0x5a827999U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define HH(a, b, c, d, e, x, s) \
{ \
(a) += H((b), (c), (d)) + (x) + 0x6ed9eba1U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define II(a, b, c, d, e, x, s) \
{ \
(a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcU; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define JJ(a, b, c, d, e, x, s) \
{ \
(a) += J((b), (c), (d)) + (x) + 0xa953fd4eU; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define FFF(a, b, c, d, e, x, s) \
{ \
(a) += F((b), (c), (d)) + (x); \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define GGG(a, b, c, d, e, x, s) \
{ \
(a) += G((b), (c), (d)) + (x) + 0x7a6d76e9U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define HHH(a, b, c, d, e, x, s) \
{ \
(a) += H((b), (c), (d)) + (x) + 0x6d703ef3U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define III(a, b, c, d, e, x, s) \
{ \
(a) += I((b), (c), (d)) + (x) + 0x5c4dd124U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define JJJ(a, b, c, d, e, x, s) \
{ \
(a) += J((b), (c), (d)) + (x) + 0x50a28be6U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
void MDinit(dword* MDbuf) {
MDbuf[0] = 0x67452301UL;
MDbuf[1] = 0xefcdab89UL;
MDbuf[2] = 0x98badcfeUL;
MDbuf[3] = 0x10325476UL;
MDbuf[4] = 0xc3d2e1f0UL;
}
void compress(dword* MDbuf, dword* X) {
// round 1-5 variables
dword aa = MDbuf[0];
dword bb = MDbuf[1];
dword cc = MDbuf[2];
dword dd = MDbuf[3];
dword ee = MDbuf[4];
// parallel round 1-5 variables
dword aaa = MDbuf[0];
dword bbb = MDbuf[1];
dword ccc = MDbuf[2];
dword ddd = MDbuf[3];
dword eee = MDbuf[4];
// round 1
FF(aa, bb, cc, dd, ee, X[0], 11);
FF(ee, aa, bb, cc, dd, X[1], 14);
FF(dd, ee, aa, bb, cc, X[2], 15);
FF(cc, dd, ee, aa, bb, X[3], 12);
FF(bb, cc, dd, ee, aa, X[4], 5);
FF(aa, bb, cc, dd, ee, X[5], 8);
FF(ee, aa, bb, cc, dd, X[6], 7);
FF(dd, ee, aa, bb, cc, X[7], 9);
FF(cc, dd, ee, aa, bb, X[8], 11);
FF(bb, cc, dd, ee, aa, X[9], 13);
FF(aa, bb, cc, dd, ee, X[10], 14);
FF(ee, aa, bb, cc, dd, X[11], 15);
FF(dd, ee, aa, bb, cc, X[12], 6);
FF(cc, dd, ee, aa, bb, X[13], 7);
FF(bb, cc, dd, ee, aa, X[14], 9);
FF(aa, bb, cc, dd, ee, X[15], 8);
// round 2
GG(ee, aa, bb, cc, dd, X[7], 7);
GG(dd, ee, aa, bb, cc, X[4], 6);
GG(cc, dd, ee, aa, bb, X[13], 8);
GG(bb, cc, dd, ee, aa, X[1], 13);
GG(aa, bb, cc, dd, ee, X[10], 11);
GG(ee, aa, bb, cc, dd, X[6], 9);
GG(dd, ee, aa, bb, cc, X[15], 7);
GG(cc, dd, ee, aa, bb, X[3], 15);
GG(bb, cc, dd, ee, aa, X[12], 7);
GG(aa, bb, cc, dd, ee, X[0], 12);
GG(ee, aa, bb, cc, dd, X[9], 15);
GG(dd, ee, aa, bb, cc, X[5], 9);
GG(cc, dd, ee, aa, bb, X[2], 11);
GG(bb, cc, dd, ee, aa, X[14], 7);
GG(aa, bb, cc, dd, ee, X[11], 13);
GG(ee, aa, bb, cc, dd, X[8], 12);
// round 3
HH(dd, ee, aa, bb, cc, X[3], 11);
HH(cc, dd, ee, aa, bb, X[10], 13);
HH(bb, cc, dd, ee, aa, X[14], 6);
HH(aa, bb, cc, dd, ee, X[4], 7);
HH(ee, aa, bb, cc, dd, X[9], 14);
HH(dd, ee, aa, bb, cc, X[15], 9);
HH(cc, dd, ee, aa, bb, X[8], 13);
HH(bb, cc, dd, ee, aa, X[1], 15);
HH(aa, bb, cc, dd, ee, X[2], 14);
HH(ee, aa, bb, cc, dd, X[7], 8);
HH(dd, ee, aa, bb, cc, X[0], 13);
HH(cc, dd, ee, aa, bb, X[6], 6);
HH(bb, cc, dd, ee, aa, X[13], 5);
HH(aa, bb, cc, dd, ee, X[11], 12);
HH(ee, aa, bb, cc, dd, X[5], 7);
HH(dd, ee, aa, bb, cc, X[12], 5);
// round 4
II(cc, dd, ee, aa, bb, X[1], 11);
II(bb, cc, dd, ee, aa, X[9], 12);
II(aa, bb, cc, dd, ee, X[11], 14);
II(ee, aa, bb, cc, dd, X[10], 15);
II(dd, ee, aa, bb, cc, X[0], 14);
II(cc, dd, ee, aa, bb, X[8], 15);
II(bb, cc, dd, ee, aa, X[12], 9);
II(aa, bb, cc, dd, ee, X[4], 8);
II(ee, aa, bb, cc, dd, X[13], 9);
II(dd, ee, aa, bb, cc, X[3], 14);
II(cc, dd, ee, aa, bb, X[7], 5);
II(bb, cc, dd, ee, aa, X[15], 6);
II(aa, bb, cc, dd, ee, X[14], 8);
II(ee, aa, bb, cc, dd, X[5], 6);
II(dd, ee, aa, bb, cc, X[6], 5);
II(cc, dd, ee, aa, bb, X[2], 12);
// round 5
JJ(bb, cc, dd, ee, aa, X[4], 9);
JJ(aa, bb, cc, dd, ee, X[0], 15);
JJ(ee, aa, bb, cc, dd, X[5], 5);
JJ(dd, ee, aa, bb, cc, X[9], 11);
JJ(cc, dd, ee, aa, bb, X[7], 6);
JJ(bb, cc, dd, ee, aa, X[12], 8);
JJ(aa, bb, cc, dd, ee, X[2], 13);
JJ(ee, aa, bb, cc, dd, X[10], 12);
JJ(dd, ee, aa, bb, cc, X[14], 5);
JJ(cc, dd, ee, aa, bb, X[1], 12);
JJ(bb, cc, dd, ee, aa, X[3], 13);
JJ(aa, bb, cc, dd, ee, X[8], 14);
JJ(ee, aa, bb, cc, dd, X[11], 11);
JJ(dd, ee, aa, bb, cc, X[6], 8);
JJ(cc, dd, ee, aa, bb, X[15], 5);
JJ(bb, cc, dd, ee, aa, X[13], 6);
// parallel round 1
JJJ(aaa, bbb, ccc, ddd, eee, X[5], 8);
JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9);
JJJ(ddd, eee, aaa, bbb, ccc, X[7], 9);
JJJ(ccc, ddd, eee, aaa, bbb, X[0], 11);
JJJ(bbb, ccc, ddd, eee, aaa, X[9], 13);
JJJ(aaa, bbb, ccc, ddd, eee, X[2], 15);
JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
JJJ(ddd, eee, aaa, bbb, ccc, X[4], 5);
JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7);
JJJ(bbb, ccc, ddd, eee, aaa, X[6], 7);
JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8);
JJJ(eee, aaa, bbb, ccc, ddd, X[8], 11);
JJJ(ddd, eee, aaa, bbb, ccc, X[1], 14);
JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
JJJ(bbb, ccc, ddd, eee, aaa, X[3], 12);
JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
// parallel round 2
III(eee, aaa, bbb, ccc, ddd, X[6], 9);
III(ddd, eee, aaa, bbb, ccc, X[11], 13);
III(ccc, ddd, eee, aaa, bbb, X[3], 15);
III(bbb, ccc, ddd, eee, aaa, X[7], 7);
III(aaa, bbb, ccc, ddd, eee, X[0], 12);
III(eee, aaa, bbb, ccc, ddd, X[13], 8);
III(ddd, eee, aaa, bbb, ccc, X[5], 9);
III(ccc, ddd, eee, aaa, bbb, X[10], 11);
III(bbb, ccc, ddd, eee, aaa, X[14], 7);
III(aaa, bbb, ccc, ddd, eee, X[15], 7);
III(eee, aaa, bbb, ccc, ddd, X[8], 12);
III(ddd, eee, aaa, bbb, ccc, X[12], 7);
III(ccc, ddd, eee, aaa, bbb, X[4], 6);
III(bbb, ccc, ddd, eee, aaa, X[9], 15);
III(aaa, bbb, ccc, ddd, eee, X[1], 13);
III(eee, aaa, bbb, ccc, ddd, X[2], 11);
// parallel round 3
HHH(ddd, eee, aaa, bbb, ccc, X[15], 9);
HHH(ccc, ddd, eee, aaa, bbb, X[5], 7);
HHH(bbb, ccc, ddd, eee, aaa, X[1], 15);
HHH(aaa, bbb, ccc, ddd, eee, X[3], 11);
HHH(eee, aaa, bbb, ccc, ddd, X[7], 8);
HHH(ddd, eee, aaa, bbb, ccc, X[14], 6);
HHH(ccc, ddd, eee, aaa, bbb, X[6], 6);
HHH(bbb, ccc, ddd, eee, aaa, X[9], 14);
HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
HHH(eee, aaa, bbb, ccc, ddd, X[8], 13);
HHH(ddd, eee, aaa, bbb, ccc, X[12], 5);
HHH(ccc, ddd, eee, aaa, bbb, X[2], 14);
HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
HHH(aaa, bbb, ccc, ddd, eee, X[0], 13);
HHH(eee, aaa, bbb, ccc, ddd, X[4], 7);
HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
// parallel round 4
GGG(ccc, ddd, eee, aaa, bbb, X[8], 15);
GGG(bbb, ccc, ddd, eee, aaa, X[6], 5);
GGG(aaa, bbb, ccc, ddd, eee, X[4], 8);
GGG(eee, aaa, bbb, ccc, ddd, X[1], 11);
GGG(ddd, eee, aaa, bbb, ccc, X[3], 14);
GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
GGG(bbb, ccc, ddd, eee, aaa, X[15], 6);
GGG(aaa, bbb, ccc, ddd, eee, X[0], 14);
GGG(eee, aaa, bbb, ccc, ddd, X[5], 6);
GGG(ddd, eee, aaa, bbb, ccc, X[12], 9);
GGG(ccc, ddd, eee, aaa, bbb, X[2], 12);
GGG(bbb, ccc, ddd, eee, aaa, X[13], 9);
GGG(aaa, bbb, ccc, ddd, eee, X[9], 12);
GGG(eee, aaa, bbb, ccc, ddd, X[7], 5);
GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
GGG(ccc, ddd, eee, aaa, bbb, X[14], 8);
// parallel round 5
FFF(bbb, ccc, ddd, eee, aaa, X[12], 8);
FFF(aaa, bbb, ccc, ddd, eee, X[15], 5);
FFF(eee, aaa, bbb, ccc, ddd, X[10], 12);
FFF(ddd, eee, aaa, bbb, ccc, X[4], 9);
FFF(ccc, ddd, eee, aaa, bbb, X[1], 12);
FFF(bbb, ccc, ddd, eee, aaa, X[5], 5);
FFF(aaa, bbb, ccc, ddd, eee, X[8], 14);
FFF(eee, aaa, bbb, ccc, ddd, X[7], 6);
FFF(ddd, eee, aaa, bbb, ccc, X[6], 8);
FFF(ccc, ddd, eee, aaa, bbb, X[2], 13);
FFF(bbb, ccc, ddd, eee, aaa, X[13], 6);
FFF(aaa, bbb, ccc, ddd, eee, X[14], 5);
FFF(eee, aaa, bbb, ccc, ddd, X[0], 15);
FFF(ddd, eee, aaa, bbb, ccc, X[3], 13);
FFF(ccc, ddd, eee, aaa, bbb, X[9], 11);
FFF(bbb, ccc, ddd, eee, aaa, X[11], 11);
// combine results
ddd += cc + MDbuf[1]; // final result for MDbuf[0]
MDbuf[1] = MDbuf[2] + dd + eee;
MDbuf[2] = MDbuf[3] + ee + aaa;
MDbuf[3] = MDbuf[4] + aa + bbb;
MDbuf[4] = MDbuf[0] + bb + ccc;
MDbuf[0] = ddd;
}
void MDfinish(dword* MDbuf, const byte* strptr, dword lswlen, dword mswlen) {
dword X[16]; // message words
memset(X, 0, 16 * sizeof(dword));
// put bytes from strptr into X
for (unsigned int i = 0; i < (lswlen & 63); ++i) {
// byte i goes into word X[i div 4] at pos. 8*(i mod 4)
X[i >> 2] ^= static_cast<dword>(*strptr++) << (8 * (i & 3));
}
// append the bit n_ == 1
X[(lswlen >> 2) & 15] ^= static_cast<dword>(1) << (8 * (lswlen & 3) + 7);
if ((lswlen & 63) > 55) {
// length goes to next block
compress(MDbuf, X);
memset(X, 0, 16 * sizeof(dword));
}
// append length in bits
X[14] = lswlen << 3;
X[15] = (lswlen >> 29) | (mswlen << 3);
compress(MDbuf, X);
}
} // namespace
namespace caf::detail {
void ripemd_160(std::array<uint8_t, 20>& storage, const std::string& data) {
dword MDbuf[5]; // contains (A, B, C, D(, E))
dword X[16]; // current 16-word chunk
dword length; // length in bytes of message
auto message = reinterpret_cast<const unsigned char*>(data.c_str());
// initialize
MDinit(MDbuf);
length = static_cast<dword>(data.size());
// process message in 16-word chunks
for (dword nbytes = length; nbytes > 63; nbytes -= 64) {
for (auto& i : X) {
i = BYTES_TO_DWORD(message);
message += 4;
}
compress(MDbuf, X);
}
// length mod 64 bytes left
// finish:
MDfinish(MDbuf, message, length, 0);
for (size_t i = 0; i < storage.size(); i += 4) {
// extracts the 8 least significant bits by casting to byte
storage[i] = static_cast<uint8_t>(MDbuf[i >> 2]);
storage[i + 1] = static_cast<uint8_t>(MDbuf[i >> 2] >> 8);
storage[i + 2] = static_cast<uint8_t>(MDbuf[i >> 2] >> 16);
storage[i + 3] = static_cast<uint8_t>(MDbuf[i >> 2] >> 24);
}
}
} // namespace caf::detail
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
// Based on http://homes.esat.kuleuven.be/~cosicart/ps/AB-9601/rmd160.h;
// original header:
//
// AUTHOR: Antoon Bosselaers, ESAT-COSIC
// DATE: 1 March 1996
// VERSION: 1.0
//
// Copyright (c) Katholieke Universiteit Leuven
// 1996, All Rights Reserved
//
// Conditions for use of the RIPEMD-160 Software
//
// The RIPEMD-160 software is freely available for use under the terms and
// conditions described hereunder, which shall be deemed to be accepted by
// any user of the software and applicable on any use of the software:
//
// 1. K.U.Leuven Department of Electrical Engineering-ESAT/COSIC shall for
// all purposes be considered the owner of the RIPEMD-160 software and of
// all copyright, trade secret, patent or other intellectual property
// rights therein.
// 2. The RIPEMD-160 software is provided on an "as is" basis without
// warranty of any sort, express or implied. K.U.Leuven makes no
// representation that the use of the software will not infringe any
// patent or proprietary right of third parties. User will indemnify
// K.U.Leuven and hold K.U.Leuven harmless from any claims or liabilities
// which may arise as a result of its use of the software. In no
// circumstances K.U.Leuven R&D will be held liable for any deficiency,
// fault or other mishappening with regard to the use or performance of
// the software.
// 3. User agrees to give due credit to K.U.Leuven in scientific publications
// or communications in relation with the use of the RIPEMD-160 software
// as follows: RIPEMD-160 software written by Antoon Bosselaers,
// available at http://www.esat.kuleuven.be/~cosicart/ps/AB-9601/.
#pragma once
#include <array>
#include <cstdint>
#include <string>
#include "caf/detail/core_export.hpp"
namespace caf::detail {
/// Creates a hash from `data` using the RIPEMD-160 algorithm.
CAF_CORE_EXPORT void ripemd_160(std::array<uint8_t, 20>& storage,
const std::string& data);
} // namespace caf::detail
...@@ -95,29 +95,18 @@ bool hashed_node_id::can_parse(std::string_view str) noexcept { ...@@ -95,29 +95,18 @@ bool hashed_node_id::can_parse(std::string_view str) noexcept {
node_id hashed_node_id::local(const actor_system_config&) { node_id hashed_node_id::local(const actor_system_config&) {
CAF_LOG_TRACE(""); CAF_LOG_TRACE("");
auto ifs = detail::get_mac_addresses(); // We add an global incrementing counter to make sure two actor systems in the
std::vector<std::string> macs; // same process won't have the same node ID - even if the user manipulates the
macs.reserve(ifs.size()); // system to always produce the same seed for its randomness.
for (auto& i : ifs) auto sys_seed = static_cast<uint8_t>(system_id.fetch_add(1));
macs.emplace_back(std::move(i.second));
auto seeded_hd_serial_and_mac_addr = join(macs, "") + detail::get_root_uuid();
// By adding 8 random ASCII characters, we make sure to assign a new (random)
// ID to a node every time we start it. Otherwise, we might run into issues
// where a restarted node produces identical actor IDs than the node it
// replaces. Especially when running nodes in a container, because then the
// process ID is most likely the same.
std::random_device rd; std::random_device rd;
std::minstd_rand gen{rd()}; std::minstd_rand gen{rd() + sys_seed};
std::uniform_int_distribution<> dis(33, 126); // uniform_int_distribution doesn't accept uint8_t as template parameter
for (int i = 0; i < 8; ++i) std::uniform_int_distribution<> dis(std::numeric_limits<uint8_t>::min(),
seeded_hd_serial_and_mac_addr += static_cast<char>(dis(gen)); std::numeric_limits<uint8_t>::max());
// One final tweak: we add another character that makes sure two actor systems
// in the same process won't have the same node ID - even if the user
// manipulates the system to always produce the same seed for its randomness.
auto sys_seed = static_cast<char>(system_id.fetch_add(1) + 33);
seeded_hd_serial_and_mac_addr += sys_seed;
host_id_type hid; host_id_type hid;
detail::ripemd_160(hid, seeded_hd_serial_and_mac_addr); for (auto& x : hid)
x = static_cast<uint8_t>(dis(gen));
return make_node_id(detail::get_process_id(), hid); return make_node_id(detail::get_process_id(), hid);
} }
......
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
#define CAF_SUITE detail.ripemd_160
#include "caf/detail/ripemd_160.hpp"
#include "core-test.hpp"
#include <iomanip>
#include <iostream>
using caf::detail::ripemd_160;
namespace {
std::string str_hash(const std::string& what) {
std::array<uint8_t, 20> hash;
ripemd_160(hash, what);
std::ostringstream oss;
oss << std::setfill('0') << std::hex;
for (auto i : hash) {
oss << std::setw(2) << static_cast<int>(i);
}
return oss.str();
}
} // namespace
// verify ripemd implementation with example hash results from
// http://homes.esat.kuleuven.be/~bosselae/ripemd160.html
CAF_TEST(hash_results) {
CHECK_EQ("9c1185a5c5e9fc54612808977ee8f548b2258d31", str_hash(""));
CHECK_EQ("0bdc9d2d256b3ee9daae347be6f4dc835a467ffe", str_hash("a"));
CHECK_EQ("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc", str_hash("abc"));
CHECK_EQ("5d0689ef49d2fae572b881b123a85ffa21595f36",
str_hash("message digest"));
CHECK_EQ("f71c27109c692c1b56bbdceb5b9d2865b3708dbc",
str_hash("abcdefghijklmnopqrstuvwxyz"));
CHECK_EQ("12a053384a9c0c88e405a06c27dcf49ada62eb2b",
str_hash("abcdbcdecdefdefgefghfghighij"
"hijkijkljklmklmnlmnomnopnopq"));
CHECK_EQ("b0e20b6e3116640286ed3a87a5713079b21f5189",
str_hash("ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
"fghijklmnopqrstuvwxyz0123456789"));
CHECK_EQ("9b752e45573d4b39f4dbd3323cab82bf63326bfb",
str_hash("1234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890"));
}
...@@ -18,11 +18,8 @@ ...@@ -18,11 +18,8 @@
#include "caf/after.hpp" #include "caf/after.hpp"
#include "caf/config.hpp" #include "caf/config.hpp"
#include "caf/defaults.hpp" #include "caf/defaults.hpp"
#include "caf/detail/get_mac_addresses.hpp"
#include "caf/detail/get_root_uuid.hpp"
#include "caf/detail/latch.hpp" #include "caf/detail/latch.hpp"
#include "caf/detail/prometheus_broker.hpp" #include "caf/detail/prometheus_broker.hpp"
#include "caf/detail/ripemd_160.hpp"
#include "caf/detail/set_thread_name.hpp" #include "caf/detail/set_thread_name.hpp"
#include "caf/event_based_actor.hpp" #include "caf/event_based_actor.hpp"
#include "caf/function_view.hpp" #include "caf/function_view.hpp"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment