diff options
Diffstat (limited to 'lib/mpi/mpicoder.c')
| -rw-r--r-- | lib/mpi/mpicoder.c | 87 |
1 files changed, 68 insertions, 19 deletions
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 | ||
