PostgreSQL Source Code  git master
sha2_int.h File Reference
#include "common/sha2.h"
Include dependency graph for sha2_int.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pg_sha256_ctx
 
struct  pg_sha512_ctx
 

Typedefs

typedef struct pg_sha256_ctx pg_sha256_ctx
 
typedef struct pg_sha512_ctx pg_sha512_ctx
 
typedef struct pg_sha256_ctx pg_sha224_ctx
 
typedef struct pg_sha512_ctx pg_sha384_ctx
 

Functions

void pg_sha224_init (pg_sha224_ctx *ctx)
 
void pg_sha224_update (pg_sha224_ctx *ctx, const uint8 *input0, size_t len)
 
void pg_sha224_final (pg_sha224_ctx *ctx, uint8 *dest)
 
void pg_sha256_init (pg_sha256_ctx *ctx)
 
void pg_sha256_update (pg_sha256_ctx *ctx, const uint8 *input0, size_t len)
 
void pg_sha256_final (pg_sha256_ctx *ctx, uint8 *dest)
 
void pg_sha384_init (pg_sha384_ctx *ctx)
 
void pg_sha384_update (pg_sha384_ctx *ctx, const uint8 *, size_t len)
 
void pg_sha384_final (pg_sha384_ctx *ctx, uint8 *dest)
 
void pg_sha512_init (pg_sha512_ctx *ctx)
 
void pg_sha512_update (pg_sha512_ctx *ctx, const uint8 *input0, size_t len)
 
void pg_sha512_final (pg_sha512_ctx *ctx, uint8 *dest)
 

Typedef Documentation

◆ pg_sha224_ctx

typedef struct pg_sha256_ctx pg_sha224_ctx

Definition at line 67 of file sha2_int.h.

◆ pg_sha256_ctx

typedef struct pg_sha256_ctx pg_sha256_ctx

◆ pg_sha384_ctx

typedef struct pg_sha512_ctx pg_sha384_ctx

Definition at line 68 of file sha2_int.h.

◆ pg_sha512_ctx

typedef struct pg_sha512_ctx pg_sha512_ctx

Function Documentation

◆ pg_sha224_final()

void pg_sha224_final ( pg_sha224_ctx ctx,
uint8 dest 
)

Definition at line 994 of file sha2.c.

References PG_SHA224_DIGEST_LENGTH, REVERSE32, SHA256_Last(), and pg_sha256_ctx::state.

Referenced by pg_cryptohash_final().

995 {
996  /* If no digest buffer is passed, we don't bother doing this: */
997  if (digest != NULL)
998  {
999  SHA256_Last(context);
1000 
1001 #ifndef WORDS_BIGENDIAN
1002  {
1003  /* Convert TO host byte order */
1004  int j;
1005 
1006  for (j = 0; j < 8; j++)
1007  {
1008  REVERSE32(context->state[j], context->state[j]);
1009  }
1010  }
1011 #endif
1012  memcpy(digest, context->state, PG_SHA224_DIGEST_LENGTH);
1013  }
1014 
1015  /* Clean up state data: */
1016  memset(context, 0, sizeof(pg_sha224_ctx));
1017 }
static void SHA256_Last(pg_sha256_ctx *context)
Definition: sha2.c:529
#define PG_SHA224_DIGEST_LENGTH
Definition: sha2.h:20
#define REVERSE32(w, x)
Definition: sha2.c:95

◆ pg_sha224_init()

void pg_sha224_init ( pg_sha224_ctx ctx)

Definition at line 978 of file sha2.c.

References pg_sha256_ctx::bitcount, pg_sha256_ctx::buffer, PG_SHA256_BLOCK_LENGTH, PG_SHA256_DIGEST_LENGTH, sha224_initial_hash_value, and pg_sha256_ctx::state.

Referenced by pg_cryptohash_init().

979 {
980  if (context == NULL)
981  return;
982  memcpy(context->state, sha224_initial_hash_value, PG_SHA256_DIGEST_LENGTH);
983  memset(context->buffer, 0, PG_SHA256_BLOCK_LENGTH);
984  context->bitcount = 0;
985 }
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:23
#define PG_SHA256_BLOCK_LENGTH
Definition: sha2.h:22
static const uint32 sha224_initial_hash_value[8]
Definition: sha2.c:185

