xref: /optee_os/core/include/crypto/internal_aes-gcm.h (revision 61b4cd9c4374b872e59d84b3d6b564264716c485)
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 <utee_defines.h>
13 
14 struct internal_aes_gcm_ctx {
15 	uint64_t ctr[2];
16 
17 #ifdef CFG_AES_GCM_TABLE_BASED
18 	uint64_t HL[16];
19 	uint64_t HH[16];
20 #else
21 	uint8_t hash_subkey[TEE_AES_BLOCK_SIZE];
22 #endif
23 	uint8_t hash_state[TEE_AES_BLOCK_SIZE];
24 
25 	uint8_t buf_tag[TEE_AES_BLOCK_SIZE];
26 	uint8_t buf_hash[TEE_AES_BLOCK_SIZE];
27 	uint8_t buf_cryp[TEE_AES_BLOCK_SIZE];
28 
29 	/* AES (CTR) encryption key and number of rounds */
30 	uint64_t enc_key[30];
31 	unsigned int rounds;
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 TEE_Result internal_aes_gcm_set_key(struct internal_aes_gcm_ctx *ctx,
63 				    const void *key, size_t key_len);
64 
65 void internal_aes_gcm_ghash_update(struct internal_aes_gcm_ctx *ctx,
66 				   const void *head, const void *data,
67 				   size_t num_blocks);
68 
69 void
70 internal_aes_gcm_update_payload_block_aligned(struct internal_aes_gcm_ctx *ctx,
71 					      TEE_OperationMode mode,
72 					      const void *src,
73 					      size_t num_blocks, void *dst);
74 
75 
76 TEE_Result internal_aes_gcm_expand_enc_key(const void *key, size_t key_len,
77 					   uint64_t *enc_key,
78 					   unsigned int *rounds);
79 
80 void internal_aes_gcm_encrypt_block(struct internal_aes_gcm_ctx *ctx,
81 				    const void *src, void *dst);
82 
83 #endif /*__CRYPTO_INTERNAL_AES_GCM_H*/
84