PostgreSQL Source Code  git master
sha2.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * sha2.h
4  * Generic headers for SHA224, 256, 384 AND 512 functions of PostgreSQL.
5  *
6  * Portions Copyright (c) 2016-2017, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * src/include/common/sha2.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 
14 /* $OpenBSD: sha2.h,v 1.2 2004/04/28 23:11:57 millert Exp $ */
15 
16 /*
17  * FILE: sha2.h
18  * AUTHOR: Aaron D. Gifford <me@aarongifford.com>
19  *
20  * Copyright (c) 2000-2001, Aaron D. Gifford
21  * All rights reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the above copyright
27  * notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  * notice, this list of conditions and the following disclaimer in the
30  * documentation and/or other materials provided with the distribution.
31  * 3. Neither the name of the copyright holder nor the names of contributors
32  * may be used to endorse or promote products derived from this software
33  * without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
36  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
39  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  *
47  * $From: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
48  */
49 
50 #ifndef _PG_SHA2_H_
51 #define _PG_SHA2_H_
52 
53 #ifdef USE_SSL
54 #include <openssl/sha.h>
55 #endif
56 
57 /*** SHA224/256/384/512 Various Length Definitions ***********************/
58 #define PG_SHA224_BLOCK_LENGTH 64
59 #define PG_SHA224_DIGEST_LENGTH 28
60 #define PG_SHA224_DIGEST_STRING_LENGTH (PG_SHA224_DIGEST_LENGTH * 2 + 1)
61 #define PG_SHA256_BLOCK_LENGTH 64
62 #define PG_SHA256_DIGEST_LENGTH 32
63 #define PG_SHA256_DIGEST_STRING_LENGTH (PG_SHA256_DIGEST_LENGTH * 2 + 1)
64 #define PG_SHA384_BLOCK_LENGTH 128
65 #define PG_SHA384_DIGEST_LENGTH 48
66 #define PG_SHA384_DIGEST_STRING_LENGTH (PG_SHA384_DIGEST_LENGTH * 2 + 1)
67 #define PG_SHA512_BLOCK_LENGTH 128
68 #define PG_SHA512_DIGEST_LENGTH 64
69 #define PG_SHA512_DIGEST_STRING_LENGTH (PG_SHA512_DIGEST_LENGTH * 2 + 1)
70 
71 /* Context Structures for SHA-1/224/256/384/512 */
72 #ifdef USE_SSL
73 typedef SHA256_CTX pg_sha256_ctx;
74 typedef SHA512_CTX pg_sha512_ctx;
75 typedef SHA256_CTX pg_sha224_ctx;
76 typedef SHA512_CTX pg_sha384_ctx;
77 #else
78 typedef struct pg_sha256_ctx
79 {
81  uint64 bitcount;
84 typedef struct pg_sha512_ctx
85 {
86  uint64 state[8];
87  uint64 bitcount[2];
92 #endif /* USE_SSL */
93 
94 /* Interface routines for SHA224/256/384/512 */
95 extern void pg_sha224_init(pg_sha224_ctx *ctx);
96 extern void pg_sha224_update(pg_sha224_ctx *ctx, const uint8 *input0,
97  size_t len);
98 extern void pg_sha224_final(pg_sha224_ctx *ctx, uint8 *dest);
99 
100 extern void pg_sha256_init(pg_sha256_ctx *ctx);
101 extern void pg_sha256_update(pg_sha256_ctx *ctx, const uint8 *input0,
102  size_t len);
103 extern void pg_sha256_final(pg_sha256_ctx *ctx, uint8 *dest);
104 
105 extern void pg_sha384_init(pg_sha384_ctx *ctx);
106 extern void pg_sha384_update(pg_sha384_ctx *ctx,
107  const uint8 *, size_t len);
108 extern void pg_sha384_final(pg_sha384_ctx *ctx, uint8 *dest);
109 
110 extern void pg_sha512_init(pg_sha512_ctx *ctx);
111 extern void pg_sha512_update(pg_sha512_ctx *ctx, const uint8 *input0,
112  size_t len);
113 extern void pg_sha512_final(pg_sha512_ctx *ctx, uint8 *dest);
114 
115 #endif /* _PG_SHA2_H_ */
uint64 bitcount
Definition: sha2.h:81
struct pg_sha256_ctx pg_sha224_ctx
Definition: sha2.h:90
void pg_sha224_final(pg_sha224_ctx *ctx, uint8 *dest)
Definition: sha2.c:983
struct pg_sha512_ctx pg_sha512_ctx
void pg_sha224_update(pg_sha224_ctx *ctx, const uint8 *input0, size_t len)
Definition: sha2.c:977
struct pg_sha512_ctx pg_sha384_ctx
Definition: sha2.h:91
uint8 buffer[PG_SHA256_BLOCK_LENGTH]
Definition: sha2.h:82
void pg_sha384_update(pg_sha384_ctx *ctx, const uint8 *, size_t len)
Definition: sha2.c:933
unsigned char uint8
Definition: c.h:294
void pg_sha256_init(pg_sha256_ctx *ctx)
Definition: sha2.c:268
void pg_sha384_init(pg_sha384_ctx *ctx)
Definition: sha2.c:923
#define PG_SHA256_BLOCK_LENGTH
Definition: sha2.h:61
void pg_sha224_init(pg_sha224_ctx *ctx)
Definition: sha2.c:967
#define PG_SHA512_BLOCK_LENGTH
Definition: sha2.h:67
unsigned int uint32
Definition: c.h:296
void pg_sha384_final(pg_sha384_ctx *ctx, uint8 *dest)
Definition: sha2.c:939
void pg_sha512_final(pg_sha512_ctx *ctx, uint8 *dest)
Definition: sha2.c:894
void pg_sha256_update(pg_sha256_ctx *ctx, const uint8 *input0, size_t len)
Definition: sha2.c:465
Definition: regguts.h:298
void pg_sha512_update(pg_sha512_ctx *ctx, const uint8 *input0, size_t len)
Definition: sha2.c:791
void pg_sha512_init(pg_sha512_ctx *ctx)
Definition: sha2.c:594
void pg_sha256_final(pg_sha256_ctx *ctx, uint8 *dest)
Definition: sha2.c:566
struct pg_sha256_ctx pg_sha256_ctx