Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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