<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.openssl.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ppelleti</id>
	<title>OpenSSLWiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.openssl.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ppelleti"/>
	<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php/Special:Contributions/Ppelleti"/>
	<updated>2026-05-12T20:33:58Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.13</generator>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=2234</id>
		<title>Related Links</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=2234"/>
		<updated>2015-06-07T23:05:50Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Testing Resources */ add sni.velox.ch&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Please feel free to edit this page and add your own OpenSSL-based project or product. This is the one place where otherwise extraneous mention of commercial products is appropriate. Note such mention does not constitute endorsement per our [[Commercial Product Disclaimer]].&lt;br /&gt;
&lt;br /&gt;
== Open Source Cryptographic Libraries ==&lt;br /&gt;
&lt;br /&gt;
Products which are available under some form of Open Source license, and which may also be available under some form of commercial license.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Open Source Cryptographic Libraries&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Library&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://sourceforge.net/projects/amissl/ AmiSSL] || an OpenSSL port to AmigaOS&lt;br /&gt;
|-&lt;br /&gt;
| [http://botan.randombit.net/ Botan] || a C++ cryptography library which includes a TLS implementation&lt;br /&gt;
|-&lt;br /&gt;
| [https://boringssl.googlesource.com/boringssl/ BoringSSL] || a Google fork of OpenSSL ([https://www.imperialviolet.org/2014/06/20/boringssl.html Announcement ]) (Press coverage: [https://news.ycombinator.com/item?id=7922745 ycombinator], [http://arstechnica.com/security/2014/06/google-unveils-independent-fork-of-openssl-called-boringssl/ Ars Technica], [http://www.pcworld.com/article/2366440/google-develops-own-boring-version-of-openssl.html PC World], [http://www.zdnet.com/google-takes-openssl-and-turns-it-into-boringssl-7000030796/ ZDNet] )&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.bouncycastle.org/ Bouncy Castle] || cryptography API for Java and C# ([[Wikipedia: Bouncy Castle (cryptography)]])&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.cs.auckland.ac.nz/~pgut001/cryptlib/ Cryptlib] || a security toolkit that allows one to easily add encryption and authentication services to software&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.cryptopp.com/ Crypto++] || a free C++ class library of cryptographic schemes&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.gnutls.org/ GnuTLS] || an LGPL-licensed TLS library with substantial documentation&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.libressl.org/ LibreSSL] || an OpenBSD fork of OpenSSL (Press coverage: [http://www.zdnet.com/openbsd-forks-prunes-fixes-openssl-7000028613/ ZDNet], [http://arstechnica.com/information-technology/2014/04/openssl-code-beyond-repair-claims-creator-of-libressl-fork/ Ars Technica])&lt;br /&gt;
|-&lt;br /&gt;
| [http://libtom.org/ LibTomCrypt] || public domain open source crypto library written in C&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.mitls.org/wsgi miTLS] || a verified reference implementation of the TLS protocol. ([http://www.reddit.com/r/netsec/comments/1zn2d3/mitls_a_verified_reference_tls_implementation/ &amp;quot;reddit: miTLS - A verified reference TLS implementation&amp;quot;])&lt;br /&gt;
|-&lt;br /&gt;
| [http://nacl.cr.yp.to/ NaCl] || NaCl (pronounced &amp;quot;salt&amp;quot;) is a easy-to-use high-speed software library for network communication, encryption, decryption, and signatures&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.mozilla.org/projects/security/pki/nss/ NSS] || a set of libraries designed to support cross-platform development of security-enabled client and server applications&lt;br /&gt;
|-&lt;br /&gt;
| [http://polarssl.org/ PolarSSL] || an SSL library that handles the complexities of the Secure Sockets Layer (SSL) protocol for an application ([[Wikipedia: PolarSSL]])&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.ohloh.net/projects/xyssl XySSL] || a C library providing a very small footprint crypto library and SSL implementation.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Open Source Products Using OpenSSL ==&lt;br /&gt;
&lt;br /&gt;
Products which are available under some form of Open Source License, and which may also be available under some form of commercial license.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Open Source Products Using OpenSSL&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Product&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://libevent.org/ libevent] || an event driven library which can [http://www.wangafu.net/~nickm/libevent-book/Ref6a_advanced_bufferevents.html#_bufferevents_and_ssl optionally use OpenSSL]&lt;br /&gt;
|-&lt;br /&gt;
| [http://en.wikipedia.org/wiki/Mod_ssl mod_ssl] || SSL/TLS module for the [http://en.wikipedia.org/wiki/Apache_HTTP_Server Apache HTTP Server]&lt;br /&gt;
|-&lt;br /&gt;
|[https://www.stunnel.org/index.html Stunnel] || an SSL encryption wrapper between remote client and local (inetd-startable) or remote server&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Languages libraries/Wrappers relying on openssl ===&lt;br /&gt;
&lt;br /&gt;
* '''libcurl''' http://curl.haxx.se/ which can use openssl&lt;br /&gt;
* '''PHP''' uses libcurl http://php.net/manual/en/intro.curl.php &lt;br /&gt;
* '''Python''' pyOpenSSL https://github.com/pyca/pyopenssl cryptography https://github.com/pyca/cryptography&lt;br /&gt;
* '''Perl''' Perl-Openssl http://sourceforge.net/projects/perl-openssl/&lt;br /&gt;
&lt;br /&gt;
== Closed Source Cryptographic Libraries ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Closed Source Cryptographic Libraries&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Library&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.example.com No Entry]  || This is a placeholder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Closed Source Products Using OpenSSL ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Closed Source Products Using OpenSSL&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Product&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| Stonesoft Firewall Appliances [https://www.stonesoft.com/en/customer_care/documentation/appliances/] &lt;br /&gt;
| Stonesoft (now McAfee) Appliances uses openssl as SSL/TLS communication library, license advertised [http://www.stonesoft.com/en/customer_care/support/third_party_licenses.html]&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.example.com No Entry]  || This is a placeholder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Even if this section is empty now, it exist a numerous number of closed source products using OpenSSL.&lt;br /&gt;
&lt;br /&gt;
== Books and Documentation == &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Books and Documentation&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Title&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [https://en.wikipedia.org/wiki/Comparison_of_TLS_implementations Comparison of TLS implementations]  || Wikipedia article comparing various TLS libraries&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.keylength.com/ keylength.com]  || site which summarizes various key length recommendations&lt;br /&gt;
|-&lt;br /&gt;
| [https://www.feistyduck.com/books/openssl-cookbook/ OpenSSL Cookbook]  || A free ebook that covers configuration and command-line usage (first&lt;br /&gt;
published in 2013; continuously updated)&lt;br /&gt;
|-&lt;br /&gt;
| [http://shop.oreilly.com/product/9780596002701.do Network Security with OpenSSL]  || O'Reilly book from 2002&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Testing Resources == &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Testing Resources&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Title&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [https://www.tls-o-matic.com/ TLS-O-MATIC]  || A set of tests for TLS clients&lt;br /&gt;
|-&lt;br /&gt;
| [https://www.ssllabs.com/ssltest/ SSL Labs server test]  || Probes any HTTPS server on the Internet and assigns it a letter grade&lt;br /&gt;
|-&lt;br /&gt;
| [https://www.ssllabs.com/ssltest/viewMyClient.html SSL Labs client test]  || Tests the TLS capabilities of your browser&lt;br /&gt;
|-&lt;br /&gt;
| [https://github.com/iSECPartners/tlspretense TLSPretense]  || A test framework for testing SSL/TLS client certificate validation&lt;br /&gt;
|-&lt;br /&gt;
| [https://sni.velox.ch/ sni.velox.ch] || Test for Server Name Indication&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=2233</id>
		<title>Related Links</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=2233"/>
		<updated>2015-06-07T02:43:18Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: add testing resources&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Please feel free to edit this page and add your own OpenSSL-based project or product. This is the one place where otherwise extraneous mention of commercial products is appropriate. Note such mention does not constitute endorsement per our [[Commercial Product Disclaimer]].&lt;br /&gt;
&lt;br /&gt;
== Open Source Cryptographic Libraries ==&lt;br /&gt;
&lt;br /&gt;
Products which are available under some form of Open Source license, and which may also be available under some form of commercial license.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Open Source Cryptographic Libraries&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Library&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://sourceforge.net/projects/amissl/ AmiSSL] || an OpenSSL port to AmigaOS&lt;br /&gt;
|-&lt;br /&gt;
| [http://botan.randombit.net/ Botan] || a C++ cryptography library which includes a TLS implementation&lt;br /&gt;
|-&lt;br /&gt;
| [https://boringssl.googlesource.com/boringssl/ BoringSSL] || a Google fork of OpenSSL ([https://www.imperialviolet.org/2014/06/20/boringssl.html Announcement ]) (Press coverage: [https://news.ycombinator.com/item?id=7922745 ycombinator], [http://arstechnica.com/security/2014/06/google-unveils-independent-fork-of-openssl-called-boringssl/ Ars Technica], [http://www.pcworld.com/article/2366440/google-develops-own-boring-version-of-openssl.html PC World], [http://www.zdnet.com/google-takes-openssl-and-turns-it-into-boringssl-7000030796/ ZDNet] )&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.bouncycastle.org/ Bouncy Castle] || cryptography API for Java and C# ([[Wikipedia: Bouncy Castle (cryptography)]])&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.cs.auckland.ac.nz/~pgut001/cryptlib/ Cryptlib] || a security toolkit that allows one to easily add encryption and authentication services to software&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.cryptopp.com/ Crypto++] || a free C++ class library of cryptographic schemes&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.gnutls.org/ GnuTLS] || an LGPL-licensed TLS library with substantial documentation&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.libressl.org/ LibreSSL] || an OpenBSD fork of OpenSSL (Press coverage: [http://www.zdnet.com/openbsd-forks-prunes-fixes-openssl-7000028613/ ZDNet], [http://arstechnica.com/information-technology/2014/04/openssl-code-beyond-repair-claims-creator-of-libressl-fork/ Ars Technica])&lt;br /&gt;
|-&lt;br /&gt;
| [http://libtom.org/ LibTomCrypt] || public domain open source crypto library written in C&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.mitls.org/wsgi miTLS] || a verified reference implementation of the TLS protocol. ([http://www.reddit.com/r/netsec/comments/1zn2d3/mitls_a_verified_reference_tls_implementation/ &amp;quot;reddit: miTLS - A verified reference TLS implementation&amp;quot;])&lt;br /&gt;
|-&lt;br /&gt;
| [http://nacl.cr.yp.to/ NaCl] || NaCl (pronounced &amp;quot;salt&amp;quot;) is a easy-to-use high-speed software library for network communication, encryption, decryption, and signatures&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.mozilla.org/projects/security/pki/nss/ NSS] || a set of libraries designed to support cross-platform development of security-enabled client and server applications&lt;br /&gt;
|-&lt;br /&gt;
| [http://polarssl.org/ PolarSSL] || an SSL library that handles the complexities of the Secure Sockets Layer (SSL) protocol for an application ([[Wikipedia: PolarSSL]])&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.ohloh.net/projects/xyssl XySSL] || a C library providing a very small footprint crypto library and SSL implementation.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Open Source Products Using OpenSSL ==&lt;br /&gt;
&lt;br /&gt;
Products which are available under some form of Open Source License, and which may also be available under some form of commercial license.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Open Source Products Using OpenSSL&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Product&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://libevent.org/ libevent] || an event driven library which can [http://www.wangafu.net/~nickm/libevent-book/Ref6a_advanced_bufferevents.html#_bufferevents_and_ssl optionally use OpenSSL]&lt;br /&gt;
|-&lt;br /&gt;
| [http://en.wikipedia.org/wiki/Mod_ssl mod_ssl] || SSL/TLS module for the [http://en.wikipedia.org/wiki/Apache_HTTP_Server Apache HTTP Server]&lt;br /&gt;
|-&lt;br /&gt;
|[https://www.stunnel.org/index.html Stunnel] || an SSL encryption wrapper between remote client and local (inetd-startable) or remote server&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Languages libraries/Wrappers relying on openssl ===&lt;br /&gt;
&lt;br /&gt;
* '''libcurl''' http://curl.haxx.se/ which can use openssl&lt;br /&gt;
* '''PHP''' uses libcurl http://php.net/manual/en/intro.curl.php &lt;br /&gt;
* '''Python''' pyOpenSSL https://github.com/pyca/pyopenssl cryptography https://github.com/pyca/cryptography&lt;br /&gt;
* '''Perl''' Perl-Openssl http://sourceforge.net/projects/perl-openssl/&lt;br /&gt;
&lt;br /&gt;
== Closed Source Cryptographic Libraries ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Closed Source Cryptographic Libraries&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Library&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.example.com No Entry]  || This is a placeholder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Closed Source Products Using OpenSSL ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Closed Source Products Using OpenSSL&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Product&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| Stonesoft Firewall Appliances [https://www.stonesoft.com/en/customer_care/documentation/appliances/] &lt;br /&gt;
| Stonesoft (now McAfee) Appliances uses openssl as SSL/TLS communication library, license advertised [http://www.stonesoft.com/en/customer_care/support/third_party_licenses.html]&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.example.com No Entry]  || This is a placeholder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Even if this section is empty now, it exist a numerous number of closed source products using OpenSSL.&lt;br /&gt;
&lt;br /&gt;
== Books and Documentation == &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Books and Documentation&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Title&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [https://en.wikipedia.org/wiki/Comparison_of_TLS_implementations Comparison of TLS implementations]  || Wikipedia article comparing various TLS libraries&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.keylength.com/ keylength.com]  || site which summarizes various key length recommendations&lt;br /&gt;
|-&lt;br /&gt;
| [https://www.feistyduck.com/books/openssl-cookbook/ OpenSSL Cookbook]  || A free ebook that covers configuration and command-line usage (first&lt;br /&gt;
published in 2013; continuously updated)&lt;br /&gt;
|-&lt;br /&gt;
| [http://shop.oreilly.com/product/9780596002701.do Network Security with OpenSSL]  || O'Reilly book from 2002&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Testing Resources == &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Testing Resources&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Title&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [https://www.tls-o-matic.com/ TLS-O-MATIC]  || A set of tests for TLS clients&lt;br /&gt;
|-&lt;br /&gt;
| [https://www.ssllabs.com/ssltest/ SSL Labs server test]  || Probes any HTTPS server on the Internet and assigns it a letter grade&lt;br /&gt;
|-&lt;br /&gt;
| [https://www.ssllabs.com/ssltest/viewMyClient.html SSL Labs client test]  || Tests the TLS capabilities of your browser&lt;br /&gt;
|-&lt;br /&gt;
| [https://github.com/iSECPartners/tlspretense TLSPretense]  || A test framework for testing SSL/TLS client certificate validation&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Hostname_validation&amp;diff=2232</id>
		<title>Hostname validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Hostname_validation&amp;diff=2232"/>
		<updated>2015-06-07T02:11:04Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: better explain version situation, and add a link to mailing list discussion&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;One [https://crypto.stanford.edu/~dabo/pubs/abstracts/ssl-client-bugs.html very common mistake] made by users of OpenSSL is to assume that OpenSSL will validate the hostname in the server's certificate.  Versions prior to 1.0.2 did not perform hostname validation.  Version 1.0.2 and up contain support for hostname validation, but they still require the user to call a few functions to set it up.&lt;br /&gt;
&lt;br /&gt;
== Recommendation ==&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
(extracted from [http://mid.gmane.org/20150125175706.GN8034@mournblade.imrryr.org mailling list discussion answer from Viktor Dukhovni] who implemented the host checking extensions) &lt;br /&gt;
&lt;br /&gt;
Starting with 1.0.2 version use [https://www.openssl.org/docs/crypto/X509_check_host.html '''X509_check_host()'''] interface&lt;br /&gt;
&lt;br /&gt;
sample :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	const char *servername;&lt;br /&gt;
	SSL *ssl;&lt;br /&gt;
	X509_VERIFY_PARAM *param;&lt;br /&gt;
&lt;br /&gt;
	servername = &amp;quot;www.example.com&amp;quot;;&lt;br /&gt;
	ssl = SSL_new(...);&lt;br /&gt;
	param = SSL_get0_param(ssl);&lt;br /&gt;
&lt;br /&gt;
	/* Enable automatic hostname checks */&lt;br /&gt;
	X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);&lt;br /&gt;
	X509_VERIFY_PARAM_set1_host(param, servername, 0);&lt;br /&gt;
&lt;br /&gt;
	/* Configure a non-zero callback if desired */&lt;br /&gt;
	SSL_set_verify(ssl, SSL_VERIFY_PEER, 0);&lt;br /&gt;
&lt;br /&gt;
	/*&lt;br /&gt;
	 * Establish SSL connection, hostname should be checked&lt;br /&gt;
	 * automatically test with a hostname that should not match,&lt;br /&gt;
	 * the connection will fail (unless you specify a callback&lt;br /&gt;
	 * that returns despite the verification failure.  In that&lt;br /&gt;
	 * case SSL_get_verify_status() can expose the problem after&lt;br /&gt;
	 * connection completion.&lt;br /&gt;
	 */&lt;br /&gt;
	 ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Wildcard support is configured via the flags documented for X509_check_host(), the two most frequently useful are:&lt;br /&gt;
* '''X509_CHECK_FLAG_NO_WILDCARDS'''&lt;br /&gt;
* '''X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS'''&lt;br /&gt;
&lt;br /&gt;
populate the X509_VERIFY_PARAMS with the desired hostname, and let the OpenSSL code call X509_check_host automatically. &lt;br /&gt;
&lt;br /&gt;
This makes it easier to some day enable DANE TLSA support, because with DANE, name checks need to be skipped for DANE-EE(3) TLSA records, as the DNSSEC TLSA records provides the requisite name&lt;br /&gt;
binding instead.&lt;br /&gt;
&lt;br /&gt;
Also with the X509_VERIFY_PARAM approach, name checks happen early, and for applications that don't continue handshakes with unauthenticated peers, terminate as early as possible.&lt;br /&gt;
&lt;br /&gt;
There is an associated new X509 error code: '''X509_V_ERR_HOSTNAME_MISMATCH'''&lt;br /&gt;
&lt;br /&gt;
== ssl-conservatory and cURL code ==&lt;br /&gt;
&lt;br /&gt;
This was the original information, might still be valid for &amp;lt; 1.0.2 openssl versions :&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iSECPartners/ssl-conservatory The ssl-conservatory repository] shows how validating the hostname can be done.  However, the ssl-conservatory code does not handle wildcard certificates, so [http://archives.seul.org/libevent/users/Feb-2013/msg00043.html borrowing some code from cURL] might be one way to go instead.  [https://github.com/ppelleti/libevent/commit/4db9da6bbf8ade7b126840393173b8bd053b3389 This commit] shows how to graft the wildcard-matching code from cURL into the ssl-conservatory code.&lt;br /&gt;
&lt;br /&gt;
Below is a copy of [https://github.com/ppelleti/libevent/blob/4db9da6bbf8ade7b126840393173b8bd053b3389/sample/openssl_hostname_validation.c openssl_hostname_validation.c], although to compile it also needs the files [https://github.com/ppelleti/libevent/blob/4db9da6bbf8ade7b126840393173b8bd053b3389/sample/hostcheck.h hostcheck.h], [https://github.com/ppelleti/libevent/blob/4db9da6bbf8ade7b126840393173b8bd053b3389/sample/hostcheck.c hostcheck.c], and [https://github.com/ppelleti/libevent/blob/4db9da6bbf8ade7b126840393173b8bd053b3389/sample/openssl_hostname_validation.h openssl_hostname_validation.h].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* Obtained from: https://github.com/iSECPartners/ssl-conservatory */&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
Copyright (C) 2012, iSEC Partners.&lt;br /&gt;
&lt;br /&gt;
Permission is hereby granted, free of charge, to any person obtaining a copy of&lt;br /&gt;
this software and associated documentation files (the &amp;quot;Software&amp;quot;), to deal in&lt;br /&gt;
the Software without restriction, including without limitation the rights to&lt;br /&gt;
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies&lt;br /&gt;
of the Software, and to permit persons to whom the Software is furnished to do&lt;br /&gt;
so, subject to the following conditions:&lt;br /&gt;
&lt;br /&gt;
The above copyright notice and this permission notice shall be included in all&lt;br /&gt;
copies or substantial portions of the Software.&lt;br /&gt;
&lt;br /&gt;
THE SOFTWARE IS PROVIDED &amp;quot;AS IS&amp;quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR&lt;br /&gt;
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,&lt;br /&gt;
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE&lt;br /&gt;
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER&lt;br /&gt;
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,&lt;br /&gt;
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE&lt;br /&gt;
SOFTWARE.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * Helper functions to perform basic hostname validation using OpenSSL.&lt;br /&gt;
 *&lt;br /&gt;
 * Please read &amp;quot;everything-you-wanted-to-know-about-openssl.pdf&amp;quot; before&lt;br /&gt;
 * attempting to use this code. This whitepaper describes how the code works,&lt;br /&gt;
 * how it should be used, and what its limitations are.&lt;br /&gt;
 *&lt;br /&gt;
 * Author:  Alban Diquet&lt;br /&gt;
 * License: See LICENSE&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Get rid of OSX 10.7 and greater deprecation warnings.&lt;br /&gt;
#if defined(__APPLE__) &amp;amp;&amp;amp; defined(__clang__)&lt;br /&gt;
#pragma clang diagnostic ignored &amp;quot;-Wdeprecated-declarations&amp;quot;&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;openssl/x509v3.h&amp;gt;&lt;br /&gt;
#include &amp;lt;openssl/ssl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;openssl_hostname_validation.h&amp;quot;&lt;br /&gt;
#include &amp;quot;hostcheck.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#define HOSTNAME_MAX_SIZE 255&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
* Tries to find a match for hostname in the certificate's Common Name field.&lt;br /&gt;
*&lt;br /&gt;
* Returns MatchFound if a match was found.&lt;br /&gt;
* Returns MatchNotFound if no matches were found.&lt;br /&gt;
* Returns MalformedCertificate if the Common Name had a NUL character embedded in it.&lt;br /&gt;
* Returns Error if the Common Name could not be extracted.&lt;br /&gt;
*/&lt;br /&gt;
static HostnameValidationResult matches_common_name(const char *hostname, const X509 *server_cert) {&lt;br /&gt;
        int common_name_loc = -1;&lt;br /&gt;
        X509_NAME_ENTRY *common_name_entry = NULL;&lt;br /&gt;
        ASN1_STRING *common_name_asn1 = NULL;&lt;br /&gt;
        char *common_name_str = NULL;&lt;br /&gt;
&lt;br /&gt;
        // Find the position of the CN field in the Subject field of the certificate&lt;br /&gt;
        common_name_loc = X509_NAME_get_index_by_NID(X509_get_subject_name((X509 *) server_cert), NID_commonName, -1);&lt;br /&gt;
        if (common_name_loc &amp;lt; 0) {&lt;br /&gt;
                return Error;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        // Extract the CN field&lt;br /&gt;
        common_name_entry = X509_NAME_get_entry(X509_get_subject_name((X509 *) server_cert), common_name_loc);&lt;br /&gt;
        if (common_name_entry == NULL) {&lt;br /&gt;
                return Error;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        // Convert the CN field to a C string&lt;br /&gt;
        common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);&lt;br /&gt;
        if (common_name_asn1 == NULL) {&lt;br /&gt;
                return Error;&lt;br /&gt;
        }&lt;br /&gt;
        common_name_str = (char *) ASN1_STRING_data(common_name_asn1);&lt;br /&gt;
&lt;br /&gt;
        // Make sure there isn't an embedded NUL character in the CN&lt;br /&gt;
        if ((size_t)ASN1_STRING_length(common_name_asn1) != strlen(common_name_str)) {&lt;br /&gt;
                return MalformedCertificate;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        // Compare expected hostname with the CN&lt;br /&gt;
        if (Curl_cert_hostcheck(common_name_str, hostname) == CURL_HOST_MATCH) {&lt;br /&gt;
                return MatchFound;&lt;br /&gt;
        }&lt;br /&gt;
        else {&lt;br /&gt;
                return MatchNotFound;&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
* Tries to find a match for hostname in the certificate's Subject Alternative Name extension.&lt;br /&gt;
*&lt;br /&gt;
* Returns MatchFound if a match was found.&lt;br /&gt;
* Returns MatchNotFound if no matches were found.&lt;br /&gt;
* Returns MalformedCertificate if any of the hostnames had a NUL character embedded in it.&lt;br /&gt;
* Returns NoSANPresent if the SAN extension was not present in the certificate.&lt;br /&gt;
*/&lt;br /&gt;
static HostnameValidationResult matches_subject_alternative_name(const char *hostname, const X509 *server_cert) {&lt;br /&gt;
        HostnameValidationResult result = MatchNotFound;&lt;br /&gt;
        int i;&lt;br /&gt;
        int san_names_nb = -1;&lt;br /&gt;
        STACK_OF(GENERAL_NAME) *san_names = NULL;&lt;br /&gt;
&lt;br /&gt;
        // Try to extract the names within the SAN extension from the certificate&lt;br /&gt;
        san_names = X509_get_ext_d2i((X509 *) server_cert, NID_subject_alt_name, NULL, NULL);&lt;br /&gt;
        if (san_names == NULL) {&lt;br /&gt;
                return NoSANPresent;&lt;br /&gt;
        }&lt;br /&gt;
        san_names_nb = sk_GENERAL_NAME_num(san_names);&lt;br /&gt;
&lt;br /&gt;
        // Check each name within the extension&lt;br /&gt;
        for (i=0; i&amp;lt;san_names_nb; i++) {&lt;br /&gt;
                const GENERAL_NAME *current_name = sk_GENERAL_NAME_value(san_names, i);&lt;br /&gt;
&lt;br /&gt;
                if (current_name-&amp;gt;type == GEN_DNS) {&lt;br /&gt;
                        // Current name is a DNS name, let's check it&lt;br /&gt;
                        char *dns_name = (char *) ASN1_STRING_data(current_name-&amp;gt;d.dNSName);&lt;br /&gt;
&lt;br /&gt;
                        // Make sure there isn't an embedded NUL character in the DNS name&lt;br /&gt;
                        if ((size_t)ASN1_STRING_length(current_name-&amp;gt;d.dNSName) != strlen(dns_name)) {&lt;br /&gt;
                                result = MalformedCertificate;&lt;br /&gt;
                                break;&lt;br /&gt;
                        }&lt;br /&gt;
                        else { // Compare expected hostname with the DNS name&lt;br /&gt;
                                if (Curl_cert_hostcheck(dns_name, hostname)&lt;br /&gt;
                                    == CURL_HOST_MATCH) {&lt;br /&gt;
                                        result = MatchFound;&lt;br /&gt;
                                        break;&lt;br /&gt;
                                }&lt;br /&gt;
                        }&lt;br /&gt;
                }&lt;br /&gt;
        }&lt;br /&gt;
        sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);&lt;br /&gt;
&lt;br /&gt;
        return result;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
* Validates the server's identity by looking for the expected hostname in the&lt;br /&gt;
* server's certificate. As described in RFC 6125, it first tries to find a match&lt;br /&gt;
* in the Subject Alternative Name extension. If the extension is not present in&lt;br /&gt;
* the certificate, it checks the Common Name instead.&lt;br /&gt;
*&lt;br /&gt;
* Returns MatchFound if a match was found.&lt;br /&gt;
* Returns MatchNotFound if no matches were found.&lt;br /&gt;
* Returns MalformedCertificate if any of the hostnames had a NUL character embedded in it.&lt;br /&gt;
* Returns Error if there was an error.&lt;br /&gt;
*/&lt;br /&gt;
HostnameValidationResult validate_hostname(const char *hostname, const X509 *server_cert) {&lt;br /&gt;
        HostnameValidationResult result;&lt;br /&gt;
&lt;br /&gt;
        if((hostname == NULL) || (server_cert == NULL))&lt;br /&gt;
                return Error;&lt;br /&gt;
&lt;br /&gt;
        // First try the Subject Alternative Names extension&lt;br /&gt;
        result = matches_subject_alternative_name(hostname, server_cert);&lt;br /&gt;
        if (result == NoSANPresent) {&lt;br /&gt;
                // Extension was not found: try the Common Name&lt;br /&gt;
                result = matches_common_name(hostname, server_cert);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return result;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:SSL/TLS]]&lt;br /&gt;
[[Category:Common Mistake]]&lt;br /&gt;
[[Category:Examples]]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Hostname_validation&amp;diff=2231</id>
		<title>Hostname validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Hostname_validation&amp;diff=2231"/>
		<updated>2015-06-07T01:40:56Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* cUrl code */ That's really ssl-conservatory code, not cURL code&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;One [https://crypto.stanford.edu/~dabo/pubs/abstracts/ssl-client-bugs.html very common mistake] made by users of OpenSSL is to assume that OpenSSL will validate the hostname in the server's certificate.  Currently, it does not, although a future version (1.1.0?) will include this functionality.&lt;br /&gt;
&lt;br /&gt;
== Recommendation ==&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
(extracted from mailling list discussion answer from Viktor Dukhovni who actual did implement part of host checking extensions ) &lt;br /&gt;
&lt;br /&gt;
Starting with 1.0.2 version use '''X509_check_host()''' https://www.openssl.org/docs/crypto/X509_check_host.html interface&lt;br /&gt;
&lt;br /&gt;
sample :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	const char *servername;&lt;br /&gt;
	SSL *ssl;&lt;br /&gt;
	X509_VERIFY_PARAM *param;&lt;br /&gt;
&lt;br /&gt;
	servername = &amp;quot;www.example.com&amp;quot;;&lt;br /&gt;
	ssl = SSL_new(...);&lt;br /&gt;
	param = SSL_get0_param(ssl);&lt;br /&gt;
&lt;br /&gt;
	/* Enable automatic hostname checks */&lt;br /&gt;
	X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);&lt;br /&gt;
	X509_VERIFY_PARAM_set1_host(param, servername, 0);&lt;br /&gt;
&lt;br /&gt;
	/* Configure a non-zero callback if desired */&lt;br /&gt;
	SSL_set_verify(ssl, SSL_VERIFY_PEER, 0);&lt;br /&gt;
&lt;br /&gt;
	/*&lt;br /&gt;
	 * Establish SSL connection, hostname should be checked&lt;br /&gt;
	 * automatically test with a hostname that should not match,&lt;br /&gt;
	 * the connection will fail (unless you specify a callback&lt;br /&gt;
	 * that returns despite the verification failure.  In that&lt;br /&gt;
	 * case SSL_get_verify_status() can expose the problem after&lt;br /&gt;
	 * connection completion.&lt;br /&gt;
	 */&lt;br /&gt;
	 ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Wildcard support is configured via the flags documented for X509_check_host(), the two most frequently useful are:&lt;br /&gt;
* '''X509_CHECK_FLAG_NO_WILDCARDS'''&lt;br /&gt;
* '''X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS'''&lt;br /&gt;
&lt;br /&gt;
populate the X509_VERIFY_PARAMS with the desired hostname, and let the OpenSSL code call X509_check_host automatically. &lt;br /&gt;
&lt;br /&gt;
This makes it easier to some day enable DANE TLSA support, because with DANE, name checks need to be skipped for DANE-EE(3) TLSA records, as the DNSSEC TLSA records provides the requisite name&lt;br /&gt;
binding instead.&lt;br /&gt;
&lt;br /&gt;
Also with the X509_VERIFY_PARAM approach, name checks happen early, and for applications that don't continue handshakes with unauthenticated peers, terminate as early as possible.&lt;br /&gt;
&lt;br /&gt;
There is an associated new X509 error code: '''X509_V_ERR_HOSTNAME_MISMATCH'''&lt;br /&gt;
&lt;br /&gt;
== ssl-conservatory and cURL code ==&lt;br /&gt;
&lt;br /&gt;
This was the original information, might still be valid for &amp;lt; 1.0.2 openssl versions :&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iSECPartners/ssl-conservatory The ssl-conservatory repository] shows how validating the hostname can be done.  However, the ssl-conservatory code does not handle wildcard certificates, so [http://archives.seul.org/libevent/users/Feb-2013/msg00043.html borrowing some code from cURL] might be one way to go instead.  [https://github.com/ppelleti/libevent/commit/4db9da6bbf8ade7b126840393173b8bd053b3389 This commit] shows how to graft the wildcard-matching code from cURL into the ssl-conservatory code.&lt;br /&gt;
&lt;br /&gt;
Below is a copy of [https://github.com/ppelleti/libevent/blob/4db9da6bbf8ade7b126840393173b8bd053b3389/sample/openssl_hostname_validation.c openssl_hostname_validation.c], although to compile it also needs the files [https://github.com/ppelleti/libevent/blob/4db9da6bbf8ade7b126840393173b8bd053b3389/sample/hostcheck.h hostcheck.h], [https://github.com/ppelleti/libevent/blob/4db9da6bbf8ade7b126840393173b8bd053b3389/sample/hostcheck.c hostcheck.c], and [https://github.com/ppelleti/libevent/blob/4db9da6bbf8ade7b126840393173b8bd053b3389/sample/openssl_hostname_validation.h openssl_hostname_validation.h].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* Obtained from: https://github.com/iSECPartners/ssl-conservatory */&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
Copyright (C) 2012, iSEC Partners.&lt;br /&gt;
&lt;br /&gt;
Permission is hereby granted, free of charge, to any person obtaining a copy of&lt;br /&gt;
this software and associated documentation files (the &amp;quot;Software&amp;quot;), to deal in&lt;br /&gt;
the Software without restriction, including without limitation the rights to&lt;br /&gt;
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies&lt;br /&gt;
of the Software, and to permit persons to whom the Software is furnished to do&lt;br /&gt;
so, subject to the following conditions:&lt;br /&gt;
&lt;br /&gt;
The above copyright notice and this permission notice shall be included in all&lt;br /&gt;
copies or substantial portions of the Software.&lt;br /&gt;
&lt;br /&gt;
THE SOFTWARE IS PROVIDED &amp;quot;AS IS&amp;quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR&lt;br /&gt;
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,&lt;br /&gt;
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE&lt;br /&gt;
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER&lt;br /&gt;
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,&lt;br /&gt;
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE&lt;br /&gt;
SOFTWARE.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * Helper functions to perform basic hostname validation using OpenSSL.&lt;br /&gt;
 *&lt;br /&gt;
 * Please read &amp;quot;everything-you-wanted-to-know-about-openssl.pdf&amp;quot; before&lt;br /&gt;
 * attempting to use this code. This whitepaper describes how the code works,&lt;br /&gt;
 * how it should be used, and what its limitations are.&lt;br /&gt;
 *&lt;br /&gt;
 * Author:  Alban Diquet&lt;br /&gt;
 * License: See LICENSE&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Get rid of OSX 10.7 and greater deprecation warnings.&lt;br /&gt;
#if defined(__APPLE__) &amp;amp;&amp;amp; defined(__clang__)&lt;br /&gt;
#pragma clang diagnostic ignored &amp;quot;-Wdeprecated-declarations&amp;quot;&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;openssl/x509v3.h&amp;gt;&lt;br /&gt;
#include &amp;lt;openssl/ssl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;openssl_hostname_validation.h&amp;quot;&lt;br /&gt;
#include &amp;quot;hostcheck.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#define HOSTNAME_MAX_SIZE 255&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
* Tries to find a match for hostname in the certificate's Common Name field.&lt;br /&gt;
*&lt;br /&gt;
* Returns MatchFound if a match was found.&lt;br /&gt;
* Returns MatchNotFound if no matches were found.&lt;br /&gt;
* Returns MalformedCertificate if the Common Name had a NUL character embedded in it.&lt;br /&gt;
* Returns Error if the Common Name could not be extracted.&lt;br /&gt;
*/&lt;br /&gt;
static HostnameValidationResult matches_common_name(const char *hostname, const X509 *server_cert) {&lt;br /&gt;
        int common_name_loc = -1;&lt;br /&gt;
        X509_NAME_ENTRY *common_name_entry = NULL;&lt;br /&gt;
        ASN1_STRING *common_name_asn1 = NULL;&lt;br /&gt;
        char *common_name_str = NULL;&lt;br /&gt;
&lt;br /&gt;
        // Find the position of the CN field in the Subject field of the certificate&lt;br /&gt;
        common_name_loc = X509_NAME_get_index_by_NID(X509_get_subject_name((X509 *) server_cert), NID_commonName, -1);&lt;br /&gt;
        if (common_name_loc &amp;lt; 0) {&lt;br /&gt;
                return Error;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        // Extract the CN field&lt;br /&gt;
        common_name_entry = X509_NAME_get_entry(X509_get_subject_name((X509 *) server_cert), common_name_loc);&lt;br /&gt;
        if (common_name_entry == NULL) {&lt;br /&gt;
                return Error;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        // Convert the CN field to a C string&lt;br /&gt;
        common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);&lt;br /&gt;
        if (common_name_asn1 == NULL) {&lt;br /&gt;
                return Error;&lt;br /&gt;
        }&lt;br /&gt;
        common_name_str = (char *) ASN1_STRING_data(common_name_asn1);&lt;br /&gt;
&lt;br /&gt;
        // Make sure there isn't an embedded NUL character in the CN&lt;br /&gt;
        if ((size_t)ASN1_STRING_length(common_name_asn1) != strlen(common_name_str)) {&lt;br /&gt;
                return MalformedCertificate;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        // Compare expected hostname with the CN&lt;br /&gt;
        if (Curl_cert_hostcheck(common_name_str, hostname) == CURL_HOST_MATCH) {&lt;br /&gt;
                return MatchFound;&lt;br /&gt;
        }&lt;br /&gt;
        else {&lt;br /&gt;
                return MatchNotFound;&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
* Tries to find a match for hostname in the certificate's Subject Alternative Name extension.&lt;br /&gt;
*&lt;br /&gt;
* Returns MatchFound if a match was found.&lt;br /&gt;
* Returns MatchNotFound if no matches were found.&lt;br /&gt;
* Returns MalformedCertificate if any of the hostnames had a NUL character embedded in it.&lt;br /&gt;
* Returns NoSANPresent if the SAN extension was not present in the certificate.&lt;br /&gt;
*/&lt;br /&gt;
static HostnameValidationResult matches_subject_alternative_name(const char *hostname, const X509 *server_cert) {&lt;br /&gt;
        HostnameValidationResult result = MatchNotFound;&lt;br /&gt;
        int i;&lt;br /&gt;
        int san_names_nb = -1;&lt;br /&gt;
        STACK_OF(GENERAL_NAME) *san_names = NULL;&lt;br /&gt;
&lt;br /&gt;
        // Try to extract the names within the SAN extension from the certificate&lt;br /&gt;
        san_names = X509_get_ext_d2i((X509 *) server_cert, NID_subject_alt_name, NULL, NULL);&lt;br /&gt;
        if (san_names == NULL) {&lt;br /&gt;
                return NoSANPresent;&lt;br /&gt;
        }&lt;br /&gt;
        san_names_nb = sk_GENERAL_NAME_num(san_names);&lt;br /&gt;
&lt;br /&gt;
        // Check each name within the extension&lt;br /&gt;
        for (i=0; i&amp;lt;san_names_nb; i++) {&lt;br /&gt;
                const GENERAL_NAME *current_name = sk_GENERAL_NAME_value(san_names, i);&lt;br /&gt;
&lt;br /&gt;
                if (current_name-&amp;gt;type == GEN_DNS) {&lt;br /&gt;
                        // Current name is a DNS name, let's check it&lt;br /&gt;
                        char *dns_name = (char *) ASN1_STRING_data(current_name-&amp;gt;d.dNSName);&lt;br /&gt;
&lt;br /&gt;
                        // Make sure there isn't an embedded NUL character in the DNS name&lt;br /&gt;
                        if ((size_t)ASN1_STRING_length(current_name-&amp;gt;d.dNSName) != strlen(dns_name)) {&lt;br /&gt;
                                result = MalformedCertificate;&lt;br /&gt;
                                break;&lt;br /&gt;
                        }&lt;br /&gt;
                        else { // Compare expected hostname with the DNS name&lt;br /&gt;
                                if (Curl_cert_hostcheck(dns_name, hostname)&lt;br /&gt;
                                    == CURL_HOST_MATCH) {&lt;br /&gt;
                                        result = MatchFound;&lt;br /&gt;
                                        break;&lt;br /&gt;
                                }&lt;br /&gt;
                        }&lt;br /&gt;
                }&lt;br /&gt;
        }&lt;br /&gt;
        sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);&lt;br /&gt;
&lt;br /&gt;
        return result;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
* Validates the server's identity by looking for the expected hostname in the&lt;br /&gt;
* server's certificate. As described in RFC 6125, it first tries to find a match&lt;br /&gt;
* in the Subject Alternative Name extension. If the extension is not present in&lt;br /&gt;
* the certificate, it checks the Common Name instead.&lt;br /&gt;
*&lt;br /&gt;
* Returns MatchFound if a match was found.&lt;br /&gt;
* Returns MatchNotFound if no matches were found.&lt;br /&gt;
* Returns MalformedCertificate if any of the hostnames had a NUL character embedded in it.&lt;br /&gt;
* Returns Error if there was an error.&lt;br /&gt;
*/&lt;br /&gt;
HostnameValidationResult validate_hostname(const char *hostname, const X509 *server_cert) {&lt;br /&gt;
        HostnameValidationResult result;&lt;br /&gt;
&lt;br /&gt;
        if((hostname == NULL) || (server_cert == NULL))&lt;br /&gt;
                return Error;&lt;br /&gt;
&lt;br /&gt;
        // First try the Subject Alternative Names extension&lt;br /&gt;
        result = matches_subject_alternative_name(hostname, server_cert);&lt;br /&gt;
        if (result == NoSANPresent) {&lt;br /&gt;
                // Extension was not found: try the Common Name&lt;br /&gt;
                result = matches_common_name(hostname, server_cert);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return result;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:SSL/TLS]]&lt;br /&gt;
[[Category:Common Mistake]]&lt;br /&gt;
[[Category:Examples]]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=1608</id>
		<title>Related Links</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=1608"/>
		<updated>2014-04-22T19:30:51Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Open Source Cryptographic Libraries */ update links for LibreSSL&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Please feel free to edit this page and add your own OpenSSL-based project or product. This is the one place where otherwise extraneous mention of commercial products is appropriate. Note such mention does not constitute endorsement per our [[Commercial Product Disclaimer]].&lt;br /&gt;
&lt;br /&gt;
== Open Source Cryptographic Libraries ==&lt;br /&gt;
&lt;br /&gt;
Products which are available under some form of Open Source license, and which may also be available under some form of commercial license.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Open Source Cryptographic Libraries&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Library&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://botan.randombit.net/ Botan]  || a C++ cryptography library which includes a TLS implementation&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.bouncycastle.org/ Bouncy Castle] || cryptography API for Java and C# ([[Wikipedia: Bouncy Castle (cryptography)]])&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.cs.auckland.ac.nz/~pgut001/cryptlib/ Cryptlib] || a security toolkit that allows one to easily add encryption and authentication services to software&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.cryptopp.com/ Crypto++] || a free C++ class library of cryptographic schemes&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.gnutls.org/ GnuTLS] || an LGPL-licensed TLS library with substantial documentation&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.libressl.org/ LibreSSL] || an OpenBSD fork of OpenSSL (Press coverage: [http://www.zdnet.com/openbsd-forks-prunes-fixes-openssl-7000028613/ ZDNet], [http://arstechnica.com/information-technology/2014/04/openssl-code-beyond-repair-claims-creator-of-libressl-fork/ Ars Technica])&lt;br /&gt;
|-&lt;br /&gt;
| [http://libtom.org/ LibTomCrypt] || public domain open source crypto library written in C&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.mitls.org/wsgi miTLS] || a verified reference implementation of the TLS protocol. ([http://www.reddit.com/r/netsec/comments/1zn2d3/mitls_a_verified_reference_tls_implementation/ &amp;quot;reddit: miTLS - A verified reference TLS implementation&amp;quot;])&lt;br /&gt;
|-&lt;br /&gt;
| [http://nacl.cr.yp.to/ NaCl] || NaCl (pronounced &amp;quot;salt&amp;quot;) is a easy-to-use high-speed software library for network communication, encryption, decryption, and signatures&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.mozilla.org/projects/security/pki/nss/ NSS] || a set of libraries designed to support cross-platform development of security-enabled client and server applications&lt;br /&gt;
|-&lt;br /&gt;
| [http://polarssl.org/ PolarSSL] || an SSL library that handles the complexities of the Secure Sockets Layer (SSL) protocol for an application ([[Wikipedia: PolarSSL]])&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.ohloh.net/projects/xyssl XySSL] || a C library providing a very small footprint crypto library and SSL implementation.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Open Source Products Using OpenSSL ==&lt;br /&gt;
&lt;br /&gt;
Products which are available under some form of Open Source License, and which may also be available under some form of commercial license.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Open Source Products Using OpenSSL&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Product&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://libevent.org/ libevent] || an event driven library which can [http://www.wangafu.net/~nickm/libevent-book/Ref6a_advanced_bufferevents.html#_bufferevents_and_ssl optionally use OpenSSL]&lt;br /&gt;
|-&lt;br /&gt;
| [http://en.wikipedia.org/wiki/Mod_ssl mod_ssl] || SSL/TLS module for the [http://en.wikipedia.org/wiki/Apache_HTTP_Server Apache HTTP Server]&lt;br /&gt;
|-&lt;br /&gt;
|[https://www.stunnel.org/index.html Stunnel] || an SSL encryption wrapper between remote client and local (inetd-startable) or remote server&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Closed Source Cryptographic Libraries ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Closed Source Cryptographic Libraries&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Library&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.example.com No Entry]  || This is a placeholder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Closed Source Products Using OpenSSL ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Closed Source Products Using OpenSSL&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Product&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.example.com No Entry]  || This is a placeholder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Books and Documentation == &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Books and Documentation&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Title&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [https://en.wikipedia.org/wiki/Comparison_of_TLS_implementations Comparison of TLS implementations]  || Wikipedia article comparing various TLS libraries&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.keylength.com/ keylength.com]  || site which summarizes various key length recommendations&lt;br /&gt;
|-&lt;br /&gt;
| [https://www.feistyduck.com/books/openssl-cookbook/ OpenSSL Cookbook]  || A free ebook that covers configuration and command-line usage (first&lt;br /&gt;
published in 2013; continuously updated)&lt;br /&gt;
|-&lt;br /&gt;
| [http://shop.oreilly.com/product/9780596002701.do Network Security with OpenSSL]  || O'Reilly book from 2002&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=1606</id>
		<title>Related Links</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=1606"/>
		<updated>2014-04-21T23:11:26Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Open Source Cryptographic Libraries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Please feel free to edit this page and add your own OpenSSL-based project or product. This is the one place where otherwise extraneous mention of commercial products is appropriate. Note such mention does not constitute endorsement per our [[Commercial Product Disclaimer]].&lt;br /&gt;
&lt;br /&gt;
== Open Source Cryptographic Libraries ==&lt;br /&gt;
&lt;br /&gt;
Products which are available under some form of Open Source license, and which may also be available under some form of commercial license.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Open Source Cryptographic Libraries&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Library&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://botan.randombit.net/ Botan]  || a C++ cryptography library which includes a TLS implementation&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.bouncycastle.org/ Bouncy Castle] || cryptography API for Java and C# ([[Wikipedia: Bouncy Castle (cryptography)]])&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.cs.auckland.ac.nz/~pgut001/cryptlib/ Cryptlib] || a security toolkit that allows one to easily add encryption and authentication services to software&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.cryptopp.com/ Crypto++] || a free C++ class library of cryptographic schemes&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.gnutls.org/ GnuTLS] || an LGPL-licensed TLS library with substantial documentation&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.openbsdfoundation.org/ LibreSSL] || an OpenBSD fork of OpenSSL ([http://www.zdnet.com/openbsd-forks-prunes-fixes-openssl-7000028613/ &amp;quot;ZDNet: OpenBSD forks, prunes, fixes OpenSSL&amp;quot;])&lt;br /&gt;
|-&lt;br /&gt;
| [http://libtom.org/ LibTomCrypt] || public domain open source crypto library written in C&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.mitls.org/wsgi miTLS] || a verified reference implementation of the TLS protocol. ([http://www.reddit.com/r/netsec/comments/1zn2d3/mitls_a_verified_reference_tls_implementation/ &amp;quot;reddit: miTLS - A verified reference TLS implementation&amp;quot;])&lt;br /&gt;
|-&lt;br /&gt;
| [http://nacl.cr.yp.to/ NaCl] || NaCl (pronounced &amp;quot;salt&amp;quot;) is a easy-to-use high-speed software library for network communication, encryption, decryption, and signatures&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.mozilla.org/projects/security/pki/nss/ NSS] || a set of libraries designed to support cross-platform development of security-enabled client and server applications&lt;br /&gt;
|-&lt;br /&gt;
| [http://polarssl.org/ PolarSSL] || an SSL library that handles the complexities of the Secure Sockets Layer (SSL) protocol for an application ([[Wikipedia: PolarSSL]])&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.ohloh.net/projects/xyssl XySSL] || a C library providing a very small footprint crypto library and SSL implementation.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Open Source Products Using OpenSSL ==&lt;br /&gt;
&lt;br /&gt;
Products which are available under some form of Open Source License, and which may also be available under some form of commercial license.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Open Source Products Using OpenSSL&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Product&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://libevent.org/ libevent] || an event driven library which can [http://www.wangafu.net/~nickm/libevent-book/Ref6a_advanced_bufferevents.html#_bufferevents_and_ssl optionally use OpenSSL]&lt;br /&gt;
|-&lt;br /&gt;
| [http://en.wikipedia.org/wiki/Mod_ssl mod_ssl] || SSL/TLS module for the [http://en.wikipedia.org/wiki/Apache_HTTP_Server Apache HTTP Server]&lt;br /&gt;
|-&lt;br /&gt;
|[https://www.stunnel.org/index.html Stunnel] || an SSL encryption wrapper between remote client and local (inetd-startable) or remote server&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Closed Source Cryptographic Libraries ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Closed Source Cryptographic Libraries&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Library&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.example.com No Entry]  || This is a placeholder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Closed Source Products Using OpenSSL ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Closed Source Products Using OpenSSL&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Product&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.example.com No Entry]  || This is a placeholder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Books and Documentation == &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Books and Documentation&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Title&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [https://en.wikipedia.org/wiki/Comparison_of_TLS_implementations Comparison of TLS implementations]  || Wikipedia article comparing various TLS libraries&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.keylength.com/ keylength.com]  || site which summarizes various key length recommendations&lt;br /&gt;
|-&lt;br /&gt;
| [https://www.feistyduck.com/books/openssl-cookbook/ OpenSSL Cookbook]  || A free ebook that covers configuration and command-line usage (first&lt;br /&gt;
published in 2013; continuously updated)&lt;br /&gt;
|-&lt;br /&gt;
| [http://shop.oreilly.com/product/9780596002701.do Network Security with OpenSSL]  || O'Reilly book from 2002&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Memory_management_internals&amp;diff=1598</id>
		<title>Memory management internals</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Memory_management_internals&amp;diff=1598"/>
		<updated>2014-04-14T06:12:43Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: document OPENSSL_NO_BUF_FREELIST&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== free list in libssl ==&lt;br /&gt;
&lt;br /&gt;
libssl maintains an internal free list, rather than calling malloc/free directly.  See these two blog posts:&lt;br /&gt;
&lt;br /&gt;
* [http://www.tedunangst.com/flak/post/heartbleed-vs-mallocconf heartbleed-vs-mallocconf]&lt;br /&gt;
* [http://www.tedunangst.com/flak/post/analysis-of-openssl-freelist-reuse analysis-of-openssl-freelist-reuse]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Internals&amp;diff=1597</id>
		<title>Internals</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Internals&amp;diff=1597"/>
		<updated>2014-04-14T06:03:38Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: first sub-page for Internals&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* [[Memory management internals]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Internals]]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=1418</id>
		<title>Related Links</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=1418"/>
		<updated>2014-01-22T09:04:54Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Books and Documentation */ add link to keylength.com&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Please feel free to edit this page and add your own OpenSSL-based project or product. This is the one place where otherwise extraneous mention of commercial products is appropriate. Note such mention does not constitute endorsement per our [[Commercial Product Disclaimer]].&lt;br /&gt;
&lt;br /&gt;
== Open Source Cryptographic Libraries ==&lt;br /&gt;
&lt;br /&gt;
Products which are available under some form of Open Source license, and which may also be available under some form of commercial license.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Open Source Cryptographic Libraries&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Library&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://botan.randombit.net/ Botan]  || a C++ cryptography library which includes a TLS implementation&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.bouncycastle.org/ Bouncy Castle] || cryptography API for Java and C#&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.cs.auckland.ac.nz/~pgut001/cryptlib/ Cryptlib] || a security toolkit that allows one to easily add encryption and authentication services to software&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.cryptopp.com/ Crypto++] || a free C++ class library of cryptographic schemes&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.gnutls.org/ GnuTLS] || an LGPL-licensed TLS library with substantial documentation&lt;br /&gt;
|-&lt;br /&gt;
| [http://libtom.org/ LibTomCrypt] || public domain open source crypto library written in C&lt;br /&gt;
|-&lt;br /&gt;
| [http://nacl.cr.yp.to/ NaCl] || NaCl (pronounced &amp;quot;salt&amp;quot;) is a easy-to-use high-speed software library for network communication, encryption, decryption, and signatures&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.mozilla.org/projects/security/pki/nss/ NSS] || a set of libraries designed to support cross-platform development of security-enabled client and server applications&lt;br /&gt;
|-&lt;br /&gt;
| [http://polarssl.org/ PolarSSL] || an SSL library that handles the complexities of the Secure Sockets Layer (SSL) protocol for an application&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.ohloh.net/projects/xyssl XySSL] || a C library providing a very small footprint crypto library and SSL implementation.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Open Source Products Using OpenSSL ==&lt;br /&gt;
&lt;br /&gt;
Products which are available under some form of Open Source License, and which may also be available under some form of commercial license.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Open Source Products Using OpenSSL&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Product&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://libevent.org/ libevent] || an event driven library which can [http://www.wangafu.net/~nickm/libevent-book/Ref6a_advanced_bufferevents.html#_bufferevents_and_ssl optionally use OpenSSL]&lt;br /&gt;
|-&lt;br /&gt;
| [http://en.wikipedia.org/wiki/Mod_ssl mod_ssl] || SSL/TLS module for the [http://en.wikipedia.org/wiki/Apache_HTTP_Server Apache HTTP Server]&lt;br /&gt;
|-&lt;br /&gt;
|[https://www.stunnel.org/index.html Stunnel] || an SSL encryption wrapper between remote client and local (inetd-startable) or remote server&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Closed Source Cryptographic Libraries ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Closed Source Cryptographic Libraries&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Library&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.example.com No Entry]  || This is a placeholder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Closed Source Products Using OpenSSL ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Closed Source Products Using OpenSSL&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Product&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.example.com No Entry]  || This is a placeholder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Books and Documentation == &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Books and Documentation&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Title&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [https://en.wikipedia.org/wiki/Comparison_of_TLS_implementations Comparison of TLS implementations]  || Wikipedia article comparing various TLS libraries&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.keylength.com/ keylength.com]  || site which summarizes various key length recommendations&lt;br /&gt;
|-&lt;br /&gt;
| [https://www.feistyduck.com/books/openssl-cookbook/ OpenSSL Cookbook]  || A free ebook that covers configuration and command-line usage (first&lt;br /&gt;
published in 2013; continuously updated)&lt;br /&gt;
|-&lt;br /&gt;
| [http://shop.oreilly.com/product/9780596002701.do Network Security with OpenSSL]  || O'Reilly book from 2002&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1417</id>
		<title>Random fork-safety</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1417"/>
		<updated>2014-01-22T08:59:23Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: another patch for fork-safety issue&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;One of the most important issues in the proper cryptographic use of random numbers is that random numbers must not be reused.  Since the UNIX fork() system call duplicates the entire process state, a random number generator which does not take this issue into account will produce the same sequence of random numbers in both the parent and the child (or in multiple children), leading to cryptographic disaster (i. e. people being able to read your communications).&lt;br /&gt;
&lt;br /&gt;
OpenSSL's default random number generator mixes in the PID, which provides a certain degree of fork safety.  However, once the PIDs wrap, new children will start to produce the same random sequence as previous children which had the same PID.  This is unlikely to happen in most common cases, but it is not impossible, which makes the issue even more insidious.&lt;br /&gt;
&lt;br /&gt;
The most comprehensive explanation of this problem is probably this blog post:&lt;br /&gt;
&lt;br /&gt;
* [http://emboss.github.io/blog/2013/08/21/openssl-prng-is-not-really-fork-safe/ OpenSSL PRNG Is Not (Really) Fork-safe - martinboßlet - Aug 21, 2013]&lt;br /&gt;
&lt;br /&gt;
However, since this issue has been &amp;quot;rediscovered&amp;quot; and discussed multiple times, here are some additional links (some are also linked from the above article)&lt;br /&gt;
&lt;br /&gt;
* [http://marc.info/?l=openssl-dev&amp;amp;m=130289811108150&amp;amp;w=2 openssl-dev: recycled pids causes PRNG to repeat - Eric Wong - Apr 15, 2011]&lt;br /&gt;
* [https://bugs.ruby-lang.org/issues/4579 Ruby bug 4579: SecureRandom + OpenSSL may repeat with fork - Apr 15, 2011]&lt;br /&gt;
* [http://www.postgresql.org/message-id/E1UKzBn-0006c2-Cy@gemulon.postgresql.org pgsql: Reset OpenSSL randomness state in each postmaster childprocess - Tom Lane - Mar 27, 2013]&lt;br /&gt;
* [https://plus.google.com/+AndroidDevelopers/posts/YxWzeNQMJS2 Google+ discussion of Android SecureRandom issue, caused by OpenSSL PID wraparound - Aug 14, 2013]&lt;br /&gt;
* [http://jbp.io/2013/08/15/android-securerandom-guess/ Android SecureRandom vulnerability guess - Joseph Birr-Pixton - Aug 15, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-users@openssl.org/msg71749.html openssl-users: DLL hell - Nico Williams - Aug 15, 2013]&lt;br /&gt;
** [http://www.mail-archive.com/openssl-dev@openssl.org/msg32867.html openssl-dev: not fork-safe if pids wrap (continuation of above openssl-users thread) - Aug 22, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33003.html openssl-dev: Patch to increase rng entropy after forking - Ernst-Udo Wallenborn - Sept 18, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33012.html openssl-dev: Mixing time into the pool - Ben Laurie - Sept 21, 2013]&lt;br /&gt;
** [https://github.com/openssl/openssl/commit/3cd8547a2018ada88a4303067a2aa15eadc17f39 commit mentioned in above message]&lt;br /&gt;
* [http://bugs.python.org/issue19227 Python issue 19227: test_multiprocessing_xxx hangs under Gentoo buildbots - Oct 11, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33604.html openssl-dev: Reseed PRNG on PID change - Florian Weimer - Jan 15, 2014]&lt;br /&gt;
&lt;br /&gt;
== Remediations ==&lt;br /&gt;
&lt;br /&gt;
OpenSSL cannot fix the fork-safety problem because its not in a position to do so. However, there are remediations available and they are listed below.&lt;br /&gt;
&lt;br /&gt;
* Don't use &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt;&lt;br /&gt;
* Call &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; after a fork&lt;br /&gt;
* Call &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; after a fork&lt;br /&gt;
* Use a hardware based generator&lt;br /&gt;
* Practice hedging cryptography&lt;br /&gt;
&lt;br /&gt;
The first remediation is to avoid using &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt;. Instead, you can read directly from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;/dev/srandom&amp;lt;/tt&amp;gt;; or use &amp;lt;tt&amp;gt;CryptGenRandom&amp;lt;/tt&amp;gt; on Windows systems. Avoiding &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; is not practical in practice because the library will use it internally.&lt;br /&gt;
&lt;br /&gt;
The second remediation is to call &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; after a fork. Entropy can be obtained from the operating system by reading from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;/dev/srandom&amp;lt;/tt&amp;gt;; or using &amp;lt;tt&amp;gt;CryptGenRandom&amp;lt;/tt&amp;gt; on Windows systems. For mobile devices with an interactive user, you could even add sensor data from the accelerometer, magnetometer and gyroscopes. This is appropriate for most programs, but might have problems in low entropy environments such as mobile devices and headless servers. Additionally, this could have problems in virtualized environments. For details, see [[Random_Numbers|Random Numbers]].&lt;br /&gt;
&lt;br /&gt;
The third remediation is to use &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; after a fork. This is used by OpenSSL to seed the generator on startup. The function always reads from &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt;, so you will have to seed the generator yourself if you want to use &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;/dev/srandom&amp;lt;/tt&amp;gt;. This is appropriate for most programs, and recommended by a number of people familiar with the library. This method has the same potential problems as &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt;. For details, see [[Random_Numbers|Random Numbers]].&lt;br /&gt;
&lt;br /&gt;
The fourth remediation is to use a hardware based generator. This is not always practical because hardware is not always present. Additionally, hardware is not usually auditable so some question its unabridged use in the post-Snowden era. For details, see [[Random_Numbers|Random Numbers]] and [[Manual:Engine(3)|OpenSSL engine(3)]] man page.&lt;br /&gt;
&lt;br /&gt;
The fifth remediation is to practice hedging cryptography. Hedging uses entropy gathered from a peer during key exchange or key agreement to add to the program's internal entropy pool (for example, the random '''R&amp;lt;sub&amp;gt;A&amp;lt;/sub&amp;gt;''' or '''R&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;''' in SSL/TLS). The benefit of hedging is its resilient against fork problems, low entropy environments, and virtual machine playbacks. For details, see [http://static.usenix.org/event/hotos05/final_papers/full_papers/garfinkel/garfinkel.pdf When Virtual is Harder than Real: Resource Allocation Challenges in Virtual Machine Based IT Environments] and [http://www.isoc.org/isoc/conferences/ndss/10/pdf/15.pdf When Good Randomness Goes Bad: Virtual Machine Reset Vulnerabilities and Hedging Deployed Cryptography].&lt;br /&gt;
&lt;br /&gt;
Some of the items above recommend reading from operating system provided facilities, such as &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt;. Since the random number generator should be in good order before the fork, you should be able to add unique information to diversify the generator states between parent and child. Unique information would include the time the call was made and the process identifier. In fact, Ben Laurie [https://github.com/openssl/openssl/commit/3cd8547a2018ada88a4303067a2aa15eadc17f39 pushed a patch to use time] as the uniqueness (unfortunately, it will be years before it gains widespread adoption in the field).&lt;br /&gt;
&lt;br /&gt;
The code below can be used to add unique information to the generator after a fork. The call to &amp;lt;tt&amp;gt;time&amp;lt;/tt&amp;gt; ensures the information is unique if the PIDs wrap. The call to &amp;lt;tt&amp;gt;getpid&amp;lt;/tt&amp;gt; ensures that two forks that occur back-to-back on hardware with low resolution timers are unique. &amp;lt;tt&amp;gt;long[2]&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;long[3]&amp;lt;/tt&amp;gt; attempts to further enhance the generator's state by using random data on the stack (if present).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;long long seed[4];&lt;br /&gt;
seed[0] = (long long)time(NULL);&lt;br /&gt;
seed[1] = (long long)getpid();&lt;br /&gt;
RAND_seed(seed, sizeof(seed));&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Random_Numbers&amp;diff=1416</id>
		<title>Random Numbers</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Random_Numbers&amp;diff=1416"/>
		<updated>2014-01-22T08:40:00Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* See also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Random Numbers]] are a cryptographic primitive and cornerstone to nearly all cryptographic systems. They are used in almost all areas of cryptography, from key agreement and transport to session keys for bulk encryption. A quality source of random bits and proper use of OpenSSL APIs will help ensure your program is cryptographically sound. On the other hand, a poor source of randomness or incorrect library usage could result in loss of security. This article will help you use random number generation routines correctly when programming with the OpenSSL library.&lt;br /&gt;
&lt;br /&gt;
OpenSSL provides a number of software based random number generators based on a variety of sources.  A software based random number generator creates random numbers by executing a software algorithm. There are a number of algorithms specified by a number of standard bodies including NIST, ANSI X9 committee (X9.17 and X9.31) and XXX. In addition, the library can use custom hardware if the hardware has an &amp;lt;tt&amp;gt;ENIGNE&amp;lt;/tt&amp;gt; interface. &lt;br /&gt;
&lt;br /&gt;
Good random numbers are notoriously hard to produce from deterministic processes such as a computer executing instructions. A number of cryptographic attacks have been developed because they are so hard to acquire. Especially vulnerable are headless servers, embedded devices, and mobile devices, and you may have to take extra steps to ensure an adequate supply of entropy is available. The extra steps could include ''Hedging'' on a headless server or embedded device, and ''Finger Painting'' on a mobile device. For recent attacks on low entropy devices (such as headless servers and mobile devices), see for example, ''[http://pages.cs.wisc.edu/~rist/papers/sslhedge.html When Good Randomness Goes Bad: Virtual Machine Reset Vulnerabilities and Hedging Deployed Cryptography ]'', ''[https://factorable.net/paper.html Mining Your Ps and Qs: Detection of Widespread Weak Keys in Network Devices]'', and ''[http://www.csoonline.com/article/723229/traffic-sensor-flaw-that-could-allow-driver-tracking-fixed Traffic sensor flaw that could allow driver tracking fixed]''.&lt;br /&gt;
&lt;br /&gt;
== Entropy ==&lt;br /&gt;
&lt;br /&gt;
Entropy is the measure of &amp;quot;randomness&amp;quot; in a sequence of bits. Different sources have different entropy. For example, a physical process in nature may have 100% entropy which appears purely random. On the other hand, the written English language provides about 3 bits/byte (or character) which is at most 38%. Some estimates have shown English characters provide only 1 bit/byte (or 12%). Other sources used as a random stream will have different estimates of entropy, and you will have to determine the quality.&lt;br /&gt;
&lt;br /&gt;
Random number generators require quality entropy for input (a ''seed'', discussed below) and must produce quality output (''quod vide''). When using OpenSSL's APIs, you will be asked to estimate entropy when seeding or reseeding (input). When estimating entropy you should error on the low side to ensure proper fitness of the generator. When receiving bytes, you will receive a code indicating the success/failure of the operation and quality of the bytes (output).&lt;br /&gt;
&lt;br /&gt;
=== Sources ===&lt;br /&gt;
&lt;br /&gt;
Sometimes the operating system offers block access to hardware random number generators via &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; can be a low volume device, and could potentially block. For example, the [http://www.portwell.com.tw/download/sbc/catalog/Intel_80802_FWH.pdf Intel 82802 Firmware Hub] used with the and i840 chipset produces one byte of data in its register. At other times, &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; can be a high volume device, such as Intel's [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key Technology]. In virtualized environments, &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; might actually be a [http://wiki.qemu.org/Features-Done/VirtIORNG VirtIO RNG].&lt;br /&gt;
&lt;br /&gt;
Entropy is important for a healthy program, and you should investigate hardware modules to help acquire it, especially if poor entropy or entropy depletion are a concern. There are a number of inexpensive and high quality hardware modules on the market, including a $40UK [http://www.entropykey.co.uk EntropyKey]. There are also a number of high quality and high priced hardware modules and accelerators.&lt;br /&gt;
&lt;br /&gt;
If you lack &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt; and cannot procure a hardware random number generator, you can also consider an alternate entropy gather such as the [http://egd.sourceforge.net Entropy Gathering Daemon (EGD)]. EGD is an userspace substitute for &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;. OpenSSL provides native support for EGD via &amp;lt;tt&amp;gt;[[Manual:RAND_egd(3)|RAND_egd]]&amp;lt;/tt&amp;gt; to connect to the Unix domain socket, and &amp;lt;tt&amp;gt;[[Manual:RAND_egd(3)|RAND_egd_bytes]]&amp;lt;/tt&amp;gt; to extract bytes from the daemon.&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
It is not possible to assess whether a source of randomness is truly random by merely examining its bits. An &amp;quot;ideal&amp;quot; source of truly random data can be thought of as a sequence of binary digits where each 1 or 0 is the result of flipping a perfectly fair, unbiased coin. Such a sequence of digits would have the following properties:&lt;br /&gt;
&lt;br /&gt;
* Each digit would have exactly 0.5 probability of being 1 and 0.5 probability of being 0&lt;br /&gt;
* The production of any one digit would be entirely independent of any other digits&lt;br /&gt;
&lt;br /&gt;
Given any arbitrary sequence of binary digits it is possible to examine it using statistical techniques. There are various suites of statistical tests available such as STS (Statistical Test Suite) available from NIST's ''[http://csrc.nist.gov/groups/ST/toolkit/rng/index.html RANDOM NUMBER GENERATION]'' page. This suite provides a number of different tests including:&lt;br /&gt;
&lt;br /&gt;
* The Frequency (Monobit) Test: Checks whether the proportion of 0s and 1s in a given  sequence are approximately as one would expect&lt;br /&gt;
* The Runs Test: Tests whether the number of runs of consecutive identical digits of varying lengths within a given sequence is as expected&lt;br /&gt;
* The Longest Run of Ones in a block: Confirms whether the longest single run of ones within a sequence is as would be expected&lt;br /&gt;
&lt;br /&gt;
Examining random data using the tests above cannot determine whether a data source is truly random or not. However, it '''can''' indicate whether data is '''likely''' to be non-random.&lt;br /&gt;
&lt;br /&gt;
== Seeds ==&lt;br /&gt;
&lt;br /&gt;
Most random number generators require a seed. A seed is a secret, unpredictable sequence of bytes that is transformed and then used to set the initial state of the generator. The seed ensures that each unique instance of a generator produces a unique stream of bits. No two generators should ever produce the same sequence of random numbers, even when faced with Virtual Machine (VM) rollback attacks (which could happen accidentally by a data center operator).&lt;br /&gt;
&lt;br /&gt;
When seeding your generators, you should use at least 256 bits (32 bytes) of material. You can verify the required number of bits by grepping the source files for &amp;lt;tt&amp;gt;#define ENTROPY_NEEDED&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Initialization ===&lt;br /&gt;
&lt;br /&gt;
OpenSSL will attempt to seed the random number generator automatically upon instantiation by calling &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt;.  &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; seeds the random number generator using a system-specific entropy source, which is &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt; on UNIX-like operating systems, and is a combination of [http://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx &amp;lt;tt&amp;gt;CryptGenRandom&amp;lt;/tt&amp;gt;] and other sources of entropy on Windows.&lt;br /&gt;
&lt;br /&gt;
Be careful when deferring to &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; on some Unix systems because it does not seed the generator. See the code guarded with &amp;lt;tt&amp;gt;OPENSSL_SYS_VXWORKS&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;rand_unix.c&amp;lt;/tt&amp;gt;. Additionally, there can be negative interactions on newer Windows platforms due to the heap walk, so your program will likely experience temporary hangs. See [http://rt.openssl.org/Ticket/Display.html?id=2100&amp;amp;user=guest&amp;amp;pass=guest Bug 2100] for details.&lt;br /&gt;
&lt;br /&gt;
If the random number generator is not properly seeded, then it refuse to deliver random bytes and a &amp;quot;PRNG not seeded error&amp;quot; will occur.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;urandom&amp;lt;/tt&amp;gt; device may lack sufficient entropy for your needs, and you might want to reseed it immediately from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;. On Unix and other operating systems that provide the block device, you can use &amp;lt;tt&amp;gt;[[Manual:RAND_load_file(3)|RAND_load_file]]&amp;lt;/tt&amp;gt; to load directly from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int rc = RAND_load_file(&amp;quot;/dev/random&amp;quot;, 32);&lt;br /&gt;
if(rc != 32) {&lt;br /&gt;
    /* RAND_load_file failed */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reseed ===&lt;br /&gt;
&lt;br /&gt;
The OpenSSL API allows you to provide a seed and refresh the generator's state with reseeds at anytime during the program's execution. Two functions are provided for seeding and reseeding: &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_seed]]&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_add]]&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_seed]]&amp;lt;/tt&amp;gt; accepts a buffer and size; while &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_add]]&amp;lt;/tt&amp;gt; accepts a buffer, size, and entropy estimate in bytes. &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_seed]]&amp;lt;/tt&amp;gt; will call &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_add]]&amp;lt;/tt&amp;gt; assuming 100% entropy.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_seed]]&amp;lt;/tt&amp;gt; is shown below. The function is &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt;, so it &amp;lt;nowiki&amp;gt;[apparently]&amp;lt;/nowiki&amp;gt; cannot fail (or convey failures). Though the example uses the actual number of bytes written to the buffer, the entire buffer can be used to increase entropy with hopes the unused bytes in the buffer has entropy to extract. Even though you can use uninitialized bytes as input, you should not expect any entropy in the uninitialized bytes. Finally, the function &amp;lt;tt&amp;gt;get_random_bytes&amp;lt;/tt&amp;gt; is a placeholder for an application supplied function which gathers random data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[32];&lt;br /&gt;
int written = get_random_bytes(buffer, sizeof(buffer));&lt;br /&gt;
&lt;br /&gt;
RAND_seed(buffer, written);&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_add]]&amp;lt;/tt&amp;gt; is similar to &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_seed]]&amp;lt;/tt&amp;gt; but requires an entropy estimate. The estimate should be the number of full bytes of entropy in the buffer. If you have a 32 byte buffer with about 50% entropy, you should provide 16 as the entropy estimate. &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_add]]&amp;lt;/tt&amp;gt; is also a &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt; function, so it cannot fail (or convey failures). The example also uses the actual number of bytes written to the buffer, but the entire buffer can be used to increase entropy. Note that &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_add]]&amp;lt;/tt&amp;gt; takes a &amp;lt;tt&amp;gt;double&amp;lt;/tt&amp;gt;, so be sure to '''avoid''' integer math. Otherwise, the entropy estimate calculation could result in 0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;char phrase[64];&lt;br /&gt;
int written = get_random_phrase(phrase, sizeof(phrase));&lt;br /&gt;
&lt;br /&gt;
RAND_add(phrase, written, 0.12f * written /* 12% */);&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On Windows machines, you can also use &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_screen]]&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_event]]&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_screen]]&amp;lt;/tt&amp;gt; will mix the contents of the screen into the generator. &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_event]]&amp;lt;/tt&amp;gt; can be used with programs that process [http://msdn.microsoft.com/en-us/library/windows/desktop/ff381405(v=vs.85).aspx Windows Messages]. Both methods should only be used with interactive programs, and not services nor drivers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; can be used to reseed the generator using the system entropy source.&lt;br /&gt;
&lt;br /&gt;
=== Persisting ===&lt;br /&gt;
&lt;br /&gt;
If you are worried about slow starts - or the time it takes to get the random number generator in good working order - you can write out a future seed and use it upon the next program execution. To save the future seed, use the library's &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt; function. When using &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt;, you only need to specify a filename. &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt; returns the number of bytes written or -1 to indicate bytes were written without an appropriate seed (failure).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int written = RAND_save_file(&amp;quot;prng.seed&amp;quot;);&lt;br /&gt;
if(written &amp;lt;= 0)&lt;br /&gt;
    /* RAND_save_file failed */&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At program startup, you can attempt to read the saved seed with &amp;lt;tt&amp;gt;RAND_load_file&amp;lt;/tt&amp;gt;. You can specify the number of bytes to read, or -1 to indicate the entire file should be used. The bytes read are automatically added to the generator. &amp;lt;tt&amp;gt;[[Manual:RAND_load_file(3)|RAND_load_file]]&amp;lt;/tt&amp;gt; returns the number of bytes read.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int read = RAND_load_file(&amp;quot;prng.seed&amp;quot;, -1);&lt;br /&gt;
if(read &amp;lt;= 0)&lt;br /&gt;
    /* RAND_load_file failed */&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If possible, you should use protected storage offered by the operating system. For example, you should avoid writing the file and store the seed in the [http://developer.apple.com/library/ios/#documentation/security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html iOS Keychain], [http://developer.android.com/reference/android/security/KeyChain.html Android KeyChain], or [http://msdn.microsoft.com/en-us/library/ms995355.aspx Windows DPAPI]. When writing the seed to the filesystem, be sure to protect the the seed through the file system's permission scheme (Linux has not realized userland needs help from the kernel when storing secrets).&lt;br /&gt;
&lt;br /&gt;
== Generation ==&lt;br /&gt;
&lt;br /&gt;
After the generator has been seeded and is in good working order, you can extract bytes. You have three functions to extract bytes. First is &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt; and the second is &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_pseudo_bytes]]&amp;lt;/tt&amp;gt;. Both are software based and produce a pseudo-random stream. The third method is hardware based and it reuses &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Software ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt; will fetch cryptographically strong random bytes. Cryptographically strong bytes are suitable for high integrity needs, such as long term key generation. If your generator is using a software algorithm, then the bytes will be pseudo-random (but still cryptographically strong). &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt; returns 1 for success, and 0 otherwise. If you changed the &amp;lt;tt&amp;gt;RAND_METHOD&amp;lt;/tt&amp;gt; and it is not supported, then the function will return -1. In case of error, you can call &amp;lt;tt&amp;gt;[[Manual:ERR_get_error(3)|ERR_get_error]]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[128];&lt;br /&gt;
&lt;br /&gt;
int rc = RAND_bytes(buffer, sizeof(buffer));&lt;br /&gt;
unsigned long err = ERR_get_error();&lt;br /&gt;
&lt;br /&gt;
if(rc != 1) {&lt;br /&gt;
    /* RAND_bytes failed */&lt;br /&gt;
    /* `err` is valid    */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_pseudo_bytes]]&amp;lt;/tt&amp;gt; returns pseudo-random bytes which ''can'' be cryptographically strong. The function returns 1 if the bytes are cryptographically strong, and 0 otherwise. If your application has high integrity requirements, it should ''not'' use &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_pseudo_bytes]]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
When using &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_pseudo_bytes]]&amp;lt;/tt&amp;gt;, both 0 and 1 indicate success. If you change the &amp;lt;tt&amp;gt;RAND_METHOD&amp;lt;/tt&amp;gt; and it is not supported, then the function will return -1. In case of error, you can call &amp;lt;tt&amp;gt;[[Manual:ERR_get_error(3)|ERR_get_error]]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[32];&lt;br /&gt;
&lt;br /&gt;
int rc = RAND_pseudo_bytes(buffer, sizeof(buffer));&lt;br /&gt;
unsigned long err = ERR_get_error();&lt;br /&gt;
&lt;br /&gt;
if(rc != 0 &amp;amp;&amp;amp; rc != 1) {&lt;br /&gt;
    /* RAND_pseudo_bytes failed */&lt;br /&gt;
    /* `err` is valid           */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hardware ===&lt;br /&gt;
&lt;br /&gt;
Hardware random number generators are almost always better to use than a software based generator. Hardware generators are often called True Random Number generators (TRNG) or Non-Deterministic Random Number Generators since they don't rely on the deterministic behavior of executing software instructions. Their bits streams are nearly always indistinguishable from random streams, and their entropy is always nearly 100%.&lt;br /&gt;
&lt;br /&gt;
Some hardware generators are easier to use than other. For example, an [http://www.entropykey.co.uk EntropyKey] will provide a driver that replenishes &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;, so an application does not have to do anything special other than reading from the device. Other generators, such as Intel's [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key], must be integrated into an application. When integrating generators using OpenSSL, you will use the library's &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
To integrate a hardware based random number generator, you should load the apporpriate &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; for the hardware based implementation. Once loaded, set the engine's &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; method as default with &amp;lt;tt&amp;gt;ENGINE_METHOD_RAND&amp;lt;/tt&amp;gt;. After you load the engine and set &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; for the hardware generator, you simply use &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt; as discussed earlier. There are no special steps necessary after the configuration.&lt;br /&gt;
&lt;br /&gt;
If you have OpenSSL 1.0.1 and a machine with [http://semiaccurate.com/2012/04/23/intel-launches-ivy-bridge-amid-crushing-marketing-buzzwords/ 3rd generation Core i5 or i7 processor (Ivy Bridge)], then the Intel [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key Technology] (formerly called Bull Mountain) [[Commercial Product Disclaimer|&amp;lt;nowiki&amp;gt;[disclaimer]&amp;lt;/nowiki&amp;gt;]] is available to you. The hardware generator is accessed through the &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; API and wraps the &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; instruction. According to Intel, [http://software.intel.com/en-us/articles/performance-impact-of-intel-secure-key-on-openssl RDRAND-enabled version of OpenSSL outperformed the non-RDRAND version by an order of magnitude].&lt;br /&gt;
&lt;br /&gt;
To ensure &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt; uses the hardware engine, you must perform three steps:&lt;br /&gt;
&lt;br /&gt;
* load the &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; engine&lt;br /&gt;
* acquire a handle to the engine&lt;br /&gt;
* set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; to the engine&lt;br /&gt;
&lt;br /&gt;
The code below shows you how to load the Intel random number generator engine and set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt;. The code is available for download at [[Media:test-rdrand.c|test-rdrand.c]]. While you can call &amp;lt;tt&amp;gt;[[Manual:engine(3)|ENGINE_load_builtin_engines]]&amp;lt;/tt&amp;gt; to make all engines available, the code below focuses on the one engine of interest and loads it via &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt;. Before the call to &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt;, be sure to call &amp;lt;tt&amp;gt;OPENSSL_cpuid_setup&amp;lt;/tt&amp;gt; to load the proper CPU capabilities. See [[Manual:Engine(3)|OpenSSL's engine(3)]] for more details on engines, their loading, and operation.&lt;br /&gt;
&lt;br /&gt;
Displaying the error code in hexadecimal gives you an error that is easily consumed by &amp;lt;tt&amp;gt;openssl errstr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; 1    unsigned long err = 0;&lt;br /&gt;
 2    int rc = 0;&lt;br /&gt;
 3&lt;br /&gt;
 4    OPENSSL_cpuid_setup();&lt;br /&gt;
 5    ENGINE_load_rdrand();&lt;br /&gt;
 6&lt;br /&gt;
 7    ENGINE* eng = ENGINE_by_id(&amp;quot;rdrand&amp;quot;);&lt;br /&gt;
 8    err = ERR_get_error();&lt;br /&gt;
 9&lt;br /&gt;
10    if(NULL == eng) {&lt;br /&gt;
11        fprintf(stderr, &amp;quot;ENGINE_load_rdrand failed, err = 0x%lx\n&amp;quot;, err);&lt;br /&gt;
12        abort(); /* failed */&lt;br /&gt;
13    }&lt;br /&gt;
14&lt;br /&gt;
15    rc = ENGINE_init(eng);&lt;br /&gt;
16    err = ERR_get_error();&lt;br /&gt;
17&lt;br /&gt;
18    if(0 == rc) {&lt;br /&gt;
19        fprintf(stderr, &amp;quot;ENGINE_init failed, err = 0x%lx\n&amp;quot;, err);&lt;br /&gt;
20        abort(); /* failed */&lt;br /&gt;
21    }&lt;br /&gt;
22  &lt;br /&gt;
23    rc = ENGINE_set_default(eng, ENGINE_METHOD_RAND);&lt;br /&gt;
24    err = ERR_get_error();&lt;br /&gt;
25&lt;br /&gt;
26    if(0 == rc) {&lt;br /&gt;
27        fprintf(stderr, &amp;quot;ENGINE_set_default failed, err = 0x%lx\n&amp;quot;, err);&lt;br /&gt;
28        abort(); /* failed */&lt;br /&gt;
29    }&lt;br /&gt;
30&lt;br /&gt;
31    /* OK to proceed */&lt;br /&gt;
32&lt;br /&gt;
33    ...&lt;br /&gt;
34    ENGINE_finish(eng);&lt;br /&gt;
35    ENGINE_free(eng);&lt;br /&gt;
36    ENGINE_cleanup();&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you hardware does not support the Intel generator, you will receive a &amp;lt;tt&amp;gt;NULL&amp;lt;/tt&amp;gt; pointer at line 7 and encounter error 0x2606c043 at line 8. The error can then be fed to &amp;lt;tt&amp;gt;openssl errstr&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ./test-rdrand.exe&lt;br /&gt;
...&lt;br /&gt;
ENGINE_load_rdrand failed, err = 0x2606c043&lt;br /&gt;
$ openssl errstr 0x2606c043&lt;br /&gt;
error:2606C043:engine routines:ENGINE_FREE_UTIL:passed a null parameter&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:test-rdrand.png|thumb|250px|right|Verifying rdrand code path]]  Line 13 attempts to set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; to that provided by the engine using &amp;lt;tt&amp;gt;[[Manual:engine(3)|ENGINE_set_default]]&amp;lt;/tt&amp;gt; with &amp;lt;tt&amp;gt;ENGINE_METHOD_RAND&amp;lt;/tt&amp;gt;. Upon success, OpenSSL will internally use &amp;lt;tt&amp;gt;OPENSSL_ia32_rdrand&amp;lt;/tt&amp;gt; for random number generation. To verify code correctness, simply set a breakpoint on the function and wait for the debugger to snap as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The 0x2606c043 error is actually caused by &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt;. The function will verify the capabilities of the hardware and load the generator's engine if available. &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt; is a &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt; function, so it cannot fail or cannot convey failures (which we know is incorrect from a test run). The source code can be found in &amp;lt;tt&amp;gt;eng_rdrand.c&amp;lt;/tt&amp;gt; and is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void ENGINE_load_rdrand (void)&lt;br /&gt;
{&lt;br /&gt;
    extern unsigned int OPENSSL_ia32cap_P[];&lt;br /&gt;
&lt;br /&gt;
    if (OPENSSL_ia32cap_P[1] &amp;amp; (1&amp;lt;&amp;lt;(62-32)))&lt;br /&gt;
    {&lt;br /&gt;
        ENGINE *toadd = ENGINE_rdrand();&lt;br /&gt;
        if(!toadd) return;&lt;br /&gt;
        ENGINE_add(toadd);&lt;br /&gt;
        ENGINE_free(toadd);&lt;br /&gt;
        ERR_clear_error();&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A patch is available to provide &amp;lt;tt&amp;gt;ENGINE_R_NO_SUCH_ENGINE&amp;lt;/tt&amp;gt; error code for non-RdRand CPUs. See ''[https://rt.openssl.org/Ticket/Display.html?id=3143 &amp;lt;nowiki&amp;gt;[openssl.org #3143]: ENGINE_load_rdrand sane failure code&amp;lt;/nowiki&amp;gt;]'' for details.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ./test-rdrand.exe&lt;br /&gt;
...&lt;br /&gt;
ENGINE_load_rdrand failed, err = 0x26077074&lt;br /&gt;
$ openssl errstr 0x26077074&lt;br /&gt;
error:26077074:engine routines:ENGINE_init:no such engine&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to [http://software.intel.com/en-us/articles/performance-impact-of-intel-secure-key-on-openssl Intel documentation], the random number generator does not need to be seeded via the &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_seed]]&amp;lt;/tt&amp;gt; function because the generator is self-seeding. For optimal performance, code that is aware of the underlying random engine can forgo gathering entropy.&lt;br /&gt;
&lt;br /&gt;
Additionally (or more importantly), the following will not cause a crash when using the hardware random number generator (and it fails silently so all looks good from outside the fishbowl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/* Bad - don't do this in production */&lt;br /&gt;
byte seed[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };&lt;br /&gt;
RAND_seed(seed, sizeof(seed));&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, you can test if your Mac OS X system has &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; available with the following (thanks to Dave Zarzycki):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$ sysctl hw.optional.rdrand&lt;br /&gt;
hw.optional.rdrand: 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On Linux, you can &amp;lt;tt&amp;gt;cat&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;cpuinfo&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ cat /proc/cpuinfo | grep -i rdrand&lt;br /&gt;
rdrand	: 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
&lt;br /&gt;
Two miscellaneous items remaining are generator cleanup  and status. &amp;lt;tt&amp;gt;[[Manual:RAND_cleanup(3)|RAND_cleanup]]&amp;lt;/tt&amp;gt; securely erases the memory used by the random number generator.&lt;br /&gt;
&lt;br /&gt;
You can query the generator's state with &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_status]]&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_status]]&amp;lt;/tt&amp;gt; returns 1 if the generator is in good working order. If your generator is not in good working order, you should reseed it with at least 256 bits (32 bytes) of entropy. The function [http://www.mail-archive.com/openssl-dev@openssl.org/msg04212.html purposefully hides the number of bytes needed] for the reseed operation.&lt;br /&gt;
&lt;br /&gt;
On Android, take care to specify &amp;lt;tt&amp;gt;-mfloat-abi=softfp&amp;lt;/tt&amp;gt; when building the library for use via JNI. If you specify &amp;lt;tt&amp;gt;-mfloat-abi=hard&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;-mhard-float&amp;lt;/tt&amp;gt; (even if the hardware support a floating point unit), then the entropy estimate passed to &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; will always be 0.0f. See [https://groups.google.com/d/msg/android-ndk/NbUq9FDDZOo/TJJsAS6nM7wJ Hard-float and JNI] for details.&lt;br /&gt;
&lt;br /&gt;
By default, OpenSSL will use the &amp;lt;tt&amp;gt;RDRANG&amp;lt;/tt&amp;gt; engine to generate random numbers if the hardware is available. The behavior has been changed, but the change is only available through git at the moment. If you are concerned with &amp;lt;tt&amp;gt;RDRANG&amp;lt;/tt&amp;gt; tampering, then see the discussion of [[Library_Initialization#ENGINEs_and_RDRAND | ENGINEs and RDRAND]].&lt;br /&gt;
&lt;br /&gt;
== FIPS Mode ==&lt;br /&gt;
&lt;br /&gt;
FIPS mode is a special mode of operation which specifies the library should operate according to the security policies and procedures specified in [http://csrc.nist.gov/publications/fips/fips140-2/fips1402.pdf FIPS 140-2]. The mode requires use of the FIPS Capable OpenSSL library, and must be enabled with a call to &amp;lt;tt&amp;gt;FIPS_mode_set&amp;lt;/tt&amp;gt;. Once in FIPS mode, a ''default DRBG'' is used as specified in [http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf SP800-90].&lt;br /&gt;
&lt;br /&gt;
The default DRBG is 256-bit CTR AES using a derivation function, and is decided by the application and not the library module. In the case of an OpenSSL application it is specified in &amp;lt;tt&amp;gt;rand_lib.c&amp;lt;/tt&amp;gt; via the &amp;lt;tt&amp;gt;OPENSSL_DRBG_DEFAULT_TYPE&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;OPENSSL_DRBG_DEFAULT_FLAGS&amp;lt;/tt&amp;gt; preprocessor macros to allow them to be overridden by local compilation options or at runtime.&lt;br /&gt;
&lt;br /&gt;
To use the FIPS random number generator, simply use &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt; as described earlier. Note that the call to &amp;lt;tt&amp;gt;FIPS_mode_set&amp;lt;/tt&amp;gt; must succeed in order to operate in FIPS 140 mode.&lt;br /&gt;
&lt;br /&gt;
== Thread Safety ==&lt;br /&gt;
&lt;br /&gt;
The random number generators (among other parts of OpenSSL) are not thread safe by default. To ensure thread safety, you must call &amp;lt;tt&amp;gt;[[Manual:Threads(3)|CRYPTO_set_locking_callback]]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Fork Safety ==&lt;br /&gt;
&lt;br /&gt;
OpenSSL's random number generator is not [[Random_fork-safety|fork-safe]], so the issue should be carefully understood and remediated if necessary. See [[Random_fork-safety|Random Fork-Safety]] for details.&lt;br /&gt;
&lt;br /&gt;
[[Category:Expert Review]]&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[EVP]]&lt;br /&gt;
* [[Libcrypto API]]&lt;br /&gt;
* [http://jbp.io/2014/01/16/openssl-rand-api/ Analysis of the OpenSSL random API]&lt;br /&gt;
&lt;br /&gt;
[[Category:Cryptography]]&lt;br /&gt;
[[Category:Crypto API]]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1415</id>
		<title>Random fork-safety</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1415"/>
		<updated>2014-01-22T08:36:52Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: another blog post about Android Zygote problem with OpenSSL PRNG&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;One of the most important issues in the proper cryptographic use of random numbers is that random numbers must not be reused.  Since the UNIX fork() system call duplicates the entire process state, a random number generator which does not take this issue into account will produce the same sequence of random numbers in both the parent and the child (or in multiple children), leading to cryptographic disaster (i. e. people being able to read your communications).&lt;br /&gt;
&lt;br /&gt;
OpenSSL's default random number generator mixes in the PID, which provides a certain degree of fork safety.  However, once the PIDs wrap, new children will start to produce the same random sequence as previous children which had the same PID.  This is unlikely to happen in most common cases, but it is not impossible, which makes the issue even more insidious.&lt;br /&gt;
&lt;br /&gt;
The most comprehensive explanation of this problem is probably this blog post:&lt;br /&gt;
&lt;br /&gt;
* [http://emboss.github.io/blog/2013/08/21/openssl-prng-is-not-really-fork-safe/ OpenSSL PRNG Is Not (Really) Fork-safe - martinboßlet - Aug 21, 2013]&lt;br /&gt;
&lt;br /&gt;
However, since this issue has been &amp;quot;rediscovered&amp;quot; and discussed multiple times, here are some additional links (some are also linked from the above article)&lt;br /&gt;
&lt;br /&gt;
* [http://marc.info/?l=openssl-dev&amp;amp;m=130289811108150&amp;amp;w=2 openssl-dev: recycled pids causes PRNG to repeat - Eric Wong - Apr 15, 2011]&lt;br /&gt;
* [https://bugs.ruby-lang.org/issues/4579 Ruby bug 4579: SecureRandom + OpenSSL may repeat with fork - Apr 15, 2011]&lt;br /&gt;
* [http://www.postgresql.org/message-id/E1UKzBn-0006c2-Cy@gemulon.postgresql.org pgsql: Reset OpenSSL randomness state in each postmaster childprocess - Tom Lane - Mar 27, 2013]&lt;br /&gt;
* [https://plus.google.com/+AndroidDevelopers/posts/YxWzeNQMJS2 Google+ discussion of Android SecureRandom issue, caused by OpenSSL PID wraparound - Aug 14, 2013]&lt;br /&gt;
* [http://jbp.io/2013/08/15/android-securerandom-guess/ Android SecureRandom vulnerability guess - Joseph Birr-Pixton - Aug 15, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-users@openssl.org/msg71749.html openssl-users: DLL hell - Nico Williams - Aug 15, 2013]&lt;br /&gt;
** [http://www.mail-archive.com/openssl-dev@openssl.org/msg32867.html openssl-dev: not fork-safe if pids wrap (continuation of above openssl-users thread) - Aug 22, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33003.html openssl-dev: Patch to increase rng entropy after forking - Ernst-Udo Wallenborn - Sept 18, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33012.html openssl-dev: Mixing time into the pool - Ben Laurie - Sept 21, 2013]&lt;br /&gt;
** [https://github.com/openssl/openssl/commit/3cd8547a2018ada88a4303067a2aa15eadc17f39 commit mentioned in above message]&lt;br /&gt;
* [http://bugs.python.org/issue19227 Python issue 19227: test_multiprocessing_xxx hangs under Gentoo buildbots - Oct 11, 2013]&lt;br /&gt;
&lt;br /&gt;
== Remediations ==&lt;br /&gt;
&lt;br /&gt;
OpenSSL cannot fix the fork-safety problem because its not in a position to do so. However, there are remediations available and they are listed below.&lt;br /&gt;
&lt;br /&gt;
* Don't use &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt;&lt;br /&gt;
* Call &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; after a fork&lt;br /&gt;
* Call &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; after a fork&lt;br /&gt;
* Use a hardware based generator&lt;br /&gt;
* Practice hedging cryptography&lt;br /&gt;
&lt;br /&gt;
The first remediation is to avoid using &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt;. Instead, you can read directly from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;/dev/srandom&amp;lt;/tt&amp;gt;; or use &amp;lt;tt&amp;gt;CryptGenRandom&amp;lt;/tt&amp;gt; on Windows systems. Avoiding &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; is not practical in practice because the library will use it internally.&lt;br /&gt;
&lt;br /&gt;
The second remediation is to call &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; after a fork. Entropy can be obtained from the operating system by reading from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;/dev/srandom&amp;lt;/tt&amp;gt;; or using &amp;lt;tt&amp;gt;CryptGenRandom&amp;lt;/tt&amp;gt; on Windows systems. For mobile devices with an interactive user, you could even add sensor data from the accelerometer, magnetometer and gyroscopes. This is appropriate for most programs, but might have problems in low entropy environments such as mobile devices and headless servers. Additionally, this could have problems in virtualized environments. For details, see [[Random_Numbers|Random Numbers]].&lt;br /&gt;
&lt;br /&gt;
The third remediation is to use &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; after a fork. This is used by OpenSSL to seed the generator on startup. The function always reads from &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt;, so you will have to seed the generator yourself if you want to use &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;/dev/srandom&amp;lt;/tt&amp;gt;. This is appropriate for most programs, and recommended by a number of people familiar with the library. This method has the same potential problems as &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt;. For details, see [[Random_Numbers|Random Numbers]].&lt;br /&gt;
&lt;br /&gt;
The fourth remediation is to use a hardware based generator. This is not always practical because hardware is not always present. Additionally, hardware is not usually auditable so some question its unabridged use in the post-Snowden era. For details, see [[Random_Numbers|Random Numbers]] and [[Manual:Engine(3)|OpenSSL engine(3)]] man page.&lt;br /&gt;
&lt;br /&gt;
The fifth remediation is to practice hedging cryptography. Hedging uses entropy gathered from a peer during key exchange or key agreement to add to the program's internal entropy pool (for example, the random '''R&amp;lt;sub&amp;gt;A&amp;lt;/sub&amp;gt;''' or '''R&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;''' in SSL/TLS). The benefit of hedging is its resilient against fork problems, low entropy environments, and virtual machine playbacks. For details, see [http://static.usenix.org/event/hotos05/final_papers/full_papers/garfinkel/garfinkel.pdf When Virtual is Harder than Real: Resource Allocation Challenges in Virtual Machine Based IT Environments] and [http://www.isoc.org/isoc/conferences/ndss/10/pdf/15.pdf When Good Randomness Goes Bad: Virtual Machine Reset Vulnerabilities and Hedging Deployed Cryptography].&lt;br /&gt;
&lt;br /&gt;
Some of the items above recommend reading from operating system provided facilities, such as &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt;. Since the random number generator should be in good order before the fork, you should be able to add unique information to diversify the generator states between parent and child. Unique information would include the time the call was made and the process identifier. In fact, Ben Laurie [https://github.com/openssl/openssl/commit/3cd8547a2018ada88a4303067a2aa15eadc17f39 pushed a patch to use time] as the uniqueness (unfortunately, it will be years before it gains widespread adoption in the field).&lt;br /&gt;
&lt;br /&gt;
The code below can be used to add unique information to the generator after a fork. The call to &amp;lt;tt&amp;gt;time&amp;lt;/tt&amp;gt; ensures the information is unique if the PIDs wrap. The call to &amp;lt;tt&amp;gt;getpid&amp;lt;/tt&amp;gt; ensures that two forks that occur back-to-back on hardware with low resolution timers are unique. &amp;lt;tt&amp;gt;long[2]&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;long[3]&amp;lt;/tt&amp;gt; attempts to further enhance the generator's state by using random data on the stack (if present).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;long long seed[4];&lt;br /&gt;
seed[0] = (long long)time(NULL);&lt;br /&gt;
seed[1] = (long long)getpid();&lt;br /&gt;
RAND_seed(seed, sizeof(seed));&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1353</id>
		<title>Random fork-safety</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1353"/>
		<updated>2013-11-17T06:10:25Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: added link to python bug about trouble fixing the rng with pthread_atfork&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;One of the most important issues in the proper cryptographic use of random numbers is that random numbers must not be reused.  Since the UNIX fork() system call duplicates the entire process state, a random number generator which does not take this issue into account will produce the same sequence of random numbers in both the parent and the child (or in multiple children), leading to cryptographic disaster (i. e. people being able to read your communications).&lt;br /&gt;
&lt;br /&gt;
OpenSSL's default random number generator mixes in the PID, which provides a certain degree of fork safety.  However, once the PIDs wrap, new children will start to produce the same random sequence as previous children which had the same PID.  This is unlikely to happen in most common cases, but it is not impossible, which makes the issue even more insidious.&lt;br /&gt;
&lt;br /&gt;
The most comprehensive explanation of this problem is probably this blog post:&lt;br /&gt;
&lt;br /&gt;
* [http://emboss.github.io/blog/2013/08/21/openssl-prng-is-not-really-fork-safe/ OpenSSL PRNG Is Not (Really) Fork-safe - martinboßlet - Aug 21, 2013]&lt;br /&gt;
&lt;br /&gt;
However, since this issue has been &amp;quot;rediscovered&amp;quot; and discussed multiple times, here are some additional links (some are also linked from the above article)&lt;br /&gt;
&lt;br /&gt;
* [http://marc.info/?l=openssl-dev&amp;amp;m=130289811108150&amp;amp;w=2 openssl-dev: recycled pids causes PRNG to repeat - Eric Wong - Apr 15, 2011]&lt;br /&gt;
* [https://bugs.ruby-lang.org/issues/4579 Ruby bug 4579: SecureRandom + OpenSSL may repeat with fork - Apr 15, 2011]&lt;br /&gt;
* [http://www.postgresql.org/message-id/E1UKzBn-0006c2-Cy@gemulon.postgresql.org pgsql: Reset OpenSSL randomness state in each postmaster childprocess - Tom Lane - Mar 27, 2013]&lt;br /&gt;
* [https://plus.google.com/+AndroidDevelopers/posts/YxWzeNQMJS2 Google+ discussion of Android SecureRandom issue, caused by OpenSSL PID wraparound - Aug 14, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-users@openssl.org/msg71749.html openssl-users: DLL hell - Nico Williams - Aug 15, 2013]&lt;br /&gt;
** [http://www.mail-archive.com/openssl-dev@openssl.org/msg32867.html openssl-dev: not fork-safe if pids wrap (continuation of above openssl-users thread) - Aug 22, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33003.html openssl-dev: Patch to increase rng entropy after forking - Ernst-Udo Wallenborn - Sept 18, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33012.html openssl-dev: Mixing time into the pool - Ben Laurie - Sept 21, 2013]&lt;br /&gt;
** [https://github.com/openssl/openssl/commit/3cd8547a2018ada88a4303067a2aa15eadc17f39 commit mentioned in above message]&lt;br /&gt;
* [http://bugs.python.org/issue19227 Python issue 19227: test_multiprocessing_xxx hangs under Gentoo buildbots - Oct 11, 2013]&lt;br /&gt;
&lt;br /&gt;
== Remediations ==&lt;br /&gt;
&lt;br /&gt;
OpenSSL cannot fix the fork-safety problem because its not in a position to do so. However, there are remediations available and they are listed below.&lt;br /&gt;
&lt;br /&gt;
* Don't use &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt;&lt;br /&gt;
* Call &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; after a fork&lt;br /&gt;
* Call &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; after a fork&lt;br /&gt;
* Use a hardware based generator&lt;br /&gt;
* Practice hedging cryptography&lt;br /&gt;
&lt;br /&gt;
The first remediation is to avoid using &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt;. Instead, you can read directly from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;/dev/srandom&amp;lt;/tt&amp;gt;; or use &amp;lt;tt&amp;gt;CryptGenRandom&amp;lt;/tt&amp;gt; on Windows systems. Avoiding &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; is not practical in practice because the library will use it internally.&lt;br /&gt;
&lt;br /&gt;
The second remediation is to call &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; after a fork. Entropy can be obtained from the operating system by reading from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;/dev/srandom&amp;lt;/tt&amp;gt;; or using &amp;lt;tt&amp;gt;CryptGenRandom&amp;lt;/tt&amp;gt; on Windows systems. For mobile devices with an interactive user, you could even add sensor data from the accelerometer, magnetometer and gyroscopes. This is appropriate for most programs, but might have problems in low entropy environments such as mobile devices and headless servers. Additionally, this could have problems in virtualized environments. For details, see [[Random_Numbers|Random Numbers]].&lt;br /&gt;
&lt;br /&gt;
The third remediation is to use &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; after a fork. This is used by OpenSSL to seed the generator on startup. The function always reads from &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt;, so you will have to seed the generator yourself if you want to use &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;/dev/srandom&amp;lt;/tt&amp;gt;. This is appropriate for most programs, and recommended by a number of people familiar with the library. This method has the same potential problems as &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt;. For details, see [[Random_Numbers|Random Numbers]].&lt;br /&gt;
&lt;br /&gt;
The fourth remediation is to use a hardware based generator. This is not always practical because hardware is not always present. Additionally, hardware is not usually auditable so some question its unabridged use in the post-Snowden era. For details, see [[Random_Numbers|Random Numbers]] and [[Manual:Engine(3)|OpenSSL engine(3)]] man page.&lt;br /&gt;
&lt;br /&gt;
The fifth remediation is to practice hedging cryptography. Hedging uses entropy gathered from a peer during key exchange or key agreement to add to the program's internal entropy pool (for example, the random '''R&amp;lt;sub&amp;gt;A&amp;lt;/sub&amp;gt;''' or '''R&amp;lt;sub&amp;gt;B&amp;lt;/sub&amp;gt;''' in SSL/TLS). The benefit of hedging is its resilient against fork problems, low entropy environments, and virtual machine playbacks. For details, see [http://static.usenix.org/event/hotos05/final_papers/full_papers/garfinkel/garfinkel.pdf When Virtual is Harder than Real: Resource Allocation Challenges in Virtual Machine Based IT Environments] and [http://www.isoc.org/isoc/conferences/ndss/10/pdf/15.pdf When Good Randomness Goes Bad: Virtual Machine Reset Vulnerabilities and Hedging Deployed Cryptography].&lt;br /&gt;
&lt;br /&gt;
Some of the items above recommend reading from operating system provided facilities, such as &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt;. Since the random number generator should be in good order before the fork, you should be able to add unique information to diversify the generator states between parent and child. Unique information would include the time the call was made and the process identifier. In fact, Ben Laurie [https://github.com/openssl/openssl/commit/3cd8547a2018ada88a4303067a2aa15eadc17f39 pushed a patch to use time] as the uniqueness (unfortunately, it will be years before it gains widespread adoption in the field).&lt;br /&gt;
&lt;br /&gt;
The code below can be used to add unique information to the generator after a fork. The call to &amp;lt;tt&amp;gt;time&amp;lt;/tt&amp;gt; ensures the information is unique if the PIDs wrap. The call to &amp;lt;tt&amp;gt;getpid&amp;lt;/tt&amp;gt; ensures that two forks that occur back-to-back on hardware with low resolution timers are unique. &amp;lt;tt&amp;gt;long[2]&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;long[3]&amp;lt;/tt&amp;gt; attempts to further enhance the generator's state by using random data on the stack (if present).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;long long seed[4];&lt;br /&gt;
seed[0] = (long long)time(NULL);&lt;br /&gt;
seed[1] = (long long)getpid();&lt;br /&gt;
RAND_seed(seed, sizeof(seed));&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Talk:Random_fork-safety&amp;diff=1346</id>
		<title>Talk:Random fork-safety</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Talk:Random_fork-safety&amp;diff=1346"/>
		<updated>2013-11-09T22:30:44Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: &amp;quot;OpenSSL cannot fix the fork-safety problem because its not in a position to do so.&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== &amp;quot;OpenSSL cannot fix the fork-safety problem because its not in a position to do so.&amp;quot; ==&lt;br /&gt;
&lt;br /&gt;
I'm puzzled by that statement.  OpenSSL '''could''' fix the fork-safety issue, and in fact Ben Laurie (among others) has submitted a patch to do so. --[[User:Ppelleti|Ppelleti]] 22:30, 9 November 2013 (UTC)&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1235</id>
		<title>Random fork-safety</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1235"/>
		<updated>2013-10-03T01:23:11Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: add a link to Ben Laurie's commit&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;One of the most important issues in the proper cryptographic use of random numbers is that random numbers must not be reused.  Since the UNIX fork() system call duplicates the entire process state, a random number generator which does not take this issue into account will produce the same sequence of random numbers in both the parent and the child (or in multiple children), leading to cryptographic disaster.  (i. e. people being able to read your communications)&lt;br /&gt;
&lt;br /&gt;
OpenSSL's default random number generator mixes in the PID, which provides a certain degree of fork safety.  However, once the PIDs wrap, new children will start to produce the same random sequence as previous children which had the same PID.  This is unlikely to happen in most common cases, but it is not impossible, which makes the issue even more insidious.&lt;br /&gt;
&lt;br /&gt;
The most comprehensive explanation of this problem is probably this blog post:&lt;br /&gt;
&lt;br /&gt;
* [http://emboss.github.io/blog/2013/08/21/openssl-prng-is-not-really-fork-safe/ OpenSSL PRNG Is Not (Really) Fork-safe - martinboßlet - Aug 21, 2013]&lt;br /&gt;
&lt;br /&gt;
However, since this issue has been &amp;quot;rediscovered&amp;quot; and discussed multiple times, here are some additional links (some are also linked from the above article)&lt;br /&gt;
&lt;br /&gt;
* [http://marc.info/?l=openssl-dev&amp;amp;m=130289811108150&amp;amp;w=2 openssl-dev: recycled pids causes PRNG to repeat - Eric Wong - Apr 15, 2011]&lt;br /&gt;
* [https://bugs.ruby-lang.org/issues/4579 Ruby bug 4579: SecureRandom + OpenSSL may repeat with fork - Apr 15, 2011]&lt;br /&gt;
* [http://www.postgresql.org/message-id/E1UKzBn-0006c2-Cy@gemulon.postgresql.org pgsql: Reset OpenSSL randomness state in each postmaster childprocess - Tom Lane - Mar 27, 2013]&lt;br /&gt;
* [https://plus.google.com/+AndroidDevelopers/posts/YxWzeNQMJS2 Google+ discussion of Android SecureRandom issue, caused by OpenSSL PID wraparound - Aug 14, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-users@openssl.org/msg71749.html openssl-users: DLL hell - Nico Williams - Aug 15, 2013]&lt;br /&gt;
** [http://www.mail-archive.com/openssl-dev@openssl.org/msg32867.html openssl-dev: not fork-safe if pids wrap (continuation of above openssl-users thread) - Aug 22, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33003.html openssl-dev: Patch to increase rng entropy after forking - Ernst-Udo Wallenborn - Sept 18, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33012.html openssl-dev: Mixing time into the pool - Ben Laurie - Sept 21, 2013]&lt;br /&gt;
** [https://github.com/openssl/openssl/commit/3cd8547a2018ada88a4303067a2aa15eadc17f39 commit mentioned in above message]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1234</id>
		<title>Random fork-safety</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1234"/>
		<updated>2013-10-03T00:52:30Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: oops, that typo changes the meaning significantly&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;One of the most important issues in the proper cryptographic use of random numbers is that random numbers must not be reused.  Since the UNIX fork() system call duplicates the entire process state, a random number generator which does not take this issue into account will produce the same sequence of random numbers in both the parent and the child (or in multiple children), leading to cryptographic disaster.  (i. e. people being able to read your communications)&lt;br /&gt;
&lt;br /&gt;
OpenSSL's default random number generator mixes in the PID, which provides a certain degree of fork safety.  However, once the PIDs wrap, new children will start to produce the same random sequence as previous children which had the same PID.  This is unlikely to happen in most common cases, but it is not impossible, which makes the issue even more insidious.&lt;br /&gt;
&lt;br /&gt;
The most comprehensive explanation of this problem is probably this blog post:&lt;br /&gt;
&lt;br /&gt;
* [http://emboss.github.io/blog/2013/08/21/openssl-prng-is-not-really-fork-safe/ OpenSSL PRNG Is Not (Really) Fork-safe - martinboßlet - Aug 21, 2013]&lt;br /&gt;
&lt;br /&gt;
However, since this issue has been &amp;quot;rediscovered&amp;quot; and discussed multiple times, here are some additional links (some are also linked from the above article)&lt;br /&gt;
&lt;br /&gt;
* [http://marc.info/?l=openssl-dev&amp;amp;m=130289811108150&amp;amp;w=2 openssl-dev: recycled pids causes PRNG to repeat - Eric Wong - Apr 15, 2011]&lt;br /&gt;
* [https://bugs.ruby-lang.org/issues/4579 Ruby bug 4579: SecureRandom + OpenSSL may repeat with fork - Apr 15, 2011]&lt;br /&gt;
* [http://www.postgresql.org/message-id/E1UKzBn-0006c2-Cy@gemulon.postgresql.org pgsql: Reset OpenSSL randomness state in each postmaster childprocess - Tom Lane - Mar 27, 2013]&lt;br /&gt;
* [https://plus.google.com/+AndroidDevelopers/posts/YxWzeNQMJS2 Google+ discussion of Android SecureRandom issue, caused by OpenSSL PID wraparound - Aug 14, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-users@openssl.org/msg71749.html openssl-users: DLL hell - Nico Williams - Aug 15, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg32867.html openssl-dev: not fork-safe if pids wrap (continuation of above openssl-users thread) - Aug 22, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33003.html openssl-dev: Patch to increase rng entropy after forking - Ernst-Udo Wallenborn - Sept 18, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33012.html openssl-dev: Mixing time into the pool - Ben Laurie - Sept 21, 2013]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1233</id>
		<title>Random fork-safety</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1233"/>
		<updated>2013-10-03T00:50:23Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: more links&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;One of the most important issues in the proper cryptographic use of random numbers is that random numbers must not be used.  Since the UNIX fork() system call duplicates the entire process state, a random number generator which does not take this issue into account will produce the same sequence of random numbers in both the parent and the child (or in multiple children), leading to cryptographic disaster.  (i. e. people being able to read your communications)&lt;br /&gt;
&lt;br /&gt;
OpenSSL's default random number generator mixes in the PID, which provides a certain degree of fork safety.  However, once the PIDs wrap, new children will start to produce the same random sequence as previous children which had the same PID.  This is unlikely to happen in most common cases, but it is not impossible, which makes the issue even more insidious.&lt;br /&gt;
&lt;br /&gt;
The most comprehensive explanation of this problem is probably this blog post:&lt;br /&gt;
&lt;br /&gt;
* [http://emboss.github.io/blog/2013/08/21/openssl-prng-is-not-really-fork-safe/ OpenSSL PRNG Is Not (Really) Fork-safe - martinboßlet - Aug 21, 2013]&lt;br /&gt;
&lt;br /&gt;
However, since this issue has been &amp;quot;rediscovered&amp;quot; and discussed multiple times, here are some additional links (some are also linked from the above article)&lt;br /&gt;
&lt;br /&gt;
* [http://marc.info/?l=openssl-dev&amp;amp;m=130289811108150&amp;amp;w=2 openssl-dev: recycled pids causes PRNG to repeat - Eric Wong - Apr 15, 2011]&lt;br /&gt;
* [https://bugs.ruby-lang.org/issues/4579 Ruby bug 4579: SecureRandom + OpenSSL may repeat with fork - Apr 15, 2011]&lt;br /&gt;
* [http://www.postgresql.org/message-id/E1UKzBn-0006c2-Cy@gemulon.postgresql.org pgsql: Reset OpenSSL randomness state in each postmaster childprocess - Tom Lane - Mar 27, 2013]&lt;br /&gt;
* [https://plus.google.com/+AndroidDevelopers/posts/YxWzeNQMJS2 Google+ discussion of Android SecureRandom issue, caused by OpenSSL PID wraparound - Aug 14, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-users@openssl.org/msg71749.html openssl-users: DLL hell - Nico Williams - Aug 15, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg32867.html openssl-dev: not fork-safe if pids wrap (continuation of above openssl-users thread) - Aug 22, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33003.html openssl-dev: Patch to increase rng entropy after forking - Ernst-Udo Wallenborn - Sept 18, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33012.html openssl-dev: Mixing time into the pool - Ben Laurie - Sept 21, 2013]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1232</id>
		<title>Random fork-safety</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1232"/>
		<updated>2013-10-03T00:34:33Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: add more links&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;One of the most important issues in the proper cryptographic use of random numbers is that random numbers must not be used.  Since the UNIX fork() system call duplicates the entire process state, a random number generator which does not take this issue into account will produce the same sequence of random numbers in both the parent and the child (or in multiple children), leading to cryptographic disaster.  (i. e. people being able to read your communications)&lt;br /&gt;
&lt;br /&gt;
OpenSSL's default random number generator mixes in the PID, which provides a certain degree of fork safety.  However, once the PIDs wrap, new children will start to produce the same random sequence as previous children which had the same PID.  This is unlikely to happen in most common cases, but it is not impossible, which makes the issue even more insidious.&lt;br /&gt;
&lt;br /&gt;
The most comprehensive explanation of this problem is probably this blog post:&lt;br /&gt;
&lt;br /&gt;
* [http://emboss.github.io/blog/2013/08/21/openssl-prng-is-not-really-fork-safe/ OpenSSL PRNG Is Not (Really) Fork-safe - martinboßlet - Aug 21, 2013]&lt;br /&gt;
&lt;br /&gt;
However, since this issue has been &amp;quot;rediscovered&amp;quot; and discussed multiple times, here are some additional links:&lt;br /&gt;
&lt;br /&gt;
* [http://marc.info/?l=openssl-dev&amp;amp;m=130289811108150&amp;amp;w=2 openssl-dev: recycled pids causes PRNG to repeat - Eric Wong - Apr 15, 2011]&lt;br /&gt;
* [https://plus.google.com/+AndroidDevelopers/posts/YxWzeNQMJS2 Google+ discussion of Android SecureRandom issue, caused by OpenSSL PID wraparound - Aug 14, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-users@openssl.org/msg71749.html openssl-users: DLL hell - Nico Williams - Aug 15, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg32867.html openssl-dev: not fork-safe if pids wrap (continuation of above openssl-users thread) - Aug 22, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33003.html openssl-dev: Patch to increase rng entropy after forking - Ernst-Udo Wallenborn - Sept 18, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33012.html openssl-dev: Mixing time into the pool - Ben Laurie - Sept 21, 2013]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1231</id>
		<title>Random fork-safety</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Random_fork-safety&amp;diff=1231"/>
		<updated>2013-10-03T00:24:40Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: new page about fork safety&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;One of the most important issues in the proper cryptographic use of random numbers is that random numbers must not be used.  Since the UNIX fork() system call duplicates the entire process state, a random number generator which does not take this issue into account will produce the same sequence of random numbers in both the parent and the child (or in multiple children), leading to cryptographic disaster.  (i. e. people being able to read your communications)&lt;br /&gt;
&lt;br /&gt;
OpenSSL's default random number generator mixes in the PID, which provides a certain degree of fork safety.  However, once the PIDs wrap, new children will start to produce the same random sequence as previous children which had the same PID.  This is unlikely to happen in most common cases, but it is not impossible, which makes the issue even more insidious.&lt;br /&gt;
&lt;br /&gt;
The most comprehensive explanation of this problem is probably this blog post:&lt;br /&gt;
&lt;br /&gt;
* [http://emboss.github.io/blog/2013/08/21/openssl-prng-is-not-really-fork-safe/ OpenSSL PRNG Is Not (Really) Fork-safe - martinboßlet - Aug 21, 2013]&lt;br /&gt;
&lt;br /&gt;
However, since this issue has been &amp;quot;rediscovered&amp;quot; and discussed multiple times, here are some addition links:&lt;br /&gt;
&lt;br /&gt;
* [http://marc.info/?l=openssl-dev&amp;amp;m=130289811108150&amp;amp;w=2 openssl-dev: recycled pids causes PRNG to repeat - Eric Wong - Apr 15, 2011]&lt;br /&gt;
* [https://plus.google.com/+AndroidDevelopers/posts/YxWzeNQMJS2 Google+ discussion of Android SecureRandom issue, caused by OpenSSL PID wraparound - Aug 14, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33003.html openssl-dev: Patch to increase rng entropy after forking - Ernst-Udo Wallenborn - Sept 18, 2013]&lt;br /&gt;
* [http://www.mail-archive.com/openssl-dev@openssl.org/msg33012.html openssl-dev: Mixing time into the pool - Ben Laurie - Sept 21, 2013]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Random_Numbers&amp;diff=1230</id>
		<title>Random Numbers</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Random_Numbers&amp;diff=1230"/>
		<updated>2013-10-02T23:59:11Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: link to a new page about fork safety&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Random Numbers]] are a cryptographic primitive and cornerstone to nearly all cryptographic systems. They are used in almost all areas of cryptography, from key agreement and transport to session keys for bulk encryption. A quality source of random bits and proper use of OpenSSL APIs will help ensure your program is cryptographically sound. On the other hand, a poor source of randomness or incorrect library usage could result in loss of security. This article will help you use random number generation routines correctly when programming with the OpenSSL library.&lt;br /&gt;
&lt;br /&gt;
OpenSSL provides a number of software based random number generators based on a variety of sources.  A software based random number generator creates random numbers by executing a software algorithm. There are a number of algorithms specified by a number of standard bodies including NIST, ANSI X9 committee (X9.17 and X9.31) and XXX. In addition, the library can use custom hardware if the hardware has an &amp;lt;tt&amp;gt;ENIGNE&amp;lt;/tt&amp;gt; interface. &lt;br /&gt;
&lt;br /&gt;
Good random numbers are notoriously hard to produce from deterministic processes such as a computer executing instructions. A number of cryptographic attacks have been developed because they are so hard to acquire. Especially vulnerable are headless servers, embedded devices, and mobile devices, and you may have to take extra steps to ensure an adequate supply of entropy is available. The extra steps could include ''Hedging'' on a headless server or embedded device, and ''Finger Painting'' on a mobile device. For recent attacks on low entropy devices (such as headless servers and mobile devices), see for example, ''[http://pages.cs.wisc.edu/~rist/papers/sslhedge.html When Good Randomness Goes Bad: Virtual Machine Reset Vulnerabilities and Hedging Deployed Cryptography ]'', ''[https://factorable.net/paper.html Mining Your Ps and Qs: Detection of Widespread Weak Keys in Network Devices]'', and ''[http://www.csoonline.com/article/723229/traffic-sensor-flaw-that-could-allow-driver-tracking-fixed Traffic sensor flaw that could allow driver tracking fixed]''.&lt;br /&gt;
&lt;br /&gt;
== Entropy ==&lt;br /&gt;
&lt;br /&gt;
Entropy is the measure of &amp;quot;randomness&amp;quot; in a sequence of bits. Different sources have different entropy. For example, a physical process in nature may have 100% entropy which appears purely random. On the other hand, the written English language provides about 3 bits/byte (or character) which is at most 38%. Some estimates have shown English characters provide only 1 bit/byte (or 12%). Other sources used as a random stream will have different estimates of entropy, and you will have to determine the quality.&lt;br /&gt;
&lt;br /&gt;
Random number generators require quality entropy for input (a ''seed'', discussed below) and must produce quality output (''quod vide''). When using OpenSSL's APIs, you will be asked to estimate entropy when seeding or reseeding (input). When estimating entropy you should error on the low side to ensure proper fitness of the generator. When receiving bytes, you will receive a code indicating the success/failure of the operation and quality of the bytes (output).&lt;br /&gt;
&lt;br /&gt;
=== Sources ===&lt;br /&gt;
&lt;br /&gt;
Sometimes the operating system offers block access to hardware random number generators via &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; can be a low volume device, and could potentially block. For example, the [http://www.portwell.com.tw/download/sbc/catalog/Intel_80802_FWH.pdf Intel 82802 Firmware Hub] used with the and i840 chipset produces one byte of data in its register. At other times, &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; can be a high volume device, such as Intel's [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key Technology]. In virtualized environments, &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; might actually be a [http://wiki.qemu.org/Features-Done/VirtIORNG VirtIO RNG].&lt;br /&gt;
&lt;br /&gt;
Entropy is important for a healthy program, and you should investigate hardware modules to help acquire it, especially if poor entropy or entropy depletion are a concern. There are a number of inexpensive and high quality hardware modules on the market, including a $40UK [http://www.entropykey.co.uk EntropyKey]. There are also a number of high quality and high priced hardware modules and accelerators.&lt;br /&gt;
&lt;br /&gt;
If you lack &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt; and cannot procure a hardware random number generator, you can also consider an alternate entropy gather such as the [http://egd.sourceforge.net Entropy Gathering Daemon (EGD)]. EGD is an userspace substitute for &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;. OpenSSL provides native support for EGD via &amp;lt;tt&amp;gt;[[Manual:RAND_egd(3)|RAND_egd]]&amp;lt;/tt&amp;gt; to connect to the Unix domain socket, and &amp;lt;tt&amp;gt;[[Manual:RAND_egd(3)|RAND_egd_bytes]]&amp;lt;/tt&amp;gt; to extract bytes from the daemon.&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
It is not possible to assess whether a source of randomness is truly random by merely examining its bits. An &amp;quot;ideal&amp;quot; source of truly random data can be thought of as a sequence of binary digits where each 1 or 0 is the result of flipping a perfectly fair, unbiased coin. Such a sequence of digits would have the following properties:&lt;br /&gt;
&lt;br /&gt;
* Each digit would have exactly 0.5 probability of being 1 and 0.5 probability of being 0&lt;br /&gt;
* The production of any one digit would be entirely independent of any other digits&lt;br /&gt;
&lt;br /&gt;
Given any arbitrary sequence of binary digits it is possible to examine it using statistical techniques. There are various suites of statistical tests available such as STS (Statistical Test Suite) available from NIST's ''[http://csrc.nist.gov/groups/ST/toolkit/rng/index.html RANDOM NUMBER GENERATION]'' page. This suite provides a number of different tests including:&lt;br /&gt;
&lt;br /&gt;
* The Frequency (Monobit) Test: Checks whether the proportion of 0s and 1s in a given  sequence are approximately as one would expect&lt;br /&gt;
* The Runs Test: Tests whether the number of runs of consecutive identical digits of varying lengths within a given sequence is as expected&lt;br /&gt;
* The Longest Run of Ones in a block: Confirms whether the longest single run of ones within a sequence is as would be expected&lt;br /&gt;
&lt;br /&gt;
Examining random data using the tests above cannot determine whether a data source is truly random or not. However, it '''can''' indicate whether data is '''likely''' to be non-random.&lt;br /&gt;
&lt;br /&gt;
== Seeds ==&lt;br /&gt;
&lt;br /&gt;
Most random number generators require a seed. A seed is a secret, unpredictable sequence of bytes that is transformed and then used to set the initial state of the generator. The seed ensures that each unique instance of a generator produces a unique stream of bits. No two generators should ever produce the same sequence of random numbers, even when faced with Virtual Machine (VM) rollback attacks (which could happen accidentally by a data center operator).&lt;br /&gt;
&lt;br /&gt;
When seeding your generators, you should use at least 256 bits (32 bytes) of material. You can verify the required number of bits by grepping the source files for &amp;lt;tt&amp;gt;#define ENTROPY_NEEDED&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Initialization ===&lt;br /&gt;
&lt;br /&gt;
OpenSSL will attempt to seed the random number generator automatically upon instantiation by calling &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt;.  &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; seeds the random number generator using a system-specific entropy source, which is &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt; on UNIX-like operating systems, and is a combination of [http://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx &amp;lt;tt&amp;gt;CryptGenRandom&amp;lt;/tt&amp;gt;] and other sources of entropy on Windows. Be careful when deferring to &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; on Windows systems. Negative interactions due to a heap walk on Windows 7 means your program will likely experience temporary hangs. See [http://rt.openssl.org/Ticket/Display.html?id=2100&amp;amp;user=guest&amp;amp;pass=guest Bug 2100] for details.&lt;br /&gt;
&lt;br /&gt;
If not properly seeded the random number generator will refuse to deliver random bytes and a &amp;quot;PRNG not seeded error&amp;quot; will occur.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;urandom&amp;lt;/tt&amp;gt; device may lack sufficient entropy for your needs, and you might want to reseed it immediately from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;. On Unix and other operating systems that provide the block device, you can use &amp;lt;tt&amp;gt;[[Manual:RAND_load_file(3)|RAND_load_file]]&amp;lt;/tt&amp;gt; to load directly from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int rc = RAND_load_file(&amp;quot;/dev/random&amp;quot;, 32);&lt;br /&gt;
if(rc != 32) {&lt;br /&gt;
    /* RAND_load_file failed */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reseed ===&lt;br /&gt;
&lt;br /&gt;
The OpenSSL API allows you to provide a seed and refresh the generator's state with reseeds at anytime during the program's execution. Two functions are provided for seeding and reseeding: &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_seed]]&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_add]]&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_seed]]&amp;lt;/tt&amp;gt; accepts a buffer and size; while &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_add]]&amp;lt;/tt&amp;gt; accepts a buffer, size, and entropy estimate in bytes. &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_seed]]&amp;lt;/tt&amp;gt; will call &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_add]]&amp;lt;/tt&amp;gt; assuming 100% entropy.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_seed]]&amp;lt;/tt&amp;gt; is shown below. The function is &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt;, so it &amp;lt;nowiki&amp;gt;[apparently]&amp;lt;/nowiki&amp;gt; cannot fail (or convey failures). Though the example uses the actual number of bytes written to the buffer, the entire buffer can be used to increase entropy with hopes the unused bytes in the buffer has entropy to extract. Even though you can use uninitialized bytes as input, you should not expect any entropy in the uninitialized bytes. Finally, the function &amp;lt;tt&amp;gt;get_random_bytes&amp;lt;/tt&amp;gt; is a placeholder for an application supplied function which gathers random data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[32];&lt;br /&gt;
int written = get_random_bytes(buffer, sizeof(buffer));&lt;br /&gt;
&lt;br /&gt;
RAND_seed(buffer, written);&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_add]]&amp;lt;/tt&amp;gt; is similar to &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_seed]]&amp;lt;/tt&amp;gt; but requires an entropy estimate. The estimate should be the number of full bytes of entropy in the buffer. If you have a 32 byte buffer with about 50% entropy, you should provide 16 as the entropy estimate. &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_add]]&amp;lt;/tt&amp;gt; is also a &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt; function, so it cannot fail (or convey failures). The example also uses the actual number of bytes written to the buffer, but the entire buffer can be used to increase entropy. Note that &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_add]]&amp;lt;/tt&amp;gt; takes a &amp;lt;tt&amp;gt;double&amp;lt;/tt&amp;gt;, so be sure to '''avoid''' integer math. Otherwise, the entropy estimate calculation could result in 0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;char phrase[64];&lt;br /&gt;
int written = get_random_phrase(phrase, sizeof(phrase));&lt;br /&gt;
&lt;br /&gt;
RAND_add(phrase, written, 0.12f * written /* 12% */);&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On Windows machines, you can also use &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_screen]]&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_event]]&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_screen]]&amp;lt;/tt&amp;gt; will mix the contents of the screen into the generator. &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_event]]&amp;lt;/tt&amp;gt; can be used with programs that process [http://msdn.microsoft.com/en-us/library/windows/desktop/ff381405(v=vs.85).aspx Windows Messages]. Both methods should only be used with interactive programs, and not services nor drivers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; can be used to reseed the generator using the system entropy source.&lt;br /&gt;
&lt;br /&gt;
=== Persisting ===&lt;br /&gt;
&lt;br /&gt;
If you are worried about slow starts - or the time it takes to get the random number generator in good working order - you can write out a future seed and use it upon the next program execution. To save the future seed, use the library's &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt; function. When using &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt;, you only need to specify a filename. &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt; returns the number of bytes written or -1 to indicate bytes were written without an appropriate seed (failure).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int written = RAND_save_file(&amp;quot;prng.seed&amp;quot;);&lt;br /&gt;
if(written &amp;lt;= 0)&lt;br /&gt;
    /* RAND_save_file failed */&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At program startup, you can attempt to read the saved seed with &amp;lt;tt&amp;gt;RAND_load_file&amp;lt;/tt&amp;gt;. You can specify the number of bytes to read, or -1 to indicate the entire file should be used. The bytes read are automatically added to the generator. &amp;lt;tt&amp;gt;[[Manual:RAND_load_file(3)|RAND_load_file]]&amp;lt;/tt&amp;gt; returns the number of bytes read.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int read = RAND_load_file(&amp;quot;prng.seed&amp;quot;, -1);&lt;br /&gt;
if(read &amp;lt;= 0)&lt;br /&gt;
    /* RAND_load_file failed */&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If possible, you should use protected storage offered by the operating system. For example, you should avoid writing the file and store the seed in the [http://developer.apple.com/library/ios/#documentation/security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html iOS Keychain], [http://developer.android.com/reference/android/security/KeyChain.html Android KeyChain], or [http://msdn.microsoft.com/en-us/library/ms995355.aspx Windows DPAPI]. When writing the seed to the filesystem, be sure to protect the the seed through the file system's permission scheme (Linux has not realized userland needs help from the kernel when storing secrets).&lt;br /&gt;
&lt;br /&gt;
== Generation ==&lt;br /&gt;
&lt;br /&gt;
After the generator has been seeded and is in good working order, you can extract bytes. You have three functions to extract bytes. First is &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt; and the second is &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_pseudo_bytes]]&amp;lt;/tt&amp;gt;. Both are software based and produce a pseudo-random stream. The third method is hardware based and it reuses &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Software ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt; will fetch cryptographically strong random bytes. Cryptographically strong bytes are suitable for high integrity needs, such as long term key generation. If your generator is using a software algorithm, then the bytes will be pseudo-random (but still cryptographically strong). &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt; returns 1 for success, and 0 otherwise. If you changed the &amp;lt;tt&amp;gt;RAND_METHOD&amp;lt;/tt&amp;gt; and it is not supported, then the function will return -1. In case of error, you can call &amp;lt;tt&amp;gt;[[Manual:ERR_get_error(3)|ERR_get_error]]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[128];&lt;br /&gt;
&lt;br /&gt;
int rc = RAND_bytes(buffer, sizeof(buffer));&lt;br /&gt;
unsigned long err = ERR_get_error();&lt;br /&gt;
&lt;br /&gt;
if(rc != 1) {&lt;br /&gt;
    /* RAND_bytes failed */&lt;br /&gt;
    /* `err` is valid    */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_pseudo_bytes]]&amp;lt;/tt&amp;gt; returns pseudo-random bytes which ''can'' be cryptographically strong. The function returns 1 if the bytes are cryptographically strong, and 0 otherwise. If your application has high integrity requirements, it should ''not'' use &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_pseudo_bytes]]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
When using &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_pseudo_bytes]]&amp;lt;/tt&amp;gt;, both 0 and 1 indicate success. If you change the &amp;lt;tt&amp;gt;RAND_METHOD&amp;lt;/tt&amp;gt; and it is not supported, then the function will return -1. In case of error, you can call &amp;lt;tt&amp;gt;[[Manual:ERR_get_error(3)|ERR_get_error]]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[32];&lt;br /&gt;
&lt;br /&gt;
int rc = RAND_pseudo_bytes(buffer, sizeof(buffer));&lt;br /&gt;
unsigned long err = ERR_get_error();&lt;br /&gt;
&lt;br /&gt;
if(rc != 0 &amp;amp;&amp;amp; rc != 1) {&lt;br /&gt;
    /* RAND_pseudo_bytes failed */&lt;br /&gt;
    /* `err` is valid           */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hardware ===&lt;br /&gt;
&lt;br /&gt;
Hardware random number generators are almost always better to use than a software based generator. Hardware generators are often called True Random Number generators (TRNG) or Non-Deterministic Random Number Generators since they don't rely on the deterministic behavior of executing software instructions. Their bits streams are nearly always indistinguishable from random streams, and their entropy is always nearly 100%.&lt;br /&gt;
&lt;br /&gt;
Some hardware generators are easier to use than other. For example, an [http://www.entropykey.co.uk EntropyKey] will provide a driver that replenishes &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;, so an application does not have to do anything special other than reading from the device. Other generators, such as Intel's [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key], must be integrated into an application. When integrating generators using OpenSSL, you will use the library's &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
To integrate a hardware based random number generator, you should load the apporpriate &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; for the hardware based implementation. Once loaded, set the engine's &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; method as default with &amp;lt;tt&amp;gt;ENGINE_METHOD_RAND&amp;lt;/tt&amp;gt;. After you load the engine and set &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; for the hardware generator, you simply use &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt; as discussed earlier. There are no special steps necessary after the configuration.&lt;br /&gt;
&lt;br /&gt;
If you have OpenSSL 1.0.1 and a machine with [http://semiaccurate.com/2012/04/23/intel-launches-ivy-bridge-amid-crushing-marketing-buzzwords/ 3rd generation Core i5 or i7 processor (Ivy Bridge)], then the Intel [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key Technology] (formerly called Bull Mountain) [[Commercial Product Disclaimer|&amp;lt;nowiki&amp;gt;[disclaimer]&amp;lt;/nowiki&amp;gt;]] is available to you. The hardware generator is accessed through the &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; API and wraps the &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; instruction. According to Intel, [http://software.intel.com/en-us/articles/performance-impact-of-intel-secure-key-on-openssl RDRAND-enabled version of OpenSSL outperformed the non-RDRAND version by an order of magnitude].&lt;br /&gt;
&lt;br /&gt;
To ensure &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt; uses the hardware engine, you must perform three steps:&lt;br /&gt;
&lt;br /&gt;
* load the &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; engine&lt;br /&gt;
* acquire a handle to the engine&lt;br /&gt;
* set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; to the engine&lt;br /&gt;
&lt;br /&gt;
The code below shows you how to load the Intel random number generator engine and set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt;. The code is available for download at [[Media:test-rdrand.c|test-rdrand.c]]. While you can call &amp;lt;tt&amp;gt;[[Manual:engine(3)|ENGINE_load_builtin_engines]]&amp;lt;/tt&amp;gt; to make all engines available, the code below focuses on the one engine of interest and loads it via &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt;. See [[Manual:Engine(3)|OpenSSL's engine(3)]] for more details on engines, their loading, and operation.&lt;br /&gt;
&lt;br /&gt;
Displaying the error code in hexadecimal gives you an error that is easily consumed by &amp;lt;tt&amp;gt;openssl errstr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; 1    unsigned long err = 0;&lt;br /&gt;
 2    int rc = 0;&lt;br /&gt;
 3&lt;br /&gt;
 4    ENGINE_load_rdrand();&lt;br /&gt;
 5    ENGINE* eng = ENGINE_by_id(&amp;quot;rdrand&amp;quot;);&lt;br /&gt;
 6    err = ERR_get_error();&lt;br /&gt;
 7&lt;br /&gt;
 8    if(NULL == eng) {&lt;br /&gt;
 9        fprintf(stderr, &amp;quot;ENGINE_load_rdrand failed, err = 0x%lx\n&amp;quot;, err);&lt;br /&gt;
10        abort(); /* failed */&lt;br /&gt;
11    }&lt;br /&gt;
12  &lt;br /&gt;
13    rc = ENGINE_set_default(eng, ENGINE_METHOD_RAND);&lt;br /&gt;
14    err = ERR_get_error();&lt;br /&gt;
15&lt;br /&gt;
16    if(1 != rc) {&lt;br /&gt;
17        fprintf(stderr, &amp;quot;ENGINE_set_default failed, err = 0x%lx\n&amp;quot;, err);&lt;br /&gt;
18        abort(); /* failed */&lt;br /&gt;
19    }&lt;br /&gt;
20&lt;br /&gt;
21    /* OK to proceed */&lt;br /&gt;
22&lt;br /&gt;
23    ...&lt;br /&gt;
24    ENGINE_free(eng);&lt;br /&gt;
25    ENGINE_cleanup();&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you hardware does not support the Intel generator, you will recieve a &amp;lt;tt&amp;gt;NULL&amp;lt;/tt&amp;gt; pointer at line 5 and encounter error 0x2606c043 at line 6. The error can then be fed to &amp;lt;tt&amp;gt;openssl errstr&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ./test-rdrand.exe&lt;br /&gt;
...&lt;br /&gt;
ENGINE_load_rdrand failed, err = 0x2606c043&lt;br /&gt;
$ openssl errstr 0x2606c043&lt;br /&gt;
error:2606C043:engine routines:ENGINE_FREE_UTIL:passed a null parameter&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:test-rdrand.png|thumb|250px|right|Verifying rdrand code path]]  Line 13 attempts to set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; to that provided by the engine using &amp;lt;tt&amp;gt;[[Manual:engine(3)|ENGINE_set_default]]&amp;lt;/tt&amp;gt; with &amp;lt;tt&amp;gt;ENGINE_METHOD_RAND&amp;lt;/tt&amp;gt;. Upon success, OpenSSL will internally use &amp;lt;tt&amp;gt;OPENSSL_ia32_rdrand&amp;lt;/tt&amp;gt; for random number generation. To verify code correctness, simply set a breakpoint on the function and wait for the debugger to snap as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The 0x2606c043 error is actually caused by &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt;. The function will verify the capabilities of the hardware and load the generator's engine if available. &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt; is a &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt; function, so it cannot fail or cannot convey failures (which we know is incorrect from a test run). The source code can be found in &amp;lt;tt&amp;gt;eng_rdrand.c&amp;lt;/tt&amp;gt; and is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void ENGINE_load_rdrand (void)&lt;br /&gt;
{&lt;br /&gt;
    extern unsigned int OPENSSL_ia32cap_P[];&lt;br /&gt;
&lt;br /&gt;
    if (OPENSSL_ia32cap_P[1] &amp;amp; (1&amp;lt;&amp;lt;(62-32)))&lt;br /&gt;
    {&lt;br /&gt;
        ENGINE *toadd = ENGINE_rdrand();&lt;br /&gt;
        if(!toadd) return;&lt;br /&gt;
        ENGINE_add(toadd);&lt;br /&gt;
        ENGINE_free(toadd);&lt;br /&gt;
        ERR_clear_error();&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to [http://software.intel.com/en-us/articles/performance-impact-of-intel-secure-key-on-openssl Intel documentation], the random number generator does not need to be seeded via the &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_seed]]&amp;lt;/tt&amp;gt; function because the generator is self-seeding. For optimal performance, code that is aware of the underlying random engine can forgo gathering entropy.&lt;br /&gt;
&lt;br /&gt;
Additionally (or more importantly), the following will not cause a crash when using the hardware random number generator (and it fails silently so all looks good from outside the fishbowl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/* Bad - don't do this in production */&lt;br /&gt;
byte seed[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };&lt;br /&gt;
RAND_seed(seed, sizeof(seed));&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, you can test if your Mac OS X system has &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; available with the following (thanks to Dave Zarzycki):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$ sysctl hw.optional.rdrand&lt;br /&gt;
hw.optional.rdrand: 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On Linux, you can &amp;lt;tt&amp;gt;cat&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;cpuinfo&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ cat /proc/cpuinfo | grep -i rdrand&lt;br /&gt;
rdrand	: 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
&lt;br /&gt;
Two miscellaneous items remaining are generator cleanup  and status. &amp;lt;tt&amp;gt;[[Manual:RAND_cleanup(3)|RAND_cleanup]]&amp;lt;/tt&amp;gt; securely erases the memory used by the random number generator.&lt;br /&gt;
&lt;br /&gt;
You can query the generator's state with &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_status]]&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;[[Manual:RAND_add(3)|RAND_status]]&amp;lt;/tt&amp;gt; returns 1 if the generator is in good working order. If your generator is not in good working order, you should reseed it with at least 256 bits (32 bytes) of entropy. The function [http://www.mail-archive.com/openssl-dev@openssl.org/msg04212.html purposefully hides the number of bytes needed].&lt;br /&gt;
&lt;br /&gt;
== FIPS Mode ==&lt;br /&gt;
&lt;br /&gt;
FIPS mode is a special mode of operation which specifies the library should operate according to the security policies and procedures specified in [http://csrc.nist.gov/publications/fips/fips140-2/fips1402.pdf FIPS 140-2]. The mode requires use of the FIPS Capable OpenSSL library, and must be enabled with a call to &amp;lt;tt&amp;gt;FIPS_mode_set&amp;lt;/tt&amp;gt;. Once in FIPS mode, a ''default DRBG'' is used as specified in [http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf SP800-90].&lt;br /&gt;
&lt;br /&gt;
The default DRBG is 256-bit CTR AES using a derivation function, and is decided by the application and not the library module. In the case of an OpenSSL application it is specified in &amp;lt;tt&amp;gt;rand_lib.c&amp;lt;/tt&amp;gt; via the &amp;lt;tt&amp;gt;OPENSSL_DRBG_DEFAULT_TYPE&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;OPENSSL_DRBG_DEFAULT_FLAGS&amp;lt;/tt&amp;gt; preprocessor macros to allow them to be overridden by local compilation options or at runtime.&lt;br /&gt;
&lt;br /&gt;
To use the FIPS random number generator, simply use &amp;lt;tt&amp;gt;[[Manual:RAND_bytes(3)|RAND_bytes]]&amp;lt;/tt&amp;gt; as described earlier. Note that the call to &amp;lt;tt&amp;gt;FIPS_mode_set&amp;lt;/tt&amp;gt; must succeed in order to operate in FIPS 140 mode.&lt;br /&gt;
&lt;br /&gt;
== Thread Safety ==&lt;br /&gt;
&lt;br /&gt;
The random number generators (among other parts of OpenSSL) are not thread safe by default. To ensure thread safety, you must call &amp;lt;tt&amp;gt;[[Manual:Threads(3)|CRYPTO_set_locking_callback]]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Fork Safety ==&lt;br /&gt;
&lt;br /&gt;
OpenSSL's random number generator is [[Random fork-safety|not entirely]] fork-safe, so this issue should be carefully understood and worked around if necessary when using OpenSSL in programs which fork.&lt;br /&gt;
&lt;br /&gt;
[[Category:Expert Review]]&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[EVP]]&lt;br /&gt;
* [[Libcrypto API]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Cryptography]]&lt;br /&gt;
[[Category:Crypto API]]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Diffie-Hellman_parameters&amp;diff=1208</id>
		<title>Diffie-Hellman parameters</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Diffie-Hellman_parameters&amp;diff=1208"/>
		<updated>2013-08-27T09:49:44Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: better link for explaining PFS&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;To use [http://blog.ivanristic.com/2013/06/ssl-labs-deploying-forward-secrecy.html perfect forward secrecy] cipher suites, you must set up [[Diffie Hellman|Diffie-Hellman]] parameters ([http://www.mail-archive.com/openssl-users@openssl.org/msg71878.html on the server side]), or the PFS cipher suites will be silently ignored.&lt;br /&gt;
&lt;br /&gt;
== Diffie-Hellman ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;SSL_CTX_set_tmp_dh&amp;lt;/code&amp;gt; is used to set the Diffie-Hellman parameters for a context.  One of the easiest ways to get Diffie-Hellman parameters to use with this function is to generate random Diffie-Hellman parameters with the [https://www.openssl.org/docs/apps/dhparam.html dhparam] command-line program with the &amp;lt;code&amp;gt;-C&amp;lt;/code&amp;gt; option, and embed the resulting code fragment in your program.  For example, &amp;lt;code&amp;gt;openssl dhparam -C 2236&amp;lt;/code&amp;gt; might result in:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifndef HEADER_DH_H&lt;br /&gt;
#include &amp;lt;openssl/dh.h&amp;gt;&lt;br /&gt;
#endif&lt;br /&gt;
DH *get_dh2236()&lt;br /&gt;
	{&lt;br /&gt;
	static unsigned char dh2236_p[]={&lt;br /&gt;
		0x0F,0x52,0xE5,0x24,0xF5,0xFA,0x9D,0xDC,0xC6,0xAB,0xE6,0x04,&lt;br /&gt;
		0xE4,0x20,0x89,0x8A,0xB4,0xBF,0x27,0xB5,0x4A,0x95,0x57,0xA1,&lt;br /&gt;
		0x06,0xE7,0x30,0x73,0x83,0x5E,0xC9,0x23,0x11,0xED,0x42,0x45,&lt;br /&gt;
		0xAC,0x49,0xD3,0xE3,0xF3,0x34,0x73,0xC5,0x7D,0x00,0x3C,0x86,&lt;br /&gt;
		0x63,0x74,0xE0,0x75,0x97,0x84,0x1D,0x0B,0x11,0xDA,0x04,0xD0,&lt;br /&gt;
		0xFE,0x4F,0xB0,0x37,0xDF,0x57,0x22,0x2E,0x96,0x42,0xE0,0x7C,&lt;br /&gt;
		0xD7,0x5E,0x46,0x29,0xAF,0xB1,0xF4,0x81,0xAF,0xFC,0x9A,0xEF,&lt;br /&gt;
		0xFA,0x89,0x9E,0x0A,0xFB,0x16,0xE3,0x8F,0x01,0xA2,0xC8,0xDD,&lt;br /&gt;
		0xB4,0x47,0x12,0xF8,0x29,0x09,0x13,0x6E,0x9D,0xA8,0xF9,0x5D,&lt;br /&gt;
		0x08,0x00,0x3A,0x8C,0xA7,0xFF,0x6C,0xCF,0xE3,0x7C,0x3B,0x6B,&lt;br /&gt;
		0xB4,0x26,0xCC,0xDA,0x89,0x93,0x01,0x73,0xA8,0x55,0x3E,0x5B,&lt;br /&gt;
		0x77,0x25,0x8F,0x27,0xA3,0xF1,0xBF,0x7A,0x73,0x1F,0x85,0x96,&lt;br /&gt;
		0x0C,0x45,0x14,0xC1,0x06,0xB7,0x1C,0x75,0xAA,0x10,0xBC,0x86,&lt;br /&gt;
		0x98,0x75,0x44,0x70,0xD1,0x0F,0x20,0xF4,0xAC,0x4C,0xB3,0x88,&lt;br /&gt;
		0x16,0x1C,0x7E,0xA3,0x27,0xE4,0xAD,0xE1,0xA1,0x85,0x4F,0x1A,&lt;br /&gt;
		0x22,0x0D,0x05,0x42,0x73,0x69,0x45,0xC9,0x2F,0xF7,0xC2,0x48,&lt;br /&gt;
		0xE3,0xCE,0x9D,0x74,0x58,0x53,0xE7,0xA7,0x82,0x18,0xD9,0x3D,&lt;br /&gt;
		0xAF,0xAB,0x40,0x9F,0xAA,0x4C,0x78,0x0A,0xC3,0x24,0x2D,0xDB,&lt;br /&gt;
		0x12,0xA9,0x54,0xE5,0x47,0x87,0xAC,0x52,0xFE,0xE8,0x3D,0x0B,&lt;br /&gt;
		0x56,0xED,0x9C,0x9F,0xFF,0x39,0xE5,0xE5,0xBF,0x62,0x32,0x42,&lt;br /&gt;
		0x08,0xAE,0x6A,0xED,0x88,0x0E,0xB3,0x1A,0x4C,0xD3,0x08,0xE4,&lt;br /&gt;
		0xC4,0xAA,0x2C,0xCC,0xB1,0x37,0xA5,0xC1,0xA9,0x64,0x7E,0xEB,&lt;br /&gt;
		0xF9,0xD3,0xF5,0x15,0x28,0xFE,0x2E,0xE2,0x7F,0xFE,0xD9,0xB9,&lt;br /&gt;
		0x38,0x42,0x57,0x03,&lt;br /&gt;
		};&lt;br /&gt;
	static unsigned char dh2236_g[]={&lt;br /&gt;
		0x02,&lt;br /&gt;
		};&lt;br /&gt;
	DH *dh;&lt;br /&gt;
&lt;br /&gt;
	if ((dh=DH_new()) == NULL) return(NULL);&lt;br /&gt;
	dh-&amp;gt;p=BN_bin2bn(dh2236_p,sizeof(dh2236_p),NULL);&lt;br /&gt;
	dh-&amp;gt;g=BN_bin2bn(dh2236_g,sizeof(dh2236_g),NULL);&lt;br /&gt;
	if ((dh-&amp;gt;p == NULL) || (dh-&amp;gt;g == NULL))&lt;br /&gt;
		{ DH_free(dh); return(NULL); }&lt;br /&gt;
	return(dh);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which can then be used like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DH *dh = get_dh2236 ();&lt;br /&gt;
if (1 != SSL_CTX_set_tmp_dh (ctx, dh))&lt;br /&gt;
  error ();&lt;br /&gt;
DH_free (dh);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Be sure to choose a bit length [http://www.keylength.com/en/3/ appropriate to the security level you want to achieve], although keep in mind that Diffie-Hellman parameters longer than 2236 bits may be [https://bugzilla.mozilla.org/show_bug.cgi?id=636802 incompatible with older versions of NSS].  Even worse, it appears that [http://stackoverflow.com/questions/6851461/java-why-does-ssl-handshake-give-could-not-generate-dh-keypair-exception versions of Java prior to 1.7 don't support Diffie-Hellman parameters longer than 1024 bits]!&lt;br /&gt;
&lt;br /&gt;
== Elliptic curve Diffie-Hellman ==&lt;br /&gt;
&lt;br /&gt;
For [[Elliptic Curve Diffie Hellman|elliptic curve Diffie-Hellman]], you can do something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);&lt;br /&gt;
if (! ecdh)&lt;br /&gt;
  error ();&lt;br /&gt;
if (1 != SSL_CTX_set_tmp_ecdh (ctx, ecdh))&lt;br /&gt;
  error ();&lt;br /&gt;
EC_KEY_free (ecdh);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in OpenSSL 1.0.2 (not yet released, as of Feb 2013) and higher, you should be able to do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SSL_CTX_set_ecdh_auto (ctx, 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, see [[Elliptic Curve Diffie Hellman]] and [[Elliptic Curve Cryptography]].&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Diffie-Hellman_parameters&amp;diff=1207</id>
		<title>Diffie-Hellman parameters</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Diffie-Hellman_parameters&amp;diff=1207"/>
		<updated>2013-08-27T09:43:14Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: link to recent thread reiterating that DH params are server-side only&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;To use [https://en.wikipedia.org/wiki/Perfect_forward_secrecy perfect forward secrecy] cipher suites, you must set up [[Diffie Hellman|Diffie-Hellman]] parameters ([http://www.mail-archive.com/openssl-users@openssl.org/msg71878.html on the server side]), or the PFS cipher suites will be silently ignored.&lt;br /&gt;
&lt;br /&gt;
== Diffie-Hellman ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;SSL_CTX_set_tmp_dh&amp;lt;/code&amp;gt; is used to set the Diffie-Hellman parameters for a context.  One of the easiest ways to get Diffie-Hellman parameters to use with this function is to generate random Diffie-Hellman parameters with the [https://www.openssl.org/docs/apps/dhparam.html dhparam] command-line program with the &amp;lt;code&amp;gt;-C&amp;lt;/code&amp;gt; option, and embed the resulting code fragment in your program.  For example, &amp;lt;code&amp;gt;openssl dhparam -C 2236&amp;lt;/code&amp;gt; might result in:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifndef HEADER_DH_H&lt;br /&gt;
#include &amp;lt;openssl/dh.h&amp;gt;&lt;br /&gt;
#endif&lt;br /&gt;
DH *get_dh2236()&lt;br /&gt;
	{&lt;br /&gt;
	static unsigned char dh2236_p[]={&lt;br /&gt;
		0x0F,0x52,0xE5,0x24,0xF5,0xFA,0x9D,0xDC,0xC6,0xAB,0xE6,0x04,&lt;br /&gt;
		0xE4,0x20,0x89,0x8A,0xB4,0xBF,0x27,0xB5,0x4A,0x95,0x57,0xA1,&lt;br /&gt;
		0x06,0xE7,0x30,0x73,0x83,0x5E,0xC9,0x23,0x11,0xED,0x42,0x45,&lt;br /&gt;
		0xAC,0x49,0xD3,0xE3,0xF3,0x34,0x73,0xC5,0x7D,0x00,0x3C,0x86,&lt;br /&gt;
		0x63,0x74,0xE0,0x75,0x97,0x84,0x1D,0x0B,0x11,0xDA,0x04,0xD0,&lt;br /&gt;
		0xFE,0x4F,0xB0,0x37,0xDF,0x57,0x22,0x2E,0x96,0x42,0xE0,0x7C,&lt;br /&gt;
		0xD7,0x5E,0x46,0x29,0xAF,0xB1,0xF4,0x81,0xAF,0xFC,0x9A,0xEF,&lt;br /&gt;
		0xFA,0x89,0x9E,0x0A,0xFB,0x16,0xE3,0x8F,0x01,0xA2,0xC8,0xDD,&lt;br /&gt;
		0xB4,0x47,0x12,0xF8,0x29,0x09,0x13,0x6E,0x9D,0xA8,0xF9,0x5D,&lt;br /&gt;
		0x08,0x00,0x3A,0x8C,0xA7,0xFF,0x6C,0xCF,0xE3,0x7C,0x3B,0x6B,&lt;br /&gt;
		0xB4,0x26,0xCC,0xDA,0x89,0x93,0x01,0x73,0xA8,0x55,0x3E,0x5B,&lt;br /&gt;
		0x77,0x25,0x8F,0x27,0xA3,0xF1,0xBF,0x7A,0x73,0x1F,0x85,0x96,&lt;br /&gt;
		0x0C,0x45,0x14,0xC1,0x06,0xB7,0x1C,0x75,0xAA,0x10,0xBC,0x86,&lt;br /&gt;
		0x98,0x75,0x44,0x70,0xD1,0x0F,0x20,0xF4,0xAC,0x4C,0xB3,0x88,&lt;br /&gt;
		0x16,0x1C,0x7E,0xA3,0x27,0xE4,0xAD,0xE1,0xA1,0x85,0x4F,0x1A,&lt;br /&gt;
		0x22,0x0D,0x05,0x42,0x73,0x69,0x45,0xC9,0x2F,0xF7,0xC2,0x48,&lt;br /&gt;
		0xE3,0xCE,0x9D,0x74,0x58,0x53,0xE7,0xA7,0x82,0x18,0xD9,0x3D,&lt;br /&gt;
		0xAF,0xAB,0x40,0x9F,0xAA,0x4C,0x78,0x0A,0xC3,0x24,0x2D,0xDB,&lt;br /&gt;
		0x12,0xA9,0x54,0xE5,0x47,0x87,0xAC,0x52,0xFE,0xE8,0x3D,0x0B,&lt;br /&gt;
		0x56,0xED,0x9C,0x9F,0xFF,0x39,0xE5,0xE5,0xBF,0x62,0x32,0x42,&lt;br /&gt;
		0x08,0xAE,0x6A,0xED,0x88,0x0E,0xB3,0x1A,0x4C,0xD3,0x08,0xE4,&lt;br /&gt;
		0xC4,0xAA,0x2C,0xCC,0xB1,0x37,0xA5,0xC1,0xA9,0x64,0x7E,0xEB,&lt;br /&gt;
		0xF9,0xD3,0xF5,0x15,0x28,0xFE,0x2E,0xE2,0x7F,0xFE,0xD9,0xB9,&lt;br /&gt;
		0x38,0x42,0x57,0x03,&lt;br /&gt;
		};&lt;br /&gt;
	static unsigned char dh2236_g[]={&lt;br /&gt;
		0x02,&lt;br /&gt;
		};&lt;br /&gt;
	DH *dh;&lt;br /&gt;
&lt;br /&gt;
	if ((dh=DH_new()) == NULL) return(NULL);&lt;br /&gt;
	dh-&amp;gt;p=BN_bin2bn(dh2236_p,sizeof(dh2236_p),NULL);&lt;br /&gt;
	dh-&amp;gt;g=BN_bin2bn(dh2236_g,sizeof(dh2236_g),NULL);&lt;br /&gt;
	if ((dh-&amp;gt;p == NULL) || (dh-&amp;gt;g == NULL))&lt;br /&gt;
		{ DH_free(dh); return(NULL); }&lt;br /&gt;
	return(dh);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which can then be used like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DH *dh = get_dh2236 ();&lt;br /&gt;
if (1 != SSL_CTX_set_tmp_dh (ctx, dh))&lt;br /&gt;
  error ();&lt;br /&gt;
DH_free (dh);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Be sure to choose a bit length [http://www.keylength.com/en/3/ appropriate to the security level you want to achieve], although keep in mind that Diffie-Hellman parameters longer than 2236 bits may be [https://bugzilla.mozilla.org/show_bug.cgi?id=636802 incompatible with older versions of NSS].  Even worse, it appears that [http://stackoverflow.com/questions/6851461/java-why-does-ssl-handshake-give-could-not-generate-dh-keypair-exception versions of Java prior to 1.7 don't support Diffie-Hellman parameters longer than 1024 bits]!&lt;br /&gt;
&lt;br /&gt;
== Elliptic curve Diffie-Hellman ==&lt;br /&gt;
&lt;br /&gt;
For [[Elliptic Curve Diffie Hellman|elliptic curve Diffie-Hellman]], you can do something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);&lt;br /&gt;
if (! ecdh)&lt;br /&gt;
  error ();&lt;br /&gt;
if (1 != SSL_CTX_set_tmp_ecdh (ctx, ecdh))&lt;br /&gt;
  error ();&lt;br /&gt;
EC_KEY_free (ecdh);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in OpenSSL 1.0.2 (not yet released, as of Feb 2013) and higher, you should be able to do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SSL_CTX_set_ecdh_auto (ctx, 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, see [[Elliptic Curve Diffie Hellman]] and [[Elliptic Curve Cryptography]].&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Diffie-Hellman_parameters&amp;diff=1199</id>
		<title>Diffie-Hellman parameters</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Diffie-Hellman_parameters&amp;diff=1199"/>
		<updated>2013-08-17T04:16:06Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Diffie-Hellman */ ugh, old Java only supports 1024-bit DH&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;To use [https://en.wikipedia.org/wiki/Perfect_forward_secrecy perfect forward secrecy] cipher suites, you must set up [[Diffie Hellman|Diffie-Hellman]] parameters (on the server side), or the PFS cipher suites will be silently ignored.&lt;br /&gt;
&lt;br /&gt;
== Diffie-Hellman ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;SSL_CTX_set_tmp_dh&amp;lt;/code&amp;gt; is used to set the Diffie-Hellman parameters for a context.  One of the easiest ways to get Diffie-Hellman parameters to use with this function is to generate random Diffie-Hellman parameters with the [https://www.openssl.org/docs/apps/dhparam.html dhparam] command-line program with the &amp;lt;code&amp;gt;-C&amp;lt;/code&amp;gt; option, and embed the resulting code fragment in your program.  For example, &amp;lt;code&amp;gt;openssl dhparam -C 2236&amp;lt;/code&amp;gt; might result in:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifndef HEADER_DH_H&lt;br /&gt;
#include &amp;lt;openssl/dh.h&amp;gt;&lt;br /&gt;
#endif&lt;br /&gt;
DH *get_dh2236()&lt;br /&gt;
	{&lt;br /&gt;
	static unsigned char dh2236_p[]={&lt;br /&gt;
		0x0F,0x52,0xE5,0x24,0xF5,0xFA,0x9D,0xDC,0xC6,0xAB,0xE6,0x04,&lt;br /&gt;
		0xE4,0x20,0x89,0x8A,0xB4,0xBF,0x27,0xB5,0x4A,0x95,0x57,0xA1,&lt;br /&gt;
		0x06,0xE7,0x30,0x73,0x83,0x5E,0xC9,0x23,0x11,0xED,0x42,0x45,&lt;br /&gt;
		0xAC,0x49,0xD3,0xE3,0xF3,0x34,0x73,0xC5,0x7D,0x00,0x3C,0x86,&lt;br /&gt;
		0x63,0x74,0xE0,0x75,0x97,0x84,0x1D,0x0B,0x11,0xDA,0x04,0xD0,&lt;br /&gt;
		0xFE,0x4F,0xB0,0x37,0xDF,0x57,0x22,0x2E,0x96,0x42,0xE0,0x7C,&lt;br /&gt;
		0xD7,0x5E,0x46,0x29,0xAF,0xB1,0xF4,0x81,0xAF,0xFC,0x9A,0xEF,&lt;br /&gt;
		0xFA,0x89,0x9E,0x0A,0xFB,0x16,0xE3,0x8F,0x01,0xA2,0xC8,0xDD,&lt;br /&gt;
		0xB4,0x47,0x12,0xF8,0x29,0x09,0x13,0x6E,0x9D,0xA8,0xF9,0x5D,&lt;br /&gt;
		0x08,0x00,0x3A,0x8C,0xA7,0xFF,0x6C,0xCF,0xE3,0x7C,0x3B,0x6B,&lt;br /&gt;
		0xB4,0x26,0xCC,0xDA,0x89,0x93,0x01,0x73,0xA8,0x55,0x3E,0x5B,&lt;br /&gt;
		0x77,0x25,0x8F,0x27,0xA3,0xF1,0xBF,0x7A,0x73,0x1F,0x85,0x96,&lt;br /&gt;
		0x0C,0x45,0x14,0xC1,0x06,0xB7,0x1C,0x75,0xAA,0x10,0xBC,0x86,&lt;br /&gt;
		0x98,0x75,0x44,0x70,0xD1,0x0F,0x20,0xF4,0xAC,0x4C,0xB3,0x88,&lt;br /&gt;
		0x16,0x1C,0x7E,0xA3,0x27,0xE4,0xAD,0xE1,0xA1,0x85,0x4F,0x1A,&lt;br /&gt;
		0x22,0x0D,0x05,0x42,0x73,0x69,0x45,0xC9,0x2F,0xF7,0xC2,0x48,&lt;br /&gt;
		0xE3,0xCE,0x9D,0x74,0x58,0x53,0xE7,0xA7,0x82,0x18,0xD9,0x3D,&lt;br /&gt;
		0xAF,0xAB,0x40,0x9F,0xAA,0x4C,0x78,0x0A,0xC3,0x24,0x2D,0xDB,&lt;br /&gt;
		0x12,0xA9,0x54,0xE5,0x47,0x87,0xAC,0x52,0xFE,0xE8,0x3D,0x0B,&lt;br /&gt;
		0x56,0xED,0x9C,0x9F,0xFF,0x39,0xE5,0xE5,0xBF,0x62,0x32,0x42,&lt;br /&gt;
		0x08,0xAE,0x6A,0xED,0x88,0x0E,0xB3,0x1A,0x4C,0xD3,0x08,0xE4,&lt;br /&gt;
		0xC4,0xAA,0x2C,0xCC,0xB1,0x37,0xA5,0xC1,0xA9,0x64,0x7E,0xEB,&lt;br /&gt;
		0xF9,0xD3,0xF5,0x15,0x28,0xFE,0x2E,0xE2,0x7F,0xFE,0xD9,0xB9,&lt;br /&gt;
		0x38,0x42,0x57,0x03,&lt;br /&gt;
		};&lt;br /&gt;
	static unsigned char dh2236_g[]={&lt;br /&gt;
		0x02,&lt;br /&gt;
		};&lt;br /&gt;
	DH *dh;&lt;br /&gt;
&lt;br /&gt;
	if ((dh=DH_new()) == NULL) return(NULL);&lt;br /&gt;
	dh-&amp;gt;p=BN_bin2bn(dh2236_p,sizeof(dh2236_p),NULL);&lt;br /&gt;
	dh-&amp;gt;g=BN_bin2bn(dh2236_g,sizeof(dh2236_g),NULL);&lt;br /&gt;
	if ((dh-&amp;gt;p == NULL) || (dh-&amp;gt;g == NULL))&lt;br /&gt;
		{ DH_free(dh); return(NULL); }&lt;br /&gt;
	return(dh);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which can then be used like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DH *dh = get_dh2236 ();&lt;br /&gt;
if (1 != SSL_CTX_set_tmp_dh (ctx, dh))&lt;br /&gt;
  error ();&lt;br /&gt;
DH_free (dh);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Be sure to choose a bit length [http://www.keylength.com/en/3/ appropriate to the security level you want to achieve], although keep in mind that Diffie-Hellman parameters longer than 2236 bits may be [https://bugzilla.mozilla.org/show_bug.cgi?id=636802 incompatible with older versions of NSS].  Even worse, it appears that [http://stackoverflow.com/questions/6851461/java-why-does-ssl-handshake-give-could-not-generate-dh-keypair-exception versions of Java prior to 1.7 don't support Diffie-Hellman parameters longer than 1024 bits]!&lt;br /&gt;
&lt;br /&gt;
== Elliptic curve Diffie-Hellman ==&lt;br /&gt;
&lt;br /&gt;
For [[Elliptic Curve Diffie Hellman|elliptic curve Diffie-Hellman]], you can do something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);&lt;br /&gt;
if (! ecdh)&lt;br /&gt;
  error ();&lt;br /&gt;
if (1 != SSL_CTX_set_tmp_ecdh (ctx, ecdh))&lt;br /&gt;
  error ();&lt;br /&gt;
EC_KEY_free (ecdh);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in OpenSSL 1.0.2 (not yet released, as of Feb 2013) and higher, you should be able to do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SSL_CTX_set_ecdh_auto (ctx, 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, see [[Elliptic Curve Diffie Hellman]] and [[Elliptic Curve Cryptography]].&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=1004</id>
		<title>Related Links</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=1004"/>
		<updated>2013-05-24T04:18:38Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: rename &amp;quot;Other&amp;quot; to &amp;quot;Books and documentation&amp;quot; and add a couple more entries to it&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Please feel free to edit this page and add your own OpenSSL-based project or product. This is the one place where otherwise extraneous mention of commercial products is appropriate. Note such mention does not constitute endorsement per our [[Commercial Product Disclaimer]].&lt;br /&gt;
&lt;br /&gt;
== Open Source Cryptographic Libraries ==&lt;br /&gt;
&lt;br /&gt;
Products which are available under some form of Open Source license, and which may also be available under some form of commercial license.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Open Source Cryptographic Libraries&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Library&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://botan.randombit.net/ Botan]  || a C++ cryptography library which includes a TLS implementation&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.bouncycastle.org/ Bouncy Castle] || cryptography API for Java and C#&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.cs.auckland.ac.nz/~pgut001/cryptlib/ Cryptlib] || a security toolkit that allows one to easily add encryption and authentication services to software&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.cryptopp.com/ Crypto++] || a free C++ class library of cryptographic schemes&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.gnutls.org/ GnuTLS] || an LGPL-licensed TLS library with substantial documentation&lt;br /&gt;
|-&lt;br /&gt;
| [http://libtom.org/ LibTomCrypt] || public domain open source crypto library written in C&lt;br /&gt;
|-&lt;br /&gt;
| [http://nacl.cr.yp.to/ NaCl] || NaCl (pronounced &amp;quot;salt&amp;quot;) is a easy-to-use high-speed software library for network communication, encryption, decryption, and signatures&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.mozilla.org/projects/security/pki/nss/ NSS] || a set of libraries designed to support cross-platform development of security-enabled client and server applications&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Open Source Products Using OpenSSL ==&lt;br /&gt;
&lt;br /&gt;
Products which are available under some form of Open Source License, and which may also be available under some form of commercial license.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Open Source Products Using OpenSSL&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Product&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://libevent.org/ libevent] || an event driven library which can [http://www.wangafu.net/~nickm/libevent-book/Ref6a_advanced_bufferevents.html#_bufferevents_and_ssl optionally use OpenSSL]&lt;br /&gt;
|-&lt;br /&gt;
| [http://en.wikipedia.org/wiki/Mod_ssl mod_ssl] || SSL/TLS module for the [http://en.wikipedia.org/wiki/Apache_HTTP_Server Apache HTTP Server]&lt;br /&gt;
|-&lt;br /&gt;
|[https://www.stunnel.org/index.html Stunnel] || an SSL encryption wrapper between remote client and local (inetd-startable) or remote server&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Closed Source Cryptographic Libraries ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Closed Source Cryptographic Libraries&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Library&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.example.com No Entry]  || This is a placeholder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Closed Source Products Using OpenSSL ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Closed Source Products Using OpenSSL&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Product&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [http://www.example.com No Entry]  || This is a placeholder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Books and Documentation == &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Books and Documentation&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; width=&amp;quot;150px&amp;quot; | Title&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; class=&amp;quot;unsortable&amp;quot; | Description&lt;br /&gt;
|-&lt;br /&gt;
| [https://en.wikipedia.org/wiki/Comparison_of_TLS_implementations Comparison of TLS implementations]  || Wikipedia article comparing various TLS libraries&lt;br /&gt;
|-&lt;br /&gt;
| [http://blog.ivanristic.com/2013/05/announcing-bulletproof-ssl-tls-and-pki.html OpenSSL Cookbook]  || Free chapter from upcoming book &amp;quot;Bulletproof SSL/TLS and PKI&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| [http://shop.oreilly.com/product/9780596002701.do Network Security with OpenSSL]  || O'Reilly book from 2002&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=User:Ppelleti&amp;diff=376</id>
		<title>User:Ppelleti</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=User:Ppelleti&amp;diff=376"/>
		<updated>2013-03-23T23:36:48Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: mention abandonment of OpenSSL WikiBook&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I am a software developer at [http://oblong.com/ Oblong Industries] in Los Angeles.  I use OpenSSL (and hopefully soon, OpenSSL in conjunction with [http://libevent.org/ libevent]) to secure the Plasma messaging layer which most of our products are built on.&lt;br /&gt;
&lt;br /&gt;
I attempted to start an [https://en.wikibooks.org/wiki/OpenSSL OpenSSL WikiBook], but it never gained much traction, so I've decided to put my effort into this wiki instead.  (Both started up independently at about the same time, trying to solve the same problem.)&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Diffie-Hellman_parameters&amp;diff=303</id>
		<title>Diffie-Hellman parameters</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Diffie-Hellman_parameters&amp;diff=303"/>
		<updated>2013-03-20T20:44:22Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: link to Diffie Hellman page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;To use [https://en.wikipedia.org/wiki/Perfect_forward_secrecy perfect forward secrecy] cipher suites, you must set up [[Diffie Hellman|Diffie-Hellman]] parameters (on the server side), or the PFS cipher suites will be silently ignored.&lt;br /&gt;
&lt;br /&gt;
== Diffie-Hellman ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;SSL_CTX_set_tmp_dh&amp;lt;/code&amp;gt; is used to set the Diffie-Hellman parameters for a context.  One of the easiest ways to get Diffie-Hellman parameters to use with this function is to generate random Diffie-Hellman parameters with the [https://www.openssl.org/docs/apps/dhparam.html dhparam] command-line program with the &amp;lt;code&amp;gt;-C&amp;lt;/code&amp;gt; option, and embed the resulting code fragment in your program.  For example, &amp;lt;code&amp;gt;openssl dhparam -C 2236&amp;lt;/code&amp;gt; might result in:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifndef HEADER_DH_H&lt;br /&gt;
#include &amp;lt;openssl/dh.h&amp;gt;&lt;br /&gt;
#endif&lt;br /&gt;
DH *get_dh2236()&lt;br /&gt;
	{&lt;br /&gt;
	static unsigned char dh2236_p[]={&lt;br /&gt;
		0x0F,0x52,0xE5,0x24,0xF5,0xFA,0x9D,0xDC,0xC6,0xAB,0xE6,0x04,&lt;br /&gt;
		0xE4,0x20,0x89,0x8A,0xB4,0xBF,0x27,0xB5,0x4A,0x95,0x57,0xA1,&lt;br /&gt;
		0x06,0xE7,0x30,0x73,0x83,0x5E,0xC9,0x23,0x11,0xED,0x42,0x45,&lt;br /&gt;
		0xAC,0x49,0xD3,0xE3,0xF3,0x34,0x73,0xC5,0x7D,0x00,0x3C,0x86,&lt;br /&gt;
		0x63,0x74,0xE0,0x75,0x97,0x84,0x1D,0x0B,0x11,0xDA,0x04,0xD0,&lt;br /&gt;
		0xFE,0x4F,0xB0,0x37,0xDF,0x57,0x22,0x2E,0x96,0x42,0xE0,0x7C,&lt;br /&gt;
		0xD7,0x5E,0x46,0x29,0xAF,0xB1,0xF4,0x81,0xAF,0xFC,0x9A,0xEF,&lt;br /&gt;
		0xFA,0x89,0x9E,0x0A,0xFB,0x16,0xE3,0x8F,0x01,0xA2,0xC8,0xDD,&lt;br /&gt;
		0xB4,0x47,0x12,0xF8,0x29,0x09,0x13,0x6E,0x9D,0xA8,0xF9,0x5D,&lt;br /&gt;
		0x08,0x00,0x3A,0x8C,0xA7,0xFF,0x6C,0xCF,0xE3,0x7C,0x3B,0x6B,&lt;br /&gt;
		0xB4,0x26,0xCC,0xDA,0x89,0x93,0x01,0x73,0xA8,0x55,0x3E,0x5B,&lt;br /&gt;
		0x77,0x25,0x8F,0x27,0xA3,0xF1,0xBF,0x7A,0x73,0x1F,0x85,0x96,&lt;br /&gt;
		0x0C,0x45,0x14,0xC1,0x06,0xB7,0x1C,0x75,0xAA,0x10,0xBC,0x86,&lt;br /&gt;
		0x98,0x75,0x44,0x70,0xD1,0x0F,0x20,0xF4,0xAC,0x4C,0xB3,0x88,&lt;br /&gt;
		0x16,0x1C,0x7E,0xA3,0x27,0xE4,0xAD,0xE1,0xA1,0x85,0x4F,0x1A,&lt;br /&gt;
		0x22,0x0D,0x05,0x42,0x73,0x69,0x45,0xC9,0x2F,0xF7,0xC2,0x48,&lt;br /&gt;
		0xE3,0xCE,0x9D,0x74,0x58,0x53,0xE7,0xA7,0x82,0x18,0xD9,0x3D,&lt;br /&gt;
		0xAF,0xAB,0x40,0x9F,0xAA,0x4C,0x78,0x0A,0xC3,0x24,0x2D,0xDB,&lt;br /&gt;
		0x12,0xA9,0x54,0xE5,0x47,0x87,0xAC,0x52,0xFE,0xE8,0x3D,0x0B,&lt;br /&gt;
		0x56,0xED,0x9C,0x9F,0xFF,0x39,0xE5,0xE5,0xBF,0x62,0x32,0x42,&lt;br /&gt;
		0x08,0xAE,0x6A,0xED,0x88,0x0E,0xB3,0x1A,0x4C,0xD3,0x08,0xE4,&lt;br /&gt;
		0xC4,0xAA,0x2C,0xCC,0xB1,0x37,0xA5,0xC1,0xA9,0x64,0x7E,0xEB,&lt;br /&gt;
		0xF9,0xD3,0xF5,0x15,0x28,0xFE,0x2E,0xE2,0x7F,0xFE,0xD9,0xB9,&lt;br /&gt;
		0x38,0x42,0x57,0x03,&lt;br /&gt;
		};&lt;br /&gt;
	static unsigned char dh2236_g[]={&lt;br /&gt;
		0x02,&lt;br /&gt;
		};&lt;br /&gt;
	DH *dh;&lt;br /&gt;
&lt;br /&gt;
	if ((dh=DH_new()) == NULL) return(NULL);&lt;br /&gt;
	dh-&amp;gt;p=BN_bin2bn(dh2236_p,sizeof(dh2236_p),NULL);&lt;br /&gt;
	dh-&amp;gt;g=BN_bin2bn(dh2236_g,sizeof(dh2236_g),NULL);&lt;br /&gt;
	if ((dh-&amp;gt;p == NULL) || (dh-&amp;gt;g == NULL))&lt;br /&gt;
		{ DH_free(dh); return(NULL); }&lt;br /&gt;
	return(dh);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which can then be used like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DH *dh = get_dh2236 ();&lt;br /&gt;
if (1 != SSL_CTX_set_tmp_dh (ctx, dh))&lt;br /&gt;
  error ();&lt;br /&gt;
DH_free (dh);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Be sure to choose a bit length [http://www.keylength.com/en/3/ appropriate to the security level you want to achieve], although keep in mind that Diffie-Hellman parameters longer than 2236 bits may be [https://bugzilla.mozilla.org/show_bug.cgi?id=636802 incompatible with older versions of NSS].&lt;br /&gt;
&lt;br /&gt;
== Elliptic curve Diffie-Hellman ==&lt;br /&gt;
&lt;br /&gt;
For elliptic curve Diffie-Hellman, you can do something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);&lt;br /&gt;
if (! ecdh)&lt;br /&gt;
  error ();&lt;br /&gt;
if (1 != SSL_CTX_set_tmp_ecdh (ctx, ecdh))&lt;br /&gt;
  error ();&lt;br /&gt;
EC_KEY_free (ecdh);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in OpenSSL 1.0.2 (not yet released, as of Feb 2013) and higher, you should be able to do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SSL_CTX_set_ecdh_auto (ctx, 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, see [[Elliptic Curve Cryptography]].&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Diffie_Hellman&amp;diff=302</id>
		<title>Diffie Hellman</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Diffie_Hellman&amp;diff=302"/>
		<updated>2013-03-20T20:41:57Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Working with Parameters and Generating Keys */ mention availability of DH_get_2048_256&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Diffie-Hellman algorithm provides the capability for two communicating parties to agree a shared secret between them. This can then be used as the basis for some encryption key to be used for further communication.&lt;br /&gt;
&lt;br /&gt;
If Alice and Bob wish to communicate with each other, they first agree between them a large prime number p, and a generator (or base) g (where 0 &amp;lt; g &amp;lt; p).&lt;br /&gt;
&lt;br /&gt;
Alice chooses a secret integer a (her private key) and then calculates g&amp;lt;sup&amp;gt;a&amp;lt;/sup&amp;gt; mod p (which is her public key).&lt;br /&gt;
Bob chooses his private key b, and calculates his public key in the same way.&lt;br /&gt;
&lt;br /&gt;
Alice and Bob then send each other their public keys. Alice now knows a and Bob's public key g&amp;lt;sup&amp;gt;b&amp;lt;/sup&amp;gt; mod p. She is not able to calculate the value b from Bob's public key as this is a hard mathematical problem (known as the discrete logarithm problem). She can however calculate (g&amp;lt;sup&amp;gt;b&amp;lt;/sup&amp;gt;)&amp;lt;sup&amp;gt;a&amp;lt;/sup&amp;gt; mod p = g&amp;lt;sup&amp;gt;ab&amp;lt;/sup&amp;gt; mod p.&lt;br /&gt;
&lt;br /&gt;
Bob knows b and g&amp;lt;sup&amp;gt;a&amp;lt;/sup&amp;gt;, so he can calculate (g&amp;lt;sup&amp;gt;a&amp;lt;/sup&amp;gt;)&amp;lt;sup&amp;gt;b&amp;lt;/sup&amp;gt; mod p = g&amp;lt;sup&amp;gt;ab&amp;lt;/sup&amp;gt; mod p. Therefore both Alice and Bob know a shared secret g&amp;lt;sup&amp;gt;ab&amp;lt;/sup&amp;gt; mod p. Eve who was listening in on the communication knows p, g, Alice's public key (g&amp;lt;sup&amp;gt;a&amp;lt;/sup&amp;gt; mod p) and Bob's public key (g&amp;lt;sup&amp;gt;b&amp;lt;/sup&amp;gt; mod p). She is unable to calculate the shared secret from these values.&lt;br /&gt;
&lt;br /&gt;
In static-static mode both Alice and Bob retain their private/public keys over multiple communications. Therefore the resulting shared secret will be the same every time. In ephemeral-static mode one party will generate a new private/public key every time, thus a new shared secret will be generated.&lt;br /&gt;
&lt;br /&gt;
==Diffie-Hellman Standards==&lt;br /&gt;
&lt;br /&gt;
There are a number of standards relevant to Diffie-Hellman key agreement. Some of the key ones are:&lt;br /&gt;
&lt;br /&gt;
* PKCS 3 defines the basic algorithm and  data formats to be used: ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-3.asc&lt;br /&gt;
* ANSI X9.42 is a later standard than PKCS 3 and provides further guidance on its use&lt;br /&gt;
* RFC 2631 summarises the key points of ANSI X9.42: http://www.ietf.org/rfc/rfc2631.txt&lt;br /&gt;
* RFC 5114 defines 3 standard sets of parameters for use with Diffie-Hellman (OpenSSL has built-in support for these parameters): http://www.ietf.org/rfc/rfc5114&lt;br /&gt;
&lt;br /&gt;
==Working with Parameters and Generating Keys==&lt;br /&gt;
&lt;br /&gt;
The first step with the Diffie-Hellman algorithm is to ensure that both parties are using the same set of parameters (i.e. the same values for p and g). Since parameter generation can be an expensive process this is normally done once in advance and then the same set of parameters are used over many key exchanges. A new set of parameters can be generated by OpenSSL, or alternatively there is support for built-in standard sets of parameters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	/* Use built-in parameters */&lt;br /&gt;
	if(NULL == (params = EVP_PKEY_new())) handleErrors();&lt;br /&gt;
	if(1 != EVP_PKEY_set1_DH(params,DH_get_2048_256())) handleErrors();&lt;br /&gt;
&lt;br /&gt;
	/* Create context for the key generation */&lt;br /&gt;
	if(!(kctx = EVP_PKEY_CTX_new(params, NULL))) handleErrors();&lt;br /&gt;
&lt;br /&gt;
	/* Generate a new key */&lt;br /&gt;
	if(1 != EVP_PKEY_keygen_init(kctx)) handleErrors();&lt;br /&gt;
	if(1 != EVP_PKEY_keygen(kctx, &amp;amp;dhkey)) handleErrors();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To generate your own parameters refer to [[EVP Key and Parameter Generation]].&lt;br /&gt;
&lt;br /&gt;
Note: It appears that &amp;lt;code&amp;gt;DH_get_2048_256&amp;lt;/code&amp;gt; is scheduled for release in OpenSSL 1.0.2; it is not available in 1.0.1e or earlier.&lt;br /&gt;
&lt;br /&gt;
==Generating a Shared Secret==&lt;br /&gt;
&lt;br /&gt;
Having obtained a private/public key pair you need to also obtain the public key of the other communicating party. Refer to [[EVP Key Derivation]] for information on how to derive a shared secret.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[EVP]]&lt;br /&gt;
* [[Libcrypto API]]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Talk:Random_Numbers&amp;diff=300</id>
		<title>Talk:Random Numbers</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Talk:Random_Numbers&amp;diff=300"/>
		<updated>2013-03-20T20:33:25Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Is RAND_poll part of the public API? */ asking underlying question without using word &amp;quot;public&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== void * casting hack? ==&lt;br /&gt;
&lt;br /&gt;
The thing about casting to void* and using &amp;quot;%p&amp;quot; just to print an unsigned long in hex seemed a little odd to me.  Am I missing something as to why &amp;quot;0x%lx&amp;quot; isn't a viable option?&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 03:00, 8 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
==Is RAND_poll part of the public API?==&lt;br /&gt;
&lt;br /&gt;
I have removed the question about whether RAND_poll is considered part of the public API. The &amp;quot;rand&amp;quot; module contains two header files: rand.h and rand_locl.h. The first becomes part of the deployment when you install OpenSSL, whilst the latter does not. This is a common theme throughout the library. Basically anything in the *_locl.h header files should be considered private to the library, whilst anything that gets deployed should be considered part of the public API. Since RAND_poll is in rand.h (which gets deployed during installation) it should be considered part of the public API.&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 12:26, 9 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
I think it would be good to get a clear statement of what constitutes the public API, because I'm getting conflicting messages.  You state that anything in an installed header file is part of the public API.  However, [[Contributions#Code_Maturity]] implies that functions are not part of the API until they are documented.  Since RAND_poll is not documented, that interpretation would suggested that RAND_poll is not yet &amp;quot;cast in stone&amp;quot; and is subject to change.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 00:03, 19 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
I don't think there is a conflict between what I said above, and what it says on [[Contributions#Code_Maturity]]. That page merely says that the API might not have completely settled down which might be the reason why there isn't any documentation. It doesn't say that the API is not &amp;quot;public&amp;quot; until its been documented.&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 21:05, 19 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Sidestepping the semantics of what it means to be &amp;quot;public&amp;quot;, the real question I'm getting at is whether we should be encouraging people to use RAND_poll?  Or did the OpenSSL developers leave RAND_poll undocumented on purpose?&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 20:33, 20 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
==Should we be promoting a commercial company?==&lt;br /&gt;
&lt;br /&gt;
I am a little concerned about the link to entropykey. Should we be promoting a commercial company? Whilst the text reads fine at the moment it could be the &amp;quot;thin end of the wedge&amp;quot;. This is a wiki after all. What's to stop other companies coming along and adding links to their products? How will we then decide which edits are ok, and which not? We don't want the page degenerating into a long list of products. Should we not have a policy of &amp;quot;no commerical endorsements&amp;quot;?&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 12:33, 9 March 2013 (UTC)&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Talk:Random_Numbers&amp;diff=289</id>
		<title>Talk:Random Numbers</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Talk:Random_Numbers&amp;diff=289"/>
		<updated>2013-03-19T00:03:38Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Is RAND_poll part of the public API? */ conflicting statement about APIs only being guaranteed once documented&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== void * casting hack? ==&lt;br /&gt;
&lt;br /&gt;
The thing about casting to void* and using &amp;quot;%p&amp;quot; just to print an unsigned long in hex seemed a little odd to me.  Am I missing something as to why &amp;quot;0x%lx&amp;quot; isn't a viable option?&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 03:00, 8 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
==Is RAND_poll part of the public API?==&lt;br /&gt;
&lt;br /&gt;
I have removed the question about whether RAND_poll is considered part of the public API. The &amp;quot;rand&amp;quot; module contains two header files: rand.h and rand_locl.h. The first becomes part of the deployment when you install OpenSSL, whilst the latter does not. This is a common theme throughout the library. Basically anything in the *_locl.h header files should be considered private to the library, whilst anything that gets deployed should be considered part of the public API. Since RAND_poll is in rand.h (which gets deployed during installation) it should be considered part of the public API.&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 12:26, 9 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
I think it would be good to get a clear statement of what constitutes the public API, because I'm getting conflicting messages.  You state that anything in an installed header file is part of the public API.  However, [[Contributions#Code_Maturity]] implies that functions are not part of the API until they are documented.  Since RAND_poll is not documented, that interpretation would suggested that RAND_poll is not yet &amp;quot;cast in stone&amp;quot; and is subject to change.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 00:03, 19 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
==Should we be promoting a commercial company?==&lt;br /&gt;
&lt;br /&gt;
I am a little concerned about the link to entropykey. Should we be promoting a commercial company? Whilst the text reads fine at the moment it could be the &amp;quot;thin end of the wedge&amp;quot;. This is a wiki after all. What's to stop other companies coming along and adding links to their products? How will we then decide which edits are ok, and which not? We don't want the page degenerating into a long list of products. Should we not have a policy of &amp;quot;no commerical endorsements&amp;quot;?&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 12:33, 9 March 2013 (UTC)&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Diffie-Hellman_parameters&amp;diff=265</id>
		<title>Diffie-Hellman parameters</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Diffie-Hellman_parameters&amp;diff=265"/>
		<updated>2013-03-13T21:15:59Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Elliptic curve Diffie-Hellman */ link to ECC page, since my treatment of ECDH here is minimal&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;To use [https://en.wikipedia.org/wiki/Perfect_forward_secrecy perfect forward secrecy] cipher suites, you must set up Diffie-Hellman parameters (on the server side), or the PFS cipher suites will be silently ignored.&lt;br /&gt;
&lt;br /&gt;
== Diffie-Hellman ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;SSL_CTX_set_tmp_dh&amp;lt;/code&amp;gt; is used to set the Diffie-Hellman parameters for a context.  One of the easiest ways to get Diffie-Hellman parameters to use with this function is to generate random Diffie-Hellman parameters with the [https://www.openssl.org/docs/apps/dhparam.html dhparam] command-line program with the &amp;lt;code&amp;gt;-C&amp;lt;/code&amp;gt; option, and embed the resulting code fragment in your program.  For example, &amp;lt;code&amp;gt;openssl dhparam -C 2236&amp;lt;/code&amp;gt; might result in:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifndef HEADER_DH_H&lt;br /&gt;
#include &amp;lt;openssl/dh.h&amp;gt;&lt;br /&gt;
#endif&lt;br /&gt;
DH *get_dh2236()&lt;br /&gt;
	{&lt;br /&gt;
	static unsigned char dh2236_p[]={&lt;br /&gt;
		0x0F,0x52,0xE5,0x24,0xF5,0xFA,0x9D,0xDC,0xC6,0xAB,0xE6,0x04,&lt;br /&gt;
		0xE4,0x20,0x89,0x8A,0xB4,0xBF,0x27,0xB5,0x4A,0x95,0x57,0xA1,&lt;br /&gt;
		0x06,0xE7,0x30,0x73,0x83,0x5E,0xC9,0x23,0x11,0xED,0x42,0x45,&lt;br /&gt;
		0xAC,0x49,0xD3,0xE3,0xF3,0x34,0x73,0xC5,0x7D,0x00,0x3C,0x86,&lt;br /&gt;
		0x63,0x74,0xE0,0x75,0x97,0x84,0x1D,0x0B,0x11,0xDA,0x04,0xD0,&lt;br /&gt;
		0xFE,0x4F,0xB0,0x37,0xDF,0x57,0x22,0x2E,0x96,0x42,0xE0,0x7C,&lt;br /&gt;
		0xD7,0x5E,0x46,0x29,0xAF,0xB1,0xF4,0x81,0xAF,0xFC,0x9A,0xEF,&lt;br /&gt;
		0xFA,0x89,0x9E,0x0A,0xFB,0x16,0xE3,0x8F,0x01,0xA2,0xC8,0xDD,&lt;br /&gt;
		0xB4,0x47,0x12,0xF8,0x29,0x09,0x13,0x6E,0x9D,0xA8,0xF9,0x5D,&lt;br /&gt;
		0x08,0x00,0x3A,0x8C,0xA7,0xFF,0x6C,0xCF,0xE3,0x7C,0x3B,0x6B,&lt;br /&gt;
		0xB4,0x26,0xCC,0xDA,0x89,0x93,0x01,0x73,0xA8,0x55,0x3E,0x5B,&lt;br /&gt;
		0x77,0x25,0x8F,0x27,0xA3,0xF1,0xBF,0x7A,0x73,0x1F,0x85,0x96,&lt;br /&gt;
		0x0C,0x45,0x14,0xC1,0x06,0xB7,0x1C,0x75,0xAA,0x10,0xBC,0x86,&lt;br /&gt;
		0x98,0x75,0x44,0x70,0xD1,0x0F,0x20,0xF4,0xAC,0x4C,0xB3,0x88,&lt;br /&gt;
		0x16,0x1C,0x7E,0xA3,0x27,0xE4,0xAD,0xE1,0xA1,0x85,0x4F,0x1A,&lt;br /&gt;
		0x22,0x0D,0x05,0x42,0x73,0x69,0x45,0xC9,0x2F,0xF7,0xC2,0x48,&lt;br /&gt;
		0xE3,0xCE,0x9D,0x74,0x58,0x53,0xE7,0xA7,0x82,0x18,0xD9,0x3D,&lt;br /&gt;
		0xAF,0xAB,0x40,0x9F,0xAA,0x4C,0x78,0x0A,0xC3,0x24,0x2D,0xDB,&lt;br /&gt;
		0x12,0xA9,0x54,0xE5,0x47,0x87,0xAC,0x52,0xFE,0xE8,0x3D,0x0B,&lt;br /&gt;
		0x56,0xED,0x9C,0x9F,0xFF,0x39,0xE5,0xE5,0xBF,0x62,0x32,0x42,&lt;br /&gt;
		0x08,0xAE,0x6A,0xED,0x88,0x0E,0xB3,0x1A,0x4C,0xD3,0x08,0xE4,&lt;br /&gt;
		0xC4,0xAA,0x2C,0xCC,0xB1,0x37,0xA5,0xC1,0xA9,0x64,0x7E,0xEB,&lt;br /&gt;
		0xF9,0xD3,0xF5,0x15,0x28,0xFE,0x2E,0xE2,0x7F,0xFE,0xD9,0xB9,&lt;br /&gt;
		0x38,0x42,0x57,0x03,&lt;br /&gt;
		};&lt;br /&gt;
	static unsigned char dh2236_g[]={&lt;br /&gt;
		0x02,&lt;br /&gt;
		};&lt;br /&gt;
	DH *dh;&lt;br /&gt;
&lt;br /&gt;
	if ((dh=DH_new()) == NULL) return(NULL);&lt;br /&gt;
	dh-&amp;gt;p=BN_bin2bn(dh2236_p,sizeof(dh2236_p),NULL);&lt;br /&gt;
	dh-&amp;gt;g=BN_bin2bn(dh2236_g,sizeof(dh2236_g),NULL);&lt;br /&gt;
	if ((dh-&amp;gt;p == NULL) || (dh-&amp;gt;g == NULL))&lt;br /&gt;
		{ DH_free(dh); return(NULL); }&lt;br /&gt;
	return(dh);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which can then be used like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DH *dh = get_dh2236 ();&lt;br /&gt;
if (1 != SSL_CTX_set_tmp_dh (ctx, dh))&lt;br /&gt;
  error ();&lt;br /&gt;
DH_free (dh);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Be sure to choose a bit length [http://www.keylength.com/en/3/ appropriate to the security level you want to achieve], although keep in mind that Diffie-Hellman parameters longer than 2236 bits may be [https://bugzilla.mozilla.org/show_bug.cgi?id=636802 incompatible with older versions of NSS].&lt;br /&gt;
&lt;br /&gt;
== Elliptic curve Diffie-Hellman ==&lt;br /&gt;
&lt;br /&gt;
For elliptic curve Diffie-Hellman, you can do something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);&lt;br /&gt;
if (! ecdh)&lt;br /&gt;
  error ();&lt;br /&gt;
if (1 != SSL_CTX_set_tmp_ecdh (ctx, ecdh))&lt;br /&gt;
  error ();&lt;br /&gt;
EC_KEY_free (ecdh);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in OpenSSL 1.0.2 (not yet released, as of Feb 2013) and higher, you should be able to do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SSL_CTX_set_ecdh_auto (ctx, 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, see [[Elliptic Curve Cryptography]].&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=239</id>
		<title>Related Links</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Related_Links&amp;diff=239"/>
		<updated>2013-03-11T23:44:38Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: add a few more links&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Please feel free to edit this page and add your own OpenSSL-based project or product. This is the one place where otherwise extraneous mention of commercial products is appropriate. Note such mention does not constitute endorsement per our [[Commercial Product Disclaimer]].&lt;br /&gt;
&lt;br /&gt;
== Noncommercial Cryptographic Libraries ==&lt;br /&gt;
* [http://www.bouncycastle.org/ Bouncy Castle] - cryptography API for Java and C#&lt;br /&gt;
* [http://www.mozilla.org/projects/security/pki/nss/ NSS] - a set of libraries designed to support cross-platform development of security-enabled client and server applications.&lt;br /&gt;
* [http://www.gnutls.org/ GnuTLS] - an LGPL-licensed TLS library with excellent documentation&lt;br /&gt;
* [http://botan.randombit.net/ Botan] - a C++ cryptography library which includes a TLS implementation&lt;br /&gt;
* [https://en.wikipedia.org/wiki/Comparison_of_TLS_implementations Wikipedia article comparing various TLS libraries]&lt;br /&gt;
&lt;br /&gt;
== Noncommercial Products Using OpenSSL ==&lt;br /&gt;
* [https://www.stunnel.org/index.html Stunnel] - an SSL encryption wrapper between remote client and local (inetd-startable) or remote server&lt;br /&gt;
* [http://libevent.org/ libevent] - an event driven library which can [http://www.wangafu.net/~nickm/libevent-book/Ref6a_advanced_bufferevents.html#_bufferevents_and_ssl optionally use OpenSSL]&lt;br /&gt;
&lt;br /&gt;
== Commercial Cryptographic Libraries ==&lt;br /&gt;
&lt;br /&gt;
== Commercial Products Using OpenSSL ==&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Random_Numbers&amp;diff=205</id>
		<title>Random Numbers</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Random_Numbers&amp;diff=205"/>
		<updated>2013-03-08T03:16:13Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Reseed */ suggest using RAND_poll for reseed if you just want to use the system entropy source&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Random Numbers]] are a cryptographic primitive and cornerstone to nearly all cryptographic systems. They are used in almost all areas of cryptography, from key agreement and transport to session keys for bulk encryption. A quality source of random bits and proper use of OpenSSL APIs will help ensure your program is cryptographically sound. On the other hand, a poor source of randomness or incorrect library usage could result in loss of security. This article will help you use random number generation routines correctly when programming with the OpenSSL library.&lt;br /&gt;
&lt;br /&gt;
OpenSSL provides a number of software based random number generators based on a variety of sources.  A software based random number generator creates random numbers by executing a software algorithm. There are a number of algorithms specified by a number of standard bodies including NIST, ANSI X9 committee (X9.17 and X9.31) and XXX. In addition, the library can use custom hardware if the hardware has an &amp;lt;tt&amp;gt;ENIGNE&amp;lt;/tt&amp;gt; interface. &lt;br /&gt;
&lt;br /&gt;
Good random numbers are notoriously hard to produce from deterministic processes such as a computer executing instructions. A number of cryptographic attacks have been developed because they are so hard to acquire. Especially vulnerable are headless servers, embedded devices, and mobile devices, and you may have to take extra steps to ensure an adequate supply of entropy is available. The extra steps could include ''Hedging'' on a headless server or embedded device, and ''Finger Painting'' on a mobile device. For recent attacks on low entropy devices (such as headless servers and mobile devices), see for example, ''[http://pages.cs.wisc.edu/~rist/papers/sslhedge.html When Good Randomness Goes Bad: Virtual Machine Reset Vulnerabilities and Hedging Deployed Cryptography ]'', ''[https://factorable.net/paper.html Mining Your Ps and Qs: Detection of Widespread Weak Keys in Network Devices]'', and ''[http://www.csoonline.com/article/723229/traffic-sensor-flaw-that-could-allow-driver-tracking-fixed Traffic sensor flaw that could allow driver tracking fixed]''.&lt;br /&gt;
&lt;br /&gt;
== Entropy ==&lt;br /&gt;
&lt;br /&gt;
Entropy is the measure of &amp;quot;randomness&amp;quot; in a sequence of bits. Different sources have different entropy. For example, a physical process in nature may have 100% entropy which appears purely random. On the other hand, the written English language provides about 3 bits/byte (or character) which is at most 38%. Some estimates have shown English characters provide only 1 bit/byte (or 12%). Other sources used as a random stream will have different estimates of entropy, and you will have to determine the quality.&lt;br /&gt;
&lt;br /&gt;
Random number generators need quality entropy for input (a ''seed'', discussed below) and must produce quality output (''quod vide''). When using OpenSSL's APIs, you will be asked to estimate entropy when seeding or reseeding (input). When estimating entropy you should error on the low side to ensure proper fitness of the generator. When receiving bytes, you will receive a code indicating the success/failure of the operation and quality of the bytes (output).&lt;br /&gt;
&lt;br /&gt;
Sometimes the operating system offers block access to hardware random number generators via &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; can be a low volume device, and could potentially block. For example, the [http://www.portwell.com.tw/download/sbc/catalog/Intel_80802_FWH.pdf Intel 82802 Firmware Hub] used with the and i840 chipset produces one byte of data in its register. At other times, &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; can be a high volume device, such as Intel's [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key Technology]. In virtualized environments, &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; might actually be a [http://wiki.qemu.org/Features-Done/VirtIORNG VirtIO RNG].&lt;br /&gt;
&lt;br /&gt;
Entropy is important for a healthy program, and you should investigate hardware modules to help acquire it, especially if poor entropy or entropy depletion are a concern. There are a number of inexpensive and high quality hardware modules on the market, including a $40UK [http://www.entropykey.co.uk EntropyKey]. There are also a number of high quality and high priced hardware modules and accelerators.&lt;br /&gt;
&lt;br /&gt;
If you lack &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt; and cannot procure a hardware random number generator, you can also consider an alternate entropy gather such as the [http://egd.sourceforge.net Entropy Gathering Daemon (EGD)]. EGD is an userspace substitute for &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;. OpenSSL provides native support for EGD via &amp;lt;tt&amp;gt;RAND_egd&amp;lt;/tt&amp;gt; to connect to the Unix domain socket, and &amp;lt;tt&amp;gt;RAND_egd_bytes&amp;lt;/tt&amp;gt; to extract bytes from the daemon.&lt;br /&gt;
&lt;br /&gt;
== Seeds ==&lt;br /&gt;
&lt;br /&gt;
Most random number generators require a seed. A seed is a secret, unpredictable sequence of bytes that is transformed and then used to set the initial state of the generator. The seed ensures that each unique instance of a generator produces a unique stream of bits. No two generators should ever produce the same sequence of random numbers, even when faced with Virtual Machine (VM) rollback attacks (which could happen accidentally by a data center operator).&lt;br /&gt;
&lt;br /&gt;
When seeding your generators, you should use at least 256 bits (32 bytes) of material. You can verify the required number of bits by grepping the source files for &amp;lt;tt&amp;gt;#define ENTROPY_NEEDED&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Initial ===&lt;br /&gt;
&lt;br /&gt;
OpenSSL will attempt to seed the random number generator automatically upon instantiation by calling &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt;.  &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; seeds the random number generator using a system-specific entropy source, which is &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt; on UNIX-like operating systems, and is a combination of [http://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx &amp;lt;tt&amp;gt;CryptGenRandom&amp;lt;/tt&amp;gt;] and other sources of entropy on Windows.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;urandom&amp;lt;/tt&amp;gt; device may lack sufficient entropy for your needs, and you might want to reseed it immediately from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;. On Unix and other operating systems that provide the block device, you can use &amp;lt;tt&amp;gt;RAND_load_file&amp;lt;/tt&amp;gt; to load directly from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int rc = RAND_load_file(&amp;quot;/dev/random&amp;quot;, 32);&lt;br /&gt;
if(rc != 32) {&lt;br /&gt;
    /* RAND_load_file failed */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reseed ===&lt;br /&gt;
&lt;br /&gt;
The OpenSSL API allows you to provide a seed and refresh the generator's state with reseeds at anytime during the program's execution. Two functions are provided for seeding and reseeding: &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; accepts a buffer and size; while &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; accepts a buffer, size, and entropy estimate in bytes. &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; will call &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; assuming 100% entropy.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; is shown below. The function is &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt;, so it &amp;lt;nowiki&amp;gt;[apparently]&amp;lt;/nowiki&amp;gt; cannot fail (or convey failures). Though the example uses the actual number of bytes written to the buffer, the entire buffer can be used to increase entropy (with hopes the unused bytes in the buffer has entropy to extract). Though you can use uninitialized bytes as input, you should not expect any entropy in the uninitialized bytes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[32];&lt;br /&gt;
int written = get_random_bytes(buffer, sizeof(buffer));&lt;br /&gt;
&lt;br /&gt;
RAND_seed(buffer, written);&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; is similar to &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; but requires an entropy estimate. The estimate should be the number of full bytes of entropy in the buffer. If you have a 32 byte buffer with about 50% entropy, you should provide 16 as the entropy estimate. &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; is also a &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt; function, so it cannot fail (or convey failures). The example also uses the actual number of bytes written to the buffer, but the entire buffer can be used to increase entropy. Note that &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; takes a &amp;lt;tt&amp;gt;double&amp;lt;/tt&amp;gt;, so be sure to '''avoid''' integer math. Otherwise, the entropy estimate calculation could result in 0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;char phrase[64];&lt;br /&gt;
int written = get_random_phrase(phrase, sizeof(phrase));&lt;br /&gt;
&lt;br /&gt;
RAND_add(phrase, written, 0.12f * written /* 12% */);&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On Windows machines, you can also use &amp;lt;tt&amp;gt;RAND_screen&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;RAND_event&amp;lt;/tt&amp;gt; on Windows machines. &amp;lt;tt&amp;gt;RAND_screen&amp;lt;/tt&amp;gt; will mix the contents of the screen into the generator. &amp;lt;tt&amp;gt;RAND_event&amp;lt;/tt&amp;gt; can be used with programs that process [http://msdn.microsoft.com/en-us/library/windows/desktop/ff381405(v=vs.85).aspx Windows Messages]. Both methods should only be used with interactive programs, and not services nor drivers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; can be used to reseed the generator using the system entropy source.  (Question: Is &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; considered a public API function?  Although it is used widely in examples, it doesn't seem to actually be documented.  Not sure if that is intentional or an oversight.)&lt;br /&gt;
&lt;br /&gt;
=== Persisting ===&lt;br /&gt;
&lt;br /&gt;
If you are worried about slow starts - or the time it takes to get the random number generator in good working order - you can write out a future seed and use it upon the next program execution. To save the future seed, use the library's &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt; function. When using &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt;, you only need to specify a filename. &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt; returns the number of bytes written or -1 to indicate bytes were written without an appropriate seed (failure).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int written = RAND_save_file(&amp;quot;prng.seed&amp;quot;);&lt;br /&gt;
if(written &amp;lt;= 0)&lt;br /&gt;
    /* RAND_save_file failed */&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At program startup, you can attempt to read the saved seed with &amp;lt;tt&amp;gt;RAND_load_file&amp;lt;/tt&amp;gt;. You can specify the number of bytes to read, or -1 to indicate the entire file should be used. The bytes read are automatically added to the generator. &amp;lt;tt&amp;gt;RAND_load_file&amp;lt;/tt&amp;gt; returns the number of bytes read.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int read = RAND_load_file(&amp;quot;prng.seed&amp;quot;, -1);&lt;br /&gt;
if(read &amp;lt;= 0)&lt;br /&gt;
    /* RAND_load_file failed */&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If possible, you should use protected storage offered by the operating system. For example, you should avoid writing the file and store the seed in the [http://developer.apple.com/library/ios/#documentation/security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html iOS Keychain], [http://developer.android.com/reference/android/security/KeyChain.html Android KeyChain], or [http://msdn.microsoft.com/en-us/library/ms995355.aspx Windows DPAPI]. When writing the seed to the filesystem, be sure to protect the the seed through the file system's permission scheme (Linux has not realized userland needs help from the kernel when storing secrets).&lt;br /&gt;
&lt;br /&gt;
== Generation ==&lt;br /&gt;
&lt;br /&gt;
After the generator has been seeded and is in good working order, you can extract bytes. You have three functions to extract bytes. First is &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; and the second is &amp;lt;tt&amp;gt;RAND_pseudo_bytes&amp;lt;/tt&amp;gt;. Both are software based and produce a pseudo-random stream. The third method is hardware based and it reuses &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Software ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; will fetch cryptographically strong random bytes. Cryptographically strong bytes are suitable for high integrity needs, such as long term key generation. If your generator is using a software algorithm, then the bytes will be pseudo-random (but still cryptographically strong). &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; returns 1 for success, and 0 otherwise. If you changed the &amp;lt;tt&amp;gt;RAND_METHOD&amp;lt;/tt&amp;gt; and it is not supported, then the function will return -1. In case of error, you can call &amp;lt;tt&amp;gt;ERR_get_error&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[128];&lt;br /&gt;
&lt;br /&gt;
int rc = RAND_bytes(buffer, sizeof(buffer));&lt;br /&gt;
unsigned long err = ERR_get_error();&lt;br /&gt;
&lt;br /&gt;
if(rc != 1) {&lt;br /&gt;
    /* RAND_bytes failed */&lt;br /&gt;
    /* `err` is valid    */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_pseudo_bytes&amp;lt;/tt&amp;gt; returns pseudo-random bytes which ''can'' be cryptographically strong. The function returns 1 if the bytes are cryptographically strong, and 0 otherwise. If your application has high integrity requirements, it should ''not'' use &amp;lt;tt&amp;gt;RAND_pseudo_bytes&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
When using &amp;lt;tt&amp;gt;RAND_pseudo_bytes&amp;lt;/tt&amp;gt;, both 0 and 1 indicate success. If you change the &amp;lt;tt&amp;gt;RAND_METHOD&amp;lt;/tt&amp;gt; and it is not supported, then the function will return -1. In case of error, you can call &amp;lt;tt&amp;gt;ERR_get_error&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[32];&lt;br /&gt;
&lt;br /&gt;
int rc = RAND_pseudo_bytes(buffer, sizeof(buffer));&lt;br /&gt;
unsigned long err = ERR_get_error();&lt;br /&gt;
&lt;br /&gt;
if(rc != 0 &amp;amp;&amp;amp; rc != 1) {&lt;br /&gt;
    /* RAND_pseudo_bytes failed */&lt;br /&gt;
    /* `err` is valid           */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hardware ===&lt;br /&gt;
&lt;br /&gt;
Hardware random number generators are almost always better to use than a software based generator. Hardware generators are often called True Random Number generators (TRNG) or Non-Deterministic Random Number Generators since they don't rely on the deterministic behavior of executing software instructions. Their bits streams are nearly always indistinguishable from random streams, and their entropy is always nearly 100%.&lt;br /&gt;
&lt;br /&gt;
Some hardware generators are easier to use than other. For example, an [http://www.entropykey.co.uk EntropyKey] will provide a driver that replenishes &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;, so an application does not have to do anything special other than reading from the device. Other generators, such as Intel's [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key], must be integrated into an application. When integrating generators using OpenSSL, you will use the library's &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
To integrate a hardware based random number generator, you should load the apporpriate &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; for the hardware based implementation. Once loaded, set the engine's &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; method as default with &amp;lt;tt&amp;gt;ENGINE_METHOD_RAND&amp;lt;/tt&amp;gt;. After you load the engine and set &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; for the hardware generator, you simply use &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; as discussed earlier. There are no special steps necessary after the configuration.&lt;br /&gt;
&lt;br /&gt;
If you have OpenSSL 1.0.1 and a machine with [http://semiaccurate.com/2012/04/23/intel-launches-ivy-bridge-amid-crushing-marketing-buzzwords/#.UTYxLlHft0w 3rd generation Core i5 or i7 processor (Ivy Bridge)], then the Intel [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key Technology] (formerly called Bull Mountain) is available to you. The hardware generator is accessed through the &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; API and wraps the &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; instruction. According to Intel, [http://software.intel.com/en-us/articles/performance-impact-of-intel-secure-key-on-openssl RDRAND-enabled version of OpenSSL outperformed the non-RDRAND version by an order of magnitude].&lt;br /&gt;
&lt;br /&gt;
To ensure &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; uses the hardware engine, you must perform three steps:&lt;br /&gt;
&lt;br /&gt;
* load the &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; engine&lt;br /&gt;
* acquire a handle to the engine&lt;br /&gt;
* set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; to the engine&lt;br /&gt;
&lt;br /&gt;
The code below shows you how to load the Intel random number generator engine and set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt;. The code is available for download at [[Media:test-rdrand.zip|test-rdrand.zip]]. While you can call &amp;lt;tt&amp;gt;ENGINE_load_builtin_engines&amp;lt;/tt&amp;gt; to make all engines available, the code below focuses on the one engine of interest and loads it via &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt;. See [http://www.openssl.org/docs/crypto/engine.html OpenSSL's engine(3)] for more details on engines, their loading, and operation.&lt;br /&gt;
&lt;br /&gt;
Casting the error code to a &amp;lt;tt&amp;gt;void*&amp;lt;/tt&amp;gt; for hexadecimal display is an abuse (hack?), but it sidesteps problems with &amp;lt;tt&amp;gt;printf&amp;lt;/tt&amp;gt; format specifiers on x86 and x64 hardware; and gives you an error that is easily consumed by &amp;lt;tt&amp;gt;openssl errstr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; 1    unsigned long err = 0;&lt;br /&gt;
 2    int rc = 0;&lt;br /&gt;
 3&lt;br /&gt;
 4    ENGINE_load_rdrand();&lt;br /&gt;
 5    ENGINE* eng = ENGINE_by_id(&amp;quot;rdrand&amp;quot;);&lt;br /&gt;
 6    err = ERR_get_error();&lt;br /&gt;
 7&lt;br /&gt;
 8    if(NULL == eng) {&lt;br /&gt;
 9        fprintf(stderr, &amp;quot;ENGINE_load_rdrand failed, err = %p\n&amp;quot;, (void*)err);&lt;br /&gt;
10        abort(); /* failed */&lt;br /&gt;
11    }&lt;br /&gt;
12  &lt;br /&gt;
13    rc = ENGINE_set_default(eng, ENGINE_METHOD_RAND);&lt;br /&gt;
14    err = ERR_get_error();&lt;br /&gt;
15&lt;br /&gt;
16    if(1 != rc) {&lt;br /&gt;
17        fprintf(stderr, &amp;quot;ENGINE_set_default failed, err = %p\n&amp;quot;, (void*)err);&lt;br /&gt;
18        abort(); /* failed */&lt;br /&gt;
19    }&lt;br /&gt;
20&lt;br /&gt;
21    /* OK to proceed */&lt;br /&gt;
22&lt;br /&gt;
23    ...&lt;br /&gt;
24    ENGINE_free(eng);&lt;br /&gt;
25    ENGINE_cleanup();&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you hardware does not support the Intel generator, you will recieve a &amp;lt;tt&amp;gt;NULL&amp;lt;/tt&amp;gt; pointer at line 5 and encounter error 0x2606c043 at line 6. The error can then be fed to &amp;lt;tt&amp;gt;openssl errstr&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ./test-rdrand.exe&lt;br /&gt;
...&lt;br /&gt;
ENGINE_load_rdrand failed, err = 0x2606c043&lt;br /&gt;
$ openssl errstr 0x2606c043&lt;br /&gt;
error:2606C043:engine routines:ENGINE_FREE_UTIL:passed a null parameter&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:test-rdrand.png|thumb|250px|right|Verifying rdrand code path]]  Line 13 attempts to set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; to that provided by the engine using &amp;lt;tt&amp;gt;ENGINE_set_default&amp;lt;/tt&amp;gt; with &amp;lt;tt&amp;gt;ENGINE_METHOD_RAND&amp;lt;/tt&amp;gt;. Upon success, OpenSSL will internally use &amp;lt;tt&amp;gt;OPENSSL_ia32_rdrand&amp;lt;/tt&amp;gt; for random number generation. To verify code correctness, simply set a breakpoint on the function and wait for the debugger to snap as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The 0x2606c043 error is actually caused by &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt;. The function will verify the capabilities of the hardware and load the generator's engine if available. &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt; is a &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt; function, so it cannot fail or cannot convey failures (which we know is incorrect from a test run). The source code can be found in &amp;lt;tt&amp;gt;eng_rdrand.c&amp;lt;/tt&amp;gt; and is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void ENGINE_load_rdrand (void)&lt;br /&gt;
{&lt;br /&gt;
    extern unsigned int OPENSSL_ia32cap_P[];&lt;br /&gt;
&lt;br /&gt;
    if (OPENSSL_ia32cap_P[1] &amp;amp; (1&amp;lt;&amp;lt;(62-32)))&lt;br /&gt;
    {&lt;br /&gt;
        ENGINE *toadd = ENGINE_rdrand();&lt;br /&gt;
        if(!toadd) return;&lt;br /&gt;
        ENGINE_add(toadd);&lt;br /&gt;
        ENGINE_free(toadd);&lt;br /&gt;
        ERR_clear_error();&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to [http://software.intel.com/en-us/articles/performance-impact-of-intel-secure-key-on-openssl Intel documentation], the random number generator does not need to be seeded via the &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; function because the generator is self-seeding. For optimal performance, code that is aware of the underlying random engine can forgo gathering entropy.&lt;br /&gt;
&lt;br /&gt;
Additionally (or more importantly), the following will not cause a crash when using the hardware random number generator (and it fails silently so all looks good from outside the fishbowl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/* Bad - don't do this in production */&lt;br /&gt;
byte seed[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };&lt;br /&gt;
RAND_seed(seed, sizeof(seed));&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, you can test if your Mac OS X system has &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; available with the following (thanks to Dave Zarzycki):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$ sysctl hw.optional.rdrand&lt;br /&gt;
hw.optional.rdrand: 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On Linux, you can &amp;lt;tt&amp;gt;cat&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;cpuinfo&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ cat /proc/cpuinfo | grep -i rdrand&lt;br /&gt;
rdrand	: 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
&lt;br /&gt;
Two miscellaneous items remaining are generator cleanup  and status. &amp;lt;tt&amp;gt;RAND_cleanup&amp;lt;/tt&amp;gt; securely erases the memory used by the random number generator.&lt;br /&gt;
&lt;br /&gt;
You can query the generator's state with &amp;lt;tt&amp;gt;RAND_status&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;RAND_status&amp;lt;/tt&amp;gt; returns 1 if the generator is in good working order. If your generator is not in good working order, you should reseed it with at least 256 bits (32 bytes) of entropy. The function [http://www.mail-archive.com/openssl-dev@openssl.org/msg04212.html purposefully hides the number of bytes needed].&lt;br /&gt;
&lt;br /&gt;
== FIPS Mode ==&lt;br /&gt;
&lt;br /&gt;
FIPS mode is a special mode of operation which specifies the library should operate according to the security policies and procedures specified in [http://csrc.nist.gov/publications/fips/fips140-2/fips1402.pdf FIPS 140-2]. The mode requires use of the FIPS Capable OpenSSL library, and must be enabled with a call to &amp;lt;tt&amp;gt;FIPS_mode_set&amp;lt;/tt&amp;gt;. Once in FIPS mode, a ''default DRBG'' is used as specified in [http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf SP800-90].&lt;br /&gt;
&lt;br /&gt;
The default DRBG is 256-bit CTR AES using a derivation function, and is decided by the application and not the library module. In the case of an OpenSSL application it is specified in &amp;lt;tt&amp;gt;rand_lib.c&amp;lt;/tt&amp;gt; via the &amp;lt;tt&amp;gt;OPENSSL_DRBG_DEFAULT_TYPE&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;OPENSSL_DRBG_DEFAULT_FLAGS&amp;lt;/tt&amp;gt; preprocessor macros to allow them to be overridden by local compilation options or at runtime.&lt;br /&gt;
&lt;br /&gt;
To use the FIPS random number generator, simply use &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; as described earlier. Note that the call to &amp;lt;tt&amp;gt;FIPS_mode_set&amp;lt;/tt&amp;gt; must succeed in order to operate in FIPS 140 mode.&lt;br /&gt;
&lt;br /&gt;
== Thread Safety ==&lt;br /&gt;
&lt;br /&gt;
The random number generators (among other parts of OpenSSL) are not thread safe by default. To ensure thread safety, you must call &amp;lt;tt&amp;gt;[https://www.openssl.org/docs/crypto/threads.html CRYPTO_set_locking_callback]&amp;lt;/tt&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Random_Numbers&amp;diff=204</id>
		<title>Random Numbers</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Random_Numbers&amp;diff=204"/>
		<updated>2013-03-08T03:10:38Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Initial */ indicate that RAND_poll is called automatically, and RAND_poll has a reasonable implementation on both UNIX and Windows&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Random Numbers]] are a cryptographic primitive and cornerstone to nearly all cryptographic systems. They are used in almost all areas of cryptography, from key agreement and transport to session keys for bulk encryption. A quality source of random bits and proper use of OpenSSL APIs will help ensure your program is cryptographically sound. On the other hand, a poor source of randomness or incorrect library usage could result in loss of security. This article will help you use random number generation routines correctly when programming with the OpenSSL library.&lt;br /&gt;
&lt;br /&gt;
OpenSSL provides a number of software based random number generators based on a variety of sources.  A software based random number generator creates random numbers by executing a software algorithm. There are a number of algorithms specified by a number of standard bodies including NIST, ANSI X9 committee (X9.17 and X9.31) and XXX. In addition, the library can use custom hardware if the hardware has an &amp;lt;tt&amp;gt;ENIGNE&amp;lt;/tt&amp;gt; interface. &lt;br /&gt;
&lt;br /&gt;
Good random numbers are notoriously hard to produce from deterministic processes such as a computer executing instructions. A number of cryptographic attacks have been developed because they are so hard to acquire. Especially vulnerable are headless servers, embedded devices, and mobile devices, and you may have to take extra steps to ensure an adequate supply of entropy is available. The extra steps could include ''Hedging'' on a headless server or embedded device, and ''Finger Painting'' on a mobile device. For recent attacks on low entropy devices (such as headless servers and mobile devices), see for example, ''[http://pages.cs.wisc.edu/~rist/papers/sslhedge.html When Good Randomness Goes Bad: Virtual Machine Reset Vulnerabilities and Hedging Deployed Cryptography ]'', ''[https://factorable.net/paper.html Mining Your Ps and Qs: Detection of Widespread Weak Keys in Network Devices]'', and ''[http://www.csoonline.com/article/723229/traffic-sensor-flaw-that-could-allow-driver-tracking-fixed Traffic sensor flaw that could allow driver tracking fixed]''.&lt;br /&gt;
&lt;br /&gt;
== Entropy ==&lt;br /&gt;
&lt;br /&gt;
Entropy is the measure of &amp;quot;randomness&amp;quot; in a sequence of bits. Different sources have different entropy. For example, a physical process in nature may have 100% entropy which appears purely random. On the other hand, the written English language provides about 3 bits/byte (or character) which is at most 38%. Some estimates have shown English characters provide only 1 bit/byte (or 12%). Other sources used as a random stream will have different estimates of entropy, and you will have to determine the quality.&lt;br /&gt;
&lt;br /&gt;
Random number generators need quality entropy for input (a ''seed'', discussed below) and must produce quality output (''quod vide''). When using OpenSSL's APIs, you will be asked to estimate entropy when seeding or reseeding (input). When estimating entropy you should error on the low side to ensure proper fitness of the generator. When receiving bytes, you will receive a code indicating the success/failure of the operation and quality of the bytes (output).&lt;br /&gt;
&lt;br /&gt;
Sometimes the operating system offers block access to hardware random number generators via &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; can be a low volume device, and could potentially block. For example, the [http://www.portwell.com.tw/download/sbc/catalog/Intel_80802_FWH.pdf Intel 82802 Firmware Hub] used with the and i840 chipset produces one byte of data in its register. At other times, &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; can be a high volume device, such as Intel's [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key Technology]. In virtualized environments, &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; might actually be a [http://wiki.qemu.org/Features-Done/VirtIORNG VirtIO RNG].&lt;br /&gt;
&lt;br /&gt;
Entropy is important for a healthy program, and you should investigate hardware modules to help acquire it, especially if poor entropy or entropy depletion are a concern. There are a number of inexpensive and high quality hardware modules on the market, including a $40UK [http://www.entropykey.co.uk EntropyKey]. There are also a number of high quality and high priced hardware modules and accelerators.&lt;br /&gt;
&lt;br /&gt;
If you lack &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt; and cannot procure a hardware random number generator, you can also consider an alternate entropy gather such as the [http://egd.sourceforge.net Entropy Gathering Daemon (EGD)]. EGD is an userspace substitute for &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;. OpenSSL provides native support for EGD via &amp;lt;tt&amp;gt;RAND_egd&amp;lt;/tt&amp;gt; to connect to the Unix domain socket, and &amp;lt;tt&amp;gt;RAND_egd_bytes&amp;lt;/tt&amp;gt; to extract bytes from the daemon.&lt;br /&gt;
&lt;br /&gt;
== Seeds ==&lt;br /&gt;
&lt;br /&gt;
Most random number generators require a seed. A seed is a secret, unpredictable sequence of bytes that is transformed and then used to set the initial state of the generator. The seed ensures that each unique instance of a generator produces a unique stream of bits. No two generators should ever produce the same sequence of random numbers, even when faced with Virtual Machine (VM) rollback attacks (which could happen accidentally by a data center operator).&lt;br /&gt;
&lt;br /&gt;
When seeding your generators, you should use at least 256 bits (32 bytes) of material. You can verify the required number of bits by grepping the source files for &amp;lt;tt&amp;gt;#define ENTROPY_NEEDED&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Initial ===&lt;br /&gt;
&lt;br /&gt;
OpenSSL will attempt to seed the random number generator automatically upon instantiation by calling &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt;.  &amp;lt;tt&amp;gt;RAND_poll&amp;lt;/tt&amp;gt; seeds the random number generator using a system-specific entropy source, which is &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt; on UNIX-like operating systems, and is a combination of [http://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx &amp;lt;tt&amp;gt;CryptGenRandom&amp;lt;/tt&amp;gt;] and other sources of entropy on Windows.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;urandom&amp;lt;/tt&amp;gt; device may lack sufficient entropy for your needs, and you might want to reseed it immediately from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;. On Unix and other operating systems that provide the block device, you can use &amp;lt;tt&amp;gt;RAND_load_file&amp;lt;/tt&amp;gt; to load directly from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int rc = RAND_load_file(&amp;quot;/dev/random&amp;quot;, 32);&lt;br /&gt;
if(rc != 32) {&lt;br /&gt;
    /* RAND_load_file failed */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reseed ===&lt;br /&gt;
&lt;br /&gt;
The OpenSSL API allows you to provide a seed and refresh the generator's state with reseeds at anytime during the program's execution. Two functions are provided for seeding and reseeding: &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; accepts a buffer and size; while &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; accepts a buffer, size, and entropy estimate in bytes. &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; will call &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; assuming 100% entropy.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; is shown below. The function is &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt;, so it &amp;lt;nowiki&amp;gt;[apparently]&amp;lt;/nowiki&amp;gt; cannot fail (or convey failures). Though the example uses the actual number of bytes written to the buffer, the entire buffer can be used to increase entropy (with hopes the unused bytes in the buffer has entropy to extract). Though you can use uninitialized bytes as input, you should not expect any entropy in the uninitialized bytes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[32];&lt;br /&gt;
int written = get_random_bytes(buffer, sizeof(buffer));&lt;br /&gt;
&lt;br /&gt;
RAND_seed(buffer, written);&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; is similar to &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; but requires an entropy estimate. The estimate should be the number of full bytes of entropy in the buffer. If you have a 32 byte buffer with about 50% entropy, you should provide 16 as the entropy estimate. &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; is also a &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt; function, so it cannot fail (or convey failures). The example also uses the actual number of bytes written to the buffer, but the entire buffer can be used to increase entropy. Note that &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; takes a &amp;lt;tt&amp;gt;double&amp;lt;/tt&amp;gt;, so be sure to '''avoid''' integer math. Otherwise, the entropy estimate calculation could result in 0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;char phrase[64];&lt;br /&gt;
int written = get_random_phrase(phrase, sizeof(phrase));&lt;br /&gt;
&lt;br /&gt;
RAND_add(phrase, written, 0.12f * written /* 12% */);&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On Windows machines, you can also use &amp;lt;tt&amp;gt;RAND_screen&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;RAND_event&amp;lt;/tt&amp;gt; on Windows machines. &amp;lt;tt&amp;gt;RAND_screen&amp;lt;/tt&amp;gt; will mix the contents of the screen into the generator. &amp;lt;tt&amp;gt;RAND_event&amp;lt;/tt&amp;gt; can be used with programs that process [http://msdn.microsoft.com/en-us/library/windows/desktop/ff381405(v=vs.85).aspx Windows Messages]. Both methods should only be used with interactive programs, and not services nor drivers.&lt;br /&gt;
&lt;br /&gt;
=== Persisting ===&lt;br /&gt;
&lt;br /&gt;
If you are worried about slow starts - or the time it takes to get the random number generator in good working order - you can write out a future seed and use it upon the next program execution. To save the future seed, use the library's &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt; function. When using &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt;, you only need to specify a filename. &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt; returns the number of bytes written or -1 to indicate bytes were written without an appropriate seed (failure).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int written = RAND_save_file(&amp;quot;prng.seed&amp;quot;);&lt;br /&gt;
if(written &amp;lt;= 0)&lt;br /&gt;
    /* RAND_save_file failed */&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At program startup, you can attempt to read the saved seed with &amp;lt;tt&amp;gt;RAND_load_file&amp;lt;/tt&amp;gt;. You can specify the number of bytes to read, or -1 to indicate the entire file should be used. The bytes read are automatically added to the generator. &amp;lt;tt&amp;gt;RAND_load_file&amp;lt;/tt&amp;gt; returns the number of bytes read.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int read = RAND_load_file(&amp;quot;prng.seed&amp;quot;, -1);&lt;br /&gt;
if(read &amp;lt;= 0)&lt;br /&gt;
    /* RAND_load_file failed */&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If possible, you should use protected storage offered by the operating system. For example, you should avoid writing the file and store the seed in the [http://developer.apple.com/library/ios/#documentation/security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html iOS Keychain], [http://developer.android.com/reference/android/security/KeyChain.html Android KeyChain], or [http://msdn.microsoft.com/en-us/library/ms995355.aspx Windows DPAPI]. When writing the seed to the filesystem, be sure to protect the the seed through the file system's permission scheme (Linux has not realized userland needs help from the kernel when storing secrets).&lt;br /&gt;
&lt;br /&gt;
== Generation ==&lt;br /&gt;
&lt;br /&gt;
After the generator has been seeded and is in good working order, you can extract bytes. You have three functions to extract bytes. First is &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; and the second is &amp;lt;tt&amp;gt;RAND_pseudo_bytes&amp;lt;/tt&amp;gt;. Both are software based and produce a pseudo-random stream. The third method is hardware based and it reuses &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Software ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; will fetch cryptographically strong random bytes. Cryptographically strong bytes are suitable for high integrity needs, such as long term key generation. If your generator is using a software algorithm, then the bytes will be pseudo-random (but still cryptographically strong). &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; returns 1 for success, and 0 otherwise. If you changed the &amp;lt;tt&amp;gt;RAND_METHOD&amp;lt;/tt&amp;gt; and it is not supported, then the function will return -1. In case of error, you can call &amp;lt;tt&amp;gt;ERR_get_error&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[128];&lt;br /&gt;
&lt;br /&gt;
int rc = RAND_bytes(buffer, sizeof(buffer));&lt;br /&gt;
unsigned long err = ERR_get_error();&lt;br /&gt;
&lt;br /&gt;
if(rc != 1) {&lt;br /&gt;
    /* RAND_bytes failed */&lt;br /&gt;
    /* `err` is valid    */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_pseudo_bytes&amp;lt;/tt&amp;gt; returns pseudo-random bytes which ''can'' be cryptographically strong. The function returns 1 if the bytes are cryptographically strong, and 0 otherwise. If your application has high integrity requirements, it should ''not'' use &amp;lt;tt&amp;gt;RAND_pseudo_bytes&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
When using &amp;lt;tt&amp;gt;RAND_pseudo_bytes&amp;lt;/tt&amp;gt;, both 0 and 1 indicate success. If you change the &amp;lt;tt&amp;gt;RAND_METHOD&amp;lt;/tt&amp;gt; and it is not supported, then the function will return -1. In case of error, you can call &amp;lt;tt&amp;gt;ERR_get_error&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[32];&lt;br /&gt;
&lt;br /&gt;
int rc = RAND_pseudo_bytes(buffer, sizeof(buffer));&lt;br /&gt;
unsigned long err = ERR_get_error();&lt;br /&gt;
&lt;br /&gt;
if(rc != 0 &amp;amp;&amp;amp; rc != 1) {&lt;br /&gt;
    /* RAND_pseudo_bytes failed */&lt;br /&gt;
    /* `err` is valid           */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hardware ===&lt;br /&gt;
&lt;br /&gt;
Hardware random number generators are almost always better to use than a software based generator. Hardware generators are often called True Random Number generators (TRNG) or Non-Deterministic Random Number Generators since they don't rely on the deterministic behavior of executing software instructions. Their bits streams are nearly always indistinguishable from random streams, and their entropy is always nearly 100%.&lt;br /&gt;
&lt;br /&gt;
Some hardware generators are easier to use than other. For example, an [http://www.entropykey.co.uk EntropyKey] will provide a driver that replenishes &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;, so an application does not have to do anything special other than reading from the device. Other generators, such as Intel's [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key], must be integrated into an application. When integrating generators using OpenSSL, you will use the library's &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
To integrate a hardware based random number generator, you should load the apporpriate &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; for the hardware based implementation. Once loaded, set the engine's &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; method as default with &amp;lt;tt&amp;gt;ENGINE_METHOD_RAND&amp;lt;/tt&amp;gt;. After you load the engine and set &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; for the hardware generator, you simply use &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; as discussed earlier. There are no special steps necessary after the configuration.&lt;br /&gt;
&lt;br /&gt;
If you have OpenSSL 1.0.1 and a machine with [http://semiaccurate.com/2012/04/23/intel-launches-ivy-bridge-amid-crushing-marketing-buzzwords/#.UTYxLlHft0w 3rd generation Core i5 or i7 processor (Ivy Bridge)], then the Intel [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key Technology] (formerly called Bull Mountain) is available to you. The hardware generator is accessed through the &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; API and wraps the &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; instruction. According to Intel, [http://software.intel.com/en-us/articles/performance-impact-of-intel-secure-key-on-openssl RDRAND-enabled version of OpenSSL outperformed the non-RDRAND version by an order of magnitude].&lt;br /&gt;
&lt;br /&gt;
To ensure &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; uses the hardware engine, you must perform three steps:&lt;br /&gt;
&lt;br /&gt;
* load the &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; engine&lt;br /&gt;
* acquire a handle to the engine&lt;br /&gt;
* set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; to the engine&lt;br /&gt;
&lt;br /&gt;
The code below shows you how to load the Intel random number generator engine and set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt;. The code is available for download at [[Media:test-rdrand.zip|test-rdrand.zip]]. While you can call &amp;lt;tt&amp;gt;ENGINE_load_builtin_engines&amp;lt;/tt&amp;gt; to make all engines available, the code below focuses on the one engine of interest and loads it via &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt;. See [http://www.openssl.org/docs/crypto/engine.html OpenSSL's engine(3)] for more details on engines, their loading, and operation.&lt;br /&gt;
&lt;br /&gt;
Casting the error code to a &amp;lt;tt&amp;gt;void*&amp;lt;/tt&amp;gt; for hexadecimal display is an abuse (hack?), but it sidesteps problems with &amp;lt;tt&amp;gt;printf&amp;lt;/tt&amp;gt; format specifiers on x86 and x64 hardware; and gives you an error that is easily consumed by &amp;lt;tt&amp;gt;openssl errstr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; 1    unsigned long err = 0;&lt;br /&gt;
 2    int rc = 0;&lt;br /&gt;
 3&lt;br /&gt;
 4    ENGINE_load_rdrand();&lt;br /&gt;
 5    ENGINE* eng = ENGINE_by_id(&amp;quot;rdrand&amp;quot;);&lt;br /&gt;
 6    err = ERR_get_error();&lt;br /&gt;
 7&lt;br /&gt;
 8    if(NULL == eng) {&lt;br /&gt;
 9        fprintf(stderr, &amp;quot;ENGINE_load_rdrand failed, err = %p\n&amp;quot;, (void*)err);&lt;br /&gt;
10        abort(); /* failed */&lt;br /&gt;
11    }&lt;br /&gt;
12  &lt;br /&gt;
13    rc = ENGINE_set_default(eng, ENGINE_METHOD_RAND);&lt;br /&gt;
14    err = ERR_get_error();&lt;br /&gt;
15&lt;br /&gt;
16    if(1 != rc) {&lt;br /&gt;
17        fprintf(stderr, &amp;quot;ENGINE_set_default failed, err = %p\n&amp;quot;, (void*)err);&lt;br /&gt;
18        abort(); /* failed */&lt;br /&gt;
19    }&lt;br /&gt;
20&lt;br /&gt;
21    /* OK to proceed */&lt;br /&gt;
22&lt;br /&gt;
23    ...&lt;br /&gt;
24    ENGINE_free(eng);&lt;br /&gt;
25    ENGINE_cleanup();&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you hardware does not support the Intel generator, you will recieve a &amp;lt;tt&amp;gt;NULL&amp;lt;/tt&amp;gt; pointer at line 5 and encounter error 0x2606c043 at line 6. The error can then be fed to &amp;lt;tt&amp;gt;openssl errstr&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ./test-rdrand.exe&lt;br /&gt;
...&lt;br /&gt;
ENGINE_load_rdrand failed, err = 0x2606c043&lt;br /&gt;
$ openssl errstr 0x2606c043&lt;br /&gt;
error:2606C043:engine routines:ENGINE_FREE_UTIL:passed a null parameter&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:test-rdrand.png|thumb|250px|right|Verifying rdrand code path]]  Line 13 attempts to set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; to that provided by the engine using &amp;lt;tt&amp;gt;ENGINE_set_default&amp;lt;/tt&amp;gt; with &amp;lt;tt&amp;gt;ENGINE_METHOD_RAND&amp;lt;/tt&amp;gt;. Upon success, OpenSSL will internally use &amp;lt;tt&amp;gt;OPENSSL_ia32_rdrand&amp;lt;/tt&amp;gt; for random number generation. To verify code correctness, simply set a breakpoint on the function and wait for the debugger to snap as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The 0x2606c043 error is actually caused by &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt;. The function will verify the capabilities of the hardware and load the generator's engine if available. &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt; is a &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt; function, so it cannot fail or cannot convey failures (which we know is incorrect from a test run). The source code can be found in &amp;lt;tt&amp;gt;eng_rdrand.c&amp;lt;/tt&amp;gt; and is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void ENGINE_load_rdrand (void)&lt;br /&gt;
{&lt;br /&gt;
    extern unsigned int OPENSSL_ia32cap_P[];&lt;br /&gt;
&lt;br /&gt;
    if (OPENSSL_ia32cap_P[1] &amp;amp; (1&amp;lt;&amp;lt;(62-32)))&lt;br /&gt;
    {&lt;br /&gt;
        ENGINE *toadd = ENGINE_rdrand();&lt;br /&gt;
        if(!toadd) return;&lt;br /&gt;
        ENGINE_add(toadd);&lt;br /&gt;
        ENGINE_free(toadd);&lt;br /&gt;
        ERR_clear_error();&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to [http://software.intel.com/en-us/articles/performance-impact-of-intel-secure-key-on-openssl Intel documentation], the random number generator does not need to be seeded via the &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; function because the generator is self-seeding. For optimal performance, code that is aware of the underlying random engine can forgo gathering entropy.&lt;br /&gt;
&lt;br /&gt;
Additionally (or more importantly), the following will not cause a crash when using the hardware random number generator (and it fails silently so all looks good from outside the fishbowl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/* Bad - don't do this in production */&lt;br /&gt;
byte seed[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };&lt;br /&gt;
RAND_seed(seed, sizeof(seed));&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, you can test if your Mac OS X system has &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; available with the following (thanks to Dave Zarzycki):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$ sysctl hw.optional.rdrand&lt;br /&gt;
hw.optional.rdrand: 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On Linux, you can &amp;lt;tt&amp;gt;cat&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;cpuinfo&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ cat /proc/cpuinfo | grep -i rdrand&lt;br /&gt;
rdrand	: 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
&lt;br /&gt;
Two miscellaneous items remaining are generator cleanup  and status. &amp;lt;tt&amp;gt;RAND_cleanup&amp;lt;/tt&amp;gt; securely erases the memory used by the random number generator.&lt;br /&gt;
&lt;br /&gt;
You can query the generator's state with &amp;lt;tt&amp;gt;RAND_status&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;RAND_status&amp;lt;/tt&amp;gt; returns 1 if the generator is in good working order. If your generator is not in good working order, you should reseed it with at least 256 bits (32 bytes) of entropy. The function [http://www.mail-archive.com/openssl-dev@openssl.org/msg04212.html purposefully hides the number of bytes needed].&lt;br /&gt;
&lt;br /&gt;
== FIPS Mode ==&lt;br /&gt;
&lt;br /&gt;
FIPS mode is a special mode of operation which specifies the library should operate according to the security policies and procedures specified in [http://csrc.nist.gov/publications/fips/fips140-2/fips1402.pdf FIPS 140-2]. The mode requires use of the FIPS Capable OpenSSL library, and must be enabled with a call to &amp;lt;tt&amp;gt;FIPS_mode_set&amp;lt;/tt&amp;gt;. Once in FIPS mode, a ''default DRBG'' is used as specified in [http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf SP800-90].&lt;br /&gt;
&lt;br /&gt;
The default DRBG is 256-bit CTR AES using a derivation function, and is decided by the application and not the library module. In the case of an OpenSSL application it is specified in &amp;lt;tt&amp;gt;rand_lib.c&amp;lt;/tt&amp;gt; via the &amp;lt;tt&amp;gt;OPENSSL_DRBG_DEFAULT_TYPE&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;OPENSSL_DRBG_DEFAULT_FLAGS&amp;lt;/tt&amp;gt; preprocessor macros to allow them to be overridden by local compilation options or at runtime.&lt;br /&gt;
&lt;br /&gt;
To use the FIPS random number generator, simply use &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; as described earlier. Note that the call to &amp;lt;tt&amp;gt;FIPS_mode_set&amp;lt;/tt&amp;gt; must succeed in order to operate in FIPS 140 mode.&lt;br /&gt;
&lt;br /&gt;
== Thread Safety ==&lt;br /&gt;
&lt;br /&gt;
The random number generators (among other parts of OpenSSL) are not thread safe by default. To ensure thread safety, you must call &amp;lt;tt&amp;gt;[https://www.openssl.org/docs/crypto/threads.html CRYPTO_set_locking_callback]&amp;lt;/tt&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Talk:Random_Numbers&amp;diff=203</id>
		<title>Talk:Random Numbers</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Talk:Random_Numbers&amp;diff=203"/>
		<updated>2013-03-08T03:00:54Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: void * casting hack?&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== void * casting hack? ==&lt;br /&gt;
&lt;br /&gt;
The thing about casting to void* and using &amp;quot;%p&amp;quot; just to print an unsigned long in hex seemed a little odd to me.  Am I missing something as to why &amp;quot;0x%lx&amp;quot; isn't a viable option?&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 03:00, 8 March 2013 (UTC)&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Random_Numbers&amp;diff=202</id>
		<title>Random Numbers</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Random_Numbers&amp;diff=202"/>
		<updated>2013-03-08T02:56:10Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Thread Safety */ random number generators are not the only non-threadsafe part of OpenSSL (e. g. all error handling is non-threadsafe since it uses a thread-local error queue), and link manpage&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Random Numbers]] are a cryptographic primitive and cornerstone to nearly all cryptographic systems. They are used in almost all areas of cryptography, from key agreement and transport to session keys for bulk encryption. A quality source of random bits and proper use of OpenSSL APIs will help ensure your program is cryptographically sound. On the other hand, a poor source of randomness or incorrect library usage could result in loss of security. This article will help you use random number generation routines correctly when programming with the OpenSSL library.&lt;br /&gt;
&lt;br /&gt;
OpenSSL provides a number of software based random number generators based on a variety of sources.  A software based random number generator creates random numbers by executing a software algorithm. There are a number of algorithms specified by a number of standard bodies including NIST, ANSI X9 committee (X9.17 and X9.31) and XXX. In addition, the library can use custom hardware if the hardware has an &amp;lt;tt&amp;gt;ENIGNE&amp;lt;/tt&amp;gt; interface. &lt;br /&gt;
&lt;br /&gt;
Good random numbers are notoriously hard to produce from deterministic processes such as a computer executing instructions. A number of cryptographic attacks have been developed because they are so hard to acquire. Especially vulnerable are headless servers, embedded devices, and mobile devices, and you may have to take extra steps to ensure an adequate supply of entropy is available. The extra steps could include ''Hedging'' on a headless server or embedded device, and ''Finger Painting'' on a mobile device. For recent attacks on low entropy devices (such as headless servers and mobile devices), see for example, ''[http://pages.cs.wisc.edu/~rist/papers/sslhedge.html When Good Randomness Goes Bad: Virtual Machine Reset Vulnerabilities and Hedging Deployed Cryptography ]'', ''[https://factorable.net/paper.html Mining Your Ps and Qs: Detection of Widespread Weak Keys in Network Devices]'', and ''[http://www.csoonline.com/article/723229/traffic-sensor-flaw-that-could-allow-driver-tracking-fixed Traffic sensor flaw that could allow driver tracking fixed]''.&lt;br /&gt;
&lt;br /&gt;
== Entropy ==&lt;br /&gt;
&lt;br /&gt;
Entropy is the measure of &amp;quot;randomness&amp;quot; in a sequence of bits. Different sources have different entropy. For example, a physical process in nature may have 100% entropy which appears purely random. On the other hand, the written English language provides about 3 bits/byte (or character) which is at most 38%. Some estimates have shown English characters provide only 1 bit/byte (or 12%). Other sources used as a random stream will have different estimates of entropy, and you will have to determine the quality.&lt;br /&gt;
&lt;br /&gt;
Random number generators need quality entropy for input (a ''seed'', discussed below) and must produce quality output (''quod vide''). When using OpenSSL's APIs, you will be asked to estimate entropy when seeding or reseeding (input). When estimating entropy you should error on the low side to ensure proper fitness of the generator. When receiving bytes, you will receive a code indicating the success/failure of the operation and quality of the bytes (output).&lt;br /&gt;
&lt;br /&gt;
Sometimes the operating system offers block access to hardware random number generators via &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; can be a low volume device, and could potentially block. For example, the [http://www.portwell.com.tw/download/sbc/catalog/Intel_80802_FWH.pdf Intel 82802 Firmware Hub] used with the and i840 chipset produces one byte of data in its register. At other times, &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; can be a high volume device, such as Intel's [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key Technology]. In virtualized environments, &amp;lt;tt&amp;gt;/dev/hwrng&amp;lt;/tt&amp;gt; might actually be a [http://wiki.qemu.org/Features-Done/VirtIORNG VirtIO RNG].&lt;br /&gt;
&lt;br /&gt;
Entropy is important for a healthy program, and you should investigate hardware modules to help acquire it, especially if poor entropy or entropy depletion are a concern. There are a number of inexpensive and high quality hardware modules on the market, including a $40UK [http://www.entropykey.co.uk EntropyKey]. There are also a number of high quality and high priced hardware modules and accelerators.&lt;br /&gt;
&lt;br /&gt;
If you lack &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt; and cannot procure a hardware random number generator, you can also consider an alternate entropy gather such as the [http://egd.sourceforge.net Entropy Gathering Daemon (EGD)]. EGD is an userspace substitute for &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;. OpenSSL provides native support for EGD via &amp;lt;tt&amp;gt;RAND_egd&amp;lt;/tt&amp;gt; to connect to the Unix domain socket, and &amp;lt;tt&amp;gt;RAND_egd_bytes&amp;lt;/tt&amp;gt; to extract bytes from the daemon.&lt;br /&gt;
&lt;br /&gt;
== Seeds ==&lt;br /&gt;
&lt;br /&gt;
Most random number generators require a seed. A seed is a secret, unpredictable sequence of bytes that is transformed and then used to set the initial state of the generator. The seed ensures that each unique instance of a generator produces a unique stream of bits. No two generators should ever produce the same sequence of random numbers, even when faced with Virtual Machine (VM) rollback attacks (which could happen accidentally by a data center operator).&lt;br /&gt;
&lt;br /&gt;
When seeding your generators, you should use at least 256 bits (32 bytes) of material. You can verify the required number of bits by grepping the source files for &amp;lt;tt&amp;gt;#define ENTROPY_NEEDED&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Initial ===&lt;br /&gt;
&lt;br /&gt;
OpenSSL will attempt to seed the random number generator automatically upon instantiation if the operating system provides &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt;. The &amp;lt;tt&amp;gt;urandom&amp;lt;/tt&amp;gt; device may lack sufficient entropy for your needs, and you might want to reseed it immediately from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;. On Unix and other operating systems that provide the block device, you can use &amp;lt;tt&amp;gt;RAND_load_file&amp;lt;/tt&amp;gt; to load directly from &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int rc = RAND_load_file(&amp;quot;/dev/random&amp;quot;, 32);&lt;br /&gt;
if(rc != 32) {&lt;br /&gt;
    /* RAND_load_file failed */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On Windows, you should seed your generator from the Wincrypt function [http://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx &amp;lt;tt&amp;gt;CryptGenRandom&amp;lt;/tt&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;HCRYPTPROV hProvider = ...;&lt;br /&gt;
BYTE seed[32];&lt;br /&gt;
BOOL rc = 0;&lt;br /&gt;
&lt;br /&gt;
rc = CryptGenRandom(hProvider, seed, sizeof(seed));&lt;br /&gt;
if(rc == FALSE) {&lt;br /&gt;
    /* CryptGenRandom failed */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RAND_seed(seed, sizeof(seed));&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reseed ===&lt;br /&gt;
&lt;br /&gt;
The OpenSSL API allows you to provide a seed and refresh the generator's state with reseeds at anytime during the program's execution. Two functions are provided for seeding and reseeding: &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; accepts a buffer and size; while &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; accepts a buffer, size, and entropy estimate in bytes. &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; will call &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; assuming 100% entropy.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; is shown below. The function is &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt;, so it &amp;lt;nowiki&amp;gt;[apparently]&amp;lt;/nowiki&amp;gt; cannot fail (or convey failures). Though the example uses the actual number of bytes written to the buffer, the entire buffer can be used to increase entropy (with hopes the unused bytes in the buffer has entropy to extract). Though you can use uninitialized bytes as input, you should not expect any entropy in the uninitialized bytes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[32];&lt;br /&gt;
int written = get_random_bytes(buffer, sizeof(buffer));&lt;br /&gt;
&lt;br /&gt;
RAND_seed(buffer, written);&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; is similar to &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; but requires an entropy estimate. The estimate should be the number of full bytes of entropy in the buffer. If you have a 32 byte buffer with about 50% entropy, you should provide 16 as the entropy estimate. &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; is also a &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt; function, so it cannot fail (or convey failures). The example also uses the actual number of bytes written to the buffer, but the entire buffer can be used to increase entropy. Note that &amp;lt;tt&amp;gt;RAND_add&amp;lt;/tt&amp;gt; takes a &amp;lt;tt&amp;gt;double&amp;lt;/tt&amp;gt;, so be sure to '''avoid''' integer math. Otherwise, the entropy estimate calculation could result in 0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;char phrase[64];&lt;br /&gt;
int written = get_random_phrase(phrase, sizeof(phrase));&lt;br /&gt;
&lt;br /&gt;
RAND_add(phrase, written, 0.12f * written /* 12% */);&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On Windows machines, you can also use &amp;lt;tt&amp;gt;RAND_screen&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;RAND_event&amp;lt;/tt&amp;gt; on Windows machines. &amp;lt;tt&amp;gt;RAND_screen&amp;lt;/tt&amp;gt; will mix the contents of the screen into the generator. &amp;lt;tt&amp;gt;RAND_event&amp;lt;/tt&amp;gt; can be used with programs that process [http://msdn.microsoft.com/en-us/library/windows/desktop/ff381405(v=vs.85).aspx Windows Messages]. Both methods should only be used with interactive programs, and not services nor drivers.&lt;br /&gt;
&lt;br /&gt;
=== Persisting ===&lt;br /&gt;
&lt;br /&gt;
If you are worried about slow starts - or the time it takes to get the random number generator in good working order - you can write out a future seed and use it upon the next program execution. To save the future seed, use the library's &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt; function. When using &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt;, you only need to specify a filename. &amp;lt;tt&amp;gt;RAND_save_file&amp;lt;/tt&amp;gt; returns the number of bytes written or -1 to indicate bytes were written without an appropriate seed (failure).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int written = RAND_save_file(&amp;quot;prng.seed&amp;quot;);&lt;br /&gt;
if(written &amp;lt;= 0)&lt;br /&gt;
    /* RAND_save_file failed */&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At program startup, you can attempt to read the saved seed with &amp;lt;tt&amp;gt;RAND_load_file&amp;lt;/tt&amp;gt;. You can specify the number of bytes to read, or -1 to indicate the entire file should be used. The bytes read are automatically added to the generator. &amp;lt;tt&amp;gt;RAND_load_file&amp;lt;/tt&amp;gt; returns the number of bytes read.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;int read = RAND_load_file(&amp;quot;prng.seed&amp;quot;, -1);&lt;br /&gt;
if(read &amp;lt;= 0)&lt;br /&gt;
    /* RAND_load_file failed */&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If possible, you should use protected storage offered by the operating system. For example, you should avoid writing the file and store the seed in the [http://developer.apple.com/library/ios/#documentation/security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html iOS Keychain], [http://developer.android.com/reference/android/security/KeyChain.html Android KeyChain], or [http://msdn.microsoft.com/en-us/library/ms995355.aspx Windows DPAPI]. When writing the seed to the filesystem, be sure to protect the the seed through the file system's permission scheme (Linux has not realized userland needs help from the kernel when storing secrets).&lt;br /&gt;
&lt;br /&gt;
== Generation ==&lt;br /&gt;
&lt;br /&gt;
After the generator has been seeded and is in good working order, you can extract bytes. You have three functions to extract bytes. First is &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; and the second is &amp;lt;tt&amp;gt;RAND_pseudo_bytes&amp;lt;/tt&amp;gt;. Both are software based and produce a pseudo-random stream. The third method is hardware based and it reuses &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Software ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; will fetch cryptographically strong random bytes. Cryptographically strong bytes are suitable for high integrity needs, such as long term key generation. If your generator is using a software algorithm, then the bytes will be pseudo-random (but still cryptographically strong). &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; returns 1 for success, and 0 otherwise. If you changed the &amp;lt;tt&amp;gt;RAND_METHOD&amp;lt;/tt&amp;gt; and it is not supported, then the function will return -1. In case of error, you can call &amp;lt;tt&amp;gt;ERR_get_error&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[128];&lt;br /&gt;
&lt;br /&gt;
int rc = RAND_bytes(buffer, sizeof(buffer));&lt;br /&gt;
unsigned long err = ERR_get_error();&lt;br /&gt;
&lt;br /&gt;
if(rc != 1) {&lt;br /&gt;
    /* RAND_bytes failed */&lt;br /&gt;
    /* `err` is valid    */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;RAND_pseudo_bytes&amp;lt;/tt&amp;gt; returns pseudo-random bytes which ''can'' be cryptographically strong. The function returns 1 if the bytes are cryptographically strong, and 0 otherwise. If your application has high integrity requirements, it should ''not'' use &amp;lt;tt&amp;gt;RAND_pseudo_bytes&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
When using &amp;lt;tt&amp;gt;RAND_pseudo_bytes&amp;lt;/tt&amp;gt;, both 0 and 1 indicate success. If you change the &amp;lt;tt&amp;gt;RAND_METHOD&amp;lt;/tt&amp;gt; and it is not supported, then the function will return -1. In case of error, you can call &amp;lt;tt&amp;gt;ERR_get_error&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;byte buffer[32];&lt;br /&gt;
&lt;br /&gt;
int rc = RAND_pseudo_bytes(buffer, sizeof(buffer));&lt;br /&gt;
unsigned long err = ERR_get_error();&lt;br /&gt;
&lt;br /&gt;
if(rc != 0 &amp;amp;&amp;amp; rc != 1) {&lt;br /&gt;
    /* RAND_pseudo_bytes failed */&lt;br /&gt;
    /* `err` is valid           */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* OK to proceed */&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hardware ===&lt;br /&gt;
&lt;br /&gt;
Hardware random number generators are almost always better to use than a software based generator. Hardware generators are often called True Random Number generators (TRNG) or Non-Deterministic Random Number Generators since they don't rely on the deterministic behavior of executing software instructions. Their bits streams are nearly always indistinguishable from random streams, and their entropy is always nearly 100%.&lt;br /&gt;
&lt;br /&gt;
Some hardware generators are easier to use than other. For example, an [http://www.entropykey.co.uk EntropyKey] will provide a driver that replenishes &amp;lt;tt&amp;gt;/dev/random&amp;lt;/tt&amp;gt;, so an application does not have to do anything special other than reading from the device. Other generators, such as Intel's [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key], must be integrated into an application. When integrating generators using OpenSSL, you will use the library's &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
To integrate a hardware based random number generator, you should load the apporpriate &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; for the hardware based implementation. Once loaded, set the engine's &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; method as default with &amp;lt;tt&amp;gt;ENGINE_METHOD_RAND&amp;lt;/tt&amp;gt;. After you load the engine and set &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; for the hardware generator, you simply use &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; as discussed earlier. There are no special steps necessary after the configuration.&lt;br /&gt;
&lt;br /&gt;
If you have OpenSSL 1.0.1 and a machine with [http://semiaccurate.com/2012/04/23/intel-launches-ivy-bridge-amid-crushing-marketing-buzzwords/#.UTYxLlHft0w 3rd generation Core i5 or i7 processor (Ivy Bridge)], then the Intel [http://software.intel.com/en-us/blogs/2012/05/14/what-is-intelr-secure-key-technology Secure Key Technology] (formerly called Bull Mountain) is available to you. The hardware generator is accessed through the &amp;lt;tt&amp;gt;ENGINE&amp;lt;/tt&amp;gt; API and wraps the &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; instruction. According to Intel, [http://software.intel.com/en-us/articles/performance-impact-of-intel-secure-key-on-openssl RDRAND-enabled version of OpenSSL outperformed the non-RDRAND version by an order of magnitude].&lt;br /&gt;
&lt;br /&gt;
To ensure &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; uses the hardware engine, you must perform three steps:&lt;br /&gt;
&lt;br /&gt;
* load the &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; engine&lt;br /&gt;
* acquire a handle to the engine&lt;br /&gt;
* set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; to the engine&lt;br /&gt;
&lt;br /&gt;
The code below shows you how to load the Intel random number generator engine and set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt;. The code is available for download at [[Media:test-rdrand.zip|test-rdrand.zip]]. While you can call &amp;lt;tt&amp;gt;ENGINE_load_builtin_engines&amp;lt;/tt&amp;gt; to make all engines available, the code below focuses on the one engine of interest and loads it via &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt;. See [http://www.openssl.org/docs/crypto/engine.html OpenSSL's engine(3)] for more details on engines, their loading, and operation.&lt;br /&gt;
&lt;br /&gt;
Casting the error code to a &amp;lt;tt&amp;gt;void*&amp;lt;/tt&amp;gt; for hexadecimal display is an abuse (hack?), but it sidesteps problems with &amp;lt;tt&amp;gt;printf&amp;lt;/tt&amp;gt; format specifiers on x86 and x64 hardware; and gives you an error that is easily consumed by &amp;lt;tt&amp;gt;openssl errstr&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; 1    unsigned long err = 0;&lt;br /&gt;
 2    int rc = 0;&lt;br /&gt;
 3&lt;br /&gt;
 4    ENGINE_load_rdrand();&lt;br /&gt;
 5    ENGINE* eng = ENGINE_by_id(&amp;quot;rdrand&amp;quot;);&lt;br /&gt;
 6    err = ERR_get_error();&lt;br /&gt;
 7&lt;br /&gt;
 8    if(NULL == eng) {&lt;br /&gt;
 9        fprintf(stderr, &amp;quot;ENGINE_load_rdrand failed, err = %p\n&amp;quot;, (void*)err);&lt;br /&gt;
10        abort(); /* failed */&lt;br /&gt;
11    }&lt;br /&gt;
12  &lt;br /&gt;
13    rc = ENGINE_set_default(eng, ENGINE_METHOD_RAND);&lt;br /&gt;
14    err = ERR_get_error();&lt;br /&gt;
15&lt;br /&gt;
16    if(1 != rc) {&lt;br /&gt;
17        fprintf(stderr, &amp;quot;ENGINE_set_default failed, err = %p\n&amp;quot;, (void*)err);&lt;br /&gt;
18        abort(); /* failed */&lt;br /&gt;
19    }&lt;br /&gt;
20&lt;br /&gt;
21    /* OK to proceed */&lt;br /&gt;
22&lt;br /&gt;
23    ...&lt;br /&gt;
24    ENGINE_free(eng);&lt;br /&gt;
25    ENGINE_cleanup();&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you hardware does not support the Intel generator, you will recieve a &amp;lt;tt&amp;gt;NULL&amp;lt;/tt&amp;gt; pointer at line 5 and encounter error 0x2606c043 at line 6. The error can then be fed to &amp;lt;tt&amp;gt;openssl errstr&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ./test-rdrand.exe&lt;br /&gt;
...&lt;br /&gt;
ENGINE_load_rdrand failed, err = 0x2606c043&lt;br /&gt;
$ openssl errstr 0x2606c043&lt;br /&gt;
error:2606C043:engine routines:ENGINE_FREE_UTIL:passed a null parameter&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:test-rdrand.png|thumb|250px|right|Verifying rdrand code path]]  Line 13 attempts to set the default &amp;lt;tt&amp;gt;RAND_method&amp;lt;/tt&amp;gt; to that provided by the engine using &amp;lt;tt&amp;gt;ENGINE_set_default&amp;lt;/tt&amp;gt; with &amp;lt;tt&amp;gt;ENGINE_METHOD_RAND&amp;lt;/tt&amp;gt;. Upon success, OpenSSL will internally use &amp;lt;tt&amp;gt;OPENSSL_ia32_rdrand&amp;lt;/tt&amp;gt; for random number generation. To verify code correctness, simply set a breakpoint on the function and wait for the debugger to snap as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The 0x2606c043 error is actually caused by &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt;. The function will verify the capabilities of the hardware and load the generator's engine if available. &amp;lt;tt&amp;gt;ENGINE_load_rdrand&amp;lt;/tt&amp;gt; is a &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt; function, so it cannot fail or cannot convey failures (which we know is incorrect from a test run). The source code can be found in &amp;lt;tt&amp;gt;eng_rdrand.c&amp;lt;/tt&amp;gt; and is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void ENGINE_load_rdrand (void)&lt;br /&gt;
{&lt;br /&gt;
    extern unsigned int OPENSSL_ia32cap_P[];&lt;br /&gt;
&lt;br /&gt;
    if (OPENSSL_ia32cap_P[1] &amp;amp; (1&amp;lt;&amp;lt;(62-32)))&lt;br /&gt;
    {&lt;br /&gt;
        ENGINE *toadd = ENGINE_rdrand();&lt;br /&gt;
        if(!toadd) return;&lt;br /&gt;
        ENGINE_add(toadd);&lt;br /&gt;
        ENGINE_free(toadd);&lt;br /&gt;
        ERR_clear_error();&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to [http://software.intel.com/en-us/articles/performance-impact-of-intel-secure-key-on-openssl Intel documentation], the random number generator does not need to be seeded via the &amp;lt;tt&amp;gt;RAND_seed&amp;lt;/tt&amp;gt; function because the generator is self-seeding. For optimal performance, code that is aware of the underlying random engine can forgo gathering entropy.&lt;br /&gt;
&lt;br /&gt;
Additionally (or more importantly), the following will not cause a crash when using the hardware random number generator (and it fails silently so all looks good from outside the fishbowl):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/* Bad - don't do this in production */&lt;br /&gt;
byte seed[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };&lt;br /&gt;
RAND_seed(seed, sizeof(seed));&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, you can test if your Mac OS X system has &amp;lt;tt&amp;gt;rdrand&amp;lt;/tt&amp;gt; available with the following (thanks to Dave Zarzycki):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$ sysctl hw.optional.rdrand&lt;br /&gt;
hw.optional.rdrand: 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On Linux, you can &amp;lt;tt&amp;gt;cat&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;cpuinfo&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ cat /proc/cpuinfo | grep -i rdrand&lt;br /&gt;
rdrand	: 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
&lt;br /&gt;
Two miscellaneous items remaining are generator cleanup  and status. &amp;lt;tt&amp;gt;RAND_cleanup&amp;lt;/tt&amp;gt; securely erases the memory used by the random number generator.&lt;br /&gt;
&lt;br /&gt;
You can query the generator's state with &amp;lt;tt&amp;gt;RAND_status&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;RAND_status&amp;lt;/tt&amp;gt; returns 1 if the generator is in good working order. If your generator is not in good working order, you should reseed it with at least 256 bits (32 bytes) of entropy. The function [http://www.mail-archive.com/openssl-dev@openssl.org/msg04212.html purposefully hides the number of bytes needed].&lt;br /&gt;
&lt;br /&gt;
== FIPS Mode ==&lt;br /&gt;
&lt;br /&gt;
FIPS mode is a special mode of operation which specifies the library should operate according to the security policies and procedures specified in [http://csrc.nist.gov/publications/fips/fips140-2/fips1402.pdf FIPS 140-2]. The mode requires use of the FIPS Capable OpenSSL library, and must be enabled with a call to &amp;lt;tt&amp;gt;FIPS_mode_set&amp;lt;/tt&amp;gt;. Once in FIPS mode, a ''default DRBG'' is used as specified in [http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf SP800-90].&lt;br /&gt;
&lt;br /&gt;
The default DRBG is 256-bit CTR AES using a derivation function, and is decided by the application and not the library module. In the case of an OpenSSL application it is specified in &amp;lt;tt&amp;gt;rand_lib.c&amp;lt;/tt&amp;gt; via the &amp;lt;tt&amp;gt;OPENSSL_DRBG_DEFAULT_TYPE&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;OPENSSL_DRBG_DEFAULT_FLAGS&amp;lt;/tt&amp;gt; preprocessor macros to allow them to be overridden by local compilation options or at runtime.&lt;br /&gt;
&lt;br /&gt;
To use the FIPS random number generator, simply use &amp;lt;tt&amp;gt;RAND_bytes&amp;lt;/tt&amp;gt; as described earlier. Note that the call to &amp;lt;tt&amp;gt;FIPS_mode_set&amp;lt;/tt&amp;gt; must succeed in order to operate in FIPS 140 mode.&lt;br /&gt;
&lt;br /&gt;
== Thread Safety ==&lt;br /&gt;
&lt;br /&gt;
The random number generators (among other parts of OpenSSL) are not thread safe by default. To ensure thread safety, you must call &amp;lt;tt&amp;gt;[https://www.openssl.org/docs/crypto/threads.html CRYPTO_set_locking_callback]&amp;lt;/tt&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Talk:Libcrypto_API&amp;diff=193</id>
		<title>Talk:Libcrypto API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Talk:Libcrypto_API&amp;diff=193"/>
		<updated>2013-03-06T01:25:56Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Return values */ agree with points about error handling&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Current Discussions==&lt;br /&gt;
&lt;br /&gt;
=== Initialization, OPENSSL_conf and engines? ===&lt;br /&gt;
&lt;br /&gt;
Should the recommended initialization code include a call to ENGINE_load_builtin_engines?  (Or to OPENSSL_config, which calls ENGINE_load_builtin_engines.)  Otherwise, the RdRand engine for getting better random numbers from newer Intel chips (as one example) won't be used.&lt;br /&gt;
&lt;br /&gt;
(My own thoughts on OpenSSL initialization are [https://en.wikibooks.org/wiki/OpenSSL/Initialization here].)&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 18:05, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Hmmm - I've not come across this as a recommendation before. What is the original source for your recommendation?&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 22:15, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
It's not from any existing documentation source, other than gleaning some information from the [https://www.openssl.org/docs/crypto/engine.html#Application_requirements engine] manpage (see &amp;quot;Automatically using builtin ENGINE implementations&amp;quot;) and the CHANGES file.  But mostly it's my own conclusion, based on reading the source code and performing experiments.&lt;br /&gt;
&lt;br /&gt;
The basic question I was trying to answer was, on modern Intel processors which support AES-NI and RdRand, is OpenSSL taking advantage of these hardware features.  The answer appears to be different for the two different features.  For AES-NI, it appears from the source code (and was recently [http://marc.info/?l=openssl-users&amp;amp;m=136209324829507&amp;amp;w=2 confirmed] on the mailing list) that AES-NI is automatically used if it is available, without needing to do anything special.&lt;br /&gt;
&lt;br /&gt;
However, for RdRand, it appears that the answer is different.  In the source code, there is a separate RdRand engine.  If the RdRand engine is not used, then the default pool implementation in md_rand.c is used, and you don't get the benefits of RdRand.&lt;br /&gt;
&lt;br /&gt;
From the section I already mentioned in the &amp;quot;engine&amp;quot; manpage, it sounded like no engines are used by default, and you must enable them by calling ENGINE_load_builtin_engines() followed by ENGINE_register_all_complete().  Although the CHANGES file partially contradicts this advice, saying:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  *) Add call to ENGINE_register_all_complete() to&lt;br /&gt;
     ENGINE_load_builtin_engines(), so some implementations get used&lt;br /&gt;
     automatically instead of needing explicit application support.&lt;br /&gt;
     [Steve Henson]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I did some experiments on a machine with RdRand.  I wrote the following little bit of code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  ENGINE *rnd = ENGINE_get_default_RAND ();&lt;br /&gt;
  if (rnd)&lt;br /&gt;
    printf (&amp;quot;default rand engine: %s\n&amp;quot;, ENGINE_get_name (rnd));&lt;br /&gt;
  else&lt;br /&gt;
    printf (&amp;quot;no default rand engine\n&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If I initialize OpenSSL the typical way:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  SSL_load_error_strings();                /* readable error messages */&lt;br /&gt;
  SSL_library_init();                      /* initialize library */&lt;br /&gt;
  OpenSSL_add_all_algorithms();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
without calling any ENGINE functions, then my little code fragment will print &amp;quot;no default rand engine&amp;quot;, indicating the implementation from md_rand.c is being used.  But if I call ENGINE_load_builtin_engines() after the other initialization functions, and before my little test, it then prints out that RdRand is the default rand engine.&lt;br /&gt;
&lt;br /&gt;
So, this is how I drew the conclusion that it's necessary to call ENGINE_load_builtin_engines() as part of your initialization, if you want to get RdRand support.&lt;br /&gt;
&lt;br /&gt;
However, this is all made a little bit trickier by the fact that OpenSSL_add_all_algorithms() can actually mean one of two vastly different things, depending on a #define at compile time.  If OPENSSL_LOAD_CONF is defined, then OpenSSL_add_all_algorithms() is really OPENSSL_add_all_algorithms_conf(), but if OPENSSL_LOAD_CONF is not defined (which is the default), then OpenSSL_add_all_algorithms() is really OPENSSL_add_all_algorithms_noconf().&lt;br /&gt;
&lt;br /&gt;
OPENSSL_add_all_algorithms_conf() is a two-line function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void OPENSSL_add_all_algorithms_conf(void)&lt;br /&gt;
	{&lt;br /&gt;
	OPENSSL_add_all_algorithms_noconf();&lt;br /&gt;
	OPENSSL_config(NULL);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So the difference is that if OPENSSL_LOAD_CONF is defined, then OPENSSL_config() is called, when it otherwise wouldn't be.  What does this have to do with RdRand?  The thing is that OPENSSL_config() calls ENGINE_load_builtin_engines().  (And then ENGINE_load_builtin_engines() in turn calls ENGINE_register_all_complete(), as mentioned in the CHANGES entry.)&lt;br /&gt;
&lt;br /&gt;
So, to get RdRand support, you can either #define OPENSSL_LOAD_CONF when building your program, or you can call either ENGINE_load_builtin_engines() or OPENSSL_config() in your initialization sequence.  However, it appears that calling ENGINE_load_builtin_engines() more than once will leak memory, so ideally you don't want to call ENGINE_load_builtin_engines() if you also plan on calling OPENSSL_config(), or if you've defined OPENSSL_LOAD_CONF.  (Of course, since it's just a small fixed-size leak at initialization, this wouldn't really be a practical problem, but still makes me feel icky.)&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 04:33, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Note that the use of OPENSSL_config() '''is''' recommended during initialisation: this is mentioned in the manual page. Currently the routines associated with OPENSSL_config() can be used for adding OIDs and configuring ENGINEs. In future it may well do much more and calling OPENSSL_config() (or the actual conf library if finer control is needed) will automatically take advantage of that.&lt;br /&gt;
&lt;br /&gt;
Here's an example of what I mean. Suppose you have a user who wants to do something weird with an ENGINE: perhaps load an unusual one that needs various ctrls to get it to work. Maybe they want to do something peculiar like use RSA with one ENGINE and DSA with another. You'd have to delve quite deep into the way ENGINE works to support that kind of thing and would it be worth it for something hardly anyone would use?&lt;br /&gt;
&lt;br /&gt;
If instead you called OPENSSL_config() that user can just set up the config file to do what they want and the application writer doesn't have to worry about all the messy ENGINE calls.&lt;br /&gt;
&lt;br /&gt;
--[[User:Steve|Steve]] 13:37, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
So, I've added a call to OPENSSL_config() during the initialisation example. This I think covers both Steve's point above, and Patrick's concern about loading the builtin engines.&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 21:01, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Return values ===&lt;br /&gt;
&lt;br /&gt;
Note that not all of the libcrypto functions return 0 for error and 1 for success. There '''are''' exceptions which can trip up the unwary. For example if you want to check a signature with some functions you get 1 if the signature is correct, 0 if it is not correct and -1 if something bad happened like a memory allocation failure. So if you do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 if (some_verify_function())&lt;br /&gt;
    /* signature successful */&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and someone can induce the &amp;quot;something bad happened&amp;quot; condition you end up behaving as though a bad signature is good. This one cropped up in the library internals at one point and was fixed in a security release. Currently you should check the manual pages or the source to be sure. It would be '''really''' useful if the exceptions were all documented, double checking with the source.&lt;br /&gt;
&lt;br /&gt;
--[[User:Steve|Steve]] 13:57, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
I've added this to the error handling section&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 21:05, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Yeah, I've noticed this, and in my own code I've chosen to always compare OpenSSL return values against 1 explicitly.  In the spirit of &amp;quot;be bold&amp;quot;, I've gone ahead and added this as a recommendation on the page itself.  But if anyone thinks this is not a good approach, feel free to change it.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 00:37, 5 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
I'm happy with that...but now I'm wondering whether for consistency we should use this throughout the examples that we post on the wiki.&lt;br /&gt;
&lt;br /&gt;
Also, the way the page now reads it looks like we are only recommending your idiom for those functions which might return something other than 0 or 1. Would it not be better to recommend this for all functions even if they do only return 0 or 1. By getting into the habit of always checking in this way it probably means you are less likely to inadvertently go wrong.&lt;br /&gt;
&lt;br /&gt;
Finally, is it not the case that most of the time the if statement is checking of an error condition. Therefore shouldn't we write the code more like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if(1 != some_function())&lt;br /&gt;
    /* handle the error */&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
--[[User:Matt|Matt]] 22:31, 5 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
I agree on both points.  I'd meant &amp;quot;this idiom can be used to avoid having to worry about whether a particular function can return more than just 0 or 1 or not&amp;quot;, and I always use that idiom in my own code for precisely that reason.  But I don't think I made my meaning clear enough on the page.  I've rephrased it a bit now, but the phrasing still feels a bit awkward, so feel free to improve it to be clearer.&lt;br /&gt;
&lt;br /&gt;
And yes, I agree about flipping the sense of the check.  I'd just been trying to be symmetrical with the example right above it.&lt;br /&gt;
&lt;br /&gt;
But I've now flipped the sense, and I've also modified the earlier example (demonstrating &amp;quot;goto&amp;quot;) to use the &amp;quot;1 !=&amp;quot; idiom.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 01:25, 6 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
==Old Discussions==&lt;br /&gt;
&lt;br /&gt;
=== Best practices for printing errors ===&lt;br /&gt;
&lt;br /&gt;
I'm curious about the recommendation to do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 err:&lt;br /&gt;
   unsigned long errCode;&lt;br /&gt;
   while(errCode = ERR_get_error())&lt;br /&gt;
   {&lt;br /&gt;
     char *err = ERR_error_string(errCode, NULL);&lt;br /&gt;
     printf(&amp;quot;%s\n&amp;quot;, err);&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Wouldn't it be much simpler to just do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 err:&lt;br /&gt;
  ERR_print_errors_fp(stderr);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if one really does want to iterate through each line of the error queue individually, wouldn't it still be better for us to recommend using ERR_error_string_n with an explicit buffer?  ERR_error_string with a NULL argument is not thread-safe.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 18:12, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Either way does the trick, but I agree yours is simpler. I'll change it.,&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 22:16, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
ERR_print_errors_fp is the best &amp;quot;call it and forget it&amp;quot; method for errors if it&lt;br /&gt;
is appropriate to use an fp. Calling the ERR routines directly can be done but&lt;br /&gt;
it's trickier and the example given is incomplete: I'd have to check it further&lt;br /&gt;
to see how best to call all the routines. [per Steve Henson]&lt;br /&gt;
&lt;br /&gt;
--[[User:Stevem|Stevem]] 14:27, 4 March 2013 (UTC)&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Libcrypto_API&amp;diff=192</id>
		<title>Libcrypto API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Libcrypto_API&amp;diff=192"/>
		<updated>2013-03-06T01:20:44Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Error Handling */ address some issues brought up on the talk page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;OpenSSL provides two primary libraries: [[Libssl API|libssl]] and libcrypto. The libcrypto library provides the fundamental cryptographic routines used by [[Libssl API|libssl]]. You can however use libcrypto without using [[Libssl API|libssl]].&lt;br /&gt;
&lt;br /&gt;
==Getting Started==&lt;br /&gt;
&lt;br /&gt;
In order to use libcrypto it must first (typically) be initialised:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;openssl/conf.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;openssl/evp.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int arc, char *argv[])&lt;br /&gt;
 { &lt;br /&gt;
   /* Load the human readable error strings for libcrypto */&lt;br /&gt;
   ERR_load_crypto_strings();&lt;br /&gt;
 &lt;br /&gt;
   /* Load all digest and cipher algorithms */&lt;br /&gt;
   OpenSSL_add_all_algorithms();&lt;br /&gt;
 &lt;br /&gt;
   /* Load config file, and other important initialisation */&lt;br /&gt;
   OPENSSL_config(NULL);&lt;br /&gt;
 &lt;br /&gt;
   /* ... Do some crypto stuff here ... */&lt;br /&gt;
 &lt;br /&gt;
   /* Clean up */&lt;br /&gt;
 &lt;br /&gt;
   /* Removes all digests and ciphers */&lt;br /&gt;
   EVP_cleanup();&lt;br /&gt;
 &lt;br /&gt;
   /* Remove error strings */&lt;br /&gt;
   ERR_free_strings();&lt;br /&gt;
 &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==High Level and Low Level Interfaces==&lt;br /&gt;
&lt;br /&gt;
For most uses, users should use the high level interface that is provided for performing cryptographic operations. This is known as the [[EVP]] interface (short for Envelope). This interface provides a suite of functions for performing encryption/decryption (both symmetric and asymmetric), signing/verifying, as well as generating hashes and MAC codes, across the full range of OpenSSL supported algorithms and modes. Working with the high level interface means that a lot of the complexity of performing cryptographic operations is hidden from view. A single consistent API is provided. In the event that you need to change your code to use a different algorithm (for example), then this is a simple change when using the high level interface. In addition low level issues such as padding and encryption modes are all handled for you.&lt;br /&gt;
&lt;br /&gt;
Refer to [[EVP]] for further information on the high level interface.&lt;br /&gt;
&lt;br /&gt;
In addition to the high level interface, OpenSSL also provides low level interfaces for working directly with the individual algorithms. These low level interfaces are not recommended for the novice user, but provide a degree of control that may not be possible when using only the high level interface.&lt;br /&gt;
&lt;br /&gt;
==Error Handling==&lt;br /&gt;
&lt;br /&gt;
Most OpenSSL functions will return an integer to indicate success or failure. Typically a function will return 1 on success or 0 on error. All return codes should be checked and handled as appropriate.&lt;br /&gt;
&lt;br /&gt;
It is common to see errors handled in a way similar to the following:&lt;br /&gt;
&lt;br /&gt;
   if(1 != EVP_xxx()) goto err;&lt;br /&gt;
   if(1 != EVP_yyy()) goto err;&lt;br /&gt;
 &lt;br /&gt;
   /* ... do some stuff ... */&lt;br /&gt;
 &lt;br /&gt;
 err:&lt;br /&gt;
   ERR_print_errors_fp(stderr);&lt;br /&gt;
&lt;br /&gt;
Note that not all of the libcrypto functions return 0 for error and 1 for success. There '''are''' exceptions which can trip up the unwary. For example if you want to check a signature with some functions you get 1 if the signature is correct, 0 if it is not correct and -1 if something bad happened like a memory allocation failure. So if you do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 if (some_verify_function())&lt;br /&gt;
    /* signature successful */&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and someone can induce the &amp;quot;something bad happened&amp;quot; condition you end up behaving as though a bad signature is good. This one cropped up in the library internals at one point and was fixed in a security release. Currently you should check the manual pages or the source to be sure.&lt;br /&gt;
&lt;br /&gt;
One way to avoid being bitten by this potential problem is to always use this idiom to check for errors when calling an OpenSSL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 if (1 != some_openssl_function())&lt;br /&gt;
    /* handle error */&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further libcrypto information ==&lt;br /&gt;
&lt;br /&gt;
There are a number of other pages that cover specific aspects of working with libcrypto:&lt;br /&gt;
* [[EVP|EVP High level interface for cryptographic operations]]&lt;br /&gt;
* [[Low Level Cryptographic APIs]]&lt;br /&gt;
* [[Random Numbers|Handling Random Numbers]]&lt;br /&gt;
* [[BIO|Working with BIOs]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Libssl API]]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Talk:Libcrypto_API&amp;diff=165</id>
		<title>Talk:Libcrypto API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Talk:Libcrypto_API&amp;diff=165"/>
		<updated>2013-03-05T01:48:55Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Return values */ spelling&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Current Discussions==&lt;br /&gt;
&lt;br /&gt;
=== Initialization, OPENSSL_conf and engines? ===&lt;br /&gt;
&lt;br /&gt;
Should the recommended initialization code include a call to ENGINE_load_builtin_engines?  (Or to OPENSSL_config, which calls ENGINE_load_builtin_engines.)  Otherwise, the RdRand engine for getting better random numbers from newer Intel chips (as one example) won't be used.&lt;br /&gt;
&lt;br /&gt;
(My own thoughts on OpenSSL initialization are [https://en.wikibooks.org/wiki/OpenSSL/Initialization here].)&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 18:05, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Hmmm - I've not come across this as a recommendation before. What is the original source for your recommendation?&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 22:15, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
It's not from any existing documentation source, other than gleaning some information from the [https://www.openssl.org/docs/crypto/engine.html#Application_requirements engine] manpage (see &amp;quot;Automatically using builtin ENGINE implementations&amp;quot;) and the CHANGES file.  But mostly it's my own conclusion, based on reading the source code and performing experiments.&lt;br /&gt;
&lt;br /&gt;
The basic question I was trying to answer was, on modern Intel processors which support AES-NI and RdRand, is OpenSSL taking advantage of these hardware features.  The answer appears to be different for the two different features.  For AES-NI, it appears from the source code (and was recently [http://marc.info/?l=openssl-users&amp;amp;m=136209324829507&amp;amp;w=2 confirmed] on the mailing list) that AES-NI is automatically used if it is available, without needing to do anything special.&lt;br /&gt;
&lt;br /&gt;
However, for RdRand, it appears that the answer is different.  In the source code, there is a separate RdRand engine.  If the RdRand engine is not used, then the default pool implementation in md_rand.c is used, and you don't get the benefits of RdRand.&lt;br /&gt;
&lt;br /&gt;
From the section I already mentioned in the &amp;quot;engine&amp;quot; manpage, it sounded like no engines are used by default, and you must enable them by calling ENGINE_load_builtin_engines() followed by ENGINE_register_all_complete().  Although the CHANGES file partially contradicts this advice, saying:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  *) Add call to ENGINE_register_all_complete() to&lt;br /&gt;
     ENGINE_load_builtin_engines(), so some implementations get used&lt;br /&gt;
     automatically instead of needing explicit application support.&lt;br /&gt;
     [Steve Henson]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I did some experiments on a machine with RdRand.  I wrote the following little bit of code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  ENGINE *rnd = ENGINE_get_default_RAND ();&lt;br /&gt;
  if (rnd)&lt;br /&gt;
    printf (&amp;quot;default rand engine: %s\n&amp;quot;, ENGINE_get_name (rnd));&lt;br /&gt;
  else&lt;br /&gt;
    printf (&amp;quot;no default rand engine\n&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If I initialize OpenSSL the typical way:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  SSL_load_error_strings();                /* readable error messages */&lt;br /&gt;
  SSL_library_init();                      /* initialize library */&lt;br /&gt;
  OpenSSL_add_all_algorithms();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
without calling any ENGINE functions, then my little code fragment will print &amp;quot;no default rand engine&amp;quot;, indicating the implementation from md_rand.c is being used.  But if I call ENGINE_load_builtin_engines() after the other initialization functions, and before my little test, it then prints out that RdRand is the default rand engine.&lt;br /&gt;
&lt;br /&gt;
So, this is how I drew the conclusion that it's necessary to call ENGINE_load_builtin_engines() as part of your initialization, if you want to get RdRand support.&lt;br /&gt;
&lt;br /&gt;
However, this is all made a little bit trickier by the fact that OpenSSL_add_all_algorithms() can actually mean one of two vastly different things, depending on a #define at compile time.  If OPENSSL_LOAD_CONF is defined, then OpenSSL_add_all_algorithms() is really OPENSSL_add_all_algorithms_conf(), but if OPENSSL_LOAD_CONF is not defined (which is the default), then OpenSSL_add_all_algorithms() is really OPENSSL_add_all_algorithms_noconf().&lt;br /&gt;
&lt;br /&gt;
OPENSSL_add_all_algorithms_conf() is a two-line function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void OPENSSL_add_all_algorithms_conf(void)&lt;br /&gt;
	{&lt;br /&gt;
	OPENSSL_add_all_algorithms_noconf();&lt;br /&gt;
	OPENSSL_config(NULL);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So the difference is that if OPENSSL_LOAD_CONF is defined, then OPENSSL_config() is called, when it otherwise wouldn't be.  What does this have to do with RdRand?  The thing is that OPENSSL_config() calls ENGINE_load_builtin_engines().  (And then ENGINE_load_builtin_engines() in turn calls ENGINE_register_all_complete(), as mentioned in the CHANGES entry.)&lt;br /&gt;
&lt;br /&gt;
So, to get RdRand support, you can either #define OPENSSL_LOAD_CONF when building your program, or you can call either ENGINE_load_builtin_engines() or OPENSSL_config() in your initialization sequence.  However, it appears that calling ENGINE_load_builtin_engines() more than once will leak memory, so ideally you don't want to call ENGINE_load_builtin_engines() if you also plan on calling OPENSSL_config(), or if you've defined OPENSSL_LOAD_CONF.  (Of course, since it's just a small fixed-size leak at initialization, this wouldn't really be a practical problem, but still makes me feel icky.)&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 04:33, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Note that the use of OPENSSL_config() '''is''' recommended during initialisation: this is mentioned in the manual page. Currently the routines associated with OPENSSL_config() can be used for adding OIDs and configuring ENGINEs. In future it may well do much more and calling OPENSSL_config() (or the actual conf library if finer control is needed) will automatically take advantage of that.&lt;br /&gt;
&lt;br /&gt;
Here's an example of what I mean. Suppose you have a user who wants to do something weird with an ENGINE: perhaps load an unusual one that needs various ctrls to get it to work. Maybe they want to do something peculiar like use RSA with one ENGINE and DSA with another. You'd have to delve quite deep into the way ENGINE works to support that kind of thing and would it be worth it for something hardly anyone would use?&lt;br /&gt;
&lt;br /&gt;
If instead you called OPENSSL_config() that user can just set up the config file to do what they want and the application writer doesn't have to worry about all the messy ENGINE calls.&lt;br /&gt;
&lt;br /&gt;
--[[User:Steve|Steve]] 13:37, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
So, I've added a call to OPENSSL_config() during the initialisation example. This I think covers both Steve's point above, and Patrick's concern about loading the builtin engines.&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 21:01, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Return values ===&lt;br /&gt;
&lt;br /&gt;
Note that not all of the libcrypto functions return 0 for error and 1 for success. There '''are''' exceptions which can trip up the unwary. For example if you want to check a signature with some functions you get 1 if the signature is correct, 0 if it is not correct and -1 if something bad happened like a memory allocation failure. So if you do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 if (some_verify_function())&lt;br /&gt;
    /* signature successful */&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and someone can induce the &amp;quot;something bad happened&amp;quot; condition you end up behaving as though a bad signature is good. This one cropped up in the library internals at one point and was fixed in a security release. Currently you should check the manual pages or the source to be sure. It would be '''really''' useful if the exceptions were all documented, double checking with the source.&lt;br /&gt;
&lt;br /&gt;
--[[User:Steve|Steve]] 13:57, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
I've added this to the error handling section&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 21:05, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Yeah, I've noticed this, and in my own code I've chosen to always compare OpenSSL return values against 1 explicitly.  In the spirit of &amp;quot;be bold&amp;quot;, I've gone ahead and added this as a recommendation on the page itself.  But if anyone thinks this is not a good approach, feel free to change it.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 00:37, 5 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
==Old Discussions==&lt;br /&gt;
&lt;br /&gt;
=== Best practices for printing errors ===&lt;br /&gt;
&lt;br /&gt;
I'm curious about the recommendation to do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 err:&lt;br /&gt;
   unsigned long errCode;&lt;br /&gt;
   while(errCode = ERR_get_error())&lt;br /&gt;
   {&lt;br /&gt;
     char *err = ERR_error_string(errCode, NULL);&lt;br /&gt;
     printf(&amp;quot;%s\n&amp;quot;, err);&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Wouldn't it be much simpler to just do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 err:&lt;br /&gt;
  ERR_print_errors_fp(stderr);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if one really does want to iterate through each line of the error queue individually, wouldn't it still be better for us to recommend using ERR_error_string_n with an explicit buffer?  ERR_error_string with a NULL argument is not thread-safe.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 18:12, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Either way does the trick, but I agree yours is simpler. I'll change it.,&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 22:16, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
ERR_print_errors_fp is the best &amp;quot;call it and forget it&amp;quot; method for errors if it&lt;br /&gt;
is appropriate to use an fp. Calling the ERR routines directly can be done but&lt;br /&gt;
it's trickier and the example given is incomplete: I'd have to check it further&lt;br /&gt;
to see how best to call all the routines. [per Steve Henson]&lt;br /&gt;
&lt;br /&gt;
--[[User:Stevem|Stevem]] 14:27, 4 March 2013 (UTC)&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Talk:Libcrypto_API&amp;diff=164</id>
		<title>Talk:Libcrypto API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Talk:Libcrypto_API&amp;diff=164"/>
		<updated>2013-03-05T00:37:54Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Return values */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Current Discussions==&lt;br /&gt;
&lt;br /&gt;
=== Initialization, OPENSSL_conf and engines? ===&lt;br /&gt;
&lt;br /&gt;
Should the recommended initialization code include a call to ENGINE_load_builtin_engines?  (Or to OPENSSL_config, which calls ENGINE_load_builtin_engines.)  Otherwise, the RdRand engine for getting better random numbers from newer Intel chips (as one example) won't be used.&lt;br /&gt;
&lt;br /&gt;
(My own thoughts on OpenSSL initialization are [https://en.wikibooks.org/wiki/OpenSSL/Initialization here].)&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 18:05, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Hmmm - I've not come across this as a recommendation before. What is the original source for your recommendation?&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 22:15, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
It's not from any existing documentation source, other than gleaning some information from the [https://www.openssl.org/docs/crypto/engine.html#Application_requirements engine] manpage (see &amp;quot;Automatically using builtin ENGINE implementations&amp;quot;) and the CHANGES file.  But mostly it's my own conclusion, based on reading the source code and performing experiments.&lt;br /&gt;
&lt;br /&gt;
The basic question I was trying to answer was, on modern Intel processors which support AES-NI and RdRand, is OpenSSL taking advantage of these hardware features.  The answer appears to be different for the two different features.  For AES-NI, it appears from the source code (and was recently [http://marc.info/?l=openssl-users&amp;amp;m=136209324829507&amp;amp;w=2 confirmed] on the mailing list) that AES-NI is automatically used if it is available, without needing to do anything special.&lt;br /&gt;
&lt;br /&gt;
However, for RdRand, it appears that the answer is different.  In the source code, there is a separate RdRand engine.  If the RdRand engine is not used, then the default pool implementation in md_rand.c is used, and you don't get the benefits of RdRand.&lt;br /&gt;
&lt;br /&gt;
From the section I already mentioned in the &amp;quot;engine&amp;quot; manpage, it sounded like no engines are used by default, and you must enable them by calling ENGINE_load_builtin_engines() followed by ENGINE_register_all_complete().  Although the CHANGES file partially contradicts this advice, saying:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  *) Add call to ENGINE_register_all_complete() to&lt;br /&gt;
     ENGINE_load_builtin_engines(), so some implementations get used&lt;br /&gt;
     automatically instead of needing explicit application support.&lt;br /&gt;
     [Steve Henson]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I did some experiments on a machine with RdRand.  I wrote the following little bit of code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  ENGINE *rnd = ENGINE_get_default_RAND ();&lt;br /&gt;
  if (rnd)&lt;br /&gt;
    printf (&amp;quot;default rand engine: %s\n&amp;quot;, ENGINE_get_name (rnd));&lt;br /&gt;
  else&lt;br /&gt;
    printf (&amp;quot;no default rand engine\n&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If I initialize OpenSSL the typical way:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  SSL_load_error_strings();                /* readable error messages */&lt;br /&gt;
  SSL_library_init();                      /* initialize library */&lt;br /&gt;
  OpenSSL_add_all_algorithms();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
without calling any ENGINE functions, then my little code fragment will print &amp;quot;no default rand engine&amp;quot;, indicating the implementation from md_rand.c is being used.  But if I call ENGINE_load_builtin_engines() after the other initialization functions, and before my little test, it then prints out that RdRand is the default rand engine.&lt;br /&gt;
&lt;br /&gt;
So, this is how I drew the conclusion that it's necessary to call ENGINE_load_builtin_engines() as part of your initialization, if you want to get RdRand support.&lt;br /&gt;
&lt;br /&gt;
However, this is all made a little bit trickier by the fact that OpenSSL_add_all_algorithms() can actually mean one of two vastly different things, depending on a #define at compile time.  If OPENSSL_LOAD_CONF is defined, then OpenSSL_add_all_algorithms() is really OPENSSL_add_all_algorithms_conf(), but if OPENSSL_LOAD_CONF is not defined (which is the default), then OpenSSL_add_all_algorithms() is really OPENSSL_add_all_algorithms_noconf().&lt;br /&gt;
&lt;br /&gt;
OPENSSL_add_all_algorithms_conf() is a two-line function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void OPENSSL_add_all_algorithms_conf(void)&lt;br /&gt;
	{&lt;br /&gt;
	OPENSSL_add_all_algorithms_noconf();&lt;br /&gt;
	OPENSSL_config(NULL);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So the difference is that if OPENSSL_LOAD_CONF is defined, then OPENSSL_config() is called, when it otherwise wouldn't be.  What does this have to do with RdRand?  The thing is that OPENSSL_config() calls ENGINE_load_builtin_engines().  (And then ENGINE_load_builtin_engines() in turn calls ENGINE_register_all_complete(), as mentioned in the CHANGES entry.)&lt;br /&gt;
&lt;br /&gt;
So, to get RdRand support, you can either #define OPENSSL_LOAD_CONF when building your program, or you can call either ENGINE_load_builtin_engines() or OPENSSL_config() in your initialization sequence.  However, it appears that calling ENGINE_load_builtin_engines() more than once will leak memory, so ideally you don't want to call ENGINE_load_builtin_engines() if you also plan on calling OPENSSL_config(), or if you've defined OPENSSL_LOAD_CONF.  (Of course, since it's just a small fixed-size leak at initialization, this wouldn't really be a practical problem, but still makes me feel icky.)&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 04:33, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Note that the use of OPENSSL_config() '''is''' recommended during initialisation: this is mentioned in the manual page. Currently the routines associated with OPENSSL_config() can be used for adding OIDs and configuring ENGINEs. In future it may well do much more and calling OPENSSL_config() (or the actual conf library if finer control is needed) will automatically take advantage of that.&lt;br /&gt;
&lt;br /&gt;
Here's an example of what I mean. Suppose you have a user who wants to do something weird with an ENGINE: perhaps load an unusual one that needs various ctrls to get it to work. Maybe they want to do something peculiar like use RSA with one ENGINE and DSA with another. You'd have to delve quite deep into the way ENGINE works to support that kind of thing and would it be worth it for something hardly anyone would use?&lt;br /&gt;
&lt;br /&gt;
If instead you called OPENSSL_config() that user can just set up the config file to do what they want and the application writer doesn't have to worry about all the messy ENGINE calls.&lt;br /&gt;
&lt;br /&gt;
--[[User:Steve|Steve]] 13:37, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
So, I've added a call to OPENSSL_config() during the initialisation example. This I think covers both Steve's point above, and Patrick's concern about loading the builtin engines.&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 21:01, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Return values ===&lt;br /&gt;
&lt;br /&gt;
Note that not all of the libcrypto functions return 0 for error and 1 for success. There '''are''' exceptions which can trip up the unwary. For example if you want to check a signature with some functions you get 1 if the signature is correct, 0 if it is not correct and -1 if something bad happened like a memory allocation failure. So if you do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 if (some_verify_function())&lt;br /&gt;
    /* signature successful */&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and someone can induce the &amp;quot;something bad happened&amp;quot; condition you end up behaving as though a bad signature is good. This one cropped up in the library internals at one point and was fixed in a security release. Currently you should check the manual pages or the source to be sure. It would be '''really''' useful if the exceptions were all documented, double checking with the source.&lt;br /&gt;
&lt;br /&gt;
--[[User:Steve|Steve]] 13:57, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
I've added this to the error handling section&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 21:05, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Yeah, I've noticed this, and in my own code I've chosen to always compare OpenSSL return values against 1 explicitly.  In the spirit of &amp;quot;be bold&amp;quot;, I've gone ahead and added this as a recommendation on the page itself.  But if anyone things this is not a good approach, feel free to change it.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 00:37, 5 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
==Old Discussions==&lt;br /&gt;
&lt;br /&gt;
=== Best practices for printing errors ===&lt;br /&gt;
&lt;br /&gt;
I'm curious about the recommendation to do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 err:&lt;br /&gt;
   unsigned long errCode;&lt;br /&gt;
   while(errCode = ERR_get_error())&lt;br /&gt;
   {&lt;br /&gt;
     char *err = ERR_error_string(errCode, NULL);&lt;br /&gt;
     printf(&amp;quot;%s\n&amp;quot;, err);&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Wouldn't it be much simpler to just do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 err:&lt;br /&gt;
  ERR_print_errors_fp(stderr);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if one really does want to iterate through each line of the error queue individually, wouldn't it still be better for us to recommend using ERR_error_string_n with an explicit buffer?  ERR_error_string with a NULL argument is not thread-safe.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 18:12, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Either way does the trick, but I agree yours is simpler. I'll change it.,&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 22:16, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
ERR_print_errors_fp is the best &amp;quot;call it and forget it&amp;quot; method for errors if it&lt;br /&gt;
is appropriate to use an fp. Calling the ERR routines directly can be done but&lt;br /&gt;
it's trickier and the example given is incomplete: I'd have to check it further&lt;br /&gt;
to see how best to call all the routines. [per Steve Henson]&lt;br /&gt;
&lt;br /&gt;
--[[User:Stevem|Stevem]] 14:27, 4 March 2013 (UTC)&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Libcrypto_API&amp;diff=163</id>
		<title>Libcrypto API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Libcrypto_API&amp;diff=163"/>
		<updated>2013-03-05T00:34:13Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* See also */ this link to BIO is now redundant, because there is another link at a more appropriate place on the page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;OpenSSL provides two primary libraries: [[Libssl API|libssl]] and libcrypto. The libcrypto library provides the fundamental cryptographic routines used by [[Libssl API|libssl]]. You can however use libcrypto without using [[Libssl API|libssl]].&lt;br /&gt;
&lt;br /&gt;
==Getting Started==&lt;br /&gt;
&lt;br /&gt;
In order to use libcrypto it must first (typically) be initialised:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;openssl/conf.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;openssl/evp.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int arc, char *argv[])&lt;br /&gt;
 { &lt;br /&gt;
   /* Load the human readable error strings for libcrypto */&lt;br /&gt;
   ERR_load_crypto_strings();&lt;br /&gt;
 &lt;br /&gt;
   /* Load all digest and cipher algorithms */&lt;br /&gt;
   OpenSSL_add_all_algorithms();&lt;br /&gt;
 &lt;br /&gt;
   /* Load config file, and other important initialisation */&lt;br /&gt;
   OPENSSL_config(NULL);&lt;br /&gt;
 &lt;br /&gt;
   /* ... Do some crypto stuff here ... */&lt;br /&gt;
 &lt;br /&gt;
   /* Clean up */&lt;br /&gt;
 &lt;br /&gt;
   /* Removes all digests and ciphers */&lt;br /&gt;
   EVP_cleanup();&lt;br /&gt;
 &lt;br /&gt;
   /* Remove error strings */&lt;br /&gt;
   ERR_free_strings();&lt;br /&gt;
 &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==High Level and Low Level Interfaces==&lt;br /&gt;
&lt;br /&gt;
For most uses, users should use the high level interface that is provided for performing cryptographic operations. This is known as the [[EVP]] interface (short for Envelope). This interface provides a suite of functions for performing encryption/decryption (both symmetric and asymmetric), signing/verifying, as well as generating hashes and MAC codes, across the full range of OpenSSL supported algorithms and modes. Working with the high level interface means that a lot of the complexity of performing cryptographic operations is hidden from view. A single consistent API is provided. In the event that you need to change your code to use a different algorithm (for example), then this is a simple change when using the high level interface. In addition low level issues such as padding and encryption modes are all handled for you.&lt;br /&gt;
&lt;br /&gt;
Refer to [[EVP]] for further information on the high level interface.&lt;br /&gt;
&lt;br /&gt;
In addition to the high level interface, OpenSSL also provides low level interfaces for working directly with the individual algorithms. These low level interfaces are not recommended for the novice user, but provide a degree of control that may not be possible when using only the high level interface.&lt;br /&gt;
&lt;br /&gt;
==Error Handling==&lt;br /&gt;
&lt;br /&gt;
Most OpenSSL functions will return an integer to indicate success or failure. Typically a function will return 1 on success or 0 on error. All return codes should be checked and handled as appropriate.&lt;br /&gt;
&lt;br /&gt;
It is common to see errors handled in a way similar to the following:&lt;br /&gt;
&lt;br /&gt;
   if(!EVP_xxx()) goto err;&lt;br /&gt;
   if(!EVP_yyy()) goto err;&lt;br /&gt;
 &lt;br /&gt;
   /* ... do some stuff ... */&lt;br /&gt;
 &lt;br /&gt;
 err:&lt;br /&gt;
   ERR_print_errors_fp(stderr);&lt;br /&gt;
&lt;br /&gt;
Note that not all of the libcrypto functions return 0 for error and 1 for success. There '''are''' exceptions which can trip up the unwary. For example if you want to check a signature with some functions you get 1 if the signature is correct, 0 if it is not correct and -1 if something bad happened like a memory allocation failure. So if you do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 if (some_verify_function())&lt;br /&gt;
    /* signature successful */&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and someone can induce the &amp;quot;something bad happened&amp;quot; condition you end up behaving as though a bad signature is good. This one cropped up in the library internals at one point and was fixed in a security release. Currently you should check the manual pages or the source to be sure.&lt;br /&gt;
&lt;br /&gt;
One way to be safe is to use this idiom:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 if (1 == some_verify_function())&lt;br /&gt;
    /* signature successful */&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further libcrypto information ==&lt;br /&gt;
&lt;br /&gt;
There are a number of other pages that cover specific aspects of working with libcrypto:&lt;br /&gt;
* [[EVP|EVP High level interface for cryptographic operations]]&lt;br /&gt;
* [[Low Level Cryptographic APIs]]&lt;br /&gt;
* [[Random Numbers|Handling Random Numbers]]&lt;br /&gt;
* [[BIO|Working with BIOs]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Libssl API]]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Libcrypto_API&amp;diff=162</id>
		<title>Libcrypto API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Libcrypto_API&amp;diff=162"/>
		<updated>2013-03-05T00:32:29Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Error Handling */ suggest explicitly comparing the return value against 1, which is what I do in my code&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;OpenSSL provides two primary libraries: [[Libssl API|libssl]] and libcrypto. The libcrypto library provides the fundamental cryptographic routines used by [[Libssl API|libssl]]. You can however use libcrypto without using [[Libssl API|libssl]].&lt;br /&gt;
&lt;br /&gt;
==Getting Started==&lt;br /&gt;
&lt;br /&gt;
In order to use libcrypto it must first (typically) be initialised:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;openssl/conf.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;openssl/evp.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int arc, char *argv[])&lt;br /&gt;
 { &lt;br /&gt;
   /* Load the human readable error strings for libcrypto */&lt;br /&gt;
   ERR_load_crypto_strings();&lt;br /&gt;
 &lt;br /&gt;
   /* Load all digest and cipher algorithms */&lt;br /&gt;
   OpenSSL_add_all_algorithms();&lt;br /&gt;
 &lt;br /&gt;
   /* Load config file, and other important initialisation */&lt;br /&gt;
   OPENSSL_config(NULL);&lt;br /&gt;
 &lt;br /&gt;
   /* ... Do some crypto stuff here ... */&lt;br /&gt;
 &lt;br /&gt;
   /* Clean up */&lt;br /&gt;
 &lt;br /&gt;
   /* Removes all digests and ciphers */&lt;br /&gt;
   EVP_cleanup();&lt;br /&gt;
 &lt;br /&gt;
   /* Remove error strings */&lt;br /&gt;
   ERR_free_strings();&lt;br /&gt;
 &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==High Level and Low Level Interfaces==&lt;br /&gt;
&lt;br /&gt;
For most uses, users should use the high level interface that is provided for performing cryptographic operations. This is known as the [[EVP]] interface (short for Envelope). This interface provides a suite of functions for performing encryption/decryption (both symmetric and asymmetric), signing/verifying, as well as generating hashes and MAC codes, across the full range of OpenSSL supported algorithms and modes. Working with the high level interface means that a lot of the complexity of performing cryptographic operations is hidden from view. A single consistent API is provided. In the event that you need to change your code to use a different algorithm (for example), then this is a simple change when using the high level interface. In addition low level issues such as padding and encryption modes are all handled for you.&lt;br /&gt;
&lt;br /&gt;
Refer to [[EVP]] for further information on the high level interface.&lt;br /&gt;
&lt;br /&gt;
In addition to the high level interface, OpenSSL also provides low level interfaces for working directly with the individual algorithms. These low level interfaces are not recommended for the novice user, but provide a degree of control that may not be possible when using only the high level interface.&lt;br /&gt;
&lt;br /&gt;
==Error Handling==&lt;br /&gt;
&lt;br /&gt;
Most OpenSSL functions will return an integer to indicate success or failure. Typically a function will return 1 on success or 0 on error. All return codes should be checked and handled as appropriate.&lt;br /&gt;
&lt;br /&gt;
It is common to see errors handled in a way similar to the following:&lt;br /&gt;
&lt;br /&gt;
   if(!EVP_xxx()) goto err;&lt;br /&gt;
   if(!EVP_yyy()) goto err;&lt;br /&gt;
 &lt;br /&gt;
   /* ... do some stuff ... */&lt;br /&gt;
 &lt;br /&gt;
 err:&lt;br /&gt;
   ERR_print_errors_fp(stderr);&lt;br /&gt;
&lt;br /&gt;
Note that not all of the libcrypto functions return 0 for error and 1 for success. There '''are''' exceptions which can trip up the unwary. For example if you want to check a signature with some functions you get 1 if the signature is correct, 0 if it is not correct and -1 if something bad happened like a memory allocation failure. So if you do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 if (some_verify_function())&lt;br /&gt;
    /* signature successful */&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and someone can induce the &amp;quot;something bad happened&amp;quot; condition you end up behaving as though a bad signature is good. This one cropped up in the library internals at one point and was fixed in a security release. Currently you should check the manual pages or the source to be sure.&lt;br /&gt;
&lt;br /&gt;
One way to be safe is to use this idiom:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 if (1 == some_verify_function())&lt;br /&gt;
    /* signature successful */&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further libcrypto information ==&lt;br /&gt;
&lt;br /&gt;
There are a number of other pages that cover specific aspects of working with libcrypto:&lt;br /&gt;
* [[EVP|EVP High level interface for cryptographic operations]]&lt;br /&gt;
* [[Low Level Cryptographic APIs]]&lt;br /&gt;
* [[Random Numbers|Handling Random Numbers]]&lt;br /&gt;
* [[BIO|Working with BIOs]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Libssl API]]&lt;br /&gt;
* [[BIO]]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=User:Ppelleti&amp;diff=101</id>
		<title>User:Ppelleti</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=User:Ppelleti&amp;diff=101"/>
		<updated>2013-03-04T05:34:55Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: brief info about self&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I am a software developer at [http://oblong.com/ Oblong Industries] in Los Angeles.  I use OpenSSL (and hopefully soon, OpenSSL in conjunction with [http://libevent.org/ libevent]) to secure the Plasma messaging layer which most of our products are built on.&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Talk:Libcrypto_API&amp;diff=100</id>
		<title>Talk:Libcrypto API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Talk:Libcrypto_API&amp;diff=100"/>
		<updated>2013-03-04T04:33:36Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Initialization and engines? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Initialization and engines? ==&lt;br /&gt;
&lt;br /&gt;
Should the recommended initialization code include a call to ENGINE_load_builtin_engines?  (Or to OPENSSL_config, which calls ENGINE_load_builtin_engines.)  Otherwise, the RdRand engine for getting better random numbers from newer Intel chips (as one example) won't be used.&lt;br /&gt;
&lt;br /&gt;
(My own thoughts on OpenSSL initialization are [https://en.wikibooks.org/wiki/OpenSSL/Initialization here].)&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 18:05, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Hmmm - I've not come across this as a recommendation before. What is the original source for your recommendation?&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 22:15, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
It's not from any existing documentation source, other than gleaning some information from the [https://www.openssl.org/docs/crypto/engine.html#Application_requirements engine] manpage (see &amp;quot;Automatically using builtin ENGINE implementations&amp;quot;) and the CHANGES file.  But mostly it's my own conclusion, based on reading the source code and performing experiments.&lt;br /&gt;
&lt;br /&gt;
The basic question I was trying to answer was, on modern Intel processors which support AES-NI and RdRand, is OpenSSL taking advantage of these hardware features.  The answer appears to be different for the two different features.  For AES-NI, it appears from the source code (and was recently [http://marc.info/?l=openssl-users&amp;amp;m=136209324829507&amp;amp;w=2 confirmed] on the mailing list) that AES-NI is automatically used if it is available, without needing to do anything special.&lt;br /&gt;
&lt;br /&gt;
However, for RdRand, it appears that the answer is different.  In the source code, there is a separate RdRand engine.  If the RdRand engine is not used, then the default pool implementation in md_rand.c is used, and you don't get the benefits of RdRand.&lt;br /&gt;
&lt;br /&gt;
From the section I already mentioned in the &amp;quot;engine&amp;quot; manpage, it sounded like no engines are used by default, and you must enable them by calling ENGINE_load_builtin_engines() followed by ENGINE_register_all_complete().  Although the CHANGES file partially contradicts this advice, saying:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  *) Add call to ENGINE_register_all_complete() to&lt;br /&gt;
     ENGINE_load_builtin_engines(), so some implementations get used&lt;br /&gt;
     automatically instead of needing explicit application support.&lt;br /&gt;
     [Steve Henson]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I did some experiments on a machine with RdRand.  I wrote the following little bit of code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  ENGINE *rnd = ENGINE_get_default_RAND ();&lt;br /&gt;
  if (rnd)&lt;br /&gt;
    printf (&amp;quot;default rand engine: %s\n&amp;quot;, ENGINE_get_name (rnd));&lt;br /&gt;
  else&lt;br /&gt;
    printf (&amp;quot;no default rand engine\n&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If I initialize OpenSSL the typical way:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  SSL_load_error_strings();                /* readable error messages */&lt;br /&gt;
  SSL_library_init();                      /* initialize library */&lt;br /&gt;
  OpenSSL_add_all_algorithms();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
without calling any ENGINE functions, then my little code fragment will print &amp;quot;no default rand engine&amp;quot;, indicating the implementation from md_rand.c is being used.  But if I call ENGINE_load_builtin_engines() after the other initialization functions, and before my little test, it then prints out that RdRand is the default rand engine.&lt;br /&gt;
&lt;br /&gt;
So, this is how I drew the conclusion that it's necessary to call ENGINE_load_builtin_engines() as part of your initialization, if you want to get RdRand support.&lt;br /&gt;
&lt;br /&gt;
However, this is all made a little bit trickier by the fact that OpenSSL_add_all_algorithms() can actually mean one of two vastly different things, depending on a #define at compile time.  If OPENSSL_LOAD_CONF is defined, then OpenSSL_add_all_algorithms() is really OPENSSL_add_all_algorithms_conf(), but if OPENSSL_LOAD_CONF is not defined (which is the default), then OpenSSL_add_all_algorithms() is really OPENSSL_add_all_algorithms_noconf().&lt;br /&gt;
&lt;br /&gt;
OPENSSL_add_all_algorithms_conf() is a two-line function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void OPENSSL_add_all_algorithms_conf(void)&lt;br /&gt;
	{&lt;br /&gt;
	OPENSSL_add_all_algorithms_noconf();&lt;br /&gt;
	OPENSSL_config(NULL);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So the difference is that if OPENSSL_LOAD_CONF is defined, then OPENSSL_config() is called, when it otherwise wouldn't be.  What does this have to do with RdRand?  The thing is that OPENSSL_config() calls ENGINE_load_builtin_engines().  (And then ENGINE_load_builtin_engines() in turn calls ENGINE_register_all_complete(), as mentioned in the CHANGES entry.)&lt;br /&gt;
&lt;br /&gt;
So, to get RdRand support, you can either #define OPENSSL_LOAD_CONF when building your program, or you can call either ENGINE_load_builtin_engines() or OPENSSL_config() in your initialization sequence.  However, it appears that calling ENGINE_load_builtin_engines() more than once will leak memory, so ideally you don't want to call ENGINE_load_builtin_engines() if you also plan on calling OPENSSL_config(), or if you've defined OPENSSL_LOAD_CONF.  (Of course, since it's just a small fixed-size leak at initialization, this wouldn't really be a practical problem, but still makes me feel icky.)&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 04:33, 4 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Best practices for printing errors ==&lt;br /&gt;
&lt;br /&gt;
I'm curious about the recommendation to do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 err:&lt;br /&gt;
   unsigned long errCode;&lt;br /&gt;
   while(errCode = ERR_get_error())&lt;br /&gt;
   {&lt;br /&gt;
     char *err = ERR_error_string(errCode, NULL);&lt;br /&gt;
     printf(&amp;quot;%s\n&amp;quot;, err);&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Wouldn't it be much simpler to just do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 err:&lt;br /&gt;
  ERR_print_errors_fp(stderr);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if one really does want to iterate through each line of the error queue individually, wouldn't it still be better for us to recommend using ERR_error_string_n with an explicit buffer?  ERR_error_string with a NULL argument is not thread-safe.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 18:12, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
Either way does the trick, but I agree yours is simpler. I'll change it.,&lt;br /&gt;
&lt;br /&gt;
--[[User:Matt|Matt]] 22:16, 3 March 2013 (UTC)&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Hostname_validation&amp;diff=92</id>
		<title>Hostname validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Hostname_validation&amp;diff=92"/>
		<updated>2013-03-03T18:50:17Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: minimal explanation of the hostname validation problem, with some links&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;One [https://crypto.stanford.edu/~dabo/pubs/abstracts/ssl-client-bugs.html very common mistake] made by users of OpenSSL is to assume that OpenSSL will validate the hostname in the server's certificate.  Currently, it does not, although a future version (1.1.0?) will include this functionality.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iSECPartners/ssl-conservatory Here is some sample code] which shows how validating the hostname can be done.  However, it does not handle wildcard certificates, so [http://archives.seul.org/libevent/users/Feb-2013/msg00043.html borrowing some code from cURL] might be one way to go.&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Diffie-Hellman_parameters&amp;diff=91</id>
		<title>Diffie-Hellman parameters</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Diffie-Hellman_parameters&amp;diff=91"/>
		<updated>2013-03-03T18:36:24Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: copy the content I wrote from the OpenSSL wikibook&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;To use [https://en.wikipedia.org/wiki/Perfect_forward_secrecy perfect forward secrecy] cipher suites, you must set up Diffie-Hellman parameters (on the server side), or the PFS cipher suites will be silently ignored.&lt;br /&gt;
&lt;br /&gt;
== Diffie-Hellman ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;SSL_CTX_set_tmp_dh&amp;lt;/code&amp;gt; is used to set the Diffie-Hellman parameters for a context.  One of the easiest ways to get Diffie-Hellman parameters to use with this function is to generate random Diffie-Hellman parameters with the [https://www.openssl.org/docs/apps/dhparam.html dhparam] command-line program with the &amp;lt;code&amp;gt;-C&amp;lt;/code&amp;gt; option, and embed the resulting code fragment in your program.  For example, &amp;lt;code&amp;gt;openssl dhparam -C 2236&amp;lt;/code&amp;gt; might result in:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifndef HEADER_DH_H&lt;br /&gt;
#include &amp;lt;openssl/dh.h&amp;gt;&lt;br /&gt;
#endif&lt;br /&gt;
DH *get_dh2236()&lt;br /&gt;
	{&lt;br /&gt;
	static unsigned char dh2236_p[]={&lt;br /&gt;
		0x0F,0x52,0xE5,0x24,0xF5,0xFA,0x9D,0xDC,0xC6,0xAB,0xE6,0x04,&lt;br /&gt;
		0xE4,0x20,0x89,0x8A,0xB4,0xBF,0x27,0xB5,0x4A,0x95,0x57,0xA1,&lt;br /&gt;
		0x06,0xE7,0x30,0x73,0x83,0x5E,0xC9,0x23,0x11,0xED,0x42,0x45,&lt;br /&gt;
		0xAC,0x49,0xD3,0xE3,0xF3,0x34,0x73,0xC5,0x7D,0x00,0x3C,0x86,&lt;br /&gt;
		0x63,0x74,0xE0,0x75,0x97,0x84,0x1D,0x0B,0x11,0xDA,0x04,0xD0,&lt;br /&gt;
		0xFE,0x4F,0xB0,0x37,0xDF,0x57,0x22,0x2E,0x96,0x42,0xE0,0x7C,&lt;br /&gt;
		0xD7,0x5E,0x46,0x29,0xAF,0xB1,0xF4,0x81,0xAF,0xFC,0x9A,0xEF,&lt;br /&gt;
		0xFA,0x89,0x9E,0x0A,0xFB,0x16,0xE3,0x8F,0x01,0xA2,0xC8,0xDD,&lt;br /&gt;
		0xB4,0x47,0x12,0xF8,0x29,0x09,0x13,0x6E,0x9D,0xA8,0xF9,0x5D,&lt;br /&gt;
		0x08,0x00,0x3A,0x8C,0xA7,0xFF,0x6C,0xCF,0xE3,0x7C,0x3B,0x6B,&lt;br /&gt;
		0xB4,0x26,0xCC,0xDA,0x89,0x93,0x01,0x73,0xA8,0x55,0x3E,0x5B,&lt;br /&gt;
		0x77,0x25,0x8F,0x27,0xA3,0xF1,0xBF,0x7A,0x73,0x1F,0x85,0x96,&lt;br /&gt;
		0x0C,0x45,0x14,0xC1,0x06,0xB7,0x1C,0x75,0xAA,0x10,0xBC,0x86,&lt;br /&gt;
		0x98,0x75,0x44,0x70,0xD1,0x0F,0x20,0xF4,0xAC,0x4C,0xB3,0x88,&lt;br /&gt;
		0x16,0x1C,0x7E,0xA3,0x27,0xE4,0xAD,0xE1,0xA1,0x85,0x4F,0x1A,&lt;br /&gt;
		0x22,0x0D,0x05,0x42,0x73,0x69,0x45,0xC9,0x2F,0xF7,0xC2,0x48,&lt;br /&gt;
		0xE3,0xCE,0x9D,0x74,0x58,0x53,0xE7,0xA7,0x82,0x18,0xD9,0x3D,&lt;br /&gt;
		0xAF,0xAB,0x40,0x9F,0xAA,0x4C,0x78,0x0A,0xC3,0x24,0x2D,0xDB,&lt;br /&gt;
		0x12,0xA9,0x54,0xE5,0x47,0x87,0xAC,0x52,0xFE,0xE8,0x3D,0x0B,&lt;br /&gt;
		0x56,0xED,0x9C,0x9F,0xFF,0x39,0xE5,0xE5,0xBF,0x62,0x32,0x42,&lt;br /&gt;
		0x08,0xAE,0x6A,0xED,0x88,0x0E,0xB3,0x1A,0x4C,0xD3,0x08,0xE4,&lt;br /&gt;
		0xC4,0xAA,0x2C,0xCC,0xB1,0x37,0xA5,0xC1,0xA9,0x64,0x7E,0xEB,&lt;br /&gt;
		0xF9,0xD3,0xF5,0x15,0x28,0xFE,0x2E,0xE2,0x7F,0xFE,0xD9,0xB9,&lt;br /&gt;
		0x38,0x42,0x57,0x03,&lt;br /&gt;
		};&lt;br /&gt;
	static unsigned char dh2236_g[]={&lt;br /&gt;
		0x02,&lt;br /&gt;
		};&lt;br /&gt;
	DH *dh;&lt;br /&gt;
&lt;br /&gt;
	if ((dh=DH_new()) == NULL) return(NULL);&lt;br /&gt;
	dh-&amp;gt;p=BN_bin2bn(dh2236_p,sizeof(dh2236_p),NULL);&lt;br /&gt;
	dh-&amp;gt;g=BN_bin2bn(dh2236_g,sizeof(dh2236_g),NULL);&lt;br /&gt;
	if ((dh-&amp;gt;p == NULL) || (dh-&amp;gt;g == NULL))&lt;br /&gt;
		{ DH_free(dh); return(NULL); }&lt;br /&gt;
	return(dh);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which can then be used like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DH *dh = get_dh2236 ();&lt;br /&gt;
if (1 != SSL_CTX_set_tmp_dh (ctx, dh))&lt;br /&gt;
  error ();&lt;br /&gt;
DH_free (dh);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Be sure to choose a bit length [http://www.keylength.com/en/3/ appropriate to the security level you want to achieve], although keep in mind that Diffie-Hellman parameters longer than 2236 bits may be [https://bugzilla.mozilla.org/show_bug.cgi?id=636802 incompatible with older versions of NSS].&lt;br /&gt;
&lt;br /&gt;
== Elliptic curve Diffie-Hellman ==&lt;br /&gt;
&lt;br /&gt;
For elliptic curve Diffie-Hellman, you can do something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);&lt;br /&gt;
if (! ecdh)&lt;br /&gt;
  error ();&lt;br /&gt;
if (1 != SSL_CTX_set_tmp_ecdh (ctx, ecdh))&lt;br /&gt;
  error ();&lt;br /&gt;
EC_KEY_free (ecdh);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in OpenSSL 1.0.2 (not yet released, as of Feb 2013) and higher, you should be able to do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SSL_CTX_set_ecdh_auto (ctx, 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Libssl_API&amp;diff=90</id>
		<title>Libssl API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Libssl_API&amp;diff=90"/>
		<updated>2013-03-03T18:31:21Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: stub for libssl&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;libssl is the portion of OpenSSL which supports TLS, and depends on libcrypto.&lt;br /&gt;
&lt;br /&gt;
* [[Diffie-Hellman parameters]]&lt;br /&gt;
* [[Hostname validation]]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Libcrypto_API&amp;diff=89</id>
		<title>Libcrypto API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Libcrypto_API&amp;diff=89"/>
		<updated>2013-03-03T18:23:05Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* See also */ link to BIO&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;OpenSSL provides two primary libraries: [[Libssl API|libssl]] and libcrypto. The libcrypto library provides the fundamental cryptographic routines used by [[Libssl API|libssl]]. You can however use libcrypto without using [[Libssl API|libssl]].&lt;br /&gt;
&lt;br /&gt;
==Getting Started==&lt;br /&gt;
&lt;br /&gt;
In order to use libcrypto it must first (typically) be initialised:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;openssl/evp.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int arc, char *argv[])&lt;br /&gt;
 { &lt;br /&gt;
   /* Load the human readable error strings for libcrypto */&lt;br /&gt;
   ERR_load_crypto_strings();&lt;br /&gt;
 &lt;br /&gt;
   /* Load all digest and cipher algorithms */&lt;br /&gt;
   OpenSSL_add_all_algorithms();&lt;br /&gt;
 &lt;br /&gt;
   /* ... Do some crypto stuff here ... */&lt;br /&gt;
 &lt;br /&gt;
   /* Clean up */&lt;br /&gt;
 &lt;br /&gt;
   /* Removes all digests and ciphers */&lt;br /&gt;
   EVP_cleanup();&lt;br /&gt;
 &lt;br /&gt;
   /* Remove error strings */&lt;br /&gt;
   ERR_free_strings();&lt;br /&gt;
 &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==High Level and Low Level Interfaces==&lt;br /&gt;
&lt;br /&gt;
For most uses, users should use the high level interface that is provided for performing cryptographic operations. This is known as the [[EVP]] interface (short for Envelope). This interface provides a suite of functions for performing encryption/decryption (both symmetric and asymmetric), signing/verifying, as well as generating hashes and MAC codes, across the full range of OpenSSL supported algorithms and modes. Working with the high level interface means that a lot of the complexity of performing cryptographic operations is hidden from view. A single consistent API is provided. In the event that you need to change your code to use a different algorithm (for example), then this is a simple change when using the high level interface. In addition low level issues such as padding and encryption modes are all handled for you.&lt;br /&gt;
&lt;br /&gt;
Refer to [[EVP]] for further information on the high level interface.&lt;br /&gt;
&lt;br /&gt;
In addition to the high level interface, OpenSSL also provides low level interfaces for working directly with the individual algorithms. These low level interfaces are not recommended for the novice user, but provide a degree of control that may not be possible when using only the high level interface.&lt;br /&gt;
&lt;br /&gt;
==Error Handling==&lt;br /&gt;
&lt;br /&gt;
Most OpenSSL functions will return an integer to indicate success or failure. Typically a function will return 1 on success or 0 on error. All return codes should be checked and handled as appropriate.&lt;br /&gt;
&lt;br /&gt;
It is common to see errors handled in a way similar to the following:&lt;br /&gt;
&lt;br /&gt;
   if(!EVP_xxx()) goto err;&lt;br /&gt;
   if(!EVP_yyy()) goto err;&lt;br /&gt;
 &lt;br /&gt;
   /* ... do some stuff ... */&lt;br /&gt;
 &lt;br /&gt;
 err:&lt;br /&gt;
   unsigned long errCode;&lt;br /&gt;
   while(errCode = ERR_get_error())&lt;br /&gt;
   {&lt;br /&gt;
     char *err = ERR_error_string(errCode, NULL);&lt;br /&gt;
     printf(&amp;quot;%s\n&amp;quot;, err);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Libssl API]]&lt;br /&gt;
* [[BIO]]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=BIO&amp;diff=88</id>
		<title>BIO</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=BIO&amp;diff=88"/>
		<updated>2013-03-03T18:22:01Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: copied the content that I wrote about BIOs from the OpenSSL wikibook&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A BIO is an I/O stream abstraction; essentially OpenSSL's answer to the C library's &amp;lt;code&amp;gt;FILE *&amp;lt;/code&amp;gt;.  OpenSSL comes with a number of useful BIO types predefined, or you can create your own.&lt;br /&gt;
&lt;br /&gt;
BIOs come in two flavors: source/sink, or filter.  BIOs can be chained together.  Each chain always has exactly one source/sink, but can have any number (zero or more) of filters.&lt;br /&gt;
&lt;br /&gt;
Reading from a BIO can be done with [https://www.openssl.org/docs/crypto/BIO_read.html BIO_read] and &amp;lt;code&amp;gt;BIO_gets&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Writing to a BIO can be done with &amp;lt;code&amp;gt;BIO_write&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;BIO_puts&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;BIO_printf&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;BIO_vprintf&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Filter BIOs ==&lt;br /&gt;
&lt;br /&gt;
* [https://www.openssl.org/docs/crypto/BIO_f_base64.html BIO_f_base64]&lt;br /&gt;
* [https://www.openssl.org/docs/crypto/BIO_f_buffer.html BIO_f_buffer]&lt;br /&gt;
* [https://www.openssl.org/docs/crypto/BIO_f_cipher.html BIO_f_cipher]&lt;br /&gt;
* [https://www.openssl.org/docs/crypto/BIO_f_md.html BIO_f_md]&lt;br /&gt;
* [https://www.openssl.org/docs/crypto/BIO_f_ssl.html BIO_f_ssl]&lt;br /&gt;
&lt;br /&gt;
== Source/sink BIOs ==&lt;br /&gt;
&lt;br /&gt;
* [https://www.openssl.org/docs/crypto/BIO_s_accept.html BIO_s_accept]&lt;br /&gt;
* [https://www.openssl.org/docs/crypto/BIO_s_bio.html BIO_s_bio]&lt;br /&gt;
* [https://www.openssl.org/docs/crypto/BIO_s_connect.html BIO_s_connect]&lt;br /&gt;
* [https://www.openssl.org/docs/crypto/BIO_s_fd.html BIO_s_fd]&lt;br /&gt;
* [https://www.openssl.org/docs/crypto/BIO_s_file.html BIO_s_file]&lt;br /&gt;
* [https://www.openssl.org/docs/crypto/BIO_s_mem.html BIO_s_mem]&lt;br /&gt;
* [https://www.openssl.org/docs/crypto/BIO_s_null.html BIO_s_null]&lt;br /&gt;
* [https://www.openssl.org/docs/crypto/BIO_s_socket.html BIO_s_socket]&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Talk:Libcrypto_API&amp;diff=87</id>
		<title>Talk:Libcrypto API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Talk:Libcrypto_API&amp;diff=87"/>
		<updated>2013-03-03T18:12:02Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: /* Best practices for printing errors */ new section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Initialization and engines? ==&lt;br /&gt;
&lt;br /&gt;
Should the recommended initialization code include a call to ENGINE_load_builtin_engines?  (Or to OPENSSL_config, which calls ENGINE_load_builtin_engines.)  Otherwise, the RdRand engine for getting better random numbers from newer Intel chips (as one example) won't be used.&lt;br /&gt;
&lt;br /&gt;
(My own thoughts on OpenSSL initialization are [https://en.wikibooks.org/wiki/OpenSSL/Initialization here].)&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 18:05, 3 March 2013 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Best practices for printing errors ==&lt;br /&gt;
&lt;br /&gt;
I'm curious about the recommendation to do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 err:&lt;br /&gt;
   unsigned long errCode;&lt;br /&gt;
   while(errCode = ERR_get_error())&lt;br /&gt;
   {&lt;br /&gt;
     char *err = ERR_error_string(errCode, NULL);&lt;br /&gt;
     printf(&amp;quot;%s\n&amp;quot;, err);&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Wouldn't it be much simpler to just do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 err:&lt;br /&gt;
  ERR_print_errors_fp(stderr);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, if one really does want to iterate through each line of the error queue individually, wouldn't it still be better for us to recommend using ERR_error_string_n with an explicit buffer?  ERR_error_string with a NULL argument is not thread-safe.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 18:12, 3 March 2013 (UTC)&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
	<entry>
		<id>https://wiki.openssl.org/index.php?title=Talk:Libcrypto_API&amp;diff=86</id>
		<title>Talk:Libcrypto API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openssl.org/index.php?title=Talk:Libcrypto_API&amp;diff=86"/>
		<updated>2013-03-03T18:05:49Z</updated>

		<summary type="html">&lt;p&gt;Ppelleti: Initialization and engines?&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Initialization and engines? ==&lt;br /&gt;
&lt;br /&gt;
Should the recommended initialization code include a call to ENGINE_load_builtin_engines?  (Or to OPENSSL_config, which calls ENGINE_load_builtin_engines.)  Otherwise, the RdRand engine for getting better random numbers from newer Intel chips (as one example) won't be used.&lt;br /&gt;
&lt;br /&gt;
(My own thoughts on OpenSSL initialization are [https://en.wikibooks.org/wiki/OpenSSL/Initialization here].)&lt;br /&gt;
&lt;br /&gt;
--[[User:Ppelleti|Ppelleti]] 18:05, 3 March 2013 (UTC)&lt;/div&gt;</summary>
		<author><name>Ppelleti</name></author>
	</entry>
</feed>