xref: /optee_os/core/include/crypto/internal_aes-gcm.h (revision 1fca7e269b134c6a02d662cdbd6ee67fa3b9801c)
1 /*
2  * Copyright (c) 2017, Linaro Limited
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-2-Clause
6  */
7 
8 #ifndef __CRYPTO_INTERNAL_AES_GCM_H
9 #define __CRYPTO_INTERNAL_AES_GCM_H
10 
11 #include <tee_api_types.h>
12 #include <tee_api_types.h>
13 #include <utee_defines.h>
14 #include <tomcrypt.h>
15 
16 struct internal_aes_gcm_ctx {
17 	uint64_t ctr[2];
18 
19 	uint8_t hash_subkey[TEE_AES_BLOCK_SIZE];
20 	uint8_t hash_state[TEE_AES_BLOCK_SIZE];
21 
22 	uint8_t buf_tag[TEE_AES_BLOCK_SIZE];
23 	uint8_t buf_hash[TEE_AES_BLOCK_SIZE];
24 	uint8_t buf_cryp[TEE_AES_BLOCK_SIZE];
25 
26 	symmetric_key skey;
27 
28 	unsigned int tag_len;
29 	unsigned int aad_bytes;
30 	unsigned int payload_bytes;
31 	unsigned int buf_pos;
32 };
33 
34 TEE_Result internal_aes_gcm_init(struct internal_aes_gcm_ctx *ctx,
35 				 TEE_OperationMode mode, const void *key,
36 				 size_t key_len, const void *nonce,
37 				 size_t nonce_len, size_t tag_len);
38 TEE_Result internal_aes_gcm_update_aad(struct internal_aes_gcm_ctx *ctx,
39 				       const void *data, size_t len);
40 TEE_Result internal_aes_gcm_update_payload(struct internal_aes_gcm_ctx *ctx,
41 					   TEE_OperationMode mode,
42 					   const void *src, size_t len,
43 					   void *dst);
44 TEE_Result internal_aes_gcm_enc_final(struct internal_aes_gcm_ctx *ctx,
45 				      const void *src, size_t len, void *dst,
46 				      void *tag, size_t *tag_len);
47 TEE_Result internal_aes_gcm_dec_final(struct internal_aes_gcm_ctx *ctx,
48 				      const void *src, size_t len, void *dst,
49 				      const void *tag, size_t tag_len);
50 
51 void internal_aes_gcm_inc_ctr(struct internal_aes_gcm_ctx *ctx);
52 
53 /*
54  * Internal weak functions that can be overridden with hardware specific
55  * implementations.
56  */
57 void internal_aes_gcm_encrypt_block(struct internal_aes_gcm_ctx *ctx,
58 				    const void *src, void *dst);
59 
60 TEE_Result internal_aes_gcm_set_key(struct internal_aes_gcm_ctx *ctx,
61 				    const void *key, size_t key_len);
62 
63 void internal_aes_gcm_ghash_update(struct internal_aes_gcm_ctx *ctx,
64 				   const void *head, const void *data,
65 				   size_t num_blocks);
66 
67 void
68 internal_aes_gcm_update_payload_block_aligned(struct internal_aes_gcm_ctx *ctx,
69 					      TEE_OperationMode mode,
70 					      const void *src,
71 					      size_t num_blocks, void *dst);
72 #endif /*__CRYPTO_INTERNAL_AES_GCM_H*/
73