Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:72027 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 48085 invoked from network); 3 Feb 2014 01:27:02 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Feb 2014 01:27:02 -0000 Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.54 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.215.54 mail-la0-f54.google.com Received: from [209.85.215.54] ([209.85.215.54:39422] helo=mail-la0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0E/77-30967-460FEE25 for ; Sun, 02 Feb 2014 20:27:01 -0500 Received: by mail-la0-f54.google.com with SMTP id y1so5064383lam.27 for ; Sun, 02 Feb 2014 17:26:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=OkvUhi15DdtedrQt48NKpIsczvPQ/nOcMGkjXAw0idM=; b=GjsNjWvRolT8kqw1RVOEAwfDZbh7Vb5WpkQYUDy6XMSlUIQLMN8Z1OEg39CoeA0ykq HRaeT1l87TpUdDDvZSDYzbd1VfmmDlRoKUCwqMhX1DBDCffE57gwd8E6Axv38w0dWjfo 2uyB54Kjm72e4H6+EH+xwe3vLwy5wdzTSr/sXonSh9/M15acRkX/BHkejNweeSXEzVvz /lP3KC9j4VgzesVAVRZ7y1Afssj/4qCX+WJbeF3L6oD6l2rJb6Mop9sFmTOdvgid9pGX gpbDUar7vv+BwrCTCA8jvWXTnha+uRZjUjXAJwGZHXoZVBr6mWn+NzUxwnVHKkHbZRum naSQ== X-Received: by 10.153.8.225 with SMTP id dn1mr11586297lad.17.1391390818098; Sun, 02 Feb 2014 17:26:58 -0800 (PST) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.112.199.37 with HTTP; Sun, 2 Feb 2014 17:26:17 -0800 (PST) In-Reply-To: References: <52EE1EDC.2010309@sugarcrm.com> Date: Mon, 3 Feb 2014 10:26:17 +0900 X-Google-Sender-Auth: jLe4ukhloYxyJ3XoxSQQ6suBrME Message-ID: To: Stas Malyshev Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a1136dfc674e4f704f1766bd5 Subject: Re: [PHP-DEV] Extending uniqid() or not? From: yohgaki@ohgaki.net (Yasuo Ohgaki) --001a1136dfc674e4f704f1766bd5 Content-Type: text/plain; charset=UTF-8 Hi all, Possible patch for mcrypt_create_iv(). I put it in github. https://github.com/yohgaki/php-src/compare/PHP-5.6-mcrypt_create_iv - Add /dev/arandom support - Raise warning for dangerous usage -- Yasuo Ohgaki yohgaki@ohgaki.net On Mon, Feb 3, 2014 at 7:46 AM, Yasuo Ohgaki wrote: > Hi all, > > On Mon, Feb 3, 2014 at 7:08 AM, Yasuo Ohgaki wrote: > >> What's wrong with mcrypt_create_iv() which exists specifically for the >>> purpose of generating secure random string? >>> >> >> User may use it. IV should be random bytes and it can be used as >> secure source for hash. I does almost the same thing that I would >> like to do. Issues are >> >> - it does not auto detect RNG and use /dev/random by default >> - it does not support /dev/arandom >> - it uses php_rand() to create random bytes if source option is not >> RANDOM or URANDOM >> - it is not an available function by default... >> >> 1st issue is not a issue actually. I think this is good that it uses >> /dev/random by default >> even if it may block script. As a crypt module, it should use most secure >> source by default. We may improve mcrypt_create_iv() a little by raising >> E_NOTICE >> error when user set source other than RANDOM or URANDOM, and add ARANDOM >> as a source. >> >> Even though mcrypt_create_iv() good enough for it's original purpose, >> it's not good as >> a general (fool proof) method for generating random bytes as it can block >> script execution. >> > > I'm about to write patch improve mcrypt_create_iv(), but there is problem > > IV source is defined as enum and user has to specify it as number > > typedef enum { > RANDOM = 0, > URANDOM, > RAND > } iv_source; > > To maintain compatibility, it would be > > typedef enum { > RANDOM = 0, > URANDOM, > RAND, > ARANDOM > } iv_source; > > Which seems a little odd to me. Anyway, possible patch would be something > like > > diff --git a/ext/mcrypt/mcrypt.c b/ext/mcrypt/mcrypt.c > index 89ad83f..98b1848 100644 > --- a/ext/mcrypt/mcrypt.c > +++ b/ext/mcrypt/mcrypt.c > @@ -539,7 +539,8 @@ PHP_MINFO_FUNCTION(mcrypt) /* {{{ */ > typedef enum { > RANDOM = 0, > URANDOM, > - RAND > + RAND, > + ARANDOM > } iv_source; > > /* {{{ proto resource mcrypt_module_open(string cipher, string > cipher_directory, string mode, string mode_directory) > @@ -1387,8 +1388,23 @@ PHP_FUNCTION(mcrypt_create_iv) > } > > iv = ecalloc(size + 1, 1); > - > - if (source == RANDOM || source == URANDOM) { > + > + switch(source) { > + case RAND: > + source = NULL; > + break; > + case URANDOM: > + source = "/dev/urandom"; > + break; > + case ARANDOM: > + source = "/dev/arandom"; > + break; > + case RANDOM: > + default: > + source = "/dev/random"; > + } > + > + if (source) { > #if PHP_WIN32 > /* random/urandom equivalent on Windows */ > BYTE *iv_b = (BYTE *) iv; > @@ -1402,7 +1418,7 @@ PHP_FUNCTION(mcrypt_create_iv) > int fd; > size_t read_bytes = 0; > > - fd = open(source == RANDOM ? "/dev/random" : "/dev/urandom", O_RDONLY); > + fd = open(source, O_RDONLY); > if (fd < 0) { > efree(iv); > php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot open source > device"); > @@ -1428,6 +1444,7 @@ PHP_FUNCTION(mcrypt_create_iv) > while (size) { > iv[--size] = (char) (255.0 * php_rand(TSRMLS_C) / RAND_MAX); > } > + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "RAND is not safe"); > } > RETURN_STRINGL(iv, n, 0); > } > > > -- > Yasuo Ohgaki > yohgaki@ohgaki.net > --001a1136dfc674e4f704f1766bd5--