Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / lib / gnutls_num.h
1 /*
2  * Copyright (C) 2000-2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
8  * The GnuTLS is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>
20  *
21  */
22
23 #ifndef GNUTLS_NUM_H
24 #define GNUTLS_NUM_H
25
26 #include <gnutls_int.h>
27
28 #include <minmax.h>
29 #include <byteswap.h>
30
31 int _gnutls_uint64pp(uint64 *);
32 int _gnutls_uint48pp(uint64 *);
33
34 #define UINT64DATA(x) ((x).i)
35
36 inline static uint32_t _gnutls_uint24touint32(uint24 num)
37 {
38         uint32_t ret = 0;
39
40         ((uint8_t *) & ret)[1] = num.pint[0];
41         ((uint8_t *) & ret)[2] = num.pint[1];
42         ((uint8_t *) & ret)[3] = num.pint[2];
43         return ret;
44 }
45
46 inline static uint24 _gnutls_uint32touint24(uint32_t num)
47 {
48         uint24 ret;
49
50         ret.pint[0] = ((uint8_t *) & num)[1];
51         ret.pint[1] = ((uint8_t *) & num)[2];
52         ret.pint[2] = ((uint8_t *) & num)[3];
53         return ret;
54
55 }
56
57 /* data should be at least 3 bytes */
58 inline static uint32_t _gnutls_read_uint24(const uint8_t * data)
59 {
60         uint32_t res;
61         uint24 num;
62
63         num.pint[0] = data[0];
64         num.pint[1] = data[1];
65         num.pint[2] = data[2];
66
67         res = _gnutls_uint24touint32(num);
68 #ifndef WORDS_BIGENDIAN
69         res = bswap_32(res);
70 #endif
71         return res;
72 }
73
74 inline static void _gnutls_write_uint64(uint64_t num, uint8_t * data)
75 {
76 #ifndef WORDS_BIGENDIAN
77         num = bswap_64(num);
78 #endif
79         memcpy(data, &num, 8);
80 }
81
82 inline static void _gnutls_write_uint24(uint32_t num, uint8_t * data)
83 {
84         uint24 tmp;
85
86 #ifndef WORDS_BIGENDIAN
87         num = bswap_32(num);
88 #endif
89         tmp = _gnutls_uint32touint24(num);
90
91         data[0] = tmp.pint[0];
92         data[1] = tmp.pint[1];
93         data[2] = tmp.pint[2];
94 }
95
96 inline static uint32_t _gnutls_read_uint32(const uint8_t * data)
97 {
98         uint32_t res;
99
100         memcpy(&res, data, sizeof(uint32_t));
101 #ifndef WORDS_BIGENDIAN
102         res = bswap_32(res);
103 #endif
104         return res;
105 }
106
107 inline static void _gnutls_write_uint32(uint32_t num, uint8_t * data)
108 {
109
110 #ifndef WORDS_BIGENDIAN
111         num = bswap_32(num);
112 #endif
113         memcpy(data, &num, sizeof(uint32_t));
114 }
115
116 inline static uint16_t _gnutls_read_uint16(const uint8_t * data)
117 {
118         uint16_t res;
119         memcpy(&res, data, sizeof(uint16_t));
120 #ifndef WORDS_BIGENDIAN
121         res = bswap_16(res);
122 #endif
123         return res;
124 }
125
126 inline static void _gnutls_write_uint16(uint16_t num, uint8_t * data)
127 {
128
129 #ifndef WORDS_BIGENDIAN
130         num = bswap_16(num);
131 #endif
132         memcpy(data, &num, sizeof(uint16_t));
133 }
134
135 inline static uint32_t _gnutls_conv_uint32(uint32_t data)
136 {
137 #ifndef WORDS_BIGENDIAN
138         return bswap_32(data);
139 #else
140         return data;
141 #endif
142 }
143
144 inline static uint16_t _gnutls_conv_uint16(uint16_t data)
145 {
146 #ifndef WORDS_BIGENDIAN
147         return bswap_16(data);
148 #else
149         return data;
150 #endif
151 }
152
153 inline static uint32_t _gnutls_uint64touint32(const uint64 * num)
154 {
155         uint32_t ret;
156
157         memcpy(&ret, &num->i[4], 4);
158 #ifndef WORDS_BIGENDIAN
159         ret = bswap_32(ret);
160 #endif
161
162         return ret;
163 }
164
165 #endif                          /* GNUTLS_NUM_H */