PostgreSQL Source Code  git master
internal-sha2.c
Go to the documentation of this file.
1 /*
2  * internal.c
3  * Wrapper for builtin functions
4  *
5  * Copyright (c) 2001 Marko Kreen
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * contrib/pgcrypto/internal-sha2.c
30  */
31 
32 #include "postgres.h"
33 
34 #include <time.h>
35 
36 #include "common/cryptohash.h"
37 #include "common/sha2.h"
38 #include "px.h"
39 
40 void init_sha224(PX_MD *h);
41 void init_sha256(PX_MD *h);
42 void init_sha384(PX_MD *h);
43 void init_sha512(PX_MD *h);
44 
45 /* SHA224 */
46 static unsigned
48 {
50 }
51 
52 static unsigned
54 {
56 }
57 
58 /* SHA256 */
59 static unsigned
61 {
63 }
64 
65 static unsigned
67 {
69 }
70 
71 /* SHA384 */
72 static unsigned
74 {
76 }
77 
78 static unsigned
80 {
82 }
83 
84 /* SHA512 */
85 static unsigned
87 {
89 }
90 
91 static unsigned
93 {
95 }
96 
97 /* Generic interface for all SHA2 methods */
98 static void
99 int_sha2_update(PX_MD *h, const uint8 *data, unsigned dlen)
100 {
102 
103  if (pg_cryptohash_update(ctx, data, dlen) < 0)
104  elog(ERROR, "could not update %s context", "SHA2");
105 }
106 
107 static void
109 {
111 
112  if (pg_cryptohash_init(ctx) < 0)
113  elog(ERROR, "could not initialize %s context", "SHA2");
114 }
115 
116 static void
118 {
120 
121  if (pg_cryptohash_final(ctx, dst, h->result_size(h)) < 0)
122  elog(ERROR, "could not finalize %s context", "SHA2");
123 }
124 
125 static void
127 {
129 
130  pg_cryptohash_free(ctx);
131  pfree(h);
132 }
133 
134 /* init functions */
135 
136 void
138 {
139  pg_cryptohash_ctx *ctx;
140 
142  md->p.ptr = ctx;
143 
146  md->reset = int_sha2_reset;
147  md->update = int_sha2_update;
148  md->finish = int_sha2_finish;
149  md->free = int_sha2_free;
150 
151  md->reset(md);
152 }
153 
154 void
156 {
157  pg_cryptohash_ctx *ctx;
158 
160  md->p.ptr = ctx;
161 
164  md->reset = int_sha2_reset;
165  md->update = int_sha2_update;
166  md->finish = int_sha2_finish;
167  md->free = int_sha2_free;
168 
169  md->reset(md);
170 }
171 
172 void
174 {
175  pg_cryptohash_ctx *ctx;
176 
178  md->p.ptr = ctx;
179 
182  md->reset = int_sha2_reset;
183  md->update = int_sha2_update;
184  md->finish = int_sha2_finish;
185  md->free = int_sha2_free;
186 
187  md->reset(md);
188 }
189 
190 void
192 {
193  pg_cryptohash_ctx *ctx;
194 
196  md->p.ptr = ctx;
197 
200  md->reset = int_sha2_reset;
201  md->update = int_sha2_update;
202  md->finish = int_sha2_finish;
203  md->free = int_sha2_free;
204 
205  md->reset(md);
206 }
static void int_sha2_reset(PX_MD *h)
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:168
void init_sha256(PX_MD *h)
void * ptr
Definition: px.h:110
#define PG_SHA384_BLOCK_LENGTH
Definition: sha2.h:25
void(* free)(PX_MD *h)
Definition: px.h:105
static unsigned int_sha512_len(PX_MD *h)
Definition: internal-sha2.c:86
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:23
unsigned char uint8
Definition: c.h:439
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:29
void pfree(void *pointer)
Definition: mcxt.c:1169
unsigned(* block_size)(PX_MD *h)
Definition: px.h:101
#define ERROR
Definition: elog.h:46
static unsigned int_sha384_block_len(PX_MD *h)
Definition: internal-sha2.c:79
#define PG_SHA256_BLOCK_LENGTH
Definition: sha2.h:22
union px_digest::@9 p
Definition: px.h:98
void(* update)(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: px.h:103
void(* finish)(PX_MD *h, uint8 *dst)
Definition: px.h:104
static unsigned int_sha256_len(PX_MD *h)
Definition: internal-sha2.c:60
#define PG_SHA512_BLOCK_LENGTH
Definition: sha2.h:28
static unsigned int_sha224_block_len(PX_MD *h)
Definition: internal-sha2.c:53
static void int_sha2_free(PX_MD *h)
static void int_sha2_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: internal-sha2.c:99
static unsigned int_sha512_block_len(PX_MD *h)
Definition: internal-sha2.c:92
unsigned(* result_size)(PX_MD *h)
Definition: px.h:100
#define PG_SHA224_BLOCK_LENGTH
Definition: sha2.h:19
static void int_sha2_finish(PX_MD *h, uint8 *dst)
void init_sha224(PX_MD *h)
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:66
void(* reset)(PX_MD *h)
Definition: px.h:102
static unsigned int_sha384_len(PX_MD *h)
Definition: internal-sha2.c:73
static unsigned int_sha256_block_len(PX_MD *h)
Definition: internal-sha2.c:66
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:92
static unsigned int_sha224_len(PX_MD *h)
Definition: internal-sha2.c:47
void init_sha384(PX_MD *h)
void init_sha512(PX_MD *h)
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:130
#define elog(elevel,...)
Definition: elog.h:232
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:216
#define PG_SHA224_DIGEST_LENGTH
Definition: sha2.h:20
#define PG_SHA384_DIGEST_LENGTH
Definition: sha2.h:26