1 /*-------------------------------------------------------------------------
2 *
3 * sha2_openssl.c
4 * Set of wrapper routines on top of OpenSSL to support SHA-224
5 * SHA-256, SHA-384 and SHA-512 functions.
6 *
7 * This should only be used if code is compiled with OpenSSL support.
8 *
9 * Portions Copyright (c) 2016-2017, PostgreSQL Global Development Group
10 *
11 * IDENTIFICATION
12 * src/common/sha2_openssl.c
13 *
14 *-------------------------------------------------------------------------
15 */
16
17 #ifndef FRONTEND
18 #include "postgres.h"
19 #else
20 #include "postgres_fe.h"
21 #endif
22
23 #include <openssl/sha.h>
24
25 #include "common/sha2.h"
26
27
28 /* Interface routines for SHA-256 */
29 void
pg_sha256_init(pg_sha256_ctx * ctx)30 pg_sha256_init(pg_sha256_ctx *ctx)
31 {
32 SHA256_Init((SHA256_CTX *) ctx);
33 }
34
35 void
pg_sha256_update(pg_sha256_ctx * ctx,const uint8 * data,size_t len)36 pg_sha256_update(pg_sha256_ctx *ctx, const uint8 *data, size_t len)
37 {
38 SHA256_Update((SHA256_CTX *) ctx, data, len);
39 }
40
41 void
pg_sha256_final(pg_sha256_ctx * ctx,uint8 * dest)42 pg_sha256_final(pg_sha256_ctx *ctx, uint8 *dest)
43 {
44 SHA256_Final(dest, (SHA256_CTX *) ctx);
45 }
46
47 /* Interface routines for SHA-512 */
48 void
pg_sha512_init(pg_sha512_ctx * ctx)49 pg_sha512_init(pg_sha512_ctx *ctx)
50 {
51 SHA512_Init((SHA512_CTX *) ctx);
52 }
53
54 void
pg_sha512_update(pg_sha512_ctx * ctx,const uint8 * data,size_t len)55 pg_sha512_update(pg_sha512_ctx *ctx, const uint8 *data, size_t len)
56 {
57 SHA512_Update((SHA512_CTX *) ctx, data, len);
58 }
59
60 void
pg_sha512_final(pg_sha512_ctx * ctx,uint8 * dest)61 pg_sha512_final(pg_sha512_ctx *ctx, uint8 *dest)
62 {
63 SHA512_Final(dest, (SHA512_CTX *) ctx);
64 }
65
66 /* Interface routines for SHA-384 */
67 void
pg_sha384_init(pg_sha384_ctx * ctx)68 pg_sha384_init(pg_sha384_ctx *ctx)
69 {
70 SHA384_Init((SHA512_CTX *) ctx);
71 }
72
73 void
pg_sha384_update(pg_sha384_ctx * ctx,const uint8 * data,size_t len)74 pg_sha384_update(pg_sha384_ctx *ctx, const uint8 *data, size_t len)
75 {
76 SHA384_Update((SHA512_CTX *) ctx, data, len);
77 }
78
79 void
pg_sha384_final(pg_sha384_ctx * ctx,uint8 * dest)80 pg_sha384_final(pg_sha384_ctx *ctx, uint8 *dest)
81 {
82 SHA384_Final(dest, (SHA512_CTX *) ctx);
83 }
84
85 /* Interface routines for SHA-224 */
86 void
pg_sha224_init(pg_sha224_ctx * ctx)87 pg_sha224_init(pg_sha224_ctx *ctx)
88 {
89 SHA224_Init((SHA256_CTX *) ctx);
90 }
91
92 void
pg_sha224_update(pg_sha224_ctx * ctx,const uint8 * data,size_t len)93 pg_sha224_update(pg_sha224_ctx *ctx, const uint8 *data, size_t len)
94 {
95 SHA224_Update((SHA256_CTX *) ctx, data, len);
96 }
97
98 void
pg_sha224_final(pg_sha224_ctx * ctx,uint8 * dest)99 pg_sha224_final(pg_sha224_ctx *ctx, uint8 *dest)
100 {
101 SHA224_Final(dest, (SHA256_CTX *) ctx);
102 }
103