help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Noob dumb question (extending emacs)


From: Emanuel Berg
Subject: Re: Noob dumb question (extending emacs)
Date: Sat, 23 Oct 2021 21:52:15 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

>> It is *not* okay to offer a library for password generation
>> using a weak generator to other people without explaining
>> its entropy characteristics so that they could assess
>> their risk.
>
> But what is the method stipulated then, we'll just do
> a `random-passwd' with that ...

>From pwgen on Debian 11:

/*
 * randnum.c -- generate (good) randum numbers.
 *
 * Copyright (C) 2001,2002 by Theodore Ts'o
 * 
 * This file may be distributed under the terms of the GNU Public
 * License.
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include "pwgen.h"

#ifdef HAVE_DRAND48
extern double drand48(void);
#endif

static int get_random_fd(void);

/* Borrowed/adapted from e2fsprogs's UUID generation code */
static int get_random_fd()
{
        struct timeval  tv;
        static int      fd = -2;

        if (fd == -2) {
                gettimeofday(&tv, 0);
                fd = open("/dev/urandom", O_RDONLY);
                if (fd == -1)
                        fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
        }
        return fd;
}

/*
 * Generate a random number n, where 0 <= n < max_num, using
 * /dev/urandom if possible.
 */
int pw_random_number(max_num)
        int max_num;
{
        unsigned int rand_num;
        int i, fd = get_random_fd();
        int lose_counter = 0, nbytes = sizeof(rand_num);
        char *cp = (char *) &rand_num;

        if (fd >= 0) {
                while (nbytes > 0) {
                        i = read(fd, cp, nbytes);
                        if ((i < 0) &&
                            ((errno == EINTR) || (errno == EAGAIN)))
                                continue;
                        if (i <= 0) {
                                if (lose_counter++ == 8)
                                        break;
                                continue;
                        }
                        nbytes -= i;
                        cp += i;
                        lose_counter = 0;
                }
        }
        if (nbytes == 0)
                return (rand_num % max_num);

        /* We weren't able to use /dev/random, fail hard */

        fprintf(stderr, "No entropy available!\n");
        exit(1);
}

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]