PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
sha2.h File Reference
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
 

Macros

#define PG_SHA224_BLOCK_LENGTH   64
 
#define PG_SHA224_DIGEST_LENGTH   28
 
#define PG_SHA224_DIGEST_STRING_LENGTH   (PG_SHA224_DIGEST_LENGTH * 2 + 1)
 
#define PG_SHA256_BLOCK_LENGTH   64
 
#define PG_SHA256_DIGEST_LENGTH   32
 
#define PG_SHA256_DIGEST_STRING_LENGTH   (PG_SHA256_DIGEST_LENGTH * 2 + 1)
 
#define PG_SHA384_BLOCK_LENGTH   128
 
#define PG_SHA384_DIGEST_LENGTH   48
 
#define PG_SHA384_DIGEST_STRING_LENGTH   (PG_SHA384_DIGEST_LENGTH * 2 + 1)
 
#define PG_SHA512_BLOCK_LENGTH   128
 
#define PG_SHA512_DIGEST_LENGTH   64
 
#define PG_SHA512_DIGEST_STRING_LENGTH   (PG_SHA512_DIGEST_LENGTH * 2 + 1)
 

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)
 

Macro Definition Documentation

#define PG_SHA224_BLOCK_LENGTH   64

Definition at line 58 of file sha2.h.

Referenced by int_sha224_block_len().

#define PG_SHA224_DIGEST_LENGTH   28

Definition at line 59 of file sha2.h.

Referenced by int_sha224_len(), and pg_sha224_final().

#define PG_SHA224_DIGEST_STRING_LENGTH   (PG_SHA224_DIGEST_LENGTH * 2 + 1)

Definition at line 60 of file sha2.h.

#define PG_SHA256_BLOCK_LENGTH   64
#define PG_SHA256_DIGEST_LENGTH   32
#define PG_SHA256_DIGEST_STRING_LENGTH   (PG_SHA256_DIGEST_LENGTH * 2 + 1)

Definition at line 63 of file sha2.h.

#define PG_SHA384_BLOCK_LENGTH   128

Definition at line 64 of file sha2.h.

Referenced by int_sha384_block_len(), and pg_sha384_init().

#define PG_SHA384_DIGEST_LENGTH   48

Definition at line 65 of file sha2.h.

Referenced by int_sha384_len(), and pg_sha384_final().

#define PG_SHA384_DIGEST_STRING_LENGTH   (PG_SHA384_DIGEST_LENGTH * 2 + 1)

Definition at line 66 of file sha2.h.

#define PG_SHA512_BLOCK_LENGTH   128

Definition at line 67 of file sha2.h.

Referenced by int_sha512_block_len(), pg_sha512_init(), pg_sha512_update(), and SHA512_Last().

#define PG_SHA512_DIGEST_LENGTH   64

Definition at line 68 of file sha2.h.

Referenced by int_sha512_len(), pg_sha384_init(), pg_sha512_final(), and pg_sha512_init().

#define PG_SHA512_DIGEST_STRING_LENGTH   (PG_SHA512_DIGEST_LENGTH * 2 + 1)

Definition at line 69 of file sha2.h.

Typedef Documentation

Definition at line 90 of file sha2.h.

Definition at line 91 of file sha2.h.

Function Documentation

void pg_sha224_final ( pg_sha224_ctx ctx,
uint8 dest 
)

Definition at line 983 of file sha2.c.

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

Referenced by int_sha224_finish().

984 {
985  /* If no digest buffer is passed, we don't bother doing this: */
986  if (digest != NULL)
987  {
988  SHA256_Last(context);
989 
990 #ifndef WORDS_BIGENDIAN
991  {
992  /* Convert TO host byte order */
993  int j;
994 
995  for (j = 0; j < 8; j++)
996  {
997  REVERSE32(context->state[j], context->state[j]);
998  }
999  }
1000 #endif
1001  memcpy(digest, context->state, PG_SHA224_DIGEST_LENGTH);
1002  }
1003 
1004  /* Clean up state data: */
1005  memset(context, 0, sizeof(pg_sha224_ctx));
1006 }
static void SHA256_Last(pg_sha256_ctx *context)
Definition: sha2.c:518
#define PG_SHA224_DIGEST_LENGTH
Definition: sha2.h:59
#define REVERSE32(w, x)
Definition: sha2.c:84
void pg_sha224_init ( pg_sha224_ctx ctx)

Definition at line 967 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 int_sha224_reset().

968 {
969  if (context == NULL)
970  return;
971  memcpy(context->state, sha224_initial_hash_value, PG_SHA256_DIGEST_LENGTH);
972  memset(context->buffer, 0, PG_SHA256_BLOCK_LENGTH);
973  context->bitcount = 0;
974 }
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:62
#define PG_SHA256_BLOCK_LENGTH
Definition: sha2.h:61
static const uint32 sha224_initial_hash_value[8]
Definition: sha2.c:174
void pg_sha224_update ( pg_sha224_ctx ctx,
const uint8 input0,
size_t  len 
)

Definition at line 977 of file sha2.c.

References pg_sha256_update().

Referenced by int_sha224_update().

