From patchwork Mon Mar 28 22:09:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 52427 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B5DAB3858036 for ; Mon, 28 Mar 2022 22:10:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B5DAB3858036 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1648505410; bh=UGJiddLNE+mylvIY1tJujR3EHrxKuDQsjQmagq0fOyY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Wkf8N34rVa9Y/2CSBKn7zlUvMqkVoBhmXYTTSRDxH592YhmOpFQUrTJ6OvRmo+8oJ C0c2fNhZwL+cdbe/R6FCuTWnmGf5QPYQWAFMicEzGbCMn/9/DXc7etr2ObkF7OQ/NA 10IXhk1gZLABVZqx3Hbkgj3aQ5s0iaxKOicfRQ9Q= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-io1-xd2d.google.com (mail-io1-xd2d.google.com [IPv6:2607:f8b0:4864:20::d2d]) by sourceware.org (Postfix) with ESMTPS id F2F5F3858C50 for ; Mon, 28 Mar 2022 22:09:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F2F5F3858C50 Received: by mail-io1-xd2d.google.com with SMTP id p21so4911530ioj.4 for ; Mon, 28 Mar 2022 15:09:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=UGJiddLNE+mylvIY1tJujR3EHrxKuDQsjQmagq0fOyY=; b=kBmqX3s/Tqd8ATzLKDryLwM+eF8T8rAzLHpqbfp0a4e88fI+GyPqpFSdZArYG7IwtI T2aFX7k+AdlAt7cUVRlM11U2ArXZjpkAcddG724pdiz9vHtRDUnb8IZ7LJqKGpzC1eEn N8eJN/CWnkeAHOwL1Artr4bWTik4vYFbmUGibNsujvOkQI4gTRrSkRvpRhq9XODXWRzu LDtzA4cWpLvm0fQrgFuEhBciskkIjZyd9zRjwm9JlmqhNq0x1cQ0Vnv1vujXA+mb1NHp ySODtYaK2zxSBNbLu5GUHDtHEBJ2w6umI3cY3iySRMfnODlCFFitVo3V/hnUG/VRiXuX 8QaQ== X-Gm-Message-State: AOAM530oEtPSUYLej7fudtJEH8GjHL5WbJ1NpHzVVRHHKsfUqK7gh77f BwVF/Fonqvjxc7GIWjRgmwIrMxdUGtU= X-Google-Smtp-Source: ABdhPJzZKpAoWselnLtJt1GsLWlqhtvC1Ir4UGxbp7uj2+HLOd26M0VV3UDKQgI/LiXj26YSSs+wGA== X-Received: by 2002:a5d:9d84:0:b0:649:d813:4d22 with SMTP id ay4-20020a5d9d84000000b00649d8134d22mr7047009iob.133.1648505386001; Mon, 28 Mar 2022 15:09:46 -0700 (PDT) Received: from localhost.localdomain (node-17-161.flex.volo.net. [76.191.17.161]) by smtp.googlemail.com with ESMTPSA id y74-20020a6bc84d000000b00645dfdd8a4csm8622442iof.38.2022.03.28.15.09.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Mar 2022 15:09:45 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v1 1/2] random-bits: Factor out entropy generating function Date: Mon, 28 Mar 2022 17:09:35 -0500 Message-Id: <20220328220936.2724834-1-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" On some architectures `clock_gettime` is undesirable as it may use a syscall or there may be a faster alternative. Future architecture specific functions can be added in sysdeps//random-bits-entropy.h to provide a version of 'random_bits_entropy' that doesn't use 'clock_gettime'. --- include/random-bits.h | 16 ++++++-------- sysdeps/generic/random-bits-entropy.h | 31 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 sysdeps/generic/random-bits-entropy.h diff --git a/include/random-bits.h b/include/random-bits.h index 17665b479a..016b87576c 100644 --- a/include/random-bits.h +++ b/include/random-bits.h @@ -19,21 +19,17 @@ #ifndef _RANDOM_BITS_H # define _RANDOM_BITS_H -#include -#include +# include +# include -/* Provides fast pseudo-random bits through clock_gettime. It has unspecified - starting time, nano-second accuracy, its randomness is significantly better - than gettimeofday, and for mostly architectures it is implemented through - vDSO instead of a syscall. Since the source is a system clock, the upper - bits will have less entropy. */ +/* Provides fast pseudo-random bits through architecture specific + random_bits_entropy. Expectation is source is some timing function so + the upper bits have less entropy. */ static inline uint32_t random_bits (void) { - struct __timespec64 tv; - __clock_gettime64 (CLOCK_MONOTONIC, &tv); + uint32_t ret = random_bits_entropy (); /* Shuffle the lower bits to minimize the clock bias. */ - uint32_t ret = tv.tv_nsec ^ tv.tv_sec; ret ^= (ret << 24) | (ret >> 8); return ret; } diff --git a/sysdeps/generic/random-bits-entropy.h b/sysdeps/generic/random-bits-entropy.h new file mode 100644 index 0000000000..53290c7f7a --- /dev/null +++ b/sysdeps/generic/random-bits-entropy.h @@ -0,0 +1,31 @@ +/* Fast function for generating entropy of random_bits. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +/* Generically use clock_gettime. It has unspecified starting time, nano-second + accuracy, its randomness is significantly better than gettimeofday, and for + mostly architectures it is implemented through vDSO instead of a syscall. */ +static inline uint32_t +random_bits_entropy (void) +{ + struct __timespec64 tv; + __clock_gettime64 (CLOCK_MONOTONIC, &tv); + return tv.tv_nsec ^ tv.tv_sec; +} From patchwork Mon Mar 28 22:09:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 52428 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 990E63857C46 for ; Mon, 28 Mar 2022 22:10:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 990E63857C46 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1648505452; bh=60pBhT+eLoo7p2bbdSSiagjcWAh/G3/4QIIpAYLHLVQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=rzQNbDT5CBiOhGW9NJgeh2Ul3T1vbz98FfT66OIDvQSEhUJPxh7WR7BJB6KfnT3qZ ybk+bbA9w7KyP7fKVXXoHvcN86HfXzAWdOWYU5cJGtvLPKh6UCrqDjw3qxiTt2L03h PsI+MQg98uZI2YHAD8QyMK6pIrN+oBndy/XLTy0A= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) by sourceware.org (Postfix) with ESMTPS id 53F183858C51 for ; Mon, 28 Mar 2022 22:09:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 53F183858C51 Received: by mail-io1-xd2f.google.com with SMTP id z7so18886574iom.1 for ; Mon, 28 Mar 2022 15:09:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=60pBhT+eLoo7p2bbdSSiagjcWAh/G3/4QIIpAYLHLVQ=; b=UGnVcLvHgXv/ynZr8NjyNMHC8QP//QDOkKSUAdlRJBURQ6GlTteDxcVHXGMLqsrWCS 8HKMB9EfvhS791G1OA6tDCVyCrG+mPARpKwzZ4dGK1kGVa15A2vM3Z7k+dOl34K9JSDc aO3RVq3iABgu4QTpI2fdBhE0lZPhHHjMA9KzzlNjAapwDq+vf912zUMn5/JPWOIrivCc WbwkLhFVL+iS8XgN/l0CFyEvF4smko46f1KyaxML+han67OYC2BRIXRaciYXGv9UcIfr /GbrOWjtsYl40l8ebAz4lhROdjnmutr/xqCVP9KgjVVRNYP5VUj/zyBW1FJ6UIKDsFoW sN9g== X-Gm-Message-State: AOAM531sXub1iau2N07FBU3xq1wo3RF5JOQ5AvhaXar3j+Ysg/n2wMGM LjJ/kOyvyRlHo8D5oczV+BglozszzKQ= X-Google-Smtp-Source: ABdhPJwbwj1M2CHPg9HmRpnL6TSN56avyks/KWONekDPFhhUnSqCGzHsPxEcGrj0F/z7pyn78mcv2Q== X-Received: by 2002:a05:6602:150c:b0:64c:6878:25ed with SMTP id g12-20020a056602150c00b0064c687825edmr6084807iow.27.1648505387407; Mon, 28 Mar 2022 15:09:47 -0700 (PDT) Received: from localhost.localdomain (node-17-161.flex.volo.net. [76.191.17.161]) by smtp.googlemail.com with ESMTPSA id y74-20020a6bc84d000000b00645dfdd8a4csm8622442iof.38.2022.03.28.15.09.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Mar 2022 15:09:46 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v1 2/2] x86: Use rdtsc for generating entropy for random_bits Date: Mon, 28 Mar 2022 17:09:36 -0500 Message-Id: <20220328220936.2724834-2-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220328220936.2724834-1-goldstein.w.n@gmail.com> References: <20220328220936.2724834-1-goldstein.w.n@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" `rdtsc` generates a comparable amount of entropy and is faster than `clock_gettime`. Entropy: random_bits w/ rdtsc: Entropy = 7.805972 bits per byte. random_bits w/ clock_gettime: Entropy = 7.806116 bits per byte. Measured entropy of `random_bits` using `rdtsc` vs `clock_gettime` using ent: https://www.fourmilab.ch/random/ Performance: random_bits w/: rdtsc, clock_gettime throughput: 16.35, 38.36 latency: 23.68, 54.15 Time as cycles (measured with `rdtsc`) / Trials. benchmarks done on Tigerlake. --- sysdeps/x86_64/random-bits-entropy.h | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 sysdeps/x86_64/random-bits-entropy.h diff --git a/sysdeps/x86_64/random-bits-entropy.h b/sysdeps/x86_64/random-bits-entropy.h new file mode 100644 index 0000000000..1e96659aa3 --- /dev/null +++ b/sysdeps/x86_64/random-bits-entropy.h @@ -0,0 +1,36 @@ +/* Function for generating entropy quick on x86 with rdtsc. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +/* rdtsc is faster than generic (clock_gettime) and has roughly the same + entropy. Result of filling a file using random_bits with rdtsc vs + clock_gettime as an entropy function: + + $> ent gettime-random.txt + Entropy = 7.806116 bits per byte. + + $> ent rdtsc-random.txt + Entropy = 7.805972 bits per byte. + */ + +static inline uint32_t +random_bits_entropy (void) +{ + return __builtin_ia32_rdtsc (); +}