1 /* 2 * FILE: sha2.h 3 * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/ 4 * 5 * Copyright (c) 2000-2001, Aaron D. Gifford 6 * All rights reserved. 7 * 8 * Modified by Jelte Jansen to fit in ldns, and not clash with any 9 * system-defined SHA code. 10 * Changes: 11 * - Renamed (external) functions and constants to fit ldns style 12 * - Removed uintXX vs. u_intXX smartness, since ldns needs uintXX 13 * anyway 14 * - BYTE ORDER check replaced by simple ifdef as defined or not by 15 * configure.ac 16 * - Removed _End and _Data functions 17 * - Added ldns_shaX(data, len, digest) functions 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions and the following disclaimer. 24 * 2. Redistributions in binary form must reproduce the above copyright 25 * notice, this list of conditions and the following disclaimer in the 26 * documentation and/or other materials provided with the distribution. 27 * 3. Neither the name of the copyright holder nor the names of contributors 28 * may be used to endorse or promote products derived from this software 29 * without specific prior written permission. 30 * 31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND 32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 34 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE 35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 41 * SUCH DAMAGE. 42 * 43 * $Id: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $ 44 */ 45 46 #ifndef __LDNS_SHA2_H__ 47 #define __LDNS_SHA2_H__ 48 49 #ifdef __cplusplus 50 extern "C" { 51 #endif 52 53 54 /* 55 * Import u_intXX_t size_t type definitions from system headers. You 56 * may need to change this, or define these things yourself in this 57 * file. 58 * 59 * (include ldns/config.h so HAVE_INTTYPES is defined (or not, depending 60 * on the system)) 61 */ 62 #include <sys/types.h> 63 64 #ifdef HAVE_INTTYPES_H 65 66 #include <inttypes.h> 67 68 #endif /* HAVE_INTTYPES_H */ 69 70 71 /*** SHA-256/384/512 Various Length Definitions ***********************/ 72 #define LDNS_SHA256_BLOCK_LENGTH 64 73 #define LDNS_SHA256_DIGEST_LENGTH 32 74 #define LDNS_SHA256_DIGEST_STRING_LENGTH (LDNS_SHA256_DIGEST_LENGTH * 2 + 1) 75 #define LDNS_SHA384_BLOCK_LENGTH 128 76 #define LDNS_SHA384_DIGEST_LENGTH 48 77 #define LDNS_SHA384_DIGEST_STRING_LENGTH (LDNS_SHA384_DIGEST_LENGTH * 2 + 1) 78 #define LDNS_SHA512_BLOCK_LENGTH 128 79 #define LDNS_SHA512_DIGEST_LENGTH 64 80 #define LDNS_SHA512_DIGEST_STRING_LENGTH (LDNS_SHA512_DIGEST_LENGTH * 2 + 1) 81 82 83 /*** SHA-256/384/512 Context Structures *******************************/ 84 85 typedef struct _ldns_sha256_CTX { 86 uint32_t state[8]; 87 uint64_t bitcount; 88 uint8_t buffer[LDNS_SHA256_BLOCK_LENGTH]; 89 } ldns_sha256_CTX; 90 typedef struct _ldns_sha512_CTX { 91 uint64_t state[8]; 92 uint64_t bitcount[2]; 93 uint8_t buffer[LDNS_SHA512_BLOCK_LENGTH]; 94 } ldns_sha512_CTX; 95 96 typedef ldns_sha512_CTX ldns_sha384_CTX; 97 98 99 /*** SHA-256/384/512 Function Prototypes ******************************/ 100 void ldns_sha256_init(ldns_sha256_CTX *); 101 void ldns_sha256_update(ldns_sha256_CTX*, const uint8_t*, size_t); 102 void ldns_sha256_final(uint8_t[LDNS_SHA256_DIGEST_LENGTH], ldns_sha256_CTX*); 103 104 void ldns_sha384_init(ldns_sha384_CTX*); 105 void ldns_sha384_update(ldns_sha384_CTX*, const uint8_t*, size_t); 106 void ldns_sha384_final(uint8_t[LDNS_SHA384_DIGEST_LENGTH], ldns_sha384_CTX*); 107 108 void ldns_sha512_init(ldns_sha512_CTX*); 109 void ldns_sha512_update(ldns_sha512_CTX*, const uint8_t*, size_t); 110 void ldns_sha512_final(uint8_t[LDNS_SHA512_DIGEST_LENGTH], ldns_sha512_CTX*); 111 112 /** 113 * Convenience function to digest a fixed block of data at once. 114 * 115 * \param[in] data the data to digest 116 * \param[in] data_len the length of data in bytes 117 * \param[out] digest the length of data in bytes 118 * This pointer MUST have LDNS_SHA256_DIGEST_LENGTH bytes 119 * available 120 * \return the SHA1 digest of the given data 121 */ 122 unsigned char *ldns_sha256(unsigned char *data, unsigned int data_len, unsigned char *digest); 123 124 /** 125 * Convenience function to digest a fixed block of data at once. 126 * 127 * \param[in] data the data to digest 128 * \param[in] data_len the length of data in bytes 129 * \param[out] digest the length of data in bytes 130 * This pointer MUST have LDNS_SHA384_DIGEST_LENGTH bytes 131 * available 132 * \return the SHA1 digest of the given data 133 */ 134 unsigned char *ldns_sha384(unsigned char *data, unsigned int data_len, unsigned char *digest); 135 136 /** 137 * Convenience function to digest a fixed block of data at once. 138 * 139 * \param[in] data the data to digest 140 * \param[in] data_len the length of data in bytes 141 * \param[out] digest the length of data in bytes 142 * This pointer MUST have LDNS_SHA512_DIGEST_LENGTH bytes 143 * available 144 * \return the SHA1 digest of the given data 145 */ 146 unsigned char *ldns_sha512(unsigned char *data, unsigned int data_len, unsigned char *digest); 147 148 #ifdef __cplusplus 149 } 150 #endif /* __cplusplus */ 151 152 #endif /* __LDNS_SHA2_H__ */ 153