From patchwork Wed Sep 4 12:37:26 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Borkmann X-Patchwork-Id: 272591 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 06F292C007A for ; Wed, 4 Sep 2013 22:38:03 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934693Ab3IDMhl (ORCPT ); Wed, 4 Sep 2013 08:37:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:64867 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934680Ab3IDMhk (ORCPT ); Wed, 4 Sep 2013 08:37:40 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r84CbZ1S005914 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 4 Sep 2013 08:37:35 -0400 Received: from localhost (vpn1-6-194.ams2.redhat.com [10.36.6.194]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r84CbYLg029165; Wed, 4 Sep 2013 08:37:35 -0400 From: Daniel Borkmann To: davem@davemloft.net Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Theodore Ts'o" , Joe Perches Subject: [PATCH net-next v2 1/2] random: add prandom_u32_range and prandom_u32_max helpers Date: Wed, 4 Sep 2013 14:37:26 +0200 Message-Id: <1378298247-29364-2-git-send-email-dborkman@redhat.com> In-Reply-To: <1378298247-29364-1-git-send-email-dborkman@redhat.com> References: <1378298247-29364-1-git-send-email-dborkman@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org We have implemented the same function over and over, so introduce generic helpers that unify these implementations in order to migrate such code to use them. Make the API similarly to randomize_range() for consistency. prandom_u32_range() generates numbers in [start, end] interval and prandom_u32_max() generates numbers in [0, end] interval. Signed-off-by: Daniel Borkmann Cc: Theodore Ts'o Cc: Joe Perches Cc: linux-kernel@vger.kernel.org --- include/linux/random.h | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/include/linux/random.h b/include/linux/random.h index 3b9377d..17c91c2 100644 --- a/include/linux/random.h +++ b/include/linux/random.h @@ -8,7 +8,6 @@ #include - extern void add_device_randomness(const void *, unsigned int); extern void add_input_randomness(unsigned int type, unsigned int code, unsigned int value); @@ -32,6 +31,36 @@ void prandom_seed(u32 seed); u32 prandom_u32_state(struct rnd_state *); void prandom_bytes_state(struct rnd_state *state, void *buf, int nbytes); +/** + * prandom_u32_range - return a random number in interval [start, end] + * @start: lower interval endpoint + * @end: higher interval endpoint + * + * Returns a number that is in the given interval: + * + * [...... .....] + * start end + * + * Callers need to make sure that start <= end. Note that the result + * depends on PRNG being well distributed in [0, ~0U] space. Here we + * use maximally equidistributed combined Tausworthe generator. + */ +static inline u32 prandom_u32_range(u32 start, u32 end) +{ + return (u32)(((u64) prandom_u32() * (end + 1 - start)) >> 32) + start; +} + +/** + * prandom_u32_max - return a random number in interval [0, max] + * @max: higher interval endpoint + * + * Returns a number that is in interval [0, end]. + */ +static inline u32 prandom_u32_max(u32 end) +{ + return prandom_u32_range(0, end); +} + /* * Handle minimum values for seeds */