lib_hash_drbg/src/sha256.c
/*
 * Convert a hex string to a byte array
 *  Read two hex characters, convert them to a single byte and copy the value
 *   to the output buffer.
 *  If the hex string is an odd number of hex characters long, or would result in
 *   copying more to the output buffer than there is room, return an error.
 *
 * Parameters:
 * char* in        - hex string with two hex characters per ascii character
 * uint32_t inlen  - maximum length of input hex string, at most inlen characters will be used
 * uint8_t* out    - byte array without terminating '\0' ('\0' is a valid byte within the array)
 * uint32_t outlen - length of output byte array
 *
 * Return code:
 * -1  - Error
 * >=0 - length of output byte array
 */
Convert a hex string to a byte array
Read two hex characters, convert them to a single byte and copy the value
to the output buffer.
If the hex string is an odd number of hex characters long, or would result in
copying more to the output buffer than there is room, return an error.

char* in        - hex string with two hex characters per ascii character
uint32_t inlen  - maximum length of input hex string, at most inlen characters will be used
uint8_t* out    - byte array without terminating '\0' ('\0' is a valid byte within the array)
uint32_t outlen - length of output byte array

Return code:
-1  - Error
>=0 - length of output byte array
Convert a hex string to a byte array Read two hex characters convert them to a single byte and copy the value to the output buffer . If the hex string is an odd number of hex characters long or would result in copying more to the output buffer than there is room return an error . char * in - hex string with two hex characters per ascii character uint32_t inlen - maximum length of input hex string at most inlen characters will be used uint8_t * out - byte array without terminating ' \ 0 ' ( ' \ 0 ' is a valid byte within the array ) uint32_t outlen - length of output byte array Return code : - 1 - Error > = 0 - length of output byte array
int
hex_to_bytes(const char* in, uint32_t inlen, uint8_t* out, uint32_t outlen)
{
    uint32_t h_len;  
    uint32_t i;      
    uint8_t  c1, c2; 
    h_len = strnlen(in, inlen);
    if (((h_len & 0x01) == 1) || ((inlen < h_len) && (inlen & 0x01) == 1)) {
        return -1;
    }
    if ((h_len / 2) > outlen) {
        return -1;
    }
    for (i = 0; ((i < h_len) && (i < inlen)); i += 2) {
        c1 = in[i];
        if (!isxdigit(c1)) {
            return -1;
        }
        if (isdigit(c1)) {
            c1 -= '0';
        } else {
            c1 = tolower(c1) - 'a' + 10;
        }
        c2 = in[i+1];
        if (!isxdigit(c2)) {
            return -1;
        }
        if (isdigit(c2)) {
            c2 -= '0';
        } else {
            c2 = tolower(c2) - 'a' + 10;
        }
        *out++ = (c1 << 4) + c2;
    }
    return (h_len / 2); 
}
int hex_to_bytes ( const char * in , uint32_t inlen , uint8_t * out , uint32_t outlen ) { uint32_t h_len ; uint32_t i ; uint8_t c1 , c2 ; h_len = strnlen ( in , inlen ) ; if ( ( ( h_len & 0x01 ) == 1 ) || ( ( inlen < h_len ) && ( inlen & 0x01 ) == 1 ) ) { return -1 ; } if ( ( h_len / 2 ) > outlen ) { return -1 ; } for ( i = 0 ; ( ( i < h_len ) && ( i < inlen ) ) ; i += 2 ) { c1 = in [ i ] ; if ( ! isxdigit ( c1 ) ) { return -1 ; } if ( isdigit ( c1 ) ) { c1 -= ' ' ; } else { c1 = tolower ( c1 ) - ' ' + 10 ; } c2 = in [ i + 1 ] ; if ( ! isxdigit ( c2 ) ) { return -1 ; } if ( isdigit ( c2 ) ) { c2 -= ' ' ; } else { c2 = tolower ( c2 ) - ' ' + 10 ; } * out ++ = ( c1 << 4 ) + c2 ; } return ( h_len / 2 ) ; }
Convert a hex string to a byte array
  Read two hex characters, convert them to a single byte and copy the value
   to the output buffer.
Convert a hex string to a byte array Read two hex characters convert them to a single byte and copy the value to the output buffer .
/* length of hex string */ /* index into the input buffer */ /* two hex characters which will make up one byte */ /*
     * Find the actual length of the input string
     */ /*
         * There are an odd number of hex characters,
         *  which do not fully fill a byte array, and is not supported
         */ /*
         * There are more input characters than there is room for in the output buffer
         */ /*
     * Copy the hex input characters to the output buffer
     */ /*
         * C1 is the first hex character of a byte
         */ /* C2 is the second hex character of a byte
         */ /*
         * Combine c1 and c2 to form a full byte
         */ /* All is well */