ctx.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. // BN_STACK
  80. // A wrapper to manage the "stack frames"
  81. typedef struct bignum_ctx_stack {
  82. // Array of indexes into the bignum stack
  83. unsigned int *indexes;
  84. // Number of stack frames, and the size of the allocated array
  85. unsigned int depth, size;
  86. } BN_STACK;
  87. static void BN_STACK_init(BN_STACK *);
  88. static void BN_STACK_finish(BN_STACK *);
  89. static int BN_STACK_push(BN_STACK *, unsigned int);
  90. static unsigned int BN_STACK_pop(BN_STACK *);
  91. // BN_CTX
  92. // The opaque BN_CTX type
  93. struct bignum_ctx {
  94. // The bignum bundles
  95. BN_POOL pool;
  96. // The "stack frames", if you will
  97. BN_STACK stack;
  98. // The number of bignums currently assigned
  99. unsigned int used;
  100. // Depth of stack overflow
  101. int err_stack;
  102. // Block "gets" until an "end" (compatibility behaviour)
  103. int too_many;
  104. };
  105. BN_CTX *BN_CTX_new(void) {
  106. BN_CTX *ret = OPENSSL_malloc(sizeof(BN_CTX));
  107. if (!ret) {
  108. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  109. return NULL;
  110. }
  111. // Initialise the structure
  112. BN_POOL_init(&ret->pool);
  113. BN_STACK_init(&ret->stack);
  114. ret->used = 0;
  115. ret->err_stack = 0;
  116. ret->too_many = 0;
  117. return ret;
  118. }
  119. void BN_CTX_free(BN_CTX *ctx) {
  120. if (ctx == NULL) {
  121. return;
  122. }
  123. BN_STACK_finish(&ctx->stack);
  124. BN_POOL_finish(&ctx->pool);
  125. OPENSSL_free(ctx);
  126. }
  127. void BN_CTX_start(BN_CTX *ctx) {
  128. // If we're already overflowing ...
  129. if (ctx->err_stack || ctx->too_many) {
  130. ctx->err_stack++;
  131. } else if (!BN_STACK_push(&ctx->stack, ctx->used)) {
  132. // (Try to) get a new frame pointer
  133. OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
  134. ctx->err_stack++;
  135. }
  136. }
  137. BIGNUM *BN_CTX_get(BN_CTX *ctx) {
  138. BIGNUM *ret;
  139. if (ctx->err_stack || ctx->too_many) {
  140. return NULL;
  141. }
  142. ret = BN_POOL_get(&ctx->pool);
  143. if (ret == NULL) {
  144. // Setting too_many prevents repeated "get" attempts from
  145. // cluttering the error stack.
  146. ctx->too_many = 1;
  147. OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
  148. return NULL;
  149. }
  150. // OK, make sure the returned bignum is "zero"
  151. BN_zero(ret);
  152. ctx->used++;
  153. return ret;
  154. }
  155. void BN_CTX_end(BN_CTX *ctx) {
  156. if (ctx->err_stack) {
  157. ctx->err_stack--;
  158. } else {
  159. unsigned int fp = BN_STACK_pop(&ctx->stack);
  160. // Does this stack frame have anything to release?
  161. if (fp < ctx->used) {
  162. BN_POOL_release(&ctx->pool, ctx->used - fp);
  163. }
  164. ctx->used = fp;
  165. // Unjam "too_many" in case "get" had failed
  166. ctx->too_many = 0;
  167. }
  168. }
  169. // BN_STACK
  170. static void BN_STACK_init(BN_STACK *st) {
  171. st->indexes = NULL;
  172. st->depth = st->size = 0;
  173. }
  174. static void BN_STACK_finish(BN_STACK *st) {
  175. OPENSSL_free(st->indexes);
  176. }
  177. static int BN_STACK_push(BN_STACK *st, unsigned int idx) {
  178. if (st->depth == st->size) {
  179. // Need to expand
  180. unsigned int newsize =
  181. (st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES);
  182. unsigned int *newitems = OPENSSL_malloc(newsize * sizeof(unsigned int));
  183. if (!newitems) {
  184. return 0;
  185. }
  186. if (st->depth) {
  187. OPENSSL_memcpy(newitems, st->indexes, st->depth * sizeof(unsigned int));
  188. }
  189. OPENSSL_free(st->indexes);
  190. st->indexes = newitems;
  191. st->size = newsize;
  192. }
  193. st->indexes[(st->depth)++] = idx;
  194. return 1;
  195. }
  196. static unsigned int BN_STACK_pop(BN_STACK *st) {
  197. return st->indexes[--(st->depth)];
  198. }
  199. static void BN_POOL_init(BN_POOL *p) {
  200. p->head = p->current = p->tail = NULL;
  201. p->used = p->size = 0;
  202. }
  203. static void BN_POOL_finish(BN_POOL *p) {
  204. while (p->head) {
  205. for (size_t i = 0; i < BN_CTX_POOL_SIZE; i++) {
  206. BN_clear_free(&p->head->vals[i]);
  207. }
  208. p->current = p->head->next;
  209. OPENSSL_free(p->head);
  210. p->head = p->current;
  211. }
  212. }
  213. static BIGNUM *BN_POOL_get(BN_POOL *p) {
  214. if (p->used == p->size) {
  215. BN_POOL_ITEM *item = OPENSSL_malloc(sizeof(BN_POOL_ITEM));
  216. if (!item) {
  217. return NULL;
  218. }
  219. // Initialise the structure
  220. for (size_t i = 0; i < BN_CTX_POOL_SIZE; i++) {
  221. BN_init(&item->vals[i]);
  222. }
  223. item->prev = p->tail;
  224. item->next = NULL;
  225. // Link it in
  226. if (!p->head) {
  227. p->head = p->current = p->tail = item;
  228. } else {
  229. p->tail->next = item;
  230. p->tail = item;
  231. p->current = item;
  232. }
  233. p->size += BN_CTX_POOL_SIZE;
  234. p->used++;
  235. // Return the first bignum from the new pool
  236. return item->vals;
  237. }
  238. if (!p->used) {
  239. p->current = p->head;
  240. } else if ((p->used % BN_CTX_POOL_SIZE) == 0) {
  241. p->current = p->current->next;
  242. }
  243. return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
  244. }
  245. static void BN_POOL_release(BN_POOL *p, unsigned int num) {
  246. unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
  247. p->used -= num;
  248. while (num--) {
  249. if (!offset) {
  250. offset = BN_CTX_POOL_SIZE - 1;
  251. p->current = p->current->prev;
  252. } else {
  253. offset--;
  254. }
  255. }
  256. }