xref: /optee_os/core/include/crypto/internal_aes-gcm.h (revision fb7ef469dfeb735e60383ad0e7410fe62dd97eb1)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2017, Linaro Limited
4  * All rights reserved.
5  *
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 internal_aes_gcm_enc(const struct internal_aes_gcm_key *enc_key,
66 				const void *nonce, size_t nonce_len,
67 				const void *aad, size_t aad_len,
68 				const void *src, size_t len, void *dst,
69 				void *tag, size_t *tag_len);
70 
71 TEE_Result internal_aes_gcm_dec(const struct internal_aes_gcm_key *enc_key,
72 				const void *nonce, size_t nonce_len,
73 				const void *aad, size_t aad_len,
74 				const void *src, size_t len, void *dst,
75 				const void *tag, size_t tag_len);
76 
77 TEE_Result
78 internal_aes_gcm_expand_enc_key(const void *key, size_t key_len,
79 				struct internal_aes_gcm_key *enc_key);
80 
81 /*
82  * Internal weak functions that can be overridden with hardware specific
83  * implementations.
84  */
85 void internal_aes_gcm_set_key(struct internal_aes_gcm_state *state,
86 			      const struct internal_aes_gcm_key *enc_key);
87 
88 void internal_aes_gcm_ghash_update(struct internal_aes_gcm_state *state,
89 				   const void *head, const void *data,
90 				   size_t num_blocks);
91 
92 void internal_aes_gcm_update_payload_block_aligned(
93 				struct internal_aes_gcm_state *state,
94 				const struct internal_aes_gcm_key *enc_key,
95 				TEE_OperationMode mode, const void *src,
96 				size_t num_blocks, void *dst);
97 
98 
99 
100 void internal_aes_gcm_encrypt_block(const struct internal_aes_gcm_key *enc_key,
101 				    const void *src, void *dst);
102 #endif /*__CRYPTO_INTERNAL_AES_GCM_H*/
103