📄 sha1.c
字号:
/* ============================================================================ Project Name : jayaCard Module Name : proto/hal/crypto/sha1.c Version : $Id: sha1.c,v 1.13 2003/10/31 23:12:52 dgil Exp $ Description: SHA-1 The Original Code is jayaCard code. The Initial Developer of the Original Code is Gilles Dumortier. Portions created by the Initial Developer are Copyright (C) 2000-2003 the Initial Developer. All Rights Reserved. Contributor(s): This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see http://www.gnu.org/licenses/gpl.html History Rev Description 050700 dgil Wrote it from scratch 121700 dgil Add Test Vectors (from FIPS PUB 180-1) 020903 dgil Port it to this project ============================================================================*/#include "precomp.h"#ifdef JAYACFG_SOFTWARE_SHA1/* ========================================================================= __crypto_SHA1_transform() ========================================================================= */jdword __SHA1_K(jbyte t){ if (t<=19) return 0x5A827999; if (t<=39) return 0x6ED9EBA1; if (t<=59) return 0x8F1BBCDC; return 0xCA62C1D6;}jdword __SHA1_F(jdword B,jdword C,jdword D,jbyte t){ if (t<=19) return (B & C) | ( (~B) & D); if (t<=39) goto out; if (t<=59) return (B & C) | (B & D) | (C & D);out: return B ^ C ^ D;}void __crypto_SHA1_transform(void){ LOCAL(jdword,A); LOCAL(jdword,B); LOCAL(jdword,C); LOCAL(jdword,D); LOCAL(jdword,E); LOCAL(jdword,TEMP); LOCAL(jbyte,t); LOCAL(jbyte,s); A = r.dwBlock[JAYA_DWCRYPTO_DIGEST_0]; B = r.dwBlock[JAYA_DWCRYPTO_DIGEST_1]; C = r.dwBlock[JAYA_DWCRYPTO_DIGEST_2]; D = r.dwBlock[JAYA_DWCRYPTO_DIGEST_3]; E = r.dwBlock[JAYA_DWCRYPTO_DIGEST_4]; for (t=0; t<80; t++) { s = t & 0x0F; if (t>=16) { u.dwBlock[s] = HAL_ROL32(u.dwBlock[(s+13)&0x0F]^u.dwBlock[(s+8)&0x0F] ^ u.dwBlock[(s+2)&0x0F]^u.dwBlock[s],1); } else { blk0(s); } TEMP = HAL_ROL32(A,5)+ __SHA1_F(B,C,D,t) + E + u.dwBlock[s] + __SHA1_K(t); E = D; D = C; C = HAL_ROL32(B,30); B = A; A = TEMP; } r.dwBlock[JAYA_DWCRYPTO_DIGEST_0] += A; r.dwBlock[JAYA_DWCRYPTO_DIGEST_1] += B; r.dwBlock[JAYA_DWCRYPTO_DIGEST_2] += C; r.dwBlock[JAYA_DWCRYPTO_DIGEST_3] += D; r.dwBlock[JAYA_DWCRYPTO_DIGEST_4] += E;}/* ========================================================================= __crypto_SHA1_init() ========================================================================= */jdword code SHA1_INIT[7] = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, 0, 0 };void __crypto_SHA1_init(void){ LOCAL(jbyte,i); for (i=0;i<7;i++) { r.dwBlock[i] = SHA1_INIT[i]; }}/* ========================================================================= __crypto_SHA1_update() ========================================================================= */void __crypto_SHA1_update(jbyte xdata* buffer,jbyte len){ LOCAL(jword,i); LOCAL(jword,j); j = (jword)((r.dwBlock[JAYA_DWCRYPTO_COUNT_0] >> 3) & 63); if ((r.dwBlock[JAYA_DWCRYPTO_COUNT_0] += (jword)(len << 3)) < (jword)(len << 3)) r.dwBlock[JAYA_DWCRYPTO_COUNT_1]++; /* r.dwBlock[JAYA_DWCRYPTO_COUNT_1] += (len >> 29); our length is never so big ? */ if ((j + len) > 63) { HAL_MEMCPY(&u.bBlock[j], buffer, (jbyte)(i = 64-j)); __crypto_SHA1_transform(); while ((i + 63) < len) { HAL_MEMCPY(u.bBlock, buffer+i, 64); __crypto_SHA1_transform(); i += 64; } j = 0; } else { i = 0; } HAL_MEMCPY(&u.bBlock[j], buffer+i, len - i);}/* ========================================================================= __crypto_SHA1_final() ========================================================================= */void __crypto_SHA1_final(void){ LOCAL(jbyte,b); /* save current buffer length - endianess independent ! */ u.bBlock[64] = HIBYTE(HIWORD(r.dwBlock[JAYA_DWCRYPTO_COUNT_1])); u.bBlock[64+1] = LOBYTE(HIWORD(r.dwBlock[JAYA_DWCRYPTO_COUNT_1])); u.bBlock[64+2] = HIBYTE(LOWORD(r.dwBlock[JAYA_DWCRYPTO_COUNT_1])); u.bBlock[64+3] = LOBYTE(LOWORD(r.dwBlock[JAYA_DWCRYPTO_COUNT_1])); u.bBlock[64+4] = HIBYTE(HIWORD(r.dwBlock[JAYA_DWCRYPTO_COUNT_0])); u.bBlock[64+5] = LOBYTE(HIWORD(r.dwBlock[JAYA_DWCRYPTO_COUNT_0])); u.bBlock[64+6] = HIBYTE(LOWORD(r.dwBlock[JAYA_DWCRYPTO_COUNT_0])); u.bBlock[64+7] = LOBYTE(LOWORD(r.dwBlock[JAYA_DWCRYPTO_COUNT_0])); /* pad with 10000000000000000000000.... */ b = '\200'; __crypto_SHA1_update((jbyte xdata*)&b, 1); while ((r.dwBlock[JAYA_DWCRYPTO_COUNT_0] & 504) != 448) { b = '\0'; __crypto_SHA1_update((jbyte xdata*)&b, 1); } /* should transform ! */ __crypto_SHA1_update(&u.bBlock[64], 8); /* erase temp */ HAL_ERASE_XRAM(u.bBlock,sizeof(CRYPTO_BUFFER));}/* ========================================================================= That's all folks ! ========================================================================= */#endif/* JAYACFG_SOFTWARE_SHA1 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -