diff options
author | Tadeusz Struk <tadeusz.struk@intel.com> | 2015-06-15 16:18:36 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2015-06-16 02:35:06 -0400 |
commit | d37e296979ed1652aec6850e2d736bd0ebf0cdb1 (patch) | |
tree | 6f26c2f3a80bbf2e04c3be67edb22631d705de95 /lib/mpi | |
parent | 4beb106045976b785a05e77ab5430ad04b6038b7 (diff) |
MPILIB: add mpi_read_buf() and mpi_get_size() helpers
Added a mpi_read_buf() helper function to export MPI to a buf provided by
the user, and a mpi_get_size() helper, that tells the user how big the buf is.
Changed mpi_free to use kzfree instead of kfree because it is used to free
crypto keys.
Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'lib/mpi')
-rw-r--r-- | lib/mpi/mpicoder.c | 87 | ||||
-rw-r--r-- | lib/mpi/mpiutil.c | 6 |
2 files changed, 71 insertions, 22 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 | ||
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 | ||