-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add libkrb5support hex functions and tests
- Loading branch information
1 parent
e8e4115
commit 720dea5
Showing
7 changed files
with
359 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ | ||
/* include/k5-hex.h - libkrb5support hex encoding/decoding declarations */ | ||
/* | ||
* Copyright (C) 2018 by the Massachusetts Institute of Technology. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in | ||
* the documentation and/or other materials provided with the | ||
* distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | ||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
* OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#ifndef K5_HEX_H | ||
#define K5_HEX_H | ||
|
||
#include "k5-platform.h" | ||
|
||
/* | ||
* Encode len bytes in hex, placing the result in allocated storage in | ||
* *hex_out. Use uppercase hex digits if uppercase is non-zero. Return 0 on | ||
* success, ENOMEM on error. | ||
*/ | ||
int k5_hex_encode(const void *bytes, size_t len, int uppercase, | ||
char **hex_out); | ||
|
||
/* | ||
* Decode hex bytes, placing the result in allocated storage in *bytes_out and | ||
* *len_out. Null-terminate the result (primarily for decoding passwords in | ||
* libkdb_ldap). Return 0 on success, ENOMEM or EINVAL on error. | ||
*/ | ||
int k5_hex_decode(const char *hex, uint8_t **bytes_out, size_t *len_out); | ||
|
||
#endif /* K5_HEX_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ | ||
/* util/support/hex.c - hex encoding/decoding implementation */ | ||
/* | ||
* Copyright (C) 2018 by the Massachusetts Institute of Technology. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in | ||
* the documentation and/or other materials provided with the | ||
* distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | ||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
* OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include <k5-platform.h> | ||
#include <k5-hex.h> | ||
#include <ctype.h> | ||
|
||
static inline char | ||
hex_digit(uint8_t bval, int uppercase) | ||
{ | ||
assert(bval >= 0 && bval <= 0xF); | ||
if (bval < 10) | ||
return '0' + bval; | ||
else if (uppercase) | ||
return 'A' + (bval - 10); | ||
else | ||
return 'a' + (bval - 10); | ||
} | ||
|
||
int | ||
k5_hex_encode(const void *bytes, size_t len, int uppercase, char **hex_out) | ||
{ | ||
size_t i; | ||
const uint8_t *p = bytes; | ||
char *hex; | ||
|
||
*hex_out = NULL; | ||
|
||
hex = malloc(len * 2 + 1); | ||
if (hex == NULL) | ||
return ENOMEM; | ||
|
||
for (i = 0; i < len; i++) { | ||
hex[i * 2] = hex_digit(p[i] >> 4, uppercase); | ||
hex[i * 2 + 1] = hex_digit(p[i] & 0xF, uppercase); | ||
} | ||
hex[len * 2] = '\0'; | ||
|
||
*hex_out = hex; | ||
return 0; | ||
} | ||
|
||
/* Decode a hex digit. Return 0-15 on success, -1 on invalid input. */ | ||
static inline int | ||
decode_hexchar(unsigned char c) | ||
{ | ||
if (isdigit(c)) | ||
return c - '0'; | ||
if (c >= 'A' && c <= 'F') | ||
return c - 'A' + 10; | ||
if (c >= 'a' && c <= 'f') | ||
return c - 'a' + 10; | ||
return -1; | ||
} | ||
|
||
int | ||
k5_hex_decode(const char *hex, uint8_t **bytes_out, size_t *len_out) | ||
{ | ||
size_t hexlen, i; | ||
int h1, h2; | ||
uint8_t *bytes; | ||
|
||
*bytes_out = NULL; | ||
*len_out = 0; | ||
|
||
hexlen = strlen(hex); | ||
if (hexlen % 2 != 0) | ||
return EINVAL; | ||
bytes = malloc(hexlen / 2 + 1); | ||
if (bytes == NULL) | ||
return ENOMEM; | ||
|
||
for (i = 0; i < hexlen / 2; i++) { | ||
h1 = decode_hexchar(hex[i * 2]); | ||
h2 = decode_hexchar(hex[i * 2 + 1]); | ||
if (h1 == -1 || h2 == -1) { | ||
free(bytes); | ||
return EINVAL; | ||
} | ||
bytes[i] = h1 * 16 + h2; | ||
} | ||
bytes[i] = 0; | ||
|
||
*bytes_out = bytes; | ||
*len_out = hexlen / 2; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.