asn1/a_int.c: remove code duplicate and optimize branches,
[openssl.git] / crypto / asn1 / x_long.c
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
14 #if !(OPENSSL_API_COMPAT < 0x10200000L)
15 NON_EMPTY_TRANSLATION_UNIT
16 #else
17 /*
18  * Custom primitive type for long handling. This converts between an
19  * ASN1_INTEGER and a long directly.
20  */
21
22 static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
23 static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
24
25 static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
26                     const ASN1_ITEM *it);
27 static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
28                     int utype, char *free_cont, const ASN1_ITEM *it);
29 static int long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
30                       int indent, const ASN1_PCTX *pctx);
31
32 static ASN1_PRIMITIVE_FUNCS long_pf = {
33     NULL, 0,
34     long_new,
35     long_free,
36     long_free,                  /* Clear should set to initial value */
37     long_c2i,
38     long_i2c,
39     long_print
40 };
41
42 ASN1_ITEM_start(LONG)
43         ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, ASN1_LONG_UNDEF, "LONG"
44 ASN1_ITEM_end(LONG)
45
46 ASN1_ITEM_start(ZLONG)
47         ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, 0, "ZLONG"
48 ASN1_ITEM_end(ZLONG)
49
50 static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
51 {
52     *(long *)pval = it->size;
53     return 1;
54 }
55
56 static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
57 {
58     *(long *)pval = it->size;
59 }
60
61 /*
62  * Originally BN_num_bits_word was called to perform this operation, but
63  * trouble is that there is no guarantee that sizeof(long) equals to
64  * sizeof(BN_ULONG). BN_ULONG is a configurable type that can be as wide
65  * as long, but also double or half...
66  */
67 static int num_bits_ulong(unsigned long value)
68 {
69     size_t i;
70     unsigned long ret = 0;
71
72     /*
73      * It is argued that *on average* constant counter loop performs
74      * not worse [if not better] than one with conditional break or
75      * mask-n-table-lookup-style, because of branch misprediction
76      * penalties.
77      */
78     for (i = 0; i < sizeof(value) * 8; i++) {
79         ret += (value != 0);
80         value >>= 1;
81     }
82
83     return (int)ret;
84 }
85
86 static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
87                     const ASN1_ITEM *it)
88 {
89     long ltmp;
90     unsigned long utmp, sign;
91     int clen, pad, i;
92     /* this exists to bypass broken gcc optimization */
93     char *cp = (char *)pval;
94
95     /* use memcpy, because we may not be long aligned */
96     memcpy(&ltmp, cp, sizeof(long));
97
98     if (ltmp == it->size)
99         return -1;
100     /*
101      * Convert the long to positive: we subtract one if negative so we can
102      * cleanly handle the padding if only the MSB of the leading octet is
103      * set.
104      */
105     if (ltmp < 0) {
106         sign = 0xff;
107         utmp = 0 - (unsigned long)ltmp - 1;
108     } else {
109         sign = 0;
110         utmp = ltmp;
111     }
112     clen = num_bits_ulong(utmp);
113     /* If MSB of leading octet set we need to pad */
114     if (!(clen & 0x7))
115         pad = 1;
116     else
117         pad = 0;
118
119     /* Convert number of bits to number of octets */
120     clen = (clen + 7) >> 3;
121
122     if (cont != NULL) {
123         if (pad)
124             *cont++ = (unsigned char)sign;
125         for (i = clen - 1; i >= 0; i--) {
126             cont[i] = (unsigned char)(utmp ^ sign);
127             utmp >>= 8;
128         }
129     }
130     return clen + pad;
131 }
132
133 static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
134                     int utype, char *free_cont, const ASN1_ITEM *it)
135 {
136     int i;
137     long ltmp;
138     unsigned long utmp = 0, sign = 0x100;
139     char *cp = (char *)pval;
140
141     if (len > 1) {
142         /*
143          * Check possible pad byte.  Worst case, we're skipping past actual
144          * content, but since that's only with 0x00 and 0xff and we set neg
145          * accordingly, the result will be correct in the end anyway.
146          */
147         switch (cont[0]) {
148         case 0xff:
149             cont++;
150             len--;
151             sign = 0xff;
152             break;
153         case 0:
154             cont++;
155             len--;
156             sign = 0;
157             break;
158         }
159     }
160     if (len > (int)sizeof(long)) {
161         ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
162         return 0;
163     }
164
165     if (sign == 0x100) {
166         /* Is it negative? */
167         if (len && (cont[0] & 0x80))
168             sign = 0xff;
169         else
170             sign = 0;
171     } else if (((sign ^ cont[0]) & 0x80) == 0) { /* same sign bit? */
172         ASN1err(ASN1_F_LONG_C2I, ASN1_R_ILLEGAL_PADDING);
173         return 0;
174     }
175     utmp = 0;
176     for (i = 0; i < len; i++) {
177         utmp <<= 8;
178         utmp |= cont[i] ^ sign;
179     }
180     ltmp = (long)utmp;
181     if (ltmp < 0) {
182         ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
183         return 0;
184     }
185     if (sign)
186         ltmp = -ltmp - 1;
187     if (ltmp == it->size) {
188         ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
189         return 0;
190     }
191     memcpy(cp, &ltmp, sizeof(long));
192     return 1;
193 }
194
195 static int long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
196                       int indent, const ASN1_PCTX *pctx)
197 {
198     return BIO_printf(out, "%ld\n", *(long *)pval);
199 }
200 #endif