-
Notifications
You must be signed in to change notification settings - Fork 25
/
wasi_ephemeral_crypto_common.witx
467 lines (387 loc) · 19 KB
/
wasi_ephemeral_crypto_common.witx
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
(module $wasi_ephemeral_crypto_common
;;; Error codes.
(typename $crypto_errno
(enum (@witx tag u16)
;;; Operation succeeded.
$success
;;; An error occurred when trying to during a conversion from a host type to a guest type.
;;;
;;; Only an internal bug can throw this error.
$guest_error
;;; The requested operation is valid, but not implemented by the host.
$not_implemented
;;; The requested feature is not supported by the chosen algorithm.
$unsupported_feature
;;; The requested operation is valid, but was administratively prohibited.
$prohibited_operation
;;; Unsupported encoding for an import or export operation.
$unsupported_encoding
;;; The requested algorithm is not supported by the host.
$unsupported_algorithm
;;; The requested option is not supported by the currently selected algorithm.
$unsupported_option
;;; An invalid or incompatible key was supplied.
;;;
;;; The key may not be valid, or was generated for a different algorithm or parameters set.
$invalid_key
;;; The currently selected algorithm doesn't support the requested output length.
;;;
;;; This error is thrown by non-extensible hash functions, when requesting an output size larger than they produce out of a single block.
$invalid_length
;;; A signature or authentication tag verification failed.
$verification_failed
;;; A secure random numbers generator is not available.
;;;
;;; The requested operation requires random numbers, but the host cannot securely generate them at the moment.
$rng_error
;;; An error was returned by the underlying cryptography library.
;;;
;;; The host may be running out of memory, parameters may be incompatible with the chosen implementation of an algorithm or another unexpected error may have happened.
;;;
;;; Ideally, the specification should provide enough details and guidance to make this error impossible to ever be thrown.
;;;
;;; Realistically, the WASI crypto module cannot possibly cover all possible error types implementations can return, especially since some of these may be language-specific.
;;; This error can thus be thrown when other error types are not suitable, and when the original error comes from the cryptographic primitives themselves and not from the WASI module.
$algorithm_failure
;;; The supplied signature is invalid, or incompatible with the chosen algorithm.
$invalid_signature
;;; An attempt was made to close a handle that was already closed.
$closed
;;; A function was called with an unassigned handle, a closed handle, or handle of an unexpected type.
$invalid_handle
;;; The host needs to copy data to a guest-allocated buffer, but that buffer is too small.
$overflow
;;; An internal error occurred.
;;;
;;; This error is reserved to internal consistency checks, and must only be sent if the internal state of the host remains safe after an inconsistency was detected.
$internal_error
;;; Too many handles are currently open, and a new one cannot be created.
;;;
;;; Implementations are free to represent handles as they want, and to enforce limits to limit resources usage.
$too_many_handles
;;; A key was provided, but the chosen algorithm doesn't support keys.
;;;
;;; This is returned by symmetric operations.
;;;
;;; Many hash functions, in particular, do not support keys without being used in particular constructions.
;;; Blindly ignoring a key provided by mistake while trying to open a context for such as function could cause serious security vulnerabilities.
;;;
;;; These functions must refuse to create the context and return this error instead.
$key_not_supported
;;; A key is required for the chosen algorithm, but none was given.
$key_required
;;; The provided authentication tag is invalid or incompatible with the current algorithm.
;;;
;;; This error is returned by decryption functions and tag verification functions.
;;;
;;; Unlike `verification_failed`, this error code is returned when the tag cannot possibly verify for any input.
$invalid_tag
;;; The requested operation is incompatible with the current scheme.
;;;
;;; For example, the `symmetric_state_encrypt()` function cannot complete if the selected construction is a key derivation function.
;;; This error code will be returned instead.
$invalid_operation
;;; A nonce is required.
;;;
;;; Most encryption schemes require a nonce.
;;;
;;; In the absence of a nonce, the WASI cryptography module can automatically generate one, if that can be done safely. The nonce can be retrieved later with the `symmetric_state_option_get()` function using the `nonce` parameter.
;;; If automatically generating a nonce cannot be done safely, the module never falls back to an insecure option and requests an explicit nonce by throwing that error.
$nonce_required
;;; The provided nonce doesn't have a correct size for the given cipher.
$invalid_nonce
;;; The named option was not set.
;;;
;;; The caller tried to read the value of an option that was not set.
;;; This error is used to make the distinction between an empty option, and an option that was not set and left to its default value.
$option_not_set
;;; A key or key pair matching the requested identifier cannot be found using the supplied information.
;;;
;;; This error is returned by a secrets manager via the `keypair_from_id()` function.
$not_found
;;; The algorithm requires parameters that haven't been set.
;;;
;;; Non-generic options are required and must be given by building an `options` set and giving that object to functions instantiating that algorithm.
$parameters_missing
;;; A requested computation is not done yet, and additional calls to the function are required.
;;;
;;; Some functions, such as functions generating key pairs and password stretching functions, can take a long time to complete.
;;;
;;; In order to avoid a host call to be blocked for too long, these functions can return prematurely, requiring additional calls with the same parameters until they complete.
$in_progress
;;; Multiple keys have been provided, but they do not share the same type.
;;;
;;; This error is returned when trying to build a key pair from a public key and a secret key that were created for different and incompatible algorithms.
$incompatible_keys
;;; A managed key or secret expired and cannot be used any more.
$expired
)
)
;;; Encoding to use for importing or exporting a key pair.
(typename $keypair_encoding
(enum (@witx tag u16)
;;; Raw bytes.
$raw
;;; PCSK8/DER encoding.
$pkcs8
;;; PEM encoding.
$pem
;;; Implementation-defined encoding.
$local
)
)
;;; Encoding to use for importing or exporting a public key.
(typename $publickey_encoding
(enum (@witx tag u16)
;;; Raw bytes.
$raw
;;; PKCS8/DER encoding.
$pkcs8
;;; PEM encoding.
$pem
;;; SEC-1 encoding.
$sec
;;; Implementation-defined encoding.
$local
)
)
;;; Encoding to use for importing or exporting a secret key.
(typename $secretkey_encoding
(enum (@witx tag u16)
;;; Raw bytes.
$raw
;;; PKCS8/DER encoding.
$pkcs8
;;; PEM encoding.
$pem
;;; SEC encoding.
$sec
;;; Implementation-defined encoding.
$local
)
)
;;; Encoding to use for importing or exporting a signature.
(typename $signature_encoding
(enum (@witx tag u16)
;;; Raw bytes.
$raw
;;; DER encoding.
$der
)
)
;;; An algorithm category.
(typename $algorithm_type
(enum (@witx tag u16)
$signatures
$symmetric
$key_exchange
)
)
;;; Version of a managed key.
;;;
;;; A version can be an arbitrary `u64` integer, with the expection of some reserved values.
(typename $version u64)
;;; Key doesn't support versioning.
(@witx const $version $unspecified 0xff00000000000000)
;;; Use the latest version of a key.
(@witx const $version $latest 0xff00000000000001)
;;; Perform an operation over all versions of a key.
(@witx const $version $all 0xff00000000000002)
;;; Size of a value.
(typename $size (@witx usize))
;;; A UNIX timestamp, in seconds since 01/01/1970.
(typename $timestamp u64)
;;; A 64-bit value
(typename $u64 u64)
(resource $handle)
;;; Handle for functions returning output whose size may be large or not known in advance.
;;;
;;; An `array_output` object contains a host-allocated byte array.
;;;
;;; A guest can get the size of that array after a function returns in order to then allocate a buffer of the correct size.
;;; In addition, the content of such an object can be consumed by a guest in a streaming fashion.
;;;
;;; An `array_output` handle is automatically closed after its full content has been consumed.
(typename $array_output (handle $handle))
;;; A set of options.
;;;
;;; This type is used to set non-default parameters.
;;;
;;; The exact set of allowed options depends on the algorithm being used.
(typename $options (handle $handle))
;;; A handle to the optional secrets management facilities offered by a host.
;;;
;;; This is used to generate, retrieve and invalidate managed keys.
(typename $secrets_manager (handle $handle))
;;; A key pair.
(typename $keypair (handle $handle))
;;; A state to absorb data to be signed.
;;;
;;; After a signature has been computed or verified, the state remains valid for further operations.
;;;
;;; A subsequent signature would sign all the data accumulated since the creation of the state object.
(typename $signature_state (handle $handle))
;;; A signature.
(typename $signature (handle $handle))
;;; A public key, for key exchange and signature verification.
(typename $publickey (handle $handle))
;;; A secret key, for key exchange mechanisms.
(typename $secretkey (handle $handle))
;;; A state to absorb signed data to be verified.
(typename $signature_verification_state (handle $handle))
;;; A state to perform symmetric operations.
;;;
;;; The state is not reset nor invalidated after an option has been performed.
;;; Incremental updates and sessions are thus supported.
(typename $symmetric_state (handle $handle))
;;; A symmetric key.
;;;
;;; The key can be imported from raw bytes, or can be a reference to a managed key.
;;;
;;; If it was imported, the host will wipe it from memory as soon as the handle is closed.
(typename $symmetric_key (handle $handle))
;;; An authentication tag.
;;;
;;; This is an object returned by functions computing authentication tags.
;;;
;;; A tag can be compared against another tag (directly supplied as raw bytes) in constant time with the `symmetric_tag_verify()` function.
;;;
;;; This object type can't be directly created from raw bytes. They are only returned by functions computing MACs.
;;;
;;; The host is reponsible for securely wiping them from memory on close.
(typename $symmetric_tag (handle $handle))
;;; Options index, only required by the Interface Types translation layer.
(typename $opt_options_u (enum (@witx tag u8) $some $none))
;;; An optional options set.
;;;
;;; This union simulates an `Option<Options>` type to make the `options` parameter of some functions optional.
(typename $opt_options (variant (@witx tag $opt_options_u) (case $some $options) (case $none)))
;;; Symmetric key index, only required by the Interface Types translation layer.
(typename $opt_symmetric_key_u (enum (@witx tag u8) $some $none))
;;; An optional symmetric key.
;;;
;;; This union simulates an `Option<SymmetricKey>` type to make the `symmetric_key` parameter of some functions optional.
(typename $opt_symmetric_key (variant (@witx tag $opt_symmetric_key_u) (case $some $symmetric_key) (case $none)))
;;; Create a new object to set non-default options.
;;;
;;; Example usage:
;;;
;;; ```rust
;;; let options_handle = options_open(AlgorithmType::Symmetric)?;
;;; options_set(options_handle, "context", context)?;
;;; options_set_u64(options_handle, "threads", 4)?;
;;; let state = symmetric_state_open("BLAKE3", None, Some(options_handle))?;
;;; options_close(options_handle)?;
;;; ```
(@interface func (export "options_open")
(param $algorithm_type $algorithm_type)
(result $error (expected $options (error $crypto_errno)))
)
;;; Destroy an options object.
;;;
;;; Objects are reference counted. It is safe to close an object immediately after the last function needing it is called.
(@interface func (export "options_close")
(param $handle $options)
(result $error (expected (error $crypto_errno)))
)
;;; Set or update an option.
;;;
;;; This is used to set algorithm-specific parameters, but also to provide credentials for the secrets management facilities, if required.
;;;
;;; This function may return `unsupported_option` if an option that doesn't exist for any implemented algorithms is specified.
(@interface func (export "options_set")
(param $handle $options)
(param $name string)
(param $value (@witx const_pointer u8))
(param $value_len $size)
(result $error (expected (error $crypto_errno)))
)
;;; Set or update an integer option.
;;;
;;; This is used to set algorithm-specific parameters.
;;;
;;; This function may return `unsupported_option` if an option that doesn't exist for any implemented algorithms is specified.
(@interface func (export "options_set_u64")
(param $handle $options)
(param $name string)
(param $value u64)
(result $error (expected (error $crypto_errno)))
)
;;; Set or update a guest-allocated memory that the host can use or return data into.
;;;
;;; This is for example used to set the scratch buffer required by memory-hard functions.
;;;
;;; This function may return `unsupported_option` if an option that doesn't exist for any implemented algorithms is specified.
(@interface func (export "options_set_guest_buffer")
(param $handle $options)
(param $name string)
(param $buffer (@witx pointer u8))
(param $buffer_len $size)
(result $error (expected (error $crypto_errno)))
)
;;; Return the length of an `array_output` object.
;;;
;;; This allows a guest to allocate a buffer of the correct size in order to copy the output of a function returning this object type.
(@interface func (export "array_output_len")
(param $array_output $array_output)
(result $error (expected $size (error $crypto_errno)))
)
;;; Copy the content of an `array_output` object into an application-allocated buffer.
;;;
;;; Multiple calls to that function can be made in order to consume the data in a streaming fashion, if necessary.
;;;
;;; The function returns the number of bytes that were actually copied. `0` means that the end of the stream has been reached. The total size always matches the output of `array_output_len()`.
;;;
;;; The handle is automatically closed after all the data has been consumed.
;;;
;;; Example usage:
;;;
;;; ```rust
;;; let len = array_output_len(output_handle)?;
;;; let mut out = vec![0u8; len];
;;; array_output_pull(output_handle, &mut out)?;
;;; ```
(@interface func (export "array_output_pull")
(param $array_output $array_output)
(param $buf (@witx pointer u8))
(param $buf_len $size)
(result $error (expected $size (error $crypto_errno)))
)
;;; __(optional)__
;;; Create a context to use a secrets manager.
;;;
;;; The set of required and supported options is defined by the host.
;;;
;;; The function returns the `unsupported_feature` error code if secrets management facilities are not supported by the host.
;;; This is also an optional import, meaning that the function may not even exist.
(@interface func (export "secrets_manager_open")
(param $options $opt_options)
(result $error (expected $secrets_manager (error $crypto_errno)))
)
;;; __(optional)__
;;; Destroy a secrets manager context.
;;;
;;; The function returns the `unsupported_feature` error code if secrets management facilities are not supported by the host.
;;; This is also an optional import, meaning that the function may not even exist.
(@interface func (export "secrets_manager_close")
(param $secrets_manager $secrets_manager)
(result $error (expected (error $crypto_errno)))
)
;;; __(optional)__
;;; Invalidate a managed key or key pair given an identifier and a version.
;;;
;;; This asks the secrets manager to delete or revoke a stored key, a specific version of a key.
;;;
;;; `key_version` can be set to a version number, to `version.latest` to invalidate the current version, or to `version.all` to invalidate all versions of a key.
;;;
;;; The function returns `unsupported_feature` if this operation is not supported by the host, and `not_found` if the identifier and version don't match any existing key.
;;;
;;; This is an optional import, meaning that the function may not even exist.
(@interface func (export "secrets_manager_invalidate")
(param $secrets_manager $secrets_manager)
(param $key_id (@witx const_pointer u8))
(param $key_id_len $size)
(param $key_version $version)
(result $error (expected (error $crypto_errno)))
)
)