From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Omar Polo Newsgroups: gmane.emacs.help,gmane.emacs.devel Subject: Re: urandom number in Emacs Date: Sun, 07 Nov 2021 09:53:42 +0100 Message-ID: <87czncs5f1.fsf@omarpolo.com> References: <87lf21nsxe.fsf@zoho.eu> <87bl2woma5.fsf@zoho.eu> <87sfw8s6zt.fsf@omarpolo.com> <87czncmju0.fsf@zoho.eu> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="35816"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.6.9; emacs 29.0.50 Cc: help-gnu-emacs@gnu.org, emacs-devel@gnu.org To: Emanuel Berg Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sun Nov 07 10:06:26 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mje8A-00094p-6u for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 07 Nov 2021 10:06:26 +0100 Original-Received: from localhost ([::1]:40720 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mje88-0003co-IQ for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 07 Nov 2021 04:06:24 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33532) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mje6G-0003bM-Hs; Sun, 07 Nov 2021 04:04:29 -0500 Original-Received: from mail.omarpolo.com ([144.91.116.244]:56445) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mje6D-0004mM-Ls; Sun, 07 Nov 2021 04:04:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=omarpolo.com; s=20200327; t=1636275860; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=B289ILcmbN4kaC8Nkenbfbm9tBg2Lbteg2tTvvdxi+c=; b=PTqpZeJuwMv/JbeZeNNM9PeoqV8x9ihJ9Rht2XWO+eoToZFjLcVJD5NZ44O1dnGM+A7QnA NmzTtwWJ+xOC7UkWAKyHxfhBQYDFiV9wJBflQe0F0geDazVPr7ifJiZ/UmU4I8mn+4HAK8 VW/Xcm4X7FSmkAF6iDuHGYoMPCWIWe0= Original-Received: from localhost (host-79-50-237-209.retail.telecomitalia.it [79.50.237.209]) by mail.omarpolo.com (OpenSMTPD) with ESMTPSA id cb1fd482 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sun, 7 Nov 2021 10:04:19 +0100 (CET) Original-Received: from venera (localhost [127.0.0.1]) by localhost (OpenSMTPD) with ESMTP id 2aa7b902; Sun, 7 Nov 2021 10:04:18 +0100 (CET) In-reply-to: <87czncmju0.fsf@zoho.eu> Received-SPF: pass client-ip=144.91.116.244; envelope-from=op@omarpolo.com; helo=mail.omarpolo.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:134439 gmane.emacs.devel:278926 Archived-At: Emanuel Berg via "Emacs development discussions." writes: > Omar Polo wrote: > >> I mean, if you really need something like that and emacs >> doesn't already provide it, why don't try cooking a diff to >> add it to base emacs instead that an external module? > > `random' isn't good enough for passwords, it could since it is > a C built-in read from /dev/urandom but it doesn't, and you > can't do that from Lisp since /dev/urandom is a non-regular > file. head(1) and pwgen(1) can do it from C and so can Emacs, > but to have it as a Lisp primitive or a DM ... but isn't that > why you have DMs, you shouldn't have to recompile the whole > thing to get it? It is more ... modular? Recompiling Emacs from sources takes a couple of minutes, and it's a trivial function that you can write and test apart before including it to Emacs. Also, I'm pretty sure that no distribution ships *by default* Emacs modules. Furthermore, it's an annoyance for the users: if I want to use it, I have to compile it anyway, whether if it's an emacs built-in all I have to do is install/update Emacs via my package manager and I'm happy ;-) >> Also, the code is wrong: > >>> if (nbytes == 0) { >>> return (rnd_num % max_num); >>> } else { >>> fprintf(stderr, "No entropy available!\n"); >>> exit(1); >>> } >> >> I don't think you want to quit emacs unconditionally if >> there's no entropy. > > OK ... I'll just return something to denote it didn't work. > Hm ... you could rewrite the function to return the value via a pointer, e.g. int get_random_number(uint32_t *n) { /* do stuff */ if (succeeded) { *n = the random number; return 0; } return -1; } /* later */ uint32_t n; if (get_random_number(&n) == -1) { /* Ooops */ } >> Also, in some circumstances there could be better (and >> faster) ways to obtain a random number, see for >> e.g. arc4random. > > Doesn't that read from /dev/urandom as well? > What's better/faster about it? You need a DM/recompile for > that as well since it is in libc, or am I wrong? arc4random uses a per-process random stream that sometimes gets re-initialized from /dev/urandom. This means that most of the time you have access to a good RNG without having to make a system call. > Nah, if it was good enough for pwgen(1) I take it ... You should also consider why that was good for pwgen. If something is good for program A, it may not be "good enough" for program B. I don't know pwgen, but given that's in the first section of the manual is probably a program that generates a password, print it to stdout and quits. It doesn't have bottlenecks. A program like Emacs on the other hand needs to call that function possibly multiple times, so investigating a bit for a faster way to obtain good random number is worth it IMHO. >> P.S.: are you sure about checking for EAGAIN? A read for >> a file descriptor not marked as non-blocking shouldn't fail >> with EAGAIN > > I am not sure, again they had it in pwgen ... > >> but that's a minor nitpick. > > Indeed ... hehe