Path: blob/main/crypto/krb5/src/util/support/hex.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* util/support/hex.c - hex encoding/decoding implementation */2/*3* Copyright (C) 2018 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132#include <k5-platform.h>33#include <k5-hex.h>34#include <ctype.h>3536static inline char37hex_digit(uint8_t bval, int uppercase)38{39assert(bval <= 0xF);40if (bval < 10)41return '0' + bval;42else if (uppercase)43return 'A' + (bval - 10);44else45return 'a' + (bval - 10);46}4748int49k5_hex_encode(const void *bytes, size_t len, int uppercase, char **hex_out)50{51size_t i;52const uint8_t *p = bytes;53char *hex;5455*hex_out = NULL;5657hex = malloc(len * 2 + 1);58if (hex == NULL)59return ENOMEM;6061for (i = 0; i < len; i++) {62hex[i * 2] = hex_digit(p[i] >> 4, uppercase);63hex[i * 2 + 1] = hex_digit(p[i] & 0xF, uppercase);64}65hex[len * 2] = '\0';6667*hex_out = hex;68return 0;69}7071/* Decode a hex digit. Return 0-15 on success, -1 on invalid input. */72static inline int73decode_hexchar(unsigned char c)74{75if (isdigit(c))76return c - '0';77if (c >= 'A' && c <= 'F')78return c - 'A' + 10;79if (c >= 'a' && c <= 'f')80return c - 'a' + 10;81return -1;82}8384int85k5_hex_decode(const char *hex, uint8_t **bytes_out, size_t *len_out)86{87size_t hexlen, i;88int h1, h2;89uint8_t *bytes;9091*bytes_out = NULL;92*len_out = 0;9394hexlen = strlen(hex);95if (hexlen % 2 != 0)96return EINVAL;97bytes = malloc(hexlen / 2 + 1);98if (bytes == NULL)99return ENOMEM;100101for (i = 0; i < hexlen / 2; i++) {102h1 = decode_hexchar(hex[i * 2]);103h2 = decode_hexchar(hex[i * 2 + 1]);104if (h1 == -1 || h2 == -1) {105free(bytes);106return EINVAL;107}108bytes[i] = h1 * 16 + h2;109}110bytes[i] = 0;111112*bytes_out = bytes;113*len_out = hexlen / 2;114return 0;115}116117118