Zephyr API Documentation 4.3.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
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
34/* ctx.flags values. Not all drivers support all flags.
35 * A user app can query the supported hw / driver
36 * capabilities via provided API (crypto_query_hwcaps()), and choose a
37 * supported config during the session setup.
38 */
39#define CAP_OPAQUE_KEY_HNDL BIT(0)
40#define CAP_RAW_KEY BIT(1)
41
42/* TBD to define */
43#define CAP_KEY_LOADING_API BIT(2)
44
46#define CAP_INPLACE_OPS BIT(3)
47#define CAP_SEPARATE_IO_BUFS BIT(4)
48
53#define CAP_SYNC_OPS BIT(5)
54#define CAP_ASYNC_OPS BIT(6)
55
57#define CAP_AUTONONCE BIT(7)
58
60#define CAP_NO_IV_PREFIX BIT(8)
61
62/* More flags to be added as necessary */
63
65__subsystem struct crypto_driver_api {
66 int (*query_hw_caps)(const struct device *dev);
67
68 /* Setup a crypto session */
69 int (*cipher_begin_session)(const struct device *dev, struct cipher_ctx *ctx,
70 enum cipher_algo algo, enum cipher_mode mode,
71 enum cipher_op op_type);
72
73 /* Tear down an established session */
74 int (*cipher_free_session)(const struct device *dev, struct cipher_ctx *ctx);
75
76 /* Register async crypto op completion callback with the driver */
77 int (*cipher_async_callback_set)(const struct device *dev,
79
80 /* Setup a hash session */
81 int (*hash_begin_session)(const struct device *dev, struct hash_ctx *ctx,
82 enum hash_algo algo);
83 /* Tear down an established hash session */
84 int (*hash_free_session)(const struct device *dev, struct hash_ctx *ctx);
85 /* Register async hash op completion callback with the driver */
86 int (*hash_async_callback_set)(const struct device *dev,
88};
89
90/* Following are the public API a user app may call.
91 * The first two relate to crypto "session" setup / teardown. Further we
92 * have four cipher mode specific (CTR, CCM, CBC ...) calls to perform the
93 * actual crypto operation in the context of a session. Also we have an
94 * API to provide the callback for async operations.
95 */
96
108static inline int crypto_query_hwcaps(const struct device *dev)
109{
110 struct crypto_driver_api *api;
111 int tmp;
112
113 api = (struct crypto_driver_api *) dev->api;
114
115 tmp = api->query_hw_caps(dev);
116
117 __ASSERT((tmp & (CAP_OPAQUE_KEY_HNDL | CAP_RAW_KEY)) != 0,
118 "Driver should support at least one key type: RAW/Opaque");
119
120 __ASSERT((tmp & (CAP_INPLACE_OPS | CAP_SEPARATE_IO_BUFS)) != 0,
121 "Driver should support at least one IO buf type: Inplace/separate");
122
123 __ASSERT((tmp & (CAP_SYNC_OPS | CAP_ASYNC_OPS)) != 0,
124 "Driver should support at least one op-type: sync/async");
125 return tmp;
126
127}
128
132
139
159static inline int cipher_begin_session(const struct device *dev,
160 struct cipher_ctx *ctx,
161 enum cipher_algo algo,
162 enum cipher_mode mode,
163 enum cipher_op optype)
164{
165 struct crypto_driver_api *api;
167
168 api = (struct crypto_driver_api *) dev->api;
169 ctx->device = dev;
170 ctx->ops.cipher_mode = mode;
171
173 __ASSERT(flags != 0U, "Keytype missing: RAW Key or OPAQUE handle");
174 __ASSERT(flags != (CAP_OPAQUE_KEY_HNDL | CAP_RAW_KEY),
175 "conflicting options for keytype");
176
178 __ASSERT(flags != 0U, "IO buffer type missing");
180 "conflicting options for IO buffer type");
181
182 flags = (ctx->flags & (CAP_SYNC_OPS | CAP_ASYNC_OPS));
183 __ASSERT(flags != 0U, "sync/async type missing");
184 __ASSERT(flags != (CAP_SYNC_OPS | CAP_ASYNC_OPS),
185 "conflicting options for sync/async");
186
187 return api->cipher_begin_session(dev, ctx, algo, mode, optype);
188}
189
201static inline int cipher_free_session(const struct device *dev,
202 struct cipher_ctx *ctx)
203{
204 struct crypto_driver_api *api;
205
206 api = (struct crypto_driver_api *) dev->api;
207
208 return api->cipher_free_session(dev, ctx);
209}
210
225static inline int cipher_callback_set(const struct device *dev,
227{
228 struct crypto_driver_api *api;
229
230 api = (struct crypto_driver_api *) dev->api;
231
232 if (api->cipher_async_callback_set) {
233 return api->cipher_async_callback_set(dev, cb);
234 }
235
236 return -ENOTSUP;
237
238}
239
249static inline int cipher_block_op(struct cipher_ctx *ctx,
250 struct cipher_pkt *pkt)
251{
252 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_ECB, "ECB mode "
253 "session invoking a different mode handler");
254
255 pkt->ctx = ctx;
256 return ctx->ops.block_crypt_hndlr(ctx, pkt);
257}
258
270static inline int cipher_cbc_op(struct cipher_ctx *ctx,
271 struct cipher_pkt *pkt, uint8_t *iv)
272{
273 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CBC, "CBC mode "
274 "session invoking a different mode handler");
275
276 pkt->ctx = ctx;
277 return ctx->ops.cbc_crypt_hndlr(ctx, pkt, iv);
278}
279
297static inline int cipher_ctr_op(struct cipher_ctx *ctx,
298 struct cipher_pkt *pkt, uint8_t *iv)
299{
300 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CTR, "CTR mode "
301 "session invoking a different mode handler");
302
303 pkt->ctx = ctx;
304 return ctx->ops.ctr_crypt_hndlr(ctx, pkt, iv);
305}
306
319static inline int cipher_ccm_op(struct cipher_ctx *ctx,
320 struct cipher_aead_pkt *pkt, uint8_t *nonce)
321{
322 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CCM, "CCM mode "
323 "session invoking a different mode handler");
324
325 pkt->pkt->ctx = ctx;
326 return ctx->ops.ccm_crypt_hndlr(ctx, pkt, nonce);
327}
328
341static inline int cipher_gcm_op(struct cipher_ctx *ctx,
342 struct cipher_aead_pkt *pkt, uint8_t *nonce)
343{
344 __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_GCM, "GCM mode "
345 "session invoking a different mode handler");
346
347 pkt->pkt->ctx = ctx;
348 return ctx->ops.gcm_crypt_hndlr(ctx, pkt, nonce);
349}
350
351
355
362
363
381static inline int hash_begin_session(const struct device *dev,
382 struct hash_ctx *ctx,
383 enum hash_algo algo)
384{
386 struct crypto_driver_api *api;
387
388 api = (struct crypto_driver_api *) dev->api;
389 ctx->device = dev;
390
392 __ASSERT(flags != 0U, "IO buffer type missing");
394 "conflicting options for IO buffer type");
395
396 flags = (ctx->flags & (CAP_SYNC_OPS | CAP_ASYNC_OPS));
397 __ASSERT(flags != 0U, "sync/async type missing");
398 __ASSERT(flags != (CAP_SYNC_OPS | CAP_ASYNC_OPS),
399 "conflicting options for sync/async");
400
401
402 return api->hash_begin_session(dev, ctx, algo);
403}
404
416static inline int hash_free_session(const struct device *dev,
417 struct hash_ctx *ctx)
418{
419 struct crypto_driver_api *api;
420
421 api = (struct crypto_driver_api *) dev->api;
422
423 return api->hash_free_session(dev, ctx);
424}
425
440static inline int hash_callback_set(const struct device *dev,
442{
443 struct crypto_driver_api *api;
444
445 api = (struct crypto_driver_api *) dev->api;
446
447 if (api->hash_async_callback_set) {
448 return api->hash_async_callback_set(dev, cb);
449 }
450
451 return -ENOTSUP;
452
453}
454
463static inline int hash_compute(struct hash_ctx *ctx, struct hash_pkt *pkt)
464{
465 pkt->ctx = ctx;
466
467 return ctx->hash_hndlr(ctx, pkt, true);
468}
469
482static inline int hash_update(struct hash_ctx *ctx, struct hash_pkt *pkt)
483{
484 pkt->ctx = ctx;
485
486 return ctx->hash_hndlr(ctx, pkt, false);
487}
488
492
493#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:249
void(* cipher_completion_cb)(struct cipher_pkt *completed, int status)
Definition cipher.h:242
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:159
cipher_op
Cipher Operation.
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:270
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:341
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:319
cipher_algo
Cipher Algorithm.
Definition cipher.h:29
static int cipher_free_session(const struct device *dev, struct cipher_ctx *ctx)
Cleanup a crypto session.
Definition crypto.h:201
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:225
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:297
@ CRYPTO_CIPHER_MODE_GCM
Definition cipher.h:49
@ CRYPTO_CIPHER_MODE_ECB
Definition cipher.h:45
@ CRYPTO_CIPHER_MODE_CCM
Definition cipher.h:48
@ CRYPTO_CIPHER_MODE_CTR
Definition cipher.h:47
@ CRYPTO_CIPHER_MODE_CBC
Definition cipher.h:46
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:463
static int hash_begin_session(const struct device *dev, struct hash_ctx *ctx, enum hash_algo algo)
Setup a hash session.
Definition crypto.h:381
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:440
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:416
static int hash_update(struct hash_ctx *ctx, struct hash_pkt *pkt)
Perform a cryptographic multipart hash operation.
Definition crypto.h:482
#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:53
#define CAP_INPLACE_OPS
Whether the output is placed in separate buffer or not.
Definition crypto.h:46
#define CAP_ASYNC_OPS
Definition crypto.h:54
#define CAP_OPAQUE_KEY_HNDL
Definition crypto.h:39
#define CAP_SEPARATE_IO_BUFS
Definition crypto.h:47
#define CAP_RAW_KEY
Definition crypto.h:40
static int crypto_query_hwcaps(const struct device *dev)
Query the crypto hardware capabilities.
Definition crypto.h:108
#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:217
struct cipher_pkt * pkt
Definition cipher.h:219
Structure encoding session parameters.
Definition cipher.h:110
const struct device * device
The device driver instance this crypto context relates to.
Definition cipher.h:131
uint16_t flags
How certain fields are to be interpreted for this session.
Definition cipher.h:169
struct cipher_ops ops
Place for driver to return function pointers to be invoked per cipher operation.
Definition cipher.h:116
block_op_t block_crypt_hndlr
Definition cipher.h:79
gcm_op_t gcm_crypt_hndlr
Definition cipher.h:83
enum cipher_mode cipher_mode
Definition cipher.h:76
cbc_op_t cbc_crypt_hndlr
Definition cipher.h:80
ctr_op_t ctr_crypt_hndlr
Definition cipher.h:81
ccm_op_t ccm_crypt_hndlr
Definition cipher.h:82
Structure encoding IO parameters of one cryptographic operation like encrypt/decrypt.
Definition cipher.h:180
struct cipher_ctx * ctx
Context this packet relates to.
Definition cipher.h:208
Crypto driver API definition.
Definition crypto.h:65
int(* query_hw_caps)(const struct device *dev)
Definition crypto.h:66
int(* cipher_async_callback_set)(const struct device *dev, cipher_completion_cb cb)
Definition crypto.h:77
int(* hash_begin_session)(const struct device *dev, struct hash_ctx *ctx, enum hash_algo algo)
Definition crypto.h:81
int(* cipher_free_session)(const struct device *dev, struct cipher_ctx *ctx)
Definition crypto.h:74
int(* hash_async_callback_set)(const struct device *dev, hash_completion_cb cb)
Definition crypto.h:86
int(* hash_free_session)(const struct device *dev, struct hash_ctx *ctx)
Definition crypto.h:84
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:69
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.