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.
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.
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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
30 #include <cryptoutil.h>
36 * hexlen should be 2 * blen + 1
41 tohexstr(uchar_t
*bytes
, size_t blen
, char *hexstr
, size_t hexlen
)
44 char hexlist
[] = "0123456789abcdef";
46 for (i
= 0; i
< blen
; i
++) {
47 if (hexlen
< (2 * i
+ 1))
49 hexstr
[2 * i
] = hexlist
[(bytes
[i
] >> 4) & 0xf];
50 hexstr
[2 * i
+ 1] = hexlist
[bytes
[i
] & 0xf];
52 hexstr
[2 * blen
] = '\0';
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
62 * The caller is responsible for freeing the malloc'ed array when done
64 * The return code is 0 if successful, otherwise the errno value is returned.
67 hexstr_to_bytes(char *hexstr
, size_t hexlen
, uchar_t
**bytes
, size_t *blen
)
76 if (hexstr
== NULL
|| (hexlen
% 2 == 1))
79 if (hexstr
[0] == '0' && ((hexstr
[1] == 'x') || (hexstr
[1] == 'X'))) {
92 for (i
= 0; i
< hexlen
; i
++) {
93 ch
= (unsigned char) *hexstr
;
102 if ((ch
>= '0') && (ch
<= '9'))
104 else if ((ch
>= 'A') && (ch
<= 'F'))
106 else if ((ch
>= 'a') && (ch
<= 'f'))
116 if (b
!= NULL
&& ret
!= 0) {