1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * lib/hexdump.c
4 */
5
6 #include <linux/types.h>
7 #include <linux/ctype.h>
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/export.h>
11 #include <asm/unaligned.h>
12
13 const char hex_asc[] = "0123456789abcdef";
14 EXPORT_SYMBOL(hex_asc);
15 const char hex_asc_upper[] = "0123456789ABCDEF";
16 EXPORT_SYMBOL(hex_asc_upper);
17
18 /**
19 * hex_to_bin - convert a hex digit to its real value
20 * @ch: ascii character represents hex digit
21 *
22 * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
23 * input.
24 *
25 * This function is used to load cryptographic keys, so it is coded in such a
26 * way that there are no conditions or memory accesses that depend on data.
27 *
28 * Explanation of the logic:
29 * (ch - '9' - 1) is negative if ch <= '9'
30 * ('0' - 1 - ch) is negative if ch >= '0'
31 * we "and" these two values, so the result is negative if ch is in the range
32 * '0' ... '9'
33 * we are only interested in the sign, so we do a shift ">> 8"; note that right
34 * shift of a negative value is implementation-defined, so we cast the
35 * value to (unsigned) before the shift --- we have 0xffffff if ch is in
36 * the range '0' ... '9', 0 otherwise
37 * we "and" this value with (ch - '0' + 1) --- we have a value 1 ... 10 if ch is
38 * in the range '0' ... '9', 0 otherwise
39 * we add this value to -1 --- we have a value 0 ... 9 if ch is in the range '0'
40 * ... '9', -1 otherwise
41 * the next line is similar to the previous one, but we need to decode both
42 * uppercase and lowercase letters, so we use (ch & 0xdf), which converts
43 * lowercase to uppercase
44 */
45 /*
46 * perserve abi due to 15b78a8e38e8 ("hex2bin: make the function hex_to_bin
47 * constant-time"
48 */
49 #ifdef __GENKSYMS__
hex_to_bin(char ch)50 int hex_to_bin(char ch)
51 #else
52 int hex_to_bin(unsigned char ch)
53 #endif
54 {
55 unsigned char cu = ch & 0xdf;
56 return -1 +
57 ((ch - '0' + 1) & (unsigned)((ch - '9' - 1) & ('0' - 1 - ch)) >> 8) +
58 ((cu - 'A' + 11) & (unsigned)((cu - 'F' - 1) & ('A' - 1 - cu)) >> 8);
59 }
60 EXPORT_SYMBOL(hex_to_bin);
61
62 /**
63 * hex2bin - convert an ascii hexadecimal string to its binary representation
64 * @dst: binary result
65 * @src: ascii hexadecimal string
66 * @count: result length
67 *
68 * Return 0 on success, -EINVAL in case of bad input.
69 */
hex2bin(u8 * dst,const char * src,size_t count)70 int hex2bin(u8 *dst, const char *src, size_t count)
71 {
72 while (count--) {
73 int hi, lo;
74
75 hi = hex_to_bin(*src++);
76 if (unlikely(hi < 0))
77 return -EINVAL;
78 lo = hex_to_bin(*src++);
79 if (unlikely(lo < 0))
80 return -EINVAL;
81
82 *dst++ = (hi << 4) | lo;
83 }
84 return 0;
85 }
86 EXPORT_SYMBOL(hex2bin);
87
88 /**
89 * bin2hex - convert binary data to an ascii hexadecimal string
90 * @dst: ascii hexadecimal result
91 * @src: binary data
92 * @count: binary data length
93 */
bin2hex(char * dst,const void * src,size_t count)94 char *bin2hex(char *dst, const void *src, size_t count)
95 {
96 const unsigned char *_src = src;
97
98 while (count--)
99 dst = hex_byte_pack(dst, *_src++);
100 return dst;
101 }
102 EXPORT_SYMBOL(bin2hex);
103
104 /**
105 * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
106 * @buf: data blob to dump
107 * @len: number of bytes in the @buf
108 * @rowsize: number of bytes to print per line; must be 16 or 32
109 * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
110 * @linebuf: where to put the converted data
111 * @linebuflen: total size of @linebuf, including space for terminating NUL
112 * @ascii: include ASCII after the hex output
113 *
114 * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
115 * 16 or 32 bytes of input data converted to hex + ASCII output.
116 *
117 * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
118 * to a hex + ASCII dump at the supplied memory location.
119 * The converted output is always NUL-terminated.
120 *
121 * E.g.:
122 * hex_dump_to_buffer(frame->data, frame->len, 16, 1,
123 * linebuf, sizeof(linebuf), true);
124 *
125 * example output buffer:
126 * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
127 *
128 * Return:
129 * The amount of bytes placed in the buffer without terminating NUL. If the
130 * output was truncated, then the return value is the number of bytes
131 * (excluding the terminating NUL) which would have been written to the final
132 * string if enough space had been available.
133 */
hex_dump_to_buffer(const void * buf,size_t len,int rowsize,int groupsize,char * linebuf,size_t linebuflen,bool ascii)134 int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
135 char *linebuf, size_t linebuflen, bool ascii)
136 {
137 const u8 *ptr = buf;
138 int ngroups;
139 u8 ch;
140 int j, lx = 0;
141 int ascii_column;
142 int ret;
143
144 if (rowsize != 16 && rowsize != 32)
145 rowsize = 16;
146
147 if (len > rowsize) /* limit to one line at a time */
148 len = rowsize;
149 if (!is_power_of_2(groupsize) || groupsize > 8)
150 groupsize = 1;
151 if ((len % groupsize) != 0) /* no mixed size output */
152 groupsize = 1;
153
154 ngroups = len / groupsize;
155 ascii_column = rowsize * 2 + rowsize / groupsize + 1;
156
157 if (!linebuflen)
158 goto overflow1;
159
160 if (!len)
161 goto nil;
162
163 if (groupsize == 8) {
164 const u64 *ptr8 = buf;
165
166 for (j = 0; j < ngroups; j++) {
167 ret = snprintf(linebuf + lx, linebuflen - lx,
168 "%s%16.16llx", j ? " " : "",
169 get_unaligned(ptr8 + j));
170 if (ret >= linebuflen - lx)
171 goto overflow1;
172 lx += ret;
173 }
174 } else if (groupsize == 4) {
175 const u32 *ptr4 = buf;
176
177 for (j = 0; j < ngroups; j++) {
178 ret = snprintf(linebuf + lx, linebuflen - lx,
179 "%s%8.8x", j ? " " : "",
180 get_unaligned(ptr4 + j));
181 if (ret >= linebuflen - lx)
182 goto overflow1;
183 lx += ret;
184 }
185 } else if (groupsize == 2) {
186 const u16 *ptr2 = buf;
187
188 for (j = 0; j < ngroups; j++) {
189 ret = snprintf(linebuf + lx, linebuflen - lx,
190 "%s%4.4x", j ? " " : "",
191 get_unaligned(ptr2 + j));
192 if (ret >= linebuflen - lx)
193 goto overflow1;
194 lx += ret;
195 }
196 } else {
197 for (j = 0; j < len; j++) {
198 if (linebuflen < lx + 2)
199 goto overflow2;
200 ch = ptr[j];
201 linebuf[lx++] = hex_asc_hi(ch);
202 if (linebuflen < lx + 2)
203 goto overflow2;
204 linebuf[lx++] = hex_asc_lo(ch);
205 if (linebuflen < lx + 2)
206 goto overflow2;
207 linebuf[lx++] = ' ';
208 }
209 if (j)
210 lx--;
211 }
212 if (!ascii)
213 goto nil;
214
215 while (lx < ascii_column) {
216 if (linebuflen < lx + 2)
217 goto overflow2;
218 linebuf[lx++] = ' ';
219 }
220 for (j = 0; j < len; j++) {
221 if (linebuflen < lx + 2)
222 goto overflow2;
223 ch = ptr[j];
224 linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
225 }
226 nil:
227 linebuf[lx] = '\0';
228 return lx;
229 overflow2:
230 linebuf[lx++] = '\0';
231 overflow1:
232 return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
233 }
234 EXPORT_SYMBOL(hex_dump_to_buffer);
235
236 #ifdef CONFIG_PRINTK
237 /**
238 * print_hex_dump - print a text hex dump to syslog for a binary blob of data
239 * @level: kernel log level (e.g. KERN_DEBUG)
240 * @prefix_str: string to prefix each line with;
241 * caller supplies trailing spaces for alignment if desired
242 * @prefix_type: controls whether prefix of an offset, address, or none
243 * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
244 * @rowsize: number of bytes to print per line; must be 16 or 32
245 * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
246 * @buf: data blob to dump
247 * @len: number of bytes in the @buf
248 * @ascii: include ASCII after the hex output
249 *
250 * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
251 * to the kernel log at the specified kernel log level, with an optional
252 * leading prefix.
253 *
254 * print_hex_dump() works on one "line" of output at a time, i.e.,
255 * 16 or 32 bytes of input data converted to hex + ASCII output.
256 * print_hex_dump() iterates over the entire input @buf, breaking it into
257 * "line size" chunks to format and print.
258 *
259 * E.g.:
260 * print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
261 * 16, 1, frame->data, frame->len, true);
262 *
263 * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
264 * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
265 * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
266 * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c pqrstuvwxyz{|}~.
267 */
print_hex_dump(const char * level,const char * prefix_str,int prefix_type,int rowsize,int groupsize,const void * buf,size_t len,bool ascii)268 void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
269 int rowsize, int groupsize,
270 const void *buf, size_t len, bool ascii)
271 {
272 const u8 *ptr = buf;
273 int i, linelen, remaining = len;
274 unsigned char linebuf[32 * 3 + 2 + 32 + 1];
275
276 if (rowsize != 16 && rowsize != 32)
277 rowsize = 16;
278
279 for (i = 0; i < len; i += rowsize) {
280 linelen = min(remaining, rowsize);
281 remaining -= rowsize;
282
283 hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
284 linebuf, sizeof(linebuf), ascii);
285
286 switch (prefix_type) {
287 case DUMP_PREFIX_ADDRESS:
288 printk("%s%s%p: %s\n",
289 level, prefix_str, ptr + i, linebuf);
290 break;
291 case DUMP_PREFIX_OFFSET:
292 printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
293 break;
294 default:
295 printk("%s%s%s\n", level, prefix_str, linebuf);
296 break;
297 }
298 }
299 }
300 EXPORT_SYMBOL(print_hex_dump);
301
302 #endif /* defined(CONFIG_PRINTK) */
303