[PATCH] crypto: tcrypt - avoid mapping from module image addresses

2015-08-27 Thread Horia Geantă
The output buffer in test_ahash_speed will point to an address located
within the tcrypt module image.
This causes problems when trying to DMA map the buffer.
For e.g. on ARM-based LS1021A, a page fault occurs within the
DMA API when trying to access the struct page returned by
virt_to_page(output):

insmod tcrypt.ko mode=403

testing speed of async sha1 (sha1-caam)
test  0 (   16 byte blocks,   16 bytes per update,   1 updates):
Unable to handle kernel paging request at virtual address f07e9080
pgd = e58d0e00
[f07e9080] *pgd=8080007003, *pmd=
Internal error: Oops: 206 [#1] SMP THUMB2
Modules linked in: tcrypt(+)
CPU: 1 PID: 1119 Comm: insmod Not tainted 4.2.0-rc1-256134-gbf433416e675 #1
Hardware name: Freescale LS1021A
task: ea063900 ti: e5a34000 task.ti: e5a34000
PC is at dma_cache_maint_page+0x38/0xd0
LR is at __dma_page_cpu_to_dev+0x15/0x64
pc : [800155a0]lr : [8001564d]psr: 000f0033
sp : e5a35ca0  ip : 8063df00  fp : f07e9080
r10: 0cd0  r9 : 8063df00  r8 : 805a2f04
r7 : 0017f804  r6 : 0002  r5 : ee7f9000  r4 : 0014
r3 : 80612d40  r2 : 01ff0080  r1 : 0380  r0 : ee7f9000
Flags: nzcv  IRQs on  FIQs on  Mode SVC_32  ISA Thumb  Segment user
Control: 70c5387d  Table: e58d0e00  DAC: 9b7ede70
Process insmod (pid: 1119, stack limit = 0xe5a34210)
Stack: (0xe5a35ca0 to 0xe5a36000)
[...]
[800155a0] (dma_cache_maint_page) from [8001564d] 
(__dma_page_cpu_to_dev+0x15/0x64)
[8001564d] (__dma_page_cpu_to_dev) from [800156eb] 
(arm_dma_map_page+0x1f/0x44)
[800156eb] (arm_dma_map_page) from [802935e3] (ahash_digest+0x35f/0x510)
[802935e3] (ahash_digest) from [7f800d03] 
(test_ahash_speed.constprop.6+0x24a/0x4e4 [tcrypt])
[7f800d03] (test_ahash_speed.constprop.6 [tcrypt]) from [7f802fd5] 
(do_test+0x1898/0x2058 [tcrypt])
[7f802fd5] (do_test [tcrypt]) from [7f80802f] (tcrypt_mod_init+0x2e/0x63 
[tcrypt])
[7f80802f] (tcrypt_mod_init [tcrypt]) from [80009517] 
(do_one_initcall+0xb3/0x134)
[80009517] (do_one_initcall) from [80351ec7] (do_init_module+0x3b/0x13c)
[80351ec7] (do_init_module) from [8005cc3f] (load_module+0x97b/0x9dc)
[8005cc3f] (load_module) from [8005cd8d] (SyS_finit_module+0x35/0x3e)
[8005cd8d] (SyS_finit_module) from [8000d101] (ret_fast_syscall+0x1/0x4c)
Code: 1aba 0152 eb00 0b02 (5882) 0f92

addr2line -f -i -e vmlinux 800155a0
page_zonenum
include/linux/mm.h:728
page_zone
include/linux/mm.h:881
dma_cache_maint_page
arch/arm/mm/dma-mapping.c:822

Signed-off-by: Horia Geantă horia.gea...@freescale.com
---

Only reproduced on ARM-based platforms (LS1021A, i.MX6).
For some unknown reason, PPC-based ones do not exhibit the problem.

 crypto/tcrypt.c | 17 +
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/crypto/tcrypt.c b/crypto/tcrypt.c
index 2b00b617daab..46a4a757d478 100644
--- a/crypto/tcrypt.c
+++ b/crypto/tcrypt.c
@@ -48,6 +48,8 @@
 #define ENCRYPT 1
 #define DECRYPT 0
 
+#define MAX_DIGEST_SIZE64
+
 /*
  * return a string with the driver name
  */
@@ -950,7 +952,7 @@ static void test_ahash_speed(const char *algo, unsigned int 
secs,
struct tcrypt_result tresult;
struct ahash_request *req;
struct crypto_ahash *tfm;
-   static char output[1024];
+   char *output;
int i, ret;
 
tfm = crypto_alloc_ahash(algo, 0, 0);
@@ -963,9 +965,9 @@ static void test_ahash_speed(const char *algo, unsigned int 
secs,
printk(KERN_INFO \ntesting speed of async %s (%s)\n, algo,
get_driver_name(crypto_ahash, tfm));
 
-   if (crypto_ahash_digestsize(tfm)  sizeof(output)) {
-   pr_err(digestsize(%u)  outputbuffer(%zu)\n,
-  crypto_ahash_digestsize(tfm), sizeof(output));
+   if (crypto_ahash_digestsize(tfm)  MAX_DIGEST_SIZE) {
+   pr_err(digestsize(%u)  %d\n, crypto_ahash_digestsize(tfm),
+  MAX_DIGEST_SIZE);
goto out;
}
 
@@ -980,6 +982,10 @@ static void test_ahash_speed(const char *algo, unsigned 
int secs,
ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
   tcrypt_complete, tresult);
 
+   output = kmalloc(MAX_DIGEST_SIZE, GFP_KERNEL);
+   if (!output)
+   goto out_nomem;
+
for (i = 0; speed[i].blen != 0; i++) {
if (speed[i].blen  TVMEMSIZE * PAGE_SIZE) {
pr_err(template (%u) too big for tvmem (%lu)\n,
@@ -1006,6 +1012,9 @@ static void test_ahash_speed(const char *algo, unsigned 
int secs,
}
}
 
+   kfree(output);
+
+out_nomem:
ahash_request_free(req);
 
 out:
-- 
2.4.4

--
To unsubscribe from this list: send the line unsubscribe linux-crypto in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH] crypto: Fix ASN.1 key handling for RSA akcipher

2015-08-27 Thread Marcel Holtmann
The RSA algorithm provides two ASN.1 key types. One for RSA Private Key
and another for RSA Public Key. Use these two already defined ASN.1
definitions instead of inventing a new one.

Signed-off-by: Marcel Holtmann mar...@holtmann.org
---
 crypto/Makefile   |  9 ++---
 crypto/rsa_helper.c   | 13 +
 crypto/rsakey.asn1|  5 -
 crypto/rsaprivatekey.asn1 | 13 +
 crypto/rsapublickey.asn1  |  4 
 5 files changed, 32 insertions(+), 12 deletions(-)
 delete mode 100644 crypto/rsakey.asn1
 create mode 100644 crypto/rsaprivatekey.asn1
 create mode 100644 crypto/rsapublickey.asn1

diff --git a/crypto/Makefile b/crypto/Makefile
index 3cc91c3301c7..0b056c411aa7 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -31,10 +31,13 @@ obj-$(CONFIG_CRYPTO_HASH2) += crypto_hash.o
 obj-$(CONFIG_CRYPTO_PCOMP2) += pcompress.o
 obj-$(CONFIG_CRYPTO_AKCIPHER2) += akcipher.o
 
-$(obj)/rsakey-asn1.o: $(obj)/rsakey-asn1.c $(obj)/rsakey-asn1.h
-clean-files += rsakey-asn1.c rsakey-asn1.h
+$(obj)/rsapublickey-asn1.o: $(obj)/rsapublickey-asn1.c 
$(obj)/rsapublickey-asn1.h
+clean-files += rsapublickey-asn1.c rsapublickey-asn1.h
 
-rsa_generic-y := rsakey-asn1.o
+$(obj)/rsaprivatekey-asn1.o: $(obj)/rsaprivatekey-asn1.c 
$(obj)/rsaprivatekey-asn1.h
+clean-files += rsaprivatekey-asn1.c rsaprivatekey-asn1.h
+
+rsa_generic-y := rsapublickey-asn1.o rsaprivatekey-asn1.o
 rsa_generic-y += rsa.o
 rsa_generic-y += rsa_helper.o
 obj-$(CONFIG_CRYPTO_RSA) += rsa_generic.o
diff --git a/crypto/rsa_helper.c b/crypto/rsa_helper.c
index 8d96ce969b44..26617e3132fb 100644
--- a/crypto/rsa_helper.c
+++ b/crypto/rsa_helper.c
@@ -15,7 +15,8 @@
 #include linux/err.h
 #include linux/fips.h
 #include crypto/internal/rsa.h
-#include rsakey-asn1.h
+#include rsapublickey-asn1.h
+#include rsaprivatekey-asn1.h
 
 int rsa_get_n(void *context, size_t hdrlen, unsigned char tag,
  const void *value, size_t vlen)
@@ -109,9 +110,13 @@ int rsa_parse_key(struct rsa_key *rsa_key, const void *key,
int ret;
 
free_mpis(rsa_key);
-   ret = asn1_ber_decoder(rsakey_decoder, rsa_key, key, key_len);
-   if (ret  0)
-   goto error;
+   ret = asn1_ber_decoder(rsapublickey_decoder, rsa_key, key, key_len);
+   if (ret  0) {
+   ret = asn1_ber_decoder(rsaprivatekey_decoder, rsa_key,
+  key, key_len);
+   if (ret  0)
+   goto error;
+   }
 
return 0;
 error:
diff --git a/crypto/rsakey.asn1 b/crypto/rsakey.asn1
deleted file mode 100644
index 3c7b5df7b428..
--- a/crypto/rsakey.asn1
+++ /dev/null
@@ -1,5 +0,0 @@
-RsaKey ::= SEQUENCE {
-   n INTEGER ({ rsa_get_n }),
-   e INTEGER ({ rsa_get_e }),
-   d INTEGER ({ rsa_get_d })
-}
diff --git a/crypto/rsaprivatekey.asn1 b/crypto/rsaprivatekey.asn1
new file mode 100644
index ..58dddc7c1536
--- /dev/null
+++ b/crypto/rsaprivatekey.asn1
@@ -0,0 +1,13 @@
+RSAPrivateKey ::= SEQUENCE {
+   version Version,
+   modulus INTEGER ({ rsa_get_n }),-- n
+   publicExponent  INTEGER ({ rsa_get_e }),-- e
+   privateExponent INTEGER ({ rsa_get_d }),-- d
+   prime1  INTEGER,-- p
+   prime2  INTEGER,-- q
+   exponent1   INTEGER,-- d mod (p-1)
+   exponent2   INTEGER,-- d mod (q-1)
+   coefficient INTEGER -- (inverse of q) mod p
+}
+
+Version ::= INTEGER
diff --git a/crypto/rsapublickey.asn1 b/crypto/rsapublickey.asn1
new file mode 100644
index ..8f7f8760f2a9
--- /dev/null
+++ b/crypto/rsapublickey.asn1
@@ -0,0 +1,4 @@
+RSAPublicKey ::= SEQUENCE {
+   modulus INTEGER ({ rsa_get_n }),-- n
+   publicExponent  INTEGER ({ rsa_get_e }) -- e
+}
-- 
2.4.3

--
To unsubscribe from this list: send the line unsubscribe linux-crypto in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html