xref: /optee_os/core/include/crypto/internal_aes-gcm.h (revision b8c186b57350152c3e67500d540920ca40309d5b)
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 #ifdef CFG_AES_GCM_TABLE_BASED
20 	uint64_t HL[16];
21 	uint64_t HH[16];
22 #else
23 	uint8_t hash_subkey[TEE_AES_BLOCK_SIZE];
24 #endif
25 	uint8_t hash_state[TEE_AES_BLOCK_SIZE];
26 
27 	uint8_t buf_tag[TEE_AES_BLOCK_SIZE];
28 	uint8_t buf_hash[TEE_AES_BLOCK_SIZE];
29 	uint8_t buf_cryp[TEE_AES_BLOCK_SIZE];
30 
31 	symmetric_key skey;
32 
33 	unsigned int tag_len;
34 	unsigned int aad_bytes;
35 	unsigned int payload_bytes;
36 	unsigned int buf_pos;
37 };
38 
39 TEE_Result internal_aes_gcm_init(struct internal_aes_gcm_ctx *ctx,
40 				 TEE_OperationMode mode, const void *key,
41 				 size_t key_len, const void *nonce,
42 				 size_t nonce_len, size_t tag_len);
43 TEE_Result internal_aes_gcm_update_aad(struct internal_aes_gcm_ctx *ctx,
44 				       const void *data, size_t len);
45 TEE_Result internal_aes_gcm_update_payload(struct internal_aes_gcm_ctx *ctx,
46 					   TEE_OperationMode mode,
47 					   const void *src, size_t len,
48 					   void *dst);
49 TEE_Result internal_aes_gcm_enc_final(struct internal_aes_gcm_ctx *ctx,
50 				      const void *src, size_t len, void *dst,
51 				      void *tag, size_t *tag_len);
52 TEE_Result internal_aes_gcm_dec_final(struct internal_aes_gcm_ctx *ctx,
53 				      const void *src, size_t len, void *dst,
54 				      const void *tag, size_t tag_len);
55 
56 void internal_aes_gcm_inc_ctr(struct internal_aes_gcm_ctx *ctx);
57 
58 /*
59  * Internal weak functions that can be overridden with hardware specific
60  * implementations.
61  */
62 void internal_aes_gcm_encrypt_block(struct internal_aes_gcm_ctx *ctx,
63 				    const void *src, void *dst);
64 
65 TEE_Result internal_aes_gcm_set_key(struct internal_aes_gcm_ctx *ctx,
66 				    const void *key, size_t key_len);
67 
68 void internal_aes_gcm_ghash_update(struct internal_aes_gcm_ctx *ctx,
69 				   const void *head, const void *data,
70 				   size_t num_blocks);
71 
72 void
73 internal_aes_gcm_update_payload_block_aligned(struct internal_aes_gcm_ctx *ctx,
74 					      TEE_OperationMode mode,
75 					      const void *src,
76 					      size_t num_blocks, void *dst);
77 #endif /*__CRYPTO_INTERNAL_AES_GCM_H*/
78