OpenCoverage

x_bignum.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/asn1/x_bignum.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/*-
2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.-
3 *-
4 * Licensed under the OpenSSL license (the "License"). You may not use-
5 * this file except in compliance with the License. You can obtain a copy-
6 * in the file LICENSE in the source distribution or at-
7 * https://www.openssl.org/source/license.html-
8 */-
9-
10#include <stdio.h>-
11#include "internal/cryptlib.h"-
12#include <openssl/asn1t.h>-
13#include <openssl/bn.h>-
14-
15/*-
16 * Custom primitive type for BIGNUM handling. This reads in an ASN1_INTEGER-
17 * as a BIGNUM directly. Currently it ignores the sign which isn't a problem-
18 * since all BIGNUMs used are non negative and anything that looks negative-
19 * is normally due to an encoding error.-
20 */-
21-
22#define BN_SENSITIVE 1-
23-
24static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it);-
25static int bn_secure_new(ASN1_VALUE **pval, const ASN1_ITEM *it);-
26static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it);-
27-
28static int bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,-
29 const ASN1_ITEM *it);-
30static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,-
31 int utype, char *free_cont, const ASN1_ITEM *it);-
32static int bn_secure_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,-
33 int utype, char *free_cont, const ASN1_ITEM *it);-
34static int bn_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,-
35 int indent, const ASN1_PCTX *pctx);-
36-
37static ASN1_PRIMITIVE_FUNCS bignum_pf = {-
38 NULL, 0,-
39 bn_new,-
40 bn_free,-
41 0,-
42 bn_c2i,-
43 bn_i2c,-
44 bn_print-
45};-
46-
47static ASN1_PRIMITIVE_FUNCS cbignum_pf = {-
48 NULL, 0,-
49 bn_secure_new,-
50 bn_free,-
51 0,-
52 bn_secure_c2i,-
53 bn_i2c,-
54 bn_print-
55};-
56-
57ASN1_ITEM_start(BIGNUM)-
58 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &bignum_pf, 0, "BIGNUM"-
59ASN1_ITEM_end(BIGNUM)-
60-
61ASN1_ITEM_start(CBIGNUM)-
62 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &cbignum_pf, BN_SENSITIVE, "CBIGNUM"-
63ASN1_ITEM_end(CBIGNUM)-
64-
65static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it)-
66{-
67 *pval = (ASN1_VALUE *)BN_new();-
68 if (*pval != NULL)
*pval != ((void *)0)Description
TRUEevaluated 71413 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEnever evaluated
0-71413
69 return 1;
executed 71413 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
71413
70 else-
71 return 0;
never executed: return 0;
0
72}-
73-
74static int bn_secure_new(ASN1_VALUE **pval, const ASN1_ITEM *it)-
75{-
76 *pval = (ASN1_VALUE *)BN_secure_new();-
77 if (*pval != NULL)
*pval != ((void *)0)Description
TRUEevaluated 67853 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-67853
78 return 1;
executed 67853 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
67853
79 else-
80 return 0;
never executed: return 0;
0
81}-
82-
83static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it)-
84{-
85 if (!*pval)
!*pvalDescription
TRUEevaluated 12507 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 26575 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
12507-26575
86 return;
executed 12507 times by 1 test: return;
Executed by:
  • libcrypto.so.1.1
12507
87 if (it->size & BN_SENSITIVE)
it->size & 1Description
TRUEevaluated 11936 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 14639 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
11936-14639
88 BN_clear_free((BIGNUM *)*pval);
executed 11936 times by 1 test: BN_clear_free((BIGNUM *)*pval);
Executed by:
  • libcrypto.so.1.1
11936
89 else-
90 BN_free((BIGNUM *)*pval);
executed 14639 times by 2 tests: BN_free((BIGNUM *)*pval);
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
14639
91 *pval = NULL;-
92}
executed 26575 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
26575
93-
94static int bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,-
95 const ASN1_ITEM *it)-
96{-
97 BIGNUM *bn;-
98 int pad;-
99 if (!*pval)
!*pvalDescription
TRUEevaluated 78 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 157537 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
78-157537
100 return -1;
executed 78 times by 1 test: return -1;
Executed by:
  • libcrypto.so.1.1
78
101 bn = (BIGNUM *)*pval;-
102 /* If MSB set in an octet we need a padding byte */-
103 if (BN_num_bits(bn) & 0x7)
BN_num_bits(bn) & 0x7Description
TRUEevaluated 113499 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 44038 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
44038-113499
104 pad = 0;
executed 113499 times by 2 tests: pad = 0;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
113499
105 else-
106 pad = 1;
executed 44038 times by 1 test: pad = 1;
Executed by:
  • libcrypto.so.1.1
44038
107 if (cont) {
contDescription
TRUEevaluated 29283 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 128254 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
29283-128254
108 if (pad)
padDescription
TRUEevaluated 8515 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 20768 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
8515-20768
109 *cont++ = 0;
executed 8515 times by 1 test: *cont++ = 0;
Executed by:
  • libcrypto.so.1.1
8515
110 BN_bn2bin(bn, cont);-
111 }
executed 29283 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
29283
112 return pad + BN_num_bytes(bn);
executed 157537 times by 2 tests: return pad + ((BN_num_bits(bn)+7)/8);
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
157537
113}-
114-
115static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,-
116 int utype, char *free_cont, const ASN1_ITEM *it)-
117{-
118 BIGNUM *bn;-
119-
120 if (*pval == NULL && !bn_new(pval, it))
*pval == ((void *)0)Description
TRUEevaluated 55930 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 64101 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
!bn_new(pval, it)Description
TRUEnever evaluated
FALSEevaluated 55930 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-64101
121 return 0;
never executed: return 0;
0
122 bn = (BIGNUM *)*pval;-
123 if (!BN_bin2bn(cont, len, bn)) {
!BN_bin2bn(cont, len, bn)Description
TRUEnever evaluated
FALSEevaluated 120031 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-120031
124 bn_free(pval, it);-
125 return 0;
never executed: return 0;
0
126 }-
127 return 1;
executed 120031 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
120031
128}-
129-
130static int bn_secure_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,-
131 int utype, char *free_cont, const ASN1_ITEM *it)-
132{-
133 if (!*pval)
!*pvalDescription
TRUEevaluated 29853 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 28997 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
28997-29853
134 bn_secure_new(pval, it);
executed 29853 times by 1 test: bn_secure_new(pval, it);
Executed by:
  • libcrypto.so.1.1
29853
135 return bn_c2i(pval, cont, len, utype, free_cont, it);
executed 58850 times by 1 test: return bn_c2i(pval, cont, len, utype, free_cont, it);
Executed by:
  • libcrypto.so.1.1
58850
136}-
137-
138static int bn_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,-
139 int indent, const ASN1_PCTX *pctx)-
140{-
141 if (!BN_print(out, *(BIGNUM **)pval))
!BN_print(out,...IGNUM **)pval)Description
TRUEnever evaluated
FALSEevaluated 11205 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-11205
142 return 0;
never executed: return 0;
0
143 if (BIO_puts(out, "\n") <= 0)
BIO_puts(out, "\n") <= 0Description
TRUEnever evaluated
FALSEevaluated 11205 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-11205
144 return 0;
never executed: return 0;
0
145 return 1;
executed 11205 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
11205
146}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2