Lines Matching +full:sha +full:- +full:256

3  * AUTHOR:	Aaron D. Gifford - http://www.aarongifford.com/
5 * Copyright (c) 2000-2001, Aaron D. Gifford
9 * system-defined SHA code.
11 * - Renamed (external) functions and constants to fit ldns style
12 * - Removed _End and _Data functions
13 * - Added ldns_shaX(data, len, digest) convenience functions
14 * - Removed prototypes of _Transform functions and made those static
60 * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
69 /*** SHA-256/384/512 Machine Architecture Definitions *****************/
74 * architecture is little-endian, make sure it also defines
84 * And for little-endian machines, add:
88 * Or for big-endian machines:
109 /*** SHA-256/384/512 Various Length Definitions ***********************/
111 #define ldns_sha256_SHORT_BLOCK_LENGTH (LDNS_SHA256_BLOCK_LENGTH - 8)
112 #define ldns_sha384_SHORT_BLOCK_LENGTH (LDNS_SHA384_BLOCK_LENGTH - 16)
113 #define ldns_sha512_SHORT_BLOCK_LENGTH (LDNS_SHA512_BLOCK_LENGTH - 16)
138 * Macro for incrementally adding the unsigned 64-bit integer n to the
139 * unsigned 128-bit integer (represented using a two-element array of
140 * 64-bit words):
183 * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
186 * S is a ROTATION) because the SHA-256/384/512 description document
187 * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
190 /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
192 /* 32-bit Rotate-right (used in SHA-256): */
193 #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
194 /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
195 #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
197 /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
201 /* Four of six logical functions used in SHA-256: */
207 /* Four of six logical functions used in SHA-384 and SHA-512: */
213 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
214 /* Hash constant words K for SHA-256: */
234 /* initial hash value H for SHA-256: */
246 /* Hash constant words K for SHA-384 and SHA-512: */
290 /* initial hash value H for SHA-384 */
302 /* initial hash value H for SHA-512 */
314 /*** SHA-256: *********************************************************/
319 MEMCPY_BCOPY(context->state, ldns_sha256_initial_hash_value, LDNS_SHA256_DIGEST_LENGTH); in ldns_sha256_init()
320 MEMSET_BZERO(context->buffer, LDNS_SHA256_BLOCK_LENGTH); in ldns_sha256_init()
321 context->bitcount = 0; in ldns_sha256_init()
326 /* Unrolled SHA-256 round macros: */
367 W256 = (sha2_word32*)context->buffer; in ldns_sha256_Transform()
370 a = context->state[0]; in ldns_sha256_Transform()
371 b = context->state[1]; in ldns_sha256_Transform()
372 c = context->state[2]; in ldns_sha256_Transform()
373 d = context->state[3]; in ldns_sha256_Transform()
374 e = context->state[4]; in ldns_sha256_Transform()
375 f = context->state[5]; in ldns_sha256_Transform()
376 g = context->state[6]; in ldns_sha256_Transform()
377 h = context->state[7]; in ldns_sha256_Transform()
405 context->state[0] += a; in ldns_sha256_Transform()
406 context->state[1] += b; in ldns_sha256_Transform()
407 context->state[2] += c; in ldns_sha256_Transform()
408 context->state[3] += d; in ldns_sha256_Transform()
409 context->state[4] += e; in ldns_sha256_Transform()
410 context->state[5] += f; in ldns_sha256_Transform()
411 context->state[6] += g; in ldns_sha256_Transform()
412 context->state[7] += h; in ldns_sha256_Transform()
426 W256 = (sha2_word32*)context->buffer; in ldns_sha256_Transform()
429 a = context->state[0]; in ldns_sha256_Transform()
430 b = context->state[1]; in ldns_sha256_Transform()
431 c = context->state[2]; in ldns_sha256_Transform()
432 d = context->state[3]; in ldns_sha256_Transform()
433 e = context->state[4]; in ldns_sha256_Transform()
434 f = context->state[5]; in ldns_sha256_Transform()
435 g = context->state[6]; in ldns_sha256_Transform()
436 h = context->state[7]; in ldns_sha256_Transform()
443 /* Apply the SHA-256 compression function to update a..h */ in ldns_sha256_Transform()
446 /* Apply the SHA-256 compression function to update a..h with copy */ in ldns_sha256_Transform()
469 /* Apply the SHA-256 compression function to update a..h */ in ldns_sha256_Transform()
486 context->state[0] += a; in ldns_sha256_Transform()
487 context->state[1] += b; in ldns_sha256_Transform()
488 context->state[2] += c; in ldns_sha256_Transform()
489 context->state[3] += d; in ldns_sha256_Transform()
490 context->state[4] += e; in ldns_sha256_Transform()
491 context->state[5] += f; in ldns_sha256_Transform()
492 context->state[6] += g; in ldns_sha256_Transform()
493 context->state[7] += h; in ldns_sha256_Transform()
506 /* Calling with no data is valid - we do nothing */ in ldns_sha256_update()
513 usedspace = (context->bitcount >> 3) % LDNS_SHA256_BLOCK_LENGTH; in ldns_sha256_update()
516 freespace = LDNS_SHA256_BLOCK_LENGTH - usedspace; in ldns_sha256_update()
520 MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace); in ldns_sha256_update()
521 context->bitcount += freespace << 3; in ldns_sha256_update()
522 len -= freespace; in ldns_sha256_update()
524 ldns_sha256_Transform(context, (sha2_word32*)context->buffer); in ldns_sha256_update()
527 MEMCPY_BCOPY(&context->buffer[usedspace], data, len); in ldns_sha256_update()
528 context->bitcount += len << 3; in ldns_sha256_update()
538 context->bitcount += LDNS_SHA256_BLOCK_LENGTH << 3; in ldns_sha256_update()
539 len -= LDNS_SHA256_BLOCK_LENGTH; in ldns_sha256_update()
543 /* There's left-overs, so save 'em */ in ldns_sha256_update()
544 MEMCPY_BCOPY(context->buffer, data, len); in ldns_sha256_update()
545 context->bitcount += len << 3; in ldns_sha256_update()
567 usedspace = (context->bitcount >> 3) % LDNS_SHA256_BLOCK_LENGTH; in ldns_sha256_final()
570 REVERSE64(context->bitcount,context->bitcount); in ldns_sha256_final()
574 context->buffer[usedspace++] = 0x80; in ldns_sha256_final()
577 /* Set-up for the last transform: */ in ldns_sha256_final()
578 MEMSET_BZERO(&context->buffer[usedspace], ldns_sha256_SHORT_BLOCK_LENGTH - usedspace); in ldns_sha256_final()
581 MEMSET_BZERO(&context->buffer[usedspace], LDNS_SHA256_BLOCK_LENGTH - usedspace); in ldns_sha256_final()
583 /* Do second-to-last transform: */ in ldns_sha256_final()
584 ldns_sha256_Transform(context, (sha2_word32*)context->buffer); in ldns_sha256_final()
586 /* And set-up for the last transform: */ in ldns_sha256_final()
587 MEMSET_BZERO(context->buffer, ldns_sha256_SHORT_BLOCK_LENGTH); in ldns_sha256_final()
590 /* Set-up for the last transform: */ in ldns_sha256_final()
591 MEMSET_BZERO(context->buffer, ldns_sha256_SHORT_BLOCK_LENGTH); in ldns_sha256_final()
594 *context->buffer = 0x80; in ldns_sha256_final()
597 cast_var.theChars = context->buffer; in ldns_sha256_final()
598 cast_var.theLongs[ldns_sha256_SHORT_BLOCK_LENGTH / 8] = context->bitcount; in ldns_sha256_final()
601 ldns_sha256_Transform(context, (sha2_word32*)context->buffer); in ldns_sha256_final()
608 REVERSE32(context->state[j],context->state[j]); in ldns_sha256_final()
609 *d++ = context->state[j]; in ldns_sha256_final()
613 MEMCPY_BCOPY(d, context->state, LDNS_SHA256_DIGEST_LENGTH); in ldns_sha256_final()
633 /*** SHA-512: *********************************************************/
638 MEMCPY_BCOPY(context->state, sha512_initial_hash_value, LDNS_SHA512_DIGEST_LENGTH); in ldns_sha512_init()
639 MEMSET_BZERO(context->buffer, LDNS_SHA512_BLOCK_LENGTH); in ldns_sha512_init()
640 context->bitcount[0] = context->bitcount[1] = 0; in ldns_sha512_init()
645 /* Unrolled SHA-512 round macros: */
682 sha2_word64 T1, *W512 = (sha2_word64*)context->buffer; in ldns_sha512_Transform()
686 a = context->state[0]; in ldns_sha512_Transform()
687 b = context->state[1]; in ldns_sha512_Transform()
688 c = context->state[2]; in ldns_sha512_Transform()
689 d = context->state[3]; in ldns_sha512_Transform()
690 e = context->state[4]; in ldns_sha512_Transform()
691 f = context->state[5]; in ldns_sha512_Transform()
692 g = context->state[6]; in ldns_sha512_Transform()
693 h = context->state[7]; in ldns_sha512_Transform()
720 context->state[0] += a; in ldns_sha512_Transform()
721 context->state[1] += b; in ldns_sha512_Transform()
722 context->state[2] += c; in ldns_sha512_Transform()
723 context->state[3] += d; in ldns_sha512_Transform()
724 context->state[4] += e; in ldns_sha512_Transform()
725 context->state[5] += f; in ldns_sha512_Transform()
726 context->state[6] += g; in ldns_sha512_Transform()
727 context->state[7] += h; in ldns_sha512_Transform()
738 sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer; in ldns_sha512_Transform()
742 a = context->state[0]; in ldns_sha512_Transform()
743 b = context->state[1]; in ldns_sha512_Transform()
744 c = context->state[2]; in ldns_sha512_Transform()
745 d = context->state[3]; in ldns_sha512_Transform()
746 e = context->state[4]; in ldns_sha512_Transform()
747 f = context->state[5]; in ldns_sha512_Transform()
748 g = context->state[6]; in ldns_sha512_Transform()
749 h = context->state[7]; in ldns_sha512_Transform()
756 /* Apply the SHA-512 compression function to update a..h */ in ldns_sha512_Transform()
759 /* Apply the SHA-512 compression function to update a..h with copy */ in ldns_sha512_Transform()
782 /* Apply the SHA-512 compression function to update a..h */ in ldns_sha512_Transform()
799 context->state[0] += a; in ldns_sha512_Transform()
800 context->state[1] += b; in ldns_sha512_Transform()
801 context->state[2] += c; in ldns_sha512_Transform()
802 context->state[3] += d; in ldns_sha512_Transform()
803 context->state[4] += e; in ldns_sha512_Transform()
804 context->state[5] += f; in ldns_sha512_Transform()
805 context->state[6] += g; in ldns_sha512_Transform()
806 context->state[7] += h; in ldns_sha512_Transform()
819 /* Calling with no data is valid - we do nothing */ in ldns_sha512_update()
826 usedspace = (context->bitcount[0] >> 3) % LDNS_SHA512_BLOCK_LENGTH; in ldns_sha512_update()
829 freespace = LDNS_SHA512_BLOCK_LENGTH - usedspace; in ldns_sha512_update()
833 MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace); in ldns_sha512_update()
834 ADDINC128(context->bitcount, freespace << 3); in ldns_sha512_update()
835 len -= freespace; in ldns_sha512_update()
837 ldns_sha512_Transform(context, (sha2_word64*)context->buffer); in ldns_sha512_update()
840 MEMCPY_BCOPY(&context->buffer[usedspace], data, len); in ldns_sha512_update()
841 ADDINC128(context->bitcount, len << 3); in ldns_sha512_update()
851 ADDINC128(context->bitcount, LDNS_SHA512_BLOCK_LENGTH << 3); in ldns_sha512_update()
852 len -= LDNS_SHA512_BLOCK_LENGTH; in ldns_sha512_update()
856 /* There's left-overs, so save 'em */ in ldns_sha512_update()
857 MEMCPY_BCOPY(context->buffer, data, len); in ldns_sha512_update()
858 ADDINC128(context->bitcount, len << 3); in ldns_sha512_update()
869 usedspace = (context->bitcount[0] >> 3) % LDNS_SHA512_BLOCK_LENGTH; in ldns_sha512_Last()
872 REVERSE64(context->bitcount[0],context->bitcount[0]); in ldns_sha512_Last()
873 REVERSE64(context->bitcount[1],context->bitcount[1]); in ldns_sha512_Last()
877 context->buffer[usedspace++] = 0x80; in ldns_sha512_Last()
880 /* Set-up for the last transform: */ in ldns_sha512_Last()
881 MEMSET_BZERO(&context->buffer[usedspace], ldns_sha512_SHORT_BLOCK_LENGTH - usedspace); in ldns_sha512_Last()
884 MEMSET_BZERO(&context->buffer[usedspace], LDNS_SHA512_BLOCK_LENGTH - usedspace); in ldns_sha512_Last()
886 /* Do second-to-last transform: */ in ldns_sha512_Last()
887 ldns_sha512_Transform(context, (sha2_word64*)context->buffer); in ldns_sha512_Last()
889 /* And set-up for the last transform: */ in ldns_sha512_Last()
890 MEMSET_BZERO(context->buffer, LDNS_SHA512_BLOCK_LENGTH - 2); in ldns_sha512_Last()
894 MEMSET_BZERO(context->buffer, ldns_sha512_SHORT_BLOCK_LENGTH); in ldns_sha512_Last()
897 *context->buffer = 0x80; in ldns_sha512_Last()
900 cast_var.theChars = context->buffer; in ldns_sha512_Last()
901 cast_var.theLongs[ldns_sha512_SHORT_BLOCK_LENGTH / 8] = context->bitcount[1]; in ldns_sha512_Last()
902 cast_var.theLongs[ldns_sha512_SHORT_BLOCK_LENGTH / 8 + 1] = context->bitcount[0]; in ldns_sha512_Last()
905 ldns_sha512_Transform(context, (sha2_word64*)context->buffer); in ldns_sha512_Last()
924 REVERSE64(context->state[j],context->state[j]); in ldns_sha512_final()
925 *d++ = context->state[j]; in ldns_sha512_final()
929 MEMCPY_BCOPY(d, context->state, LDNS_SHA512_DIGEST_LENGTH); in ldns_sha512_final()
947 /*** SHA-384: *********************************************************/
952 MEMCPY_BCOPY(context->state, sha384_initial_hash_value, LDNS_SHA512_DIGEST_LENGTH); in ldns_sha384_init()
953 MEMSET_BZERO(context->buffer, LDNS_SHA384_BLOCK_LENGTH); in ldns_sha384_init()
954 context->bitcount[0] = context->bitcount[1] = 0; in ldns_sha384_init()
977 REVERSE64(context->state[j],context->state[j]); in ldns_sha384_final()
978 *d++ = context->state[j]; in ldns_sha384_final()
982 MEMCPY_BCOPY(d, context->state, LDNS_SHA384_DIGEST_LENGTH); in ldns_sha384_final()