void InitSHA1State(ubsec_HMAC_State_pt HMAC_State,unsigned char *HashKey) { SHA_CTX ctx; unsigned char pad[64]; /* First prepare the inner block. */ bytnxor((unsigned char *)pad,(unsigned char *)HashKey,ipad, 64); /* Init the context, the initial values in memory are 01 23 45 ... */ RTL_MemZero(&ctx,sizeof(SHA_CTX)); ctx.buffer[0] = 0x67452301; ctx.buffer[1] = 0xefcdab89; ctx.buffer[2] = 0x98badcfe; ctx.buffer[3] = 0x10325476; ctx.buffer[4] = 0xc3d2e1f0; SHAUpdate(&ctx,pad,64); /* ctx comes out as an array of long ints. The byte order of ctx is dependent on the CPU endianess. The byte order of the memory destination is dependent on the CryptoNet memory endianess. Based on our SHA1 algorithm's CPU endianess assumptions, the net result is that we do a straight copy if the CPU and CryptoNet memory are of the same endianess. If the CPU and CryptoNet memory are of opposite endianess, we'll do 32-bit byteswapping during the copy, taken care of by the copywords() routine. */ #if (UBS_CPU_ATTRIBUTE != UBS_CRYPTONET_ATTRIBUTE) copywords((UBS_UINT32 *)&HMAC_State->InnerState[0],&ctx.buffer[0], SHA_HASH_LENGTH/4); #else RTL_Memcpy(&HMAC_State->InnerState[0],&ctx.buffer[0], SHA_HASH_LENGTH); #endif /* UBS_CPU_ATTRIBUTE */ /* Do do the same for the outer block */ bytnxor((unsigned char *)pad,(unsigned char *)HashKey, opad, 64); RTL_MemZero(&ctx,sizeof(SHA_CTX)); ctx.buffer[0] = 0x67452301; ctx.buffer[1] = 0xefcdab89; ctx.buffer[2] = 0x98badcfe; ctx.buffer[3] = 0x10325476; ctx.buffer[4] = 0xc3d2e1f0; SHAUpdate(&ctx, pad,64); /* ctx comes out as an array of long ints. The byte order of ctx is dependent on the CPU endianess. The byte order of the memory destination is dependent on the CryptoNet memory endianess. Based on our SHA1 algorithm's CPU endianess assumptions, the net result is that we do a straight copy if the CPU and CryptoNet memory are of the same endianess. If the CPU and CryptoNet memory are of opposite endianess, we'll do 32-bit byteswapping during the copy, taken care of by the copywords() routine. */ #if (UBS_CPU_ATTRIBUTE != UBS_CRYPTONET_ATTRIBUTE) copywords((UBS_UINT32 *)&HMAC_State->OuterState[0],&ctx.buffer[0], SHA_HASH_LENGTH/4); #else RTL_Memcpy(&HMAC_State->OuterState[0],&ctx.buffer[0], SHA_HASH_LENGTH); #endif /* UBS_CPU_ATTRIBUTE */ }
static void GetNewKeyFromSHA(u_char *StartKey, u_char *SessionKey, int SessionKeyLength, u_char *InterimKey) { u_char Digest[20]; SHA_CTX Context; ZeroMemory(Digest, 20); SHAInit(&Context); SHAUpdate(&Context, StartKey, SessionKeyLength); SHAUpdate(&Context, SHAPad1, 40); SHAUpdate(&Context, SessionKey, SessionKeyLength); SHAUpdate(&Context, SHAPad2, 40); SHAFinal(&Context, Digest); MoveMemory(InterimKey, Digest, SessionKeyLength); }