xref: /titanic_51/usr/src/lib/libcryptoutil/common/tohexstr.c (revision a7e661a2db6ff10cd43a089e7f4dcbd0cfd609f1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*a7e661a2SAnthony Scarpino  * Common Development and Distribution License (the "License").
6*a7e661a2SAnthony Scarpino  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*a7e661a2SAnthony Scarpino  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
27*a7e661a2SAnthony Scarpino #include <errno.h>
28*a7e661a2SAnthony Scarpino #include <ctype.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <cryptoutil.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * tohexstr
347c478bd9Sstevel@tonic-gate  * IN	bytes
357c478bd9Sstevel@tonic-gate  *	blen
367c478bd9Sstevel@tonic-gate  *	hexlen should be 2 * blen + 1
377c478bd9Sstevel@tonic-gate  * OUT
387c478bd9Sstevel@tonic-gate  *	hexstr
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate void
417c478bd9Sstevel@tonic-gate tohexstr(uchar_t *bytes, size_t blen, char *hexstr, size_t hexlen)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	size_t i;
447c478bd9Sstevel@tonic-gate 	char hexlist[] = "0123456789abcdef";
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	for (i = 0; i < blen; i++) {
477c478bd9Sstevel@tonic-gate 		if (hexlen < (2 * i + 1))
487c478bd9Sstevel@tonic-gate 			break;
497c478bd9Sstevel@tonic-gate 		hexstr[2 * i] = hexlist[(bytes[i] >> 4) & 0xf];
507c478bd9Sstevel@tonic-gate 		hexstr[2 * i + 1] = hexlist[bytes[i] & 0xf];
517c478bd9Sstevel@tonic-gate 	}
527c478bd9Sstevel@tonic-gate 	hexstr[2 * blen] = '\0';
537c478bd9Sstevel@tonic-gate }
54*a7e661a2SAnthony Scarpino 
55*a7e661a2SAnthony Scarpino /*
56*a7e661a2SAnthony Scarpino  * This function takes a char[] and length of hexadecimal values and
57*a7e661a2SAnthony Scarpino  * returns a malloc'ed byte array with the length of that new byte array.
58*a7e661a2SAnthony Scarpino  * The caller needs to provide a pointer to where this new malloc'ed byte array
59*a7e661a2SAnthony Scarpino  * will be passed back; as well as, a pointer for the length of the new
60*a7e661a2SAnthony Scarpino  * byte array.
61*a7e661a2SAnthony Scarpino  *
62*a7e661a2SAnthony Scarpino  * The caller is responsible for freeing the malloc'ed array when done
63*a7e661a2SAnthony Scarpino  *
64*a7e661a2SAnthony Scarpino  * The return code is 0 if successful, otherwise the errno value is returned.
65*a7e661a2SAnthony Scarpino  */
66*a7e661a2SAnthony Scarpino int
67*a7e661a2SAnthony Scarpino hexstr_to_bytes(char *hexstr, size_t hexlen, uchar_t **bytes, size_t *blen)
68*a7e661a2SAnthony Scarpino {
69*a7e661a2SAnthony Scarpino 	int i, ret = 0;
70*a7e661a2SAnthony Scarpino 	unsigned char ch;
71*a7e661a2SAnthony Scarpino 	uchar_t *b = NULL;
72*a7e661a2SAnthony Scarpino 
73*a7e661a2SAnthony Scarpino 	*bytes = NULL;
74*a7e661a2SAnthony Scarpino 	*blen = 0;
75*a7e661a2SAnthony Scarpino 
76*a7e661a2SAnthony Scarpino 	if (hexstr == NULL || (hexlen % 2 == 1))
77*a7e661a2SAnthony Scarpino 		return (EINVAL);
78*a7e661a2SAnthony Scarpino 
79*a7e661a2SAnthony Scarpino 	if (hexstr[0] == '0' && ((hexstr[1] == 'x') || (hexstr[1] == 'X'))) {
80*a7e661a2SAnthony Scarpino 		hexstr += 2;
81*a7e661a2SAnthony Scarpino 		hexlen -= 2;
82*a7e661a2SAnthony Scarpino 	}
83*a7e661a2SAnthony Scarpino 
84*a7e661a2SAnthony Scarpino 	*blen = (hexlen / 2);
85*a7e661a2SAnthony Scarpino 
86*a7e661a2SAnthony Scarpino 	b = malloc(*blen);
87*a7e661a2SAnthony Scarpino 	if (b == NULL) {
88*a7e661a2SAnthony Scarpino 		*blen = 0;
89*a7e661a2SAnthony Scarpino 		return (errno);
90*a7e661a2SAnthony Scarpino 	}
91*a7e661a2SAnthony Scarpino 
92*a7e661a2SAnthony Scarpino 	for (i = 0; i < hexlen; i++) {
93*a7e661a2SAnthony Scarpino 		ch = (unsigned char) *hexstr;
94*a7e661a2SAnthony Scarpino 
95*a7e661a2SAnthony Scarpino 		if (!isxdigit(ch)) {
96*a7e661a2SAnthony Scarpino 			ret = EINVAL;
97*a7e661a2SAnthony Scarpino 			goto out;
98*a7e661a2SAnthony Scarpino 		}
99*a7e661a2SAnthony Scarpino 
100*a7e661a2SAnthony Scarpino 		hexstr++;
101*a7e661a2SAnthony Scarpino 
102*a7e661a2SAnthony Scarpino 		if ((ch >= '0') && (ch <= '9'))
103*a7e661a2SAnthony Scarpino 			ch -= '0';
104*a7e661a2SAnthony Scarpino 		else if ((ch >= 'A') && (ch <= 'F'))
105*a7e661a2SAnthony Scarpino 			ch = ch - 'A' + 10;
106*a7e661a2SAnthony Scarpino 		else if ((ch >= 'a') && (ch <= 'f'))
107*a7e661a2SAnthony Scarpino 			ch = ch - 'a' + 10;
108*a7e661a2SAnthony Scarpino 
109*a7e661a2SAnthony Scarpino 		if (i & 1)
110*a7e661a2SAnthony Scarpino 			b[i/2] |= ch;
111*a7e661a2SAnthony Scarpino 		else
112*a7e661a2SAnthony Scarpino 			b[i/2] = (ch << 4);
113*a7e661a2SAnthony Scarpino 	}
114*a7e661a2SAnthony Scarpino 
115*a7e661a2SAnthony Scarpino out:
116*a7e661a2SAnthony Scarpino 	if (b != NULL && ret != 0) {
117*a7e661a2SAnthony Scarpino 		free(b);
118*a7e661a2SAnthony Scarpino 		*blen = 0;
119*a7e661a2SAnthony Scarpino 	} else
120*a7e661a2SAnthony Scarpino 		*bytes = b;
121*a7e661a2SAnthony Scarpino 
122*a7e661a2SAnthony Scarpino 	return (ret);
123*a7e661a2SAnthony Scarpino }
124