LCOV - code coverage report
Current view: top level - src/backend/utils/adt - cryptohashes.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta2 Lines: 54 56 96.4 %
Date: 2019-06-19 14:06:47 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * cryptohashes.c
       4             :  *    Cryptographic hash functions
       5             :  *
       6             :  * Portions Copyright (c) 2018-2019, PostgreSQL Global Development Group
       7             :  *
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/utils/adt/cryptohashes.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #include "postgres.h"
      15             : 
      16             : #include "common/md5.h"
      17             : #include "common/sha2.h"
      18             : #include "utils/builtins.h"
      19             : 
      20             : 
      21             : /*
      22             :  * MD5
      23             :  */
      24             : 
      25             : /* MD5 produces a 16 byte (128 bit) hash; double it for hex */
      26             : #define MD5_HASH_LEN  32
      27             : 
      28             : /*
      29             :  * Create an MD5 hash of a text value and return it as hex string.
      30             :  */
      31             : Datum
      32       97602 : md5_text(PG_FUNCTION_ARGS)
      33             : {
      34       97602 :     text       *in_text = PG_GETARG_TEXT_PP(0);
      35             :     size_t      len;
      36             :     char        hexsum[MD5_HASH_LEN + 1];
      37             : 
      38             :     /* Calculate the length of the buffer using varlena metadata */
      39       97602 :     len = VARSIZE_ANY_EXHDR(in_text);
      40             : 
      41             :     /* get the hash result */
      42       97602 :     if (pg_md5_hash(VARDATA_ANY(in_text), len, hexsum) == false)
      43           0 :         ereport(ERROR,
      44             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
      45             :                  errmsg("out of memory")));
      46             : 
      47             :     /* convert to text and return it */
      48       97602 :     PG_RETURN_TEXT_P(cstring_to_text(hexsum));
      49             : }
      50             : 
      51             : /*
      52             :  * Create an MD5 hash of a bytea value and return it as a hex string.
      53             :  */
      54             : Datum
      55          36 : md5_bytea(PG_FUNCTION_ARGS)
      56             : {
      57          36 :     bytea      *in = PG_GETARG_BYTEA_PP(0);
      58             :     size_t      len;
      59             :     char        hexsum[MD5_HASH_LEN + 1];
      60             : 
      61          36 :     len = VARSIZE_ANY_EXHDR(in);
      62          36 :     if (pg_md5_hash(VARDATA_ANY(in), len, hexsum) == false)
      63           0 :         ereport(ERROR,
      64             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
      65             :                  errmsg("out of memory")));
      66             : 
      67          36 :     PG_RETURN_TEXT_P(cstring_to_text(hexsum));
      68             : }
      69             : 
      70             : 
      71             : /*
      72             :  * SHA-2 variants
      73             :  */
      74             : 
      75             : Datum
      76           8 : sha224_bytea(PG_FUNCTION_ARGS)
      77             : {
      78           8 :     bytea      *in = PG_GETARG_BYTEA_PP(0);
      79             :     const uint8 *data;
      80             :     size_t      len;
      81             :     pg_sha224_ctx ctx;
      82             :     unsigned char buf[PG_SHA224_DIGEST_LENGTH];
      83             :     bytea      *result;
      84             : 
      85           8 :     len = VARSIZE_ANY_EXHDR(in);
      86           8 :     data = (unsigned char *) VARDATA_ANY(in);
      87             : 
      88           8 :     pg_sha224_init(&ctx);
      89           8 :     pg_sha224_update(&ctx, data, len);
      90           8 :     pg_sha224_final(&ctx, buf);
      91             : 
      92           8 :     result = palloc(sizeof(buf) + VARHDRSZ);
      93           8 :     SET_VARSIZE(result, sizeof(buf) + VARHDRSZ);
      94           8 :     memcpy(VARDATA(result), buf, sizeof(buf));
      95             : 
      96           8 :     PG_RETURN_BYTEA_P(result);
      97             : }
      98             : 
      99             : Datum
     100           8 : sha256_bytea(PG_FUNCTION_ARGS)
     101             : {
     102           8 :     bytea      *in = PG_GETARG_BYTEA_PP(0);
     103             :     const uint8 *data;
     104             :     size_t      len;
     105             :     pg_sha256_ctx ctx;
     106             :     unsigned char buf[PG_SHA256_DIGEST_LENGTH];
     107             :     bytea      *result;
     108             : 
     109           8 :     len = VARSIZE_ANY_EXHDR(in);
     110           8 :     data = (unsigned char *) VARDATA_ANY(in);
     111             : 
     112           8 :     pg_sha256_init(&ctx);
     113           8 :     pg_sha256_update(&ctx, data, len);
     114           8 :     pg_sha256_final(&ctx, buf);
     115             : 
     116           8 :     result = palloc(sizeof(buf) + VARHDRSZ);
     117           8 :     SET_VARSIZE(result, sizeof(buf) + VARHDRSZ);
     118           8 :     memcpy(VARDATA(result), buf, sizeof(buf));
     119             : 
     120           8 :     PG_RETURN_BYTEA_P(result);
     121             : }
     122             : 
     123             : Datum
     124           8 : sha384_bytea(PG_FUNCTION_ARGS)
     125             : {
     126           8 :     bytea      *in = PG_GETARG_BYTEA_PP(0);
     127             :     const uint8 *data;
     128             :     size_t      len;
     129             :     pg_sha384_ctx ctx;
     130             :     unsigned char buf[PG_SHA384_DIGEST_LENGTH];
     131             :     bytea      *result;
     132             : 
     133           8 :     len = VARSIZE_ANY_EXHDR(in);
     134           8 :     data = (unsigned char *) VARDATA_ANY(in);
     135             : 
     136           8 :     pg_sha384_init(&ctx);
     137           8 :     pg_sha384_update(&ctx, data, len);
     138           8 :     pg_sha384_final(&ctx, buf);
     139             : 
     140           8 :     result = palloc(sizeof(buf) + VARHDRSZ);
     141           8 :     SET_VARSIZE(result, sizeof(buf) + VARHDRSZ);
     142           8 :     memcpy(VARDATA(result), buf, sizeof(buf));
     143             : 
     144           8 :     PG_RETURN_BYTEA_P(result);
     145             : }
     146             : 
     147             : Datum
     148           8 : sha512_bytea(PG_FUNCTION_ARGS)
     149             : {
     150           8 :     bytea      *in = PG_GETARG_BYTEA_PP(0);
     151             :     const uint8 *data;
     152             :     size_t      len;
     153             :     pg_sha512_ctx ctx;
     154             :     unsigned char buf[PG_SHA512_DIGEST_LENGTH];
     155             :     bytea      *result;
     156             : 
     157           8 :     len = VARSIZE_ANY_EXHDR(in);
     158           8 :     data = (unsigned char *) VARDATA_ANY(in);
     159             : 
     160           8 :     pg_sha512_init(&ctx);
     161           8 :     pg_sha512_update(&ctx, data, len);
     162           8 :     pg_sha512_final(&ctx, buf);
     163             : 
     164           8 :     result = palloc(sizeof(buf) + VARHDRSZ);
     165           8 :     SET_VARSIZE(result, sizeof(buf) + VARHDRSZ);
     166           8 :     memcpy(VARDATA(result), buf, sizeof(buf));
     167             : 
     168           8 :     PG_RETURN_BYTEA_P(result);
     169             : }

Generated by: LCOV version 1.13