xref: /openbsd-src/usr.bin/dig/lib/dns/key.c (revision b9558d14c675017cf470d7469a47201d05e39444)
1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /* $Id: key.c,v 1.8 2020/02/25 05:00:42 jsg Exp $ */
18 
19 #include <stddef.h>
20 #include <stdint.h>
21 
22 #include <isc/region.h>
23 #include <isc/util.h>
24 
25 #include <dst/dst.h>
26 
27 #include "dst_internal.h"
28 
29 uint16_t
dst_region_computeid(const isc_region_t * source,unsigned int alg)30 dst_region_computeid(const isc_region_t *source, unsigned int alg) {
31 	uint32_t ac;
32 	const unsigned char *p;
33 	int size;
34 
35 	REQUIRE(source != NULL);
36 	REQUIRE(source->length >= 4);
37 
38 	p = source->base;
39 	size = source->length;
40 
41 	if (alg == DST_ALG_RSAMD5)
42 		return ((p[size - 3] << 8) + p[size - 2]);
43 
44 	for (ac = 0; size > 1; size -= 2, p += 2)
45 		ac += ((*p) << 8) + *(p + 1);
46 
47 	if (size > 0)
48 		ac += ((*p) << 8);
49 	ac += (ac >> 16) & 0xffff;
50 
51 	return ((uint16_t)(ac & 0xffff));
52 }
53 
54 unsigned int
dst_key_size(const dst_key_t * key)55 dst_key_size(const dst_key_t *key) {
56 	return (key->key_size);
57 }
58 
59 unsigned int
dst_key_alg(const dst_key_t * key)60 dst_key_alg(const dst_key_t *key) {
61 	return (key->key_alg);
62 }
63 
64 void
dst_key_setbits(dst_key_t * key,uint16_t bits)65 dst_key_setbits(dst_key_t *key, uint16_t bits) {
66 	unsigned int maxbits;
67 	if (bits != 0) {
68 		RUNTIME_CHECK(dst_key_sigsize(key, &maxbits) == ISC_R_SUCCESS);
69 		maxbits *= 8;
70 		REQUIRE(bits <= maxbits);
71 	}
72 	key->key_bits = bits;
73 }
74 
75 uint16_t
dst_key_getbits(const dst_key_t * key)76 dst_key_getbits(const dst_key_t *key) {
77 	return (key->key_bits);
78 }
79 
80 /*! \file */
81