2
0

bn_ctx.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #include "bn_lcl.h"
  11. /*-
  12. * TODO list
  13. *
  14. * 1. Check a bunch of "(words+1)" type hacks in various bignum functions and
  15. * check they can be safely removed.
  16. * - Check +1 and other ugliness in BN_from_montgomery()
  17. *
  18. * 2. Consider allowing a BN_new_ex() that, at least, lets you specify an
  19. * appropriate 'block' size that will be honoured by bn_expand_internal() to
  20. * prevent piddly little reallocations. OTOH, profiling bignum expansions in
  21. * BN_CTX doesn't show this to be a big issue.
  22. */
  23. /* How many bignums are in each "pool item"; */
  24. #define BN_CTX_POOL_SIZE 16
  25. /* The stack frame info is resizing, set a first-time expansion size; */
  26. #define BN_CTX_START_FRAMES 32
  27. /***********/
  28. /* BN_POOL */
  29. /***********/
  30. /* A bundle of bignums that can be linked with other bundles */
  31. typedef struct bignum_pool_item {
  32. /* The bignum values */
  33. BIGNUM vals[BN_CTX_POOL_SIZE];
  34. /* Linked-list admin */
  35. struct bignum_pool_item *prev, *next;
  36. } BN_POOL_ITEM;
  37. /* A linked-list of bignums grouped in bundles */
  38. typedef struct bignum_pool {
  39. /* Linked-list admin */
  40. BN_POOL_ITEM *head, *current, *tail;
  41. /* Stack depth and allocation size */
  42. unsigned used, size;
  43. } BN_POOL;
  44. static void BN_POOL_init(BN_POOL *);
  45. static void BN_POOL_finish(BN_POOL *);
  46. static BIGNUM *BN_POOL_get(BN_POOL *, int);
  47. static void BN_POOL_release(BN_POOL *, unsigned int);
  48. /************/
  49. /* BN_STACK */
  50. /************/
  51. /* A wrapper to manage the "stack frames" */
  52. typedef struct bignum_ctx_stack {
  53. /* Array of indexes into the bignum stack */
  54. unsigned int *indexes;
  55. /* Number of stack frames, and the size of the allocated array */
  56. unsigned int depth, size;
  57. } BN_STACK;
  58. static void BN_STACK_init(BN_STACK *);
  59. static void BN_STACK_finish(BN_STACK *);
  60. static int BN_STACK_push(BN_STACK *, unsigned int);
  61. static unsigned int BN_STACK_pop(BN_STACK *);
  62. /**********/
  63. /* BN_CTX */
  64. /**********/
  65. /* The opaque BN_CTX type */
  66. struct bignum_ctx {
  67. /* The bignum bundles */
  68. BN_POOL pool;
  69. /* The "stack frames", if you will */
  70. BN_STACK stack;
  71. /* The number of bignums currently assigned */
  72. unsigned int used;
  73. /* Depth of stack overflow */
  74. int err_stack;
  75. /* Block "gets" until an "end" (compatibility behaviour) */
  76. int too_many;
  77. /* Flags. */
  78. int flags;
  79. };
  80. /* Enable this to find BN_CTX bugs */
  81. #ifdef BN_CTX_DEBUG
  82. static const char *ctxdbg_cur = NULL;
  83. static void ctxdbg(BN_CTX *ctx)
  84. {
  85. unsigned int bnidx = 0, fpidx = 0;
  86. BN_POOL_ITEM *item = ctx->pool.head;
  87. BN_STACK *stack = &ctx->stack;
  88. fprintf(stderr, "(%16p): ", ctx);
  89. while (bnidx < ctx->used) {
  90. fprintf(stderr, "%03x ", item->vals[bnidx++ % BN_CTX_POOL_SIZE].dmax);
  91. if (!(bnidx % BN_CTX_POOL_SIZE))
  92. item = item->next;
  93. }
  94. fprintf(stderr, "\n");
  95. bnidx = 0;
  96. fprintf(stderr, " : ");
  97. while (fpidx < stack->depth) {
  98. while (bnidx++ < stack->indexes[fpidx])
  99. fprintf(stderr, " ");
  100. fprintf(stderr, "^^^ ");
  101. bnidx++;
  102. fpidx++;
  103. }
  104. fprintf(stderr, "\n");
  105. }
  106. # define CTXDBG_ENTRY(str, ctx) do { \
  107. ctxdbg_cur = (str); \
  108. fprintf(stderr,"Starting %s\n", ctxdbg_cur); \
  109. ctxdbg(ctx); \
  110. } while(0)
  111. # define CTXDBG_EXIT(ctx) do { \
  112. fprintf(stderr,"Ending %s\n", ctxdbg_cur); \
  113. ctxdbg(ctx); \
  114. } while(0)
  115. # define CTXDBG_RET(ctx,ret)
  116. #else
  117. # define CTXDBG_ENTRY(str, ctx)
  118. # define CTXDBG_EXIT(ctx)
  119. # define CTXDBG_RET(ctx,ret)
  120. #endif
  121. BN_CTX *BN_CTX_new(void)
  122. {
  123. BN_CTX *ret;
  124. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
  125. BNerr(BN_F_BN_CTX_NEW, ERR_R_MALLOC_FAILURE);
  126. return NULL;
  127. }
  128. /* Initialise the structure */
  129. BN_POOL_init(&ret->pool);
  130. BN_STACK_init(&ret->stack);
  131. return ret;
  132. }
  133. BN_CTX *BN_CTX_secure_new(void)
  134. {
  135. BN_CTX *ret = BN_CTX_new();
  136. if (ret != NULL)
  137. ret->flags = BN_FLG_SECURE;
  138. return ret;
  139. }
  140. void BN_CTX_free(BN_CTX *ctx)
  141. {
  142. if (ctx == NULL)
  143. return;
  144. #ifdef BN_CTX_DEBUG
  145. {
  146. BN_POOL_ITEM *pool = ctx->pool.head;
  147. fprintf(stderr, "BN_CTX_free, stack-size=%d, pool-bignums=%d\n",
  148. ctx->stack.size, ctx->pool.size);
  149. fprintf(stderr, "dmaxs: ");
  150. while (pool) {
  151. unsigned loop = 0;
  152. while (loop < BN_CTX_POOL_SIZE)
  153. fprintf(stderr, "%02x ", pool->vals[loop++].dmax);
  154. pool = pool->next;
  155. }
  156. fprintf(stderr, "\n");
  157. }
  158. #endif
  159. BN_STACK_finish(&ctx->stack);
  160. BN_POOL_finish(&ctx->pool);
  161. OPENSSL_free(ctx);
  162. }
  163. void BN_CTX_start(BN_CTX *ctx)
  164. {
  165. CTXDBG_ENTRY("BN_CTX_start", ctx);
  166. /* If we're already overflowing ... */
  167. if (ctx->err_stack || ctx->too_many)
  168. ctx->err_stack++;
  169. /* (Try to) get a new frame pointer */
  170. else if (!BN_STACK_push(&ctx->stack, ctx->used)) {
  171. BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
  172. ctx->err_stack++;
  173. }
  174. CTXDBG_EXIT(ctx);
  175. }
  176. void BN_CTX_end(BN_CTX *ctx)
  177. {
  178. CTXDBG_ENTRY("BN_CTX_end", ctx);
  179. if (ctx->err_stack)
  180. ctx->err_stack--;
  181. else {
  182. unsigned int fp = BN_STACK_pop(&ctx->stack);
  183. /* Does this stack frame have anything to release? */
  184. if (fp < ctx->used)
  185. BN_POOL_release(&ctx->pool, ctx->used - fp);
  186. ctx->used = fp;
  187. /* Unjam "too_many" in case "get" had failed */
  188. ctx->too_many = 0;
  189. }
  190. CTXDBG_EXIT(ctx);
  191. }
  192. BIGNUM *BN_CTX_get(BN_CTX *ctx)
  193. {
  194. BIGNUM *ret;
  195. CTXDBG_ENTRY("BN_CTX_get", ctx);
  196. if (ctx->err_stack || ctx->too_many)
  197. return NULL;
  198. if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {
  199. /*
  200. * Setting too_many prevents repeated "get" attempts from cluttering
  201. * the error stack.
  202. */
  203. ctx->too_many = 1;
  204. BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
  205. return NULL;
  206. }
  207. /* OK, make sure the returned bignum is "zero" */
  208. BN_zero(ret);
  209. ctx->used++;
  210. CTXDBG_RET(ctx, ret);
  211. return ret;
  212. }
  213. /************/
  214. /* BN_STACK */
  215. /************/
  216. static void BN_STACK_init(BN_STACK *st)
  217. {
  218. st->indexes = NULL;
  219. st->depth = st->size = 0;
  220. }
  221. static void BN_STACK_finish(BN_STACK *st)
  222. {
  223. OPENSSL_free(st->indexes);
  224. st->indexes = NULL;
  225. }
  226. static int BN_STACK_push(BN_STACK *st, unsigned int idx)
  227. {
  228. if (st->depth == st->size) {
  229. /* Need to expand */
  230. unsigned int newsize =
  231. st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES;
  232. unsigned int *newitems;
  233. if ((newitems = OPENSSL_malloc(sizeof(*newitems) * newsize)) == NULL) {
  234. BNerr(BN_F_BN_STACK_PUSH, ERR_R_MALLOC_FAILURE);
  235. return 0;
  236. }
  237. if (st->depth)
  238. memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth);
  239. OPENSSL_free(st->indexes);
  240. st->indexes = newitems;
  241. st->size = newsize;
  242. }
  243. st->indexes[(st->depth)++] = idx;
  244. return 1;
  245. }
  246. static unsigned int BN_STACK_pop(BN_STACK *st)
  247. {
  248. return st->indexes[--(st->depth)];
  249. }
  250. /***********/
  251. /* BN_POOL */
  252. /***********/
  253. static void BN_POOL_init(BN_POOL *p)
  254. {
  255. p->head = p->current = p->tail = NULL;
  256. p->used = p->size = 0;
  257. }
  258. static void BN_POOL_finish(BN_POOL *p)
  259. {
  260. unsigned int loop;
  261. BIGNUM *bn;
  262. while (p->head) {
  263. for (loop = 0, bn = p->head->vals; loop++ < BN_CTX_POOL_SIZE; bn++)
  264. if (bn->d)
  265. BN_clear_free(bn);
  266. p->current = p->head->next;
  267. OPENSSL_free(p->head);
  268. p->head = p->current;
  269. }
  270. }
  271. static BIGNUM *BN_POOL_get(BN_POOL *p, int flag)
  272. {
  273. BIGNUM *bn;
  274. unsigned int loop;
  275. /* Full; allocate a new pool item and link it in. */
  276. if (p->used == p->size) {
  277. BN_POOL_ITEM *item;
  278. if ((item = OPENSSL_malloc(sizeof(*item))) == NULL) {
  279. BNerr(BN_F_BN_POOL_GET, ERR_R_MALLOC_FAILURE);
  280. return NULL;
  281. }
  282. for (loop = 0, bn = item->vals; loop++ < BN_CTX_POOL_SIZE; bn++) {
  283. bn_init(bn);
  284. if ((flag & BN_FLG_SECURE) != 0)
  285. BN_set_flags(bn, BN_FLG_SECURE);
  286. }
  287. item->prev = p->tail;
  288. item->next = NULL;
  289. if (p->head == NULL)
  290. p->head = p->current = p->tail = item;
  291. else {
  292. p->tail->next = item;
  293. p->tail = item;
  294. p->current = item;
  295. }
  296. p->size += BN_CTX_POOL_SIZE;
  297. p->used++;
  298. /* Return the first bignum from the new pool */
  299. return item->vals;
  300. }
  301. if (!p->used)
  302. p->current = p->head;
  303. else if ((p->used % BN_CTX_POOL_SIZE) == 0)
  304. p->current = p->current->next;
  305. return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
  306. }
  307. static void BN_POOL_release(BN_POOL *p, unsigned int num)
  308. {
  309. unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
  310. p->used -= num;
  311. while (num--) {
  312. bn_check_top(p->current->vals + offset);
  313. if (offset == 0) {
  314. offset = BN_CTX_POOL_SIZE - 1;
  315. p->current = p->current->prev;
  316. } else
  317. offset--;
  318. }
  319. }