1*3860a9d1Sotto /* $OpenBSD: mem.c,v 1.10 2017/12/17 08:21:10 otto Exp $ */
2a6ce4a44Sotto
3a6ce4a44Sotto /*
4a6ce4a44Sotto * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
5a6ce4a44Sotto *
6a6ce4a44Sotto * Permission to use, copy, modify, and distribute this software for any
7a6ce4a44Sotto * purpose with or without fee is hereby granted, provided that the above
8a6ce4a44Sotto * copyright notice and this permission notice appear in all copies.
9a6ce4a44Sotto *
10a6ce4a44Sotto * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11a6ce4a44Sotto * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12a6ce4a44Sotto * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13a6ce4a44Sotto * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14a6ce4a44Sotto * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15a6ce4a44Sotto * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16a6ce4a44Sotto * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17a6ce4a44Sotto */
18a6ce4a44Sotto
198e25e9fcSjca #include <openssl/err.h>
20a6ce4a44Sotto
21a6ce4a44Sotto #include <err.h>
22a6ce4a44Sotto #include <stdlib.h>
23a6ce4a44Sotto #include <string.h>
24a6ce4a44Sotto
25a6ce4a44Sotto #include "extern.h"
26a6ce4a44Sotto
27a6ce4a44Sotto struct number *
new_number(void)28a6ce4a44Sotto new_number(void)
29a6ce4a44Sotto {
30a6ce4a44Sotto struct number *n;
31a6ce4a44Sotto
32a6ce4a44Sotto n = bmalloc(sizeof(*n));
33a6ce4a44Sotto n->scale = 0;
34a6ce4a44Sotto n->number = BN_new();
354ac08767Sotto bn_checkp(n->number);
36a6ce4a44Sotto return n;
37a6ce4a44Sotto }
38a6ce4a44Sotto
39a6ce4a44Sotto void
free_number(struct number * n)40a6ce4a44Sotto free_number(struct number *n)
41a6ce4a44Sotto {
42a6ce4a44Sotto BN_free(n->number);
43a6ce4a44Sotto free(n);
44a6ce4a44Sotto }
45a6ce4a44Sotto
46a6ce4a44Sotto struct number *
dup_number(const struct number * a)47a6ce4a44Sotto dup_number(const struct number *a)
48a6ce4a44Sotto {
49a6ce4a44Sotto struct number *n;
50a6ce4a44Sotto
51a6ce4a44Sotto n = bmalloc(sizeof(*n));
52a6ce4a44Sotto n->scale = a->scale;
53a6ce4a44Sotto n->number = BN_dup(a->number);
54a6ce4a44Sotto bn_checkp(n->number);
55a6ce4a44Sotto return n;
56a6ce4a44Sotto }
57a6ce4a44Sotto
58a6ce4a44Sotto void *
bmalloc(size_t sz)59a6ce4a44Sotto bmalloc(size_t sz)
60a6ce4a44Sotto {
61a6ce4a44Sotto void *p;
62a6ce4a44Sotto
63a6ce4a44Sotto p = malloc(sz);
64a6ce4a44Sotto if (p == NULL)
65abdc5753Sotto err(1, NULL);
66a6ce4a44Sotto return p;
67a6ce4a44Sotto }
68a6ce4a44Sotto
69a6ce4a44Sotto void *
breallocarray(void * p,size_t nmemb,size_t size)705805b5e9Sderaadt breallocarray(void *p, size_t nmemb, size_t size)
71a6ce4a44Sotto {
72a6ce4a44Sotto void *q;
73a6ce4a44Sotto
745805b5e9Sderaadt q = reallocarray(p, nmemb, size);
75c699c217Sotto if (q == NULL)
76abdc5753Sotto err(1, NULL);
77a6ce4a44Sotto return q;
78a6ce4a44Sotto }
79a6ce4a44Sotto
80a6ce4a44Sotto char *
bstrdup(const char * p)81a6ce4a44Sotto bstrdup(const char *p)
82a6ce4a44Sotto {
83a6ce4a44Sotto char *q;
84a6ce4a44Sotto
85a6ce4a44Sotto q = strdup(p);
86a6ce4a44Sotto if (q == NULL)
87abdc5753Sotto err(1, NULL);
88a6ce4a44Sotto return q;
89a6ce4a44Sotto }
90a6ce4a44Sotto
91a6ce4a44Sotto void
bn_check(int x)929e55bacaSotto bn_check(int x)
93a6ce4a44Sotto {
94*3860a9d1Sotto if (x == 0) {
95*3860a9d1Sotto ERR_load_BN_strings();
96*3860a9d1Sotto errx(1, "BN failure: %s",
97*3860a9d1Sotto ERR_reason_error_string(ERR_get_error()));
98*3860a9d1Sotto }
99a6ce4a44Sotto }
100a6ce4a44Sotto
101a6ce4a44Sotto void
bn_checkp(const void * p)1029e55bacaSotto bn_checkp(const void *p)
103a6ce4a44Sotto {
104*3860a9d1Sotto if (p == NULL) {
105*3860a9d1Sotto ERR_load_BN_strings();
106*3860a9d1Sotto errx(1, "BN failure: %s",
107*3860a9d1Sotto ERR_reason_error_string(ERR_get_error()));
108*3860a9d1Sotto }
109a6ce4a44Sotto }
110