aboutsummaryrefslogtreecommitdiff
path: root/vendor/doc/html/crypto_lock_aead_auth.html
blob: fd6c6888101e5778b282b21e981eb9f7db9403b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8"/>
  <style>
    table.head, table.foot { width: 100%; }
    td.head-rtitle, td.foot-os { text-align: right; }
    td.head-vol { text-align: center; }
    div.Pp { margin: 1ex 0ex; }
  </style>
  <link rel="stylesheet" href="style.css" type="text/css" media="all"/>
  <title>CRYPTO_LOCK_INIT(3MONOCYPHER)</title>
</head>
<body>
<table class="head">
  <tr>
    <td class="head-ltitle">CRYPTO_LOCK_INIT(3MONOCYPHER)</td>
    <td class="head-vol">3MONOCYPHER</td>
    <td class="head-rtitle">CRYPTO_LOCK_INIT(3MONOCYPHER)</td>
  </tr>
</table>
<div class="manual-text">
<h1 class="Sh" title="Sh" id="NAME"><a class="selflink" href="#NAME">NAME</a></h1>
<b class="Nm" title="Nm">crypto_lock_init</b>,
  <b class="Nm" title="Nm">crypto_lock_aead_auth</b>,
  <b class="Nm" title="Nm">crypto_lock_update</b>,
  <b class="Nm" title="Nm">crypto_lock_final</b>,
  <b class="Nm" title="Nm">crypto_unlock_init</b>,
  <b class="Nm" title="Nm">crypto_unlock_aead_auth</b>,
  <b class="Nm" title="Nm">crypto_unlock_update</b>,
  <b class="Nm" title="Nm">crypto_unlock_final</b>,
  <b class="Nm" title="Nm">crypto_lock_auth</b>,
  <b class="Nm" title="Nm">crypto_lock_encrypt</b> &#x2014;
  <span class="Nd" title="Nd">incremental authenticated encryption with
  additional data</span>
<h1 class="Sh" title="Sh" id="SYNOPSIS"><a class="selflink" href="#SYNOPSIS">SYNOPSIS</a></h1>
<b class="In" title="In">#include
  &lt;<a class="In" title="In">monocypher.h</a>&gt;</b>
<div class="Pp"></div>
<var class="Ft" title="Ft">void</var>
<br/>
<b class="Fn" title="Fn">crypto_lock_init</b>(<var class="Fa" title="Fa">crypto_lock_ctx
  *ctx</var>, <var class="Fa" title="Fa">const uint8_t key[32]</var>,
  <var class="Fa" title="Fa">const uint8_t nonce[24]</var>);
<div class="Pp"></div>
<var class="Ft" title="Ft">void</var>
<br/>
<b class="Fn" title="Fn">crypto_lock_aead_auth</b>(<var class="Fa" title="Fa">crypto_lock_ctx
  *ctx</var>, <var class="Fa" title="Fa">const uint8_t *ad</var>,
  <var class="Fa" title="Fa">size_t ad_size</var>);
<div class="Pp"></div>
<var class="Ft" title="Ft">void</var>
<br/>
<b class="Fn" title="Fn">crypto_lock_update</b>(<var class="Fa" title="Fa">crypto_lock_ctx
  *ctx</var>, <var class="Fa" title="Fa">uint8_t *cipher_text</var>,
  <var class="Fa" title="Fa">const uint8_t *plain_text</var>,
  <var class="Fa" title="Fa">size_t text_size</var>);
<div class="Pp"></div>
<var class="Ft" title="Ft">void</var>
<br/>
<b class="Fn" title="Fn">crypto_lock_final</b>(<var class="Fa" title="Fa">crypto_lock_ctx
  *ctx</var>, <var class="Fa" title="Fa">uint8_t mac[16]</var>);
<div class="Pp"></div>
<var class="Ft" title="Ft">void</var>
<br/>
<b class="Fn" title="Fn">crypto_unlock_init</b>(<var class="Fa" title="Fa">crypto_unlock_ctx
  *ctx</var>, <var class="Fa" title="Fa">const uint8_t key[32]</var>,
  <var class="Fa" title="Fa">const uint8_t nonce[24]</var>);
<div class="Pp"></div>
<var class="Ft" title="Ft">void</var>
<br/>
<b class="Fn" title="Fn">crypto_unlock_aead_auth</b>(<var class="Fa" title="Fa">crypto_unlock_ctx
  *ctx</var>, <var class="Fa" title="Fa">const uint8_t *ad</var>,
  <var class="Fa" title="Fa">size_t ad_size</var>);
