blob: 716802b774ea9a92d58828cb1d7e99356098016b [file] [log] [blame]
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +03001/* mpicoder.c - Coder for the external representation of MPIs
2 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3 *
4 * This file is part of GnuPG.
5 *
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include "mpi-internal.h"
22
23#define DIM(v) (sizeof(v)/sizeof((v)[0]))
24#define MAX_EXTERN_MPI_BITS 16384
25
26static uint8_t asn[15] = /* Object ID is 1.3.14.3.2.26 */
27{ 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
28 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14
29};
30
31MPI do_encode_md(const void *sha_buffer, unsigned nbits)
32{
33 int nframe = (nbits + 7) / 8;
34 uint8_t *frame, *fr_pt;
35 int i = 0, n;
36 size_t asnlen = DIM(asn);
37 MPI a = MPI_NULL;
38
39 if (SHA1_DIGEST_LENGTH + asnlen + 4 > nframe)
40 pr_info("MPI: can't encode a %d bit MD into a %d bits frame\n",
41 (int)(SHA1_DIGEST_LENGTH * 8), (int)nbits);
42
43 /* We encode the MD in this way:
44 *
45 * 0 A PAD(n bytes) 0 ASN(asnlen bytes) MD(len bytes)
46 *
47 * PAD consists of FF bytes.
48 */
49 frame = kmalloc(nframe, GFP_KERNEL);
50 if (!frame)
51 return MPI_NULL;
52 n = 0;
53 frame[n++] = 0;
54 frame[n++] = 1; /* block type */
55 i = nframe - SHA1_DIGEST_LENGTH - asnlen - 3;
56
57 if (i <= 1) {
58 pr_info("MPI: message digest encoding failed\n");
59 kfree(frame);
60 return a;
61 }
62
63 memset(frame + n, 0xff, i);
64 n += i;
65 frame[n++] = 0;
66 memcpy(frame + n, &asn, asnlen);
67 n += asnlen;
68 memcpy(frame + n, sha_buffer, SHA1_DIGEST_LENGTH);
69 n += SHA1_DIGEST_LENGTH;
70
71 i = nframe;
72 fr_pt = frame;
73
74 if (n != nframe) {
75 printk
76 ("MPI: message digest encoding failed, frame length is wrong\n");
77 kfree(frame);
78 return a;
79 }
80
81 a = mpi_alloc((nframe + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB);
82 mpi_set_buffer(a, frame, nframe, 0);
83 kfree(frame);
84
85 return a;
86}
87
88MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
89{
90 const uint8_t *buffer = xbuffer;
91 int i, j;
92 unsigned nbits, nbytes, nlimbs, nread = 0;
93 mpi_limb_t a;
94 MPI val = MPI_NULL;
95
96 if (*ret_nread < 2)
97 goto leave;
98 nbits = buffer[0] << 8 | buffer[1];
99
100 if (nbits > MAX_EXTERN_MPI_BITS) {
101 pr_info("MPI: mpi too large (%u bits)\n", nbits);
102 goto leave;
103 }
104 buffer += 2;
105 nread = 2;
106
107 nbytes = (nbits + 7) / 8;
108 nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
109 val = mpi_alloc(nlimbs);
110 if (!val)
111 return MPI_NULL;
112 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
113 i %= BYTES_PER_MPI_LIMB;
114 val->nbits = nbits;
115 j = val->nlimbs = nlimbs;
116 val->sign = 0;
117 for (; j > 0; j--) {
118 a = 0;
119 for (; i < BYTES_PER_MPI_LIMB; i++) {
120 if (++nread > *ret_nread) {
121 printk
122 ("MPI: mpi larger than buffer nread=%d ret_nread=%d\n",
123 nread, *ret_nread);
124 goto leave;
125 }
126 a <<= 8;
127 a |= *buffer++;
128 }
129 i = 0;
130 val->d[j - 1] = a;
131 }
132
133leave:
134 *ret_nread = nread;
135 return val;
136}
137EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
138
139/****************
140 * Make an mpi from a character string.
141 */
142int mpi_fromstr(MPI val, const char *str)
143{
144 int hexmode = 0, sign = 0, prepend_zero = 0, i, j, c, c1, c2;
145 unsigned nbits, nbytes, nlimbs;
146 mpi_limb_t a;
147
148 if (*str == '-') {
149 sign = 1;
150 str++;
151 }
152 if (*str == '0' && str[1] == 'x')
153 hexmode = 1;
154 else
155 return -EINVAL; /* other bases are not yet supported */
156 str += 2;
157
158 nbits = strlen(str) * 4;
159 if (nbits % 8)
160 prepend_zero = 1;
161 nbytes = (nbits + 7) / 8;
162 nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
163 if (val->alloced < nlimbs)
164 if (!mpi_resize(val, nlimbs))
165 return -ENOMEM;
166 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
167 i %= BYTES_PER_MPI_LIMB;
168 j = val->nlimbs = nlimbs;
169 val->sign = sign;
170 for (; j > 0; j--) {
171 a = 0;
172 for (; i < BYTES_PER_MPI_LIMB; i++) {
173 if (prepend_zero) {
174 c1 = '0';
175 prepend_zero = 0;
176 } else
177 c1 = *str++;
178 assert(c1);
179 c2 = *str++;
180 assert(c2);
181 if (c1 >= '0' && c1 <= '9')
182 c = c1 - '0';
183 else if (c1 >= 'a' && c1 <= 'f')
184 c = c1 - 'a' + 10;
185 else if (c1 >= 'A' && c1 <= 'F')
186 c = c1 - 'A' + 10;
187 else {
188 mpi_clear(val);
189 return 1;
190 }
191 c <<= 4;
192 if (c2 >= '0' && c2 <= '9')
193 c |= c2 - '0';
194 else if (c2 >= 'a' && c2 <= 'f')
195 c |= c2 - 'a' + 10;
196 else if (c2 >= 'A' && c2 <= 'F')
197 c |= c2 - 'A' + 10;
198 else {
199 mpi_clear(val);
200 return 1;
201 }
202 a <<= 8;
203 a |= c;
204 }
205 i = 0;
206
207 val->d[j - 1] = a;
208 }
209
210 return 0;
211}
212EXPORT_SYMBOL_GPL(mpi_fromstr);
213
214/****************
215 * Special function to get the low 8 bytes from an mpi.
216 * This can be used as a keyid; KEYID is an 2 element array.
217 * Return the low 4 bytes.
218 */
219u32 mpi_get_keyid(const MPI a, u32 *keyid)
220{
221#if BYTES_PER_MPI_LIMB == 4
222 if (keyid) {
223 keyid[0] = a->nlimbs >= 2 ? a->d[1] : 0;
224 keyid[1] = a->nlimbs >= 1 ? a->d[0] : 0;
225 }
226 return a->nlimbs >= 1 ? a->d[0] : 0;
227#elif BYTES_PER_MPI_LIMB == 8
228 if (keyid) {
229 keyid[0] = a->nlimbs ? (u32) (a->d[0] >> 32) : 0;
230 keyid[1] = a->nlimbs ? (u32) (a->d[0] & 0xffffffff) : 0;
231 }
232 return a->nlimbs ? (u32) (a->d[0] & 0xffffffff) : 0;
233#else
234#error Make this function work with other LIMB sizes
235#endif
236}
237
238/****************
239 * Return an allocated buffer with the MPI (msb first).
240 * NBYTES receives the length of this buffer. Caller must free the
241 * return string (This function does return a 0 byte buffer with NBYTES
242 * set to zero if the value of A is zero. If sign is not NULL, it will
243 * be set to the sign of the A.
244 */
245void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
246{
247 uint8_t *p, *buffer;
248 mpi_limb_t alimb;
249 int i;
250 unsigned int n;
251
252 if (sign)
253 *sign = a->sign;
254 *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
255 if (!n)
256 n++; /* avoid zero length allocation */
257 p = buffer = kmalloc(n, GFP_KERNEL);
David Howells4bf19242012-01-18 10:03:54 +0000258 if (!p)
259 return NULL;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300260
261 for (i = a->nlimbs - 1; i >= 0; i--) {
262 alimb = a->d[i];
263#if BYTES_PER_MPI_LIMB == 4
264 *p++ = alimb >> 24;
265 *p++ = alimb >> 16;
266 *p++ = alimb >> 8;
267 *p++ = alimb;
268#elif BYTES_PER_MPI_LIMB == 8
269 *p++ = alimb >> 56;
270 *p++ = alimb >> 48;
271 *p++ = alimb >> 40;
272 *p++ = alimb >> 32;
273 *p++ = alimb >> 24;
274 *p++ = alimb >> 16;
275 *p++ = alimb >> 8;
276 *p++ = alimb;
277#else
278#error please implement for this limb size.
279#endif
280 }
281
282 /* this is sub-optimal but we need to do the shift operation
283 * because the caller has to free the returned buffer */
284 for (p = buffer; !*p && *nbytes; p++, --*nbytes)
285 ;
286 if (p != buffer)
287 memmove(buffer, p, *nbytes);
288
289 return buffer;
290}
291EXPORT_SYMBOL_GPL(mpi_get_buffer);
292
293/****************
294 * Use BUFFER to update MPI.
295 */
296int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign)
297{
298 const uint8_t *buffer = xbuffer, *p;
299 mpi_limb_t alimb;
300 int nlimbs;
301 int i;
302
303 nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
304 if (RESIZE_IF_NEEDED(a, nlimbs) < 0)
305 return -ENOMEM;
306 a->sign = sign;
307
308 for (i = 0, p = buffer + nbytes - 1; p >= buffer + BYTES_PER_MPI_LIMB;) {
309#if BYTES_PER_MPI_LIMB == 4
310 alimb = (mpi_limb_t) *p--;
311 alimb |= (mpi_limb_t) *p-- << 8;
312 alimb |= (mpi_limb_t) *p-- << 16;
313 alimb |= (mpi_limb_t) *p-- << 24;
314#elif BYTES_PER_MPI_LIMB == 8
315 alimb = (mpi_limb_t) *p--;
316 alimb |= (mpi_limb_t) *p-- << 8;
317 alimb |= (mpi_limb_t) *p-- << 16;
318 alimb |= (mpi_limb_t) *p-- << 24;
319 alimb |= (mpi_limb_t) *p-- << 32;
320 alimb |= (mpi_limb_t) *p-- << 40;
321 alimb |= (mpi_limb_t) *p-- << 48;
322 alimb |= (mpi_limb_t) *p-- << 56;
323#else
324#error please implement for this limb size.
325#endif
326 a->d[i++] = alimb;
327 }
328 if (p >= buffer) {
329#if BYTES_PER_MPI_LIMB == 4
330 alimb = *p--;
331 if (p >= buffer)
332 alimb |= (mpi_limb_t) *p-- << 8;
333 if (p >= buffer)
334 alimb |= (mpi_limb_t) *p-- << 16;
335 if (p >= buffer)
336 alimb |= (mpi_limb_t) *p-- << 24;
337#elif BYTES_PER_MPI_LIMB == 8
338 alimb = (mpi_limb_t) *p--;
339 if (p >= buffer)
340 alimb |= (mpi_limb_t) *p-- << 8;
341 if (p >= buffer)
342 alimb |= (mpi_limb_t) *p-- << 16;
343 if (p >= buffer)
344 alimb |= (mpi_limb_t) *p-- << 24;
345 if (p >= buffer)
346 alimb |= (mpi_limb_t) *p-- << 32;
347 if (p >= buffer)
348 alimb |= (mpi_limb_t) *p-- << 40;
349 if (p >= buffer)
350 alimb |= (mpi_limb_t) *p-- << 48;
351 if (p >= buffer)
352 alimb |= (mpi_limb_t) *p-- << 56;
353#else
354#error please implement for this limb size.
355#endif
356 a->d[i++] = alimb;
357 }
358 a->nlimbs = i;
359
360 if (i != nlimbs) {
361 pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i,
362 nlimbs);
363 BUG();
364 }
365 return 0;
366}
367EXPORT_SYMBOL_GPL(mpi_set_buffer);