1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2021, STMicroelectronics - All Rights Reserved
4  *
5  * Authenticated Encryption interface calling the crypto driver
6  */
7 #ifndef __DRVCRYPT_AUTHENC_H__
8 #define __DRVCRYPT_AUTHENC_H__
9 
10 #include <crypto/crypto_impl.h>
11 #include <tee_api_types.h>
12 
13 /*
14  * Authenticated Encryption operation context
15  */
16 struct crypto_authenc {
17 	struct crypto_authenc_ctx authenc_ctx;	/* Crypto authenc API context */
18 	void *ctx;				/* Authenc context */
19 	struct drvcrypt_authenc *op;		/* Reference to the operation */
20 };
21 
22 /*
23  * Authenticated Encryption algorithm initialization data
24  */
25 struct drvcrypt_authenc_init {
26 	void *ctx;		     /* Software context */
27 	bool encrypt;		     /* Encrypt or decrypt direction */
28 	struct drvcrypt_buf key;     /* First key */
29 	struct drvcrypt_buf nonce;   /* Nonce */
30 	size_t tag_len;		     /* Tag length  */
31 	size_t aad_len;		     /* Additional Authenticated Data length */
32 	size_t payload_len;	     /* Payload length */
33 };
34 
35 /*
36  * Authenticated Encryption algorithm update_aad data
37  */
38 struct drvcrypt_authenc_update_aad {
39 	void *ctx;		 /* Software context */
40 	bool encrypt;		 /* Encrypt or decrypt direction */
41 	struct drvcrypt_buf aad; /* Additional Authenticated Data buffer */
42 };
43 
44 /*
45  * Authenticated Encryption algorithm update_aad data
46  */
47 struct drvcrypt_authenc_update_payload {
48 	void *ctx;		 /* Software context */
49 	bool encrypt;		 /* Encrypt or decrypt direction */
50 	struct drvcrypt_buf src; /* Buffer source (message or cipher) */
51 	struct drvcrypt_buf dst; /* Buffer destination (cipher or message) */
52 };
53 
54 /*
55  * Authenticated Encryption algorithm final data
56  */
57 struct drvcrypt_authenc_final {
58 	void *ctx;		 /* Software context */
59 	bool encrypt;		 /* Encrypt or decrypt direction */
60 	struct drvcrypt_buf src; /* Buffer source (message or cipher) */
61 	struct drvcrypt_buf dst; /* Buffer destination (cipher or message) */
62 	struct drvcrypt_buf tag; /* Tag buffer */
63 };
64 
65 /*
66  * Crypto library authenc driver operations
67  */
68 struct drvcrypt_authenc {
69 	/* Allocate context */
70 	TEE_Result (*alloc_ctx)(void **ctx, uint32_t algo);
71 	/* Free context */
72 	void (*free_ctx)(void *ctx);
73 	/* Initialize the authenc operation */
74 	TEE_Result (*init)(struct drvcrypt_authenc_init *dinit);
75 	/* Update the authenc operation with associated data */
76 	TEE_Result (*update_aad)(struct drvcrypt_authenc_update_aad *dupdate);
77 	/* Update the authenc operation with payload data */
78 	TEE_Result (*update_payload)(struct drvcrypt_authenc_update_payload *d);
79 	/* Update (or not) with payload data and get tag for encrypt op. */
80 	TEE_Result (*enc_final)(struct drvcrypt_authenc_final *dfinal);
81 	/* Update (or not) with payload data and verify tag for decrypt op. */
82 	TEE_Result (*dec_final)(struct drvcrypt_authenc_final *dfinal);
83 	/* Finalize the authenc operation */
84 	void (*final)(void *ctx);
85 	/* Copy authenc context */
86 	void (*copy_state)(void *dst_ctx, void *src_ctx);
87 };
88 
89 /*
90  * Register an authenc processing driver in the crypto API
91  *
92  * @ops - Driver operations
93  */
drvcrypt_register_authenc(struct drvcrypt_authenc * ops)94 static inline TEE_Result drvcrypt_register_authenc(struct drvcrypt_authenc *ops)
95 {
96 	return drvcrypt_register(CRYPTO_AUTHENC, (void *)ops);
97 }
98 
99 #endif /* __DRVCRYPT_AUTHENC_H__ */
100