<div class="Pp"></div>
<var class="Ft" title="Ft">void</var>
<br/>
<b class="Fn" title="Fn">crypto_unlock_update</b>(<var class="Fa" title="Fa">crypto_unlock_ctx
  *ctx</var>, <var class="Fa" title="Fa">uint8_t *plain_text</var>,
  <var class="Fa" title="Fa">const uint8_t *cipher_text</var>,
  <var class="Fa" title="Fa">size_t text_size</var>);
<div class="Pp"></div>
<var class="Ft" title="Ft">int</var>
<br/>
<b class="Fn" title="Fn">crypto_unlock_final</b>(<var class="Fa" title="Fa">crypto_unlock_ctx
  *ctx</var>, <var class="Fa" title="Fa">const uint8_t mac[16]</var>);
<div class="Pp"></div>
<var class="Ft" title="Ft">void</var>
<br/>
<b class="Fn" title="Fn">crypto_lock_auth</b>(<var class="Fa" title="Fa">crypto_lock_ctx
  *ctx</var>, <var class="Fa" title="Fa">const uint8_t *ad</var>,
  <var class="Fa" title="Fa">size_t ad_size</var>);
<div class="Pp"></div>
<var class="Ft" title="Ft">void</var>
<br/>
<b class="Fn" title="Fn">crypto_lock_encrypt</b>(<var class="Fa" title="Fa">crypto_lock_ctx
  *ctx</var>, <var class="Fa" title="Fa">uint8_t *cipher_text</var>,
  <var class="Fa" title="Fa">const uint8_t *plain_text</var>,
  <var class="Fa" title="Fa">size_t text_size</var>);
<h1 class="Sh" title="Sh" id="DESCRIPTION"><a class="selflink" href="#DESCRIPTION">DESCRIPTION</a></h1>
These functions are variants of
  <a class="Xr" title="Xr" href="crypto_lock.html">crypto_lock(3monocypher)</a>,
  <a class="Xr" title="Xr" href="crypto_unlock.html">crypto_unlock(3monocypher)</a>,
  <a class="Xr" title="Xr" href="crypto_aead_lock.html">crypto_aead_lock(3monocypher)</a>
  and
  <a class="Xr" title="Xr" href="crypto_aead_unlock.html">crypto_aead_unlock(3monocypher)</a>.
  Prefer those simpler functions if possible.
<div class="Pp"></div>
This incremental interface can be used to encrypt and decrypt messages too large
  to fit in a single buffer. The arguments are the same as described for the
  direct interface described in
  <a class="Xr" title="Xr" href="crypto_lock.html">crypto_lock(3monocypher)</a>.
<div class="Pp"></div>
Encryption requires four steps:
<ul class="Bl-bullet">
  <li class="It-bullet">Initialise a context with
      <b class="Fn" title="Fn">crypto_lock_init</b>().</li>
  <li class="It-bullet">Authenticate additional data, if any, with
      <b class="Fn" title="Fn">crypto_lock_aead_auth</b>().</li>
  <li class="It-bullet">Encrypt and authenticate some data with
      <b class="Fn" title="Fn">crypto_lock_update</b>().</li>
  <li class="It-bullet">Generate the MAC with
      <b class="Fn" title="Fn">crypto_lock_final</b>().</li>
</ul>
<div class="Pp"></div>
Decryption also requires four steps:
<ul class="Bl-bullet">
  <li class="It-bullet">Initialise a context with
      <b class="Fn" title="Fn">crypto_unlock_init</b>().</li>
  <li class="It-bullet">Verify additional data, if any, with
      <b class="Fn" title="Fn">crypto_unlock_aead_auth</b>().</li>
  <li class="It-bullet">Decrypt and verify some data with
      <b class="Fn" title="Fn">crypto_unlock_update</b>().</li>
  <li class="It-bullet">Verify the MAC with
      <b class="Fn" title="Fn">crypto_unlock_final</b>().</li>
</ul>
<div class="Pp"></div>
<b class="Fn" title="Fn">crypto_lock_encrypt</b>() encrypts or decrypts data
  <i class="Em" title="Em">without authenticating it</i>. It is meant as a
  building block. Used with <b class="Fn" title="Fn">crypto_lock_auth</b>(), it
  enables various AEAD constructions. Most users do not need either of them.
  Prefer <b class="Fn" title="Fn">crypto_lock_update</b>() and
  <b class="Fn" title="Fn">crypto_unlock_update</b>() instead.
