3333 *
3434 * $From: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
3535 *
36- * $PostgreSQL: pgsql/contrib/pgcrypto/sha2.c,v 1.2 2005/07/11 15:07:59 tgl Exp $
36+ * $PostgreSQL: pgsql/contrib/pgcrypto/sha2.c,v 1.3 2005/07/11 15:40:38 tgl Exp $
3737 */
3838
3939#include "postgres.h"
4242
4343#include "sha2.h"
4444
45- #undef bcopy
46- #undef bzero
47- #define bcopy (src ,dst ,len )memcpy((dst), (src), (len))
48- #define bzero (ptr ,len )memset((ptr), 0, (len))
49-
5045/*
5146 * UNROLLED TRANSFORM LOOP NOTE:
5247 * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
@@ -281,8 +276,8 @@ SHA256_Init(SHA256_CTX *context)
281276{
282277if (context == NULL )
283278return ;
284- bcopy ( sha256_initial_hash_value , context -> state ,SHA256_DIGEST_LENGTH );
285- bzero (context -> buffer ,SHA256_BLOCK_LENGTH );
279+ memcpy ( context -> state , sha256_initial_hash_value ,SHA256_DIGEST_LENGTH );
280+ memset (context -> buffer , 0 ,SHA256_BLOCK_LENGTH );
286281context -> bitcount = 0 ;
287282}
288283
@@ -466,14 +461,14 @@ SHA256_Update(SHA256_CTX *context, const uint8 *data, size_t len)
466461
467462if (len >=freespace ) {
468463/* Fill the buffer completely and process it */
469- bcopy ( data , & context -> buffer [usedspace ],freespace );
464+ memcpy ( & context -> buffer [usedspace ], data ,freespace );
470465context -> bitcount += freespace <<3 ;
471466len -= freespace ;
472467data += freespace ;
473468SHA256_Transform (context ,context -> buffer );
474469}else {
475470/* The buffer is not yet full */
476- bcopy ( data , & context -> buffer [usedspace ],len );
471+ memcpy ( & context -> buffer [usedspace ], data ,len );
477472context -> bitcount += len <<3 ;
478473/* Clean up: */
479474usedspace = freespace = 0 ;
@@ -489,7 +484,7 @@ SHA256_Update(SHA256_CTX *context, const uint8 *data, size_t len)
489484}
490485if (len > 0 ) {
491486/* There's left-overs, so save 'em */
492- bcopy ( data , context -> buffer ,len );
487+ memcpy ( context -> buffer , data ,len );
493488context -> bitcount += len <<3 ;
494489}
495490/* Clean up: */
@@ -514,20 +509,20 @@ SHA256_Final(uint8 digest[], SHA256_CTX *context)
514509
515510if (usedspace <=SHA256_SHORT_BLOCK_LENGTH ) {
516511/* Set-up for the last transform: */
517- bzero (& context -> buffer [usedspace ],SHA256_SHORT_BLOCK_LENGTH - usedspace );
512+ memset (& context -> buffer [usedspace ], 0 ,SHA256_SHORT_BLOCK_LENGTH - usedspace );
518513}else {
519514if (usedspace < SHA256_BLOCK_LENGTH ) {
520- bzero (& context -> buffer [usedspace ],SHA256_BLOCK_LENGTH - usedspace );
515+ memset (& context -> buffer [usedspace ], 0 ,SHA256_BLOCK_LENGTH - usedspace );
521516}
522517/* Do second-to-last transform: */
523518SHA256_Transform (context ,context -> buffer );
524519
525520/* And set-up for the last transform: */
526- bzero (context -> buffer ,SHA256_SHORT_BLOCK_LENGTH );
521+ memset (context -> buffer , 0 ,SHA256_SHORT_BLOCK_LENGTH );
527522}
528523}else {
529524/* Set-up for the last transform: */
530- bzero (context -> buffer ,SHA256_SHORT_BLOCK_LENGTH );
525+ memset (context -> buffer , 0 ,SHA256_SHORT_BLOCK_LENGTH );
531526
532527/* Begin padding with a 1 bit: */
533528* context -> buffer = 0x80 ;
@@ -547,11 +542,11 @@ SHA256_Final(uint8 digest[], SHA256_CTX *context)
547542}
548543}
549544#endif
550- bcopy ( context -> state , digest ,SHA256_DIGEST_LENGTH );
545+ memcpy ( digest , context -> state ,SHA256_DIGEST_LENGTH );
551546}
552547
553548/* Clean up state data: */
554- bzero (context ,sizeof (* context ));
549+ memset (context , 0 ,sizeof (* context ));
555550usedspace = 0 ;
556551}
557552
@@ -562,8 +557,8 @@ SHA512_Init(SHA512_CTX *context)
562557{
563558if (context == NULL )
564559return ;
565- bcopy ( sha512_initial_hash_value , context -> state ,SHA512_DIGEST_LENGTH );
566- bzero (context -> buffer ,SHA512_BLOCK_LENGTH );
560+ memcpy ( context -> state , sha512_initial_hash_value ,SHA512_DIGEST_LENGTH );
561+ memset (context -> buffer , 0 ,SHA512_BLOCK_LENGTH );
567562context -> bitcount [0 ]= context -> bitcount [1 ]= 0 ;
568563}
569564
@@ -747,14 +742,14 @@ SHA512_Update(SHA512_CTX *context, const uint8 *data, size_t len)
747742
748743if (len >=freespace ) {
749744/* Fill the buffer completely and process it */
750- bcopy ( data , & context -> buffer [usedspace ],freespace );
745+ memcpy ( & context -> buffer [usedspace ], data ,freespace );
751746ADDINC128 (context -> bitcount ,freespace <<3 );
752747len -= freespace ;
753748data += freespace ;
754749SHA512_Transform (context ,context -> buffer );
755750}else {
756751/* The buffer is not yet full */
757- bcopy ( data , & context -> buffer [usedspace ],len );
752+ memcpy ( & context -> buffer [usedspace ], data ,len );
758753ADDINC128 (context -> bitcount ,len <<3 );
759754/* Clean up: */
760755usedspace = freespace = 0 ;
@@ -770,7 +765,7 @@ SHA512_Update(SHA512_CTX *context, const uint8 *data, size_t len)
770765}
771766if (len > 0 ) {
772767/* There's left-overs, so save 'em */
773- bcopy ( data , context -> buffer ,len );
768+ memcpy ( context -> buffer , data ,len );
774769ADDINC128 (context -> bitcount ,len <<3 );
775770}
776771/* Clean up: */
@@ -794,20 +789,20 @@ SHA512_Last(SHA512_CTX *context)
794789
795790if (usedspace <=SHA512_SHORT_BLOCK_LENGTH ) {
796791/* Set-up for the last transform: */
797- bzero (& context -> buffer [usedspace ],SHA512_SHORT_BLOCK_LENGTH - usedspace );
792+ memset (& context -> buffer [usedspace ], 0 ,SHA512_SHORT_BLOCK_LENGTH - usedspace );
798793}else {
799794if (usedspace < SHA512_BLOCK_LENGTH ) {
800- bzero (& context -> buffer [usedspace ],SHA512_BLOCK_LENGTH - usedspace );
795+ memset (& context -> buffer [usedspace ], 0 ,SHA512_BLOCK_LENGTH - usedspace );
801796}
802797/* Do second-to-last transform: */
803798SHA512_Transform (context ,context -> buffer );
804799
805800/* And set-up for the last transform: */
806- bzero (context -> buffer ,SHA512_BLOCK_LENGTH - 2 );
801+ memset (context -> buffer , 0 ,SHA512_BLOCK_LENGTH - 2 );
807802}
808803}else {
809804/* Prepare for final transform: */
810- bzero (context -> buffer ,SHA512_SHORT_BLOCK_LENGTH );
805+ memset (context -> buffer , 0 ,SHA512_SHORT_BLOCK_LENGTH );
811806
812807/* Begin padding with a 1 bit: */
813808* context -> buffer = 0x80 ;
@@ -837,11 +832,11 @@ SHA512_Final(uint8 digest[], SHA512_CTX *context)
837832}
838833}
839834#endif
840- bcopy ( context -> state , digest ,SHA512_DIGEST_LENGTH );
835+ memcpy ( digest , context -> state ,SHA512_DIGEST_LENGTH );
841836}
842837
843838/* Zero out state data */
844- bzero (context ,sizeof (* context ));
839+ memset (context , 0 ,sizeof (* context ));
845840}
846841
847842
@@ -851,8 +846,8 @@ SHA384_Init(SHA384_CTX *context)
851846{
852847if (context == NULL )
853848return ;
854- bcopy ( sha384_initial_hash_value , context -> state ,SHA512_DIGEST_LENGTH );
855- bzero (context -> buffer ,SHA384_BLOCK_LENGTH );
849+ memcpy ( context -> state , sha384_initial_hash_value ,SHA512_DIGEST_LENGTH );
850+ memset (context -> buffer , 0 ,SHA384_BLOCK_LENGTH );
856851context -> bitcount [0 ]= context -> bitcount [1 ]= 0 ;
857852}
858853
@@ -879,9 +874,9 @@ SHA384_Final(uint8 digest[], SHA384_CTX *context)
879874}
880875}
881876#endif
882- bcopy ( context -> state , digest ,SHA384_DIGEST_LENGTH );
877+ memcpy ( digest , context -> state ,SHA384_DIGEST_LENGTH );
883878}
884879
885880/* Zero out state data */
886- bzero (context ,sizeof (* context ));
881+ memset (context , 0 ,sizeof (* context ));
887882}