1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2017-2020, Linaro Limited
4  *
5  */
6 
7 #ifndef __CRYPTO_INTERNAL_AES_GCM_H
8 #define __CRYPTO_INTERNAL_AES_GCM_H
9 
10 #include <assert.h>
11 #include <tee_api_types.h>
12 #include <utee_defines.h>
13 #include <util.h>
14 
15 #ifdef CFG_CRYPTO_WITH_CE
16 #include <crypto/ghash-ce-core.h>
17 #else
18 struct internal_ghash_key {
19 #ifdef CFG_AES_GCM_TABLE_BASED
20 	uint64_t HL[16];
21 	uint64_t HH[16];
22 #else
23 	uint64_t hash_subkey[2];
24 #endif
25 };
26 #endif
27 
28 struct internal_aes_gcm_key {
29 	/* AES (CTR) encryption key and number of rounds */
30 	uint64_t data[30];
31 	unsigned int rounds;
32 };
33 
34 struct internal_aes_gcm_state {
35 	uint64_t ctr[2];
36 
37 	struct internal_ghash_key ghash_key;
38 	uint8_t hash_state[TEE_AES_BLOCK_SIZE];
39 
40 	uint8_t buf_tag[TEE_AES_BLOCK_SIZE];
41 	uint8_t buf_hash[TEE_AES_BLOCK_SIZE];
42 	uint8_t buf_cryp[TEE_AES_BLOCK_SIZE];
43 
44 	unsigned int tag_len;
45 	unsigned int aad_bytes;
46 	unsigned int payload_bytes;
47 	unsigned int buf_pos;
48 };
49 
50 struct internal_aes_gcm_ctx {
51 	struct internal_aes_gcm_state state;
52 	struct internal_aes_gcm_key key;
53 };
54 
55 TEE_Result internal_aes_gcm_init(struct internal_aes_gcm_ctx *ctx,
56 				 TEE_OperationMode mode, const void *key,
57 				 size_t key_len, const void *nonce,
58 				 size_t nonce_len, size_t tag_len);
59 TEE_Result internal_aes_gcm_update_aad(struct internal_aes_gcm_ctx *ctx,
60 				       const void *data, size_t len);
61 TEE_Result internal_aes_gcm_update_payload(struct internal_aes_gcm_ctx *ctx,
62 					   TEE_OperationMode mode,
63 					   const void *src, size_t len,
64 					   void *dst);
65 TEE_Result internal_aes_gcm_enc_final(struct internal_aes_gcm_ctx *ctx,
66 				      const void *src, size_t len, void *dst,
67 				      void *tag, size_t *tag_len);
68 TEE_Result internal_aes_gcm_dec_final(struct internal_aes_gcm_ctx *ctx,
69 				      const void *src, size_t len, void *dst,
70 				      const void *tag, size_t tag_len);
71 
72 void internal_aes_gcm_inc_ctr(struct internal_aes_gcm_state *state);
73 void internal_aes_gcm_dec_ctr(struct internal_aes_gcm_state *state);
74 
75 TEE_Result internal_aes_gcm_enc(const struct internal_aes_gcm_key *enc_key,
76 				const void *nonce, size_t nonce_len,
77 				const void *aad, size_t aad_len,
78 				const void *src, size_t len, void *dst,
79 				void *tag, size_t *tag_len);
80 
81 TEE_Result internal_aes_gcm_dec(const struct internal_aes_gcm_key *enc_key,
82 				const void *nonce, size_t nonce_len,
83 				const void *aad, size_t aad_len,
84 				const void *src, size_t len, void *dst,
85 				const void *tag, size_t tag_len);
86 
87 void internal_aes_gcm_gfmul(const uint64_t X[2], const uint64_t Y[2],
88 			    uint64_t product[2]);
89 
internal_aes_gcm_xor_block(void * dst,const void * src)90 static inline void internal_aes_gcm_xor_block(void *dst, const void *src)
91 {
92 	uint64_t *d = dst;
93 	const uint64_t *s = src;
94 
95 	assert(IS_ALIGNED_WITH_TYPE(dst, uint64_t));
96 	assert(IS_ALIGNED_WITH_TYPE(src, uint64_t));
97 
98 	d[0] ^= s[0];
99 	d[1] ^= s[1];
100 }
101 
internal_aes_gcm_ptr_is_block_aligned(const void * p)102 static inline bool internal_aes_gcm_ptr_is_block_aligned(const void *p)
103 {
104 	return !((vaddr_t)p & (TEE_AES_BLOCK_SIZE - 1));
105 }
106 
107 #ifdef CFG_AES_GCM_TABLE_BASED
108 void internal_aes_gcm_ghash_gen_tbl(struct internal_ghash_key *ghash_key,
109 				    const struct internal_aes_gcm_key *enc_key);
110 void internal_aes_gcm_ghash_mult_tbl(struct internal_ghash_key *ghash_key,
111 				     const unsigned char x[16],
112 				     unsigned char output[16]);
113 #endif
114 
115 /*
116  * Must be implemented in core/arch/arm/crypto/ if CFG_CRYPTO_WITH_CE=y
117  */
118 void internal_aes_gcm_set_key(struct internal_aes_gcm_state *state,
119 			      const struct internal_aes_gcm_key *enc_key);
120 
121 void internal_aes_gcm_ghash_update(struct internal_aes_gcm_state *state,
122 				   const void *head, const void *data,
123 				   size_t num_blocks);
124 /*
125  * Internal weak function that can be overridden with hardware specific
126  * implementation.
127  */
128 void
129 internal_aes_gcm_update_payload_blocks(struct internal_aes_gcm_state *state,
130 				       const struct internal_aes_gcm_key *ek,
131 				       TEE_OperationMode mode, const void *src,
132 				       size_t num_blocks, void *dst);
133 
134 #endif /*__CRYPTO_INTERNAL_AES_GCM_H*/
135