123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- __subsystem struct crypto_driver_api {
- int (*query_hw_caps)(const struct device *dev);
-
- int (*begin_session)(const struct device *dev, struct cipher_ctx *ctx,
- enum cipher_algo algo, enum cipher_mode mode,
- enum cipher_op op_type);
-
- int (*free_session)(const struct device *dev, struct cipher_ctx *ctx);
-
- int (*crypto_async_callback_set)(const struct device *dev,
- crypto_completion_cb cb);
- };
- static inline int cipher_query_hwcaps(const struct device *dev)
- {
- struct crypto_driver_api *api;
- int tmp;
- api = (struct crypto_driver_api *) dev->api;
- tmp = api->query_hw_caps(dev);
- __ASSERT((tmp & (CAP_OPAQUE_KEY_HNDL | CAP_RAW_KEY)) != 0,
- "Driver should support at least one key type: RAW/Opaque");
- __ASSERT((tmp & (CAP_INPLACE_OPS | CAP_SEPARATE_IO_BUFS)) != 0,
- "Driver should support at least one IO buf type: Inplace/separate");
- __ASSERT((tmp & (CAP_SYNC_OPS | CAP_ASYNC_OPS)) != 0,
- "Driver should support at least one op-type: sync/async");
- return tmp;
- }
- static inline int cipher_begin_session(const struct device *dev,
- struct cipher_ctx *ctx,
- enum cipher_algo algo,
- enum cipher_mode mode,
- enum cipher_op optype)
- {
- struct crypto_driver_api *api;
- uint32_t flags;
- api = (struct crypto_driver_api *) dev->api;
- ctx->device = dev;
- ctx->ops.cipher_mode = mode;
- flags = (ctx->flags & (CAP_OPAQUE_KEY_HNDL | CAP_RAW_KEY));
- __ASSERT(flags != 0U, "Keytype missing: RAW Key or OPAQUE handle");
- __ASSERT(flags != (CAP_OPAQUE_KEY_HNDL | CAP_RAW_KEY),
- "conflicting options for keytype");
- flags = (ctx->flags & (CAP_INPLACE_OPS | CAP_SEPARATE_IO_BUFS));
- __ASSERT(flags != 0U, "IO buffer type missing");
- __ASSERT(flags != (CAP_INPLACE_OPS | CAP_SEPARATE_IO_BUFS),
- "conflicting options for IO buffer type");
- flags = (ctx->flags & (CAP_SYNC_OPS | CAP_ASYNC_OPS));
- __ASSERT(flags != 0U, "sync/async type missing");
- __ASSERT(flags != (CAP_SYNC_OPS | CAP_ASYNC_OPS),
- "conflicting options for sync/async");
- return api->begin_session(dev, ctx, algo, mode, optype);
- }
- static inline int cipher_free_session(const struct device *dev,
- struct cipher_ctx *ctx)
- {
- struct crypto_driver_api *api;
- api = (struct crypto_driver_api *) dev->api;
- return api->free_session(dev, ctx);
- }
- static inline int cipher_callback_set(const struct device *dev,
- crypto_completion_cb cb)
- {
- struct crypto_driver_api *api;
- api = (struct crypto_driver_api *) dev->api;
- if (api->crypto_async_callback_set) {
- return api->crypto_async_callback_set(dev, cb);
- }
- return -ENOTSUP;
- }
- static inline int cipher_block_op(struct cipher_ctx *ctx,
- struct cipher_pkt *pkt)
- {
- __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_ECB, "ECB mode "
- "session invoking a different mode handler");
- pkt->ctx = ctx;
- return ctx->ops.block_crypt_hndlr(ctx, pkt);
- }
- static inline int cipher_cbc_op(struct cipher_ctx *ctx,
- struct cipher_pkt *pkt, uint8_t *iv)
- {
- __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CBC, "CBC mode "
- "session invoking a different mode handler");
- pkt->ctx = ctx;
- return ctx->ops.cbc_crypt_hndlr(ctx, pkt, iv);
- }
- static inline int cipher_ctr_op(struct cipher_ctx *ctx,
- struct cipher_pkt *pkt, uint8_t *iv)
- {
- __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CTR, "CTR mode "
- "session invoking a different mode handler");
- pkt->ctx = ctx;
- return ctx->ops.ctr_crypt_hndlr(ctx, pkt, iv);
- }
- static inline int cipher_ccm_op(struct cipher_ctx *ctx,
- struct cipher_aead_pkt *pkt, uint8_t *nonce)
- {
- __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_CCM, "CCM mode "
- "session invoking a different mode handler");
- pkt->pkt->ctx = ctx;
- return ctx->ops.ccm_crypt_hndlr(ctx, pkt, nonce);
- }
- static inline int cipher_gcm_op(struct cipher_ctx *ctx,
- struct cipher_aead_pkt *pkt, uint8_t *nonce)
- {
- __ASSERT(ctx->ops.cipher_mode == CRYPTO_CIPHER_MODE_GCM, "GCM mode "
- "session invoking a different mode handler");
- pkt->pkt->ctx = ctx;
- return ctx->ops.gcm_crypt_hndlr(ctx, pkt, nonce);
- }
|