# Make mcrypt_create_iv() an alias and move the code into /ext/standard

- Thread: https://externals.io/message/72364
- Original archive: https://news-web.php.net/php.internals/72364
- Started by Thomas Hruska on 2014-02-07T07:05:37+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #72364 — Thomas Hruska — 2014-02-07T07:05:37+00:00

URL: https://externals.io/message/72364#72364

mcrypt_create_iv() is the cleanest access to a universal system-level 
primitive that supports both /dev/urandom and 
php_win32_get_random_bytes() under the hood.  Unfortunately, it resides 
in /ext/mcrypt and the mcrypt extension isn't always enabled/available 
(nor should it be).  At any rate, it is quite odd that the function 
php_win32_get_random_bytes() is compiled into the core of PHP by default 
under Windows but there's currently no way to get at it except through 
an extension.  There's good code in mcrypt_create_iv() that is 
significantly useful outside of the rest of that extension and it isn't 
actually dependent upon libmcrypt for proper operation.

Moving the guts of this non-dependent function into the core would allow 
mcrypt_create_iv() to just become an alias.  The new userland function 
could be located in /ext/standard/rand.c and have an uncreative name 
like rand_bytes().

-- 
Thomas Hruska
CubicleSoft President

I've got great, time saving software that you will find useful.

http://cubiclesoft.com/

---

## #72366 — Tjerk Meesters — 2014-02-07T07:10:29+00:00

URL: https://externals.io/message/72364#72366 · In reply to: #72364

Hi Thomas,

On Fri, Feb 7, 2014 at 3:05 PM, Thomas Hruska <thruska@cubiclesoft.com>wrote:

> mcrypt_create_iv() is the cleanest access to a universal system-level
> primitive that supports both /dev/urandom and php_win32_get_random_bytes()
> under the hood.  Unfortunately, it resides in /ext/mcrypt and the mcrypt
> extension isn't always enabled/available (nor should it be).  At any rate,
> it is quite odd that the function php_win32_get_random_bytes() is compiled
> into the core of PHP by default under Windows but there's currently no way
> to get at it except through an extension.  There's good code in
> mcrypt_create_iv() that is significantly useful outside of the rest of that
> extension and it isn't actually dependent upon libmcrypt for proper
> operation.
>

We already have something similar to that in password.c:

http://lxr.php.net/xref/PHP_5_6/ext/standard/password.c#111

It shouldn't be hard to turn this into an API function so that other code
written against php can use it for their own purposes.


> Moving the guts of this non-dependent function into the core would allow
> mcrypt_create_iv() to just become an alias.  The new userland function
> could be located in /ext/standard/rand.c and have an uncreative name like
> rand_bytes().
>
> --
> Thomas Hruska
> CubicleSoft President
>
> I've got great, time saving software that you will find useful.
>
> http://cubiclesoft.com/
>
> --
> 
> 
>
>


-- 
--
Tjerk

---

## #72367 — Pierre Joye — 2014-02-07T07:11:01+00:00

URL: https://externals.io/message/72364#72367 · In reply to: #72364

> mcrypt_create_iv() is the cleanest access to a universal system-level
> primitive that supports both /dev/urandom and php_win32_get_random_bytes()
> under the hood.  Unfortunately, it resides in /ext/mcrypt and the mcrypt
> extension isn't always enabled/available (nor should it be).  At any rate,
> it is quite odd that the function php_win32_get_random_bytes() is compiled
> into the core of PHP by default under Windows but there's currently no way
> to get at it except through an extension.  There's good code in
> mcrypt_create_iv() that is significantly useful outside of the rest of that
> extension and it isn't actually dependent upon libmcrypt for proper
> operation.
>
> Moving the guts of this non-dependent function into the core would allow
> mcrypt_create_iv() to just become an alias.  The new userland function could
> be located in /ext/standard/rand.c and have an uncreative name like
> rand_bytes().

We do not force to enable extension with external dependencies, even
less in ext/standard.

However, if any function should be enabled by default, if the deps are
available obviously, it should be the one from openssl, it is by far
more available and better.

Also we need something to differentiate crypto safe vs strong entropy
sources to avoid the usual issues with crypto safe used in cases with
no requirements for crypto safe entropy.

Cheers,
-- 
Pierre

@pierrejoye | http://www.libgd.org

---

## #72368 — Pierre Joye — 2014-02-07T07:15:23+00:00

URL: https://externals.io/message/72364#72368 · In reply to: #72366

> Hi Thomas,
>
> On Fri, Feb 7, 2014 at 3:05 PM, Thomas Hruska <thruska@cubiclesoft.com>wrote:
>
> > […]
>
> We already have something similar to that in password.c:
>
> http://lxr.php.net/xref/PHP_5_6/ext/standard/password.c#111
>
> It shouldn't be hard to turn this into an API function so that other code
> written against php can use it for their own purposes.

While reading this code portion, I wonder if anyone has tested it on
system where there is no urandom available but other names. I think it
should use the entropy source ini setting instead of hard coded
urandom. Thoughts?

Cheers,
-- 
Pierre

