Lines Matching defs:sgr0

5    it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
14 along with this program; see the file COPYING. If not, write to the
15 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 #include "runtime.hpp"
27 #include "algorithm.hpp"
31 namespace STL = STL_NAMESPACE;
37 #define blk0(i) (W[i] = buffer_[i])
38 #define blk1(i) (W[i&15] = \
39 rotlFixed(W[(i+13)&15]^W[(i+8)&15]^W[(i+2)&15]^W[i&15],1))
41 #define f1(x,y,z) (z^(x &(y^z)))
46 // (R0+R1), R2, R3, R4 are the different operations used in SHA1
47 #define R0(v,w,x,y,z,i) z+= f1(w,x,y) + blk0(i) + 0x5A827999+ \
50 rotlFixed(v,5); w = rotlFixed(w,30);
52 rotlFixed(v,5); w = rotlFixed(w,30);
54 rotlFixed(v,5); w = rotlFixed(w,30);
62 digest_[1] = 0xEFCDAB89L;
64 digest_[3] = 0x10325476L;
65 digest_[4] = 0xC3D2E1F0L;
74 digest_[0] = 0x6A09E667L;
76 digest_[2] = 0x3C6EF372L;
77 digest_[3] = 0xA54FF53AL;
79 digest_[5] = 0x9B05688CL;
80 digest_[6] = 0x1F83D9ABL;
89 void SHA224::Init()
91 digest_[0] = 0xc1059ed8;
92 digest_[1] = 0x367cd507;
93 digest_[2] = 0x3070dd17;
95 digest_[4] = 0xffc00b31;
96 digest_[5] = 0x68581511;
97 digest_[6] = 0x64f98fa7;
108 void SHA512::Init()
109 {
110 digest_[0] = W64LIT(0x6a09e667f3bcc908);
111 digest_[1] = W64LIT(0xbb67ae8584caa73b);
113 digest_[3] = W64LIT(0xa54ff53a5f1d36f1);
114 digest_[4] = W64LIT(0x510e527fade682d1);
117 digest_[7] = W64LIT(0x5be0cd19137e2179);
118
119 buffLen_ = 0;
120 loLen_ = 0;
127 digest_[0] = W64LIT(0xcbbb9d5dc1059ed8);
128 digest_[1] = W64LIT(0x629a292a367cd507);
129 digest_[2] = W64LIT(0x9159015a3070dd17);
130 digest_[3] = W64LIT(0x152fecd8f70e5939);
132 digest_[5] = W64LIT(0x8eb44a8768581511);
133 digest_[6] = W64LIT(0xdb0c2e0d64f98fa7);
144 SHA::SHA(const SHA& that) : HASHwithTransform(DIGEST_SIZE / sizeof(word32),
145 BLOCK_SIZE)
147 buffLen_ = that.buffLen_;
152 memcpy(buffer_, that.buffer_, BLOCK_SIZE);
156 SHA256::SHA256(const SHA256& that) : HASHwithTransform(DIGEST_SIZE /
157 sizeof(word32), BLOCK_SIZE)
159 buffLen_ = that.buffLen_;
164 memcpy(buffer_, that.buffer_, BLOCK_SIZE);
168 SHA224::SHA224(const SHA224& that) : HASHwithTransform(SHA256::DIGEST_SIZE /
169 sizeof(word32), BLOCK_SIZE)
173 hiLen_ = that.hiLen_;
175 memcpy(digest_, that.digest_, DIGEST_SIZE);
176 memcpy(buffer_, that.buffer_, BLOCK_SIZE);
180 #ifdef WORD64_AVAILABLE
182 SHA512::SHA512(const SHA512& that) : HASH64withTransform(DIGEST_SIZE /
183 sizeof(word64), BLOCK_SIZE)
184 {
185 buffLen_ = that.buffLen_;
190 memcpy(buffer_, that.buffer_, BLOCK_SIZE);
194 SHA384::SHA384(const SHA384& that) : HASH64withTransform(SHA512::DIGEST_SIZE /
197 buffLen_ = that.buffLen_;
199 hiLen_ = that.hiLen_;
202 memcpy(buffer_, that.buffer_, BLOCK_SIZE);
205 #endif // WORD64_AVAILABLE
213 return *this;
214 }
220 Swap(tmp);
226 SHA224& SHA224::operator= (const SHA224& that)
228 SHA224 tmp(that);
229 Swap(tmp);
231 return *this;
235 #ifdef WORD64_AVAILABLE
237 SHA512& SHA512::operator= (const SHA512& that)
244
246 SHA384& SHA384::operator= (const SHA384& that)
248 SHA384 tmp(that);
261 STL::swap(buffLen_, other.buffLen_);
263 memcpy(digest_, other.digest_, DIGEST_SIZE);
268 void SHA256::Swap(SHA256& other)
270 STL::swap(loLen_, other.loLen_);
272 STL::swap(buffLen_, other.buffLen_);
274 memcpy(digest_, other.digest_, DIGEST_SIZE);
275 memcpy(buffer_, other.buffer_, BLOCK_SIZE);
280 {
281 STL::swap(loLen_, other.loLen_);
282 STL::swap(hiLen_, other.hiLen_);
283 STL::swap(buffLen_, other.buffLen_);
286 memcpy(buffer_, other.buffer_, BLOCK_SIZE);
294 STL::swap(loLen_, other.loLen_);
298 memcpy(digest_, other.digest_, DIGEST_SIZE);
299 memcpy(buffer_, other.buffer_, BLOCK_SIZE);
305 STL::swap(loLen_, other.loLen_);
306 STL::swap(hiLen_, other.hiLen_);
307 STL::swap(buffLen_, other.buffLen_);
309 memcpy(digest_, other.digest_, DIGEST_SIZE);
310 memcpy(buffer_, other.buffer_, BLOCK_SIZE);
317
318 // Update digest with data of size len
321 if (!isMMX) {
322 HASHwithTransform::Update(data, len);
323 return;
326 byte* local = reinterpret_cast<byte*>(buffer_);
328 // remove buffered data if possible
330 word32 add = min(len, BLOCK_SIZE - buffLen_);
331 memcpy(&local[buffLen_], data, add);
334 data += add;
337 if (buffLen_ == BLOCK_SIZE) {
338 ByteReverse(local, local, BLOCK_SIZE);
340 AddLength(BLOCK_SIZE);
341 buffLen_ = 0;
347 word32 times = len / BLOCK_SIZE;
348 if (times) {
349 AsmTransform(data, times);
350 const word32 add = BLOCK_SIZE * times;
352 len -= add;
357 // cache any data left
358 if (len) {
359 memcpy(&local[buffLen_], data, len);
361 }
362 }
363
364 #endif // DO_SHA_ASM
367 void SHA::Transform()
371 // Copy context->state[] to working vars
372 word32 a = digest_[0];
375 word32 d = digest_[3];
378 // 4 rounds of 20 operations each. Loop unrolled.
380 R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
386 R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
387 R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
388 R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
390 R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
394 R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
399 R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
400 R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
402 R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
412 a = b = c = d = e = 0;
413 memset(W, 0, sizeof(W));
417 #define blk2(i) (W[i&15]+=s1(W[(i-2)&15])+W[(i-7)&15]+s0(W[(i-15)&15]))
419 #define Ch(x,y,z) (z^(x&(y^z)))
420 #define Maj(x,y,z) ((x&y)|(z&(x|y)))
423 #define b(i) T[(1-i)&7]
427 #define f(i) T[(5-i)&7]
431 #define R(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j]+(j?blk2(i):blk0(i));\
435 #define S0(x) (rotrFixed(x,2)^rotrFixed(x,13)^rotrFixed(x,22))
437 #define s0(x) (rotrFixed(x,7)^rotrFixed(x,18)^(x>>3))
442 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
443 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
445 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
448 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
450 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
452 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
454 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
456 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
461 static void Transform256(word32* digest_, word32* buffer_)
468 // Copy digest to working vars
472 for (unsigned int j = 0; j < 64; j += 16) {
475 R( 8); R( 9); R(10); R(11);
479 // Add the working vars back into digest
480 digest_[0] += a(0);
481 digest_[1] += b(0);
486 digest_[6] += g(0);
487 digest_[7] += h(0);
489 // Wipe variables
491 memset(T, 0, sizeof(T));
492 }
493
494
495 // undef for 256
499 #undef s1
502 void SHA256::Transform()
508 void SHA224::Transform()
510 Transform256(digest_, buffer_);
514 #ifdef WORD64_AVAILABLE
516 static const word64 K512[80] = {
517 W64LIT(0x428a2f98d728ae22), W64LIT(0x7137449123ef65cd),
518 W64LIT(0xb5c0fbcfec4d3b2f), W64LIT(0xe9b5dba58189dbbc),
519 W64LIT(0x3956c25bf348b538), W64LIT(0x59f111f1b605d019),
523 W64LIT(0x72be5d74f27b896f), W64LIT(0x80deb1fe3b1696b1),
524 W64LIT(0x9bdc06a725c71235), W64LIT(0xc19bf174cf692694),
526 W64LIT(0x0fc19dc68b8cd5b5), W64LIT(0x240ca1cc77ac9c65),
543 W64LIT(0x391c0cb3c5c95a63), W64LIT(0x4ed8aa4ae3418acb),
545 W64LIT(0x748f82ee5defb2fc), W64LIT(0x78a5636f43172f60),
547 W64LIT(0x90befffa23631e28), W64LIT(0xa4506cebde82bde9),
548 W64LIT(0xbef9a3f7b2c67915), W64LIT(0xc67178f2e372532b),
550 W64LIT(0xeada7dd6cde0eb1e), W64LIT(0xf57d4f7fee6ed178),
552 W64LIT(0x113f9804bef90dae), W64LIT(0x1b710b35131c471b),
554 W64LIT(0x3c9ebe0a15c9bebc), W64LIT(0x431d67c49c100d4c),
560 // for SHA512
561 #define S0(x) (rotrFixed(x,28)^rotrFixed(x,34)^rotrFixed(x,39))
562 #define S1(x) (rotrFixed(x,14)^rotrFixed(x,18)^rotrFixed(x,41))
564 #define s1(x) (rotrFixed(x,19)^rotrFixed(x,61)^(x>>6))
569 const word64* K = K512;
571 word64 W[16];
574 // Copy digest to working vars
577 // 64 operations, partially loop unrolled
581 R( 8); R( 9); R(10); R(11);
590 digest_[3] += d(0);
591 digest_[4] += e(0);
597 memset(W, 0, sizeof(W));
604 Transform512(digest_, buffer_);
608 void SHA384::Transform()
610 Transform512(digest_, buffer_);
613 #endif // WORD64_AVIALABLE
616 #ifdef DO_SHA_ASM
618 // f1(x,y,z) (z^(x &(y^z)))
620 #define ASMf1(x,y,z) \
622 AS2( xor esi, z ) \
623 AS2( and esi, x ) \
628 // z+= f1(w,x,y) + W[i] + 0x5A827999 + rotlFixed(v,5);
631 // use esi for f
635 #define ASMR0(v,w,x,y,z,i) \
637 AS2( mov edi, [esp + i * 4] ) \
639 AS2( and esi, w ) \
640 AS2( lea z, [edi + z + 0x5A827999] ) \
651
652 #define xstr(s) str(s)
656 #define WOFF2(a) ((a + 2) & 15)
657 #define WOFF3(a) ((a + 8) & 15)
661 #define WGET1(i) asm("mov esp, [edi - "xstr(WOFF1(i))" * 4] ");
662 #define WGET2(i) asm("xor esp, [edi - "xstr(WOFF2(i))" * 4] ");
664 #define WGET4(i) asm("xor esp, [edi - "xstr(WOFF4(i))" * 4] ");
671 #define WPUT1(i) AS2( mov [edi - WOFF1(i) * 4], esp )
675 // ASMR1 = ASMR0 but use esp for W calcs
678 AS2( mov edi, [esp + W1 * 4] ) \
680 AS2( xor edi, [esp + W2 * 4] ) \
681 AS2( xor esi, y ) \
683 AS2( and esi, w ) \
685 AS2( rol edi, 1 ) \
687 AS2( mov [esp + W1 * 4], edi ) \
689 AS2( mov edi, v ) \
691 AS2( add z, esi ) \
693 AS2( add z, edi )
699 AS2( mov edi, [esp + W1 * 4] ) \
700 AS2( mov esi, x ) \
701 AS2( xor edi, [esp + W2 * 4] ) \
703 AS2( xor edi, [esp + W3 * 4] ) \
705 AS2( xor edi, [esp + W4 * 4] ) \
707 AS2( add z, esi ) \
709 AS2( lea z, [edi + z + 0x6ED9EBA1] ) \
711 AS2( rol edi, 5 ) \
713 AS2( add z, edi )
717 // which is (w&x)|(y&(w|x))
718
719 #define ASMR3(v,w,x,y,z,i,W1,W2,W3,W4) \
720 AS2( mov edi, [esp + W1 * 4] ) \
723 AS2( or esi, w ) \
726 AS2( xor edi, [esp + W4 * 4] ) \
728 AS2( rol edi, 1 ) \
730 AS2( mov [esp + W1 * 4], edi ) \
731 AS2( and esi, w ) \
733 AS2( movd edi, mm0 ) \
735 AS2( mov edi, v ) \
737 AS2( add z, esi ) \
738 AS2( rol w, 30 ) \
744 #define ASMR4(v,w,x,y,z,i,W1,W2,W3,W4) \
745 AS2( mov edi, [esp + W1 * 4] ) \
746 AS2( mov esi, x ) \
748 AS2( xor esi, y ) \
750 AS2( xor esi, w ) \
753 AS2( add z, esi ) \
755 AS2( lea z, [edi + z + 0xCA62C1D6] ) \
756 AS2( mov edi, v ) \
759 AS2( add z, edi )
765 __attribute__ ((noinline))
767 void SHA::AsmTransform(const byte* data, word32 times)
769 #ifdef __GNUC__
770 #define AS1(x) #x ";"
773 #define PROLOG() \
776 ".intel_syntax noprefix;" \
778 "push ebp;"
785 : "c" (this), "D" (data), "a" (times) \
786 : "%esi", "%edx", "memory", "cc" \
791 #define AS2(x, y) __asm x, y
794 AS1( push ebp ) \
795 AS2( mov ebp, esp ) \
797 AS2( movd mm4, ebx ) \
800 AS2( mov edi, data ) \
805 AS2( movd esi, mm5 ) \
806 AS2( movd ebx, mm4 ) \
807 AS2( movd edi, mm3 ) \
809 AS1( pop ebp ) \
811 AS1( ret 8 )
816 AS2( mov esi, ecx )
817
818 #ifdef OLD_GCC_OFFSET
820 #else
821 AS2( add esi, 16 ) // digest_[0]
822 #endif
823
824 AS2( movd mm2, eax ) // store times_
827 AS2( sub esp, 68 ) // make room on stack
832 AS1( 0: ) // loopStart for some gas (need numeric for jump back
835 // byte reverse 16 words of input, 4 at a time, put on stack for W[]
841 AS2( mov edx, [edi + 12] )
842
843 AS1( bswap eax )
844 AS1( bswap ebx )
848 AS2( mov [esp], eax )
849 AS2( mov [esp + 4], ebx )
850 AS2( mov [esp + 8], ecx )
853 // part 2
854 AS2( mov eax, [edi + 16] )
856 AS2( mov ecx, [edi + 24] )
857 AS2( mov edx, [edi + 28] )
861 AS1( bswap ecx )
862 AS1( bswap edx )
867 AS2( mov [esp + 28], edx )
870 // part 3
874 AS2( mov edx, [edi + 44] )
878 AS1( bswap ecx )
882 AS2( mov [esp + 36], ebx )
883 AS2( mov [esp + 40], ecx )
884 AS2( mov [esp + 44], edx )
887 // part 4
888 AS2( mov eax, [edi + 48] )
890 AS2( mov ecx, [edi + 56] )
893 AS1( bswap eax )
895 AS1( bswap ecx )
898 AS2( mov [esp + 48], eax )
899 AS2( mov [esp + 52], ebx )
901 AS2( mov [esp + 60], edx )
905 // read from digest_
907 AS2( mov ebx, [esi + 4] ) // b1
910 AS2( mov ebp, [esi + 16] ) // e1
914 ASMR0(ebp, eax, ebx, ecx, edx, 1)
917 ASMR0(ebx, ecx, edx, ebp, eax, 4)
919 ASMR0(ebp, eax, ebx, ecx, edx, 6)
921 ASMR0(ecx, edx, ebp, eax, ebx, 8)
922 ASMR0(ebx, ecx, edx, ebp, eax, 9)
926 ASMR0(ecx, edx, ebp, eax, ebx, 13)
930 ASMR1(ebp, eax, ebx, ecx, edx, 16, 0, 2, 8, 13)
931 ASMR1(edx, ebp, eax, ebx, ecx, 17, 1, 3, 9, 14)
933 ASMR1(ebx, ecx, edx, ebp, eax, 19, 3, 5, 11, 0)
935 ASMR2(eax, ebx, ecx, edx, ebp, 20, 4, 6, 12, 1)
937 ASMR2(edx, ebp, eax, ebx, ecx, 22, 6, 8, 14, 3)
938 ASMR2(ecx, edx, ebp, eax, ebx, 23, 7, 9, 15, 4)
940 ASMR2(eax, ebx, ecx, edx, ebp, 25, 9, 11, 1, 6)
942 ASMR2(edx, ebp, eax, ebx, ecx, 27, 11, 13, 3, 8)
943 ASMR2(ecx, edx, ebp, eax, ebx, 28, 12, 14, 4, 9)
944 ASMR2(ebx, ecx, edx, ebp, eax, 29, 13, 15, 5, 10)
946 ASMR2(ebp, eax, ebx, ecx, edx, 31, 15, 1, 7, 12)
953 ASMR2(ecx, edx, ebp, eax, ebx, 38, 6, 8, 14, 3)
954 ASMR2(ebx, ecx, edx, ebp, eax, 39, 7, 9, 15, 4)
958 ASMR3(ebp, eax, ebx, ecx, edx, 41, 9, 11, 1, 6)
961 ASMR3(ebx, ecx, edx, ebp, eax, 44, 12, 14, 4, 9)
962 ASMR3(eax, ebx, ecx, edx, ebp, 45, 13, 15, 5, 10)
964 ASMR3(edx, ebp, eax, ebx, ecx, 47, 15, 1, 7, 12)
966 ASMR3(ebx, ecx, edx, ebp, eax, 49, 1, 3, 9, 14)
968 ASMR3(ebp, eax, ebx, ecx, edx, 51, 3, 5, 11, 0)
978 ASMR4(eax, ebx, ecx, edx, ebp, 60, 12, 14, 4, 9)
980 ASMR4(edx, ebp, eax, ebx, ecx, 62, 14, 0, 6, 11)
987 ASMR4(ebx, ecx, edx, ebp, eax, 69, 5, 7, 13, 2)
990 ASMR4(edx, ebp, eax, ebx, ecx, 72, 8, 10, 0, 5)
991 ASMR4(ecx, edx, ebp, eax, ebx, 73, 9, 11, 1, 6)
992 ASMR4(ebx, ecx, edx, ebp, eax, 74, 10, 12, 2, 7)
994 ASMR4(ebp, eax, ebx, ecx, edx, 76, 12, 14, 4, 9)
997 ASMR4(ebx, ecx, edx, ebp, eax, 79, 15, 1, 7, 12)
1000 AS2( movd esi, mm1 ) // digest_
1003 AS2( add [esi + 4], ebx )
1005 AS2( add [esi + 12], edx )
1006 AS2( add [esi + 16], ebp )
1009 AS2( movd ebp, mm2 ) // times
1011 AS2( mov edi, DWORD PTR [esp + 64] ) // data
1013 AS2( add edi, 64 ) // next round of data
1014 AS2( mov [esp + 64], edi ) // restore
1018 #ifdef _MSC_VER
1019 AS1( jnz loopStart ) // loopStart
1020 #else
1025 AS2( add esp, 68 ) // fix room on stack
1027 EPILOG()