diff options
Diffstat (limited to 'lib/mpi')
| -rw-r--r-- | lib/mpi/longlong.h | 4 | ||||
| -rw-r--r-- | lib/mpi/mpicoder.c | 87 | ||||
| -rw-r--r-- | lib/mpi/mpiutil.c | 6 |
3 files changed, 73 insertions, 24 deletions
diff --git a/lib/mpi/longlong.h b/lib/mpi/longlong.h index aac511417ad1..a89d041592c8 100644 --- a/lib/mpi/longlong.h +++ b/lib/mpi/longlong.h | |||
| @@ -639,7 +639,7 @@ do { \ | |||
| 639 | ************** MIPS ***************** | 639 | ************** MIPS ***************** |
| 640 | ***************************************/ | 640 | ***************************************/ |
| 641 | #if defined(__mips__) && W_TYPE_SIZE == 32 | 641 | #if defined(__mips__) && W_TYPE_SIZE == 32 |
| 642 | #if __GNUC__ >= 4 && __GNUC_MINOR__ >= 4 | 642 | #if (__GNUC__ >= 5) || (__GNUC__ >= 4 && __GNUC_MINOR__ >= 4) |
| 643 | #define umul_ppmm(w1, w0, u, v) \ | 643 | #define umul_ppmm(w1, w0, u, v) \ |
| 644 | do { \ | 644 | do { \ |
| 645 | UDItype __ll = (UDItype)(u) * (v); \ | 645 | UDItype __ll = (UDItype)(u) * (v); \ |
| @@ -671,7 +671,7 @@ do { \ | |||
| 671 | ************** MIPS/64 ************** | 671 | ************** MIPS/64 ************** |
| 672 | ***************************************/ | 672 | ***************************************/ |
| 673 | #if (defined(__mips) && __mips >= 3) && W_TYPE_SIZE == 64 | 673 | #if (defined(__mips) && __mips >= 3) && W_TYPE_SIZE == 64 |
| 674 | #if __GNUC__ >= 4 && __GNUC_MINOR__ >= 4 | 674 | #if (__GNUC__ >= 5) || (__GNUC__ >= 4 && __GNUC_MINOR__ >= 4) |
| 675 | #define umul_ppmm(w1, w0, u, v) \ | 675 | #define umul_ppmm(w1, w0, u, v) \ |
| 676 | do { \ | 676 | do { \ |
| 677 | typedef unsigned int __ll_UTItype __attribute__((mode(TI))); \ | 677 | typedef unsigned int __ll_UTItype __attribute__((mode(TI))); \ |
diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index 4cc6442733f4..bc0a1da8afba 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c | |||
| @@ -128,28 +128,36 @@ leave: | |||
| 128 | } | 128 | } |
| 129 | EXPORT_SYMBOL_GPL(mpi_read_from_buffer); | 129 | EXPORT_SYMBOL_GPL(mpi_read_from_buffer); |
| 130 | 130 | ||
| 131 | /**************** | 131 | /** |
| 132 | * Return an allocated buffer with the MPI (msb first). | 132 | * mpi_read_buffer() - read MPI to a bufer provided by user (msb first) |
| 133 | * NBYTES receives the length of this buffer. Caller must free the | 133 | * |
| 134 | * return string (This function does return a 0 byte buffer with NBYTES | 134 | * @a: a multi precision integer |
| 135 | * set to zero if the value of A is zero. If sign is not NULL, it will | 135 | * @buf: bufer to which the output will be written to. Needs to be at |
| 136 | * be set to the sign of the A. | 136 | * leaset mpi_get_size(a) long. |
| 137 | * @buf_len: size of the buf. | ||
| 138 | * @nbytes: receives the actual length of the data written. | ||
| 139 | * @sign: if not NULL, it will be set to the sign of a. | ||
| 140 | * | ||
| 141 | * Return: 0 on success or error code in case of error | ||
| 137 | */ | 142 | */ |
| 138 | void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign) | 143 | int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, |
| 144 | int *sign) | ||
| 139 | { | 145 | { |
| 140 | uint8_t *p, *buffer; | 146 | uint8_t *p; |
| 141 | mpi_limb_t alimb; | 147 | mpi_limb_t alimb; |
| 148 | unsigned int n = mpi_get_size(a); | ||
| 142 | int i; | 149 | int i; |
| 143 | unsigned int n; | 150 | |
| 151 | if (buf_len < n || !buf) | ||
| 152 | return -EINVAL; | ||
| 144 | 153 | ||
| 145 | if (sign) | 154 | if (sign) |
| 146 | *sign = a->sign; | 155 | *sign = a->sign; |
| 147 | *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB; | 156 | |
| 148 | if (!n) | 157 | if (nbytes) |
| 149 | n++; /* avoid zero length allocation */ | 158 | *nbytes = n; |
| 150 | p = buffer = kmalloc(n, GFP_KERNEL); | 159 | |
| 151 | if (!p) | 160 | p = buf; |
| 152 | return NULL; | ||
| 153 | 161 | ||
| 154 | for (i = a->nlimbs - 1; i >= 0; i--) { | 162 | for (i = a->nlimbs - 1; i >= 0; i--) { |
| 155 | alimb = a->d[i]; | 163 | alimb = a->d[i]; |
| @@ -171,15 +179,56 @@ void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign) | |||
| 171 | #error please implement for this limb size. | 179 | #error please implement for this limb size. |
| 172 | #endif | 180 | #endif |
| 173 | } | 181 | } |
| 182 | return 0; | ||
| 183 | } | ||
| 184 | EXPORT_SYMBOL_GPL(mpi_read_buffer); | ||
| 185 | |||
| 186 | /* | ||
| 187 | * mpi_get_buffer() - Returns an allocated buffer with the MPI (msb first). | ||
| 188 | * Caller must free the return string. | ||
| 189 | * This function does return a 0 byte buffer with nbytes set to zero if the | ||
| 190 | * value of A is zero. | ||
| 191 | * | ||
| 192 | * @a: a multi precision integer. | ||
| 193 | * @nbytes: receives the length of this buffer. | ||
| 194 | * @sign: if not NULL, it will be set to the sign of the a. | ||
| 195 | * | ||
| 196 | * Return: Pointer to MPI buffer or NULL on error | ||
| 197 | */ | ||
| 198 | void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign) | ||
| 199 | { | ||
| 200 | uint8_t *buf, *p; | ||
| 201 | unsigned int n; | ||
| 202 | int ret; | ||
| 203 | |||
| 204 | if (!nbytes) | ||
| 205 | return NULL; | ||
| 206 | |||
| 207 | n = mpi_get_size(a); | ||
| 208 | |||
| 209 | if (!n) | ||
| 210 | n++; | ||
| 211 | |||
| 212 | buf = kmalloc(n, GFP_KERNEL); | ||
| 213 | |||
| 214 | if (!buf) | ||
| 215 | return NULL; | ||
| 216 | |||
| 217 | ret = mpi_read_buffer(a, buf, n, nbytes, sign); | ||
| 218 | |||
| 219 | if (ret) { | ||
| 220 | kfree(buf); | ||
| 221 | return NULL; | ||
| 222 | } | ||
| 174 | 223 | ||
| 175 | /* this is sub-optimal but we need to do the shift operation | 224 | /* this is sub-optimal but we need to do the shift operation |
| 176 | * because the caller has to free the returned buffer */ | 225 | * because the caller has to free the returned buffer */ |
| 177 | for (p = buffer; !*p && *nbytes; p++, --*nbytes) | 226 | for (p = buf; !*p && *nbytes; p++, --*nbytes) |
| 178 | ; | 227 | ; |
| 179 | if (p != buffer) | 228 | if (p != buf) |
| 180 | memmove(buffer, p, *nbytes); | 229 | memmove(buf, p, *nbytes); |
| 181 | 230 | ||
| 182 | return buffer; | 231 | return buf; |
| 183 | } | 232 | } |
| 184 | EXPORT_SYMBOL_GPL(mpi_get_buffer); | 233 | EXPORT_SYMBOL_GPL(mpi_get_buffer); |
| 185 | 234 | ||
diff --git a/lib/mpi/mpiutil.c b/lib/mpi/mpiutil.c index bf076d281d40..314f4dfa603e 100644 --- a/lib/mpi/mpiutil.c +++ b/lib/mpi/mpiutil.c | |||
| @@ -69,7 +69,7 @@ void mpi_free_limb_space(mpi_ptr_t a) | |||
| 69 | if (!a) | 69 | if (!a) |
| 70 | return; | 70 | return; |
| 71 | 71 | ||
| 72 | kfree(a); | 72 | kzfree(a); |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs) | 75 | void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs) |
| @@ -95,7 +95,7 @@ int mpi_resize(MPI a, unsigned nlimbs) | |||
| 95 | if (!p) | 95 | if (!p) |
| 96 | return -ENOMEM; | 96 | return -ENOMEM; |
| 97 | memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t)); | 97 | memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t)); |
| 98 | kfree(a->d); | 98 | kzfree(a->d); |
| 99 | a->d = p; | 99 | a->d = p; |
| 100 | } else { | 100 | } else { |
| 101 | a->d = kzalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL); | 101 | a->d = kzalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL); |
| @@ -112,7 +112,7 @@ void mpi_free(MPI a) | |||
| 112 | return; | 112 | return; |
| 113 | 113 | ||
| 114 | if (a->flags & 4) | 114 | if (a->flags & 4) |
| 115 | kfree(a->d); | 115 | kzfree(a->d); |
| 116 | else | 116 | else |
| 117 | mpi_free_limb_space(a->d); | 117 | mpi_free_limb_space(a->d); |
| 118 | 118 | ||
