428 lines
18 KiB
HTML
428 lines
18 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta charset="utf-8">
|
||
|
<meta content="IE=edge" http-equiv="X-UA-Compatible">
|
||
|
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||
|
<title>Defensive Coding Guide | Defensive Coding Guide | Specific Programming Tasks | Cryptography</title>
|
||
|
|
||
|
<!-- Bootstrap -->
|
||
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
|
||
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
|
||
|
|
||
|
<!-- Overpass Font -->
|
||
|
<link rel="stylesheet" href="https://overpass-30e2.kxcdn.com/overpass.css">
|
||
|
|
||
|
<link href="../../../master/_stylesheets/asciibinder.css" rel="stylesheet" />
|
||
|
|
||
|
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
|
||
|
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
|
||
|
<!--[if lt IE 9]>
|
||
|
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
|
||
|
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
|
||
|
<![endif]-->
|
||
|
|
||
|
<link href="../../../master/_images/favicon32x32.png" rel="shortcut icon" type="text/css">
|
||
|
<!--[if IE]><link rel="shortcut icon" href="../../../master/_images/favicon.ico"><![endif]-->
|
||
|
<meta content="AsciiBinder" name="application-name">
|
||
|
</head>
|
||
|
<body>
|
||
|
<div class="navbar navbar-default" role="navigation">
|
||
|
<div class="container-fluid">
|
||
|
<div class="navbar-header">
|
||
|
<a class="navbar-brand" href="https://docs.fedoraproject.org/"><img alt="Fedora Documentation" src="../../../master/_images/fedora.svg"></a>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="container">
|
||
|
<p class="toggle-nav visible-xs pull-left">
|
||
|
<button class="btn btn-default btn-sm" type="button" data-toggle="offcanvas">Toggle nav</button>
|
||
|
</p>
|
||
|
<ol class="breadcrumb">
|
||
|
<li class="sitename">
|
||
|
<a href="../../../index.html">Home</a>
|
||
|
</li>
|
||
|
<li class="hidden-xs active">
|
||
|
<a href="../../en-US/index.html">Defensive Coding Guide </a>
|
||
|
</li>
|
||
|
<li class="hidden-xs active">
|
||
|
<a href="../../en-US/index.html">Defensive Coding Guide</a>
|
||
|
</li>
|
||
|
<li class="hidden-xs active"><a href="../../en-US/tasks/Tasks-Library_Design.html">Specific Programming Tasks</a></li>
|
||
|
<li class="hidden-xs active">
|
||
|
Cryptography
|
||
|
</li>
|
||
|
</ol>
|
||
|
<div class="row row-offcanvas row-offcanvas-left">
|
||
|
<div class="col-xs-8 col-sm-3 col-md-3 sidebar sidebar-offcanvas">
|
||
|
<ul class="nav nav-sidebar">
|
||
|
<li class="nav-header">
|
||
|
<a class="" href="#" data-toggle="collapse" data-target="#topicGroup0">
|
||
|
<span id="tgSpan0" class="fa fa-angle-down"></span>Defensive Coding Guide
|
||
|
</a>
|
||
|
<ul id="topicGroup0" class="collapse in list-unstyled">
|
||
|
<li><a class="" href="../../en-US/index.html">Book Information</a></li>
|
||
|
<li class="nav-header">
|
||
|
<a class="" href="#" data-toggle="collapse" data-target="#topicSubGroup-0-1">
|
||
|
<span id="sgSpan-0-1" class="fa fa-caret-right"></span> Programming Languages
|
||
|
</a>
|
||
|
<ul id="topicSubGroup-0-1" class="nav-tertiary list-unstyled collapse">
|
||
|
<li><a class="" href="../../en-US/programming-languages/C.html">The C Programming Language</a></li>
|
||
|
<li><a class="" href="../../en-US/programming-languages/CXX.html">The C++ Programming Language</a></li>
|
||
|
<li><a class="" href="../../en-US/programming-languages/Java.html">The Java Programming Language</a></li>
|
||
|
<li><a class="" href="../../en-US/programming-languages/Python.html">The Python Programming Language</a></li>
|
||
|
<li><a class="" href="../../en-US/programming-languages/Shell.html">Shell Programming and bash</a></li>
|
||
|
<li><a class="" href="../../en-US/programming-languages/Go.html">The Go Programming Language</a></li>
|
||
|
<li><a class="" href="../../en-US/programming-languages/Vala.html">The Vala Programming Language</a></li>
|
||
|
</ul>
|
||
|
</li>
|
||
|
<li class="nav-header">
|
||
|
<a class="" href="#" data-toggle="collapse" data-target="#topicSubGroup-0-2">
|
||
|
<span id="sgSpan-0-2" class="fa fa-caret-down"></span> Specific Programming Tasks
|
||
|
</a>
|
||
|
<ul id="topicSubGroup-0-2" class="nav-tertiary list-unstyled collapse in">
|
||
|
<li><a class="" href="../../en-US/tasks/Tasks-Library_Design.html">Library Design</a></li>
|
||
|
<li><a class="" href="../../en-US/tasks/Tasks-Descriptors.html">File Descriptor Management</a></li>
|
||
|
<li><a class="" href="../../en-US/tasks/Tasks-File_System.html">File System Manipulation</a></li>
|
||
|
<li><a class="" href="../../en-US/tasks/Tasks-Temporary_Files.html">Temporary Files</a></li>
|
||
|
<li><a class="" href="../../en-US/tasks/Tasks-Processes.html">Processes</a></li>
|
||
|
<li><a class="" href="../../en-US/tasks/Tasks-Serialization.html">Serialization and Deserialization</a></li>
|
||
|
<li><a class=" active" href="../../en-US/tasks/Tasks-Cryptography.html">Cryptography</a></li>
|
||
|
<li><a class="" href="../../en-US/tasks/Tasks-Packaging.html">RPM Packaging</a></li>
|
||
|
</ul>
|
||
|
</li>
|
||
|
<li class="nav-header">
|
||
|
<a class="" href="#" data-toggle="collapse" data-target="#topicSubGroup-0-3">
|
||
|
<span id="sgSpan-0-3" class="fa fa-caret-right"></span> Implementing Security Features
|
||
|
</a>
|
||
|
<ul id="topicSubGroup-0-3" class="nav-tertiary list-unstyled collapse">
|
||
|
<li><a class="" href="../../en-US/features/Features-Authentication.html">Authentication and Authorization</a></li>
|
||
|
<li><a class="" href="../../en-US/features/Features-TLS.html">Transport Layer Security (TLS)</a></li>
|
||
|
<li><a class="" href="../../en-US/features/Features-HSM.html">Hardware Security Modules and Smart Cards</a></li>
|
||
|
</ul>
|
||
|
</li>
|
||
|
<li><a class="" href="../../en-US/Revision_History.html">Revision History</a></li>
|
||
|
</ul>
|
||
|
</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
<div class="col-xs-12 col-sm-9 col-md-9 main">
|
||
|
<div class="page-header">
|
||
|
<h2>Cryptography</h2>
|
||
|
</div>
|
||
|
<div class="sect1">
|
||
|
<h2 id="primitives"><a class="anchor" href="#primitives"></a>Primitives</h2>
|
||
|
<div class="sectionbody">
|
||
|
<div class="paragraph">
|
||
|
<p>Choosing from the following cryptographic primitives is
|
||
|
recommended:</p>
|
||
|
</div>
|
||
|
<div class="ulist">
|
||
|
<ul>
|
||
|
<li>
|
||
|
<p>RSA with 2048-bit keys and OAEP or PSS
|
||
|
padding</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>AES-128 in CBC mode</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>AES-128 in GCM mode</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>AES-256 in CBC mode</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>AES-256 in GCM mode</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>SHA-256</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>HMAC-SHA-256</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>HMAC-SHA-1</p>
|
||
|
</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
<div class="paragraph">
|
||
|
<p>Other cryptographic algorithms can be used if they are required
|
||
|
for interoperability with existing software:</p>
|
||
|
</div>
|
||
|
<div class="ulist">
|
||
|
<ul>
|
||
|
<li>
|
||
|
<p>RSA with key sizes larger than 1024
|
||
|
and legacy padding</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>AES-192</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>3DES (triple DES, with two or three 56-bit keys),
|
||
|
but strongly discouraged</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>RC4 (but very, very strongly discouraged)</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>SHA-1</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>HMAC-MD5</p>
|
||
|
</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
<div class="admonitionblock important">
|
||
|
<table>
|
||
|
<tr>
|
||
|
<td class="icon">
|
||
|
<i class="fa icon-important" title="Important"></i>
|
||
|
</td>
|
||
|
<td class="content">
|
||
|
<div class="title">Important</div>
|
||
|
<div class="paragraph">
|
||
|
<p>These primitives are difficult to use in a secure way. Custom
|
||
|
implementation of security protocols should be avoided. For
|
||
|
protecting confidentiality and integrity of network
|
||
|
transmissions, TLS should be used (<a href="#chap-Defensive_Coding-TLS">[chap-Defensive_Coding-TLS]</a>).</p>
|
||
|
</div>
|
||
|
<div class="paragraph">
|
||
|
<p>In particular, when using AES in CBC mode, it is necessary to
|
||
|
add integrity checking by other means, preferably using
|
||
|
HMAC-SHA-256 and <strong>after</strong> encryption (that
|
||
|
is, on the encrypted cipher text). For AES in GCM mode,
|
||
|
correct construction of nonces is absolutely essential.</p>
|
||
|
</div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="sect1">
|
||
|
<h2 id="randomness"><a class="anchor" href="#randomness"></a>Randomness</h2>
|
||
|
<div class="sectionbody">
|
||
|
<div class="paragraph">
|
||
|
<p>The following facilities can be used to generate unpredictable
|
||
|
and non-repeating values. When these functions are used without
|
||
|
special safeguards, each individual random value should be at
|
||
|
least 12 bytes long.</p>
|
||
|
</div>
|
||
|
<div class="ulist">
|
||
|
<ul>
|
||
|
<li>
|
||
|
<p><code>PK11_GenerateRandom</code> in the NSS library
|
||
|
(usable for high data rates)</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p><code>RAND_bytes</code> in the OpenSSL library
|
||
|
(usable for high data rates)</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p><code>gnutls_rnd</code> in GNUTLS, with
|
||
|
<code>GNUTLS_RND_RANDOM</code> as the first argument
|
||
|
(usable for high data rates)</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p><code>java.security.SecureRandom</code> in Java
|
||
|
(usable for high data rates)</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p><code>os.urandom</code> in Python</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>The <code>getrandom</code> system call since glibc 2.25</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>The <code>getentropy</code> call since glibc 2.25</p>
|
||
|
</li>
|
||
|
<li>
|
||
|
<p>Reading from the <code>/dev/urandom</code>
|
||
|
character device</p>
|
||
|
</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
<div class="paragraph">
|
||
|
<p>All these functions should be non-blocking, and they should not
|
||
|
wait until physical randomness becomes available. (Some
|
||
|
cryptography providers for Java can cause
|
||
|
<code>java.security.SecureRandom</code> to block, however.)
|
||
|
Those functions which do not obtain all bits directly from
|
||
|
<code>/dev/urandom</code> are suitable for high data
|
||
|
rates because they do not deplete the system-wide entropy pool.</p>
|
||
|
</div>
|
||
|
<div class="admonitionblock important">
|
||
|
<table>
|
||
|
<tr>
|
||
|
<td class="icon">
|
||
|
<i class="fa icon-important" title="Important"></i>
|
||
|
</td>
|
||
|
<td class="content">
|
||
|
<div class="title">Difficult to use API</div>
|
||
|
<div class="paragraph">
|
||
|
<p>Both <code>RAND_bytes</code> and
|
||
|
<code>PK11_GenerateRandom</code> have three-state
|
||
|
return values (with conflicting meanings). Careful error
|
||
|
checking is required. Please review the documentation when
|
||
|
using these functions.</p>
|
||
|
</div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</div>
|
||
|
<div class="admonitionblock important">
|
||
|
<table>
|
||
|
<tr>
|
||
|
<td class="icon">
|
||
|
<i class="fa icon-important" title="Important"></i>
|
||
|
</td>
|
||
|
<td class="content">
|
||
|
<div class="title">Difficult to use API</div>
|
||
|
<div class="paragraph">
|
||
|
<p>The <code>getrandom</code> system call has three-state
|
||
|
return values, hence requires careful error checking.</p>
|
||
|
</div>
|
||
|
<div class="paragraph">
|
||
|
<p>It was introduced in Linux kernel 3.17, but before glibc 2.25 no API wrappers were
|
||
|
provided. As such one could only use it via the syscall interface
|
||
|
as <code>syscall(SYS_getrandom, (void*)dest, (size_t)size, (unsigned int)0)</code>.
|
||
|
For portable code targetting multiple kernel versions one has to check
|
||
|
for the function beingavailable on run-time, and switch to another
|
||
|
facility if the running kernel does not support this call.</p>
|
||
|
</div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</div>
|
||
|
<div class="paragraph">
|
||
|
<p>Other sources of randomness should be considered predictable.</p>
|
||
|
</div>
|
||
|
<div class="paragraph">
|
||
|
<p>Generating randomness for cryptographic keys in long-term use
|
||
|
may need different steps and is best left to cryptographic
|
||
|
libraries.</p>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div id="bottom" class="text-muted py-3" >
|
||
|
<div class="foot">
|
||
|
<div class="container">
|
||
|
<div class="row footerlinks">
|
||
|
<div class="col-sm-3 col-xs-6 widget">
|
||
|
<h3 class="widget-title">About</h3>
|
||
|
<div class="widget-body">
|
||
|
<dl>
|
||
|
<dd><a href="https://fedoraproject.org/wiki/Overview">About Fedora</a></dd>
|
||
|
<dd><a href="https://getfedora.org/en/sponsors">Sponsors</a></dd>
|
||
|
<dd><a href="https://fedoramagazine.org">Fedora Magazine</a></dd>
|
||
|
<dd><a href="https://fedoraproject.org/wiki/Legal:Main#Legal">Legal</a></dd>
|
||
|
</dl>
|
||
|
<ul class="list-inline">
|
||
|
<li>
|
||
|
<a href="https://www.facebook.com/TheFedoraProject" class="btn-social btn-outline"><i class="fa fa-fw fa-facebook"></i></a>
|
||
|
</li>
|
||
|
<li>
|
||
|
<a href="https://plus.google.com/112917221531140868607" class="btn-social btn-outline"><i class="fa fa-fw fa-google-plus"></i></a>
|
||
|
</li>
|
||
|
<li>
|
||
|
<a href="https://twitter.com/fedora" class="btn-social btn-outline"><i class="fa fa-fw fa-twitter"></i></a>
|
||
|
</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<div class="col-sm-3 col-xs-6 widget">
|
||
|
<h3 class="widget-title uppercase">Download</h3>
|
||
|
<div class="widget-body">
|
||
|
<dl>
|
||
|
<dd><a href="https://getfedora.org/en/workstation/download">Get Fedora Workstation</a></dd>
|
||
|
<dd><a href="https://getfedora.org/en/server/download">Get Fedora Server</a></dd>
|
||
|
<dd><a href="https://getfedora.org/en/atomic/download">Get Fedora Atomic</a></dd>
|
||
|
<dd><a href="https://spins.fedoraproject.org">Fedora Spins</a></dd>
|
||
|
<dd><a href="https://labs.fedoraproject.org">Fedora Labs</a></dd>
|
||
|
<dd><a href="https://arm.fedoraproject.org">Fedora ARM<span class="sup">®</span></a></dd>
|
||
|
<dd><a href="https://alt.fedoraproject.org/">Alternative Downloads</a></dd>
|
||
|
|
||
|
</dl>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<div class="col-sm-3 col-xs-6 widget">
|
||
|
<h3 class="widget-title">Support</h3>
|
||
|
<div class="widget-body">
|
||
|
<dl>
|
||
|
<dd><a href="https://fedoraproject.org/wiki/Communicating_and_getting_help">Get Help</a></dd>
|
||
|
<dd><a href="https://ask.fedoraproject.org/">Ask Fedora</a></dd>
|
||
|
<dd><a href="https://fedoraproject.org/wiki/Common_F27_bugs">Common Bugs</a></dd>
|
||
|
<dd><a href="https://developer.fedoraproject.org/">Fedora Developer Portal</a></dd>
|
||
|
<dd><a href="https://docs.fedoraproject.org/f27/install-guide/index.html">Installation Guide</a></dd>
|
||
|
</dl>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<div class="col-sm-3 col-xs-6 widget">
|
||
|
<h3 class="widget-title">Join</h3>
|
||
|
<div class="widget-body">
|
||
|
<dl>
|
||
|
<dd><a href="https://fedoraproject.org/wiki/Join">Join Fedora</a></dd>
|
||
|
<dd><a href="http://fedoraplanet.org">Planet Fedora</a></dd>
|
||
|
<dd><a href="https://fedoraproject.org/wiki/SIGs">Fedora SIGs</a></dd>
|
||
|
<dd><a href="https://admin.fedoraproject.org/accounts/">Fedora Account System</a></dd>
|
||
|
<dd><a href="https://fedoracommunity.org/">Fedora Community</a></dd>
|
||
|
</dl>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div> <!-- /row of widgets -->
|
||
|
|
||
|
<div class="row">
|
||
|
<div class="col-md-2">
|
||
|
<div class="widget-body">
|
||
|
<a href="https://www.redhat.com/"><img class="rh-logo" src="../../../master/_images/redhat-logo.png" alt="Red Hat Logo" /></a>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="col-md-7">
|
||
|
<div class="widget-body">
|
||
|
<p class="sponsor">Fedora is sponsored by Red Hat.</p>
|
||
|
<p class="sponsor"><a href="https://www.redhat.com/en/technologies/linux-platforms/articles/relationship-between-fedora-and-rhel">Learn more about the relationship between Red Hat and Fedora »</a></p>
|
||
|
<p class="copy">© 2017 Red Hat, Inc. and others. Please send any comments or corrections to the <a href="https://pagure.io/fedora-docs/docs-fp-o">documentation team</a></p>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div> <!-- /row of widgets -->
|
||
|
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
|
||
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
|
||
|
<!-- Latest compiled and minified JavaScript -->
|
||
|
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
|
||
|
<script src="../../../master/_javascripts/bootstrap-offcanvas.js" type="text/javascript"></script>
|
||
|
<script type="text/javascript">
|
||
|
/*<![CDATA[*/
|
||
|
$(document).ready(function() {
|
||
|
$("[id^='topicGroup']").on('show.bs.collapse', function (event) {
|
||
|
if (!($(event.target).attr('id').match(/^topicSubGroup/))) {
|
||
|
$(this).parent().find("[id^='tgSpan']").toggleClass("fa-angle-right fa-angle-down");
|
||
|
}
|
||
|
});
|
||
|
$("[id^='topicGroup']").on('hide.bs.collapse', function (event) {
|
||
|
if (!($(event.target).attr('id').match(/^topicSubGroup/))) {
|
||
|
$(this).parent().find("[id^='tgSpan']").toggleClass("fa-angle-right fa-angle-down");
|
||
|
}
|
||
|
});
|
||
|
$("[id^='topicSubGroup']").on('show.bs.collapse', function () {
|
||
|
$(this).parent().find("[id^='sgSpan']").toggleClass("fa-caret-right fa-caret-down");
|
||
|
});
|
||
|
$("[id^='topicSubGroup']").on('hide.bs.collapse', function () {
|
||
|
$(this).parent().find("[id^='sgSpan']").toggleClass("fa-caret-right fa-caret-down");
|
||
|
});
|
||
|
});
|
||
|
/*]]>*/
|
||
|
</script>
|
||
|
</body>
|
||
|
</html>
|