static uint64_t get_platform_seed(uint64_t seed) { #ifdef SOL_PLATFORM_LINUX int ret; #endif /* SOL_PLATFORM_LINUX */ /* If a seed is provided, use it. */ if (seed) return seed; #ifdef SOL_PLATFORM_LINUX /* Use Linux-specific getrandom(2) if available to initialize the * seed. If syscall isn't available, read from /dev/urandom instead. */ ret = getrandom_shim(&seed, sizeof(seed), 0); if (ret == sizeof(seed)) return seed; #endif /* SOL_PLATFORM_LINUX */ /* Fall back to using a bad source of entropy if platform-specific, * higher quality random sources, are unavailable. */ #ifdef SOL_PLATFORM_ZEPHYR return ((uint64_t)sys_rand32_get() << 32) | sys_rand32_get(); #else return (uint64_t)time(NULL); #endif }
void net_dhcpv4_start(struct net_if *iface) { int ret; iface->dhcpv4.state = NET_DHCPV4_INIT; iface->dhcpv4.attempts = 0; iface->dhcpv4.lease_time = 0; iface->dhcpv4.renewal_time = 0; /* A DHCP client MUST choose xid's in such a way as to * minimize the change of using and xid identical to one used * by another client. Choose a random xid st startup and * increment it on each new request. */ iface->dhcpv4.xid = sys_rand32_get(); /* * Register UDP input callback on * DHCPV4_SERVER_PORT(67) and DHCPV4_CLIENT_PORT(68) for * all dhcpv4 related incoming packets. */ ret = net_udp_register(NULL, NULL, DHCPV4_SERVER_PORT, DHCPV4_CLIENT_PORT, net_dhcpv4_input, NULL, NULL); if (ret < 0) { NET_DBG("UDP callback registration failed"); return; } send_discover(iface); }
void sending(int resend) { static bool send_unicast = true; PRINT("%s: Sending packet\n", __func__); if (resend) { expecting = resend; } else { expecting = sys_rand32_get() % ipsum_len; } if (send_unicast) { if (send_packet(__func__, unicast, ipsum_len, expecting)) { PRINT("Unicast sending %d bytes FAIL\n", ipsum_len - expecting); } } else { if (send_packet(__func__, multicast, ipsum_len, expecting)) { PRINT("Multicast sending %d bytes FAIL\n", ipsum_len - expecting); } } }
static void send_tcp_data(struct net_app_ctx *ctx, struct data *data) { struct net_pkt *pkt; size_t len; int ret; do { data->expecting_tcp = sys_rand32_get() % ipsum_len; } while (data->expecting_tcp == 0); data->received_tcp = 0; pkt = prepare_send_pkt(ctx, data->proto, data->expecting_tcp); if (!pkt) { return; } len = net_pkt_get_len(pkt); NET_ASSERT_INFO(data->expecting_tcp == len, "%s data to send %d bytes, real len %zu", data->proto, data->expecting_tcp, len); ret = net_app_send_pkt(ctx, pkt, NULL, 0, K_FOREVER, UINT_TO_POINTER(len)); if (ret < 0) { NET_ERR("Cannot send %s data to peer (%d)", data->proto, ret); net_pkt_unref(pkt); } }
static void slip_iface_init(struct net_if *iface) { struct slip_context *slip = net_if_get_device(iface)->driver_data; struct net_linkaddr *ll_addr = slip_get_mac(slip); slip->init_done = true; slip->iface = iface; if (CONFIG_SLIP_MAC_ADDR[0] != 0) { if (_slip_mac_addr_from_str(slip, CONFIG_SLIP_MAC_ADDR) < 0) { goto use_random_mac; } } else { use_random_mac: /* 00-00-5E-00-53-xx Documentation RFC 7042 */ slip->mac_addr[0] = 0x00; slip->mac_addr[1] = 0x00; slip->mac_addr[2] = 0x5E; slip->mac_addr[3] = 0x00; slip->mac_addr[4] = 0x53; slip->mac_addr[5] = sys_rand32_get(); } net_if_set_link_addr(iface, ll_addr->addr, ll_addr->len, NET_LINK_ETHERNET); }
static void eth_iface_init(struct net_if *iface) { struct eth_context *ctx = net_if_get_device(iface)->driver_data; struct net_linkaddr *ll_addr = eth_get_mac(ctx); ctx->iface = iface; ethernet_init(iface); if (ctx->init_done) { return; } net_lldp_set_lldpdu(iface); ctx->init_done = true; #if defined(CONFIG_ETH_NATIVE_POSIX_RANDOM_MAC) /* 00-00-5E-00-53-xx Documentation RFC 7042 */ ctx->mac_addr[0] = 0x00; ctx->mac_addr[1] = 0x00; ctx->mac_addr[2] = 0x5E; ctx->mac_addr[3] = 0x00; ctx->mac_addr[4] = 0x53; ctx->mac_addr[5] = sys_rand32_get(); /* The TUN/TAP setup script will by default set the MAC address of host * interface to 00:00:5E:00:53:FF so do not allow that. */ if (ctx->mac_addr[5] == 0xff) { ctx->mac_addr[5] = 0x01; } #else if (CONFIG_ETH_NATIVE_POSIX_MAC_ADDR[0] != 0) { if (net_bytes_from_str(ctx->mac_addr, sizeof(ctx->mac_addr), CONFIG_ETH_NATIVE_POSIX_MAC_ADDR) < 0) { LOG_ERR("Invalid MAC address %s", CONFIG_ETH_NATIVE_POSIX_MAC_ADDR); } } #endif net_if_set_link_addr(iface, ll_addr->addr, ll_addr->len, NET_LINK_ETHERNET); ctx->if_name = ETH_NATIVE_POSIX_DRV_NAME; ctx->dev_fd = eth_iface_create(ctx->if_name, false); if (ctx->dev_fd < 0) { LOG_ERR("Cannot create %s (%d)", ctx->if_name, ctx->dev_fd); } else { /* Create a thread that will handle incoming data from host */ create_rx_handler(ctx); eth_setup_host(ctx->if_name); eth_start_script(ctx->if_name); } }
static int set_endpoint_name(char *ep_name, sa_family_t family) { int ret; ret = snprintk(ep_name, ENDPOINT_LEN, "%s-%s-%u", CONFIG_BOARD, (family == AF_INET6 ? "ipv6" : "ipv4"), sys_rand32_get()); if (ret < 0 || ret >= ENDPOINT_LEN) { SYS_LOG_ERR("Can't fill name buffer"); return -EINVAL; } return 0; }
/** * FIXME choose correct OUI, or add support in L2 */ static u8_t *get_mac(struct device *dev) { u32_t *ptr = (u32_t *)mac_addr; mac_addr[7] = 0x00; mac_addr[6] = 0x12; mac_addr[5] = 0x4b; mac_addr[4] = 0x00; UNALIGNED_PUT(sys_rand32_get(), ptr); mac_addr[0] = (mac_addr[0] & ~0x01) | 0x02; return mac_addr; }
static void prepare_mqtt_publish_msg(struct mqtt_publish_msg *pub_msg, enum mqtt_qos qos) { /* MQTT message payload may be anything, we we use C strings */ pub_msg->msg = get_mqtt_payload(qos); /* Payload's length */ pub_msg->msg_len = strlen(client_ctx.pub_msg.msg); /* MQTT Quality of Service */ pub_msg->qos = qos; /* Message's topic */ pub_msg->topic = get_mqtt_topic(); pub_msg->topic_len = strlen(client_ctx.pub_msg.topic); /* Packet Identifier, always use different values */ pub_msg->pkt_id = sys_rand32_get(); }
static char *get_mqtt_payload(enum mqtt_qos qos) { #if APP_BLUEMIX_TOPIC static char payload[30]; snprintk(payload, sizeof(payload), "{d:{temperature:%d}}", (u8_t)sys_rand32_get()); #else static char payload[] = "DOORS:OPEN_QoSx"; payload[strlen(payload) - 1] = '0' + qos; #endif return payload; }
static u8_t *net_arp_get_mac(struct device *dev) { struct net_arp_context *context = dev->driver_data; if (context->mac_addr[2] == 0x00) { /* 00-00-5E-00-53-xx Documentation RFC 7042 */ context->mac_addr[0] = 0x00; context->mac_addr[1] = 0x00; context->mac_addr[2] = 0x5E; context->mac_addr[3] = 0x00; context->mac_addr[4] = 0x53; context->mac_addr[5] = sys_rand32_get(); } return context->mac_addr; }
static int entropy_source(void *data, unsigned char *output, size_t len, size_t *olen) { u32_t seed; ARG_UNUSED(data); seed = sys_rand32_get(); if (len > sizeof(seed)) { len = sizeof(seed); } memcpy(output, &seed, len); *olen = len; return 0; }
void RegressionTaskEntry(void) { int tc_result; /* test result code */ uint32_t rnd_values[N_VALUES]; int i; PRINT_DATA("Starting random number tests\n"); PRINT_LINE; /* * Test subsequently calls sys_rand32_get(), checking * that two values are not equal. */ PRINT_DATA("Generating random numbers\n"); /* * Get several subsequent numbers as fast as possible. * If random number generator is based on timer, check * the situation when random number generator is called * faster than timer clock ticks. * In order to do this, make several subsequent calls * and save results in an array to verify them on the * next step */ for (i = 0; i < N_VALUES; i++) { rnd_values[i] = sys_rand32_get(); } for (tc_result = TC_PASS, i = 1; i < N_VALUES; i++) { if (rnd_values[i - 1] == rnd_values[i]) { tc_result = TC_FAIL; break; } } if (tc_result == TC_FAIL) { TC_ERROR("random number subsequent calls\n" "returned same value %d\n", rnd_values[i]); } else { PRINT_DATA("Generated %d values with expected randomness\n", N_VALUES); } TC_END_RESULT(tc_result); TC_END_REPORT(tc_result); }
static u8_t *net_iface_get_mac(struct device *dev) { struct net_if_test *data = dev->driver_data; if (data->mac_addr[2] == 0x00) { /* 00-00-5E-00-53-xx Documentation RFC 7042 */ data->mac_addr[0] = 0x00; data->mac_addr[1] = 0x00; data->mac_addr[2] = 0x5E; data->mac_addr[3] = 0x00; data->mac_addr[4] = 0x53; data->mac_addr[5] = sys_rand32_get(); } data->ll_addr.addr = data->mac_addr; data->ll_addr.len = 6; return data->mac_addr; }
/** * * @brief Initialize kernel * * This routine is invoked when the system is ready to run C code. The * processor must be running in 32-bit mode, and the BSS must have been * cleared/zeroed. * * @return Does not return */ FUNC_NORETURN void _Cstart(void) { #ifdef CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN void *dummy_thread = NULL; #else /* floating point is NOT used during kernel init */ char __stack dummy_stack[_K_THREAD_NO_FLOAT_SIZEOF]; void *dummy_thread = dummy_stack; #endif /* * Initialize kernel data structures. This step includes * initializing the interrupt subsystem, which must be performed * before the hardware initialization phase. */ prepare_multithreading(dummy_thread); /* perform basic hardware initialization */ _sys_device_do_config_level(_SYS_INIT_LEVEL_PRE_KERNEL_1); _sys_device_do_config_level(_SYS_INIT_LEVEL_PRE_KERNEL_2); /* initialize stack canaries */ #ifdef CONFIG_STACK_CANARIES __stack_chk_guard = (void *)sys_rand32_get(); #endif /* display boot banner */ PRINT_BOOT_BANNER(); switch_to_main_thread(); /* * Compiler can't tell that the above routines won't return and issues * a warning unless we explicitly tell it that control never gets this * far. */ CODE_UNREACHABLE; }
static inline u8_t *get_mac(struct device *dev) { struct upipe_context *upipe = dev->driver_data; upipe->mac_addr[0] = 0x00; upipe->mac_addr[1] = 0x10; upipe->mac_addr[2] = 0x20; upipe->mac_addr[3] = 0x30; #if defined(CONFIG_IEEE802154_UPIPE_RANDOM_MAC) UNALIGNED_PUT(sys_cpu_to_be32(sys_rand32_get()), (u32_t *) ((u8_t *)upipe->mac_addr+4)); #else upipe->mac_addr[4] = CONFIG_IEEE802154_UPIPE_MAC4; upipe->mac_addr[5] = CONFIG_IEEE802154_UPIPE_MAC5; upipe->mac_addr[6] = CONFIG_IEEE802154_UPIPE_MAC6; upipe->mac_addr[7] = CONFIG_IEEE802154_UPIPE_MAC7; #endif return upipe->mac_addr; }
static int entropy_source(void *data, unsigned char *output, size_t len, size_t *olen) { uint32_t seed; char *ptr = data; seed = sys_rand32_get(); if (!seed) { seed = 7; } for (int i = 0; i < len; i++) { seed ^= seed << 13; seed ^= seed >> 17; seed ^= seed << 5; *ptr++ = (char)seed; } *olen = len; return 0; }
static void csc_simulation(void) { static uint8_t i; uint32_t rand = sys_rand32_get(); bool nfy_crank = false, nfy_wheel = false; /* Measurements don't have to be updated every second */ if (!(i % 2)) { lwet += 1050 + rand % 50; cwr += 2; nfy_wheel = true; } if (!(i % 3)) { lcet += 1000 + rand % 50; ccr += 1; nfy_crank = true; } /* * In typical applications, the CSC Measurement characteristic is * notified approximately once per second. This interval may vary * and is determined by the Server and not required to be configurable * by the Client. */ measurement_nfy(NULL, nfy_wheel ? cwr : 0, nfy_wheel ? lwet : 0, nfy_crank ? ccr : 0, nfy_crank ? lcet : 0); /* * The Last Crank Event Time value and Last Wheel Event Time roll over * every 64 seconds. */ if (!(i % 64)) { lcet = 0; lwet = 0; i = 0; } i++; }