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