xref: /freebsd/contrib/unbound/validator/val_secalgo.h (revision 5469a9953005a9a4d4aad7be88545d441622e9a0)
18ed2b524SDag-Erling Smørgrav /*
28ed2b524SDag-Erling Smørgrav  * validator/val_secalgo.h - validator security algorithm functions.
38ed2b524SDag-Erling Smørgrav  *
48ed2b524SDag-Erling Smørgrav  * Copyright (c) 2012, NLnet Labs. All rights reserved.
58ed2b524SDag-Erling Smørgrav  *
68ed2b524SDag-Erling Smørgrav  * This software is open source.
78ed2b524SDag-Erling Smørgrav  *
88ed2b524SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
98ed2b524SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
108ed2b524SDag-Erling Smørgrav  * are met:
118ed2b524SDag-Erling Smørgrav  *
128ed2b524SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
138ed2b524SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
148ed2b524SDag-Erling Smørgrav  *
158ed2b524SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
168ed2b524SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
178ed2b524SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
188ed2b524SDag-Erling Smørgrav  *
198ed2b524SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
208ed2b524SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
218ed2b524SDag-Erling Smørgrav  * specific prior written permission.
228ed2b524SDag-Erling Smørgrav  *
238ed2b524SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
348ed2b524SDag-Erling Smørgrav  */
358ed2b524SDag-Erling Smørgrav 
368ed2b524SDag-Erling Smørgrav /**
378ed2b524SDag-Erling Smørgrav  * \file
388ed2b524SDag-Erling Smørgrav  *
398ed2b524SDag-Erling Smørgrav  * This file contains helper functions for the validator module.
408ed2b524SDag-Erling Smørgrav  * The functions take buffers with raw data and convert to library calls.
418ed2b524SDag-Erling Smørgrav  */
428ed2b524SDag-Erling Smørgrav 
438ed2b524SDag-Erling Smørgrav #ifndef VALIDATOR_VAL_SECALGO_H
448ed2b524SDag-Erling Smørgrav #define VALIDATOR_VAL_SECALGO_H
4517d15b25SDag-Erling Smørgrav struct sldns_buffer;
46*5469a995SCy Schubert struct secalgo_hash;
478ed2b524SDag-Erling Smørgrav 
4805ab2901SDag-Erling Smørgrav /** Return size of nsec3 hash algorithm, 0 if not supported */
4905ab2901SDag-Erling Smørgrav size_t nsec3_hash_algo_size_supported(int id);
5005ab2901SDag-Erling Smørgrav 
5105ab2901SDag-Erling Smørgrav /**
5205ab2901SDag-Erling Smørgrav  * Hash a single hash call of an NSEC3 hash algorithm.
5305ab2901SDag-Erling Smørgrav  * Iterations and salt are done by the caller.
5405ab2901SDag-Erling Smørgrav  * @param algo: nsec3 hash algorithm.
5505ab2901SDag-Erling Smørgrav  * @param buf: the buffer to digest
5605ab2901SDag-Erling Smørgrav  * @param len: length of buffer to digest.
5705ab2901SDag-Erling Smørgrav  * @param res: result stored here (must have sufficient space).
5805ab2901SDag-Erling Smørgrav  * @return false on failure.
5905ab2901SDag-Erling Smørgrav */
6005ab2901SDag-Erling Smørgrav int secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len,
6105ab2901SDag-Erling Smørgrav         unsigned char* res);
6205ab2901SDag-Erling Smørgrav 
638ed2b524SDag-Erling Smørgrav /**
64e2d15004SDag-Erling Smørgrav  * Calculate the sha256 hash for the data buffer into the result.
65e2d15004SDag-Erling Smørgrav  * @param buf: buffer to digest.
66e2d15004SDag-Erling Smørgrav  * @param len: length of the buffer to digest.
67e2d15004SDag-Erling Smørgrav  * @param res: result is stored here (space 256/8 bytes).
68e2d15004SDag-Erling Smørgrav  */
69e2d15004SDag-Erling Smørgrav void secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res);
70e2d15004SDag-Erling Smørgrav 
71e2d15004SDag-Erling Smørgrav /**
72*5469a995SCy Schubert  * Start a hash of type sha384. Allocates structure, then inits it,
73*5469a995SCy Schubert  * so that a series of updates can be performed, before the final result.
74*5469a995SCy Schubert  * @return hash structure.  NULL on malloc failure or no support.
75*5469a995SCy Schubert  */
76*5469a995SCy Schubert struct secalgo_hash* secalgo_hash_create_sha384(void);
77*5469a995SCy Schubert 
78*5469a995SCy Schubert /**
79*5469a995SCy Schubert  * Start a hash of type sha512. Allocates structure, then inits it,
80*5469a995SCy Schubert  * so that a series of updates can be performed, before the final result.
81*5469a995SCy Schubert  * @return hash structure.  NULL on malloc failure or no support.
82*5469a995SCy Schubert  */
83*5469a995SCy Schubert struct secalgo_hash* secalgo_hash_create_sha512(void);
84*5469a995SCy Schubert 
85*5469a995SCy Schubert /**
86*5469a995SCy Schubert  * Update a hash with more information to add to it.
87*5469a995SCy Schubert  * @param hash: the hash that is updated.
88*5469a995SCy Schubert  * @param data: data to add.
89*5469a995SCy Schubert  * @param len: length of data.
90*5469a995SCy Schubert  * @return false on failure.
91*5469a995SCy Schubert  */
92*5469a995SCy Schubert int secalgo_hash_update(struct secalgo_hash* hash, uint8_t* data, size_t len);
93*5469a995SCy Schubert 
94*5469a995SCy Schubert /**
95*5469a995SCy Schubert  * Get the final result of the hash.
96*5469a995SCy Schubert  * @param hash: the hash that has had updates to it.
97*5469a995SCy Schubert  * @param result: where to store the result.
98*5469a995SCy Schubert  * @param maxlen: length of the result buffer, eg. size of the allocation.
99*5469a995SCy Schubert  *	If not large enough the routine fails.
100*5469a995SCy Schubert  * @param resultlen: the length of the result, returned to the caller.
101*5469a995SCy Schubert  *	How much of maxlen is used.
102*5469a995SCy Schubert  * @return false on failure.
103*5469a995SCy Schubert  */
104*5469a995SCy Schubert int secalgo_hash_final(struct secalgo_hash* hash, uint8_t* result,
105*5469a995SCy Schubert 	size_t maxlen, size_t* resultlen);
106*5469a995SCy Schubert 
107*5469a995SCy Schubert /**
108*5469a995SCy Schubert  * Delete the hash structure.
109*5469a995SCy Schubert  * @param hash: the hash to delete.
110*5469a995SCy Schubert  */
111*5469a995SCy Schubert void secalgo_hash_delete(struct secalgo_hash* hash);
112*5469a995SCy Schubert 
113*5469a995SCy Schubert /**
1148ed2b524SDag-Erling Smørgrav  * Return size of DS digest according to its hash algorithm.
1158ed2b524SDag-Erling Smørgrav  * @param algo: DS digest algo.
1168ed2b524SDag-Erling Smørgrav  * @return size in bytes of digest, or 0 if not supported.
1178ed2b524SDag-Erling Smørgrav  */
1188ed2b524SDag-Erling Smørgrav size_t ds_digest_size_supported(int algo);
1198ed2b524SDag-Erling Smørgrav 
1208ed2b524SDag-Erling Smørgrav /**
1218ed2b524SDag-Erling Smørgrav  * @param algo: the DS digest algo
1228ed2b524SDag-Erling Smørgrav  * @param buf: the buffer to digest
1238ed2b524SDag-Erling Smørgrav  * @param len: length of buffer to digest.
1248ed2b524SDag-Erling Smørgrav  * @param res: result stored here (must have sufficient space).
1258ed2b524SDag-Erling Smørgrav  * @return false on failure.
1268ed2b524SDag-Erling Smørgrav  */
1278ed2b524SDag-Erling Smørgrav int secalgo_ds_digest(int algo, unsigned char* buf, size_t len,
1288ed2b524SDag-Erling Smørgrav 	unsigned char* res);
1298ed2b524SDag-Erling Smørgrav 
1308ed2b524SDag-Erling Smørgrav /** return true if DNSKEY algorithm id is supported */
1318ed2b524SDag-Erling Smørgrav int dnskey_algo_id_is_supported(int id);
1328ed2b524SDag-Erling Smørgrav 
1338ed2b524SDag-Erling Smørgrav /**
1348ed2b524SDag-Erling Smørgrav  * Check a canonical sig+rrset and signature against a dnskey
1358ed2b524SDag-Erling Smørgrav  * @param buf: buffer with data to verify, the first rrsig part and the
1368ed2b524SDag-Erling Smørgrav  *	canonicalized rrset.
1378ed2b524SDag-Erling Smørgrav  * @param algo: DNSKEY algorithm.
1388ed2b524SDag-Erling Smørgrav  * @param sigblock: signature rdata field from RRSIG
1398ed2b524SDag-Erling Smørgrav  * @param sigblock_len: length of sigblock data.
1408ed2b524SDag-Erling Smørgrav  * @param key: public key data from DNSKEY RR.
1418ed2b524SDag-Erling Smørgrav  * @param keylen: length of keydata.
1428ed2b524SDag-Erling Smørgrav  * @param reason: bogus reason in more detail.
1438ed2b524SDag-Erling Smørgrav  * @return secure if verification succeeded, bogus on crypto failure,
1448ed2b524SDag-Erling Smørgrav  *	unchecked on format errors and alloc failures.
1458ed2b524SDag-Erling Smørgrav  */
14617d15b25SDag-Erling Smørgrav enum sec_status verify_canonrrset(struct sldns_buffer* buf, int algo,
1478ed2b524SDag-Erling Smørgrav 	unsigned char* sigblock, unsigned int sigblock_len,
1488ed2b524SDag-Erling Smørgrav 	unsigned char* key, unsigned int keylen, char** reason);
1498ed2b524SDag-Erling Smørgrav 
1508ed2b524SDag-Erling Smørgrav #endif /* VALIDATOR_VAL_SECALGO_H */
151