xref: /onnv-gate/usr/src/lib/libcryptoutil/common/tohexstr.c (revision 8309:c32ddef7d3ad)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*8309SAnthony.Scarpino@Sun.COM  * Common Development and Distribution License (the "License").
6*8309SAnthony.Scarpino@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8309SAnthony.Scarpino@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
27*8309SAnthony.Scarpino@Sun.COM #include <errno.h>
28*8309SAnthony.Scarpino@Sun.COM #include <ctype.h>
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <cryptoutil.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * tohexstr
340Sstevel@tonic-gate  * IN	bytes
350Sstevel@tonic-gate  *	blen
360Sstevel@tonic-gate  *	hexlen should be 2 * blen + 1
370Sstevel@tonic-gate  * OUT
380Sstevel@tonic-gate  *	hexstr
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate void
tohexstr(uchar_t * bytes,size_t blen,char * hexstr,size_t hexlen)410Sstevel@tonic-gate tohexstr(uchar_t *bytes, size_t blen, char *hexstr, size_t hexlen)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate 	size_t i;
440Sstevel@tonic-gate 	char hexlist[] = "0123456789abcdef";
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	for (i = 0; i < blen; i++) {
470Sstevel@tonic-gate 		if (hexlen < (2 * i + 1))
480Sstevel@tonic-gate 			break;
490Sstevel@tonic-gate 		hexstr[2 * i] = hexlist[(bytes[i] >> 4) & 0xf];
500Sstevel@tonic-gate 		hexstr[2 * i + 1] = hexlist[bytes[i] & 0xf];
510Sstevel@tonic-gate 	}
520Sstevel@tonic-gate 	hexstr[2 * blen] = '\0';
530Sstevel@tonic-gate }
54*8309SAnthony.Scarpino@Sun.COM 
55*8309SAnthony.Scarpino@Sun.COM /*
56*8309SAnthony.Scarpino@Sun.COM  * This function takes a char[] and length of hexadecimal values and
57*8309SAnthony.Scarpino@Sun.COM  * returns a malloc'ed byte array with the length of that new byte array.
58*8309SAnthony.Scarpino@Sun.COM  * The caller needs to provide a pointer to where this new malloc'ed byte array
59*8309SAnthony.Scarpino@Sun.COM  * will be passed back; as well as, a pointer for the length of the new
60*8309SAnthony.Scarpino@Sun.COM  * byte array.
61*8309SAnthony.Scarpino@Sun.COM  *
62*8309SAnthony.Scarpino@Sun.COM  * The caller is responsible for freeing the malloc'ed array when done
63*8309SAnthony.Scarpino@Sun.COM  *
64*8309SAnthony.Scarpino@Sun.COM  * The return code is 0 if successful, otherwise the errno value is returned.
65*8309SAnthony.Scarpino@Sun.COM  */
66*8309SAnthony.Scarpino@Sun.COM int
hexstr_to_bytes(char * hexstr,size_t hexlen,uchar_t ** bytes,size_t * blen)67*8309SAnthony.Scarpino@Sun.COM hexstr_to_bytes(char *hexstr, size_t hexlen, uchar_t **bytes, size_t *blen)
68*8309SAnthony.Scarpino@Sun.COM {
69*8309SAnthony.Scarpino@Sun.COM 	int i, ret = 0;
70*8309SAnthony.Scarpino@Sun.COM 	unsigned char ch;
71*8309SAnthony.Scarpino@Sun.COM 	uchar_t *b = NULL;
72*8309SAnthony.Scarpino@Sun.COM 
73*8309SAnthony.Scarpino@Sun.COM 	*bytes = NULL;
74*8309SAnthony.Scarpino@Sun.COM 	*blen = 0;
75*8309SAnthony.Scarpino@Sun.COM 
76*8309SAnthony.Scarpino@Sun.COM 	if (hexstr == NULL || (hexlen % 2 == 1))
77*8309SAnthony.Scarpino@Sun.COM 		return (EINVAL);
78*8309SAnthony.Scarpino@Sun.COM 
79*8309SAnthony.Scarpino@Sun.COM 	if (hexstr[0] == '0' && ((hexstr[1] == 'x') || (hexstr[1] == 'X'))) {
80*8309SAnthony.Scarpino@Sun.COM 		hexstr += 2;
81*8309SAnthony.Scarpino@Sun.COM 		hexlen -= 2;
82*8309SAnthony.Scarpino@Sun.COM 	}
83*8309SAnthony.Scarpino@Sun.COM 
84*8309SAnthony.Scarpino@Sun.COM 	*blen = (hexlen / 2);
85*8309SAnthony.Scarpino@Sun.COM 
86*8309SAnthony.Scarpino@Sun.COM 	b = malloc(*blen);
87*8309SAnthony.Scarpino@Sun.COM 	if (b == NULL) {
88*8309SAnthony.Scarpino@Sun.COM 		*blen = 0;
89*8309SAnthony.Scarpino@Sun.COM 		return (errno);
90*8309SAnthony.Scarpino@Sun.COM 	}
91*8309SAnthony.Scarpino@Sun.COM 
92*8309SAnthony.Scarpino@Sun.COM 	for (i = 0; i < hexlen; i++) {
93*8309SAnthony.Scarpino@Sun.COM 		ch = (unsigned char) *hexstr;
94*8309SAnthony.Scarpino@Sun.COM 
95*8309SAnthony.Scarpino@Sun.COM 		if (!isxdigit(ch)) {
96*8309SAnthony.Scarpino@Sun.COM 			ret = EINVAL;
97*8309SAnthony.Scarpino@Sun.COM 			goto out;
98*8309SAnthony.Scarpino@Sun.COM 		}
99*8309SAnthony.Scarpino@Sun.COM 
100*8309SAnthony.Scarpino@Sun.COM 		hexstr++;
101*8309SAnthony.Scarpino@Sun.COM 
102*8309SAnthony.Scarpino@Sun.COM 		if ((ch >= '0') && (ch <= '9'))
103*8309SAnthony.Scarpino@Sun.COM 			ch -= '0';
104*8309SAnthony.Scarpino@Sun.COM 		else if ((ch >= 'A') && (ch <= 'F'))
105*8309SAnthony.Scarpino@Sun.COM 			ch = ch - 'A' + 10;
106*8309SAnthony.Scarpino@Sun.COM 		else if ((ch >= 'a') && (ch <= 'f'))
107*8309SAnthony.Scarpino@Sun.COM 			ch = ch - 'a' + 10;
108*8309SAnthony.Scarpino@Sun.COM 
109*8309SAnthony.Scarpino@Sun.COM 		if (i & 1)
110*8309SAnthony.Scarpino@Sun.COM 			b[i/2] |= ch;
111*8309SAnthony.Scarpino@Sun.COM 		else
112*8309SAnthony.Scarpino@Sun.COM 			b[i/2] = (ch << 4);
113*8309SAnthony.Scarpino@Sun.COM 	}
114*8309SAnthony.Scarpino@Sun.COM 
115*8309SAnthony.Scarpino@Sun.COM out:
116*8309SAnthony.Scarpino@Sun.COM 	if (b != NULL && ret != 0) {
117*8309SAnthony.Scarpino@Sun.COM 		free(b);
118*8309SAnthony.Scarpino@Sun.COM 		*blen = 0;
119*8309SAnthony.Scarpino@Sun.COM 	} else
120*8309SAnthony.Scarpino@Sun.COM 		*bytes = b;
121*8309SAnthony.Scarpino@Sun.COM 
122*8309SAnthony.Scarpino@Sun.COM 	return (ret);
123*8309SAnthony.Scarpino@Sun.COM }
124