mpicoder.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 = MPI_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 MPI_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. mpi_set_buffer(a, frame, nframe, 0);
  72. kfree(frame);
  73. return a;
  74. }
  75. MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
  76. {
  77. const uint8_t *buffer = xbuffer;
  78. int i, j;
  79. unsigned nbits, nbytes, nlimbs, nread = 0;
  80. mpi_limb_t a;
  81. MPI val = MPI_NULL;
  82. if (*ret_nread < 2)
  83. goto leave;
  84. nbits = buffer[0] << 8 | buffer[1];
  85. if (nbits > MAX_EXTERN_MPI_BITS) {
  86. pr_info("MPI: mpi too large (%u bits)\n", nbits);
  87. goto leave;
  88. }
  89. buffer += 2;
  90. nread = 2;
  91. nbytes = (nbits + 7) / 8;
  92. nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
  93. val = mpi_alloc(nlimbs);
  94. if (!val)
  95. return MPI_NULL;
  96. i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
  97. i %= BYTES_PER_MPI_LIMB;
  98. val->nbits = nbits;
  99. j = val->nlimbs = nlimbs;
  100. val->sign = 0;
  101. for (; j > 0; j--) {
  102. a = 0;
  103. for (; i < BYTES_PER_MPI_LIMB; i++) {
  104. if (++nread > *ret_nread) {
  105. printk
  106. ("MPI: mpi larger than buffer nread=%d ret_nread=%d\n",
  107. nread, *ret_nread);
  108. goto leave;
  109. }
  110. a <<= 8;
  111. a |= *buffer++;
  112. }
  113. i = 0;
  114. val->d[j - 1] = a;
  115. }
  116. leave:
  117. *ret_nread = nread;
  118. return val;
  119. }
  120. EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
  121. /****************
  122. * Make an mpi from a character string.
  123. */
  124. int mpi_fromstr(MPI val, const char *str)
  125. {
  126. int hexmode = 0, sign = 0, prepend_zero = 0, i, j, c, c1, c2;
  127. unsigned nbits, nbytes, nlimbs;
  128. mpi_limb_t a;
  129. if (*str == '-') {
  130. sign = 1;
  131. str++;
  132. }
  133. if (*str == '0' && str[1] == 'x')
  134. hexmode = 1;
  135. else
  136. return -EINVAL; /* other bases are not yet supported */
  137. str += 2;
  138. nbits = strlen(str) * 4;
  139. if (nbits % 8)
  140. prepend_zero = 1;
  141. nbytes = (nbits + 7) / 8;
  142. nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
  143. if (val->alloced < nlimbs)
  144. if (!mpi_resize(val, nlimbs))
  145. return -ENOMEM;
  146. i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
  147. i %= BYTES_PER_MPI_LIMB;
  148. j = val->nlimbs = nlimbs;
  149. val->sign = sign;
  150. for (; j > 0; j--) {
  151. a = 0;
  152. for (; i < BYTES_PER_MPI_LIMB; i++) {
  153. if (prepend_zero) {
  154. c1 = '0';
  155. prepend_zero = 0;
  156. } else
  157. c1 = *str++;
  158. assert(c1);
  159. c2 = *str++;
  160. assert(c2);
  161. if (c1 >= '0' && c1 <= '9')
  162. c = c1 - '0';
  163. else if (c1 >= 'a' && c1 <= 'f')
  164. c = c1 - 'a' + 10;
  165. else if (c1 >= 'A' && c1 <= 'F')
  166. c = c1 - 'A' + 10;
  167. else {
  168. mpi_clear(val);
  169. return 1;
  170. }
  171. c <<= 4;
  172. if (c2 >= '0' && c2 <= '9')
  173. c |= c2 - '0';
  174. else if (c2 >= 'a' && c2 <= 'f')
  175. c |= c2 - 'a' + 10;
  176. else if (c2 >= 'A' && c2 <= 'F')
  177. c |= c2 - 'A' + 10;
  178. else {
  179. mpi_clear(val);
  180. return 1;
  181. }
  182. a <<= 8;
  183. a |= c;
  184. }
  185. i = 0;
  186. val->d[j - 1] = a;
  187. }
  188. return 0;
  189. }
  190. EXPORT_SYMBOL_GPL(mpi_fromstr);
  191. /****************
  192. * Special function to get the low 8 bytes from an mpi.
  193. * This can be used as a keyid; KEYID is an 2 element array.
  194. * Return the low 4 bytes.
  195. */
  196. u32 mpi_get_keyid(const MPI a, u32 *keyid)
  197. {
  198. #if BYTES_PER_MPI_LIMB == 4
  199. if (keyid) {
  200. keyid[0] = a->nlimbs >= 2 ? a->d[1] : 0;
  201. keyid[1] = a->nlimbs >= 1 ? a->d[0] : 0;
  202. }
  203. return a->nlimbs >= 1 ? a->d[0] : 0;
  204. #elif BYTES_PER_MPI_LIMB == 8
  205. if (keyid) {
  206. keyid[0] = a->nlimbs ? (u32) (a->d[0] >> 32) : 0;
  207. keyid[1] = a->nlimbs ? (u32) (a->d[0] & 0xffffffff) : 0;
  208. }
  209. return a->nlimbs ? (u32) (a->d[0] & 0xffffffff) : 0;
  210. #else
  211. #error Make this function work with other LIMB sizes
  212. #endif
  213. }
  214. /****************
  215. * Return an allocated buffer with the MPI (msb first).
  216. * NBYTES receives the length of this buffer. Caller must free the
  217. * return string (This function does return a 0 byte buffer with NBYTES
  218. * set to zero if the value of A is zero. If sign is not NULL, it will
  219. * be set to the sign of the A.
  220. */
  221. void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
  222. {
  223. uint8_t *p, *buffer;
  224. mpi_limb_t alimb;
  225. int i;
  226. unsigned int n;
  227. if (sign)
  228. *sign = a->sign;
  229. *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
  230. if (!n)
  231. n++; /* avoid zero length allocation */
  232. p = buffer = kmalloc(n, GFP_KERNEL);
  233. for (i = a->nlimbs - 1; i >= 0; i--) {
  234. alimb = a->d[i];
  235. #if BYTES_PER_MPI_LIMB == 4
  236. *p++ = alimb >> 24;
  237. *p++ = alimb >> 16;
  238. *p++ = alimb >> 8;
  239. *p++ = alimb;
  240. #elif BYTES_PER_MPI_LIMB == 8
  241. *p++ = alimb >> 56;
  242. *p++ = alimb >> 48;
  243. *p++ = alimb >> 40;
  244. *p++ = alimb >> 32;
  245. *p++ = alimb >> 24;
  246. *p++ = alimb >> 16;
  247. *p++ = alimb >> 8;
  248. *p++ = alimb;
  249. #else
  250. #error please implement for this limb size.
  251. #endif
  252. }
  253. /* this is sub-optimal but we need to do the shift operation
  254. * because the caller has to free the returned buffer */
  255. for (p = buffer; !*p && *nbytes; p++, --*nbytes)
  256. ;
  257. if (p != buffer)
  258. memmove(buffer, p, *nbytes);
  259. return buffer;
  260. }
  261. EXPORT_SYMBOL_GPL(mpi_get_buffer);
  262. /****************
  263. * Use BUFFER to update MPI.
  264. */
  265. int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign)
  266. {
  267. const uint8_t *buffer = xbuffer, *p;
  268. mpi_limb_t alimb;
  269. int nlimbs;
  270. int i;
  271. nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
  272. if (RESIZE_IF_NEEDED(a, nlimbs) < 0)
  273. return -ENOMEM;
  274. a->sign = sign;
  275. for (i = 0, p = buffer + nbytes - 1; p >= buffer + BYTES_PER_MPI_LIMB;) {
  276. #if BYTES_PER_MPI_LIMB == 4
  277. alimb = (mpi_limb_t) *p--;
  278. alimb |= (mpi_limb_t) *p-- << 8;
  279. alimb |= (mpi_limb_t) *p-- << 16;
  280. alimb |= (mpi_limb_t) *p-- << 24;
  281. #elif BYTES_PER_MPI_LIMB == 8
  282. alimb = (mpi_limb_t) *p--;
  283. alimb |= (mpi_limb_t) *p-- << 8;
  284. alimb |= (mpi_limb_t) *p-- << 16;
  285. alimb |= (mpi_limb_t) *p-- << 24;
  286. alimb |= (mpi_limb_t) *p-- << 32;
  287. alimb |= (mpi_limb_t) *p-- << 40;
  288. alimb |= (mpi_limb_t) *p-- << 48;
  289. alimb |= (mpi_limb_t) *p-- << 56;
  290. #else
  291. #error please implement for this limb size.
  292. #endif
  293. a->d[i++] = alimb;
  294. }
  295. if (p >= buffer) {
  296. #if BYTES_PER_MPI_LIMB == 4
  297. alimb = *p--;
  298. if (p >= buffer)
  299. alimb |= (mpi_limb_t) *p-- << 8;
  300. if (p >= buffer)
  301. alimb |= (mpi_limb_t) *p-- << 16;
  302. if (p >= buffer)
  303. alimb |= (mpi_limb_t) *p-- << 24;
  304. #elif BYTES_PER_MPI_LIMB == 8
  305. alimb = (mpi_limb_t) *p--;
  306. if (p >= buffer)
  307. alimb |= (mpi_limb_t) *p-- << 8;
  308. if (p >= buffer)
  309. alimb |= (mpi_limb_t) *p-- << 16;
  310. if (p >= buffer)
  311. alimb |= (mpi_limb_t) *p-- << 24;
  312. if (p >= buffer)
  313. alimb |= (mpi_limb_t) *p-- << 32;
  314. if (p >= buffer)
  315. alimb |= (mpi_limb_t) *p-- << 40;
  316. if (p >= buffer)
  317. alimb |= (mpi_limb_t) *p-- << 48;
  318. if (p >= buffer)
  319. alimb |= (mpi_limb_t) *p-- << 56;
  320. #else
  321. #error please implement for this limb size.
  322. #endif
  323. a->d[i++] = alimb;
  324. }
  325. a->nlimbs = i;
  326. if (i != nlimbs) {
  327. pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i,
  328. nlimbs);
  329. BUG();
  330. }
  331. return 0;
  332. }
  333. EXPORT_SYMBOL_GPL(mpi_set_buffer);