xref: /illumos-gate/usr/src/lib/libcryptoutil/common/tohexstr.c (revision 48633f182599946aebd63dccdc852ad722b57d0e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <errno.h>
28 #include <ctype.h>
29 
30 #include <cryptoutil.h>
31 
32 /*
33  * tohexstr
34  * IN	bytes
35  *	blen
36  *	hexlen should be 2 * blen + 1
37  * OUT
38  *	hexstr
39  */
40 void
41 tohexstr(uchar_t *bytes, size_t blen, char *hexstr, size_t hexlen)
42 {
43 	size_t i;
44 	char hexlist[] = "0123456789abcdef";
45 
46 	for (i = 0; i < blen; i++) {
47 		if (hexlen < (2 * i + 1))
48 			break;
49 		hexstr[2 * i] = hexlist[(bytes[i] >> 4) & 0xf];
50 		hexstr[2 * i + 1] = hexlist[bytes[i] & 0xf];
51 	}
52 	hexstr[2 * blen] = '\0';
53 }
54 
55 /*
56  * This function takes a char[] and length of hexadecimal values and
57  * returns a malloc'ed byte array with the length of that new byte array.
58  * The caller needs to provide a pointer to where this new malloc'ed byte array
59  * will be passed back; as well as, a pointer for the length of the new
60  * byte array.
61  *
62  * The caller is responsible for freeing the malloc'ed array when done
63  *
64  * The return code is 0 if successful, otherwise the errno value is returned.
65  */
66 int
67 hexstr_to_bytes(char *hexstr, size_t hexlen, uchar_t **bytes, size_t *blen)
68 {
69 	int i, ret = 0;
70 	unsigned char ch;
71 	uchar_t *b = NULL;
72 
73 	*bytes = NULL;
74 	*blen = 0;
75 
76 	if (hexstr == NULL || (hexlen % 2 == 1))
77 		return (EINVAL);
78 
79 	if (hexstr[0] == '0' && ((hexstr[1] == 'x') || (hexstr[1] == 'X'))) {
80 		hexstr += 2;
81 		hexlen -= 2;
82 	}
83 
84 	*blen = (hexlen / 2);
85 
86 	b = malloc(*blen);
87 	if (b == NULL) {
88 		*blen = 0;
89 		return (errno);
90 	}
91 
92 	for (i = 0; i < hexlen; i++) {
93 		ch = (unsigned char) *hexstr;
94 
95 		if (!isxdigit(ch)) {
96 			ret = EINVAL;
97 			goto out;
98 		}
99 
100 		hexstr++;
101 
102 		if ((ch >= '0') && (ch <= '9'))
103 			ch -= '0';
104 		else if ((ch >= 'A') && (ch <= 'F'))
105 			ch = ch - 'A' + 10;
106 		else if ((ch >= 'a') && (ch <= 'f'))
107 			ch = ch - 'a' + 10;
108 
109 		if (i & 1)
110 			b[i/2] |= ch;
111 		else
112 			b[i/2] = (ch << 4);
113 	}
114 
115 out:
116 	if (b != NULL && ret != 0) {
117 		free(b);
118 		*blen = 0;
119 	} else
120 		*bytes = b;
121 
122 	return (ret);
123 }
124