Commit 9b45b7bb authored by Herbert Xu's avatar Herbert Xu

crypto: rsa - Generate fixed-length output

Every implementation of RSA that we have naturally generates
output with leading zeroes.  The one and only user of RSA,
pkcs1pad wants to have those leading zeroes in place, in fact
because they are currently absent it has to write those zeroes
itself.

So we shouldn't be stripping leading zeroes in the first place.
In fact this patch makes rsa-generic produce output with fixed
length so that pkcs1pad does not need to do any extra work.

This patch also changes DH to use the new interface.
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 50d2b643
...@@ -129,7 +129,7 @@ static int dh_compute_value(struct kpp_request *req) ...@@ -129,7 +129,7 @@ static int dh_compute_value(struct kpp_request *req)
if (ret) if (ret)
goto err_free_base; goto err_free_base;
ret = mpi_write_to_sgl(val, req->dst, &req->dst_len, &sign); ret = mpi_write_to_sgl(val, req->dst, req->dst_len, &sign);
if (ret) if (ret)
goto err_free_base; goto err_free_base;
......
...@@ -108,7 +108,7 @@ static int rsa_enc(struct akcipher_request *req) ...@@ -108,7 +108,7 @@ static int rsa_enc(struct akcipher_request *req)
if (ret) if (ret)
goto err_free_m; goto err_free_m;
ret = mpi_write_to_sgl(c, req->dst, &req->dst_len, &sign); ret = mpi_write_to_sgl(c, req->dst, req->dst_len, &sign);
if (ret) if (ret)
goto err_free_m; goto err_free_m;
...@@ -147,7 +147,7 @@ static int rsa_dec(struct akcipher_request *req) ...@@ -147,7 +147,7 @@ static int rsa_dec(struct akcipher_request *req)
if (ret) if (ret)
goto err_free_c; goto err_free_c;
ret = mpi_write_to_sgl(m, req->dst, &req->dst_len, &sign); ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign);
if (ret) if (ret)
goto err_free_c; goto err_free_c;
...@@ -185,7 +185,7 @@ static int rsa_sign(struct akcipher_request *req) ...@@ -185,7 +185,7 @@ static int rsa_sign(struct akcipher_request *req)
if (ret) if (ret)
goto err_free_m; goto err_free_m;
ret = mpi_write_to_sgl(s, req->dst, &req->dst_len, &sign); ret = mpi_write_to_sgl(s, req->dst, req->dst_len, &sign);
if (ret) if (ret)
goto err_free_m; goto err_free_m;
...@@ -226,7 +226,7 @@ static int rsa_verify(struct akcipher_request *req) ...@@ -226,7 +226,7 @@ static int rsa_verify(struct akcipher_request *req)
if (ret) if (ret)
goto err_free_s; goto err_free_s;
ret = mpi_write_to_sgl(m, req->dst, &req->dst_len, &sign); ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign);
if (ret) if (ret)
goto err_free_s; goto err_free_s;
......
...@@ -80,7 +80,7 @@ void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign); ...@@ -80,7 +80,7 @@ void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign);
int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
int *sign); int *sign);
void *mpi_get_secure_buffer(MPI a, unsigned *nbytes, int *sign); void *mpi_get_secure_buffer(MPI a, unsigned *nbytes, int *sign);
int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned *nbytes, int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned nbytes,
int *sign); int *sign);
#define log_mpidump g10_log_mpidump #define log_mpidump g10_log_mpidump
......
...@@ -237,16 +237,13 @@ EXPORT_SYMBOL_GPL(mpi_get_buffer); ...@@ -237,16 +237,13 @@ EXPORT_SYMBOL_GPL(mpi_get_buffer);
* @a: a multi precision integer * @a: a multi precision integer
* @sgl: scatterlist to write to. Needs to be at least * @sgl: scatterlist to write to. Needs to be at least
* mpi_get_size(a) long. * mpi_get_size(a) long.
* @nbytes: in/out param - it has the be set to the maximum number of * @nbytes: the number of bytes to write. Leading bytes will be
* bytes that can be written to sgl. This has to be at least * filled with zero.
* the size of the integer a. On return it receives the actual
* length of the data written on success or the data that would
* be written if buffer was too small.
* @sign: if not NULL, it will be set to the sign of a. * @sign: if not NULL, it will be set to the sign of a.
* *
* Return: 0 on success or error code in case of error * Return: 0 on success or error code in case of error
*/ */
int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned nbytes,
int *sign) int *sign)
{ {
u8 *p, *p2; u8 *p, *p2;
...@@ -258,43 +255,44 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, ...@@ -258,43 +255,44 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
#error please implement for this limb size. #error please implement for this limb size.
#endif #endif
unsigned int n = mpi_get_size(a); unsigned int n = mpi_get_size(a);
int i, x, y = 0, lzeros, buf_len; int i, x, buf_len;
if (!nbytes)
return -EINVAL;
if (sign) if (sign)
*sign = a->sign; *sign = a->sign;
lzeros = count_lzeros(a); if (nbytes < n)
if (*nbytes < n - lzeros) {
*nbytes = n - lzeros;
return -EOVERFLOW; return -EOVERFLOW;
}
*nbytes = n - lzeros;
buf_len = sgl->length; buf_len = sgl->length;
p2 = sg_virt(sgl); p2 = sg_virt(sgl);
for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB, while (nbytes > n) {
lzeros %= BYTES_PER_MPI_LIMB; if (!buf_len) {
i >= 0; i--) { sgl = sg_next(sgl);
if (!sgl)
return -EINVAL;
buf_len = sgl->length;
p2 = sg_virt(sgl);
}
i = min_t(unsigned, nbytes - n, buf_len);
memset(p2, 0, i);
p2 += i;
buf_len -= i;
nbytes -= i;
}
for (i = a->nlimbs - 1; i >= 0; i--) {
#if BYTES_PER_MPI_LIMB == 4 #if BYTES_PER_MPI_LIMB == 4
alimb = cpu_to_be32(a->d[i]); alimb = a->d[i] ? cpu_to_be32(a->d[i]) : 0;
#elif BYTES_PER_MPI_LIMB == 8 #elif BYTES_PER_MPI_LIMB == 8
alimb = cpu_to_be64(a->d[i]); alimb = a->d[i] ? cpu_to_be64(a->d[i]) : 0;
#else #else
#error please implement for this limb size. #error please implement for this limb size.
#endif #endif
if (lzeros) { p = (u8 *)&alimb;
y = lzeros;
lzeros = 0;
}
p = (u8 *)&alimb + y;
for (x = 0; x < sizeof(alimb) - y; x++) { for (x = 0; x < sizeof(alimb); x++) {
if (!buf_len) { if (!buf_len) {
sgl = sg_next(sgl); sgl = sg_next(sgl);
if (!sgl) if (!sgl)
...@@ -305,7 +303,6 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, ...@@ -305,7 +303,6 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
*p2++ = *p++; *p2++ = *p++;
buf_len--; buf_len--;
} }
y = 0;
} }
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment