<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE chapter SYSTEM "chapter.dtd">

<chapter>
  <header>
    <copyright>
      <year>2014</year><year>2022</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      The contents of this file are subject to the Erlang Public License,
      Version 1.1, (the "License"); you may not use this file except in
      compliance with the License. You should have received a copy of the
      Erlang Public License along with this software. If not, it can be
      retrieved online at http://www.erlang.org/.

      Software distributed under the License is distributed on an "AS IS"
      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
      the License for the specific language governing rights and limitations
      under the License.

    </legalnotice>

    <title>Algorithm Details</title>
    <prepared>Hans Nilsson</prepared>
    <docno></docno>
    <date>2018-08-22</date>
    <rev>A</rev>
    <file>algorithm_details.xml</file>
  </header>
  <p>
    This chapter describes details of algorithms in the crypto application.
  </p>
  <p>The tables only documents the supported cryptos and key lengths. The user should not draw any conclusion
  on security from the supplied tables.
  </p>

  <section>
    <title>Ciphers</title>
    <p>A <seetype marker="crypto#cipher">cipher</seetype> in the
    <seeguide marker="crypto:new_api#the-new-api">new api</seeguide>
    is categorized as either
    <seetype marker="crypto#cipher_no_iv">cipher_no_iv()</seetype>,
    <seetype marker="crypto#cipher_iv">cipher_iv()</seetype> or
    <seetype marker="crypto#cipher_aead">cipher_aead()</seetype>.
    The letters IV are short for <i>Initialization Vector</i> and
    AEAD is an abbreviation of <i>Authenticated Encryption with Associated Data</i>.
    </p>
    <p>Due to irregular naming conventions, some cipher names in the old api are
    substituted by new names in the new api. For a list of retired names, see
    <seeguide marker="crypto:new_api#retired-cipher-names">Retired cipher names</seeguide>.
    </p>
    <p>To dynamically check availability, check that the name in the <i>Cipher and Mode</i> column is present in the
    list returned by <seemfa marker="crypto#supports/1">crypto:supports(ciphers)</seemfa>.
    </p>
    
    <section>
      <title>Ciphers without an IV - cipher_no_iv()</title>
      <p>To be used with:
      </p>
      <list>
	<item><seemfa marker="crypto#crypto_one_time/4">crypto_one_time/4</seemfa></item>
	<item><seemfa marker="crypto#crypto_init/3">crypto_init/3</seemfa></item>
      </list>
      <p>The ciphers are:
      </p>
      <table>
	<row>
	  <cell><strong>Cipher and Mode</strong></cell>
	  <cell><strong>Key length</strong><br/><strong>[bytes]</strong></cell>
	  <cell><strong>Block size</strong><br/><strong>[bytes]</strong></cell>
	</row>
	<row><cell><c>aes_128_ecb</c></cell>  <cell>16</cell>      <cell>16</cell></row>
	<row><cell><c>aes_192_ecb</c></cell>  <cell>24</cell>      <cell>16</cell></row>
	<row><cell><c>aes_256_ecb</c></cell>  <cell>32</cell>      <cell>16</cell></row>
	<row><cell><c>blowfish_ecb</c></cell> <cell>16</cell>      <cell>&nbsp;8</cell></row>
	<row><cell><c>des_ecb</c></cell>      <cell>&nbsp;8</cell> <cell>&nbsp;8</cell></row>
	<row><cell><c>rc4</c></cell>          <cell>16</cell>      <cell>&nbsp;1</cell></row>
	<tcaption>Ciphers without IV</tcaption>
      </table>
    </section>

    <section>
      <title>Ciphers with an IV - cipher_iv()</title>
      <p>To be used with:
      </p>
      <list>
	<item><seemfa marker="crypto#crypto_one_time/5">crypto_one_time/5</seemfa></item>
	<item><seemfa marker="crypto#crypto_init/4">crypto_init/4</seemfa></item>
	<item><seemfa marker="crypto#crypto_dyn_iv_init/3">crypto_dyn_iv_init/3</seemfa></item>
      </list>
      <p>The ciphers are:
      </p>
      <table>
	<row>
	  <cell><strong>Cipher and Mode</strong></cell>
	  <cell><strong>Key length</strong><br/><strong>[bytes]</strong></cell>
	  <cell><strong>IV length</strong><br/><strong>[bytes]</strong></cell>
	  <cell><strong>Block size</strong><br/><strong>[bytes]</strong></cell>
	  <cell><strong>Limited to</strong><br/><strong>OpenSSL versions</strong></cell>
	</row>
	<row><cell><c>aes_128_cbc</c></cell>   <cell>16</cell>      <cell>16</cell>      <cell>16</cell>      <cell></cell></row>
	<row><cell><c>aes_192_cbc</c></cell>   <cell>24</cell>      <cell>16</cell>      <cell>16</cell>      <cell></cell></row>
	<row><cell><c>aes_256_cbc</c></cell>   <cell>32</cell>      <cell>16</cell>      <cell>16</cell>      <cell></cell></row>
	<row><cell><c>aes_128_cfb8</c></cell>  <cell>16</cell>      <cell>16</cell>      <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>aes_192_cfb8</c></cell>  <cell>24</cell>      <cell>16</cell>      <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>aes_256_cfb8</c></cell>  <cell>32</cell>      <cell>16</cell>      <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>aes_128_cfb128</c></cell><cell>16</cell>      <cell>16</cell>      <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>aes_192_cfb128</c></cell><cell>24</cell>      <cell>16</cell>      <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>aes_256_cfb128</c></cell><cell>32</cell>      <cell>16</cell>      <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>aes_128_ctr</c></cell>   <cell>16</cell>      <cell>16</cell>      <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>aes_192_ctr</c></cell>   <cell>24</cell>      <cell>16</cell>      <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>aes_256_ctr</c></cell>   <cell>32</cell>      <cell>16</cell>      <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>aes_128_ofb</c></cell>   <cell>16</cell>      <cell>16</cell>      <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>aes_192_ofb</c></cell>   <cell>24</cell>      <cell>16</cell>      <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>aes_256_ofb</c></cell>   <cell>32</cell>      <cell>16</cell>      <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>blowfish_cbc</c></cell>  <cell>16</cell>      <cell>&nbsp;8</cell> <cell>&nbsp;8</cell> <cell></cell></row>
	<row><cell><c>blowfish_cfb64</c></cell><cell>16</cell>      <cell>&nbsp;8</cell> <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>blowfish_ofb64</c></cell><cell>16</cell>      <cell>&nbsp;8</cell> <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>chacha20</c></cell>      <cell>32</cell>      <cell>16</cell>      <cell>&nbsp;1</cell> <cell>&#8805;1.1.0d</cell></row>
	<row><cell><c>des_cbc</c></cell>       <cell>&nbsp;8</cell> <cell>&nbsp;8</cell> <cell>&nbsp;8</cell> <cell></cell></row>
	<row><cell><c>des_ede3_cbc</c></cell>  <cell>24</cell>      <cell>&nbsp;8</cell> <cell>&nbsp;8</cell> <cell></cell></row>
	<row><cell><c>des_cfb</c></cell>       <cell>&nbsp;8</cell> <cell>&nbsp;8</cell> <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>des_ede3_cfb</c></cell>  <cell>24</cell>      <cell>&nbsp;8</cell> <cell>&nbsp;1</cell> <cell></cell></row>
	<row><cell><c>rc2_cbc</c></cell>       <cell>16</cell>      <cell>&nbsp;8</cell> <cell>&nbsp;8</cell> <cell></cell></row>
	<tcaption>Ciphers with IV</tcaption>
      </table>
    </section>

    <section>
      <title>Ciphers with AEAD - cipher_aead()</title>
      <p>To be used with:
      </p>
      <list>
	<item><seemfa marker="crypto#crypto_one_time_aead/6">crypto_one_time_aead/6</seemfa></item>
	<item><seemfa marker="crypto#crypto_one_time_aead/7">crypto_one_time_aead/7</seemfa></item>
      </list>
      <p>The ciphers are:
      </p>
      <table>
	<row>
	  <cell><strong>Cipher and Mode</strong></cell>
	  <cell><strong>Key length</strong><br/><strong>[bytes]</strong></cell>
	  <cell><strong>IV length</strong><br/><strong>[bytes]</strong></cell>
	  <cell><strong>AAD length</strong><br/><strong>[bytes]</strong></cell>
	  <cell><strong>Tag length</strong><br/><strong>[bytes]</strong></cell>
	  <cell><strong>Block size</strong><br/><strong>[bytes]</strong></cell>
	  <cell><strong>Limited to</strong><br/><strong>OpenSSL versions</strong></cell>
	</row>
	<row><cell><c>aes_128_ccm</c></cell> <cell>16</cell> <cell>7-13</cell> <cell>any</cell> <cell>even 4-16<br/>default: 12</cell> <cell>any</cell><cell>&#8805;1.0.1</cell></row>
	<row><cell><c>aes_192_ccm</c></cell> <cell>24</cell> <cell>7-13</cell> <cell>any</cell> <cell>even 4-16<br/>default: 12</cell> <cell>any</cell><cell>&#8805;1.0.1</cell></row>
	<row><cell><c>aes_256_ccm</c></cell> <cell>32</cell> <cell>7-13</cell> <cell>any</cell> <cell>even 4-16<br/>default: 12</cell> <cell>any</cell><cell>&#8805;1.0.1</cell></row>

	<row><cell><c>aes_128_gcm</c></cell> <cell>16</cell> <cell>&#8805;1</cell> <cell>any</cell> <cell>1-16<br/>default: 16</cell> <cell>any</cell><cell>&#8805;1.0.1</cell></row>
	<row><cell><c>aes_192_gcm</c></cell> <cell>24</cell> <cell>&#8805;1</cell> <cell>any</cell> <cell>1-16<br/>default: 16</cell> <cell>any</cell><cell>&#8805;1.0.1</cell></row>
	<row><cell><c>aes_256_gcm</c></cell> <cell>32</cell> <cell>&#8805;1</cell> <cell>any</cell> <cell>1-16<br/>default: 16</cell> <cell>any</cell><cell>&#8805;1.0.1</cell></row>

	<row><cell><c>chacha20_poly1305</c></cell><cell>32</cell> <cell>1-16</cell> <cell>any</cell> <cell>16</cell> <cell>any</cell><cell>&#8805;1.1.0</cell></row>
	<tcaption>AEAD ciphers</tcaption>
      </table>
    </section>
  </section>

  
  <section>
    <title>Message Authentication Codes (MACs)</title>
    <p>To be used in <seemfa marker="crypto#mac/4">mac/4</seemfa> and
    <seeguide marker="crypto:new_api#macs--message-authentication-codes-">related functions</seeguide>.
    </p>

    <section>
      <title>CMAC</title>
      <p>CMAC with the following ciphers are available with OpenSSL 1.0.1 or later if not disabled by configuration.
      </p>

      <p>To dynamically check availability, check that the name <c>cmac</c> is present in the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(macs)</seemfa>.
      Also check that the name in the <i>Cipher and Mode</i> column is present in the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(ciphers)</seemfa>.
      </p>
      <table>
	<row>
	   <cell><strong>Cipher and Mode</strong></cell>
	   <cell><strong>Key length</strong><br/><strong>[bytes]</strong></cell>
	   <cell><strong>Max Mac Length</strong><br/><strong>(= default length)</strong><br/><strong>[bytes]</strong></cell>
	</row>
	<row><cell><c>aes_128_cbc</c></cell>  <cell>16</cell>      <cell>16</cell></row>
	<row><cell><c>aes_192_cbc</c></cell>  <cell>24</cell>      <cell>16</cell></row>
	<row><cell><c>aes_256_cbc</c></cell>  <cell>32</cell>      <cell>16</cell></row>
	<row><cell><c>aes_128_ecb</c></cell>  <cell>16</cell>      <cell>16</cell></row>
	<row><cell><c>aes_192_ecb</c></cell>  <cell>24</cell>      <cell>16</cell></row>
	<row><cell><c>aes_256_ecb</c></cell>  <cell>32</cell>      <cell>16</cell></row>
	<row><cell><c>blowfish_cbc</c></cell> <cell>16</cell>      <cell>&nbsp;8</cell></row>
	<row><cell><c>blowfish_ecb</c></cell> <cell>16</cell>      <cell>&nbsp;8</cell></row>
	<row><cell><c>des_cbc</c></cell>      <cell>&nbsp;8</cell> <cell>&nbsp;8</cell></row>
	<row><cell><c>des_ecb</c></cell>      <cell>&nbsp;8</cell> <cell>&nbsp;8</cell></row>
	<row><cell><c>des_ede3_cbc</c></cell> <cell>24</cell>      <cell>&nbsp;8</cell></row>
	<row><cell><c>rc2_cbc</c></cell>      <cell>16</cell>      <cell>&nbsp;8</cell></row>
	<tcaption>CMAC cipher key lengths</tcaption>
      </table>
    </section>

    <section>
      <title>HMAC</title>
      <p>Available in all OpenSSL compatible with Erlang CRYPTO if not disabled by configuration.
      </p>
      <p>To dynamically check availability, check that the name <c>hmac</c> is present in the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(macs)</seemfa> and
      that the hash name is present in the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(hashs)</seemfa>.
      </p>

      <table>
	<row>
	   <cell><strong>Hash</strong></cell>
	   <cell><strong>Max Mac Length</strong><br/><strong>(= default length)</strong><br/><strong>[bytes]</strong></cell>
	</row>
	<row><cell><c>sha</c></cell>       <cell>20</cell></row>
	<row><cell><c>sha224</c></cell>    <cell>28</cell></row>
	<row><cell><c>sha256</c></cell>    <cell>32</cell></row>
	<row><cell><c>sha384</c></cell>    <cell>48</cell></row>
	<row><cell><c>sha512</c></cell>    <cell>64</cell></row>
	<row><cell><c>sha3_224</c></cell>  <cell>28</cell></row>
	<row><cell><c>sha3_256</c></cell>  <cell>32</cell></row>
	<row><cell><c>sha3_384</c></cell>  <cell>48</cell></row>
	<row><cell><c>sha3_512</c></cell>  <cell>64</cell></row>
	<row><cell><c>blake2b</c></cell>   <cell>64</cell></row>
	<row><cell><c>blake2s</c></cell>   <cell>32</cell></row>
	<row><cell><c>md4</c></cell>       <cell>16</cell></row>
	<row><cell><c>md5</c></cell>       <cell>16</cell></row>
	<row><cell><c>ripemd160</c></cell> <cell>20</cell></row>
	<tcaption>HMAC output sizes</tcaption>
      </table>
	
      
    </section>

    <section>
      <title>POLY1305</title>
      <p>POLY1305 is available with OpenSSL 1.1.1 or later if not disabled by configuration.
      </p>
      <p>To dynamically check availability, check that the name <c>poly1305</c> is present in the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(macs)</seemfa>.
      </p>
      <p>The poly1305 mac wants an 32 bytes key and produces a 16 byte MAC by default.
      </p>
    </section>

  </section>

  <section>
    <title>Hash</title>

    <p>To dynamically check availability, check that the wanted name in the <i>Names</i> column is present in the
    list returned by <seemfa marker="crypto#supports/1">crypto:supports(hashs)</seemfa>.
    </p>

    <table>
      <row><cell><strong>Type</strong></cell>
           <cell><strong>Names</strong></cell>
           <cell><strong>Limitated to</strong><br/><strong>OpenSSL versions</strong></cell>
      </row>
      <row><cell>SHA1</cell><cell>sha</cell><cell></cell></row>
      <row><cell>SHA2</cell><cell>sha224, sha256, sha384, sha512</cell><cell></cell></row>
      <row><cell>SHA3</cell><cell>sha3_224, sha3_256, sha3_384, sha3_512</cell><cell>&#8805;1.1.1</cell></row>
      <row><cell>MD4</cell><cell>md4</cell><cell></cell></row>
      <row><cell>MD5</cell><cell>md5</cell><cell></cell></row>
      <row><cell>RIPEMD</cell><cell>ripemd160</cell><cell></cell></row>
      <tcaption></tcaption>
    </table>
  </section>

  <section>
    <title>Public Key Cryptography</title>

    <section>
      <title>RSA</title>
      <p>RSA is available with all OpenSSL versions compatible with Erlang CRYPTO if not disabled by configuration.
      To dynamically check availability, check that the atom <c>rsa</c> is present in the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(public_keys)</seemfa>.
      </p>
      <warning>
	<!-- In RefMan rsa_opt(), rsa_sign_verify_opt() and User's man RSA -->
	<p>The RSA options are experimental.
	</p>
	<p>The exact set of options and there syntax <em>may</em> be changed
	without prior notice.</p>
      </warning>
      <table>
	<row><cell><strong>Option</strong></cell>
	     <cell><strong>sign/verify</strong></cell>
	     <cell><strong>public encrypt</strong><br/><strong>private decrypt</strong></cell>
	     <cell><strong>private encrypt</strong><br/><strong>public decrypt</strong></cell>
	</row>
	<row><cell>{rsa_padding,rsa_x931_padding}</cell>
             <cell>x</cell>
             <cell></cell>
             <cell>x</cell>
	</row>
	<row><cell>{rsa_padding,rsa_pkcs1_padding}</cell>
	     <cell>x</cell>
             <cell>x</cell>
             <cell>x</cell>
	</row>
	<row><cell>{rsa_padding,rsa_pkcs1_pss_padding}<br/>
	           {rsa_pss_saltlen, -2..}<br/>
		   {rsa_mgf1_md, atom()}
             </cell>
	     <cell>x (2)<br/>
	           x (2)<br/>
	           x (2)</cell>
	     <cell></cell>
             <cell></cell>
	</row>
	<row><cell>{rsa_padding,rsa_pkcs1_oaep_padding}<br/>
                   {rsa_mgf1_md, atom()}<br/>
	           {rsa_oaep_label, binary()}}<br/>
		   {rsa_oaep_md, atom()}
	     </cell>
	     <cell></cell>
             <cell>x (2)<br/>
	           x (2)<br/>
	           x (3)<br/>
	           x (3)
	     </cell>
	     <cell></cell>
	</row>
	<row><cell>{rsa_padding,rsa_no_padding}</cell>
             <cell>x (1)</cell>
	     <cell></cell>
             <cell></cell>
	</row>
	<!-- row><cell>{rsa_padding,rsa_sslv23_padding}</cell>
	     <cell></cell>
             <cell></cell>
             <cell></cell>
	</row -->
	<tcaption></tcaption>
      </table>
      <p>Notes:</p>
      <list type="ordered">
	<item>(1)  OpenSSL &#8804; 1.0.0</item>
	<item>(2)  OpenSSL &#8805; 1.0.1</item>
	<item>(3)  OpenSSL &#8805; 1.1.0</item>
      </list>
    </section>

    <section>
      <title>DSS</title>
      <p>DSS is available with OpenSSL versions compatible with Erlang CRYPTO if not disabled by configuration.
      To dynamically check availability, check that the atom <c>dss</c> is present in the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(public_keys)</seemfa>.
      </p>
    </section>

    <section>
      <title>ECDSA</title>
      <p>ECDSA is available with OpenSSL 0.9.8o or later if not disabled by configuration.
      To dynamically check availability, check that the atom <c>ecdsa</c> is present in the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(public_keys)</seemfa>.
      If the atom <c>ec_gf2m</c> also is present, the characteristic two field curves are available.
      </p>
      <p>The actual supported named curves could be checked by examining the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(curves)</seemfa>.
      </p>
    </section>

    <section>
      <title>EdDSA</title>
      <p>EdDSA is available with OpenSSL 1.1.1 or later if not disabled by configuration.
      To dynamically check availability, check that the atom <c>eddsa</c> is present in the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(public_keys)</seemfa>.
      </p>
      <p>Support for the curves ed25519 and ed448 is implemented.
      The actual supported named curves could be checked by examining the list with the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(curves)</seemfa>.
      </p>
    </section>

    <section>
      <title>Diffie-Hellman</title>
      <p>Diffie-Hellman computations are available with OpenSSL versions compatible with Erlang CRYPTO
      if not disabled by configuration.
      To dynamically check availability, check that the atom <c>dh</c> is present in the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(public_keys)</seemfa>.
      </p>
    </section>

    <section>
      <title>Elliptic Curve Diffie-Hellman</title>
      <p>Elliptic Curve Diffie-Hellman is available with OpenSSL 0.9.8o or later if not disabled by configuration.
      To dynamically check availability, check that the atom <c>ecdh</c> is present in the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(public_keys)</seemfa>.
      </p>

      <p>The Edward curves <c>x25519</c> and <c>x448</c> are supported with OpenSSL 1.1.1 or later
      if not disabled by configuration.
      </p>

      <p>The actual supported named curves could be checked by examining the
      list returned by <seemfa marker="crypto#supports/1">crypto:supports(curves)</seemfa>.
      </p>
    </section>

  </section>


</chapter>





