1 /* $OpenBSD: bn_to_string.c,v 1.3 2023/02/13 04:26:32 jsing Exp $ */ 2 /* 3 * Copyright (c) 2019 Theo Buehler <tb@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <err.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <string.h> 22 23 #include <openssl/bn.h> 24 25 char *bn_to_string(const BIGNUM *bn); 26 27 struct convert_st { 28 const char *input; 29 const char *expected; 30 }; 31 32 struct convert_st testcases[] = { 33 {"0", "0"}, 34 {"-0", "0"}, 35 {"7", "7"}, 36 {"-7", "-7"}, 37 {"8", "8"}, 38 {"-8", "-8"}, 39 {"F", "15"}, 40 {"-F", "-15"}, 41 {"10", "16"}, 42 {"-10", "-16"}, 43 {"7F", "127"}, 44 {"-7F", "-127"}, 45 {"80", "128"}, 46 {"-80", "-128"}, 47 {"FF", "255"}, 48 {"-FF", "-255"}, 49 {"100", "256"}, 50 {"7FFF", "32767"}, 51 {"-7FFF", "-32767"}, 52 {"8000", "32768"}, 53 {"-8000", "-32768"}, 54 {"FFFF", "65535"}, 55 {"-FFFF", "-65535"}, 56 {"10000", "65536"}, 57 {"-10000", "-65536"}, 58 {"7FFFFFFF", "2147483647"}, 59 {"-7FFFFFFF", "-2147483647"}, 60 {"80000000", "2147483648"}, 61 {"-80000000", "-2147483648"}, 62 {"FFFFFFFF", "4294967295"}, 63 {"-FFFFFFFF", "-4294967295"}, 64 {"100000000", "4294967296"}, 65 {"-100000000", "-4294967296"}, 66 {"7FFFFFFFFFFFFFFF", "9223372036854775807"}, 67 {"-7FFFFFFFFFFFFFFF", "-9223372036854775807"}, 68 {"8000000000000000", "9223372036854775808"}, 69 {"-8000000000000000", "-9223372036854775808"}, 70 {"FFFFFFFFFFFFFFFF", "18446744073709551615"}, 71 {"-FFFFFFFFFFFFFFFF", "-18446744073709551615"}, 72 {"10000000000000000", "18446744073709551616"}, 73 {"-10000000000000000", "-18446744073709551616"}, 74 {"7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 75 "170141183460469231731687303715884105727"}, 76 {"-7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 77 "-170141183460469231731687303715884105727"}, 78 {"80000000000000000000000000000000", 79 "0x80000000000000000000000000000000"}, 80 {"-80000000000000000000000000000000", 81 "-0x80000000000000000000000000000000"}, 82 {"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 83 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"}, 84 {"-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 85 "-0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"}, 86 {"100000000000000000000000000000000", 87 "0x0100000000000000000000000000000000"}, 88 {"-100000000000000000000000000000000", 89 "-0x0100000000000000000000000000000000"}, 90 { NULL, NULL }, 91 }; 92 93 int 94 main(int argc, char *argv[]) 95 { 96 struct convert_st *test; 97 BIGNUM *bn = NULL; 98 char *bnstr; 99 int failed = 0; 100 101 for (test = testcases; test->input != NULL; test++) { 102 if (!BN_hex2bn(&bn, test->input)) 103 errx(1, "BN_hex2bn(%s)", test->input); 104 if ((bnstr = bn_to_string(bn)) == NULL) 105 errx(1, "bn_to_string(%s)", test->input); 106 if (strcmp(bnstr, test->expected) != 0) { 107 warnx("%s != %s", bnstr, test->expected); 108 failed = 1; 109 } 110 free(bnstr); 111 } 112 113 BN_free(bn); 114 115 return failed; 116 } 117