Zephyr API Documentation 4.4.0
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
13
14#ifndef ZEPHYR_INCLUDE_CRYPTO_H_
15#define ZEPHYR_INCLUDE_CRYPTO_H_
16
17#include <zephyr/device.h>
18#include <errno.h>
19#include <zephyr/sys/util.h>
20#include <zephyr/sys/__assert.h>
21#include <zephyr/crypto/hash.h>
22#include "cipher.h"
23
32
33/* ctx.flags values. Not all drivers support all flags.
34 * A user app can query the supported hw / driver
35 * capabilities via provided API (crypto_query_hwcaps()), and choose a
36 * supported config during the session setup.
37 */
38#define CAP_OPAQUE_KEY_HNDL BIT(0)
39#define CAP_RAW_KEY BIT(1)
40
41/* TBD to define */
42#define CAP_KEY_LOADING_API BIT(2)
43
45#define CAP_INPLACE_OPS BIT(3)
46#define CAP_SEPARATE_IO_BUFS BIT(4)
47
52#define CAP_SYNC_OPS BIT(5)
53#define CAP_ASYNC_OPS BIT(6)
54
56#define CAP_AUTONONCE BIT(7)
57
59#define CAP_NO_IV_PREFIX BIT(8)
60
61/* More flags to be added as necessary */
62
64__subsystem struct crypto_driver_api {
65 int (*query_hw_caps)(const struct device *dev);
66
67 /* Setup a crypto session */
68 int (*cipher_begin_session)(const struct device *dev, struct cipher_ctx *ctx,
69 enum cipher_algo algo, enum cipher_mode mode,
70 enum cipher_op op_type);
71
72 /* Tear down an established session */
73 int (*cipher_free_session)(const struct device *dev, struct cipher_ctx *ctx);
74
75 /* Register async crypto op completion callback with the driver */
77
78 /* Setup a hash session */
79 int (*hash_begin_session)(const struct device *dev, struct hash_ctx *ctx,
80 enum hash_algo algo);
81 /* Tear down an established hash session */
82 int (*hash_free_session)(const struct device *dev, struct hash_ctx *ctx);
83 /* Register async hash op completion callback with the driver */
84 int (*hash_async_callback_set)(const struct device *dev, hash_completion_cb cb);
85};
86
87/* Following are the public API a user app may call.
88 * The first two relate to crypto "session" setup / teardown. Further we
89 * have four cipher mode specific (CTR, CCM, CBC ...) calls to perform the
90 * actual crypto operation in the context of a session. Also we have an
91 * API to provide the callback for async operations.
92 */
93
105static inline int crypto_query_hwcaps(const struct device *dev)
106{
107 struct crypto_driver_api *api;
108 int tmp;
109
110 api = (struct crypto_driver_api *)dev->api;
111
112 tmp = api->query_hw_caps(dev);
113
114 __ASSERT((tmp & (CAP_OPAQUE_KEY_HNDL | CAP_RAW_KEY)) != 0,
115 "Driver should support at least one key type: RAW/Opaque");
116
117 __ASSERT((tmp & (CAP_INPLACE_OPS | CAP_SEPARATE_IO_BUFS)) != 0,
118 "Driver should support at least one IO buf type: Inplace/separate");
119
120 __ASSERT((tmp & (CAP_SYNC_OPS | CAP_ASYNC_OPS)) != 0,
121 "Driver should support at least one op-type: sync/async");
122 return tmp;
123}
124
128
135
155static inline int cipher_begin_session(const struct device *dev, struct cipher_ctx *ctx,
156 enum cipher_algo algo, enum cipher_mode mode,
157 enum cipher_op optype)
158{
159 struct crypto_driver_api *api;
161
162 api = (struct crypto_driver_api *)dev->api;
163 ctx->device = dev;
164 ctx->ops.cipher_mode = mode;
165
167 __ASSERT(flags != 0U, "Keytype missing: RAW Key or OPAQUE handle");
168 __ASSERT(flags != (CAP_OPAQUE_KEY_HNDL | CAP_RAW_KEY), "conflicting options for keytype");
169
171 __ASSERT(flags != 0U, "IO buffer type missing");
173 "conflicting options for IO buffer type");
174
175 flags = (ctx->flags & (CAP_SYNC_OPS | CAP_ASYNC_OPS));
176 __ASSERT(flags != 0U, "sync/async type missing");
177 __ASSERT(flags != (CAP_SYNC_OPS | CAP_ASYNC_OPS), "conflicting options for sync/async");
178
179 return api->cipher_begin_session(dev, ctx, algo, mode, optype);
180}
181
193static inline int cipher_free_session(const struct device *dev, struct cipher_ctx *ctx)
194{
195 struct crypto_driver_api *api;
196
197 api = (struct crypto_driver_api *)dev->api;
198
199 return api->cipher_free_session(dev, ctx);
200}
201
216static inline int cipher_callback_set(const struct device *dev, cipher_completion_cb cb)
217{
218 struct crypto_driver_api *api;
219
220 api = (struct crypto_driver_api *)dev->api;
221
222 if (api->cipher_async_callback_set) {
223 return api->cipher_async_callback_set(dev, cb);
224 }
225
226 return -ENOTSUP;
227}
228
238static inline int cipher_block_op(struct cipher_ctx *ctx, struct cipher_pkt *pkt)
239{
240 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_ECB,
241 "ECB mode "
242 "session invoking a different mode handler");
243
244 pkt->ctx = ctx;
245 return ctx->ops.block_crypt_hndlr(ctx, pkt);
246}
247
259static inline int cipher_cbc_op(struct cipher_ctx *ctx, struct cipher_pkt *pkt, uint8_t *iv)
260{
261 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CBC,
262 "CBC mode "
263 "session invoking a different mode handler");
264
265 pkt->ctx = ctx;
266 return ctx->ops.cbc_crypt_hndlr(ctx, pkt, iv);
267}
268
280static inline int cipher_cfb_op(struct cipher_ctx *ctx,
281 struct cipher_pkt *pkt, uint8_t *iv)
282{
283 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CFB, "CFB mode "
284 "session invoking a different mode handler");
285
286 pkt->ctx = ctx;
287 return ctx->ops.cfb_crypt_hndlr(ctx, pkt, iv);
288}
289
301static inline int cipher_ofb_op(struct cipher_ctx *ctx,
302 struct cipher_pkt *pkt, uint8_t *iv)
303{
304 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_OFB, "OFB mode "
305 "session invoking a different mode handler");
306
307 pkt->ctx = ctx;
308 return ctx->ops.ofb_crypt_hndlr(ctx, pkt, iv);
309}
310
328static inline int cipher_ctr_op(struct cipher_ctx *ctx, struct cipher_pkt *pkt, uint8_t *iv)
329{
330 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CTR,
331 "CTR mode "
332 "session invoking a different mode handler");
333
334 pkt->ctx = ctx;
335 return ctx->ops.ctr_crypt_hndlr(ctx, pkt, iv);
336}
337
350static inline int cipher_ccm_op(struct cipher_ctx *ctx, struct cipher_aead_pkt *pkt, uint8_t *nonce)
351{
352 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CCM,
353 "CCM mode "
354 "session invoking a different mode handler");
355
356 pkt->pkt->ctx = ctx;
357 return ctx->ops.ccm_crypt_hndlr(ctx, pkt, nonce);
358}
359
372static inline int cipher_gcm_op(struct cipher_ctx *ctx, struct cipher_aead_pkt *pkt, uint8_t *nonce)
373{
374 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_GCM,
375 "GCM mode "
376 "session invoking a different mode handler");
377
378 pkt->pkt->ctx = ctx;
379 return ctx->ops.gcm_crypt_hndlr(ctx, pkt, nonce);
380}
381
403static inline int cipher_ctr_drbg_op(struct cipher_ctx *ctx, struct cipher_pkt *pkt, uint8_t *iv)
404{
406 "CTR-DRBG mode "
407 "session invoking a different mode handler");
408
409 pkt->ctx = ctx;
410 return ctx->ops.ctr_drbg_crypt_hndlr(ctx, pkt, iv);
411}
412
416
423
441static inline int hash_begin_session(const struct device *dev, struct hash_ctx *ctx,
442 enum hash_algo algo)
443{
445 struct crypto_driver_api *api;
446
447 api = (struct crypto_driver_api *)dev->api;
448 ctx->device = dev;
449
451 __ASSERT(flags != 0U, "IO buffer type missing");
453 "conflicting options for IO buffer type");
454
455 flags = (ctx->flags & (CAP_SYNC_OPS | CAP_ASYNC_OPS));
456 __ASSERT(flags != 0U, "sync/async type missing");
457 __ASSERT(flags != (CAP_SYNC_OPS | CAP_ASYNC_OPS), "conflicting options for sync/async");
458
459 return api->hash_begin_session(dev, ctx, algo);
460}
461
473static inline int hash_free_session(const struct device *dev, struct hash_ctx *ctx)
474{
475 struct crypto_driver_api *api;
476
477 api = (struct crypto_driver_api *)dev->api;
478
479 return api->hash_free_session(dev, ctx);
480}
481
496static inline int hash_callback_set(const struct device *dev, hash_completion_cb cb)
497{
498 struct crypto_driver_api *api;
499
500 api = (struct crypto_driver_api *)dev->api;
501
502 if (api->hash_async_callback_set) {
503 return api->hash_async_callback_set(dev, cb);
504 }
505
506 return -ENOTSUP;
507}
508
517static inline int hash_compute(struct hash_ctx *ctx, struct hash_pkt *pkt)
518{
519 pkt->ctx = ctx;
520
521 return ctx->hash_hndlr(ctx, pkt, true);
522}
523
536static inline int hash_update(struct hash_ctx *ctx, struct hash_pkt *pkt)
537{
538 pkt->ctx = ctx;
539
540 return ctx->hash_hndlr(ctx, pkt, false);
541}
542
546
547#endif /* ZEPHYR_INCLUDE_CRYPTO_H_ */
Crypto Cipher structure definitions.
System error numbers.
static int cipher_block_op(struct cipher_ctx *ctx, struct cipher_pkt *pkt)
Perform single-block crypto operation (ECB cipher mode).
Definition crypto.h:238
void(* cipher_completion_cb)(struct cipher_pkt *completed, int status)
Definition cipher.h:287
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:155
cipher_op
Cipher Operation.
Definition cipher.h:33
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:259
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:372
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:350
cipher_algo
Cipher Algorithm.
Definition cipher.h:28
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:301
static int cipher_free_session(const struct device *dev, struct cipher_ctx *ctx)
Cleanup a crypto session.
Definition crypto.h:193
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:216
static int cipher_ctr_drbg_op(struct cipher_ctx *ctx, struct cipher_pkt *pkt, uint8_t *iv)
Perform Counter-Deterministic Random Bit Generator (CTR-DRBG) mode crypto operation.
Definition crypto.h:403
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:280
cipher_mode
Possible cipher mode options.
Definition cipher.h:43
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:328
@ CRYPTO_CIPHER_MODE_CTR_DRBG
Definition cipher.h:51
@ CRYPTO_CIPHER_MODE_GCM
Galois/Counter mode.
Definition cipher.h:48
@ CRYPTO_CIPHER_MODE_ECB
Electronic Codebook mode.
Definition cipher.h:44
@ CRYPTO_CIPHER_MODE_CCM
Counter with CBC-MAC mode.
Definition cipher.h:47
@ CRYPTO_CIPHER_MODE_CTR
Counter mode.
Definition cipher.h:46
@ CRYPTO_CIPHER_MODE_CBC
Cipher Block Chaining mode.
Definition cipher.h:45
@ CRYPTO_CIPHER_MODE_OFB
Output Feedback mode.
Definition cipher.h:50
@ CRYPTO_CIPHER_MODE_CFB
Cipher Feedback mode.
Definition cipher.h:49
void(* hash_completion_cb)(struct hash_pkt *completed, int status)
Definition hash.h:114
static int hash_compute(struct hash_ctx *ctx, struct hash_pkt *pkt)
Perform a cryptographic hash function.
Definition crypto.h:517
static int hash_begin_session(const struct device *dev, struct hash_ctx *ctx, enum hash_algo algo)
Setup a hash session.
Definition crypto.h:441
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:496
hash_algo
Hash algorithm.
Definition hash.h:26
static int hash_free_session(const struct device *dev, struct hash_ctx *ctx)
Cleanup a hash session.
Definition crypto.h:473
static int hash_update(struct hash_ctx *ctx, struct hash_pkt *pkt)
Perform a cryptographic multipart hash operation.
Definition crypto.h:536
#define CAP_SYNC_OPS
These denotes if the output (completion of a cipher_xxx_op) is conveyed by the op function returning,...
Definition crypto.h:52
#define CAP_INPLACE_OPS
Whether the output is placed in separate buffer or not.
Definition crypto.h:45
#define CAP_ASYNC_OPS
Definition crypto.h:53
#define CAP_OPAQUE_KEY_HNDL
Definition crypto.h:38
#define CAP_SEPARATE_IO_BUFS
Definition crypto.h:46
#define CAP_RAW_KEY
Definition crypto.h:39
static int crypto_query_hwcaps(const struct device *dev)
Query the crypto hardware capabilities.
Definition crypto.h:105
#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:262
struct cipher_pkt * pkt
Definition cipher.h:264
Structure encoding session parameters.
Definition cipher.h:154
const struct device * device
The device driver instance this crypto context relates to.
Definition cipher.h:175
uint16_t flags
How certain fields are to be interpreted for this session.
Definition cipher.h:214
struct cipher_ops ops
Place for driver to return function pointers to be invoked per cipher operation.
Definition cipher.h:160
block_op_t block_crypt_hndlr
Definition cipher.h:111
gcm_op_t gcm_crypt_hndlr
Definition cipher.h:115
ctr_drbg_op_t ctr_drbg_crypt_hndlr
Definition cipher.h:120
enum cipher_mode cipher_mode
Definition cipher.h:108
cfb_op_t cfb_crypt_hndlr
Handler for CFB operations.
Definition cipher.h:117
cbc_op_t cbc_crypt_hndlr
Definition cipher.h:112
ctr_op_t ctr_crypt_hndlr
Definition cipher.h:113
ofb_op_t ofb_crypt_hndlr
Handler for OFB operations.
Definition cipher.h:119
ccm_op_t ccm_crypt_hndlr
Definition cipher.h:114
Structure encoding IO parameters of one cryptographic operation like encrypt/decrypt.
Definition cipher.h:225
struct cipher_ctx * ctx
Context this packet relates to.
Definition cipher.h:253
Crypto driver API definition.
Definition crypto.h:64
int(* query_hw_caps)(const struct device *dev)
Definition crypto.h:65
int(* cipher_async_callback_set)(const struct device *dev, cipher_completion_cb cb)
Definition crypto.h:76
int(* hash_begin_session)(const struct device *dev, struct hash_ctx *ctx, enum hash_algo algo)
Definition crypto.h:79
int(* cipher_free_session)(const struct device *dev, struct cipher_ctx *ctx)
Definition crypto.h:73
int(* hash_async_callback_set)(const struct device *dev, hash_completion_cb cb)
Definition crypto.h:84
int(* hash_free_session)(const struct device *dev, struct hash_ctx *ctx)
Definition crypto.h:82
int(* cipher_begin_session)(const struct device *dev, struct cipher_ctx *ctx, enum cipher_algo algo, enum cipher_mode mode, enum cipher_op op_type)
Definition crypto.h:68
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:519
Structure encoding session parameters.
Definition hash.h:47
hash_op_t hash_hndlr
Hash handler set up when the session begins.
Definition hash.h:65
uint16_t flags
How certain fields are to be interpreted for this session.
Definition hash.h:78
const struct device * device
The device driver instance this crypto context relates to.
Definition hash.h:51
Structure encoding IO parameters of a hash operation.
Definition hash.h:88
struct hash_ctx * ctx
Context this packet relates to.
Definition hash.h:107
Misc utilities.