Header And Logo

PostgreSQL
| The world's most advanced open source database.

internal-sha2.c

Go to the documentation of this file.
00001 /*
00002  * internal.c
00003  *      Wrapper for builtin functions
00004  *
00005  * Copyright (c) 2001 Marko Kreen
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * contrib/pgcrypto/internal-sha2.c
00030  */
00031 
00032 #include "postgres.h"
00033 
00034 #include <time.h>
00035 
00036 #include "px.h"
00037 #include "sha2.h"
00038 
00039 void        init_sha224(PX_MD *h);
00040 void        init_sha256(PX_MD *h);
00041 void        init_sha384(PX_MD *h);
00042 void        init_sha512(PX_MD *h);
00043 
00044 /* SHA224 */
00045 
00046 static unsigned
00047 int_sha224_len(PX_MD *h)
00048 {
00049     return SHA224_DIGEST_LENGTH;
00050 }
00051 
00052 static unsigned
00053 int_sha224_block_len(PX_MD *h)
00054 {
00055     return SHA224_BLOCK_LENGTH;
00056 }
00057 
00058 static void
00059 int_sha224_update(PX_MD *h, const uint8 *data, unsigned dlen)
00060 {
00061     SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
00062 
00063     SHA224_Update(ctx, data, dlen);
00064 }
00065 
00066 static void
00067 int_sha224_reset(PX_MD *h)
00068 {
00069     SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
00070 
00071     SHA224_Init(ctx);
00072 }
00073 
00074 static void
00075 int_sha224_finish(PX_MD *h, uint8 *dst)
00076 {
00077     SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
00078 
00079     SHA224_Final(dst, ctx);
00080 }
00081 
00082 static void
00083 int_sha224_free(PX_MD *h)
00084 {
00085     SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
00086 
00087     memset(ctx, 0, sizeof(*ctx));
00088     px_free(ctx);
00089     px_free(h);
00090 }
00091 
00092 /* SHA256 */
00093 
00094 static unsigned
00095 int_sha256_len(PX_MD *h)
00096 {
00097     return SHA256_DIGEST_LENGTH;
00098 }
00099 
00100 static unsigned
00101 int_sha256_block_len(PX_MD *h)
00102 {
00103     return SHA256_BLOCK_LENGTH;
00104 }
00105 
00106 static void
00107 int_sha256_update(PX_MD *h, const uint8 *data, unsigned dlen)
00108 {
00109     SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
00110 
00111     SHA256_Update(ctx, data, dlen);
00112 }
00113 
00114 static void
00115 int_sha256_reset(PX_MD *h)
00116 {
00117     SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
00118 
00119     SHA256_Init(ctx);
00120 }
00121 
00122 static void
00123 int_sha256_finish(PX_MD *h, uint8 *dst)
00124 {
00125     SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
00126 
00127     SHA256_Final(dst, ctx);
00128 }
00129 
00130 static void
00131 int_sha256_free(PX_MD *h)
00132 {
00133     SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
00134 
00135     memset(ctx, 0, sizeof(*ctx));
00136     px_free(ctx);
00137     px_free(h);
00138 }
00139 
00140 /* SHA384 */
00141 
00142 static unsigned
00143 int_sha384_len(PX_MD *h)
00144 {
00145     return SHA384_DIGEST_LENGTH;
00146 }
00147 
00148 static unsigned
00149 int_sha384_block_len(PX_MD *h)
00150 {
00151     return SHA384_BLOCK_LENGTH;
00152 }
00153 
00154 static void
00155 int_sha384_update(PX_MD *h, const uint8 *data, unsigned dlen)
00156 {
00157     SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
00158 
00159     SHA384_Update(ctx, data, dlen);
00160 }
00161 
00162 static void
00163 int_sha384_reset(PX_MD *h)
00164 {
00165     SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
00166 
00167     SHA384_Init(ctx);
00168 }
00169 
00170 static void
00171 int_sha384_finish(PX_MD *h, uint8 *dst)
00172 {
00173     SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
00174 
00175     SHA384_Final(dst, ctx);
00176 }
00177 
00178 static void
00179 int_sha384_free(PX_MD *h)
00180 {
00181     SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
00182 
00183     memset(ctx, 0, sizeof(*ctx));
00184     px_free(ctx);
00185     px_free(h);
00186 }
00187 
00188 /* SHA512 */
00189 
00190 static unsigned
00191 int_sha512_len(PX_MD *h)
00192 {
00193     return SHA512_DIGEST_LENGTH;
00194 }
00195 
00196 static unsigned
00197 int_sha512_block_len(PX_MD *h)
00198 {
00199     return SHA512_BLOCK_LENGTH;
00200 }
00201 
00202 static void
00203 int_sha512_update(PX_MD *h, const uint8 *data, unsigned dlen)
00204 {
00205     SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
00206 
00207     SHA512_Update(ctx, data, dlen);
00208 }
00209 
00210 static void
00211 int_sha512_reset(PX_MD *h)
00212 {
00213     SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
00214 
00215     SHA512_Init(ctx);
00216 }
00217 
00218 static void
00219 int_sha512_finish(PX_MD *h, uint8 *dst)
00220 {
00221     SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
00222 
00223     SHA512_Final(dst, ctx);
00224 }
00225 
00226 static void
00227 int_sha512_free(PX_MD *h)
00228 {
00229     SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
00230 
00231     memset(ctx, 0, sizeof(*ctx));
00232     px_free(ctx);
00233     px_free(h);
00234 }
00235 
00236 /* init functions */
00237 
00238 void
00239 init_sha224(PX_MD *md)
00240 {
00241     SHA224_CTX *ctx;
00242 
00243     ctx = px_alloc(sizeof(*ctx));
00244     memset(ctx, 0, sizeof(*ctx));
00245 
00246     md->p.ptr = ctx;
00247 
00248     md->result_size = int_sha224_len;
00249     md->block_size = int_sha224_block_len;
00250     md->reset = int_sha224_reset;
00251     md->update = int_sha224_update;
00252     md->finish = int_sha224_finish;
00253     md->free = int_sha224_free;
00254 
00255     md->reset(md);
00256 }
00257 
00258 void
00259 init_sha256(PX_MD *md)
00260 {
00261     SHA256_CTX *ctx;
00262 
00263     ctx = px_alloc(sizeof(*ctx));
00264     memset(ctx, 0, sizeof(*ctx));
00265 
00266     md->p.ptr = ctx;
00267 
00268     md->result_size = int_sha256_len;
00269     md->block_size = int_sha256_block_len;
00270     md->reset = int_sha256_reset;
00271     md->update = int_sha256_update;
00272     md->finish = int_sha256_finish;
00273     md->free = int_sha256_free;
00274 
00275     md->reset(md);
00276 }
00277 
00278 void
00279 init_sha384(PX_MD *md)
00280 {
00281     SHA384_CTX *ctx;
00282 
00283     ctx = px_alloc(sizeof(*ctx));
00284     memset(ctx, 0, sizeof(*ctx));
00285 
00286     md->p.ptr = ctx;
00287 
00288     md->result_size = int_sha384_len;
00289     md->block_size = int_sha384_block_len;
00290     md->reset = int_sha384_reset;
00291     md->update = int_sha384_update;
00292     md->finish = int_sha384_finish;
00293     md->free = int_sha384_free;
00294 
00295     md->reset(md);
00296 }
00297 
00298 void
00299 init_sha512(PX_MD *md)
00300 {
00301     SHA512_CTX *ctx;
00302 
00303     ctx = px_alloc(sizeof(*ctx));
00304     memset(ctx, 0, sizeof(*ctx));
00305 
00306     md->p.ptr = ctx;
00307 
00308     md->result_size = int_sha512_len;
00309     md->block_size = int_sha512_block_len;
00310     md->reset = int_sha512_reset;
00311     md->update = int_sha512_update;
00312     md->finish = int_sha512_finish;
00313     md->free = int_sha512_free;
00314 
00315     md->reset(md);
00316 }