2020
2121#include "internal.h"
2222
23+ static inline struct crypto_istat_aead * aead_get_stat (struct aead_alg * alg )
24+ {
25+ #ifdef CONFIG_CRYPTO_STATS
26+ return & alg -> stat ;
27+ #else
28+ return NULL ;
29+ #endif
30+ }
31+
2332static int setkey_unaligned (struct crypto_aead * tfm , const u8 * key ,
2433 unsigned int keylen )
2534{
@@ -81,28 +90,62 @@ int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
8190}
8291EXPORT_SYMBOL_GPL (crypto_aead_setauthsize );
8392
93+ static inline int crypto_aead_errstat (struct crypto_istat_aead * istat , int err )
94+ {
95+ if (!IS_ENABLED (CONFIG_CRYPTO_STATS ))
96+ return err ;
97+
98+ if (err && err != - EINPROGRESS && err != - EBUSY )
99+ atomic64_inc (& istat -> err_cnt );
100+
101+ return err ;
102+ }
103+
84104int crypto_aead_encrypt (struct aead_request * req )
85105{
86106 struct crypto_aead * aead = crypto_aead_reqtfm (req );
107+ struct aead_alg * alg = crypto_aead_alg (aead );
108+ struct crypto_istat_aead * istat ;
109+ int ret ;
110+
111+ istat = aead_get_stat (alg );
112+
113+ if (IS_ENABLED (CONFIG_CRYPTO_STATS )) {
114+ atomic64_inc (& istat -> encrypt_cnt );
115+ atomic64_add (req -> cryptlen , & istat -> encrypt_tlen );
116+ }
87117
88118 if (crypto_aead_get_flags (aead ) & CRYPTO_TFM_NEED_KEY )
89- return - ENOKEY ;
119+ ret = - ENOKEY ;
120+ else
121+ ret = alg -> encrypt (req );
90122
91- return crypto_aead_alg ( aead ) -> encrypt ( req );
123+ return crypto_aead_errstat ( istat , ret );
92124}
93125EXPORT_SYMBOL_GPL (crypto_aead_encrypt );
94126
95127int crypto_aead_decrypt (struct aead_request * req )
96128{
97129 struct crypto_aead * aead = crypto_aead_reqtfm (req );
130+ struct aead_alg * alg = crypto_aead_alg (aead );
131+ struct crypto_istat_aead * istat ;
132+ int ret ;
98133
99- if (crypto_aead_get_flags (aead ) & CRYPTO_TFM_NEED_KEY )
100- return - ENOKEY ;
134+ istat = aead_get_stat (alg );
101135
102- if (req -> cryptlen < crypto_aead_authsize (aead ))
103- return - EINVAL ;
136+ if (IS_ENABLED (CONFIG_CRYPTO_STATS )) {
137+ atomic64_inc (& istat -> encrypt_cnt );
138+ atomic64_add (req -> cryptlen , & istat -> encrypt_tlen );
139+ }
140+
141+ if (crypto_aead_get_flags (aead ) & CRYPTO_TFM_NEED_KEY )
142+ ret = - ENOKEY ;
143+ else if (req -> cryptlen < crypto_aead_authsize (aead ))
144+ ret = - EINVAL ;
145+ else
146+ ret = alg -> decrypt (req );
104147
105- return crypto_aead_alg ( aead ) -> decrypt ( req );
148+ return crypto_aead_errstat ( istat , ret );
106149}
107150EXPORT_SYMBOL_GPL (crypto_aead_decrypt );
108151
@@ -172,6 +215,26 @@ static void crypto_aead_free_instance(struct crypto_instance *inst)
172215 aead -> free (aead );
173216}
174217
218+ static int __maybe_unused crypto_aead_report_stat (
219+ struct sk_buff * skb , struct crypto_alg * alg )
220+ {
221+ struct aead_alg * aead = container_of (alg , struct aead_alg , base );
222+ struct crypto_istat_aead * istat = aead_get_stat (aead );
223+ struct crypto_stat_aead raead ;
224+
225+ memset (& raead , 0 , sizeof (raead ));
226+
227+ strscpy (raead .type , "aead" , sizeof (raead .type ));
228+
229+ raead .stat_encrypt_cnt = atomic64_read (& istat -> encrypt_cnt );
230+ raead .stat_encrypt_tlen = atomic64_read (& istat -> encrypt_tlen );
231+ raead .stat_decrypt_cnt = atomic64_read (& istat -> decrypt_cnt );
232+ raead .stat_decrypt_tlen = atomic64_read (& istat -> decrypt_tlen );
233+ raead .stat_err_cnt = atomic64_read (& istat -> err_cnt );
234+
235+ return nla_put (skb , CRYPTOCFGA_STAT_AEAD , sizeof (raead ), & raead );
236+ }
237+
175238static const struct crypto_type crypto_aead_type = {
176239 .extsize = crypto_alg_extsize ,
177240 .init_tfm = crypto_aead_init_tfm ,
@@ -181,6 +244,9 @@ static const struct crypto_type crypto_aead_type = {
181244#endif
182245#if IS_ENABLED (CONFIG_CRYPTO_USER )
183246 .report = crypto_aead_report ,
247+ #endif
248+ #ifdef CONFIG_CRYPTO_STATS
249+ .report_stat = crypto_aead_report_stat ,
184250#endif
185251 .maskclear = ~CRYPTO_ALG_TYPE_MASK ,
186252 .maskset = CRYPTO_ALG_TYPE_MASK ,
@@ -211,6 +277,7 @@ EXPORT_SYMBOL_GPL(crypto_has_aead);
211277
212278static int aead_prepare_alg (struct aead_alg * alg )
213279{
280+ struct crypto_istat_aead * istat = aead_get_stat (alg );
214281 struct crypto_alg * base = & alg -> base ;
215282
216283 if (max3 (alg -> maxauthsize , alg -> ivsize , alg -> chunksize ) >
@@ -224,6 +291,9 @@ static int aead_prepare_alg(struct aead_alg *alg)
224291 base -> cra_flags &= ~CRYPTO_ALG_TYPE_MASK ;
225292 base -> cra_flags |= CRYPTO_ALG_TYPE_AEAD ;
226293
294+ if (IS_ENABLED (CONFIG_CRYPTO_STATS ))
295+ memset (istat , 0 , sizeof (* istat ));
296+
227297 return 0 ;
228298}
229299
0 commit comments