diff options
author | Mattias Andrée <maandree@kth.se> | 2023-07-06 07:20:03 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2023-07-06 07:20:03 +0200 |
commit | 0c3cf7819ceb7fe970f0fc240fa177b4e6dd5506 (patch) | |
tree | 29dbd39a897dbef2e391a095d39fe03b07b22d94 | |
parent | Remove -Wall -O3 from CFLAGS (unportable) and -s from LDFLAGS (diff) | |
download | blakesum-0c3cf7819ceb7fe970f0fc240fa177b4e6dd5506.tar.gz blakesum-0c3cf7819ceb7fe970f0fc240fa177b4e6dd5506.tar.bz2 blakesum-0c3cf7819ceb7fe970f0fc240fa177b4e6dd5506.tar.xz |
Add -S (salt) to bsum
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r-- | b224sum.1 | 22 | ||||
-rw-r--r-- | b256sum.1 | 22 | ||||
-rw-r--r-- | b384sum.1 | 22 | ||||
-rw-r--r-- | b512sum.1 | 22 | ||||
-rw-r--r-- | bsum.1 | 25 | ||||
-rw-r--r-- | bsum.c | 54 | ||||
-rw-r--r-- | common.c | 36 | ||||
-rw-r--r-- | common.h | 1 |
8 files changed, 178 insertions, 26 deletions
@@ -3,7 +3,7 @@ b224sum - Compute and check BLAKE-224 message digests .SH SYNOPSIS .B b224sum -[-c | -B | -L | -U] [-xz] +[-c | -B | -L | -U] [-S salt] [-xz] .RI [ file "] ..." .SH DESCRIPTION Print or check BLAKE-224 checksums. @@ -34,6 +34,26 @@ option is also used. .B -L Output checksums in lower-case hexadecimal representation. (Default) .TP +.BI "-S " salt +Specify a 16-byte salt that the BLAKE-224 algorithm shall use. +This salt shall be expressed in full length hexadecimal: 32 +hexadecimal digits, or rather 16 pairs of hexadecimal digits, +ordered from left to right to specify the values from index 0 +to the last byte. In each pair, the left digit stores the high +bits of the byte, and the right digit stores the low bits of +the byte. For example, the digit pair +.B 80 +represents a byte with the decimal value 128, whereas the +digit pair +.B 08 +represents a byte with the decimal value 8. The pairs +are joined without any delimiters, and no byte may be +omitted. So, for a salt where each byte's value is its +index, the salt is expressed as, +.BR 000102030405060708090a0b0c0d0e0f , +however each letter may be either small or capital. +If no salt is specified, an all-zeroes salt is used. +.TP .B -U Output checksums in upper-case hexadecimal representation. .TP @@ -3,7 +3,7 @@ b256sum - Compute and check BLAKE-256 message digests .SH SYNOPSIS .B b256sum -[-c | -B | -L | -U] [-xz] +[-c | -B | -L | -U] [-S salt] [-xz] .RI [ file "] ..." .SH DESCRIPTION Print or check BLAKE-256 checksums. @@ -34,6 +34,26 @@ option is also used. .B -L Output checksums in lower-case hexadecimal representation. (Default) .TP +.BI "-S " salt +Specify a 16-byte salt that the BLAKE-256 algorithm shall use. +This salt shall be expressed in full length hexadecimal: 32 +hexadecimal digits, or rather 16 pairs of hexadecimal digits, +ordered from left to right to specify the values from index 0 +to the last byte. In each pair, the left digit stores the high +bits of the byte, and the right digit stores the low bits of +the byte. For example, the digit pair +.B 80 +represents a byte with the decimal value 128, whereas the +digit pair +.B 08 +represents a byte with the decimal value 8. The pairs +are joined without any delimiters, and no byte may be +omitted. So, for a salt where each byte's value is its +index, the salt is expressed as, +.BR 000102030405060708090a0b0c0d0e0f , +however each letter may be either small or capital. +If no salt is specified, an all-zeroes salt is used. +.TP .B -U Output checksums in upper-case hexadecimal representation. .TP @@ -3,7 +3,7 @@ b384sum - Compute and check BLAKE-384 message digests .SH SYNOPSIS .B b384sum -[-c | -B | -L | -U] [-xz] +[-c | -B | -L | -U] [-S salt] [-xz] .RI [ file "] ..." .SH DESCRIPTION Print or check BLAKE-384 checksums. @@ -34,6 +34,26 @@ option is also used. .B -L Output checksums in lower-case hexadecimal representation. (Default) .TP +.BI "-S " salt +Specify a 32-byte salt that the BLAKE-384 algorithm shall use. +This salt shall be expressed in full length hexadecimal: 64 +hexadecimal digits, or rather 32 pairs of hexadecimal digits, +ordered from left to right to specify the values from index 0 +to the last byte. In each pair, the left digit stores the high +bits of the byte, and the right digit stores the low bits of +the byte. For example, the digit pair +.B 80 +represents a byte with the decimal value 128, whereas the +digit pair +.B 08 +represents a byte with the decimal value 8. The pairs +are joined without any delimiters, and no byte may be +omitted. So, for a salt where each byte's value is its +index, the salt is expressed as, +.BR 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f , +however each letter may be either small or capital. +If no salt is specified, an all-zeroes salt is used. +.TP .B -U Output checksums in upper-case hexadecimal representation. .TP @@ -3,7 +3,7 @@ b512sum - Compute and check BLAKE-512 message digests .SH SYNOPSIS .B b512sum -[-c | -B | -L | -U] [-xz] +[-c | -B | -L | -U] [-S salt] [-xz] .RI [ file "] ..." .SH DESCRIPTION Print or check BLAKE-512 checksums. @@ -34,6 +34,26 @@ option is also used. .B -L Output checksums in lower-case hexadecimal representation. (Default) .TP +.BI "-S " salt +Specify a 32-byte salt that the BLAKE-512 algorithm shall use. +This salt shall be expressed in full length hexadecimal: 64 +hexadecimal digits, or rather 32 pairs of hexadecimal digits, +ordered from left to right to specify the values from index 0 +to the last byte. In each pair, the left digit stores the high +bits of the byte, and the right digit stores the low bits of +the byte. For example, the digit pair +.B 80 +represents a byte with the decimal value 128, whereas the +digit pair +.B 08 +represents a byte with the decimal value 8. The pairs +are joined without any delimiters, and no byte may be +omitted. So, for a salt where each byte's value is its +index, the salt is expressed as, +.BR 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f , +however each letter may be either small or capital. +If no salt is specified, an all-zeroes salt is used. +.TP .B -U Output checksums in upper-case hexadecimal representation. .TP @@ -3,7 +3,7 @@ bsum - Compute and check BLAKE message digests .SH SYNOPSIS .B bsum -[-l bits] [-c | -B | -L | -U] [-xz] +[-l bits] [-c | -B | -L | -U] [-S salt] [-xz] .RI [ file "] ..." .SH DESCRIPTION Print or check BLAKE checksums. @@ -38,6 +38,29 @@ Output checksums in lower-case hexadecimal representation. (Default) Select version of the BLAKE algorithm. Valid values are 224 (default), 256, 384, and 512. .TP +.BI "-S " salt +Specify a 16-byte (for the 224 and 256 bit versions) or +32-byte salt (for the 384 and 512 bit versions) that the +BLAKE algorithm shall use. This salt shall be expressed +in full length hexadecimal: 32 (for 16-bytes) or 64 +(for 32-bytes) hexadecimal digits, or rather 16 or 32 pairs +of hexadecimal digits, ordered from left to right to specify +the values from index 0 to the last byte. In each pair, +the left digit stores the high bits of the byte, and the +right digit stores the low bits of the byte. For example, +the digit pair +.B 80 +represents a byte with the decimal value 128, whereas the +digit pair +.B 08 +represents a byte with the decimal value 8. The pairs +are joined without any delimiters, and no byte may be +omitted. So, for a 16 byte salt where each byte's value +is its index, the salt is expressed as, +.BR 000102030405060708090a0b0c0d0e0f , +however each letter may be either small or capital. +If no salt is specified, an all-zeroes salt is used. +.TP .B -U Output checksums in upper-case hexadecimal representation. .TP @@ -12,12 +12,12 @@ static int flag_upper = 0; static int flag_hex = 0; static int flag_zero = 0; static int length; +static void *salt = NULL; static void usage(void) { - /* TODO add support for salt (-S) */ - fprintf(stderr, "usage: %s%s [-c | -B | -L | -U] [-xz] [file] ...", + fprintf(stderr, "usage: %s%s [-c | -B | -L | -U] [-S salt] [-xz] [file] ...", argv0, lenght_by_command_name ? "" : " [-l bits]"); exit(2); } @@ -39,8 +39,8 @@ get_lenght_by_command_name(const char *command) } static int -hash_fd_blake(int fd, const char *fname, int decode_hex, unsigned char hash[], void *state, - void (*init_func)(void *state), size_t (*update_func)(void *state, const void *msg, size_t n), +hash_fd_blake(int fd, const char *fname, int decode_hex, unsigned char hash[], void *state, void *salt, + void (*init_func)(void *state, void *salt), size_t (*update_func)(void *state, const void *msg, size_t n), size_t (*get_buf_size_func)(size_t bytes, size_t bits, const char *suffix), void (*digest_func)(void *state, void *msg, size_t bytes, size_t bits, const char *suffix, unsigned char out[])) { @@ -51,7 +51,7 @@ hash_fd_blake(int fd, const char *fname, int decode_hex, unsigned char hash[], v size_t req; ssize_t r; int ok; - init_func(state); + init_func(state, salt); for (;;) { if (len == size) buf = erealloc(buf, size += 8 << 10); @@ -91,44 +91,44 @@ hash_fd_blake(int fd, const char *fname, int decode_hex, unsigned char hash[], v } static int -hash_fd_blake224(int fd, const char *fname, int decode_hex, unsigned char hash[]) +hash_fd_blake224(int fd, const char *fname, int decode_hex, unsigned char hash[], void *salt) { struct libblake_blake224_state state; - return hash_fd_blake(fd, fname, decode_hex, hash, &state, - (void (*)(void *))&libblake_blake224_init, + return hash_fd_blake(fd, fname, decode_hex, hash, &state, salt, + (void (*)(void *, void *))&libblake_blake224_init2, (size_t (*)(void *, const void *, size_t))&libblake_blake224_update, &libblake_blake224_digest_get_required_input_size, (void (*)(void *, void *, size_t, size_t, const char *, unsigned char[]))&libblake_blake224_digest); } static int -hash_fd_blake256(int fd, const char *fname, int decode_hex, unsigned char hash[]) +hash_fd_blake256(int fd, const char *fname, int decode_hex, unsigned char hash[], void *salt) { struct libblake_blake256_state state; - return hash_fd_blake(fd, fname, decode_hex, hash, &state, - (void (*)(void *))&libblake_blake256_init, + return hash_fd_blake(fd, fname, decode_hex, hash, &state, salt, + (void (*)(void *, void *))&libblake_blake256_init2, (size_t (*)(void *, const void *, size_t))&libblake_blake256_update, &libblake_blake256_digest_get_required_input_size, (void (*)(void *, void *, size_t, size_t, const char *, unsigned char[]))&libblake_blake256_digest); } static int -hash_fd_blake384(int fd, const char *fname, int decode_hex, unsigned char hash[]) +hash_fd_blake384(int fd, const char *fname, int decode_hex, unsigned char hash[], void *salt) { struct libblake_blake384_state state; - return hash_fd_blake(fd, fname, decode_hex, hash, &state, - (void (*)(void *))&libblake_blake384_init, + return hash_fd_blake(fd, fname, decode_hex, hash, &state, salt, + (void (*)(void *, void *))&libblake_blake384_init2, (size_t (*)(void *, const void *, size_t))&libblake_blake384_update, &libblake_blake384_digest_get_required_input_size, (void (*)(void *, void *, size_t, size_t, const char *, unsigned char[]))&libblake_blake384_digest); } static int -hash_fd_blake512(int fd, const char *fname, int decode_hex, unsigned char hash[]) +hash_fd_blake512(int fd, const char *fname, int decode_hex, unsigned char hash[], void *salt) { struct libblake_blake512_state state; - return hash_fd_blake(fd, fname, decode_hex, hash, &state, - (void (*)(void *))&libblake_blake512_init, + return hash_fd_blake(fd, fname, decode_hex, hash, &state, salt, + (void (*)(void *, void *))&libblake_blake512_init2, (size_t (*)(void *, const void *, size_t))&libblake_blake512_update, &libblake_blake512_digest_get_required_input_size, (void (*)(void *, void *, size_t, size_t, const char *, unsigned char[]))&libblake_blake512_digest); @@ -140,13 +140,13 @@ hash_fd(int fd, const char *fname, int decode_hex, unsigned char hash[]) int ret; if (length == 224) - ret = hash_fd_blake224(fd, fname, decode_hex, hash); + ret = hash_fd_blake224(fd, fname, decode_hex, hash, salt); else if (length == 256) - ret = hash_fd_blake256(fd, fname, decode_hex, hash); + ret = hash_fd_blake256(fd, fname, decode_hex, hash, salt); else if (length == 384) - ret = hash_fd_blake384(fd, fname, decode_hex, hash); + ret = hash_fd_blake384(fd, fname, decode_hex, hash, salt); else if (length == 512) - ret = hash_fd_blake512(fd, fname, decode_hex, hash); + ret = hash_fd_blake512(fd, fname, decode_hex, hash, salt); else abort(); @@ -156,6 +156,8 @@ hash_fd(int fd, const char *fname, int decode_hex, unsigned char hash[]) int main(int argc, char *argv[]) { + const char *salt_str = NULL; + uint_least8_t salt_buf[32]; int status = 0; int output_case; char newline; @@ -180,6 +182,11 @@ main(int argc, char *argv[]) flag_upper = 1; flag_lower = 0; break; + case 'S': + if (salt_str) + usage(); + salt_str = ARG(); + break; case 'x': flag_hex = 1; break; @@ -205,6 +212,11 @@ main(int argc, char *argv[]) if (!length) length = 224; + if (salt_str) { + parse_salt(salt_buf, salt_str, length <= 256 ? 16 : 32); + salt = salt_buf; + } + newline = flag_zero ? '\0' : '\n'; if (flag_check) { if (!argc) { @@ -226,3 +226,39 @@ hash_and_print(const char *path, size_t hashlen, int decode_hex, char newline, i free(hex); return 0; } + + +void +parse_salt(uint_least8_t *salt, const char *s, size_t required_length) +{ + size_t i; + + for (i = 0; i < required_length; i++, s = &s[2]) { + if (!s[0] || !s[1]) + goto too_short; + if (!isxdigit(s[0]) || !isxdigit(s[1])) + goto not_hexadecimal; + + salt[i] = (uint_least8_t)((((s[0] & 15) + (s[0] > '9' ? 9 : 0)) << 4) | + (s[1] & 15) + (s[1] > '9' ? 9 : 0)); + } + + if (*s) + goto too_long; + + return; + +not_hexadecimal: + fprintf(stderr, "%s: specified salt contains non-hexadecimal-digit character\n", argv0); + exit(2); + +too_short: + fprintf(stderr, "%s: specified salt is shorter than expected, should be %zu hexadecimal digits\n", + argv0, required_length * 2); + exit(2); + +too_long: + fprintf(stderr, "%s: specified salt is longer than expected, should be %zu hexadecimal digits\n", + argv0, required_length * 2); + exit(2); +} @@ -19,6 +19,7 @@ void *emalloc(size_t n); int open_file(const char *path, int *closep); int check_and_print(const char *path, size_t hashlen, int decode_hex, char newline); int hash_and_print(const char *path, size_t hashlen, int decode_hex, char newline, int output_case); +void parse_salt(uint_least8_t *salt, const char *s, size_t required_length); /* *sum.c */ int hash_fd(int fd, const char *fname, int decode_hex, unsigned char hash[]); |