ctx.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* Written by Ulf Moeller for the OpenSSL project. */
  2. /* ====================================================================
  3. * Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com). */
  53. #include <openssl/bn.h>
  54. #include <string.h>
  55. #include <openssl/err.h>
  56. #include <openssl/mem.h>
  57. #include "../internal.h"
  58. /* How many bignums are in each "pool item"; */
  59. #define BN_CTX_POOL_SIZE 16
  60. /* The stack frame info is resizing, set a first-time expansion size; */
  61. #define BN_CTX_START_FRAMES 32
  62. /* A bundle of bignums that can be linked with other bundles */
  63. typedef struct bignum_pool_item {
  64. /* The bignum values */
  65. BIGNUM vals[BN_CTX_POOL_SIZE];
  66. /* Linked-list admin */
  67. struct bignum_pool_item *prev, *next;
  68. } BN_POOL_ITEM;
  69. typedef struct bignum_pool {
  70. /* Linked-list admin */
  71. BN_POOL_ITEM *head, *current, *tail;
  72. /* Stack depth and allocation size */
  73. unsigned used, size;
  74. } BN_POOL;
  75. static void BN_POOL_init(BN_POOL *);
  76. static void BN_POOL_finish(BN_POOL *);
  77. static BIGNUM *BN_POOL_get(BN_POOL *);
  78. static void BN_POOL_release(BN_POOL *, unsigned int);
  79. /************/
  80. /* BN_STACK */
  81. /************/
  82. /* A wrapper to manage the "stack frames" */
  83. typedef struct bignum_ctx_stack {
  84. /* Array of indexes into the bignum stack */
  85. unsigned int *indexes;
  86. /* Number of stack frames, and the size of the allocated array */
  87. unsigned int depth, size;
  88. } BN_STACK;
  89. static void BN_STACK_init(BN_STACK *);
  90. static void BN_STACK_finish(BN_STACK *);
  91. static int BN_STACK_push(BN_STACK *, unsigned int);
  92. static unsigned int BN_STACK_pop(BN_STACK *);
  93. /**********/
  94. /* BN_CTX */
  95. /**********/
  96. /* The opaque BN_CTX type */
  97. struct bignum_ctx {
  98. /* The bignum bundles */
  99. BN_POOL pool;
  100. /* The "stack frames", if you will */
  101. BN_STACK stack;
  102. /* The number of bignums currently assigned */
  103. unsigned int used;
  104. /* Depth of stack overflow */
  105. int err_stack;
  106. /* Block "gets" until an "end" (compatibility behaviour) */
  107. int too_many;
  108. };
  109. BN_CTX *BN_CTX_new(void) {
  110. BN_CTX *ret = OPENSSL_malloc(sizeof(BN_CTX));
  111. if (!ret) {
  112. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  113. return NULL;
  114. }
  115. /* Initialise the structure */
  116. BN_POOL_init(&ret->pool);
  117. BN_STACK_init(&ret->stack);
  118. ret->used = 0;
  119. ret->err_stack = 0;
  120. ret->too_many = 0;
  121. return ret;
  122. }
  123. void BN_CTX_free(BN_CTX *ctx) {
  124. if (ctx == NULL) {
  125. return;
  126. }
  127. BN_STACK_finish(&ctx->stack);
  128. BN_POOL_finish(&ctx->pool);
  129. OPENSSL_free(ctx);
  130. }
  131. void BN_CTX_start(BN_CTX *ctx) {
  132. /* If we're already overflowing ... */
  133. if (ctx->err_stack || ctx->too_many) {
  134. ctx->err_stack++;
  135. } else if (!BN_STACK_push(&ctx->stack, ctx->used)) {
  136. /* (Try to) get a new frame pointer */
  137. OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
  138. ctx->err_stack++;
  139. }
  140. }
  141. BIGNUM *BN_CTX_get(BN_CTX *ctx) {
  142. BIGNUM *ret;
  143. if (ctx->err_stack || ctx->too_many) {
  144. return NULL;
  145. }
  146. ret = BN_POOL_get(&ctx->pool);
  147. if (ret == NULL) {
  148. /* Setting too_many prevents repeated "get" attempts from
  149. * cluttering the error stack. */
  150. ctx->too_many = 1;
  151. OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
  152. return NULL;
  153. }
  154. /* OK, make sure the returned bignum is "zero" */
  155. BN_zero(ret);
  156. ctx->used++;
  157. return ret;
  158. }
  159. void BN_CTX_end(BN_CTX *ctx) {
  160. if (ctx->err_stack) {
  161. ctx->err_stack--;
  162. } else {
  163. unsigned int fp = BN_STACK_pop(&ctx->stack);
  164. /* Does this stack frame have anything to release? */
  165. if (fp < ctx->used) {
  166. BN_POOL_release(&ctx->pool, ctx->used - fp);
  167. }
  168. ctx->used = fp;
  169. /* Unjam "too_many" in case "get" had failed */
  170. ctx->too_many = 0;
  171. }
  172. }
  173. /************/
  174. /* BN_STACK */
  175. /************/
  176. static void BN_STACK_init(BN_STACK *st) {
  177. st->indexes = NULL;
  178. st->depth = st->size = 0;
  179. }
  180. static void BN_STACK_finish(BN_STACK *st) {
  181. OPENSSL_free(st->indexes);
  182. }
  183. static int BN_STACK_push(BN_STACK *st, unsigned int idx) {
  184. if (st->depth == st->size) {
  185. /* Need to expand */
  186. unsigned int newsize =
  187. (st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES);
  188. unsigned int *newitems = OPENSSL_malloc(newsize * sizeof(unsigned int));
  189. if (!newitems) {
  190. return 0;
  191. }
  192. if (st->depth) {
  193. OPENSSL_memcpy(newitems, st->indexes, st->depth * sizeof(unsigned int));
  194. }
  195. OPENSSL_free(st->indexes);
  196. st->indexes = newitems;
  197. st->size = newsize;
  198. }
  199. st->indexes[(st->depth)++] = idx;
  200. return 1;
  201. }
  202. static unsigned int BN_STACK_pop(BN_STACK *st) {
  203. return st->indexes[--(st->depth)];
  204. }
  205. static void BN_POOL_init(BN_POOL *p) {
  206. p->head = p->current = p->tail = NULL;
  207. p->used = p->size = 0;
  208. }
  209. static void BN_POOL_finish(BN_POOL *p) {
  210. while (p->head) {
  211. unsigned int loop = 0;
  212. BIGNUM *bn = p->head->vals;
  213. while (loop++ < BN_CTX_POOL_SIZE) {
  214. if (bn->d) {
  215. BN_clear_free(bn);
  216. }
  217. bn++;
  218. }
  219. p->current = p->head->next;
  220. OPENSSL_free(p->head);
  221. p->head = p->current;
  222. }
  223. }
  224. static BIGNUM *BN_POOL_get(BN_POOL *p) {
  225. if (p->used == p->size) {
  226. BIGNUM *bn;
  227. unsigned int loop = 0;
  228. BN_POOL_ITEM *item = OPENSSL_malloc(sizeof(BN_POOL_ITEM));
  229. if (!item) {
  230. return NULL;
  231. }
  232. /* Initialise the structure */
  233. bn = item->vals;
  234. while (loop++ < BN_CTX_POOL_SIZE) {
  235. BN_init(bn++);
  236. }
  237. item->prev = p->tail;
  238. item->next = NULL;
  239. /* Link it in */
  240. if (!p->head) {
  241. p->head = p->current = p->tail = item;
  242. } else {
  243. p->tail->next = item;
  244. p->tail = item;
  245. p->current = item;
  246. }
  247. p->size += BN_CTX_POOL_SIZE;
  248. p->used++;
  249. /* Return the first bignum from the new pool */
  250. return item->vals;
  251. }
  252. if (!p->used) {
  253. p->current = p->head;
  254. } else if ((p->used % BN_CTX_POOL_SIZE) == 0) {
  255. p->current = p->current->next;
  256. }
  257. return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
  258. }
  259. static void BN_POOL_release(BN_POOL *p, unsigned int num) {
  260. unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
  261. p->used -= num;
  262. while (num--) {
  263. if (!offset) {
  264. offset = BN_CTX_POOL_SIZE - 1;
  265. p->current = p->current->prev;
  266. } else {
  267. offset--;
  268. }
  269. }
  270. }