crytpo: rsa - use mpi sgl helpers
Subsystems (2)
cover crytpo: rsa - use mpi sgl helpers
This series introduces new mpi helpers to read from and write to an sgl without a need of using an intermediate flat buffer.
First patch adds helpers to mpi library. Second patch changes rsa implementation to use the new functions.
This is an incremental patch series on top of these two:
https://patchwork.kernel.org/patch/7241041/
https://patchwork.kernel.org/patch/7241051/
A simple test for the new helpers can be found here:
https://github.com/tstruk/mpi_sgl_test/blob/master/test.c
---
Tadeusz Struk (2):
lib/mpi: Add mpi sgl helpers
crytpo: rsa - use mpi slg helpers to import and export data
crypto/rsa.c | 110 ++---------------------------- include/linux/mpi.h | 4 + lib/mpi/mpicoder.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 102 deletions(-)
--
PATCH 1/2 lib/mpi: Add mpi sgl helpers
Add mpi_read_raw_from_sgl and mpi_write_to_sgl helpers.
--- include/linux/mpi.h | 4 + lib/mpi/mpicoder.c | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 193 insertions(+)
include/linux/mpi.h +4 -0
@@ -31,6 +31,7 @@#define G10_MPI_H #include <linux/types.h> +#include <linux/scatterlist.h> /* DSI defines */
@@ -78,6 +79,7 @@ void mpi_swap(MPI a, MPI b);MPI do_encode_md (const void *sha_buffer,unsigned nbits);MPI mpi_read_raw_data (const void *xbuffer,size_t nbytes);MPI mpi_read_from_buffer (const void *buffer,unsigned *ret_nread); +MPI mpi_read_raw_from_sgl (struct scatterlist *sgl);int mpi_fromstr (MPI val,const char *str);u32 mpi_get_keyid (MPI a,u32 *keyid);void *mpi_get_buffer (MPI a,unsigned *nbytes,int *sign);
@@ -85,6 +87,8 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, int *sign);void *mpi_get_secure_buffer (MPI a,unsigned *nbytes,int *sign);int mpi_set_buffer (MPI a,const void *buffer,unsigned nbytes,int sign); +int mpi_write_to_sgl (MPI a,struct scatterlist *sg,unsigned *nbytes, +int *sign);#define log_mpidump g10_log_mpidump
lib/mpi/mpicoder.c +189 -0
192 lines not on this page · raw message
PATCH 2/2 crytpo: rsa - use mpi slg helpers to import and export data
In reply to Tadeusz StrukUse the new mpi_write_to_sgl and mpi_read_raw_from_sgl helpers to import and export data.
--- crypto/rsa.c | 127 +++++----------------------------------------------------- 1 file changed, 12 insertions(+), 115 deletions(-)
crypto/rsa.c +12 -115
@@ -13,7 +13,6 @@#include <crypto/internal/rsa.h> #include <crypto/internal/akcipher.h> #include <crypto/akcipher.h> -#include <crypto/scatterwalk.h> /* * RSAEP function [RFC3447 sec 5.1.1]
@@ -100,18 +99,7 @@ static int rsa_enc(struct akcipher_request *req) } ret = -ENOMEM ; -if (sg_is_last (req->src)) { - m =mpi_read_raw_data (sg_virt (req->src), src_len); - }else { -void *ptr =kmalloc (src_len,GFP_KERNEL ); - -if (!ptr) - goto err_free_c; - -scatterwalk_map_and_copy (ptr, req->src,0 , src_len,0 ); - m =mpi_read_raw_data (ptr, src_len); -kfree (ptr); - } + m =mpi_read_raw_from_sgl (req->src);if (!m) goto err_free_c;
@@ -119,26 +107,12 @@ static int rsa_enc(struct akcipher_request *req)if (ret) goto err_free_m; -if (sg_is_last (req->dst)) { - ret =mpi_read_buffer (c,sg_virt (req->dst), dst_len, - &req->out_len, &sign); - }else { -void *ptr =kmalloc (dst_len,GFP_KERNEL ); - -if (!ptr) - goto err_free_m; - - ret =mpi_read_buffer (c, ptr, dst_len, &req->out_len, &sign); -scatterwalk_map_and_copy (ptr, req->dst,0 , dst_len,1 ); -kfree (ptr); - } + ret =mpi_write_to_sgl (c, req->dst, &req->out_len, &sign);if (ret) goto err_free_m; -if (sign <0 ) { +if (sign <0 ) ret = -EBADMSG ; - goto err_free_m; - } err_free_m:mpi_free (m);
@@ -171,18 +145,7 @@ static int rsa_dec(struct akcipher_request *req) } ret = -ENOMEM ; -if (sg_is_last (req->src)) { - c =mpi_read_raw_data (sg_virt (req->src), src_len); - }else { -void *ptr =kmalloc (src_len,GFP_KERNEL ); - -if (!ptr) - goto err_free_m; - -scatterwalk_map_and_copy (ptr, req->src,0 , src_len,0 ); - c =mpi_read_raw_data (ptr, src_len); -kfree (ptr); - } + c =mpi_read_raw_from_sgl (req->src);if (!c) goto err_free_m;
@@ -190,27 +153,12 @@ static int rsa_dec(struct akcipher_request *req)if (ret) goto err_free_c; -if (sg_is_last (req->dst)) { - ret =mpi_read_buffer (m,sg_virt (req->dst), dst_len, - &req->out_len, &sign); - }else { -void *ptr =kmalloc (dst_len,GFP_KERNEL ); - -if (!ptr) - goto err_free_c; - - ret =mpi_read_buffer (m, ptr, dst_len, &req->out_len, &sign); -scatterwalk_map_and_copy (ptr, req->dst,0 , dst_len,1 ); -kfree (ptr); - } + ret =mpi_write_to_sgl (m, req->dst, &req->out_len, &sign);if (ret) goto err_free_c; -if (sign <0 ) { +if (sign <0 ) ret = -EBADMSG ; - goto err_free_c; - } - err_free_c:mpi_free (c); err_free_m:
@@ -242,19 +190,7 @@ static int rsa_sign(struct akcipher_request *req) } ret = -ENOMEM ; -if (sg_is_last (req->src)) { - m =mpi_read_raw_data (sg_virt (req->src), src_len); - }else { -void *ptr =kmalloc (src_len,GFP_KERNEL ); - -if (!ptr) - goto err_free_s; - -scatterwalk_map_and_copy (ptr, req->src,0 , src_len,0 ); - m =mpi_read_raw_data (ptr, src_len); -kfree (ptr); - - } + m =mpi_read_raw_from_sgl (req->src);if (!m) goto err_free_s;
@@ -262,26 +198,12 @@ static int rsa_sign(struct akcipher_request *req)if (ret) goto err_free_m; -if (sg_is_last (req->dst)) { - ret =mpi_read_buffer (s,sg_virt (req->dst), dst_len, - &req->out_len, &sign); - }else { -void *ptr =kmalloc (dst_len,GFP_KERNEL ); - -if (!ptr) - goto err_free_m; - - ret =mpi_read_buffer (s, ptr, dst_len, &req->out_len, &sign); -scatterwalk_map_and_copy (ptr, req->dst,0 , dst_len,1 ); -kfree (ptr); - } + ret =mpi_write_to_sgl (s, req->dst, &req->out_len, &sign);if (ret) goto err_free_m; -if (sign <0 ) { +if (sign <0 ) ret = -EBADMSG ; - goto err_free_m; - } err_free_m:mpi_free (m);
@@ -314,18 +236,7 @@ static int rsa_verify(struct akcipher_request *req) } ret = -ENOMEM ; -if (sg_is_last (req->src)) { - s =mpi_read_raw_data (sg_virt (req->src), src_len); - }else { -void *ptr =kmalloc (src_len,GFP_KERNEL ); - -if (!ptr) - goto err_free_m; - -scatterwalk_map_and_copy (ptr, req->src,0 , src_len,0 ); - s =mpi_read_raw_data (ptr, src_len); -kfree (ptr); - } + s =mpi_read_raw_from_sgl (req->src);if (!s) { ret = -ENOMEM ; goto err_free_m;
@@ -335,26 +246,12 @@ static int rsa_verify(struct akcipher_request *req)if (ret) goto err_free_s; -if (sg_is_last (req->dst)) { - ret =mpi_read_buffer (m,sg_virt (req->dst), dst_len, - &req->out_len, &sign); - }else { -void *ptr =kmalloc (dst_len,GFP_KERNEL ); - -if (!ptr) - goto err_free_s; - - ret =mpi_read_buffer (m, ptr, dst_len, &req->out_len, &sign); -scatterwalk_map_and_copy (ptr, req->dst,0 , dst_len,1 ); -kfree (ptr); - } + ret =mpi_write_to_sgl (m, req->dst, &req->out_len, &sign);if (ret) goto err_free_s; -if (sign <0 ) { +if (sign <0 ) ret = -EBADMSG ; - goto err_free_s; - } err_free_s:mpi_free (s);