Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
crypto.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
14
15#ifndef ZEPHYR_INCLUDE_CRYPTO_CRYPTO_H_
16#define ZEPHYR_INCLUDE_CRYPTO_CRYPTO_H_
17
18#include <zephyr/device.h>
19#include <errno.h>
20#include <zephyr/sys/util.h>
21#include <zephyr/sys/__assert.h>
22#include <zephyr/crypto/hash.h>
23#include "cipher.h"
24
33
44
46#define CAP_OPAQUE_KEY_HNDL BIT(0)
47
49#define CAP_RAW_KEY BIT(1)
50
52#define CAP_KEY_LOADING_API BIT(2)
53
55#define CAP_INPLACE_OPS BIT(3)
56
58#define CAP_SEPARATE_IO_BUFS BIT(4)
59
61#define CAP_SYNC_OPS BIT(5)
62
64#define CAP_ASYNC_OPS BIT(6)
65
67#define CAP_AUTONONCE BIT(7)
68
70#define CAP_NO_IV_PREFIX BIT(8)
71
75
80
85typedef int (*crypto_api_query_hw_caps)(const struct device *dev);
86
91typedef int (*crypto_api_cipher_begin_session)(const struct device *dev, struct cipher_ctx *ctx,
92 enum cipher_algo algo, enum cipher_mode mode,
93 enum cipher_op op_type);
94
99typedef int (*crypto_api_cipher_free_session)(const struct device *dev, struct cipher_ctx *ctx);
100
105typedef int (*crypto_api_cipher_async_callback_set)(const struct device *dev,
107
112typedef int (*crypto_api_hash_begin_session)(const struct device *dev, struct hash_ctx *ctx,
113 enum hash_algo algo);
114
119typedef int (*crypto_api_hash_free_session)(const struct device *dev, struct hash_ctx *ctx);
120
126
152
156
157/* Following are the public API a user app may call.
158 * The first two relate to crypto "session" setup / teardown. Further we
159 * have four cipher mode specific (CTR, CCM, CBC ...) calls to perform the
160 * actual crypto operation in the context of a session. Also we have an
161 * API to provide the callback for async operations.
162 */
163
175static inline int crypto_query_hwcaps(const struct device *dev)
176{
177 const struct crypto_driver_api *api = DEVICE_API_GET(crypto, dev);
178 int tmp;
179
180 tmp = api->query_hw_caps(dev);
181
182 __ASSERT((tmp & (CAP_OPAQUE_KEY_HNDL | CAP_RAW_KEY)) != 0,
183 "Driver should support at least one key type: RAW/Opaque");
184
185 __ASSERT((tmp & (CAP_INPLACE_OPS | CAP_SEPARATE_IO_BUFS)) != 0,
186 "Driver should support at least one IO buf type: Inplace/separate");
187
188 __ASSERT((tmp & (CAP_SYNC_OPS | CAP_ASYNC_OPS)) != 0,
189 "Driver should support at least one op-type: sync/async");
190 return tmp;
191
192}
193
197
204
224static inline int cipher_begin_session(const struct device *dev,
225 struct cipher_ctx *ctx,
226 enum cipher_algo algo,
227 enum cipher_mode mode,
228 enum cipher_op optype)
229{
230 const struct crypto_driver_api *api = DEVICE_API_GET(crypto, dev);
232
233 ctx->device = dev;
234 ctx->ops.cipher_mode = mode;
235
237 __ASSERT(flags != 0U, "Keytype missing: RAW Key or OPAQUE handle");
238 __ASSERT(flags != (CAP_OPAQUE_KEY_HNDL | CAP_RAW_KEY),
239 "conflicting options for keytype");
240
242 __ASSERT(flags != 0U, "IO buffer type missing");
244 "conflicting options for IO buffer type");
245
246 flags = (ctx->flags & (CAP_SYNC_OPS | CAP_ASYNC_OPS));
247 __ASSERT(flags != 0U, "sync/async type missing");
248 __ASSERT(flags != (CAP_SYNC_OPS | CAP_ASYNC_OPS),
249 "conflicting options for sync/async");
250
251 return api->cipher_begin_session(dev, ctx, algo, mode, optype);
252}
253
265static inline int cipher_free_session(const struct device *dev,
266 struct cipher_ctx *ctx)
267{
268 return DEVICE_API_GET(crypto, dev)->cipher_free_session(dev, ctx);
269}
270
285static inline int cipher_callback_set(const struct device *dev,
287{
288 const struct crypto_driver_api *api = DEVICE_API_GET(crypto, dev);
289
290 if (api->cipher_async_callback_set) {
291 return api->cipher_async_callback_set(dev, cb);
292 }
293
294 return -ENOTSUP;
295}
296
306static inline int cipher_block_op(struct cipher_ctx *ctx,
307 struct cipher_pkt *pkt)
308{
309 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_ECB, "ECB mode "
310 "session invoking a different mode handler");
311
312 pkt->ctx = ctx;
313 return ctx->ops.block_crypt_hndlr(ctx, pkt);
314}
315
327static inline int cipher_cbc_op(struct cipher_ctx *ctx,
328 struct cipher_pkt *pkt, uint8_t *iv)
329{
330 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CBC, "CBC mode "
331 "session invoking a different mode handler");
332
333 pkt->ctx = ctx;
334 return ctx->ops.cbc_crypt_hndlr(ctx, pkt, iv);
335}
336
348static inline int cipher_cfb_op(struct cipher_ctx *ctx,
349 struct cipher_pkt *pkt, uint8_t *iv)
350{
351 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CFB, "CFB mode "
352 "session invoking a different mode handler");
353
354 pkt->ctx = ctx;
355 return ctx->ops.cfb_crypt_hndlr(ctx, pkt, iv);
356}
357
369static inline int cipher_ofb_op(struct cipher_ctx *ctx,
370 struct cipher_pkt *pkt, uint8_t *iv)
371{
372 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_OFB, "OFB mode "
373 "session invoking a different mode handler");
374
375 pkt->ctx = ctx;
376 return ctx->ops.ofb_crypt_hndlr(ctx, pkt, iv);
377}
378
396static inline int cipher_ctr_op(struct cipher_ctx *ctx,
397 struct cipher_pkt *pkt, uint8_t *iv)
398{
399 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CTR, "CTR mode "
400 "session invoking a different mode handler");
401
402 pkt->ctx = ctx;
403 return ctx->ops.ctr_crypt_hndlr(ctx, pkt, iv);
404}
405
418static inline int cipher_ccm_op(struct cipher_ctx *ctx,
419 struct cipher_aead_pkt *pkt, uint8_t *nonce)
420{
421 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CCM, "CCM mode "
422 "session invoking a different mode handler");
423
424 pkt->pkt->ctx = ctx;
425 return ctx->ops.ccm_crypt_hndlr(ctx, pkt, nonce);
426}
427
440static inline int cipher_gcm_op(struct cipher_ctx *ctx,
441 struct cipher_aead_pkt *pkt, uint8_t *nonce)
442{
443 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_GCM, "GCM mode "
444 "session invoking a different mode handler");
445
446 pkt->pkt->ctx = ctx;
447 return ctx->ops.gcm_crypt_hndlr(ctx, pkt, nonce);
448}
449
450
454
461
462
480static inline int hash_begin_session(const struct device *dev,
481 struct hash_ctx *ctx,
482 enum hash_algo algo)
483{
484 const struct crypto_driver_api *api = DEVICE_API_GET(crypto, dev);
486
487 ctx->device = dev;
488
490 __ASSERT(flags != 0U, "IO buffer type missing");
492 "conflicting options for IO buffer type");
493
494 flags = (ctx->flags & (CAP_SYNC_OPS | CAP_ASYNC_OPS));
495 __ASSERT(flags != 0U, "sync/async type missing");
496 __ASSERT(flags != (CAP_SYNC_OPS | CAP_ASYNC_OPS),
497 "conflicting options for sync/async");
498
499
500 return api->hash_begin_session(dev, ctx, algo);
501}
502
514static inline int hash_free_session(const struct device *dev,
515 struct hash_ctx *ctx)
516{
517 return DEVICE_API_GET(crypto, dev)->hash_free_session(dev, ctx);
518}
519
534static inline int hash_callback_set(const struct device *dev,
536{
537 const struct crypto_driver_api *api = DEVICE_API_GET(crypto, dev);
538
539 if (api->hash_async_callback_set) {
540 return api->hash_async_callback_set(dev, cb);
541 }
542
543 return -ENOTSUP;
544}
545
554static inline int hash_compute(struct hash_ctx *ctx, struct hash_pkt *pkt)
555{
556 pkt->ctx = ctx;
557
558 return ctx->hash_hndlr(ctx, pkt, true);
559}
560
573static inline int hash_update(struct hash_ctx *ctx, struct hash_pkt *pkt)
574{
575 pkt->ctx = ctx;
576
577 return ctx->hash_hndlr(ctx, pkt, false);
578}
579
583
584#endif /* ZEPHYR_INCLUDE_CRYPTO_CRYPTO_H_ */
Crypto Cipher structure definitions.
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1425
System error numbers.
int(* crypto_api_cipher_begin_session)(const struct device *dev, struct cipher_ctx *ctx, enum cipher_algo algo, enum cipher_mode mode, enum cipher_op op_type)
Setup a crypto cipher session.
Definition crypto.h:91
int(* crypto_api_hash_free_session)(const struct device *dev, struct hash_ctx *ctx)
Cleanup a crypto hash session.
Definition crypto.h:119
int(* crypto_api_hash_async_callback_set)(const struct device *dev, hash_completion_cb cb)
Register an asynchronous crypto hash callback.
Definition crypto.h:125
int(* crypto_api_cipher_free_session)(const struct device *dev, struct cipher_ctx *ctx)
Cleanup a crypto cipher session.
Definition crypto.h:99
int(* crypto_api_query_hw_caps)(const struct device *dev)
Query crypto hardware capabilities.
Definition crypto.h:85
int(* crypto_api_cipher_async_callback_set)(const struct device *dev, cipher_completion_cb cb)
Register an asynchronous crypto cipher callback.
Definition crypto.h:105
int(* crypto_api_hash_begin_session)(const struct device *dev, struct hash_ctx *ctx, enum hash_algo algo)
Setup a crypto hash session.
Definition crypto.h:112
static int cipher_block_op(struct cipher_ctx *ctx, struct cipher_pkt *pkt)
Perform single-block crypto operation (ECB cipher mode).
Definition crypto.h:306
void(* cipher_completion_cb)(struct cipher_pkt *completed, int status)
Handle completion of an asynchronous cipher request.
Definition cipher.h:379
static int cipher_begin_session(const struct device *dev, struct cipher_ctx *ctx, enum cipher_algo algo, enum cipher_mode mode, enum cipher_op optype)
Setup a crypto session.
Definition crypto.h:224
cipher_op
Cipher operation types.
Definition cipher.h:34
static int cipher_cbc_op(struct cipher_ctx *ctx, struct cipher_pkt *pkt, uint8_t *iv)
Perform Cipher Block Chaining (CBC) crypto operation.
Definition crypto.h:327
static int cipher_gcm_op(struct cipher_ctx *ctx, struct cipher_aead_pkt *pkt, uint8_t *nonce)
Perform Galois/Counter Mode (GCM) crypto operation.
Definition crypto.h:440
static int cipher_ccm_op(struct cipher_ctx *ctx, struct cipher_aead_pkt *pkt, uint8_t *nonce)
Perform Counter with CBC-MAC (CCM) mode crypto operation.
Definition crypto.h:418
cipher_algo
Cipher algorithms.
Definition cipher.h:29
static int cipher_ofb_op(struct cipher_ctx *ctx, struct cipher_pkt *pkt, uint8_t *iv)
Perform Output Feedback (OFB) mode crypto operation.
Definition crypto.h:369
static int cipher_free_session(const struct device *dev, struct cipher_ctx *ctx)
Cleanup a crypto session.
Definition crypto.h:265
static int cipher_callback_set(const struct device *dev, cipher_completion_cb cb)
Registers an async crypto op completion callback with the driver.
Definition crypto.h:285
static int cipher_cfb_op(struct cipher_ctx *ctx, struct cipher_pkt *pkt, uint8_t *iv)
Perform Cipher Feedback (CFB) crypto operation.
Definition crypto.h:348
cipher_mode
Possible cipher mode options.
Definition cipher.h:44
static int cipher_ctr_op(struct cipher_ctx *ctx, struct cipher_pkt *pkt, uint8_t *iv)
Perform Counter (CTR) mode crypto operation.
Definition crypto.h:396
@ CRYPTO_CIPHER_MODE_GCM
Galois/Counter mode.
Definition cipher.h:49
@ CRYPTO_CIPHER_MODE_ECB
Electronic Codebook mode.
Definition cipher.h:45
@ CRYPTO_CIPHER_MODE_CCM
Counter with CBC-MAC mode.
Definition cipher.h:48
@ CRYPTO_CIPHER_MODE_CTR
Counter mode.
Definition cipher.h:47
@ CRYPTO_CIPHER_MODE_CBC
Cipher Block Chaining mode.
Definition cipher.h:46
@ CRYPTO_CIPHER_MODE_OFB
Output Feedback mode.
Definition cipher.h:51
@ CRYPTO_CIPHER_MODE_CFB
Cipher Feedback mode.
Definition cipher.h:50
void(* hash_completion_cb)(struct hash_pkt *completed, int status)
Handle completion of an asynchronous hash request.
Definition hash.h:148
static int hash_compute(struct hash_ctx *ctx, struct hash_pkt *pkt)
Perform a cryptographic hash function.
Definition crypto.h:554
static int hash_begin_session(const struct device *dev, struct hash_ctx *ctx, enum hash_algo algo)
Setup a hash session.
Definition crypto.h:480
static int hash_callback_set(const struct device *dev, hash_completion_cb cb)
Registers an async hash completion callback with the driver.
Definition crypto.h:534
hash_algo
Hash algorithms.
Definition hash.h:25
static int hash_free_session(const struct device *dev, struct hash_ctx *ctx)
Cleanup a hash session.
Definition crypto.h:514
static int hash_update(struct hash_ctx *ctx, struct hash_pkt *pkt)
Perform a cryptographic multipart hash operation.
Definition crypto.h:573
#define CAP_SYNC_OPS
Synchronous operations are supported.
Definition crypto.h:61
#define CAP_INPLACE_OPS
In-place operations are supported.
Definition crypto.h:55
#define CAP_ASYNC_OPS
Asynchronous operations with completion notifications are supported.
Definition crypto.h:64
#define CAP_OPAQUE_KEY_HNDL
Key material is referenced through an opaque driver-specific handle.
Definition crypto.h:46
#define CAP_SEPARATE_IO_BUFS
Separate input and output buffers are supported.
Definition crypto.h:58
#define CAP_RAW_KEY
Key material is supplied as raw key bytes.
Definition crypto.h:49
static int crypto_query_hwcaps(const struct device *dev)
Query the crypto hardware capabilities.
Definition crypto.h:175
#define ENOTSUP
Unsupported value.
Definition errno.h:114
Crypto Hash APIs.
flags
Definition parser.h:97
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
Structure encoding IO parameters in AEAD (Authenticated Encryption with Associated Data) scenario lik...
Definition cipher.h:341
struct cipher_pkt * pkt
Packet containing input and output buffers.
Definition cipher.h:346
Structure encoding session parameters.
Definition cipher.h:217
const struct device * device
The device driver instance this crypto context relates to.
Definition cipher.h:245
uint16_t flags
How certain fields are to be interpreted for this session.
Definition cipher.h:286
struct cipher_ops ops
Place for driver to return function pointers to be invoked per cipher operation.
Definition cipher.h:223
block_op_t block_crypt_hndlr
Handler for ECB block operations.
Definition cipher.h:168
gcm_op_t gcm_crypt_hndlr
Handler for GCM authenticated operations.
Definition cipher.h:176
enum cipher_mode cipher_mode
Cipher mode associated with the active handler.
Definition cipher.h:163
cfb_op_t cfb_crypt_hndlr
Handler for CFB operations.
Definition cipher.h:178
cbc_op_t cbc_crypt_hndlr
Handler for CBC operations.
Definition cipher.h:170
ctr_op_t ctr_crypt_hndlr
Handler for CTR operations.
Definition cipher.h:172
ofb_op_t ofb_crypt_hndlr
Handler for OFB operations.
Definition cipher.h:180
ccm_op_t ccm_crypt_hndlr
Handler for CCM authenticated operations.
Definition cipher.h:174
Structure encoding IO parameters of one cryptographic operation like encrypt/decrypt.
Definition cipher.h:297
struct cipher_ctx * ctx
Context this packet relates to.
Definition cipher.h:332
<span class="mlabel">Driver Operations</span> Crypto driver operations
Definition crypto.h:130
crypto_api_cipher_begin_session cipher_begin_session
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition crypto.h:135
crypto_api_hash_free_session hash_free_session
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition crypto.h:147
crypto_api_hash_async_callback_set hash_async_callback_set
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition crypto.h:150
crypto_api_cipher_free_session cipher_free_session
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition crypto.h:138
crypto_api_query_hw_caps query_hw_caps
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition crypto.h:132
crypto_api_hash_begin_session hash_begin_session
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition crypto.h:144
crypto_api_cipher_async_callback_set cipher_async_callback_set
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition crypto.h:141
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
Structure encoding session parameters.
Definition hash.h:57
hash_op_t hash_hndlr
Hash operation handler selected for this session.
Definition hash.h:82
uint16_t flags
Flags describing how certain fields are interpreted for this session.
Definition hash.h:99
const struct device * device
Device driver instance this crypto context relates to.
Definition hash.h:63
Structure encoding IO parameters of a hash operation.
Definition hash.h:109
struct hash_ctx * ctx
Context this packet relates to.
Definition hash.h:135
Misc utilities.