diff --git a/fakeradio.c b/fakeradio.c index 9bc19a75..21107d2b 100644 --- a/fakeradio.c +++ b/fakeradio.c @@ -430,7 +430,11 @@ int main(int argc,char **argv) if (argc>=2) ber=calc_ber(atof(argv[2])); } - + { + struct timeval tv; + gettimeofday(&tv, NULL); + srandom((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec); + } struct pollfd fds[2]; struct radio_state radios[2]; diff --git a/keyring.c b/keyring.c index 4374e451..416d10a2 100644 --- a/keyring.c +++ b/keyring.c @@ -105,8 +105,7 @@ static int keyring_initialise(keyring_file *k) return WHYF_perror("fseeko(%d, 0, SEEK_SET)", fileno(k->file)); unsigned char buffer[KEYRING_PAGE_SIZE]; bzero(&buffer[0], KEYRING_BAM_BYTES); - if (urandombytes(&buffer[KEYRING_BAM_BYTES], KEYRING_PAGE_SIZE - KEYRING_BAM_BYTES)) - return WHYF("Could not generate random keyring salt"); + randombytes_buf(&buffer[KEYRING_BAM_BYTES], KEYRING_PAGE_SIZE - KEYRING_BAM_BYTES); if (fwrite(buffer, KEYRING_PAGE_SIZE, 1, k->file) != 1) { WHYF_perror("fwrite(%p, %zu, 1, %d)", buffer, KEYRING_PAGE_SIZE - KEYRING_BAM_BYTES, fileno(k->file)); return WHYF("Could not write page into keyring file"); @@ -486,7 +485,7 @@ static void create_cryptosign(keypair *kp) static void create_rhizome(keypair *kp) { - urandombytes(kp->private_key, kp->private_key_len); + randombytes_buf(kp->private_key, kp->private_key_len); } static int pack_private_only(const keypair *kp, struct rotbuf *rb) @@ -852,8 +851,7 @@ static int keyring_pack_identity(const keyring_identity *id, unsigned char packe { /* Convert an identity to a KEYRING_PAGE_SIZE bytes long block that consists of 32 bytes of random * salt, a 64 byte (512 bit) message authentication code (MAC) and the list of key pairs. */ - if (urandombytes(packed, PKR_SALT_BYTES) == -1) - return WHY("Could not generate salt"); + randombytes_buf(packed, PKR_SALT_BYTES); /* Calculate MAC */ if (keyring_identity_mac(id, packed /* pkr salt */, packed + PKR_SALT_BYTES /* write mac in after salt */) == -1) return -1; @@ -861,11 +859,9 @@ static int keyring_pack_identity(const keyring_identity *id, unsigned char packe * likely deducible, e.g., the location of the trailing 0x00 byte can probably be guessed with * confidence. Payload rotation will frustrate this attack. */ - uint16_t rotation; - if (urandombytes((unsigned char *)&rotation, sizeof rotation) == -1) - return WHY("urandombytes() failed to generate random rotation"); -#ifdef NO_ROTATION - rotation=0; + uint16_t rotation = 0; +#ifndef NO_ROTATION + rotation=randombytes_random(); #endif // The two bytes immediately following the MAC describe the rotation offset. packed[PKR_SALT_BYTES + PKR_MAC_BYTES] = rotation >> 8; @@ -946,16 +942,14 @@ static int keyring_pack_identity(const keyring_identity *id, unsigned char packe unsigned char *buf; size_t len; while (rotbuf_next_chunk(&rbuf, &buf, &len)) - if (urandombytes(buf, len)) - return WHY("urandombytes() failed to back-fill packed identity block"); + randombytes_buf(buf, len); } return 0; scram: /* Randomfill the entire slot to erase any secret keys that may have found their way into it, to * avoid leaking sensitive information out through a possibly re-used memory buffer. */ - if (urandombytes(packed, KEYRING_PAGE_SIZE) == -1) - WHY("urandombytes() failed to in-fill packed identity block"); + randombytes_buf(packed, KEYRING_PAGE_SIZE); return -1; } @@ -1692,7 +1686,7 @@ static int keyring_send_challenge(struct subscriber *source, struct subscriber * time_ms_t now = gettime_ms(); if (source->identity->challenge_expires < now){ source->identity->challenge_expires = now + 5000; - urandombytes(source->identity->challenge, sizeof(source->identity->challenge)); + randombytes_buf(source->identity->challenge, sizeof(source->identity->challenge)); } struct overlay_buffer *payload = ob_new(); @@ -1974,7 +1968,7 @@ unsigned char *keyring_get_nm_bytes(const sid_t *known_sidp, const sid_t *unknow if (nm_slots_used 0) { - ssize_t i = read(urandomfd, buf, (len < 1048576) ? len : 1048576); - if (i == -1) { - if (++tries > 4) { - WHY_perror("read(/dev/urandom)"); - if (errno==EBADF) urandomfd=-1; - return -1; - } - } else { - tries = 0; - buf += i; - len -= i; - } - } - return 0; -} - time_ms_t gettime_ms() { struct timeval nowtv; diff --git a/os.h b/os.h index f2fe09e2..bf79d239 100644 --- a/os.h +++ b/os.h @@ -132,9 +132,6 @@ int _emkdirsn(struct __sourceloc, const char *path, size_t len, mode_t mode, MKD #define emkdirs_info(path, mode) emkdirs_log((path), (mode), log_info_mkdir) #define emkdirsn_info(path, len, mode) emkdirsn_log((path), (len), (mode), log_info_mkdir) -void srandomdev(); -int urandombytes(unsigned char *buf, size_t len); - /* Read the symbolic link into the supplied buffer and add a terminating nul. * Logs an ERROR and returns -1 if the buffer is too short to hold the link * content and the terminating nul. If readlink(2) returns an error, then logs diff --git a/overlay_mdp.c b/overlay_mdp.c index b81f0c00..0531102a 100644 --- a/overlay_mdp.c +++ b/overlay_mdp.c @@ -695,8 +695,7 @@ static int generate_nonce(uint8_t *nonce, size_t bytes) { if (bytes<1||bytes>128) return -1; if (!nonce_initialised) { - if (urandombytes(nonce_buffer,128)) - return -1; + randombytes_buf(nonce_buffer,128); nonce_initialised=1; } @@ -1341,7 +1340,23 @@ static void mdp_interface_packet(struct socket_address *client, struct mdp_heade } } -static void mdp_process_packet(struct socket_address *client, struct mdp_header *header, +static mdp_port_t get_next_port(){ + again: + + if (next_port_binding > 32*1024) + next_port_binding=256; + else + next_port_binding++; + + unsigned i; + for(i=0;ilocal.port==0 && header->flags & MDP_FLAG_BIND){ - again: - - if (next_port_binding > 32*1024) - next_port_binding=256; - else - next_port_binding++; - - unsigned i; - for(i=0;ilocal.port=next_port_binding; + header->local.port=get_next_port(); } internal_header.source_port = header->local.port; diff --git a/randombytes.c b/randombytes.c deleted file mode 100644 index 3f2925a5..00000000 --- a/randombytes.c +++ /dev/null @@ -1,56 +0,0 @@ -/* -Serval Distributed Numbering Architecture (DNA) -Copyright (C) 2010 Paul Gardner-Stephen - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ - -#include "os.h" -#include -#include -#include -#include -#include - -/* it's really stupid that there isn't a syscall for this */ - -static int fd = -1; - -void randombytes(unsigned char *x,unsigned long long xlen) -{ - int i; - - if (fd == -1) { - for (;;) { - fd = open("/dev/urandom",O_RDONLY); - if (fd != -1) break; - sleep_ms(1000); - } - } - - while (xlen > 0) { - if (xlen < 1048576) i = xlen; else i = 1048576; - - i = read(fd,x,i); - if (i < 1) { - sleep_ms(1000); - continue; - } - - x += i; - xlen -= i; - } -} - diff --git a/serval.h b/serval.h index cb980297..1f0332b1 100644 --- a/serval.h +++ b/serval.h @@ -226,7 +226,7 @@ struct vomp_call_state; void set_codec_flag(int codec, unsigned char *flags); -struct vomp_call_state *vomp_find_call_by_session(unsigned int session_token); +struct vomp_call_state *vomp_find_call_by_session(uint16_t session_token); int vomp_parse_dtmf_digit(char c); int vomp_dial(struct subscriber *local, struct subscriber *remote, const char *local_did, const char *remote_did); int vomp_pickup(struct vomp_call_state *call); diff --git a/sourcefiles.mk b/sourcefiles.mk index 4b2c2de6..74f0f465 100644 --- a/sourcefiles.mk +++ b/sourcefiles.mk @@ -17,11 +17,9 @@ SERVAL_CLIENT_SOURCES = \ net.c \ os.c \ performance_timing.c \ - randombytes.c \ rotbuf.c \ sighandlers.c \ socket.c \ - srandomdev.c \ strbuf.c \ strbuf_helpers.c \ str.c \ diff --git a/srandomdev.c b/srandomdev.c deleted file mode 100644 index a91847b2..00000000 --- a/srandomdev.c +++ /dev/null @@ -1,116 +0,0 @@ -/* -Serval Distributed Numbering Architecture (DNA) -Copyright (C) 2010 Paul Gardner-Stephen - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ - -/* - Portions Copyright (C) 2013 Petter Reinholdtsen - Some rights reserved - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. -*/ - -/*- - * Copyright (c) 2006 Verdens Gang AS - * Copyright (c) 2006-2008 Linpro AS - * All rights reserved. - * - * Author: Dag-Erling SmÃ\u017ergrav - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $Id$ - */ - -#ifndef HAVE_SRANDOMDEV - -#ifndef WIN32 -#include -#include -#endif - -#include "os.h" -#include "log.h" -#include -#include -#include - -void -srandomdev(void) -{ - unsigned int seed; - int seeded = 0; -#ifndef WIN32 - FILE *fd; - fd = fopen("/dev/urandom", "r"); - if (NULL != fd) { - if (fread(&seed, sizeof seed, 1, fd) != 1) - WARNF("fread(\"/dev/urandom\") failed -- falling back to gettimeofday()"); - else - seeded = 1; - fclose(fd); - } -#endif - if (!seeded) { - struct timeval tv; - gettimeofday(&tv, NULL); - seed = (getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec; - } - srandom(seed); -} -#endif diff --git a/test_cli.c b/test_cli.c index 4e52b69c..5fffdc77 100644 --- a/test_cli.c +++ b/test_cli.c @@ -66,8 +66,8 @@ static int app_crypt_test(const struct cli_parsed *parsed, struct cli_context *c unsigned char plain_block[65536]; - urandombytes(nonce,sizeof(nonce)); - urandombytes(k,sizeof(k)); + randombytes_buf(nonce,sizeof(nonce)); + randombytes_buf(k,sizeof(k)); int len,i; diff --git a/uuid.c b/uuid.c index 88c4696c..aea5c36d 100644 --- a/uuid.c +++ b/uuid.c @@ -21,6 +21,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "uuid.h" #include "os.h" #include "str.h" +#include "sodium.h" #include #ifdef HAVE_ARPA_INET_H @@ -57,8 +58,7 @@ void uuid_set_version(serval_uuid_t *uuid, enum uuid_version version) int uuid_generate_random(serval_uuid_t *uuid) { - if (urandombytes(uuid->u.binary, sizeof uuid->u.binary) == -1) - return -1; + randombytes_buf(uuid->u.binary, sizeof uuid->u.binary); // The following discards 6 random bits. uuid->u.record.clock_seq_hi_and_reserved &= 0x3f; uuid->u.record.clock_seq_hi_and_reserved |= 0x80; diff --git a/vomp.c b/vomp.c index b7ec1a1c..094d6d0e 100644 --- a/vomp.c +++ b/vomp.c @@ -134,7 +134,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define VOMP_REJECT_BUSY 3 #define VOMP_REJECT_TIMEOUT 4 -#define VOMP_SESSION_MASK 0xffff #define VOMP_MAX_CALLS 16 #define VOMP_VERSION 0x02 @@ -142,8 +141,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. struct vomp_call_half { struct subscriber *subscriber; char did[64]; - unsigned char state; - unsigned int session; + uint8_t state; + uint16_t session; unsigned int sequence; }; @@ -331,7 +330,7 @@ int is_codec_set(int codec, unsigned char *flags){ return flags[codec >> 3] & (1<<(codec & 7)); } -struct vomp_call_state *vomp_find_call_by_session(unsigned int session_token) +struct vomp_call_state *vomp_find_call_by_session(uint16_t session_token) { unsigned i; for(i=0;i>4; - unsigned char sender_state=state&0xf; + uint16_t sender_session=ob_get_ui16(payload); + uint16_t recvr_session=ob_get_ui16(payload); + uint8_t state = ob_get(payload); + uint8_t recvr_state=state>>4; + uint8_t sender_state=state&0xf; /* wants to create a call session. Main aim here: replay protection. An adversary should not be able to