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 1 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 1 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.

995 {
996  /* If no digest buffer is passed, we don't bother doing this: */
997  if (digest != NULL)
998  {
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 }
int j
Definition: isn.c:73
tree context
Definition: radixtree.h:1835
#define REVERSE32(w, x)
Definition: sha2.c:95
static void SHA256_Last(pg_sha256_ctx *context)
Definition: sha2.c:529
#define PG_SHA224_DIGEST_LENGTH
Definition: sha2.h:20

References context, j, PG_SHA224_DIGEST_LENGTH, REVERSE32, and SHA256_Last().

Referenced by pg_cryptohash_final().

◆ pg_sha224_init()

void pg_sha224_init ( pg_sha224_ctx ctx)

Definition at line 978 of file sha2.c.

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

References context, PG_SHA256_BLOCK_LENGTH, PG_SHA256_DIGEST_LENGTH, and sha224_initial_hash_value.

Referenced by pg_cryptohash_init().

◆ 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.

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

References context, data, len, and pg_sha256_update().

Referenced by pg_cryptohash_update().

◆ pg_sha256_final()

void pg_sha256_final ( pg_sha256_ctx ctx,
uint8 dest 
)

Definition at line 577 of file sha2.c.

578 {
579  /* If no digest buffer is passed, we don't bother doing this: */
580  if (digest != NULL)
581  {
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 }

References context, j, PG_SHA256_DIGEST_LENGTH, REVERSE32, and SHA256_Last().

Referenced by pg_cryptohash_final().

◆ pg_sha256_init()

void pg_sha256_init ( pg_sha256_ctx ctx)

Definition at line 279 of file sha2.c.

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

References context, PG_SHA256_BLOCK_LENGTH, PG_SHA256_DIGEST_LENGTH, and sha256_initial_hash_value.

Referenced by pg_cryptohash_init().

◆ 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.

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 */
514  context->bitcount += PG_SHA256_BLOCK_LENGTH << 3;
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

References context, data, len, PG_SHA256_BLOCK_LENGTH, and SHA256_Transform().

Referenced by pg_cryptohash_update(), and pg_sha224_update().

◆ pg_sha384_final()

void pg_sha384_final ( pg_sha384_ctx ctx,
uint8 dest 
)

Definition at line 950 of file sha2.c.

951 {
952  /* If no digest buffer is passed, we don't bother doing this: */
953  if (digest != NULL)
954  {
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 }
#define REVERSE64(w, x)
Definition: sha2.c:100
static void SHA512_Last(pg_sha512_ctx *context)
Definition: sha2.c:855
#define PG_SHA384_DIGEST_LENGTH
Definition: sha2.h:26

References context, j, PG_SHA384_DIGEST_LENGTH, REVERSE64, and SHA512_Last().

Referenced by pg_cryptohash_final().

◆ pg_sha384_init()

void pg_sha384_init ( pg_sha384_ctx ctx)

Definition at line 934 of file sha2.c.

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

References context, PG_SHA384_BLOCK_LENGTH, PG_SHA512_DIGEST_LENGTH, and sha384_initial_hash_value.

Referenced by pg_cryptohash_init().

◆ pg_sha384_update()

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

Definition at line 944 of file sha2.c.

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

References context, data, len, and pg_sha512_update().

Referenced by pg_cryptohash_update().

◆ pg_sha512_final()

void pg_sha512_final ( pg_sha512_ctx ctx,
uint8 dest 
)

Definition at line 905 of file sha2.c.

906 {
907  /* If no digest buffer is passed, we don't bother doing this: */
908  if (digest != NULL)
909  {
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 }

References context, j, PG_SHA512_DIGEST_LENGTH, REVERSE64, and SHA512_Last().

Referenced by pg_cryptohash_final().

◆ pg_sha512_init()

void pg_sha512_init ( pg_sha512_ctx ctx)

Definition at line 605 of file sha2.c.

606 {
607  if (context == NULL)
608  return;
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_BLOCK_LENGTH
Definition: sha2.h:28

References context, PG_SHA512_BLOCK_LENGTH, PG_SHA512_DIGEST_LENGTH, and sha512_initial_hash_value.

Referenced by pg_cryptohash_init().

◆ 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.

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 */
840  ADDINC128(context->bitcount, PG_SHA512_BLOCK_LENGTH << 3);
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 ADDINC128(w, n)
Definition: sha2.c:115
static void SHA512_Transform(pg_sha512_ctx *context, const uint8 *data)
Definition: sha2.c:712

References ADDINC128, context, data, len, PG_SHA512_BLOCK_LENGTH, and SHA512_Transform().

Referenced by pg_cryptohash_update(), and pg_sha384_update().