sha2.h
Go to the documentation of this file.
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 #include <stdint.h> /* uint32_t and friends */
50 #include <stddef.h> /* size_t and NULL */
51 
52 #if LDNS_BUILD_CONFIG_HAVE_INTTYPES_H
53 # include <inttypes.h>
54 #endif
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 
61 /*** SHA-256/384/512 Various Length Definitions ***********************/
62 #define LDNS_SHA256_BLOCK_LENGTH 64
63 #define LDNS_SHA256_DIGEST_LENGTH 32
64 #define LDNS_SHA256_DIGEST_STRING_LENGTH (LDNS_SHA256_DIGEST_LENGTH * 2 + 1)
65 #define LDNS_SHA384_BLOCK_LENGTH 128
66 #define LDNS_SHA384_DIGEST_LENGTH 48
67 #define LDNS_SHA384_DIGEST_STRING_LENGTH (LDNS_SHA384_DIGEST_LENGTH * 2 + 1)
68 #define LDNS_SHA512_BLOCK_LENGTH 128
69 #define LDNS_SHA512_DIGEST_LENGTH 64
70 #define LDNS_SHA512_DIGEST_STRING_LENGTH (LDNS_SHA512_DIGEST_LENGTH * 2 + 1)
71 
72 
73 /*** SHA-256/384/512 Context Structures *******************************/
74 
75 typedef struct _ldns_sha256_CTX {
76  uint32_t state[8];
77  uint64_t bitcount;
80 typedef struct _ldns_sha512_CTX {
81  uint64_t state[8];
82  uint64_t bitcount[2];
85 
87 
88 
89 /*** SHA-256/384/512 Function Prototypes ******************************/
91 void ldns_sha256_update(ldns_sha256_CTX*, const uint8_t*, size_t);
93 
95 void ldns_sha384_update(ldns_sha384_CTX*, const uint8_t*, size_t);
97 
99 void ldns_sha512_update(ldns_sha512_CTX*, const uint8_t*, size_t);
101 
112 unsigned char *ldns_sha256(const unsigned char *data, unsigned int data_len, unsigned char *digest);
113 
124 unsigned char *ldns_sha384(const unsigned char *data, unsigned int data_len, unsigned char *digest);
125 
136 unsigned char *ldns_sha512(const unsigned char *data, unsigned int data_len, unsigned char *digest);
137 
138 #ifdef __cplusplus
139 }
140 #endif /* __cplusplus */
141 
142 #endif /* __LDNS_SHA2_H__ */
unsigned char * ldns_sha384(const unsigned char *data, unsigned int data_len, unsigned char *digest)
Convenience function to digest a fixed block of data at once.
Definition: sha2.c:991
ldns_sha512_CTX ldns_sha384_CTX
Definition: sha2.h:86
void ldns_sha512_update(ldns_sha512_CTX *, const uint8_t *, size_t)
Definition: sha2.c:815
#define LDNS_SHA512_BLOCK_LENGTH
Definition: sha2.h:68
void ldns_sha256_final(uint8_t[32], ldns_sha256_CTX *)
Definition: sha2.c:557
void ldns_sha384_init(ldns_sha384_CTX *)
Definition: sha2.c:948
unsigned char * ldns_sha256(const unsigned char *data, unsigned int data_len, unsigned char *digest)
Convenience function to digest a fixed block of data at once.
Definition: sha2.c:624
void ldns_sha512_init(ldns_sha512_CTX *)
Definition: sha2.c:634
#define LDNS_SHA512_DIGEST_LENGTH
Definition: sha2.h:69
#define LDNS_SHA256_BLOCK_LENGTH
Definition: sha2.h:62
unsigned char * ldns_sha512(const unsigned char *data, unsigned int data_len, unsigned char *digest)
Convenience function to digest a fixed block of data at once.
Definition: sha2.c:938
void ldns_sha512_final(uint8_t[64], ldns_sha512_CTX *)
Definition: sha2.c:908
#define LDNS_SHA384_DIGEST_LENGTH
Definition: sha2.h:66
void ldns_sha256_update(ldns_sha256_CTX *, const uint8_t *, size_t)
Definition: sha2.c:502
#define LDNS_SHA256_DIGEST_LENGTH
Definition: sha2.h:63
void ldns_sha384_update(ldns_sha384_CTX *, const uint8_t *, size_t)
Definition: sha2.c:957
struct _ldns_sha512_CTX ldns_sha512_CTX
struct _ldns_sha256_CTX ldns_sha256_CTX
void ldns_sha256_init(ldns_sha256_CTX *)
Definition: sha2.c:315
void ldns_sha384_final(uint8_t[48], ldns_sha384_CTX *)
Definition: sha2.c:961
uint32_t state[8]
Definition: sha2.h:76
uint64_t bitcount
Definition: sha2.h:77
uint8_t buffer[64]
Definition: sha2.h:78
uint8_t buffer[128]
Definition: sha2.h:83
uint64_t state[8]
Definition: sha2.h:81
uint64_t bitcount[2]
Definition: sha2.h:82