Commit 706fb055 authored by Daniel Salzman's avatar Daniel Salzman

dnssec: remove redundant base32hex implementation

parent a94af821
......@@ -118,8 +118,6 @@ src/dnssec/lib/sign/der.c
src/dnssec/lib/sign/der.h
src/dnssec/lib/sign/sign.c
src/dnssec/lib/tsig.c
src/dnssec/shared/base32hex.c
src/dnssec/shared/base32hex.h
src/dnssec/shared/bignum.c
src/dnssec/shared/bignum.h
src/dnssec/shared/dname.c
......@@ -154,7 +152,6 @@ src/dnssec/tests/nsec_bitmap.c
src/dnssec/tests/nsec_hash.c
src/dnssec/tests/random.c
src/dnssec/tests/sample_keys.h
src/dnssec/tests/shared_base32hex.c
src/dnssec/tests/shared_bignum.c
src/dnssec/tests/shared_dname.c
src/dnssec/tests/shared_timestamp.c
......
......@@ -548,7 +548,7 @@ khost_LDADD = $(libidn_LIBS) libknotus.la
knsupdate_CPPFLAGS = $(AM_CPPFLAGS) $(gnutls_CFLAGS)
knsupdate_LDADD = zscanner/libzscanner.la libknotus.la
knsec3hash_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/dnssec/lib/dnssec -I$(srcdir)/dnssec
knsec3hash_LDADD = dnssec/libdnssec.la dnssec/libshared.la
knsec3hash_LDADD = libknot.la libcontrib.la dnssec/libdnssec.la dnssec/libshared.la
kzonecheck_LDADD = libknotd.la libcontrib.la
kjournalprint_CPPFLAGS = $(AM_CPPFLAGS) $(gnutls_CFLAGS) $(liburcu_CFLAGS)
kjournalprint_LDADD = $(libidn_LIBS) $(liburcu_LIBS) libknotd.la libcontrib.la
......
......@@ -19,8 +19,6 @@ libshared_la_CPPFLAGS = \
$(AM_CPPFLAGS)
libshared_la_SOURCES = \
shared/base32hex.c \
shared/base32hex.h \
shared/bignum.c \
shared/bignum.h \
shared/dname.c \
......
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include "base32hex.h"
#include "binary.h"
#include "error.h"
#include "wire.h"
/*!
* Reorder five eight-bit groups into eight five-bit groups.
*/
static void reorder_block(const uint8_t in[5], uint8_t out[8])
{
out[0] = (in[0] & 0xf8) >> 3;
out[1] = (in[0] & 0x07) << 2 | (in[1] & 0xc0) >> 6;
out[2] = (in[1] & 0x3e) >> 1;
out[3] = (in[1] & 0x01) << 4 | (in[2] & 0xf0) >> 4;
out[4] = (in[2] & 0x0f) << 1 | (in[3] & 0x80) >> 7;
out[5] = (in[3] & 0x7c) >> 2;
out[6] = (in[3] & 0x03) << 3 | (in[4] & 0xe0) >> 5;
out[7] = (in[4] & 0x1f);
}
/*!
* Write one reordered block into output binary.
*/
static void write_block(const uint8_t data[8], wire_ctx_t *dst_ctx)
{
for (int i = 0; i < 8; i++) {
assert(data[i] < 32);
assert(wire_available(dst_ctx) > 0);
if (data[i] < 10) {
wire_write_u8(dst_ctx, '0' + data[i]);
} else {
wire_write_u8(dst_ctx, 'A' - 10 + data[i]);
}
}
}
/*!
* Convert binary data to base32hex.
*/
int base32hex_encode(const dnssec_binary_t *src, dnssec_binary_t *dst)
{
assert(src && src->data);
assert(dst);
if (src->size % 5 != 0) {
return DNSSEC_EINVAL;
}
int r = dnssec_binary_alloc(dst, src->size / 5 * 8);
if (r != DNSSEC_EOK) {
return r;
}
wire_ctx_t src_ctx = wire_init_binary(src);
wire_ctx_t dst_ctx = wire_init_binary(dst);
while (wire_available(&src_ctx) > 0) {
uint8_t in[5] = { 0 };
assert(wire_available(&src_ctx) >= sizeof(in));
wire_read(&src_ctx, in, sizeof(in));
uint8_t out[8];
reorder_block(in, out);
write_block(out, &dst_ctx);
}
assert(wire_available(&src_ctx) == 0);
assert(wire_available(&dst_ctx) == 0);
return DNSSEC_EOK;
}
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "binary.h"
/*!
* Convert binary data into base32hex encoding.
*
* \note Supports only aligned blocks.
*
* \see RFC 4648, section 7
*
* \param[in] src Source binary data.
* \param[out] dst Allocated output base32hex data.
*
* \return Error code, DNSSEC_EOK if successul.
*/
int base32hex_encode(const dnssec_binary_t *src, dnssec_binary_t *dst);
......@@ -34,7 +34,6 @@ check_PROGRAMS = \
random \
sign \
sign_der \
shared_base32hex \
shared_bignum \
shared_dname \
shared_timestamp \
......
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <tap/basic.h>
#include "base32hex.h"
#include "error.h"
struct test {
const char *name;
const dnssec_binary_t in;
const dnssec_binary_t out;
};
static const struct test TESTS[] = {
{
.name = "invalid, unaligned",
.in = {
.data = (uint8_t *)"abcdefghijk",
.size = 12
},
.out = {
.data = NULL,
.size = 0
}
},
{
.name = "valid, short",
.in = {
.data = (uint8_t *)"\x1a\x2b\x3c\x4d\x5e",
.size = 5
},
.out = {
.data = (uint8_t *)"38LJOJAU",
.size = 8
}
},
{
.name = "valid, long",
.in = {
.data = (uint8_t *)"\xc7\x75\x08\x05\x9e"
"\x1d\x75\x00\x99\xab"
"\x43\xb5\xa2\xfe\xea"
"\xe7\x0b\x3a\x6b\x83",
.size = 20
},
.out = {
.data = (uint8_t *)"OTQGG1CU3LQG16DB8EQQ5VNASS5JKQS3",
.size = 32
}
},
{ NULL }
};
int main(void)
{
plan_lazy();
for (const struct test *t = TESTS; t->name; t++) {
dnssec_binary_t out = { 0 };
int r = base32hex_encode(&t->in, &out);
if (t->out.data == NULL) {
ok(r != DNSSEC_EOK && out.size == 0 && out.data == NULL,
"expected failure: %s", t->name);
} else {
ok(r == DNSSEC_EOK && dnssec_binary_cmp(&out, &t->out) == 0,
"expected success: %s", t->name);
dnssec_binary_free(&out);
}
}
return 0;
}
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -21,13 +21,14 @@
#include <stdio.h>
#include <string.h>
#include "contrib/base32hex.h"
#include "contrib/strtonum.h"
#include "dnssec/error.h"
#include "dnssec/nsec.h"
#include "shared/base32hex.h"
#include "shared/dname.h"
#include "shared/hex.h"
#include "shared/print.h"
#include "libknot/error.h"
#define PROGRAM_NAME "knsec3hash"
......@@ -161,11 +162,12 @@ int main(int argc, char *argv[])
goto fail;
}
r = base32hex_encode(&digest, &digest_print);
if (r != DNSSEC_EOK) {
error("Cannot encode computed hash, %s.", dnssec_strerror(r));
r = base32hex_encode_alloc(digest.data, digest.size, &digest_print.data);
if (r < 0) {
error("Cannot encode computed hash, %s.", knot_strerror(r));
goto fail;
}
digest_print.size = r;
exit_code = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment