diff --git a/mars.h b/mars.h index e0831172..31e2cb35 100644 --- a/mars.h +++ b/mars.h @@ -339,6 +339,14 @@ static inline void unuse_fake_mm(void) {} ///////////////////////////////////////////////////////////////////////// +/* Crypto stuff + */ + +extern int mars_digest_size; +extern void mars_digest(unsigned char *digest, void *data, int len); + +///////////////////////////////////////////////////////////////////////// + // init extern int init_mars(void); diff --git a/mars_generic.c b/mars_generic.c index 3f66bf5c..50307936 100644 --- a/mars_generic.c +++ b/mars_generic.c @@ -93,6 +93,34 @@ const struct meta mars_timespec_meta[] = { EXPORT_SYMBOL_GPL(mars_timespec_meta); +////////////////////////////////////////////////////////////// + +// crypto stuff + +#include + +struct crypto_hash *mars_tfm = NULL; +int mars_digest_size = 0; +EXPORT_SYMBOL_GPL(mars_digest_size); + +void mars_digest(unsigned char *digest, void *data, int len) +{ + struct hash_desc desc = { + .tfm = mars_tfm, + .flags = 0, + }; + struct scatterlist sg; + + memset(digest, 0, mars_digest_size); + + crypto_hash_init(&desc); + sg_init_table(&sg, 1); + sg_set_buf(&sg, data, len); + crypto_hash_update(&desc, &sg, sg.length); + crypto_hash_final(&desc, digest); +} +EXPORT_SYMBOL_GPL(mars_digest); + ///////////////////////////////////////////////////////////////////// // tracing @@ -250,13 +278,38 @@ int __init init_mars(void) } } #endif + + mars_tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC); + if (!mars_tfm) { + MARS_ERR("cannot alloc crypto hash\n"); + return -ENOMEM; + } + if (IS_ERR(mars_tfm)) { + MARS_ERR("alloc crypto hash failed, status = %d\n", (int)PTR_ERR(mars_tfm)); + return PTR_ERR(mars_tfm); + } +#if 0 + if (crypto_tfm_alg_type(crypto_hash_tfm(mars_tfm)) != CRYPTO_ALG_TYPE_DIGEST) { + MARS_ERR("bad crypto hash type\n"); + return -EINVAL; + } +#endif + mars_digest_size = crypto_hash_digestsize(mars_tfm); + MARS_INF("digest_size = %d\n", mars_digest_size); + return 0; } void __exit exit_mars(void) { MARS_INF("exit_mars()\n"); + put_fake(); + + if (mars_tfm) { + crypto_free_hash(mars_tfm); + } + #ifdef MARS_TRACING if (mars_log_file) { filp_close(mars_log_file, NULL); diff --git a/sy_old/strategy.h b/sy_old/strategy.h index c9ada6fc..398c763d 100644 --- a/sy_old/strategy.h +++ b/sy_old/strategy.h @@ -132,11 +132,6 @@ extern struct mars_global *mars_global; extern int mars_power_button(struct mars_brick *brick, bool val, bool force_off); extern int mars_power_button_recursive(struct mars_brick *brick, bool val, bool force_off, int timeout); -/* Crypto stuff - */ -extern int mars_digest_size; -extern void mars_digest(unsigned char *digest, void *data, int len); - ///////////////////////////////////////////////////////////////////////// // quirk diff --git a/sy_old/sy_generic.c b/sy_old/sy_generic.c index dd860028..3b5f895c 100644 --- a/sy_old/sy_generic.c +++ b/sy_old/sy_generic.c @@ -260,34 +260,6 @@ EXPORT_SYMBOL_GPL(mars_remaining_space); ////////////////////////////////////////////////////////////// -// crypto stuff - -#include - -struct crypto_hash *mars_tfm = NULL; -int mars_digest_size = 0; -EXPORT_SYMBOL_GPL(mars_digest_size); - -void mars_digest(unsigned char *digest, void *data, int len) -{ - struct hash_desc desc = { - .tfm = mars_tfm, - .flags = 0, - }; - struct scatterlist sg; - - memset(digest, 0, mars_digest_size); - - crypto_hash_init(&desc); - sg_init_table(&sg, 1); - sg_set_buf(&sg, data, len); - crypto_hash_update(&desc, &sg, sg.length); - crypto_hash_final(&desc, digest); -} -EXPORT_SYMBOL_GPL(mars_digest); - -////////////////////////////////////////////////////////////// - // infrastructure struct mars_global *mars_global = NULL; @@ -1484,33 +1456,12 @@ int __init init_sy(void) _mars_trigger = __mars_trigger; - mars_tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC); - if (!mars_tfm) { - MARS_ERR("cannot alloc crypto hash\n"); - return -ENOMEM; - } - if (IS_ERR(mars_tfm)) { - MARS_ERR("alloc crypto hash failed, status = %d\n", (int)PTR_ERR(mars_tfm)); - return PTR_ERR(mars_tfm); - } -#if 0 - if (crypto_tfm_alg_type(crypto_hash_tfm(mars_tfm)) != CRYPTO_ALG_TYPE_DIGEST) { - MARS_ERR("bad crypto hash type\n"); - return -EINVAL; - } -#endif - mars_digest_size = crypto_hash_digestsize(mars_tfm); - MARS_INF("digest_size = %d\n", mars_digest_size); - return 0; } void __exit exit_sy(void) { MARS_INF("exit_sy()\n"); - if (mars_tfm) { - crypto_free_hash(mars_tfm); - } } #ifndef CONFIG_MARS_HAVE_BIGMODULE