PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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

Definition at line 67 of file sha2_int.h.

◆ pg_sha256_ctx

◆ pg_sha384_ctx

Definition at line 68 of file sha2_int.h.

◆ pg_sha512_ctx

Function Documentation

◆ pg_sha224_final()

void pg_sha224_final ( pg_sha224_ctx ctx,
uint8 dest 
)
extern

Definition at line 995 of file sha2.c.

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

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

Referenced by pg_cryptohash_final().

◆ pg_sha224_init()

void pg_sha224_init ( pg_sha224_ctx ctx)
extern

Definition at line 979 of file sha2.c.

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

References pg_sha256_ctx::bitcount, pg_sha256_ctx::buffer, fb(), memcpy(), PG_SHA256_BLOCK_LENGTH, PG_SHA256_DIGEST_LENGTH, sha224_initial_hash_value, and pg_sha256_ctx::state.

Referenced by pg_cryptohash_init().

◆ pg_sha224_update()

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

Definition at line 989 of file sha2.c.

990{
991 pg_sha256_update((pg_sha256_ctx *) context, data, len);
992}
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:477

References data, len, and pg_sha256_update().

Referenced by pg_cryptohash_update().

◆ pg_sha256_final()

void pg_sha256_final ( pg_sha256_ctx ctx,
uint8 dest 
)
extern

Definition at line 578 of file sha2.c.

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

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

Referenced by pg_cryptohash_final().

◆ pg_sha256_init()

void pg_sha256_init ( pg_sha256_ctx ctx)
extern

Definition at line 280 of file sha2.c.

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

References pg_sha256_ctx::bitcount, pg_sha256_ctx::buffer, fb(), memcpy(), PG_SHA256_BLOCK_LENGTH, PG_SHA256_DIGEST_LENGTH, sha256_initial_hash_value, and pg_sha256_ctx::state.

Referenced by pg_cryptohash_init().

◆ pg_sha256_update()

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

Definition at line 477 of file sha2.c.

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

References pg_sha256_ctx::bitcount, pg_sha256_ctx::buffer, data, fb(), len, memcpy(), 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 
)
extern

Definition at line 951 of file sha2.c.

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

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

Referenced by pg_cryptohash_final().

◆ pg_sha384_init()

void pg_sha384_init ( pg_sha384_ctx ctx)
extern

Definition at line 935 of file sha2.c.

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

References pg_sha512_ctx::bitcount, pg_sha512_ctx::buffer, fb(), memcpy(), PG_SHA384_BLOCK_LENGTH, PG_SHA512_DIGEST_LENGTH, sha384_initial_hash_value, and pg_sha512_ctx::state.

Referenced by pg_cryptohash_init().

◆ pg_sha384_update()

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

Definition at line 945 of file sha2.c.

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

References data, len, and pg_sha512_update().

Referenced by pg_cryptohash_update().

◆ pg_sha512_final()

void pg_sha512_final ( pg_sha512_ctx ctx,
uint8 dest 
)
extern

Definition at line 906 of file sha2.c.

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

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

Referenced by pg_cryptohash_final().

◆ pg_sha512_init()

void pg_sha512_init ( pg_sha512_ctx ctx)
extern

Definition at line 606 of file sha2.c.

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

References pg_sha512_ctx::bitcount, pg_sha512_ctx::buffer, fb(), memcpy(), PG_SHA512_BLOCK_LENGTH, PG_SHA512_DIGEST_LENGTH, sha512_initial_hash_value, and pg_sha512_ctx::state.

Referenced by pg_cryptohash_init().

◆ pg_sha512_update()

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

Definition at line 803 of file sha2.c.

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

References ADDINC128, pg_sha512_ctx::bitcount, pg_sha512_ctx::buffer, data, fb(), len, memcpy(), PG_SHA512_BLOCK_LENGTH, and SHA512_Transform().

Referenced by pg_cryptohash_update(), and pg_sha384_update().