<h1 class="Sh" title="Sh" id="RETURN_VALUES"><a class="selflink" href="#RETURN_VALUES">RETURN
  VALUES</a></h1>
<b class="Fn" title="Fn">crypto_lock_init</b>(),
  <b class="Fn" title="Fn">crypto_unlock_init</b>(),
  <b class="Fn" title="Fn">crypto_lock_auth</b>(),
  <b class="Fn" title="Fn">crypto_lock_encrypt</b>(),
  <b class="Fn" title="Fn">crypto_lock_aead_auth</b>(),
  <b class="Fn" title="Fn">crypto_unlock_aead_auth</b>(),
  <b class="Fn" title="Fn">crypto_lock_update</b>(),
  <b class="Fn" title="Fn">crypto_unlock_update</b>(), and
  <b class="Fn" title="Fn">crypto_lock_final</b>() return nothing. They cannot
  fail.
<div class="Pp"></div>
<b class="Fn" title="Fn">crypto_unlock_final</b>() returns 0 on success or -1 if
  the message was corrupted. Corruption can be caused by transmission errors,
  programmer error, or an attacker's interference.
  <i class="Em" title="Em">Always check the return value</i>.
<h1 class="Sh" title="Sh" id="EXAMPLES"><a class="selflink" href="#EXAMPLES">EXAMPLES</a></h1>
Encryption:
<div class="Pp"></div>
<div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
const uint8_t key        [ 32]; /* Session key                 */ 
const uint8_t nonce      [ 32]; /* Unique per session key      */ 
const uint8_t ad         [500]; /* Optional additional data    */ 
const uint8_t plain_text [500]; /* Secret message              */ 
uint8_t       cipher_text[500]; /* Encrypted message           */ 
uint8_t       mac        [ 16]; /* Message authentication code */ 
 
/* Set up initial context */ 
crypto_lock_ctx ctx; 
crypto_lock_init(&amp;ctx, key, nonce); 
/* Wipe the key if it is no longer needed */ 
crypto_wipe(key, 32); 
 
/* Authenticate additional data */ 
for (size_t i = 0; i &lt; 500; i += 100) { 
    crypto_lock_aead_auth(&amp;ctx, ad + i, 100); 
} 
 
/* Encrypt message */ 
for (size_t i = 0; i &lt; 500; i += 100) { 
    crypto_lock_update(&amp;ctx, cipher_text + i, plain_text + i, 100); 
    /* Wipe the secret message if it is no longer needed */ 
    crypto_wipe(plain_text + i, 100); 
} 
 
/* Produce the MAC */ 
crypto_lock_final(&amp;ctx, mac);
</pre>
</div>
<div class="Pp"></div>
To decrypt the above:
<div class="Pp"></div>
<div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
const uint8_t key        [ 32]; /* Session key              */ 
const uint8_t nonce      [ 32]; /* Unique per session key   */ 
const uint8_t mac        [ 16]; /* Transmitted MAC          */ 
const uint8_t ad         [500]; /* Optional additional data */ 
const uint8_t cipher_text[500]; /* Encrypted message        */ 
uint8_t       plain_text [500]; /* Secret message           */ 
 
/* Set up initial context */ 
crypto_unlock_ctx ctx; 
crypto_unlock_init(&amp;ctx, key, nonce); 
/* Wipe the key if it is no longer needed */ 
crypto_wipe(key, 32); 
 
/* Verify additional data */ 
for (size_t i = 0; i &lt; 500; i += 100) { 
    crypto_unlock_aead_auth(&amp;ctx, ad + i, 100); 
} 
 
/* Decrypt message */ 
for (size_t i = 0; i &lt; 500; i += 100) { 
    crypto_unlock_update(&amp;ctx, plain_text + i, cipher_text + i, 100); 
} 
 
/* Check the MAC */ 
if (crypto_unlock_final(&amp;ctx, mac)) { 
    /* Corrupted message, abort processing */ 
} else { 
    /* Genuine message */ 
} 
 