@pierrejoye | http://www.libgd.org

---

## #72370 — Yasuo Ohgaki — 2014-02-07T10:30:56+00:00

URL: https://externals.io/message/72364#72370 · In reply to: #72364

Hi Thomas,

On Fri, Feb 7, 2014 at 4:05 PM, Thomas Hruska <thruska@cubiclesoft.com>wrote:

> Moving the guts of this non-dependent function into the core would allow
> mcrypt_create_iv() to just become an alias.  The new userland function
> could be located in /ext/standard/rand.c and have an uncreative name like
> rand_bytes().


There is new great PECL package.
http://pecl.php.net/package/crypto
I would like to see it as default for crypt related feature.

Anyway, we need default rundom_bytes() function in ext/standard. This is
mandatory for secure apps, but we have no default function. This should be
resolved.

Anyway, mcrypt_create_iv() is not optimum, yet. Here is possible
improvement.

https://github.com/yohgaki/php-src/compare/PHP-5.6-mcrypt_create_iv

It still requires random source and it does not support windows well,
though.
I'm about to adding new function which solves all of them like session
module.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #72375 — Tjerk Meesters — 2014-02-07T11:31:08+00:00

URL: https://externals.io/message/72364#72375 · In reply to: #72370

> Hi Thomas,
>
> On Fri, Feb 7, 2014 at 4:05 PM, Thomas Hruska <thruska@cubiclesoft.com
> > […]
>
> > […]
>
>
> There is new great PECL package.
> http://pecl.php.net/package/crypto
> I would like to see it as default for crypt related feature.
>
> Anyway, we need default rundom_bytes() function in ext/standard. This is
> mandatory for secure apps, but we have no default function. This should be
> resolved.
>
> Anyway, mcrypt_create_iv() is not optimum, yet. Here is possible
> improvement.
>
> https://github.com/yohgaki/php-src/compare/PHP-5.6-mcrypt_create_iv
>
> It still requires random source and it does not support windows well,
> though.
> I'm about to adding new function which solves all of them like session
> module.
>

Speaking of which, it would be nice if session id generation depended on
this shiny new API as well ... basically we solve two problems:

1. An easy way to get a bunch of random bytes
2. Centralize random number generation internally, i.e. session ids and
password salts are generated with it.

I'm not sure whether this should extend to providing an easy way to
generate crypto safe random numbers, but that would obviously be nice if
feasible.


> Regards,
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net
>



-- 
--
Tjerk

---

## #72376 — Pierre Joye — 2014-02-07T11:32:37+00:00

URL: https://externals.io/message/72364#72376 · In reply to: #72375

On Fri, Feb 7, 2014 at 12:31 PM, Tjerk Meesters
<tjerk.meesters@gmail.com> wrote:

> Speaking of which, it would be nice if session id generation depended on
> this shiny new API as well ... basically we solve two problems:
>
> 1. An easy way to get a bunch of random bytes
> 2. Centralize random number generation internally, i.e. session ids and
> password salts are generated with it.
>
> I'm not sure whether this should extend to providing an easy way to
> generate crypto safe random numbers, but that would obviously be nice if
> feasible.

See my other post today about unifying entropy source. We may discuss
that there.


-- 
Pierre

@pierrejoye | http://www.libgd.org

---

## #72388 — Yasuo Ohgaki — 2014-02-07T21:28:13+00:00

URL: https://externals.io/message/72364#72388 · In reply to: #72364

Hi Thomas,

On Fri, Feb 7, 2014 at 4:05 PM, Thomas Hruska <thruska@cubiclesoft.com>wrote:

> mcrypt_create_iv() is the cleanest access to a universal system-level
> primitive that supports both /dev/urandom and php_win32_get_random_bytes()
> under the hood.  Unfortunately, it resides in /ext/mcrypt and the mcrypt
> extension isn't always enabled/available (nor should it be).  At any rate,
> it is quite odd that the function php_win32_get_random_bytes() is compiled
> into the core of PHP by default under Windows but there's currently no way
> to get at it except through an extension.  There's good code in
> mcrypt_create_iv() that is significantly useful outside of the rest of that
> extension and it isn't actually dependent upon libmcrypt for proper
> operation.
>
> Moving the guts of this non-dependent function into the core would allow
> mcrypt_create_iv() to just become an alias.  The new userland function
> could be located in /ext/standard/rand.c and have an uncreative name like
> rand_bytes().
>

rand_bytes() (or `random_bytes()`) is common names for the task. We may adopt
it.
Anyway, I've wrote portable one for the task.

https://github.com/yohgaki/php-src/compare/PHP-5.6-rfc-random_bytes

Function name could be rand_bytes() or anything. Name is important, but
implementation is
more important here. It's still work in progress, as it does not use
OpenSSL.

I would like to use OpenSSL's RAND_bytes() for this. This mean when OpenSSL
is available,
PHP is linked with OpenSSL. With OpenSSL's RAND_bytes() and
RAND_peduso_bytes(), we
may be able to use better RNG/PRNG for the platform.

Any comments?

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net
