blob: daaba7aec1b825a18f917a1a1b1432cd10512b87 [file] [log] [blame]
Gilles Peskine6c723a22020-04-17 16:57:52 +02001
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml">
6 <head>
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Gilles Peskinec2db5f02021-01-18 20:36:53 +01008 <title>3. Functionality overview &#8212; PSA Crypto API 1.0.1 documentation</title>
Gilles Peskine6c723a22020-04-17 16:57:52 +02009 <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
10 <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
11 <script type="text/javascript">
12 var DOCUMENTATION_OPTIONS = {
13 URL_ROOT: '../',
Gilles Peskinec2db5f02021-01-18 20:36:53 +010014 VERSION: '1.0.1',
Gilles Peskine6c723a22020-04-17 16:57:52 +020015 COLLAPSE_INDEX: false,
16 FILE_SUFFIX: '.html',
Gilles Peskinec2db5f02021-01-18 20:36:53 +010017 HAS_SOURCE: false,
Gilles Peskine6c723a22020-04-17 16:57:52 +020018 SOURCELINK_SUFFIX: '.txt'
19 };
20 </script>
21 <script type="text/javascript" src="../_static/jquery.js"></script>
22 <script type="text/javascript" src="../_static/underscore.js"></script>
23 <script type="text/javascript" src="../_static/doctools.js"></script>
Gilles Peskinec2db5f02021-01-18 20:36:53 +010024 <link rel="author" title="About these documents" href="../about.html" />
Gilles Peskine6c723a22020-04-17 16:57:52 +020025 <link rel="index" title="Index" href="../genindex.html" />
26 <link rel="search" title="Search" href="../search.html" />
Gilles Peskinec2db5f02021-01-18 20:36:53 +010027 <link rel="next" title="4. Sample architectures" href="sample-arch.html" />
28 <link rel="prev" title="2. Design goals" href="goals.html" />
Gilles Peskine6c723a22020-04-17 16:57:52 +020029
30 <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
31
32 <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
33
34 </head>
35 <body>
36
37
38 <div class="document">
39 <div class="documentwrapper">
40 <div class="bodywrapper">
41 <div class="body" role="main">
42
43 <div class="section" id="functionality-overview">
Gilles Peskinec2db5f02021-01-18 20:36:53 +010044<span id="id1"></span><h1>3. Functionality overview</h1>
Gilles Peskine6c723a22020-04-17 16:57:52 +020045<p>This section provides a high-level overview of the functionality provided by the
Gilles Peskinec2db5f02021-01-18 20:36:53 +010046interface defined in this specification. Refer to the
47<a class="reference internal" href="../api/library/index.html#api-reference"><span class="std std-ref">API definition</span></a> for a detailed description.</p>
Gilles Peskine6c723a22020-04-17 16:57:52 +020048<p><a class="reference internal" href="../appendix/history.html#future"><span class="std std-ref">Future additions</span></a> describes features that might be included in future versions of this
49specification.</p>
50<p>Due to the modularity of the interface, almost every part of the library is
51optional. The only mandatory function is <a class="reference internal" href="../api/library/library.html#c.psa_crypto_init" title="psa_crypto_init"><code class="xref any c c-func docutils literal"><span class="pre">psa_crypto_init()</span></code></a>.</p>
52<div class="section" id="library-management">
Gilles Peskinec2db5f02021-01-18 20:36:53 +010053<h2>3.1. Library management</h2>
Gilles Peskine6c723a22020-04-17 16:57:52 +020054<p>Applications must call <a class="reference internal" href="../api/library/library.html#c.psa_crypto_init" title="psa_crypto_init"><code class="xref any c c-func docutils literal"><span class="pre">psa_crypto_init()</span></code></a> to initialize the library before
55using any other function.</p>
56</div>
57<div class="section" id="key-management">
Gilles Peskinec2db5f02021-01-18 20:36:53 +010058<h2>3.2. Key management</h2>
Gilles Peskine6c723a22020-04-17 16:57:52 +020059<p>Applications always access keys indirectly via an identifier, and can perform
60operations using a key without accessing the key material. This allows keys to
61be <em>non-extractable</em>, where an application can use a key but is not permitted to
62obtain the key material. Non-extractable keys are bound to the device, can be
63rate-limited and can have their usage restricted by policies.</p>
64<p>Each key has a set of attributes that describe the key and the policy for using
65the key. A <a class="reference internal" href="../api/keys/attributes.html#c.psa_key_attributes_t" title="psa_key_attributes_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_key_attributes_t</span></code></a> object contains all of the attributes, which
66is used when creating a key and when querying key attributes.</p>
Gilles Peskinec2db5f02021-01-18 20:36:53 +010067<p>The key attributes include:</p>
68<ul class="simple">
69<li>A <a class="reference internal" href="../api/keys/types.html#key-types"><span class="std std-ref">type</span></a> and size that describe the key material.</li>
70<li>The key <a class="reference internal" href="#key-ids"><span class="std std-ref">identifier</span></a> that the application uses to refer to the key.</li>
71<li>A <a class="reference internal" href="#key-life"><span class="std std-ref">lifetime</span></a> that determines when the key material is destroyed, and where it is stored.</li>
72<li>A <a class="reference internal" href="#key-usage-policies"><span class="std std-ref">policy</span></a> that determines how the key can be used.</li>
73</ul>
74<p>Keys are created using one of the <em>key creation functions</em>:</p>
Gilles Peskine6c723a22020-04-17 16:57:52 +020075<ul class="simple">
76<li><a class="reference internal" href="../api/keys/management.html#c.psa_import_key" title="psa_import_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_import_key()</span></code></a></li>
77<li><a class="reference internal" href="../api/keys/management.html#c.psa_generate_key" title="psa_generate_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_generate_key()</span></code></a></li>
78<li><a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_output_key" title="psa_key_derivation_output_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_output_key()</span></code></a></li>
79<li><a class="reference internal" href="../api/keys/management.html#c.psa_copy_key" title="psa_copy_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_copy_key()</span></code></a></li>
80</ul>
Gilles Peskinec2db5f02021-01-18 20:36:53 +010081<p>These output the key identifier, that is used to access the key in all other parts of the API.</p>
82<p>All of the key attributes are set when the key is created and cannot be changed
83without destroying the key first. If the original key permits copying, then the
84application can specify a different lifetime or restricted policy for the
85copy of the key.</p>
Gilles Peskine6c723a22020-04-17 16:57:52 +020086<p>A call to <a class="reference internal" href="../api/keys/management.html#c.psa_destroy_key" title="psa_destroy_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_destroy_key()</span></code></a> destroys the key material, and will cause any active
87operations that are using the key to fail. Therefore an application must not
88destroy a key while an operation using that key is in progress, unless the
89application is prepared to handle a failure of the operation.</p>
Gilles Peskinec2db5f02021-01-18 20:36:53 +010090<div class="section" id="key-identifiers">
91<span id="key-ids"></span><h3>3.2.1. Key identifiers</h3>
92<p>Key identifiers are integral values that act as permanent names for persistent keys, or as transient references to volatile keys. Key identifiers are defined by the application for persistent keys, and by the implementation for volatile keys and for built-in keys.</p>
93<p>Key identifiers are output from a successful call to one of the key creation functions.</p>
94<p>Valid key identifiers must have distinct values within the same application. If
95the implementation provides <a class="reference internal" href="goals.html#isolation"><span class="std std-ref">caller isolation</span></a>, then key
96identifiers are local to each application. That is, the same key identifier in two
97applications corresponds to two different keys.</p>
98<p>See <a class="reference internal" href="../api/keys/ids.html#key-identifiers"><span class="secref">Key identifiers</span></a>.</p>
99</div>
100<div class="section" id="key-lifetimes">
101<span id="key-life"></span><h3>3.2.2. Key lifetimes</h3>
102<p>The lifetime of a key indicates where it is stored and which application and system actions will create and destroy it.</p>
103<p>There are two main types of lifetimes: <em>volatile</em> and <em>persistent</em>.</p>
104<p>Volatile keys are automatically destroyed when the application instance terminates or on a power reset of the device. Volatile key identifiers are allocated by the implementation when the key is created. Volatile keys can be explicitly destroyed with a call to <a class="reference internal" href="../api/keys/management.html#c.psa_destroy_key" title="psa_destroy_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_destroy_key()</span></code></a>.</p>
105<p>Persistent keys are preserved until the application explicitly destroys them or until an implementation-specific device management event occurs, for example, a factory reset. The key identifier for a persistent key is set by the application when creating the key, and remains valid throughout the lifetime of the key, even if the application instance that created the key terminates.</p>
106<p>See <a class="reference internal" href="../api/keys/lifetimes.html#key-lifetimes"><span class="secref">Key lifetimes</span></a>.</p>
107</div>
108<div class="section" id="key-policies">
109<span id="key-usage-policies"></span><h3>3.2.3. Key policies</h3>
110<p>All keys have an associated policy that regulates which operations are permitted on the key. Each key policy is a set of usage flags and a specific algorithm that is permitted with the key. See <a class="reference internal" href="../api/keys/policy.html#key-policy"><span class="secref">Key policies</span></a>.</p>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200111</div>
112<div class="section" id="recommendations-of-minimum-standards-for-key-management">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100113<h3>3.2.4. Recommendations of minimum standards for key management</h3>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200114<p>Most implementations provide the following functions:</p>
115<ul class="simple">
116<li><a class="reference internal" href="../api/keys/management.html#c.psa_import_key" title="psa_import_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_import_key()</span></code></a>. The exceptions are implementations that only give access
117to a key or keys that are provisioned by proprietary means, and do not allow
118the main application to use its own cryptographic material.</li>
119<li><a class="reference internal" href="../api/keys/attributes.html#c.psa_get_key_attributes" title="psa_get_key_attributes"><code class="xref any c c-func docutils literal"><span class="pre">psa_get_key_attributes()</span></code></a> and the <code class="docutils literal"><span class="pre">psa_get_key_xxx()</span></code> accessor functions.
120They are easy to implement, and it is difficult to write applications and to
121diagnose issues without being able to check the metadata.</li>
122<li><a class="reference internal" href="../api/keys/management.html#c.psa_export_public_key" title="psa_export_public_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_export_public_key()</span></code></a>. This function is usually provided if the
123implementation supports any asymmetric algorithm, since public-key
124cryptography often requires the delivery of a public key that is associated
125with a protected private key.</li>
126<li><a class="reference internal" href="../api/keys/management.html#c.psa_export_key" title="psa_export_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_export_key()</span></code></a>. However, highly constrained implementations that are
127designed to work only with short-term keys, or only with long-term
128non-extractable keys, do not need to provide this function.</li>
129</ul>
130</div>
131</div>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200132<div class="section" id="symmetric-cryptography">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100133<h2>3.3. Symmetric cryptography</h2>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200134<p>This specification defines interfaces for the following types of symmetric
135cryptographic operation:</p>
136<ul class="simple">
137<li>Message digests, commonly known as hash functions.</li>
138<li>Message authentication codes (MAC).</li>
139<li>Symmetric ciphers.</li>
140<li>Authenticated encryption with associated data (AEAD).</li>
141</ul>
142<p>For each type of symmetric cryptographic operation, the API includes:</p>
143<ul class="simple">
144<li>A pair of <em>single-part</em> functions. For example, compute and verify, or
145encrypt and decrypt.</li>
146<li>A series of functions that permit <em>multi-part operations</em>.</li>
147</ul>
148<div class="section" id="single-part-functions">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100149<h3>3.3.1. Single-part Functions</h3>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200150<p>Single-part functions are APIs that implement the cryptographic operation in a
151single function call. This is the easiest API to use when all of the inputs and
152outputs fit into the application memory.</p>
153<p>Some use cases involve messages that are too large to be assembled in memory, or
154require non-default configuration of the algorithm. These use cases require the
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100155use of a <a class="reference internal" href="#multi-part-operations"><span class="secref">multi-part operation</span></a>.</p>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200156</div>
157<div class="section" id="multi-part-operations">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100158<span id="id2"></span><h3>3.3.2. Multi-part operations</h3>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200159<p>Multi-part operations are APIs which split a single cryptographic operation into
160a sequence of separate steps. This enables fine control over the configuration
161of the cryptographic operation, and allows the message data to be processed in
162fragments instead of all at once. For example, the following situations require
163the use of a multi-part operation:</p>
164<ul class="simple">
165<li>Processing messages that cannot be assembled in memory.</li>
166<li>Using a deterministic IV for unauthenticated encryption.</li>
167<li>Providing the IV separately for unauthenticated encryption or decryption.</li>
168<li>Separating the AEAD authentication tag from the cipher text.</li>
169</ul>
170<p>Each multi-part operation defines a specific object type to maintain the state
171of the operation. These types are implementation-defined. All multi-part
172operations follow the same pattern of use:</p>
173<ol class="arabic">
174<li><p class="first"><strong>Allocate:</strong> Allocate memory for an operation object of the appropriate
175type. The application can use any allocation strategy: stack, heap, static, etc.</p>
176</li>
177<li><p class="first"><strong>Initialize:</strong> Initialize or assign the operation object by one of the
178following methods:</p>
179<blockquote>
180<div><ul class="simple">
181<li>Set it to logical zero. This is automatic for static and global
182variables. Explicit initialization must use the associated
183<code class="docutils literal"><span class="pre">PSA_xxx_INIT</span></code> macro as the type is implementation-defined.</li>
184<li>Set it to all-bits zero. This is automatic if the object was
185allocated with <code class="docutils literal"><span class="pre">calloc()</span></code>.</li>
186<li>Assign the value of the associated macro <code class="docutils literal"><span class="pre">PSA_xxx_INIT</span></code>.</li>
187<li>Assign the result of calling the associated function
188<code class="docutils literal"><span class="pre">psa_xxx_init()</span></code>.</li>
189</ul>
190</div></blockquote>
191<p>The resulting object is now <em>inactive</em>.</p>
192<p>It is an error to initialize an operation object that is in <em>active</em> or
193<em>error</em> states. This can leak memory or other resources.</p>
194</li>
195<li><p class="first"><strong>Setup:</strong> Start a new multi-part operation on an <em>inactive</em> operation
196object. Each operation object will define one or more setup functions to
197start a specific operation.</p>
198<p>On success, a setup function will put an operation object into an <em>active</em>
199state. On failure, the operation object will remain <em>inactive</em>.</p>
200</li>
201<li><p class="first"><strong>Update:</strong> Update an <em>active</em> operation object. The update function can
202provide additional parameters, supply data for processing or generate
203outputs.</p>
204<p>On success, the operation object remains <em>active</em>. On failure, the
205operation object will enter an <em>error</em> state.</p>
206</li>
207<li><p class="first"><strong>Finish:</strong> To end the operation, call the applicable finishing function.
208This will take any final inputs, produce any final outputs, and then
209release any resources associated with the operation.</p>
210<p>On success, the operation object returns to the <em>inactive</em> state. On
211failure, the operation object will enter an <em>error</em> state.</p>
212</li>
213</ol>
214<p>An operation can be aborted at any stage during its use by calling the
215associated <code class="docutils literal"><span class="pre">psa_xxx_abort()</span></code> function. This will release any resources
216associated with the operation and return the operation object to the <em>inactive</em>
217state.</p>
218<p>Any error that occurs to an operation while it is in an <em>active</em> state will
219result in the operation entering an <em>error</em> state. The application must call the
220associated <code class="docutils literal"><span class="pre">psa_xxx_abort()</span></code> function to release the operation resources and
221return the object to the <em>inactive</em> state.</p>
222<p>Once an operation object is returned to the <em>inactive</em> state, it can be reused
223by calling one of the applicable setup functions again.</p>
224<p>If a multi-part operation object is not initialized before use, the behavior is
225undefined.</p>
226<p>If a multi-part operation function determines that the operation object is not in
227any valid state, it can return <a class="reference internal" href="../api/library/status.html#c.PSA_ERROR_CORRUPTION_DETECTED" title="PSA_ERROR_CORRUPTION_DETECTED"><code class="xref any c c-macro docutils literal"><span class="pre">PSA_ERROR_CORRUPTION_DETECTED</span></code></a>.</p>
228<p>If a multi-part operation function is called with an operation object in the
229wrong state, the function will return <a class="reference internal" href="../api/library/status.html#c.PSA_ERROR_BAD_STATE" title="PSA_ERROR_BAD_STATE"><code class="xref any c c-macro docutils literal"><span class="pre">PSA_ERROR_BAD_STATE</span></code></a> and the operation
230object will enter the <em>error</em> state.</p>
231<p>It is safe to move a multi-part operation object to a different memory location,
232for example, using a bitwise copy, and then to use the object in the new
233location. For example, an application can allocate an operation object on the
234stack and return it, or the operation object can be allocated within memory
235managed by a garbage collector. However, this does not permit the following
236behaviors:</p>
237<ul class="simple">
238<li>Moving the object while a function is being called on the object. This is
239not safe. See also <a class="reference internal" href="conventions.html#concurrency"><span class="std std-ref">Concurrent calls</span></a>.</li>
240<li>Working with both the original and the copied operation objects. This
241requires cloning the operation, which is only available for hash operations
242using <a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_clone" title="psa_hash_clone"><code class="xref any c c-func docutils literal"><span class="pre">psa_hash_clone()</span></code></a>.</li>
243</ul>
244<p>Each type of multi-part operation can have multiple <em>active</em> states.
245Documentation for the specific operation describes the configuration and update
246functions, and any requirements about their usage and ordering.</p>
247</div>
248<div class="section" id="message-digests-hashes">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100249<h3>3.3.3. Message digests (Hashes)</h3>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200250<p>The single-part hash functions are:</p>
251<ul class="simple">
252<li><a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_compute" title="psa_hash_compute"><code class="xref any c c-func docutils literal"><span class="pre">psa_hash_compute()</span></code></a> to calculate the hash of a message.</li>
253<li><a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_compare" title="psa_hash_compare"><code class="xref any c c-func docutils literal"><span class="pre">psa_hash_compare()</span></code></a> to compare the hash of a message with a reference value.</li>
254</ul>
255<p>The <a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_operation_t" title="psa_hash_operation_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_hash_operation_t</span></code></a> <a class="reference internal" href="#multi-part-operations"><span class="std std-ref">multi-part operation</span></a>
256allows messages to be processed in fragments:</p>
257<ol class="arabic simple">
258<li>Initialize the <a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_operation_t" title="psa_hash_operation_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_hash_operation_t</span></code></a> object to zero, or by assigning the
259value of the associated macro <a class="reference internal" href="../api/ops/hashes.html#c.PSA_HASH_OPERATION_INIT" title="PSA_HASH_OPERATION_INIT"><code class="xref any c c-macro docutils literal"><span class="pre">PSA_HASH_OPERATION_INIT</span></code></a>.</li>
260<li>Call <a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_setup" title="psa_hash_setup"><code class="xref any c c-func docutils literal"><span class="pre">psa_hash_setup()</span></code></a> to specify the required hash algorithm, call
261<a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_clone" title="psa_hash_clone"><code class="xref any c c-func docutils literal"><span class="pre">psa_hash_clone()</span></code></a> to duplicate the state of <em>active</em> <a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_operation_t" title="psa_hash_operation_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_hash_operation_t</span></code></a>
262object, or call <a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_resume" title="psa_hash_resume"><code class="xref any c c-func docutils literal"><span class="pre">psa_hash_resume()</span></code></a> to restart a hash operation with the
263output from a previously suspended hash operation.</li>
264<li>Call the <a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_update" title="psa_hash_update"><code class="xref any c c-func docutils literal"><span class="pre">psa_hash_update()</span></code></a> function on successive chunks of the message.</li>
265<li>At the end of the message, call the required finishing function:<ul>
266<li>To suspend the hash operation and extract a hash suspend state,
267call <a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_suspend" title="psa_hash_suspend"><code class="xref any c c-func docutils literal"><span class="pre">psa_hash_suspend()</span></code></a>. The output state can subsequently be used
268to resume the hash operation.</li>
269<li>To calculate the digest of a message, call <a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_finish" title="psa_hash_finish"><code class="xref any c c-func docutils literal"><span class="pre">psa_hash_finish()</span></code></a>.</li>
270<li>To verify the digest of a message against a reference value, call
271<a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_verify" title="psa_hash_verify"><code class="xref any c c-func docutils literal"><span class="pre">psa_hash_verify()</span></code></a>.</li>
272</ul>
273</li>
274</ol>
275<p>To abort the operation or recover from an error, call <a class="reference internal" href="../api/ops/hashes.html#c.psa_hash_abort" title="psa_hash_abort"><code class="xref any c c-func docutils literal"><span class="pre">psa_hash_abort()</span></code></a>.</p>
276</div>
277<div class="section" id="message-authentication-codes-macs">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100278<h3>3.3.4. Message authentication codes (MACs)</h3>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200279<p>The single-part MAC functions are:</p>
280<ul class="simple">
281<li><a class="reference internal" href="../api/ops/macs.html#c.psa_mac_compute" title="psa_mac_compute"><code class="xref any c c-func docutils literal"><span class="pre">psa_mac_compute()</span></code></a> to calculate the MAC of a message.</li>
282<li><a class="reference internal" href="../api/ops/macs.html#c.psa_mac_verify" title="psa_mac_verify"><code class="xref any c c-func docutils literal"><span class="pre">psa_mac_verify()</span></code></a> to compare the MAC of a message with a reference value.</li>
283</ul>
284<p>The <a class="reference internal" href="../api/ops/macs.html#c.psa_mac_operation_t" title="psa_mac_operation_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_mac_operation_t</span></code></a> <a class="reference internal" href="#multi-part-operations"><span class="std std-ref">multi-part operation</span></a>
285allows messages to be processed in fragments:</p>
286<ol class="arabic simple">
287<li>Initialize the <a class="reference internal" href="../api/ops/macs.html#c.psa_mac_operation_t" title="psa_mac_operation_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_mac_operation_t</span></code></a> object to zero, or by assigning the
288value of the associated macro <a class="reference internal" href="../api/ops/macs.html#c.PSA_MAC_OPERATION_INIT" title="PSA_MAC_OPERATION_INIT"><code class="xref any c c-macro docutils literal"><span class="pre">PSA_MAC_OPERATION_INIT</span></code></a>.</li>
289<li>Call <a class="reference internal" href="../api/ops/macs.html#c.psa_mac_sign_setup" title="psa_mac_sign_setup"><code class="xref any c c-func docutils literal"><span class="pre">psa_mac_sign_setup()</span></code></a> or <a class="reference internal" href="../api/ops/macs.html#c.psa_mac_verify_setup" title="psa_mac_verify_setup"><code class="xref any c c-func docutils literal"><span class="pre">psa_mac_verify_setup()</span></code></a> to specify the
290algorithm and key.</li>
291<li>Call the <a class="reference internal" href="../api/ops/macs.html#c.psa_mac_update" title="psa_mac_update"><code class="xref any c c-func docutils literal"><span class="pre">psa_mac_update()</span></code></a> function on successive chunks of the message.</li>
292<li>At the end of the message, call the required finishing function:<ul>
293<li>To calculate the MAC of the message, call <a class="reference internal" href="../api/ops/macs.html#c.psa_mac_sign_finish" title="psa_mac_sign_finish"><code class="xref any c c-func docutils literal"><span class="pre">psa_mac_sign_finish()</span></code></a>.</li>
294<li>To verify the MAC of the message against a reference value, call
295<a class="reference internal" href="../api/ops/macs.html#c.psa_mac_verify_finish" title="psa_mac_verify_finish"><code class="xref any c c-func docutils literal"><span class="pre">psa_mac_verify_finish()</span></code></a>.</li>
296</ul>
297</li>
298</ol>
299<p>To abort the operation or recover from an error, call <a class="reference internal" href="../api/ops/macs.html#c.psa_mac_abort" title="psa_mac_abort"><code class="xref any c c-func docutils literal"><span class="pre">psa_mac_abort()</span></code></a>.</p>
300</div>
301<div class="section" id="encryption-and-decryption">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100302<h3>3.3.5. Encryption and decryption</h3>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200303<div class="admonition note">
304<p class="first admonition-title">Note</p>
305<p class="last">The unauthenticated cipher API is provided to implement legacy protocols and
306for use cases where the data integrity and authenticity is guaranteed by
307non-cryptographic means. It is recommended that newer protocols use
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100308<a class="reference internal" href="#func-aead"><span class="secref">Authenticated encryption (AEAD)</span></a>.</p>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200309</div>
310<p>The single-part functions for encrypting or decrypting a message using an
311unauthenticated symmetric cipher are:</p>
312<ul class="simple">
313<li><a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_encrypt" title="psa_cipher_encrypt"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_encrypt()</span></code></a> to encrypt a message using an unauthenticated symmetric
314cipher. The encryption function generates a random IV. Use the multi-part API
315to provide a deterministic IV: this is not secure in general, but
316can be secure in some conditions that depend on the algorithm.</li>
317<li><a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_decrypt" title="psa_cipher_decrypt"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_decrypt()</span></code></a> to decrypt a message using an unauthenticated symmetric
318cipher.</li>
319</ul>
320<p>The <a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_operation_t" title="psa_cipher_operation_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_cipher_operation_t</span></code></a> <a class="reference internal" href="#multi-part-operations"><span class="std std-ref">multi-part operation</span></a>
321permits alternative initialization parameters and allows messages to be
322processed in fragments:</p>
323<ol class="arabic simple">
324<li>Initialize the <a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_operation_t" title="psa_cipher_operation_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_cipher_operation_t</span></code></a> object to zero, or by assigning the
325value of the associated macro <a class="reference internal" href="../api/ops/ciphers.html#c.PSA_CIPHER_OPERATION_INIT" title="PSA_CIPHER_OPERATION_INIT"><code class="xref any c c-macro docutils literal"><span class="pre">PSA_CIPHER_OPERATION_INIT</span></code></a>.</li>
326<li>Call <a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_encrypt_setup" title="psa_cipher_encrypt_setup"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_encrypt_setup()</span></code></a> or <a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_decrypt_setup" title="psa_cipher_decrypt_setup"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_decrypt_setup()</span></code></a> to specify the
327algorithm and key.</li>
328<li>Provide additional parameters:<ul>
329<li>When encrypting data, generate or set an initialization vector (IV),
330nonce, or similar initial value such as an initial counter value. To
331generate a random IV, which is recommended in most protocols, call
332<a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_generate_iv" title="psa_cipher_generate_iv"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_generate_iv()</span></code></a>. To set the IV, call <a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_set_iv" title="psa_cipher_set_iv"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_set_iv()</span></code></a>.</li>
333<li>When decrypting, set the IV or nonce. To set the IV, call
334<a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_set_iv" title="psa_cipher_set_iv"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_set_iv()</span></code></a>.</li>
335</ul>
336</li>
337<li>Call the <a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_update" title="psa_cipher_update"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_update()</span></code></a> function on successive chunks of the message.</li>
338<li>Call <a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_finish" title="psa_cipher_finish"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_finish()</span></code></a> to complete the operation and return any final
339output.</li>
340</ol>
341<p>To abort the operation or recover from an error, call <a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_abort" title="psa_cipher_abort"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_abort()</span></code></a>.</p>
342</div>
343<div class="section" id="authenticated-encryption-aead">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100344<span id="func-aead"></span><h3>3.3.6. Authenticated encryption (AEAD)</h3>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200345<p>The single-part AEAD functions are:</p>
346<ul class="simple">
347<li><a class="reference internal" href="../api/ops/aead.html#c.psa_aead_encrypt" title="psa_aead_encrypt"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_encrypt()</span></code></a> to encrypt a message using an authenticated symmetric
348cipher.</li>
349<li><a class="reference internal" href="../api/ops/aead.html#c.psa_aead_decrypt" title="psa_aead_decrypt"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_decrypt()</span></code></a> to decrypt a message using an authenticated symmetric
350cipher.</li>
351</ul>
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100352<p>These functions follow the interface recommended by <span><em>An Interface and Algorithms for Authenticated Encryption</em> <a class="reference internal" href="../about.html#citation-rfc5116"><span class="cite">[RFC5116]</span></a></span>.</p>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200353<p>The encryption function requires a nonce to be provided. To generate a random
354nonce, either call <a class="reference internal" href="../api/ops/rng.html#c.psa_generate_random" title="psa_generate_random"><code class="xref any c c-func docutils literal"><span class="pre">psa_generate_random()</span></code></a> or use the AEAD multi-part API.</p>
355<p>The <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_operation_t" title="psa_aead_operation_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_aead_operation_t</span></code></a> <a class="reference internal" href="#multi-part-operations"><span class="std std-ref">multi-part operation</span></a>
356permits alternative initialization parameters and allows messages to be
357processed in fragments:</p>
358<ol class="arabic simple">
359<li>Initialize the <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_operation_t" title="psa_aead_operation_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_aead_operation_t</span></code></a> object to zero, or by assigning the
360value of the associated macro <a class="reference internal" href="../api/ops/aead.html#c.PSA_AEAD_OPERATION_INIT" title="PSA_AEAD_OPERATION_INIT"><code class="xref any c c-macro docutils literal"><span class="pre">PSA_AEAD_OPERATION_INIT</span></code></a>.</li>
361<li>Call <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_encrypt_setup" title="psa_aead_encrypt_setup"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_encrypt_setup()</span></code></a> or <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_decrypt_setup" title="psa_aead_decrypt_setup"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_decrypt_setup()</span></code></a> to specify the
362algorithm and key.</li>
363<li>Provide additional parameters:<ul>
364<li>If the algorithm requires it, call <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_set_lengths" title="psa_aead_set_lengths"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_set_lengths()</span></code></a> to specify the
365length of the non-encrypted and encrypted inputs to the operation.</li>
366<li>When encrypting, call either <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_generate_nonce" title="psa_aead_generate_nonce"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_generate_nonce()</span></code></a> or
367<a class="reference internal" href="../api/ops/aead.html#c.psa_aead_set_nonce" title="psa_aead_set_nonce"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_set_nonce()</span></code></a> to generate or set the nonce.</li>
368<li>When decrypting, call <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_set_nonce" title="psa_aead_set_nonce"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_set_nonce()</span></code></a> to set the nonce.</li>
369</ul>
370</li>
371<li>Call <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_update_ad" title="psa_aead_update_ad"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_update_ad()</span></code></a> zero or more times with fragments of the
372non-encrypted additional data.</li>
373<li>Call <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_update" title="psa_aead_update"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_update()</span></code></a> zero or more times with fragments of the plaintext
374or ciphertext to encrypt or decrypt.</li>
375<li>At the end of the message, call the required finishing function:<ul>
376<li>To complete an encryption operation, call <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_finish" title="psa_aead_finish"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_finish()</span></code></a> to compute
377and return authentication tag.</li>
378<li>To complete a decryption operation, call <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_verify" title="psa_aead_verify"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_verify()</span></code></a> to
379compute the authentication tag and verify it against a reference value.</li>
380</ul>
381</li>
382</ol>
383<p>To abort the operation or recover from an error, call <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_abort" title="psa_aead_abort"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_abort()</span></code></a>.</p>
384<p>Having a multi-part interface to authenticated encryption raises specific issues.</p>
385<p>Multi-part authenticated decryption produces partial results that are not
386authenticated. Applications must not use or expose partial results of
387authenticated decryption until <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_verify" title="psa_aead_verify"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_verify()</span></code></a> has returned a success
388status and must destroy all partial results without revealing them if
389<a class="reference internal" href="../api/ops/aead.html#c.psa_aead_verify" title="psa_aead_verify"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_verify()</span></code></a> returns a failure status. Revealing partial results, either directly or indirectly through the application’s behavior, can compromise the
390confidentiality of all inputs that are encrypted with the same key.</p>
391<p>For encryption, some common algorithms cannot be processed in a streaming
392fashion. For SIV mode, the whole plaintext must be known before the encryption
393can start; the multi-part AEAD API is not meant to be usable with SIV mode. For
394CCM mode, the length of the plaintext must be known before the encryption can
395start; the application can call the function <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_set_lengths" title="psa_aead_set_lengths"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_set_lengths()</span></code></a> to provide
396these lengths before providing input.</p>
397</div>
398<div class="section" id="key-derivation">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100399<span id="id3"></span><h3>3.3.7. Key derivation</h3>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200400<p>A key derivation encodes a deterministic method to generate a finite stream of
401bytes. This data stream is computed by the cryptoprocessor and extracted in
402chunks. If two key derivation operations are constructed with the same
403parameters, then they produce the same output.</p>
404<p>A key derivation consists of two phases:</p>
405<ol class="arabic simple">
406<li>Input collection. This is sometimes known as <em>extraction</em>: the operation
407“extracts” information from the inputs to generate a pseudorandom
408intermediate secret value.</li>
409<li>Output generation. This is sometimes known as <em>expansion</em>: the operation
410“expands” the intermediate secret value to the desired output length.</li>
411</ol>
412<p>The specification defines a <a class="reference internal" href="#multi-part-operations"><span class="std std-ref">multi-part operation</span></a>
413API for key derivation that allows for multiple key and non-key outputs to be
414extracted from a single derivation operation object.</p>
415<p>In an implementation with <a class="reference internal" href="goals.html#isolation"><span class="std std-ref">isolation</span></a>, the intermediate
416state of the key derivation is not visible to the caller, and if an output of
417the derivation is a non-exportable key, then this key cannot be recovered
418outside the isolation boundary.</p>
419<p>Applications use the <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_operation_t" title="psa_key_derivation_operation_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_key_derivation_operation_t</span></code></a> type to create key
420derivation operations. The operation object is used as follows:</p>
421<ol class="arabic simple">
422<li>Initialize a <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_operation_t" title="psa_key_derivation_operation_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_key_derivation_operation_t</span></code></a> object to zero or to
423<a class="reference internal" href="../api/ops/kdf.html#c.PSA_KEY_DERIVATION_OPERATION_INIT" title="PSA_KEY_DERIVATION_OPERATION_INIT"><code class="xref any c c-macro docutils literal"><span class="pre">PSA_KEY_DERIVATION_OPERATION_INIT</span></code></a>.</li>
424<li>Call <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_setup" title="psa_key_derivation_setup"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_setup()</span></code></a> to select a key derivation algorithm.</li>
425<li>Call the functions <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_input_bytes" title="psa_key_derivation_input_bytes"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_input_bytes()</span></code></a> and
426<a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_input_key" title="psa_key_derivation_input_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_input_key()</span></code></a>, or <a class="reference internal" href="../api/ops/ka.html#c.psa_key_derivation_key_agreement" title="psa_key_derivation_key_agreement"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_key_agreement()</span></code></a> to
427provide the inputs to the key derivation algorithm. Many key derivation
428algorithms take multiple inputs; the <code class="docutils literal"><span class="pre">step</span></code> parameter to these functions
429indicates which input is being provided. The documentation for each key
430derivation algorithm describes the expected inputs for that algorithm and
431in what order to pass them.</li>
432<li>Optionally, call <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_set_capacity" title="psa_key_derivation_set_capacity"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_set_capacity()</span></code></a> to set a limit on the
433amount of data that can be output from the key derivation operation.</li>
434<li>Call <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_output_key" title="psa_key_derivation_output_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_output_key()</span></code></a> to create a derived key, or
435<a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_output_bytes" title="psa_key_derivation_output_bytes"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_output_bytes()</span></code></a> to export the derived data. These
436functions can be called multiple times to read successive output from the key
437derivation, until the stream is exhausted when its capacity has been reached.</li>
438<li>Key derivation does not finish in the same way as other multi-part
439operations. Call <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_abort" title="psa_key_derivation_abort"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_abort()</span></code></a> to release the key derivation
440operation memory when the object is no longer required.</li>
441</ol>
442<p>To recover from an error, call <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_abort" title="psa_key_derivation_abort"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_abort()</span></code></a> to release the key
443derivation operation memory.</p>
444<p>A key derivation operation cannot be rewound. Once a part of the stream has been
445output, it cannot be output again. This ensures that the same part of the output
446will not be used for different purposes.</p>
447</div>
448<div class="section" id="example-of-the-symmetric-cryptography-api">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100449<h3>3.3.8. Example of the symmetric cryptography API</h3>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200450<p>Here is an example of a use case where a master key is used to generate both a
451message encryption key and an IV for the encryption, and the derived key and IV
452are then used to encrypt a message.</p>
453<ol class="arabic simple">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100454<li>Derive the message encryption material from the master key.<ol class="loweralpha">
Gilles Peskine6c723a22020-04-17 16:57:52 +0200455<li>Initialize a <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_operation_t" title="psa_key_derivation_operation_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_key_derivation_operation_t</span></code></a> object to zero or to
456<a class="reference internal" href="../api/ops/kdf.html#c.PSA_KEY_DERIVATION_OPERATION_INIT" title="PSA_KEY_DERIVATION_OPERATION_INIT"><code class="xref any c c-macro docutils literal"><span class="pre">PSA_KEY_DERIVATION_OPERATION_INIT</span></code></a>.</li>
457<li>Call <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_setup" title="psa_key_derivation_setup"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_setup()</span></code></a> with <a class="reference internal" href="../api/ops/kdf.html#c.PSA_ALG_HKDF" title="PSA_ALG_HKDF"><code class="xref any c c-macro docutils literal"><span class="pre">PSA_ALG_HKDF</span></code></a> as the algorithm.</li>
458<li>Call <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_input_key" title="psa_key_derivation_input_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_input_key()</span></code></a> with the step
459<a class="reference internal" href="../api/ops/kdf.html#c.PSA_KEY_DERIVATION_INPUT_SECRET" title="PSA_KEY_DERIVATION_INPUT_SECRET"><code class="xref any c c-macro docutils literal"><span class="pre">PSA_KEY_DERIVATION_INPUT_SECRET</span></code></a> and the master key.</li>
460<li>Call <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_input_bytes" title="psa_key_derivation_input_bytes"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_input_bytes()</span></code></a> with the step
461<a class="reference internal" href="../api/ops/kdf.html#c.PSA_KEY_DERIVATION_INPUT_INFO" title="PSA_KEY_DERIVATION_INPUT_INFO"><code class="xref any c c-macro docutils literal"><span class="pre">PSA_KEY_DERIVATION_INPUT_INFO</span></code></a> and a public value that uniquely
462identifies the message.</li>
463<li>Populate a <a class="reference internal" href="../api/keys/attributes.html#c.psa_key_attributes_t" title="psa_key_attributes_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_key_attributes_t</span></code></a> object with the derived message
464encryption key’s attributes.</li>
465<li>Call <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_output_key" title="psa_key_derivation_output_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_output_key()</span></code></a> to create the derived message key.</li>
466<li>Call <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_output_bytes" title="psa_key_derivation_output_bytes"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_output_bytes()</span></code></a> to generate the derived IV.</li>
467<li>Call <a class="reference internal" href="../api/ops/kdf.html#c.psa_key_derivation_abort" title="psa_key_derivation_abort"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_abort()</span></code></a> to release the key derivation operation
468memory.</li>
469</ol>
470</li>
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100471<li>Encrypt the message with the derived material.<ol class="loweralpha">
Gilles Peskine6c723a22020-04-17 16:57:52 +0200472<li>Initialize a <a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_operation_t" title="psa_cipher_operation_t"><code class="xref any c c-type docutils literal"><span class="pre">psa_cipher_operation_t</span></code></a> object to zero or to
473<a class="reference internal" href="../api/ops/ciphers.html#c.PSA_CIPHER_OPERATION_INIT" title="PSA_CIPHER_OPERATION_INIT"><code class="xref any c c-macro docutils literal"><span class="pre">PSA_CIPHER_OPERATION_INIT</span></code></a>.</li>
474<li>Call <a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_encrypt_setup" title="psa_cipher_encrypt_setup"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_encrypt_setup()</span></code></a> with the derived message encryption key.</li>
475<li>Call <a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_set_iv" title="psa_cipher_set_iv"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_set_iv()</span></code></a> using the derived IV retrieved above.</li>
476<li>Call <a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_update" title="psa_cipher_update"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_update()</span></code></a> one or more times to encrypt the message.</li>
477<li>Call <a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_finish" title="psa_cipher_finish"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_finish()</span></code></a> at the end of the message.</li>
478</ol>
479</li>
480<li>Call <a class="reference internal" href="../api/keys/management.html#c.psa_destroy_key" title="psa_destroy_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_destroy_key()</span></code></a> to clear the generated key.</li>
481</ol>
482</div>
483</div>
484<div class="section" id="asymmetric-cryptography">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100485<h2>3.4. Asymmetric cryptography</h2>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200486<p>This specification defines functions for asymmetric cryptography, including
487asymmetric encryption, asymmetric signature, and two-way key agreement.</p>
488<div class="section" id="asymmetric-encryption">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100489<h3>3.4.1. Asymmetric encryption</h3>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200490<p>Asymmetric encryption is provided through the functions
491<a class="reference internal" href="../api/ops/pke.html#c.psa_asymmetric_encrypt" title="psa_asymmetric_encrypt"><code class="xref any c c-func docutils literal"><span class="pre">psa_asymmetric_encrypt()</span></code></a> and <a class="reference internal" href="../api/ops/pke.html#c.psa_asymmetric_decrypt" title="psa_asymmetric_decrypt"><code class="xref any c c-func docutils literal"><span class="pre">psa_asymmetric_decrypt()</span></code></a>.</p>
492</div>
493<div class="section" id="hash-and-sign">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100494<h3>3.4.2. Hash-and-sign</h3>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200495<p>The signature and verification functions <a class="reference internal" href="../api/ops/sign.html#c.psa_sign_message" title="psa_sign_message"><code class="xref any c c-func docutils literal"><span class="pre">psa_sign_message()</span></code></a> and
496<a class="reference internal" href="../api/ops/sign.html#c.psa_verify_message" title="psa_verify_message"><code class="xref any c c-func docutils literal"><span class="pre">psa_verify_message()</span></code></a> take a message as one of their inputs and perform a
497hash-and-sign algorithm.</p>
498<p>The functions <a class="reference internal" href="../api/ops/sign.html#c.psa_sign_hash" title="psa_sign_hash"><code class="xref any c c-func docutils literal"><span class="pre">psa_sign_hash()</span></code></a> and <a class="reference internal" href="../api/ops/sign.html#c.psa_verify_hash" title="psa_verify_hash"><code class="xref any c c-func docutils literal"><span class="pre">psa_verify_hash()</span></code></a> take a message hash as
499one of their inputs. This is useful for signing pre-computed hashes, or for
500implementing hash-and-sign using a <a class="reference internal" href="../api/ops/hashes.html#hash-mp"><span class="std std-ref">multi-part hash operation</span></a>
501before signing the resulting hash. To determine which
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100502hash algorithm to use, call the macro <a class="reference internal" href="../api/ops/algorithms.html#c.PSA_ALG_GET_HASH" title="PSA_ALG_GET_HASH"><code class="xref any c c-macro docutils literal"><span class="pre">PSA_ALG_GET_HASH()</span></code></a> on the
Gilles Peskine6c723a22020-04-17 16:57:52 +0200503corresponding signature algorithm.</p>
504<p>Some hash-and-sign algorithms add padding to the message hash before completing
505the signing operation. The format of the padding that is used depends on the
506algorithm used to construct the signature.</p>
507</div>
508<div class="section" id="key-agreement">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100509<h3>3.4.3. Key agreement</h3>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200510<p>This specification defines two functions for a Diffie-Hellman-style key
511agreement where each party combines its own private key with the peer’s public
512key.</p>
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100513<p>The recommended approach is to use a
514<a class="reference internal" href="#key-derivation"><span class="std std-ref">key derivation operation</span></a> with the
515<a class="reference internal" href="../api/ops/ka.html#c.psa_key_derivation_key_agreement" title="psa_key_derivation_key_agreement"><code class="xref any c c-func docutils literal"><span class="pre">psa_key_derivation_key_agreement()</span></code></a> input function, which calculates a shared
516secret for the key derivation function.</p>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200517<p>Where an application needs direct access to the shared secret, it can call
518<a class="reference internal" href="../api/ops/ka.html#c.psa_raw_key_agreement" title="psa_raw_key_agreement"><code class="xref any c c-func docutils literal"><span class="pre">psa_raw_key_agreement()</span></code></a> instead. Note that in general the shared secret is not
519directly suitable for use as a key because it is biased.</p>
520</div>
521</div>
522<div class="section" id="randomness-and-key-generation">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100523<h2>3.5. Randomness and key generation</h2>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200524<p>We strongly recommended that implementations include a random generator,
525consisting of a cryptographically secure pseudo-random generator (CSPRNG), which
526is adequately seeded with a cryptographic-quality hardware entropy source,
527commonly referred to as a true random number generator (TRNG). Constrained
528implementations can omit the random generation functionality if they do not
529implement any algorithm that requires randomness internally, and they do not
530provide a key generation functionality. For example, a special-purpose component
531for signature verification can omit this.</p>
532<p>It is recommended that applications use <a class="reference internal" href="../api/keys/management.html#c.psa_generate_key" title="psa_generate_key"><code class="xref any c c-func docutils literal"><span class="pre">psa_generate_key()</span></code></a>,
533<a class="reference internal" href="../api/ops/ciphers.html#c.psa_cipher_generate_iv" title="psa_cipher_generate_iv"><code class="xref any c c-func docutils literal"><span class="pre">psa_cipher_generate_iv()</span></code></a> or <a class="reference internal" href="../api/ops/aead.html#c.psa_aead_generate_nonce" title="psa_aead_generate_nonce"><code class="xref any c c-func docutils literal"><span class="pre">psa_aead_generate_nonce()</span></code></a> to generate
534suitably-formatted random data, as applicable. In addition, the API includes a
535function <a class="reference internal" href="../api/ops/rng.html#c.psa_generate_random" title="psa_generate_random"><code class="xref any c c-func docutils literal"><span class="pre">psa_generate_random()</span></code></a> to generate and extract arbitrary random data.</p>
536</div>
537</div>
538
539
540 </div>
541 </div>
542 </div>
543 <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100544 <div class="sphinxsidebarwrapper"><h3><a href="../index.html"><b>PSA Crypto API</b></a></h3>
545IHI 0086<br/>
546Non-confidential<br/>
547Version 1.0.1
548<span style="color: red; font-weight: bold;"></span>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200549<ul>
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100550<li class="toctree-l1"><a class="reference internal" href="../about.html">About this document</a></li>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200551</ul>
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100552<ul class="current">
553<li class="toctree-l1"><a class="reference internal" href="intro.html">1. Introduction</a></li>
554<li class="toctree-l1"><a class="reference internal" href="goals.html">2. Design goals</a></li>
555<li class="toctree-l1 current"><a class="current reference internal" href="#">3. Functionality overview</a><ul>
556<li class="toctree-l2"><a class="reference internal" href="#library-management">3.1. Library management</a></li>
557<li class="toctree-l2"><a class="reference internal" href="#key-management">3.2. Key management</a><ul>
558<li class="toctree-l3"><a class="reference internal" href="#key-identifiers">3.2.1. Key identifiers</a></li>
559<li class="toctree-l3"><a class="reference internal" href="#key-lifetimes">3.2.2. Key lifetimes</a></li>
560<li class="toctree-l3"><a class="reference internal" href="#key-policies">3.2.3. Key policies</a></li>
561<li class="toctree-l3"><a class="reference internal" href="#recommendations-of-minimum-standards-for-key-management">3.2.4. Recommendations of minimum standards for key management</a></li>
562</ul>
563</li>
564<li class="toctree-l2"><a class="reference internal" href="#symmetric-cryptography">3.3. Symmetric cryptography</a><ul>
565<li class="toctree-l3"><a class="reference internal" href="#single-part-functions">3.3.1. Single-part Functions</a></li>
566<li class="toctree-l3"><a class="reference internal" href="#multi-part-operations">3.3.2. Multi-part operations</a></li>
567<li class="toctree-l3"><a class="reference internal" href="#message-digests-hashes">3.3.3. Message digests (Hashes)</a></li>
568<li class="toctree-l3"><a class="reference internal" href="#message-authentication-codes-macs">3.3.4. Message authentication codes (MACs)</a></li>
569<li class="toctree-l3"><a class="reference internal" href="#encryption-and-decryption">3.3.5. Encryption and decryption</a></li>
570<li class="toctree-l3"><a class="reference internal" href="#authenticated-encryption-aead">3.3.6. Authenticated encryption (AEAD)</a></li>
571<li class="toctree-l3"><a class="reference internal" href="#key-derivation">3.3.7. Key derivation</a></li>
572<li class="toctree-l3"><a class="reference internal" href="#example-of-the-symmetric-cryptography-api">3.3.8. Example of the symmetric cryptography API</a></li>
573</ul>
574</li>
575<li class="toctree-l2"><a class="reference internal" href="#asymmetric-cryptography">3.4. Asymmetric cryptography</a><ul>
576<li class="toctree-l3"><a class="reference internal" href="#asymmetric-encryption">3.4.1. Asymmetric encryption</a></li>
577<li class="toctree-l3"><a class="reference internal" href="#hash-and-sign">3.4.2. Hash-and-sign</a></li>
578<li class="toctree-l3"><a class="reference internal" href="#key-agreement">3.4.3. Key agreement</a></li>
579</ul>
580</li>
581<li class="toctree-l2"><a class="reference internal" href="#randomness-and-key-generation">3.5. Randomness and key generation</a></li>
582</ul>
583</li>
584<li class="toctree-l1"><a class="reference internal" href="sample-arch.html">4. Sample architectures</a></li>
585<li class="toctree-l1"><a class="reference internal" href="conventions.html">5. Library conventions</a></li>
586<li class="toctree-l1"><a class="reference internal" href="implementation.html">6. Implementation considerations</a></li>
587<li class="toctree-l1"><a class="reference internal" href="usage.html">7. Usage considerations</a></li>
588<li class="toctree-l1"><a class="reference internal" href="../api/library/index.html">8. Library management reference</a></li>
589<li class="toctree-l1"><a class="reference internal" href="../api/keys/index.html">9. Key management reference</a></li>
590<li class="toctree-l1"><a class="reference internal" href="../api/ops/index.html">10. Cryptographic operation reference</a></li>
591</ul>
592<ul>
593<li class="toctree-l1"><a class="reference internal" href="../appendix/example_header.html">Example header file</a></li>
594<li class="toctree-l1"><a class="reference internal" href="../appendix/specdef_values.html">Example macro implementations</a></li>
595<li class="toctree-l1"><a class="reference internal" href="../appendix/history.html">Changes to the API</a></li>
596</ul>
597<ul>
598<li class="toctree-l1"><a class="reference internal" href="../psa_c-identifiers.html">Index of API elements</a></li>
599</ul>
Gilles Peskine6c723a22020-04-17 16:57:52 +0200600<div id="searchbox" style="display: none" role="search">
601 <h3>Quick search</h3>
602 <form class="search" action="../search.html" method="get">
603 <div><input type="text" name="q" /></div>
604 <div><input type="submit" value="Go" /></div>
605 <input type="hidden" name="check_keywords" value="yes" />
606 <input type="hidden" name="area" value="default" />
607 </form>
608</div>
609<script type="text/javascript">$('#searchbox').show(0);</script>
610 </div>
611 </div>
612 <div class="clearer"></div>
613 </div>
614 <div class="footer">
Gilles Peskinec2db5f02021-01-18 20:36:53 +0100615 &copy; 2018-2020, Arm Limited or its affiliates. All rights reserved.
Gilles Peskine6c723a22020-04-17 16:57:52 +0200616
617 |
618 Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.7</a>
619 &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
620
Gilles Peskine6c723a22020-04-17 16:57:52 +0200621 </div>
622
623
624
625
626 </body>
627</html>