mpicoder.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* 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. #include "mpi-internal.h"
  21. #define DIM(v) (sizeof(v)/sizeof((v)[0]))
  22. #define MAX_EXTERN_MPI_BITS 16384
  23. static uint8_t asn[15] = /* Object ID is 1.3.14.3.2.26 */
  24. { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
  25. 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14
  26. };
  27. MPI do_encode_md(const void *sha_buffer, unsigned nbits)
  28. {
  29. int nframe = (nbits + 7) / 8;
  30. uint8_t *frame, *fr_pt;
  31. int i = 0, n;
  32. size_t asnlen = DIM(asn);
  33. MPI a = NULL;
  34. if (SHA1_DIGEST_LENGTH + asnlen + 4 > nframe)
  35. pr_info("MPI: can't encode a %d bit MD into a %d bits frame\n",
  36. (int)(SHA1_DIGEST_LENGTH * 8), (int)nbits);
  37. /* We encode the MD in this way:
  38. *
  39. * 0 A PAD(n bytes) 0 ASN(asnlen bytes) MD(len bytes)
  40. *
  41. * PAD consists of FF bytes.
  42. */
  43. frame = kmalloc(nframe, GFP_KERNEL);
  44. if (!frame)
  45. return NULL;
  46. n = 0;
  47. frame[n++] = 0;
  48. frame[n++] = 1; /* block type */
  49. i = nframe - SHA1_DIGEST_LENGTH - asnlen - 3;
  50. if (i <= 1) {
  51. pr_info("MPI: message digest encoding failed\n");
  52. kfree(frame);
  53. return a;
  54. }
  55. memset(frame + n, 0xff, i);
  56. n += i;
  57. frame[n++] = 0;
  58. memcpy(frame + n, &asn, asnlen);
  59. n += asnlen;
  60. memcpy(frame + n, sha_buffer, SHA1_DIGEST_LENGTH);
  61. n += SHA1_DIGEST_LENGTH;
  62. i = nframe;
  63. fr_pt = frame;
  64. if (n != nframe) {
  65. printk
  66. ("MPI: message digest encoding failed, frame length is wrong\n");
  67. kfree(frame);
  68. return a;
  69. }
  70. a = mpi_alloc((nframe + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB);
  71. if (a)
  72. mpi_set_buffer(a, frame, nframe, 0);
  73. kfree(frame);
  74. return a;
  75. }
  76. MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
  77. {
  78. const uint8_t *buffer = xbuffer;
  79. int i, j;
  80. unsigned nbits, nbytes, nlimbs, nread = 0;
  81. mpi_limb_t a;
  82. MPI val = NULL;
  83. if (*ret_nread < 2)
  84. goto leave;
  85. nbits = buffer[0] << 8 | buffer[1];
  86. if (nbits > MAX_EXTERN_MPI_BITS) {
  87. pr_info("MPI: mpi too large (%u bits)\n", nbits);
  88. goto leave;
  89. }
  90. buffer += 2;
  91. nread = 2;
  92. nbytes = (nbits + 7) / 8;
  93. nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
  94. val = mpi_alloc(nlimbs);
  95. if (!val)
  96. return NULL;
  97. i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
  98. i %= BYTES_PER_MPI_LIMB;
  99. val->nbits = nbits;
  100. j = val->nlimbs = nlimbs;
  101. val->sign = 0;
  102. for (; j > 0; j--) {
  103. a = 0;
  104. for (; i < BYTES_PER_MPI_LIMB; i++) {
  105. if (++nread > *ret_nread) {
  106. printk
  107. ("MPI: mpi larger than buffer nread=%d ret_nread=%d\n",
  108. nread, *ret_nread);
  109. goto leave;
  110. }
  111. a <<= 8;
  112. a |= *buffer++;
  113. }
  114. i = 0;
  115. val->d[j - 1] = a;
  116. }
  117. leave:
  118. *ret_nread = nread;
  119. return val;
  120. }
  121. EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
  122. /****************
  123. * Make an mpi from a character string.
  124. */
  125. int mpi_fromstr(MPI val, const char *str)
  126. {
  127. int hexmode = 0, sign = 0, prepend_zero = 0, i, j, c, c1, c2;
  128. unsigned nbits, nbytes, nlimbs;
  129. mpi_limb_t a;
  130. if (*str == '-') {
  131. sign = 1;
  132. str++;
  133. }
  134. if (*str == '0' && str[1] == 'x')
  135. hexmode = 1;
  136. else
  137. return -EINVAL; /* other bases are not yet supported */
  138. str += 2;
  139. nbits = strlen(str) * 4;
  140. if (nbits % 8)
  141. prepend_zero = 1;
  142. nbytes = (nbits + 7) / 8;
  143. nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
  144. if (val->alloced < nlimbs)
  145. if (!mpi_resize(val, nlimbs))
  146. return -ENOMEM;
  147. i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
  148. i %= BYTES_PER_MPI_LIMB;
  149. j = val->nlimbs = nlimbs;
  150. val->sign = sign;
  151. for (; j > 0; j--) {
  152. a = 0;
  153. for (; i < BYTES_PER_MPI_LIMB; i++) {
  154. if (prepend_zero) {
  155. c1 = '0';
  156. prepend_zero = 0;
  157. } else
  158. c1 = *str++;
  159. assert(c1);
  160. c2 = *str++;
  161. assert(c2);
  162. if (c1 >= '0' && c1 <= '9')
  163. c = c1 - '0';
  164. else if (c1 >= 'a' && c1 <= 'f')
  165. c = c1 - 'a' + 10;
  166. else if (c1 >= 'A' && c1 <= 'F')
  167. c = c1 - 'A' + 10;
  168. else {
  169. mpi_clear(val);
  170. return 1;
  171. }
  172. c <<= 4;
  173. if (c2 >= '0' && c2 <= '9')
  174. c |= c2 - '0';
  175. else if (c2 >= 'a' && c2 <= 'f')
  176. c |= c2 - 'a' + 10;
  177. else if (c2 >= 'A' && c2 <= 'F')
  178. c |= c2 - 'A' + 10;
  179. else {
  180. mpi_clear(val);
  181. return 1;
  182. }
  183. a <<= 8;
  184. a |= c;
  185. }
  186. i = 0;
  187. val->d[j - 1] = a;
  188. }
  189. return 0;
  190. }
  191. EXPORT_SYMBOL_GPL(mpi_fromstr);
  192. /****************
  193. * Special function to get the low 8 bytes from an mpi.
  194. * This can be used as a keyid; KEYID is an 2 element array.
  195. * Return the low 4 bytes.
  196. */
  197. u32 mpi_get_keyid(const MPI a, u32 *keyid)
  198. {
  199. #if BYTES_PER_MPI_LIMB == 4
  200. if (keyid) {
  201. keyid[0] = a->nlimbs >= 2 ? a->d[1] : 0;
  202. keyid[1] = a->nlimbs >= 1 ? a->d[0] : 0;
  203. }
  204. return a->nlimbs >= 1 ? a->d[0] : 0;
  205. #elif BYTES_PER_MPI_LIMB == 8
  206. if (keyid) {
  207. keyid[0] = a->nlimbs ? (u32) (a->d[0] >> 32) : 0;
  208. keyid[1] = a->nlimbs ? (u32) (a->d[0] & 0xffffffff) : 0;
  209. }
  210. return a->nlimbs ? (u32) (a->d[0] & 0xffffffff) : 0;
  211. #else
  212. #error Make this function work with other LIMB sizes
  213. #endif
  214. }
  215. /****************
  216. * Return an allocated buffer with the MPI (msb first).
  217. * NBYTES receives the length of this buffer. Caller must free the
  218. * return string (This function does return a 0 byte buffer with NBYTES
  219. * set to zero if the value of A is zero. If sign is not NULL, it will
  220. * be set to the sign of the A.
  221. */
  222. void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
  223. {
  224. uint8_t *p, *buffer;
  225. mpi_limb_t alimb;
  226. int i;
  227. unsigned int n;
  228. if (sign)
  229. *sign = a->sign;
  230. *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
  231. if (!n)
  232. n++; /* avoid zero length allocation */
  233. p = buffer = kmalloc(n, GFP_KERNEL);
  234. if (!p)
  235. return NULL;
  236. for (i = a->nlimbs - 1; i >= 0; i--) {
  237. alimb = a->d[i];
  238. #if BYTES_PER_MPI_LIMB == 4
  239. *p++ = alimb >> 24;
  240. *p++ = alimb >> 16;
  241. *p++ = alimb >> 8;
  242. *p++ = alimb;
  243. #elif BYTES_PER_MPI_LIMB == 8
  244. *p++ = alimb >> 56;
  245. *p++ = alimb >> 48;
  246. *p++ = alimb >> 40;
  247. *p++ = alimb >> 32;
  248. *p++ = alimb >> 24;
  249. *p++ = alimb >> 16;
  250. *p++ = alimb >> 8;
  251. *p++ = alimb;
  252. #else
  253. #error please implement for this limb size.
  254. #endif
  255. }
  256. /* this is sub-optimal but we need to do the shift operation
  257. * because the caller has to free the returned buffer */
  258. for (p = buffer; !*p && *nbytes; p++, --*nbytes)
  259. ;
  260. if (p != buffer)
  261. memmove(buffer, p, *nbytes);
  262. return buffer;
  263. }
  264. EXPORT_SYMBOL_GPL(mpi_get_buffer);
  265. /****************
  266. * Use BUFFER to update MPI.
  267. */
  268. int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign)
  269. {
  270. const uint8_t *buffer = xbuffer, *p;
  271. mpi_limb_t alimb;
  272. int nlimbs;
  273. int i;
  274. nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
  275. if (RESIZE_IF_NEEDED(a, nlimbs) < 0)
  276. return -ENOMEM;
  277. a->sign = sign;
  278. for (i = 0, p = buffer + nbytes - 1; p >= buffer + BYTES_PER_MPI_LIMB;) {
  279. #if BYTES_PER_MPI_LIMB == 4
  280. alimb = (mpi_limb_t) *p--;
  281. alimb |= (mpi_limb_t) *p-- << 8;
  282. alimb |= (mpi_limb_t) *p-- << 16;
  283. alimb |= (mpi_limb_t) *p-- << 24;
  284. #elif BYTES_PER_MPI_LIMB == 8
  285. alimb = (mpi_limb_t) *p--;
  286. alimb |= (mpi_limb_t) *p-- << 8;
  287. alimb |= (mpi_limb_t) *p-- << 16;
  288. alimb |= (mpi_limb_t) *p-- << 24;
  289. alimb |= (mpi_limb_t) *p-- << 32;
  290. alimb |= (mpi_limb_t) *p-- << 40;
  291. alimb |= (mpi_limb_t) *p-- << 48;
  292. alimb |= (mpi_limb_t) *p-- << 56;
  293. #else
  294. #error please implement for this limb size.
  295. #endif
  296. a->d[i++] = alimb;
  297. }
  298. if (p >= buffer) {
  299. #if BYTES_PER_MPI_LIMB == 4
  300. alimb = *p--;
  301. if (p >= buffer)
  302. alimb |= (mpi_limb_t) *p-- << 8;
  303. if (p >= buffer)
  304. alimb |= (mpi_limb_t) *p-- << 16;
  305. if (p >= buffer)
  306. alimb |= (mpi_limb_t) *p-- << 24;
  307. #elif BYTES_PER_MPI_LIMB == 8
  308. alimb = (mpi_limb_t) *p--;
  309. if (p >= buffer)
  310. alimb |= (mpi_limb_t) *p-- << 8;
  311. if (p >= buffer)
  312. alimb |= (mpi_limb_t) *p-- << 16;
  313. if (p >= buffer)
  314. alimb |= (mpi_limb_t) *p-- << 24;
  315. if (p >= buffer)
  316. alimb |= (mpi_limb_t) *p-- << 32;
  317. if (p >= buffer)
  318. alimb |= (mpi_limb_t) *p-- << 40;
  319. if (p >= buffer)
  320. alimb |= (mpi_limb_t) *p-- << 48;
  321. if (p >= buffer)
  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. a->nlimbs = i;
  329. if (i != nlimbs) {
  330. pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i,
  331. nlimbs);
  332. BUG();
  333. }
  334. return 0;
  335. }
  336. EXPORT_SYMBOL_GPL(mpi_set_buffer);