1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (C) Foundries Ltd. 2020 - All Rights Reserved
4 * Author: Jorge Ramirez <jorge@foundries.io>
5 */
6
7 #include <se050_cipher_algorithms.h>
8 #include <crypto/crypto_impl.h>
9 #include <drvcrypt.h>
10 #include <drvcrypt_cipher.h>
11 #include <initcall.h>
12 #include <string.h>
13 #include <utee_defines.h>
14 #include <util.h>
15
do_init(struct drvcrypt_cipher_init * dinit)16 static TEE_Result do_init(struct drvcrypt_cipher_init *dinit)
17 {
18 struct crypto_cipher_ctx *ctx = dinit->ctx;
19 TEE_OperationMode mode = TEE_MODE_DECRYPT;
20
21 if (dinit->encrypt)
22 mode = TEE_MODE_ENCRYPT;
23
24 return ctx->ops->init(dinit->ctx, mode,
25 dinit->key1.data, dinit->key1.length,
26 dinit->key2.data, dinit->key2.length,
27 dinit->iv.data, dinit->iv.length);
28 }
29
do_update(struct drvcrypt_cipher_update * dupdate)30 static TEE_Result do_update(struct drvcrypt_cipher_update *dupdate)
31 {
32 struct crypto_cipher_ctx *ctx = dupdate->ctx;
33
34 return ctx->ops->update(ctx, dupdate->last, dupdate->src.data,
35 dupdate->src.length, dupdate->dst.data);
36 }
37
do_final(void * context)38 static void do_final(void *context)
39 {
40 struct crypto_cipher_ctx *ctx = context;
41
42 ctx->ops->final(ctx);
43 }
44
do_free(void * context)45 static void do_free(void *context)
46 {
47 struct crypto_cipher_ctx *ctx = context;
48
49 ctx->ops->free_ctx(ctx);
50 }
51
do_copy_state(void * out,void * in)52 static void do_copy_state(void *out, void *in)
53 {
54 struct crypto_cipher_ctx *dst_ctx = out;
55 struct crypto_cipher_ctx *src_ctx = in;
56
57 src_ctx->ops->copy_state(dst_ctx, src_ctx);
58 }
59
do_allocate(void ** ctx,uint32_t algo)60 static TEE_Result do_allocate(void **ctx, uint32_t algo)
61 {
62 switch (algo) {
63 case TEE_ALG_AES_CTR:
64 return se050_aes_ctr_allocate(ctx);
65 default:
66 return TEE_ERROR_NOT_IMPLEMENTED;
67 }
68 }
69
70 static struct drvcrypt_cipher driver_cipher = {
71 .alloc_ctx = do_allocate,
72 .free_ctx = do_free,
73 .init = do_init,
74 .update = do_update,
75 .final = do_final,
76 .copy_state = do_copy_state,
77 };
78
se050_cipher_init(void)79 static TEE_Result se050_cipher_init(void)
80 {
81 return drvcrypt_register_cipher(&driver_cipher);
82 }
83
84 driver_init_late(se050_cipher_init);
85