xref: /optee_os/core/include/crypto/internal_aes-gcm.h (revision 5a913ee74d3c71af2a2860ce8a4e7aeab2916f9b)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2017, Linaro Limited
4  *
5  */
6 
7 #ifndef __CRYPTO_INTERNAL_AES_GCM_H
8 #define __CRYPTO_INTERNAL_AES_GCM_H
9 
10 #include <tee_api_types.h>
11 #include <utee_defines.h>
12 
13 struct internal_aes_gcm_key {
14 	/* AES (CTR) encryption key and number of rounds */
15 	uint64_t data[30];
16 	unsigned int rounds;
17 };
18 
19 struct internal_aes_gcm_state {
20 	uint64_t ctr[2];
21 
22 #ifdef CFG_AES_GCM_TABLE_BASED
23 	uint64_t HL[16];
24 	uint64_t HH[16];
25 #else
26 	uint8_t hash_subkey[TEE_AES_BLOCK_SIZE];
27 #endif
28 	uint8_t hash_state[TEE_AES_BLOCK_SIZE];
29 
30 	uint8_t buf_tag[TEE_AES_BLOCK_SIZE];
31 	uint8_t buf_hash[TEE_AES_BLOCK_SIZE];
32 	uint8_t buf_cryp[TEE_AES_BLOCK_SIZE];
33 
34 	unsigned int tag_len;
35 	unsigned int aad_bytes;
36 	unsigned int payload_bytes;
37 	unsigned int buf_pos;
38 };
39 
40 struct internal_aes_gcm_ctx {
41 	struct internal_aes_gcm_state state;
42 	struct internal_aes_gcm_key key;
43 };
44 
45 TEE_Result internal_aes_gcm_init(struct internal_aes_gcm_ctx *ctx,
46 				 TEE_OperationMode mode, const void *key,
47 				 size_t key_len, const void *nonce,
48 				 size_t nonce_len, size_t tag_len);
49 TEE_Result internal_aes_gcm_update_aad(struct internal_aes_gcm_ctx *ctx,
50 				       const void *data, size_t len);
51 TEE_Result internal_aes_gcm_update_payload(struct internal_aes_gcm_ctx *ctx,
52 					   TEE_OperationMode mode,
53 					   const void *src, size_t len,
54 					   void *dst);
55 TEE_Result internal_aes_gcm_enc_final(struct internal_aes_gcm_ctx *ctx,
56 				      const void *src, size_t len, void *dst,
57 				      void *tag, size_t *tag_len);
58 TEE_Result internal_aes_gcm_dec_final(struct internal_aes_gcm_ctx *ctx,
59 				      const void *src, size_t len, void *dst,
60 				      const void *tag, size_t tag_len);
61 
62 void internal_aes_gcm_inc_ctr(struct internal_aes_gcm_state *state);
63 
64 TEE_Result internal_aes_gcm_enc(const struct internal_aes_gcm_key *enc_key,
65 				const void *nonce, size_t nonce_len,
66 				const void *aad, size_t aad_len,
67 				const void *src, size_t len, void *dst,
68 				void *tag, size_t *tag_len);
69 
70 TEE_Result internal_aes_gcm_dec(const struct internal_aes_gcm_key *enc_key,
71 				const void *nonce, size_t nonce_len,
72 				const void *aad, size_t aad_len,
73 				const void *src, size_t len, void *dst,
74 				const void *tag, size_t tag_len);
75 
76 TEE_Result
77 internal_aes_gcm_expand_enc_key(const void *key, size_t key_len,
78 				struct internal_aes_gcm_key *enc_key);
79 
80 /*
81  * Internal weak functions that can be overridden with hardware specific
82  * implementations.
83  */
84 void internal_aes_gcm_set_key(struct internal_aes_gcm_state *state,
85 			      const struct internal_aes_gcm_key *enc_key);
86 
87 void internal_aes_gcm_ghash_update(struct internal_aes_gcm_state *state,
88 				   const void *head, const void *data,
89 				   size_t num_blocks);
90 
91 void internal_aes_gcm_update_payload_block_aligned(
92 				struct internal_aes_gcm_state *state,
93 				const struct internal_aes_gcm_key *enc_key,
94 				TEE_OperationMode mode, const void *src,
95 				size_t num_blocks, void *dst);
96 
97 
98 
99 void internal_aes_gcm_encrypt_block(const struct internal_aes_gcm_key *enc_key,
100 				    const void *src, void *dst);
101 #endif /*__CRYPTO_INTERNAL_AES_GCM_H*/
102