/* Wipe the secret message if it is no longer needed */ 
crypto_wipe(plain_text, 500);
</pre>
</div>
<div class="Pp"></div>
In-place encryption without additional data:
<div class="Pp"></div>
<div class="Bd" style="margin-left: 5.00ex;">
<pre class="Li">
const uint8_t key   [ 32]; /* Session key                 */ 
const uint8_t nonce [ 32]; /* Unique per session key      */ 
uint8_t       text  [500]; /* Message                     */ 
uint8_t       mac   [ 16]; /* Message authentication code */ 
 
/* Set up initial context */ 
crypto_lock_ctx ctx; 
crypto_lock_init(&amp;ctx, key, nonce); 
/* Wipe the key if it is no longer needed */ 
crypto_wipe(key, 32); 
 
/* Encrypt message */ 
for (size_t i = 0; i &lt; 500; i += 100) { 
    crypto_lock_update(&amp;ctx, text + i, text + i, 100); 
} 
 
/* Produce the MAC */ 
crypto_lock_final(&amp;ctx, mac);
</pre>
</div>
<h1 class="Sh" title="Sh" id="SEE_ALSO"><a class="selflink" href="#SEE_ALSO">SEE
  ALSO</a></h1>
<a class="Xr" title="Xr" href="crypto_aead_lock.html">crypto_aead_lock(3monocypher)</a>,
  <a class="Xr" title="Xr" href="crypto_aead_unlock.html">crypto_aead_unlock(3monocypher)</a>,
  <a class="Xr" title="Xr" href="crypto_key_exchange.html">crypto_key_exchange(3monocypher)</a>,
  <a class="Xr" title="Xr" href="crypto_lock.html">crypto_lock(3monocypher)</a>,
  <a class="Xr" title="Xr" href="crypto_unlock.html">crypto_unlock(3monocypher)</a>,
  <a class="Xr" title="Xr" href="crypto_wipe.html">crypto_wipe(3monocypher)</a>,
  <a class="Xr" title="Xr" href="intro.html">intro(3monocypher)</a>
<h1 class="Sh" title="Sh" id="STANDARDS"><a class="selflink" href="#STANDARDS">STANDARDS</a></h1>
These functions implement the XChacha20 (encryption) and Poly1305 (MAC)
  primitives. Chacha20 and Poly1305 are described in RFC 7539. XChacha20 derives
  from Chacha20 the same way XSalsa20 derives from Salsa20, and benefits from
  the same security reduction (proven secure as long as Chacha20 itself is
  secure).
<h1 class="Sh" title="Sh" id="SECURITY_CONSIDERATIONS"><a class="selflink" href="#SECURITY_CONSIDERATIONS">SECURITY
  CONSIDERATIONS</a></h1>
Messages are not verified until the call to
  <b class="Fn" title="Fn">crypto_unlock_final</b>(). Make sure to call it and
  check the return value <i class="Em" title="Em">before</i> processing the
  message. Messages may be stored before they are verified, but they cannot be
  trusted. Processing untrusted messages increases the attack surface of the
  system. Doing so securely is hard. Do not process messages before calling
  <b class="Fn" title="Fn">crypto_unlock_final</b>().
<h1 class="Sh" title="Sh" id="IMPLEMENTATION_DETAILS"><a class="selflink" href="#IMPLEMENTATION_DETAILS">IMPLEMENTATION
  DETAILS</a></h1>
<ul class="Bl-bullet">
  <li class="It-bullet"><var class="Vt" title="Vt">crypto_unlock_ctx</var> is an
      alias to <var class="Vt" title="Vt">crypto_lock_ctx</var>.</li>
  <li class="It-bullet"><b class="Fn" title="Fn">crypto_unlock_init</b>() is an
      alias to <b class="Fn" title="Fn">crypto_lock_init</b>().</li>
  <li class="It-bullet"><b class="Fn" title="Fn">crypto_lock_aead_auth</b>() and
      <b class="Fn" title="Fn">crypto_unlock_aead_auth</b>() are aliases to
      <b class="Fn" title="Fn">crypto_lock_auth</b>().</li>
</ul>
<div class="Pp"></div>
The incremental interface is roughly three times slower than the direct
  interface at identifying corrupted messages. This is because the incremental
  interface works in a single pass and has to interleave decryption and
  verification. Users who expect a high corruption rate may want to avoid
  it.</div>
<table class="foot">
  <tr>
    <td class="foot-date">December 28, 2017</td>
    <td class="foot-os">Linux 4.4.0-116-generic</td>
  </tr>
</table>
</body>
</html>