◆ pg_sha224_update()

void pg_sha224_update ( pg_sha224_ctx ctx,
const uint8 input0,
size_t  len 
)

Definition at line 988 of file sha2.c.

References pg_sha256_update().

Referenced by pg_cryptohash_update().

989 {
990  pg_sha256_update((pg_sha256_ctx *) context, data, len);
991 }
void pg_sha256_update(pg_sha256_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:476

◆ pg_sha256_final()

void pg_sha256_final ( pg_sha256_ctx ctx,
uint8 dest 
)

Definition at line 577 of file sha2.c.

References PG_SHA256_DIGEST_LENGTH, REVERSE32, SHA256_Last(), and pg_sha256_ctx::state.

Referenced by pg_cryptohash_final().

578 {
579  /* If no digest buffer is passed, we don't bother doing this: */
580  if (digest != NULL)
581  {
582  SHA256_Last(context);
583 
584 #ifndef WORDS_BIGENDIAN
585  {
586  /* Convert TO host byte order */
587  int j;
588 
589  for (j = 0; j < 8; j++)
590  {
591  REVERSE32(context->state[j], context->state[j]);
592  }
593  }
594 #endif
595  memcpy(digest, context->state, PG_SHA256_DIGEST_LENGTH);
596  }
597 
598  /* Clean up state data: */
599  memset(context, 0, sizeof(pg_sha256_ctx));
600 }
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:23
static void SHA256_Last(pg_sha256_ctx *context)
Definition: sha2.c:529
#define REVERSE32(w, x)
Definition: sha2.c:95

◆ pg_sha256_init()

void pg_sha256_init ( pg_sha256_ctx ctx)

Definition at line 279 of file sha2.c.

References pg_sha256_ctx::bitcount, pg_sha256_ctx::buffer, PG_SHA256_BLOCK_LENGTH, PG_SHA256_DIGEST_LENGTH, s1, sha256_initial_hash_value, SHA256_Transform(), and pg_sha256_ctx::state.

Referenced by pg_cryptohash_init().

280 {
281  if (context == NULL)
282  return;
283  memcpy(context->state, sha256_initial_hash_value, PG_SHA256_DIGEST_LENGTH);
284  memset(context->buffer, 0, PG_SHA256_BLOCK_LENGTH);
285  context->bitcount = 0;
286 }
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:23
static const uint32 sha256_initial_hash_value[8]
Definition: sha2.c:197
#define PG_SHA256_BLOCK_LENGTH
Definition: sha2.h:22

◆ pg_sha256_update()

void pg_sha256_update ( pg_sha256_ctx ctx,
const uint8 input0,
size_t  len 
)

Definition at line 476 of file sha2.c.

References pg_sha256_ctx::bitcount, pg_sha256_ctx::buffer, PG_SHA256_BLOCK_LENGTH, and SHA256_Transform().

Referenced by pg_cryptohash_update(), and pg_sha224_update().

477 {
478  size_t freespace,
479  usedspace;
480 
481  /* Calling with no data is valid (we do nothing) */
482  if (len == 0)
483  return;
484 
485  usedspace = (context->bitcount >> 3) % PG_SHA256_BLOCK_LENGTH;
486  if (usedspace > 0)
487  {
488  /* Calculate how much free space is available in the buffer */
489  freespace = PG_SHA256_BLOCK_LENGTH - usedspace;
490 
491  if (len >= freespace)
492  {
493  /* Fill the buffer completely and process it */
494  memcpy(&context->buffer[usedspace], data, freespace);
495  context->bitcount += freespace << 3;
496  len -= freespace;
497  data += freespace;
498  SHA256_Transform(context, context->buffer);
499  }
500  else
501  {
502  /* The buffer is not yet full */
503  memcpy(&context->buffer[usedspace], data, len);
504  context->bitcount += len << 3;
505  /* Clean up: */
506  usedspace = freespace = 0;
507  return;
508  }
509  }
510  while (len >= PG_SHA256_BLOCK_LENGTH)
511  {
512  /* Process as many complete blocks as we can */
513  SHA256_Transform(context, data);
514  context->bitcount += PG_SHA256_BLOCK_LENGTH << 3;
515  len -= PG_SHA256_BLOCK_LENGTH;
516  data += PG_SHA256_BLOCK_LENGTH;
517  }
518  if (len > 0)
519  {
520  /* There's left-overs, so save 'em */
521  memcpy(context->buffer, data, len);
522  context->bitcount += len << 3;
523  }
524  /* Clean up: */
525  usedspace = freespace = 0;
526 }
static void SHA256_Transform(pg_sha256_ctx *context, const uint8 *data)
Definition: sha2.c:386
#define PG_SHA256_BLOCK_LENGTH
Definition: sha2.h:22

◆ pg_sha384_final()

void pg_sha384_final ( pg_sha384_ctx ctx,
uint8 dest 
)

Definition at line 950 of file sha2.c.

References PG_SHA384_DIGEST_LENGTH, REVERSE64, SHA512_Last(), and pg_sha512_ctx::state.

Referenced by pg_cryptohash_final().

951 {
952  /* If no digest buffer is passed, we don't bother doing this: */
953  if (digest != NULL)
954  {
955  SHA512_Last((pg_sha512_ctx *) context);
956 
957  /* Save the hash data for output: */
958 #ifndef WORDS_BIGENDIAN
959  {
960  /* Convert TO host byte order */
961  int j;
962 
963  for (j = 0; j < 6; j++)
964  {
965  REVERSE64(context->state[j], context->state[j]);
966  }
967  }
968 #endif
969  memcpy(digest, context->state, PG_SHA384_DIGEST_LENGTH);
970  }
971 
972  /* Zero out state data */
973  memset(context, 0, sizeof(pg_sha384_ctx));
974 }
static void SHA512_Last(pg_sha512_ctx *context)
Definition: sha2.c:855
#define REVERSE64(w, x)
Definition: sha2.c:100
#define PG_SHA384_DIGEST_LENGTH
Definition: sha2.h:26

◆ pg_sha384_init()

void pg_sha384_init ( pg_sha384_ctx ctx)

Definition at line 934 of file sha2.c.

References pg_sha512_ctx::bitcount, pg_sha512_ctx::buffer, PG_SHA384_BLOCK_LENGTH, PG_SHA512_DIGEST_LENGTH, sha384_initial_hash_value, and pg_sha512_ctx::state.

Referenced by pg_cryptohash_init().

935 {
936  if (context == NULL)
937  return;
938  memcpy(context->state, sha384_initial_hash_value, PG_SHA512_DIGEST_LENGTH);
939  memset(context->buffer, 0, PG_SHA384_BLOCK_LENGTH);
940  context->bitcount[0] = context->bitcount[1] = 0;
941 }
#define PG_SHA384_BLOCK_LENGTH
Definition: sha2.h:25
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:29
static const uint64 sha384_initial_hash_value[8]
Definition: sha2.c:253

◆ pg_sha384_update()

void pg_sha384_update ( pg_sha384_ctx ctx,
const uint8 ,
size_t  len 
)

Definition at line 944 of file sha2.c.

References pg_sha512_update().

Referenced by pg_cryptohash_update().

945 {
946  pg_sha512_update((pg_sha512_ctx *) context, data, len);
947 }
void pg_sha512_update(pg_sha512_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:802

◆ pg_sha512_final()

void pg_sha512_final ( pg_sha512_ctx ctx,
uint8 dest 
)

Definition at line 905 of file sha2.c.

References PG_SHA512_DIGEST_LENGTH, REVERSE64, SHA512_Last(), and pg_sha512_ctx::state.

Referenced by pg_cryptohash_final().

906 {
907  /* If no digest buffer is passed, we don't bother doing this: */
908  if (digest != NULL)
909  {
910  SHA512_Last(context);
911 
912  /* Save the hash data for output: */
913 #ifndef WORDS_BIGENDIAN
914  {
915  /* Convert TO host byte order */
916  int j;
917 
918  for (j = 0; j < 8; j++)
919  {
920  REVERSE64(context->state[j], context->state[j]);
921  }
922  }
923 #endif
924  memcpy(digest, context->state, PG_SHA512_DIGEST_LENGTH);
925  }
926 
927  /* Zero out state data */
928  memset(context, 0, sizeof(pg_sha512_ctx));
929 }
static void SHA512_Last(pg_sha512_ctx *context)
Definition: sha2.c:855
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:29
#define REVERSE64(w, x)
Definition: sha2.c:100

◆ pg_sha512_init()

void pg_sha512_init ( pg_sha512_ctx ctx)

Definition at line 605 of file sha2.c.

References pg_sha512_ctx::bitcount, pg_sha512_ctx::buffer, PG_SHA512_BLOCK_LENGTH, PG_SHA512_DIGEST_LENGTH, s1, sha512_initial_hash_value, SHA512_Transform(), and pg_sha512_ctx::state.

Referenced by pg_cryptohash_init().

606 {
607  if (context == NULL)
608  return;
609  memcpy(context->state, sha512_initial_hash_value, PG_SHA512_DIGEST_LENGTH);
610  memset(context->buffer, 0, PG_SHA512_BLOCK_LENGTH);
611  context->bitcount[0] = context->bitcount[1] = 0;
612 }
static const uint64 sha512_initial_hash_value[8]
Definition: sha2.c:265
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:29
#define PG_SHA512_BLOCK_LENGTH
Definition: sha2.h:28

◆ pg_sha512_update()

void pg_sha512_update ( pg_sha512_ctx ctx,
const uint8 input0,
size_t  len 
)

Definition at line 802 of file sha2.c.

References ADDINC128, pg_sha512_ctx::bitcount, pg_sha512_ctx::buffer, PG_SHA512_BLOCK_LENGTH, and SHA512_Transform().

Referenced by pg_cryptohash_update(), and pg_sha384_update().

803 {
804  size_t freespace,
805  usedspace;
806 
807  /* Calling with no data is valid (we do nothing) */
808  if (len == 0)
809  return;
810 
811  usedspace = (context->bitcount[0] >> 3) % PG_SHA512_BLOCK_LENGTH;
812  if (usedspace > 0)
813  {
814  /* Calculate how much free space is available in the buffer */
815  freespace = PG_SHA512_BLOCK_LENGTH - usedspace;
816 
817  if (len >= freespace)
818  {
819  /* Fill the buffer completely and process it */
820  memcpy(&context->buffer[usedspace], data, freespace);
821  ADDINC128(context->bitcount, freespace << 3);
822  len -= freespace;
823  data += freespace;
824  SHA512_Transform(context, context->buffer);
825  }
826  else
827  {
828  /* The buffer is not yet full */
829  memcpy(&context->buffer[usedspace], data, len);
830  ADDINC128(context->bitcount, len << 3);
831  /* Clean up: */
832  usedspace = freespace = 0;
833  return;
834  }
835  }
836  while (len >= PG_SHA512_BLOCK_LENGTH)
837  {
838  /* Process as many complete blocks as we can */
839  SHA512_Transform(context, data);
840  ADDINC128(context->bitcount, PG_SHA512_BLOCK_LENGTH << 3);
841  len -= PG_SHA512_BLOCK_LENGTH;
842  data += PG_SHA512_BLOCK_LENGTH;
843  }
844  if (len > 0)
845  {
846  /* There's left-overs, so save 'em */
847  memcpy(context->buffer, data, len);
848  ADDINC128(context->bitcount, len << 3);
849  }
850  /* Clean up: */
851  usedspace = freespace = 0;
852 }
#define PG_SHA512_BLOCK_LENGTH
Definition: sha2.h:28
#define ADDINC128(w, n)
Definition: sha2.c:115
static void SHA512_Transform(pg_sha512_ctx *context, const uint8 *data)
Definition: sha2.c:712