Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

314 Zeilen
8.4 KiB

  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 <openssl/err.h>
  55. #include <openssl/mem.h>
  56. /* How many bignums are in each "pool item"; */
  57. #define BN_CTX_POOL_SIZE 16
  58. /* The stack frame info is resizing, set a first-time expansion size; */
  59. #define BN_CTX_START_FRAMES 32
  60. /* A bundle of bignums that can be linked with other bundles */
  61. typedef struct bignum_pool_item {
  62. /* The bignum values */
  63. BIGNUM vals[BN_CTX_POOL_SIZE];
  64. /* Linked-list admin */
  65. struct bignum_pool_item *prev, *next;
  66. } BN_POOL_ITEM;
  67. typedef struct bignum_pool {
  68. /* Linked-list admin */
  69. BN_POOL_ITEM *head, *current, *tail;
  70. /* Stack depth and allocation size */
  71. unsigned used, size;
  72. } BN_POOL;
  73. static void BN_POOL_init(BN_POOL *);
  74. static void BN_POOL_finish(BN_POOL *);
  75. static BIGNUM *BN_POOL_get(BN_POOL *);
  76. static void BN_POOL_release(BN_POOL *, unsigned int);
  77. /************/
  78. /* BN_STACK */
  79. /************/
  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. /**********/
  92. /* BN_CTX */
  93. /**********/
  94. /* The opaque BN_CTX type */
  95. struct bignum_ctx {
  96. /* The bignum bundles */
  97. BN_POOL pool;
  98. /* The "stack frames", if you will */
  99. BN_STACK stack;
  100. /* The number of bignums currently assigned */
  101. unsigned int used;
  102. /* Depth of stack overflow */
  103. int err_stack;
  104. /* Block "gets" until an "end" (compatibility behaviour) */
  105. int too_many;
  106. };
  107. BN_CTX *BN_CTX_new(void) {
  108. BN_CTX *ret = OPENSSL_malloc(sizeof(BN_CTX));
  109. if (!ret) {
  110. OPENSSL_PUT_ERROR(BN, BN_CTX_new, ERR_R_MALLOC_FAILURE);
  111. return NULL;
  112. }
  113. /* Initialise the structure */
  114. BN_POOL_init(&ret->pool);
  115. BN_STACK_init(&ret->stack);
  116. ret->used = 0;
  117. ret->err_stack = 0;
  118. ret->too_many = 0;
  119. return ret;
  120. }
  121. void BN_CTX_free(BN_CTX *ctx) {
  122. if (ctx == NULL) {
  123. return;
  124. }
  125. BN_STACK_finish(&ctx->stack);
  126. BN_POOL_finish(&ctx->pool);
  127. OPENSSL_free(ctx);
  128. }
  129. void BN_CTX_start(BN_CTX *ctx) {
  130. /* If we're already overflowing ... */
  131. if (ctx->err_stack || ctx->too_many) {
  132. ctx->err_stack++;
  133. } else if (!BN_STACK_push(&ctx->stack, ctx->used)) {
  134. /* (Try to) get a new frame pointer */
  135. OPENSSL_PUT_ERROR(BN, BN_CTX_start, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
  136. ctx->err_stack++;
  137. }
  138. }
  139. BIGNUM *BN_CTX_get(BN_CTX *ctx) {
  140. BIGNUM *ret;
  141. if (ctx->err_stack || ctx->too_many) {
  142. return NULL;
  143. }
  144. ret = BN_POOL_get(&ctx->pool);
  145. if (ret == NULL) {
  146. /* Setting too_many prevents repeated "get" attempts from
  147. * cluttering the error stack. */
  148. ctx->too_many = 1;
  149. OPENSSL_PUT_ERROR(BN, BN_CTX_get, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
  150. return NULL;
  151. }
  152. /* OK, make sure the returned bignum is "zero" */
  153. BN_zero(ret);
  154. ctx->used++;
  155. return ret;
  156. }
  157. void BN_CTX_end(BN_CTX *ctx) {
  158. if (ctx->err_stack) {
  159. ctx->err_stack--;
  160. } else {
  161. unsigned int fp = BN_STACK_pop(&ctx->stack);
  162. /* Does this stack frame have anything to release? */
  163. if (fp < ctx->used) {
  164. BN_POOL_release(&ctx->pool, ctx->used - fp);
  165. }
  166. ctx->used = fp;
  167. /* Unjam "too_many" in case "get" had failed */
  168. ctx->too_many = 0;
  169. }
  170. }
  171. /************/
  172. /* BN_STACK */
  173. /************/
  174. static void BN_STACK_init(BN_STACK *st) {
  175. st->indexes = NULL;
  176. st->depth = st->size = 0;
  177. }
  178. static void BN_STACK_finish(BN_STACK *st) {
  179. if (st->size)
  180. OPENSSL_free(st->indexes);
  181. }
  182. static int BN_STACK_push(BN_STACK *st, unsigned int idx) {
  183. if (st->depth == st->size)
  184. /* Need to expand */
  185. {
  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. memcpy(newitems, st->indexes, st->depth * sizeof(unsigned int));
  194. }
  195. if (st->size) {
  196. OPENSSL_free(st->indexes);
  197. }
  198. st->indexes = newitems;
  199. st->size = newsize;
  200. }
  201. st->indexes[(st->depth)++] = idx;
  202. return 1;
  203. }
  204. static unsigned int BN_STACK_pop(BN_STACK *st) {
  205. return st->indexes[--(st->depth)];
  206. }
  207. static void BN_POOL_init(BN_POOL *p) {
  208. p->head = p->current = p->tail = NULL;
  209. p->used = p->size = 0;
  210. }
  211. static void BN_POOL_finish(BN_POOL *p) {
  212. while (p->head) {
  213. unsigned int loop = 0;
  214. BIGNUM *bn = p->head->vals;
  215. while (loop++ < BN_CTX_POOL_SIZE) {
  216. if (bn->d) {
  217. BN_clear_free(bn);
  218. }
  219. bn++;
  220. }
  221. p->current = p->head->next;
  222. OPENSSL_free(p->head);
  223. p->head = p->current;
  224. }
  225. }
  226. static BIGNUM *BN_POOL_get(BN_POOL *p) {
  227. if (p->used == p->size) {
  228. BIGNUM *bn;
  229. unsigned int loop = 0;
  230. BN_POOL_ITEM *item = OPENSSL_malloc(sizeof(BN_POOL_ITEM));
  231. if (!item) {
  232. return NULL;
  233. }
  234. /* Initialise the structure */
  235. bn = item->vals;
  236. while (loop++ < BN_CTX_POOL_SIZE) {
  237. BN_init(bn++);
  238. }
  239. item->prev = p->tail;
  240. item->next = NULL;
  241. /* Link it in */
  242. if (!p->head) {
  243. p->head = p->current = p->tail = item;
  244. } else {
  245. p->tail->next = item;
  246. p->tail = item;
  247. p->current = item;
  248. }
  249. p->size += BN_CTX_POOL_SIZE;
  250. p->used++;
  251. /* Return the first bignum from the new pool */
  252. return item->vals;
  253. }
  254. if (!p->used) {
  255. p->current = p->head;
  256. } else if ((p->used % BN_CTX_POOL_SIZE) == 0) {
  257. p->current = p->current->next;
  258. }
  259. return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
  260. }
  261. static void BN_POOL_release(BN_POOL *p, unsigned int num) {
  262. unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
  263. p->used -= num;
  264. while (num--) {
  265. if (!offset) {
  266. offset = BN_CTX_POOL_SIZE - 1;
  267. p->current = p->current->prev;
  268. } else {
  269. offset--;
  270. }
  271. }
  272. }