978 {
979  pg_sha256_update((pg_sha256_ctx *) context, data, len);
980 }
void pg_sha256_update(pg_sha256_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:465
void pg_sha256_final ( pg_sha256_ctx ctx,
uint8 dest 
)

Definition at line 566 of file sha2.c.

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

Referenced by int_sha256_finish(), scram_H(), scram_HMAC_final(), scram_HMAC_init(), and scram_mock_salt().

567 {
568  /* If no digest buffer is passed, we don't bother doing this: */
569  if (digest != NULL)
570  {
571  SHA256_Last(context);
572 
573 #ifndef WORDS_BIGENDIAN
574  {
575  /* Convert TO host byte order */
576  int j;
577 
578  for (j = 0; j < 8; j++)
579  {
580  REVERSE32(context->state[j], context->state[j]);
581  }
582  }
583 #endif
584  memcpy(digest, context->state, PG_SHA256_DIGEST_LENGTH);
585  }
586 
587  /* Clean up state data: */
588  memset(context, 0, sizeof(pg_sha256_ctx));
589 }
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:62
static void SHA256_Last(pg_sha256_ctx *context)
Definition: sha2.c:518
#define REVERSE32(w, x)
Definition: sha2.c:84
void pg_sha256_init ( pg_sha256_ctx ctx)

Definition at line 268 of file sha2.c.

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

Referenced by int_sha256_reset(), scram_H(), scram_HMAC_final(), scram_HMAC_init(), and scram_mock_salt().

269 {
270  if (context == NULL)
271  return;
272  memcpy(context->state, sha256_initial_hash_value, PG_SHA256_DIGEST_LENGTH);
273  memset(context->buffer, 0, PG_SHA256_BLOCK_LENGTH);
274  context->bitcount = 0;
275 }
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:62
static const uint32 sha256_initial_hash_value[8]
Definition: sha2.c:186
#define PG_SHA256_BLOCK_LENGTH
Definition: sha2.h:61
void pg_sha256_update ( pg_sha256_ctx ctx,
const uint8 input0,
size_t  len 
)

Definition at line 465 of file sha2.c.

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

Referenced by int_sha256_update(), pg_sha224_update(), scram_H(), scram_HMAC_final(), scram_HMAC_init(), scram_HMAC_update(), and scram_mock_salt().

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

Definition at line 939 of file sha2.c.

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

Referenced by int_sha384_finish().

940 {
941  /* If no digest buffer is passed, we don't bother doing this: */
942  if (digest != NULL)
943  {
944  SHA512_Last((pg_sha512_ctx *) context);
945 
946  /* Save the hash data for output: */
947 #ifndef WORDS_BIGENDIAN
948  {
949  /* Convert TO host byte order */
950  int j;
951 
952  for (j = 0; j < 6; j++)
953  {
954  REVERSE64(context->state[j], context->state[j]);
955  }
956  }
957 #endif
958  memcpy(digest, context->state, PG_SHA384_DIGEST_LENGTH);
959  }
960 
961  /* Zero out state data */
962  memset(context, 0, sizeof(pg_sha384_ctx));
963 }
static void SHA512_Last(pg_sha512_ctx *context)
Definition: sha2.c:844
#define REVERSE64(w, x)
Definition: sha2.c:89
#define PG_SHA384_DIGEST_LENGTH
Definition: sha2.h:65
void pg_sha384_init ( pg_sha384_ctx ctx)

Definition at line 923 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 int_sha384_reset().

924 {
925  if (context == NULL)
926  return;
927  memcpy(context->state, sha384_initial_hash_value, PG_SHA512_DIGEST_LENGTH);
928  memset(context->buffer, 0, PG_SHA384_BLOCK_LENGTH);
929  context->bitcount[0] = context->bitcount[1] = 0;
930 }
#define PG_SHA384_BLOCK_LENGTH
Definition: sha2.h:64
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:68
static const uint64 sha384_initial_hash_value[8]
Definition: sha2.c:242
void pg_sha384_update ( pg_sha384_ctx ctx,
const uint8 ,
size_t  len 
)

Definition at line 933 of file sha2.c.

References pg_sha512_update().

Referenced by int_sha384_update().

934 {
935  pg_sha512_update((pg_sha512_ctx *) context, data, len);
936 }
void pg_sha512_update(pg_sha512_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:791
void pg_sha512_final ( pg_sha512_ctx ctx,
uint8 dest 
)

Definition at line 894 of file sha2.c.

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

Referenced by int_sha512_finish().

895 {
896  /* If no digest buffer is passed, we don't bother doing this: */
897  if (digest != NULL)
898  {
899  SHA512_Last(context);
900 
901  /* Save the hash data for output: */
902 #ifndef WORDS_BIGENDIAN
903  {
904  /* Convert TO host byte order */
905  int j;
906 
907  for (j = 0; j < 8; j++)
908  {
909  REVERSE64(context->state[j], context->state[j]);
910  }
911  }
912 #endif
913  memcpy(digest, context->state, PG_SHA512_DIGEST_LENGTH);
914  }
915 
916  /* Zero out state data */
917  memset(context, 0, sizeof(pg_sha512_ctx));
918 }
static void SHA512_Last(pg_sha512_ctx *context)
Definition: sha2.c:844
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:68
#define REVERSE64(w, x)
Definition: sha2.c:89
void pg_sha512_init ( pg_sha512_ctx ctx)

Definition at line 594 of file sha2.c.

References pg_sha512_ctx::bitcount, pg_sha512_ctx::buffer, PG_SHA512_BLOCK_LENGTH, PG_SHA512_DIGEST_LENGTH, sha512_initial_hash_value, and pg_sha512_ctx::state.

Referenced by int_sha512_reset().

595 {
596  if (context == NULL)
597  return;
598  memcpy(context->state, sha512_initial_hash_value, PG_SHA512_DIGEST_LENGTH);
599  memset(context->buffer, 0, PG_SHA512_BLOCK_LENGTH);
600  context->bitcount[0] = context->bitcount[1] = 0;
601 }
static const uint64 sha512_initial_hash_value[8]
Definition: sha2.c:254
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:68
#define PG_SHA512_BLOCK_LENGTH
Definition: sha2.h:67
void pg_sha512_update ( pg_sha512_ctx ctx,
const uint8 input0,
size_t  len 
)

Definition at line 791 of file sha2.c.

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

Referenced by int_sha512_update(), and pg_sha384_update().

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