diff --git a/OpenCL/inc_hash_constants.h b/OpenCL/inc_hash_constants.h index 8c0835a1e..8ac7cedd0 100644 --- a/OpenCL/inc_hash_constants.h +++ b/OpenCL/inc_hash_constants.h @@ -3,34 +3,6 @@ * License.....: MIT */ -#ifdef SHARED_H -#define _AXCRYPT_ -#define _BCRYPT_ -#define _CLOUDKEY_ -#define _KECCAK_ -#define _MD4_ -#define _MD5_ -#define _MD5H_ -#define _OFFICE2013_ -#define _PBKDF2_MD5_ -#define _PBKDF2_SHA1_ -#define _PBKDF2_SHA256_ -#define _PBKDF2_SHA512_ -#define _PDF17L8_ -#define _RAR3_ -#define _RIPEMD160_ -#define _SAPB_ -#define _SHA1_ -#define _SHA256_ -#define _SHA384_ -#define _SHA512_ -#define _SIPHASH_ -#define _WHIRLPOOL_ -#define _ZIP2_ -#endif - -#ifdef _SIPHASH_ - typedef enum siphash_constants { SIPHASHM_0=0x736f6d6570736575, @@ -40,250 +12,226 @@ typedef enum siphash_constants } siphash_constants_t; -#endif - -#if defined _BCRYPT_ || defined _PSAFE2_ - typedef enum bcrypt_constants { - BCRYPTM_0=0x4F727068u, - BCRYPTM_1=0x65616E42u, - BCRYPTM_2=0x65686F6Cu, - BCRYPTM_3=0x64657253u, - BCRYPTM_4=0x63727944u, + BCRYPTM_0=0x4F727068, + BCRYPTM_1=0x65616E42, + BCRYPTM_2=0x65686F6C, + BCRYPTM_3=0x64657253, + BCRYPTM_4=0x63727944, BCRYPTM_5=0x6F756274u } bcrypt_constants_t; -#endif - -#if defined _SHA1_ || defined _SAPG_ || defined _OFFICE2007_ || defined _OFFICE2010_ || defined _OLDOFFICE34_ || defined _ANDROIDFDE_ || defined _DCC2_ || defined _WPA_ || defined _MD5_SHA1_ || defined _SHA1_MD5_ || defined _PSAFE2_ || defined _LOTUS8_ || defined _PBKDF2_SHA1_ || defined _RAR3_ || defined _SHA256_SHA1_ || defined _ZIP2_ || defined _AXCRYPT_ - -typedef enum sha1_constants -{ - SHA1M_A=0x67452301u, - SHA1M_B=0xefcdab89u, - SHA1M_C=0x98badcfeu, - SHA1M_D=0x10325476u, - SHA1M_E=0xc3d2e1f0u, - - SHA1C00=0x5a827999u, - SHA1C01=0x6ed9eba1u, - SHA1C02=0x8f1bbcdcu, - SHA1C03=0xca62c1d6u - -} sha1_constants_t; - -#endif - -#if defined _SHA256_ || defined _PDF17L8_ || defined _SEVEN_ZIP_ || defined _ANDROIDFDE_ || defined _CLOUDKEY_ || defined _SCRYPT_ || defined _PBKDF2_SHA256_ || defined _SHA256_SHA1_ || defined _MS_DRSR_ || defined _ANDROIDFDE_SAMSUNG_ || defined _RAR5_ || defined _KEEPASS_ - -typedef enum sha256_constants -{ - SHA256M_A=0x6a09e667u, - SHA256M_B=0xbb67ae85u, - SHA256M_C=0x3c6ef372u, - SHA256M_D=0xa54ff53au, - SHA256M_E=0x510e527fu, - SHA256M_F=0x9b05688cu, - SHA256M_G=0x1f83d9abu, - SHA256M_H=0x5be0cd19u, - - SHA256C00=0x428a2f98u, - SHA256C01=0x71374491u, - SHA256C02=0xb5c0fbcfu, - SHA256C03=0xe9b5dba5u, - SHA256C04=0x3956c25bu, - SHA256C05=0x59f111f1u, - SHA256C06=0x923f82a4u, - SHA256C07=0xab1c5ed5u, - SHA256C08=0xd807aa98u, - SHA256C09=0x12835b01u, - SHA256C0a=0x243185beu, - SHA256C0b=0x550c7dc3u, - SHA256C0c=0x72be5d74u, - SHA256C0d=0x80deb1feu, - SHA256C0e=0x9bdc06a7u, - SHA256C0f=0xc19bf174u, - SHA256C10=0xe49b69c1u, - SHA256C11=0xefbe4786u, - SHA256C12=0x0fc19dc6u, - SHA256C13=0x240ca1ccu, - SHA256C14=0x2de92c6fu, - SHA256C15=0x4a7484aau, - SHA256C16=0x5cb0a9dcu, - SHA256C17=0x76f988dau, - SHA256C18=0x983e5152u, - SHA256C19=0xa831c66du, - SHA256C1a=0xb00327c8u, - SHA256C1b=0xbf597fc7u, - SHA256C1c=0xc6e00bf3u, - SHA256C1d=0xd5a79147u, - SHA256C1e=0x06ca6351u, - SHA256C1f=0x14292967u, - SHA256C20=0x27b70a85u, - SHA256C21=0x2e1b2138u, - SHA256C22=0x4d2c6dfcu, - SHA256C23=0x53380d13u, - SHA256C24=0x650a7354u, - SHA256C25=0x766a0abbu, - SHA256C26=0x81c2c92eu, - SHA256C27=0x92722c85u, - SHA256C28=0xa2bfe8a1u, - SHA256C29=0xa81a664bu, - SHA256C2a=0xc24b8b70u, - SHA256C2b=0xc76c51a3u, - SHA256C2c=0xd192e819u, - SHA256C2d=0xd6990624u, - SHA256C2e=0xf40e3585u, - SHA256C2f=0x106aa070u, - SHA256C30=0x19a4c116u, - SHA256C31=0x1e376c08u, - SHA256C32=0x2748774cu, - SHA256C33=0x34b0bcb5u, - SHA256C34=0x391c0cb3u, - SHA256C35=0x4ed8aa4au, - SHA256C36=0x5b9cca4fu, - SHA256C37=0x682e6ff3u, - SHA256C38=0x748f82eeu, - SHA256C39=0x78a5636fu, - SHA256C3a=0x84c87814u, - SHA256C3b=0x8cc70208u, - SHA256C3c=0x90befffau, - SHA256C3d=0xa4506cebu, - SHA256C3e=0xbef9a3f7u, - SHA256C3f=0xc67178f2u - -} sha256_constants_t; - -#endif - -#if defined _MD4_ || defined _DCC2_ || defined _NETNTLMV2_ || defined _KRB5PA_ || defined _MS_DRSR_ || defined _KRB5TGS_ - typedef enum md4_constants { - MD4M_A=0x67452301u, - MD4M_B=0xefcdab89u, - MD4M_C=0x98badcfeu, - MD4M_D=0x10325476u, + MD4M_A=0x67452301, + MD4M_B=0xefcdab89, + MD4M_C=0x98badcfe, + MD4M_D=0x10325476, - MD4S00=3u, - MD4S01=7u, - MD4S02=11u, - MD4S03=19u, - MD4S10=3u, - MD4S11=5u, - MD4S12=9u, - MD4S13=13u, - MD4S20=3u, - MD4S21=9u, - MD4S22=11u, - MD4S23=15u, + MD4S00=3, + MD4S01=7, + MD4S02=11, + MD4S03=19, + MD4S10=3, + MD4S11=5, + MD4S12=9, + MD4S13=13, + MD4S20=3, + MD4S21=9, + MD4S22=11, + MD4S23=15, - MD4C00=0x00000000u, - MD4C01=0x5a827999u, + MD4C00=0x00000000, + MD4C01=0x5a827999, MD4C02=0x6ed9eba1u } md4_constants_t; -#endif - -#if defined _MD5_ || defined _MD5H_ || defined _SAPB_ || defined _OLDOFFICE01_ || defined _WPA_ || defined _MD5_SHA1_ || defined _SHA1_MD5_ || defined _NETNTLMV2_ || defined _KRB5PA_ || defined _PBKDF2_MD5_ || defined _KRB5TGS_ - typedef enum md5_constants { - MD5M_A=0x67452301u, - MD5M_B=0xefcdab89u, - MD5M_C=0x98badcfeu, - MD5M_D=0x10325476u, + MD5M_A=0x67452301, + MD5M_B=0xefcdab89, + MD5M_C=0x98badcfe, + MD5M_D=0x10325476, - MD5S00=7u, - MD5S01=12u, - MD5S02=17u, - MD5S03=22u, - MD5S10=5u, - MD5S11=9u, - MD5S12=14u, - MD5S13=20u, - MD5S20=4u, - MD5S21=11u, - MD5S22=16u, - MD5S23=23u, - MD5S30=6u, - MD5S31=10u, - MD5S32=15u, - MD5S33=21u, + MD5S00=7, + MD5S01=12, + MD5S02=17, + MD5S03=22, + MD5S10=5, + MD5S11=9, + MD5S12=14, + MD5S13=20, + MD5S20=4, + MD5S21=11, + MD5S22=16, + MD5S23=23, + MD5S30=6, + MD5S31=10, + MD5S32=15, + MD5S33=21, - MD5C00=0xd76aa478u, - MD5C01=0xe8c7b756u, - MD5C02=0x242070dbu, - MD5C03=0xc1bdceeeu, - MD5C04=0xf57c0fafu, - MD5C05=0x4787c62au, - MD5C06=0xa8304613u, - MD5C07=0xfd469501u, - MD5C08=0x698098d8u, - MD5C09=0x8b44f7afu, - MD5C0a=0xffff5bb1u, - MD5C0b=0x895cd7beu, - MD5C0c=0x6b901122u, - MD5C0d=0xfd987193u, - MD5C0e=0xa679438eu, - MD5C0f=0x49b40821u, - MD5C10=0xf61e2562u, - MD5C11=0xc040b340u, - MD5C12=0x265e5a51u, - MD5C13=0xe9b6c7aau, - MD5C14=0xd62f105du, - MD5C15=0x02441453u, - MD5C16=0xd8a1e681u, - MD5C17=0xe7d3fbc8u, - MD5C18=0x21e1cde6u, - MD5C19=0xc33707d6u, - MD5C1a=0xf4d50d87u, - MD5C1b=0x455a14edu, - MD5C1c=0xa9e3e905u, - MD5C1d=0xfcefa3f8u, - MD5C1e=0x676f02d9u, - MD5C1f=0x8d2a4c8au, - MD5C20=0xfffa3942u, - MD5C21=0x8771f681u, - MD5C22=0x6d9d6122u, - MD5C23=0xfde5380cu, - MD5C24=0xa4beea44u, - MD5C25=0x4bdecfa9u, - MD5C26=0xf6bb4b60u, - MD5C27=0xbebfbc70u, - MD5C28=0x289b7ec6u, - MD5C29=0xeaa127fau, - MD5C2a=0xd4ef3085u, - MD5C2b=0x04881d05u, - MD5C2c=0xd9d4d039u, - MD5C2d=0xe6db99e5u, - MD5C2e=0x1fa27cf8u, - MD5C2f=0xc4ac5665u, - MD5C30=0xf4292244u, - MD5C31=0x432aff97u, - MD5C32=0xab9423a7u, - MD5C33=0xfc93a039u, - MD5C34=0x655b59c3u, - MD5C35=0x8f0ccc92u, - MD5C36=0xffeff47du, - MD5C37=0x85845dd1u, - MD5C38=0x6fa87e4fu, - MD5C39=0xfe2ce6e0u, - MD5C3a=0xa3014314u, - MD5C3b=0x4e0811a1u, - MD5C3c=0xf7537e82u, - MD5C3d=0xbd3af235u, - MD5C3e=0x2ad7d2bbu, + MD5C00=0xd76aa478, + MD5C01=0xe8c7b756, + MD5C02=0x242070db, + MD5C03=0xc1bdceee, + MD5C04=0xf57c0faf, + MD5C05=0x4787c62a, + MD5C06=0xa8304613, + MD5C07=0xfd469501, + MD5C08=0x698098d8, + MD5C09=0x8b44f7af, + MD5C0a=0xffff5bb1, + MD5C0b=0x895cd7be, + MD5C0c=0x6b901122, + MD5C0d=0xfd987193, + MD5C0e=0xa679438e, + MD5C0f=0x49b40821, + MD5C10=0xf61e2562, + MD5C11=0xc040b340, + MD5C12=0x265e5a51, + MD5C13=0xe9b6c7aa, + MD5C14=0xd62f105d, + MD5C15=0x02441453, + MD5C16=0xd8a1e681, + MD5C17=0xe7d3fbc8, + MD5C18=0x21e1cde6, + MD5C19=0xc33707d6, + MD5C1a=0xf4d50d87, + MD5C1b=0x455a14ed, + MD5C1c=0xa9e3e905, + MD5C1d=0xfcefa3f8, + MD5C1e=0x676f02d9, + MD5C1f=0x8d2a4c8a, + MD5C20=0xfffa3942, + MD5C21=0x8771f681, + MD5C22=0x6d9d6122, + MD5C23=0xfde5380c, + MD5C24=0xa4beea44, + MD5C25=0x4bdecfa9, + MD5C26=0xf6bb4b60, + MD5C27=0xbebfbc70, + MD5C28=0x289b7ec6, + MD5C29=0xeaa127fa, + MD5C2a=0xd4ef3085, + MD5C2b=0x04881d05, + MD5C2c=0xd9d4d039, + MD5C2d=0xe6db99e5, + MD5C2e=0x1fa27cf8, + MD5C2f=0xc4ac5665, + MD5C30=0xf4292244, + MD5C31=0x432aff97, + MD5C32=0xab9423a7, + MD5C33=0xfc93a039, + MD5C34=0x655b59c3, + MD5C35=0x8f0ccc92, + MD5C36=0xffeff47d, + MD5C37=0x85845dd1, + MD5C38=0x6fa87e4f, + MD5C39=0xfe2ce6e0, + MD5C3a=0xa3014314, + MD5C3b=0x4e0811a1, + MD5C3c=0xf7537e82, + MD5C3d=0xbd3af235, + MD5C3e=0x2ad7d2bb, MD5C3f=0xeb86d391u } md5_constants_t; -#endif +typedef enum sha1_constants +{ + SHA1M_A=0x67452301, + SHA1M_B=0xefcdab89, + SHA1M_C=0x98badcfe, + SHA1M_D=0x10325476, + SHA1M_E=0xc3d2e1f0, -#if defined _SHA384_ || defined _PDF17L8_ + SHA1C00=0x5a827999, + SHA1C01=0x6ed9eba1, + SHA1C02=0x8f1bbcdc, + SHA1C03=0xca62c1d6u + +} sha1_constants_t; + +typedef enum sha256_constants +{ + SHA256M_A=0x6a09e667, + SHA256M_B=0xbb67ae85, + SHA256M_C=0x3c6ef372, + SHA256M_D=0xa54ff53a, + SHA256M_E=0x510e527f, + SHA256M_F=0x9b05688c, + SHA256M_G=0x1f83d9ab, + SHA256M_H=0x5be0cd19, + + SHA256C00=0x428a2f98, + SHA256C01=0x71374491, + SHA256C02=0xb5c0fbcf, + SHA256C03=0xe9b5dba5, + SHA256C04=0x3956c25b, + SHA256C05=0x59f111f1, + SHA256C06=0x923f82a4, + SHA256C07=0xab1c5ed5, + SHA256C08=0xd807aa98, + SHA256C09=0x12835b01, + SHA256C0a=0x243185be, + SHA256C0b=0x550c7dc3, + SHA256C0c=0x72be5d74, + SHA256C0d=0x80deb1fe, + SHA256C0e=0x9bdc06a7, + SHA256C0f=0xc19bf174, + SHA256C10=0xe49b69c1, + SHA256C11=0xefbe4786, + SHA256C12=0x0fc19dc6, + SHA256C13=0x240ca1cc, + SHA256C14=0x2de92c6f, + SHA256C15=0x4a7484aa, + SHA256C16=0x5cb0a9dc, + SHA256C17=0x76f988da, + SHA256C18=0x983e5152, + SHA256C19=0xa831c66d, + SHA256C1a=0xb00327c8, + SHA256C1b=0xbf597fc7, + SHA256C1c=0xc6e00bf3, + SHA256C1d=0xd5a79147, + SHA256C1e=0x06ca6351, + SHA256C1f=0x14292967, + SHA256C20=0x27b70a85, + SHA256C21=0x2e1b2138, + SHA256C22=0x4d2c6dfc, + SHA256C23=0x53380d13, + SHA256C24=0x650a7354, + SHA256C25=0x766a0abb, + SHA256C26=0x81c2c92e, + SHA256C27=0x92722c85, + SHA256C28=0xa2bfe8a1, + SHA256C29=0xa81a664b, + SHA256C2a=0xc24b8b70, + SHA256C2b=0xc76c51a3, + SHA256C2c=0xd192e819, + SHA256C2d=0xd6990624, + SHA256C2e=0xf40e3585, + SHA256C2f=0x106aa070, + SHA256C30=0x19a4c116, + SHA256C31=0x1e376c08, + SHA256C32=0x2748774c, + SHA256C33=0x34b0bcb5, + SHA256C34=0x391c0cb3, + SHA256C35=0x4ed8aa4a, + SHA256C36=0x5b9cca4f, + SHA256C37=0x682e6ff3, + SHA256C38=0x748f82ee, + SHA256C39=0x78a5636f, + SHA256C3a=0x84c87814, + SHA256C3b=0x8cc70208, + SHA256C3c=0x90befffa, + SHA256C3d=0xa4506ceb, + SHA256C3e=0xbef9a3f7, + SHA256C3f=0xc67178f2u + +} sha256_constants_t; typedef enum sha384_constants { @@ -379,10 +327,6 @@ typedef enum sha384_constants } sha384_constants_t; -#endif - -#if defined _SHA512_ || defined _CLOUDKEY_ || defined _OFFICE2013_ || defined _PDF17L8_ || defined _PBKDF2_SHA512_ - typedef enum sha512_constants { SHA512M_A=0x6a09e667f3bcc908, @@ -477,205 +421,197 @@ typedef enum sha512_constants } sha512_constants_t; -#endif - -#ifdef _RIPEMD160_ - typedef enum ripemd160_constants { - RIPEMD160M_A=0x67452301u, - RIPEMD160M_B=0xefcdab89u, - RIPEMD160M_C=0x98badcfeu, - RIPEMD160M_D=0x10325476u, - RIPEMD160M_E=0xc3d2e1f0u, + RIPEMD160M_A=0x67452301, + RIPEMD160M_B=0xefcdab89, + RIPEMD160M_C=0x98badcfe, + RIPEMD160M_D=0x10325476, + RIPEMD160M_E=0xc3d2e1f0, - RIPEMD160C00=0x00000000u, - RIPEMD160C10=0x5a827999u, - RIPEMD160C20=0x6ed9eba1u, - RIPEMD160C30=0x8f1bbcdcu, - RIPEMD160C40=0xa953fd4eu, - RIPEMD160C50=0x50a28be6u, - RIPEMD160C60=0x5c4dd124u, - RIPEMD160C70=0x6d703ef3u, - RIPEMD160C80=0x7a6d76e9u, - RIPEMD160C90=0x00000000u, + RIPEMD160C00=0x00000000, + RIPEMD160C10=0x5a827999, + RIPEMD160C20=0x6ed9eba1, + RIPEMD160C30=0x8f1bbcdc, + RIPEMD160C40=0xa953fd4e, + RIPEMD160C50=0x50a28be6, + RIPEMD160C60=0x5c4dd124, + RIPEMD160C70=0x6d703ef3, + RIPEMD160C80=0x7a6d76e9, + RIPEMD160C90=0x00000000, - RIPEMD160S00=11u, - RIPEMD160S01=14u, - RIPEMD160S02=15u, - RIPEMD160S03=12u, - RIPEMD160S04=5u, - RIPEMD160S05=8u, - RIPEMD160S06=7u, - RIPEMD160S07=9u, - RIPEMD160S08=11u, - RIPEMD160S09=13u, - RIPEMD160S0A=14u, - RIPEMD160S0B=15u, - RIPEMD160S0C=6u, - RIPEMD160S0D=7u, - RIPEMD160S0E=9u, - RIPEMD160S0F=8u, + RIPEMD160S00=11, + RIPEMD160S01=14, + RIPEMD160S02=15, + RIPEMD160S03=12, + RIPEMD160S04=5, + RIPEMD160S05=8, + RIPEMD160S06=7, + RIPEMD160S07=9, + RIPEMD160S08=11, + RIPEMD160S09=13, + RIPEMD160S0A=14, + RIPEMD160S0B=15, + RIPEMD160S0C=6, + RIPEMD160S0D=7, + RIPEMD160S0E=9, + RIPEMD160S0F=8, - RIPEMD160S10=7u, - RIPEMD160S11=6u, - RIPEMD160S12=8u, - RIPEMD160S13=13u, - RIPEMD160S14=11u, - RIPEMD160S15=9u, - RIPEMD160S16=7u, - RIPEMD160S17=15u, - RIPEMD160S18=7u, - RIPEMD160S19=12u, - RIPEMD160S1A=15u, - RIPEMD160S1B=9u, - RIPEMD160S1C=11u, - RIPEMD160S1D=7u, - RIPEMD160S1E=13u, - RIPEMD160S1F=12u, + RIPEMD160S10=7, + RIPEMD160S11=6, + RIPEMD160S12=8, + RIPEMD160S13=13, + RIPEMD160S14=11, + RIPEMD160S15=9, + RIPEMD160S16=7, + RIPEMD160S17=15, + RIPEMD160S18=7, + RIPEMD160S19=12, + RIPEMD160S1A=15, + RIPEMD160S1B=9, + RIPEMD160S1C=11, + RIPEMD160S1D=7, + RIPEMD160S1E=13, + RIPEMD160S1F=12, - RIPEMD160S20=11u, - RIPEMD160S21=13u, - RIPEMD160S22=6u, - RIPEMD160S23=7u, - RIPEMD160S24=14u, - RIPEMD160S25=9u, - RIPEMD160S26=13u, - RIPEMD160S27=15u, - RIPEMD160S28=14u, - RIPEMD160S29=8u, - RIPEMD160S2A=13u, - RIPEMD160S2B=6u, - RIPEMD160S2C=5u, - RIPEMD160S2D=12u, - RIPEMD160S2E=7u, - RIPEMD160S2F=5u, + RIPEMD160S20=11, + RIPEMD160S21=13, + RIPEMD160S22=6, + RIPEMD160S23=7, + RIPEMD160S24=14, + RIPEMD160S25=9, + RIPEMD160S26=13, + RIPEMD160S27=15, + RIPEMD160S28=14, + RIPEMD160S29=8, + RIPEMD160S2A=13, + RIPEMD160S2B=6, + RIPEMD160S2C=5, + RIPEMD160S2D=12, + RIPEMD160S2E=7, + RIPEMD160S2F=5, - RIPEMD160S30=11u, - RIPEMD160S31=12u, - RIPEMD160S32=14u, - RIPEMD160S33=15u, - RIPEMD160S34=14u, - RIPEMD160S35=15u, - RIPEMD160S36=9u, - RIPEMD160S37=8u, - RIPEMD160S38=9u, - RIPEMD160S39=14u, - RIPEMD160S3A=5u, - RIPEMD160S3B=6u, - RIPEMD160S3C=8u, - RIPEMD160S3D=6u, - RIPEMD160S3E=5u, - RIPEMD160S3F=12u, + RIPEMD160S30=11, + RIPEMD160S31=12, + RIPEMD160S32=14, + RIPEMD160S33=15, + RIPEMD160S34=14, + RIPEMD160S35=15, + RIPEMD160S36=9, + RIPEMD160S37=8, + RIPEMD160S38=9, + RIPEMD160S39=14, + RIPEMD160S3A=5, + RIPEMD160S3B=6, + RIPEMD160S3C=8, + RIPEMD160S3D=6, + RIPEMD160S3E=5, + RIPEMD160S3F=12, - RIPEMD160S40=9u, - RIPEMD160S41=15u, - RIPEMD160S42=5u, - RIPEMD160S43=11u, - RIPEMD160S44=6u, - RIPEMD160S45=8u, - RIPEMD160S46=13u, - RIPEMD160S47=12u, - RIPEMD160S48=5u, - RIPEMD160S49=12u, - RIPEMD160S4A=13u, - RIPEMD160S4B=14u, - RIPEMD160S4C=11u, - RIPEMD160S4D=8u, - RIPEMD160S4E=5u, - RIPEMD160S4F=6u, + RIPEMD160S40=9, + RIPEMD160S41=15, + RIPEMD160S42=5, + RIPEMD160S43=11, + RIPEMD160S44=6, + RIPEMD160S45=8, + RIPEMD160S46=13, + RIPEMD160S47=12, + RIPEMD160S48=5, + RIPEMD160S49=12, + RIPEMD160S4A=13, + RIPEMD160S4B=14, + RIPEMD160S4C=11, + RIPEMD160S4D=8, + RIPEMD160S4E=5, + RIPEMD160S4F=6, - RIPEMD160S50=8u, - RIPEMD160S51=9u, - RIPEMD160S52=9u, - RIPEMD160S53=11u, - RIPEMD160S54=13u, - RIPEMD160S55=15u, - RIPEMD160S56=15u, - RIPEMD160S57=5u, - RIPEMD160S58=7u, - RIPEMD160S59=7u, - RIPEMD160S5A=8u, - RIPEMD160S5B=11u, - RIPEMD160S5C=14u, - RIPEMD160S5D=14u, - RIPEMD160S5E=12u, - RIPEMD160S5F=6u, + RIPEMD160S50=8, + RIPEMD160S51=9, + RIPEMD160S52=9, + RIPEMD160S53=11, + RIPEMD160S54=13, + RIPEMD160S55=15, + RIPEMD160S56=15, + RIPEMD160S57=5, + RIPEMD160S58=7, + RIPEMD160S59=7, + RIPEMD160S5A=8, + RIPEMD160S5B=11, + RIPEMD160S5C=14, + RIPEMD160S5D=14, + RIPEMD160S5E=12, + RIPEMD160S5F=6, - RIPEMD160S60=9u, - RIPEMD160S61=13u, - RIPEMD160S62=15u, - RIPEMD160S63=7u, - RIPEMD160S64=12u, - RIPEMD160S65=8u, - RIPEMD160S66=9u, - RIPEMD160S67=11u, - RIPEMD160S68=7u, - RIPEMD160S69=7u, - RIPEMD160S6A=12u, - RIPEMD160S6B=7u, - RIPEMD160S6C=6u, - RIPEMD160S6D=15u, - RIPEMD160S6E=13u, - RIPEMD160S6F=11u, + RIPEMD160S60=9, + RIPEMD160S61=13, + RIPEMD160S62=15, + RIPEMD160S63=7, + RIPEMD160S64=12, + RIPEMD160S65=8, + RIPEMD160S66=9, + RIPEMD160S67=11, + RIPEMD160S68=7, + RIPEMD160S69=7, + RIPEMD160S6A=12, + RIPEMD160S6B=7, + RIPEMD160S6C=6, + RIPEMD160S6D=15, + RIPEMD160S6E=13, + RIPEMD160S6F=11, - RIPEMD160S70=9u, - RIPEMD160S71=7u, - RIPEMD160S72=15u, - RIPEMD160S73=11u, - RIPEMD160S74=8u, - RIPEMD160S75=6u, - RIPEMD160S76=6u, - RIPEMD160S77=14u, - RIPEMD160S78=12u, - RIPEMD160S79=13u, - RIPEMD160S7A=5u, - RIPEMD160S7B=14u, - RIPEMD160S7C=13u, - RIPEMD160S7D=13u, - RIPEMD160S7E=7u, - RIPEMD160S7F=5u, + RIPEMD160S70=9, + RIPEMD160S71=7, + RIPEMD160S72=15, + RIPEMD160S73=11, + RIPEMD160S74=8, + RIPEMD160S75=6, + RIPEMD160S76=6, + RIPEMD160S77=14, + RIPEMD160S78=12, + RIPEMD160S79=13, + RIPEMD160S7A=5, + RIPEMD160S7B=14, + RIPEMD160S7C=13, + RIPEMD160S7D=13, + RIPEMD160S7E=7, + RIPEMD160S7F=5, - RIPEMD160S80=15u, - RIPEMD160S81=5u, - RIPEMD160S82=8u, - RIPEMD160S83=11u, - RIPEMD160S84=14u, - RIPEMD160S85=14u, - RIPEMD160S86=6u, - RIPEMD160S87=14u, - RIPEMD160S88=6u, - RIPEMD160S89=9u, - RIPEMD160S8A=12u, - RIPEMD160S8B=9u, - RIPEMD160S8C=12u, - RIPEMD160S8D=5u, - RIPEMD160S8E=15u, - RIPEMD160S8F=8u, + RIPEMD160S80=15, + RIPEMD160S81=5, + RIPEMD160S82=8, + RIPEMD160S83=11, + RIPEMD160S84=14, + RIPEMD160S85=14, + RIPEMD160S86=6, + RIPEMD160S87=14, + RIPEMD160S88=6, + RIPEMD160S89=9, + RIPEMD160S8A=12, + RIPEMD160S8B=9, + RIPEMD160S8C=12, + RIPEMD160S8D=5, + RIPEMD160S8E=15, + RIPEMD160S8F=8, - RIPEMD160S90=8u, - RIPEMD160S91=5u, - RIPEMD160S92=12u, - RIPEMD160S93=9u, - RIPEMD160S94=12u, - RIPEMD160S95=5u, - RIPEMD160S96=14u, - RIPEMD160S97=6u, - RIPEMD160S98=8u, - RIPEMD160S99=13u, - RIPEMD160S9A=6u, - RIPEMD160S9B=5u, - RIPEMD160S9C=15u, - RIPEMD160S9D=13u, - RIPEMD160S9E=11u, + RIPEMD160S90=8, + RIPEMD160S91=5, + RIPEMD160S92=12, + RIPEMD160S93=9, + RIPEMD160S94=12, + RIPEMD160S95=5, + RIPEMD160S96=14, + RIPEMD160S97=6, + RIPEMD160S98=8, + RIPEMD160S99=13, + RIPEMD160S9A=6, + RIPEMD160S9B=5, + RIPEMD160S9C=15, + RIPEMD160S9D=13, + RIPEMD160S9E=11, RIPEMD160S9F=11u } ripemd160_constants_t; -#endif - -#ifdef _KECCAK_ - typedef enum keccak_constants { KECCAK_RNDC_00=0x0000000000000001, @@ -703,67 +639,61 @@ typedef enum keccak_constants KECCAK_RNDC_22=0x0000000080000001, KECCAK_RNDC_23=0x0000000080008008, - KECCAK_PILN_00=10u, - KECCAK_PILN_01=7u, - KECCAK_PILN_02=11u, - KECCAK_PILN_03=17u, - KECCAK_PILN_04=18u, - KECCAK_PILN_05=3u, - KECCAK_PILN_06=5u, - KECCAK_PILN_07=16u, - KECCAK_PILN_08=8u, - KECCAK_PILN_09=21u, - KECCAK_PILN_10=24u, - KECCAK_PILN_11=4u, - KECCAK_PILN_12=15u, - KECCAK_PILN_13=23u, - KECCAK_PILN_14=19u, - KECCAK_PILN_15=13u, - KECCAK_PILN_16=12u, - KECCAK_PILN_17=2u, - KECCAK_PILN_18=20u, - KECCAK_PILN_19=14u, - KECCAK_PILN_20=22u, - KECCAK_PILN_21=9u, - KECCAK_PILN_22=6u, - KECCAK_PILN_23=1u, + KECCAK_PILN_00=10, + KECCAK_PILN_01=7, + KECCAK_PILN_02=11, + KECCAK_PILN_03=17, + KECCAK_PILN_04=18, + KECCAK_PILN_05=3, + KECCAK_PILN_06=5, + KECCAK_PILN_07=16, + KECCAK_PILN_08=8, + KECCAK_PILN_09=21, + KECCAK_PILN_10=24, + KECCAK_PILN_11=4, + KECCAK_PILN_12=15, + KECCAK_PILN_13=23, + KECCAK_PILN_14=19, + KECCAK_PILN_15=13, + KECCAK_PILN_16=12, + KECCAK_PILN_17=2, + KECCAK_PILN_18=20, + KECCAK_PILN_19=14, + KECCAK_PILN_20=22, + KECCAK_PILN_21=9, + KECCAK_PILN_22=6, + KECCAK_PILN_23=1, - KECCAK_ROTC_00=1u, - KECCAK_ROTC_01=3u, - KECCAK_ROTC_02=6u, - KECCAK_ROTC_03=10u, - KECCAK_ROTC_04=15u, - KECCAK_ROTC_05=21u, - KECCAK_ROTC_06=28u, - KECCAK_ROTC_07=36u, - KECCAK_ROTC_08=45u, - KECCAK_ROTC_09=55u, - KECCAK_ROTC_10=2u, - KECCAK_ROTC_11=14u, - KECCAK_ROTC_12=27u, - KECCAK_ROTC_13=41u, - KECCAK_ROTC_14=56u, - KECCAK_ROTC_15=8u, - KECCAK_ROTC_16=25u, - KECCAK_ROTC_17=43u, - KECCAK_ROTC_18=62u, - KECCAK_ROTC_19=18u, - KECCAK_ROTC_20=39u, - KECCAK_ROTC_21=61u, - KECCAK_ROTC_22=20u, - KECCAK_ROTC_23=44u, + KECCAK_ROTC_00=1, + KECCAK_ROTC_01=3, + KECCAK_ROTC_02=6, + KECCAK_ROTC_03=10, + KECCAK_ROTC_04=15, + KECCAK_ROTC_05=21, + KECCAK_ROTC_06=28, + KECCAK_ROTC_07=36, + KECCAK_ROTC_08=45, + KECCAK_ROTC_09=55, + KECCAK_ROTC_10=2, + KECCAK_ROTC_11=14, + KECCAK_ROTC_12=27, + KECCAK_ROTC_13=41, + KECCAK_ROTC_14=56, + KECCAK_ROTC_15=8, + KECCAK_ROTC_16=25, + KECCAK_ROTC_17=43, + KECCAK_ROTC_18=62, + KECCAK_ROTC_19=18, + KECCAK_ROTC_20=39, + KECCAK_ROTC_21=61, + KECCAK_ROTC_22=20, + KECCAK_ROTC_23=44, } keccak_constants_t; -#endif - -#ifdef _MYSQL323_ - typedef enum mysql323_constants { - MYSQL323_A=0x50305735u, + MYSQL323_A=0x50305735, MYSQL323_B=0x12345671u } mysql323_constants_t; - -#endif diff --git a/OpenCL/inc_hash_functions.cl b/OpenCL/inc_hash_functions.cl index 9e2a58d32..673716aca 100644 --- a/OpenCL/inc_hash_functions.cl +++ b/OpenCL/inc_hash_functions.cl @@ -3,8 +3,6 @@ * License.....: MIT */ -#if defined _MD4_ || defined _DCC2_ || defined _NETNTLMV2_ || defined _KRB5PA_ || defined _MS_DRSR_ || defined _KRB5TGS_ - #define MD4_F_S(x,y,z) (((x) & (y)) | ((~(x)) & (z))) #define MD4_G_S(x,y,z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) #define MD4_H_S(x,y,z) ((x) ^ (y) ^ (z)) @@ -56,10 +54,6 @@ a = rotl32 (a, s); \ } -#endif - -#if defined _MD5_ || defined _MD5H_ || defined _SAPB_ || defined _OLDOFFICE01_ || defined _WPA_ || defined _MD5_SHA1_ || defined _SHA1_MD5_ || defined _NETNTLMV2_ || defined _KRB5PA_ || defined _PBKDF2_MD5_ || defined _KRB5TGS_ - #define MD5_F_S(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) #define MD5_G_S(x,y,z) ((y) ^ ((z) & ((x) ^ (y)))) #define MD5_H_S(x,y,z) ((x) ^ (y) ^ (z)) @@ -117,9 +111,6 @@ a = rotl32 (a, s); \ a += b; \ } -#endif - -#if defined _SHA1_ || defined _SAPG_ || defined _OFFICE2007_ || defined _OFFICE2010_ || defined _OLDOFFICE34_ || defined _ANDROIDFDE_ || defined _DCC2_ || defined _WPA_ || defined _MD5_SHA1_ || defined _SHA1_MD5_ || defined _PSAFE2_ || defined _LOTUS8_ || defined _PBKDF2_SHA1_ || defined _RAR3_ || defined _SHA256_SHA1_ || defined _ZIP2_ || defined _AXCRYPT_ #ifdef IS_NV #define SHA1_F0(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) @@ -191,9 +182,6 @@ e += K; \ b = rotl32 (b, 30u); \ } -#endif - -#if defined _SHA256_ || defined _PDF17L8_ || defined _SEVEN_ZIP_ || defined _ANDROIDFDE_ || defined _CLOUDKEY_ || defined _SCRYPT_ || defined _PBKDF2_SHA256_ || defined _SHA256_SHA1_ || defined _MS_DRSR_ || defined _ANDROIDFDE_SAMSUNG_ || defined _RAR5_ || defined _KEEPASS_ #define SHIFT_RIGHT_32(x,n) ((x) >> (n)) @@ -254,10 +242,6 @@ #define SHA256_EXPAND(x,y,z,w) (SHA256_S1 (x) + y + SHA256_S0 (z) + w) -#endif - -#if defined _SHA384_ || defined _PDF17L8_ - #define SHIFT_RIGHT_64(x,n) ((x) >> (n)) #define SHA384_S0(x) (rotr64 ((x), 28) ^ rotr64 ((x), 34) ^ rotr64 ((x), 39)) @@ -295,9 +279,6 @@ } #define SHA384_EXPAND(x,y,z,w) (SHA384_S3 (x) + y + SHA384_S2 (z) + w) -#endif - -#if defined _SHA512_ || defined _CLOUDKEY_ || defined _OFFICE2013_ || defined _PDF17L8_ || defined _PBKDF2_SHA512_ #define SHIFT_RIGHT_64(x,n) ((x) >> (n)) @@ -354,9 +335,6 @@ } #define SHA512_EXPAND(x,y,z,w) (SHA512_S3 (x) + y + SHA512_S2 (z) + w) -#endif - -#ifdef _RIPEMD160_ #ifdef IS_NV #define RIPEMD160_F(x,y,z) ((x) ^ (y) ^ (z)) @@ -409,5 +387,3 @@ a += e; \ c = rotl32 (c, 10u); \ } - -#endif diff --git a/include/bitops.h b/include/bitops.h index 8f40ee408..c7cd4dc55 100644 --- a/include/bitops.h +++ b/include/bitops.h @@ -5,8 +5,6 @@ #pragma once -#include "common.h" - u32 is_power_of_2 (const u32 v); u32 rotl32 (const u32 a, const u32 n); diff --git a/include/common.h b/include/common.h index 1690db880..f4fcff188 100644 --- a/include/common.h +++ b/include/common.h @@ -5,112 +5,13 @@ * License.....: MIT */ -#ifndef COMMON_H -#define COMMON_H +#pragma once #define _POSIX_SOURCE #define _GNU_SOURCE #define _FILE_OFFSET_BITS 64 #define _CRT_SECURE_NO_WARNINGS -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef _POSIX -#include -#include -#include -#include -#include -#include - -#ifdef __linux__ -#include -#include -#endif - -#ifdef __APPLE__ -#include -#include -#include -#include -#endif - -#ifdef __FreeBSD__ -#include -#include -#endif - -typedef void *OCL_LIB; - -#ifdef HAVE_HWMON -typedef void *ADL_LIB; -typedef void *NVAPI_LIB; -typedef void *NVML_LIB; -typedef void *XNVCTRL_LIB; -#ifdef __APPLE__ -#define __stdcall -#endif -#endif - -#endif // _POSIX - -#ifdef _WIN -#define WIN32_LEAN_AND_MEAN -#include -#include -#include -#include -#include -#include - -#ifndef _BASETSD_H -typedef UINT8 uint8_t; -typedef UINT16 uint16_t; -typedef UINT32 uint32_t; -typedef UINT64 uint64_t; -typedef INT8 int8_t; -typedef INT16 int16_t; -typedef INT32 int32_t; -typedef INT64 int64_t; -#endif - -typedef HINSTANCE OCL_LIB; - -#ifdef HAVE_HWMON -typedef HINSTANCE ADL_LIB; -typedef HINSTANCE NVAPI_LIB; -typedef HINSTANCE NVML_LIB; -typedef HINSTANCE XNVCTRL_LIB; -#endif - -#define mkdir(name,mode) mkdir (name) - -#endif // _WIN - -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; - -typedef uint32_t uint; // we need to get rid of this sooner or later, for consistency - #define EXEC_CACHE 128 #define SPEED_CACHE 128 @@ -120,21 +21,17 @@ typedef uint32_t uint; // we need to get rid of this sooner or later, for consis #define EXPECTED_ITERATIONS 10000 -/** - * functions - */ - -int log_out_nn (FILE *fp, const char *fmt, ...); -int log_info_nn (const char *fmt, ...); -int log_error_nn (const char *fmt, ...); - -int log_out (FILE *fp, const char *fmt, ...); -int log_info (const char *fmt, ...); -int log_error (const char *fmt, ...); - #define MIN(a,b) (((a) < (b)) ? (a) : (b)) #define MAX(a,b) (((a) > (b)) ? (a) : (b)) +#define DEVICES_MAX 128 + #define CEIL(a) ((a - (int) (a)) > 0 ? a + 1 : a) -#endif // COMMON_H +#ifdef __APPLE__ +#define __stdcall +#endif + +#ifdef _WIN +#define WIN32_LEAN_AND_MEAN +#endif diff --git a/include/cpu-aes.h b/include/cpu-aes.h deleted file mode 100644 index d256ef28d..000000000 --- a/include/cpu-aes.h +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Author......: Jens Steube - * License.....: MIT - */ - -#ifndef AES_H -#define AES_H - -/* AES context. */ -typedef struct aes_context -{ - u32 bits; - - u32 rek[60]; - u32 rdk[60]; - -} aes_context_t; - -typedef aes_context_t aes_ctx; - -#define AES_KEY aes_ctx -void AES_set_encrypt_key (const u8 *key, int keysize, AES_KEY *aes_key); -void AES_set_decrypt_key (const u8 *key, int keysize, AES_KEY *aes_key); -void AES_encrypt (AES_KEY *aes_key, const u8 *input, u8 *output); -void AES_decrypt (AES_KEY *aes_key, const u8 *input, u8 *output); - -#endif diff --git a/include/cpu-des.h b/include/cpu-des.h deleted file mode 100644 index 5cfc82629..000000000 --- a/include/cpu-des.h +++ /dev/null @@ -1,2 +0,0 @@ -void _des_keysetup (u32 data[2], u32 Kc[16], u32 Kd[16], const u32 s_skb[8][64]); -void _des_encrypt (u32 data[2], u32 Kc[16], u32 Kd[16], const u32 s_SPtrans[8][64]); diff --git a/include/cpu-md5.c b/include/cpu-md5.c deleted file mode 100644 index 94f955e26..000000000 --- a/include/cpu-md5.c +++ /dev/null @@ -1,285 +0,0 @@ -#define MAGIC_A 0x67452301 -#define MAGIC_B 0xefcdab89 -#define MAGIC_C 0x98badcfe -#define MAGIC_D 0x10325476 - -#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) -#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) -#define H(x, y, z) ((x) ^ (y) ^ (z)) -#define I(x, y, z) ((y) ^ ((x) | (~z))) - -#define FF(a, b, c, d, x, s, ac) \ - {(a) += F ((b), (c), (d)) + (x) + (ac); \ - (a) = rotl32 ((a), (s)); \ - (a) += (b); \ - } -#define GG(a, b, c, d, x, s, ac) \ - {(a) += G ((b), (c), (d)) + (x) + (ac); \ - (a) = rotl32 ((a), (s)); \ - (a) += (b); \ - } -#define HH(a, b, c, d, x, s, ac) \ - {(a) += H ((b), (c), (d)) + (x) + (ac); \ - (a) = rotl32 ((a), (s)); \ - (a) += (b); \ - } -#define II(a, b, c, d, x, s, ac) \ - {(a) += I ((b), (c), (d)) + (x) + (ac); \ - (a) = rotl32 ((a), (s)); \ - (a) += (b); \ - } - -#define S11 7 -#define S12 12 -#define S13 17 -#define S14 22 -#define S21 5 -#define S22 9 -#define S23 14 -#define S24 20 -#define S31 4 -#define S32 11 -#define S33 16 -#define S34 23 -#define S41 6 -#define S42 10 -#define S43 15 -#define S44 21 - -void md5_64 (uint block[16], uint digest[4]) -{ - uint a = digest[0]; - uint b = digest[1]; - uint c = digest[2]; - uint d = digest[3]; - - FF ( a, b, c, d, block[ 0], S11, 0xd76aa478); - FF ( d, a, b, c, block[ 1], S12, 0xe8c7b756); - FF ( c, d, a, b, block[ 2], S13, 0x242070db); - FF ( b, c, d, a, block[ 3], S14, 0xc1bdceee); - FF ( a, b, c, d, block[ 4], S11, 0xf57c0faf); - FF ( d, a, b, c, block[ 5], S12, 0x4787c62a); - FF ( c, d, a, b, block[ 6], S13, 0xa8304613); - FF ( b, c, d, a, block[ 7], S14, 0xfd469501); - FF ( a, b, c, d, block[ 8], S11, 0x698098d8); - FF ( d, a, b, c, block[ 9], S12, 0x8b44f7af); - FF ( c, d, a, b, block[10], S13, 0xffff5bb1); - FF ( b, c, d, a, block[11], S14, 0x895cd7be); - FF ( a, b, c, d, block[12], S11, 0x6b901122); - FF ( d, a, b, c, block[13], S12, 0xfd987193); - FF ( c, d, a, b, block[14], S13, 0xa679438e); - FF ( b, c, d, a, block[15], S14, 0x49b40821); - - GG ( a, b, c, d, block[ 1], S21, 0xf61e2562); - GG ( d, a, b, c, block[ 6], S22, 0xc040b340); - GG ( c, d, a, b, block[11], S23, 0x265e5a51); - GG ( b, c, d, a, block[ 0], S24, 0xe9b6c7aa); - GG ( a, b, c, d, block[ 5], S21, 0xd62f105d); - GG ( d, a, b, c, block[10], S22, 0x02441453); - GG ( c, d, a, b, block[15], S23, 0xd8a1e681); - GG ( b, c, d, a, block[ 4], S24, 0xe7d3fbc8); - GG ( a, b, c, d, block[ 9], S21, 0x21e1cde6); - GG ( d, a, b, c, block[14], S22, 0xc33707d6); - GG ( c, d, a, b, block[ 3], S23, 0xf4d50d87); - GG ( b, c, d, a, block[ 8], S24, 0x455a14ed); - GG ( a, b, c, d, block[13], S21, 0xa9e3e905); - GG ( d, a, b, c, block[ 2], S22, 0xfcefa3f8); - GG ( c, d, a, b, block[ 7], S23, 0x676f02d9); - GG ( b, c, d, a, block[12], S24, 0x8d2a4c8a); - - HH ( a, b, c, d, block[ 5], S31, 0xfffa3942); - HH ( d, a, b, c, block[ 8], S32, 0x8771f681); - HH ( c, d, a, b, block[11], S33, 0x6d9d6122); - HH ( b, c, d, a, block[14], S34, 0xfde5380c); - HH ( a, b, c, d, block[ 1], S31, 0xa4beea44); - HH ( d, a, b, c, block[ 4], S32, 0x4bdecfa9); - HH ( c, d, a, b, block[ 7], S33, 0xf6bb4b60); - HH ( b, c, d, a, block[10], S34, 0xbebfbc70); - HH ( a, b, c, d, block[13], S31, 0x289b7ec6); - HH ( d, a, b, c, block[ 0], S32, 0xeaa127fa); - HH ( c, d, a, b, block[ 3], S33, 0xd4ef3085); - HH ( b, c, d, a, block[ 6], S34, 0x04881d05); - HH ( a, b, c, d, block[ 9], S31, 0xd9d4d039); - HH ( d, a, b, c, block[12], S32, 0xe6db99e5); - HH ( c, d, a, b, block[15], S33, 0x1fa27cf8); - HH ( b, c, d, a, block[ 2], S34, 0xc4ac5665); - - II ( a, b, c, d, block[ 0], S41, 0xf4292244); - II ( d, a, b, c, block[ 7], S42, 0x432aff97); - II ( c, d, a, b, block[14], S43, 0xab9423a7); - II ( b, c, d, a, block[ 5], S44, 0xfc93a039); - II ( a, b, c, d, block[12], S41, 0x655b59c3); - II ( d, a, b, c, block[ 3], S42, 0x8f0ccc92); - II ( c, d, a, b, block[10], S43, 0xffeff47d); - II ( b, c, d, a, block[ 1], S44, 0x85845dd1); - II ( a, b, c, d, block[ 8], S41, 0x6fa87e4f); - II ( d, a, b, c, block[15], S42, 0xfe2ce6e0); - II ( c, d, a, b, block[ 6], S43, 0xa3014314); - II ( b, c, d, a, block[13], S44, 0x4e0811a1); - II ( a, b, c, d, block[ 4], S41, 0xf7537e82); - II ( d, a, b, c, block[11], S42, 0xbd3af235); - II ( c, d, a, b, block[ 2], S43, 0x2ad7d2bb); - II ( b, c, d, a, block[ 9], S44, 0xeb86d391); - - digest[0] += a; - digest[1] += b; - digest[2] += c; - digest[3] += d; -} - -// only use this when really, really needed, SLOW - -void md5_complete_no_limit (uint digest[4], uint *plain, uint plain_len) -{ - uint a = MAGIC_A; - uint b = MAGIC_B; - uint c = MAGIC_C; - uint d = MAGIC_D; - - digest[0] = a; - digest[1] = b; - digest[2] = c; - digest[3] = d; - - uint r_a = digest[0]; - uint r_b = digest[1]; - uint r_c = digest[2]; - uint r_d = digest[3]; - - uint block[16]; - int block_total_len = 16 * 4; // sizeof (block) - - char *block_ptr = (char *) block; - char *plain_ptr = (char *) plain; - - // init - - int remaining_len = plain_len; - - // loop - - uint loop = 1; - - while (loop) - { - loop = (remaining_len > 55); - - int cur_len = MIN (block_total_len, remaining_len); - int copy_len = MAX (cur_len, 0); // should never be negative of course - - memcpy (block_ptr, plain_ptr, copy_len); - - // clear the remaining bytes of the block - - memset (block_ptr + copy_len, 0, block_total_len - copy_len); - - /* - * final block - */ - - // set 0x80 if neeeded - - if (cur_len >= 0) - { - if (cur_len != block_total_len) - { - block_ptr[copy_len] = 0x80; - } - } - - // set block[14] set to total_len - - if (! loop) block[14] = plain_len * 8; - - /* - * md5 () - */ - - FF ( a, b, c, d, block[ 0], S11, 0xd76aa478); - FF ( d, a, b, c, block[ 1], S12, 0xe8c7b756); - FF ( c, d, a, b, block[ 2], S13, 0x242070db); - FF ( b, c, d, a, block[ 3], S14, 0xc1bdceee); - FF ( a, b, c, d, block[ 4], S11, 0xf57c0faf); - FF ( d, a, b, c, block[ 5], S12, 0x4787c62a); - FF ( c, d, a, b, block[ 6], S13, 0xa8304613); - FF ( b, c, d, a, block[ 7], S14, 0xfd469501); - FF ( a, b, c, d, block[ 8], S11, 0x698098d8); - FF ( d, a, b, c, block[ 9], S12, 0x8b44f7af); - FF ( c, d, a, b, block[10], S13, 0xffff5bb1); - FF ( b, c, d, a, block[11], S14, 0x895cd7be); - FF ( a, b, c, d, block[12], S11, 0x6b901122); - FF ( d, a, b, c, block[13], S12, 0xfd987193); - FF ( c, d, a, b, block[14], S13, 0xa679438e); - FF ( b, c, d, a, block[15], S14, 0x49b40821); - - GG ( a, b, c, d, block[ 1], S21, 0xf61e2562); - GG ( d, a, b, c, block[ 6], S22, 0xc040b340); - GG ( c, d, a, b, block[11], S23, 0x265e5a51); - GG ( b, c, d, a, block[ 0], S24, 0xe9b6c7aa); - GG ( a, b, c, d, block[ 5], S21, 0xd62f105d); - GG ( d, a, b, c, block[10], S22, 0x02441453); - GG ( c, d, a, b, block[15], S23, 0xd8a1e681); - GG ( b, c, d, a, block[ 4], S24, 0xe7d3fbc8); - GG ( a, b, c, d, block[ 9], S21, 0x21e1cde6); - GG ( d, a, b, c, block[14], S22, 0xc33707d6); - GG ( c, d, a, b, block[ 3], S23, 0xf4d50d87); - GG ( b, c, d, a, block[ 8], S24, 0x455a14ed); - GG ( a, b, c, d, block[13], S21, 0xa9e3e905); - GG ( d, a, b, c, block[ 2], S22, 0xfcefa3f8); - GG ( c, d, a, b, block[ 7], S23, 0x676f02d9); - GG ( b, c, d, a, block[12], S24, 0x8d2a4c8a); - - HH ( a, b, c, d, block[ 5], S31, 0xfffa3942); - HH ( d, a, b, c, block[ 8], S32, 0x8771f681); - HH ( c, d, a, b, block[11], S33, 0x6d9d6122); - HH ( b, c, d, a, block[14], S34, 0xfde5380c); - HH ( a, b, c, d, block[ 1], S31, 0xa4beea44); - HH ( d, a, b, c, block[ 4], S32, 0x4bdecfa9); - HH ( c, d, a, b, block[ 7], S33, 0xf6bb4b60); - HH ( b, c, d, a, block[10], S34, 0xbebfbc70); - HH ( a, b, c, d, block[13], S31, 0x289b7ec6); - HH ( d, a, b, c, block[ 0], S32, 0xeaa127fa); - HH ( c, d, a, b, block[ 3], S33, 0xd4ef3085); - HH ( b, c, d, a, block[ 6], S34, 0x04881d05); - HH ( a, b, c, d, block[ 9], S31, 0xd9d4d039); - HH ( d, a, b, c, block[12], S32, 0xe6db99e5); - HH ( c, d, a, b, block[15], S33, 0x1fa27cf8); - HH ( b, c, d, a, block[ 2], S34, 0xc4ac5665); - - II ( a, b, c, d, block[ 0], S41, 0xf4292244); - II ( d, a, b, c, block[ 7], S42, 0x432aff97); - II ( c, d, a, b, block[14], S43, 0xab9423a7); - II ( b, c, d, a, block[ 5], S44, 0xfc93a039); - II ( a, b, c, d, block[12], S41, 0x655b59c3); - II ( d, a, b, c, block[ 3], S42, 0x8f0ccc92); - II ( c, d, a, b, block[10], S43, 0xffeff47d); - II ( b, c, d, a, block[ 1], S44, 0x85845dd1); - II ( a, b, c, d, block[ 8], S41, 0x6fa87e4f); - II ( d, a, b, c, block[15], S42, 0xfe2ce6e0); - II ( c, d, a, b, block[ 6], S43, 0xa3014314); - II ( b, c, d, a, block[13], S44, 0x4e0811a1); - II ( a, b, c, d, block[ 4], S41, 0xf7537e82); - II ( d, a, b, c, block[11], S42, 0xbd3af235); - II ( c, d, a, b, block[ 2], S43, 0x2ad7d2bb); - II ( b, c, d, a, block[ 9], S44, 0xeb86d391); - - remaining_len -= block_total_len; - - plain_ptr += 64; - - a += r_a; - b += r_b; - c += r_c; - d += r_d; - - digest[0] = a; - digest[1] = b; - digest[2] = c; - digest[3] = d; - - r_a = digest[0]; - r_b = digest[1]; - r_c = digest[2]; - r_d = digest[3]; - } -} diff --git a/include/cpu-sha1.h b/include/cpu-sha1.h deleted file mode 100644 index ba893af59..000000000 --- a/include/cpu-sha1.h +++ /dev/null @@ -1 +0,0 @@ -void sha1_64 (uint block[16], uint digest[5]); diff --git a/include/cpu-sha256.h b/include/cpu-sha256.h deleted file mode 100644 index e4c2ac9a8..000000000 --- a/include/cpu-sha256.h +++ /dev/null @@ -1 +0,0 @@ -void sha256_64 (uint block[16], uint digest[8]); diff --git a/include/cpu-aes.c b/include/cpu_aes.h similarity index 53% rename from include/cpu-aes.c rename to include/cpu_aes.h index c9ef4b78b..36690b001 100644 --- a/include/cpu-aes.c +++ b/include/cpu_aes.h @@ -3,6 +3,20 @@ * License.....: MIT */ +#pragma once + +/* AES context. */ +typedef struct aes_context +{ + u32 bits; + + u32 rek[60]; + u32 rdk[60]; + +} aes_context_t; + +typedef aes_context_t aes_ctx; + static const u32 te0[256] = { 0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, @@ -690,620 +704,8 @@ static const u32 rcon[] = 0x1b000000, 0x36000000, }; -// 128 bit - -static void AES128_ExpandKey (const u32 *userkey, u32 *rek) -{ - u32 userkey_s[4]; - - userkey_s[0] = byte_swap_32 (userkey[0]); - userkey_s[1] = byte_swap_32 (userkey[1]); - userkey_s[2] = byte_swap_32 (userkey[2]); - userkey_s[3] = byte_swap_32 (userkey[3]); - - rek[0] = userkey_s[0]; - rek[1] = userkey_s[1]; - rek[2] = userkey_s[2]; - rek[3] = userkey_s[3]; - - int i; - int j; - - for (i = 0, j = 0; i < 10; i += 1, j += 4) - { - u32 temp = rek[j + 3]; - - temp = (te2[(temp >> 16) & 0xff] & 0xff000000) - ^ (te3[(temp >> 8) & 0xff] & 0x00ff0000) - ^ (te0[(temp >> 0) & 0xff] & 0x0000ff00) - ^ (te1[(temp >> 24) & 0xff] & 0x000000ff); - - rek[j + 4] = rek[j + 0] - ^ temp - ^ rcon[i]; - - rek[j + 5] = rek[j + 1] ^ rek[j + 4]; - rek[j + 6] = rek[j + 2] ^ rek[j + 5]; - rek[j + 7] = rek[j + 3] ^ rek[j + 6]; - } -} - -static void AES128_InvertKey (u32 *rdk) -{ - int i; - int j; - - for (i = 0, j = 40; i < j; i += 4, j -= 4) - { - u32 temp; - - temp = rdk[i + 0]; rdk[i + 0] = rdk[j + 0]; rdk[j + 0] = temp; - temp = rdk[i + 1]; rdk[i + 1] = rdk[j + 1]; rdk[j + 1] = temp; - temp = rdk[i + 2]; rdk[i + 2] = rdk[j + 2]; rdk[j + 2] = temp; - temp = rdk[i + 3]; rdk[i + 3] = rdk[j + 3]; rdk[j + 3] = temp; - } - - for (i = 1, j = 4; i < 10; i += 1, j += 4) - { - rdk[j + 0] = - td0[te1[(rdk[j + 0] >> 24) & 0xff] & 0xff] ^ - td1[te1[(rdk[j + 0] >> 16) & 0xff] & 0xff] ^ - td2[te1[(rdk[j + 0] >> 8) & 0xff] & 0xff] ^ - td3[te1[(rdk[j + 0] >> 0) & 0xff] & 0xff]; - - rdk[j + 1] = - td0[te1[(rdk[j + 1] >> 24) & 0xff] & 0xff] ^ - td1[te1[(rdk[j + 1] >> 16) & 0xff] & 0xff] ^ - td2[te1[(rdk[j + 1] >> 8) & 0xff] & 0xff] ^ - td3[te1[(rdk[j + 1] >> 0) & 0xff] & 0xff]; - - rdk[j + 2] = - td0[te1[(rdk[j + 2] >> 24) & 0xff] & 0xff] ^ - td1[te1[(rdk[j + 2] >> 16) & 0xff] & 0xff] ^ - td2[te1[(rdk[j + 2] >> 8) & 0xff] & 0xff] ^ - td3[te1[(rdk[j + 2] >> 0) & 0xff] & 0xff]; - - rdk[j + 3] = - td0[te1[(rdk[j + 3] >> 24) & 0xff] & 0xff] ^ - td1[te1[(rdk[j + 3] >> 16) & 0xff] & 0xff] ^ - td2[te1[(rdk[j + 3] >> 8) & 0xff] & 0xff] ^ - td3[te1[(rdk[j + 3] >> 0) & 0xff] & 0xff]; - } -} - -static void AES128_encrypt (const u32 *in, u32 *out, const u32 *rek) -{ - u32 in_s[4]; - - in_s[0] = byte_swap_32 (in[0]); - in_s[1] = byte_swap_32 (in[1]); - in_s[2] = byte_swap_32 (in[2]); - in_s[3] = byte_swap_32 (in[3]); - - u32 s0 = in_s[0] ^ rek[0]; - u32 s1 = in_s[1] ^ rek[1]; - u32 s2 = in_s[2] ^ rek[2]; - u32 s3 = in_s[3] ^ rek[3]; - - u32 t0; - u32 t1; - u32 t2; - u32 t3; - - t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[ 4]; - t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[ 5]; - t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[ 6]; - t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[ 7]; - s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[ 8]; - s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[ 9]; - s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[10]; - s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[11]; - t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[12]; - t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[13]; - t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[14]; - t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[15]; - s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[16]; - s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[17]; - s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[18]; - s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[19]; - t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[20]; - t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[21]; - t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[22]; - t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[23]; - s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[24]; - s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[25]; - s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[26]; - s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[27]; - t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[28]; - t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[29]; - t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[30]; - t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[31]; - s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[32]; - s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[33]; - s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[34]; - s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[35]; - t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[36]; - t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[37]; - t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[38]; - t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[39]; - - out[0] = (te4[(t0 >> 24) & 0xff] & 0xff000000) - ^ (te4[(t1 >> 16) & 0xff] & 0x00ff0000) - ^ (te4[(t2 >> 8) & 0xff] & 0x0000ff00) - ^ (te4[(t3 >> 0) & 0xff] & 0x000000ff) - ^ rek[40]; - - out[1] = (te4[(t1 >> 24) & 0xff] & 0xff000000) - ^ (te4[(t2 >> 16) & 0xff] & 0x00ff0000) - ^ (te4[(t3 >> 8) & 0xff] & 0x0000ff00) - ^ (te4[(t0 >> 0) & 0xff] & 0x000000ff) - ^ rek[41]; - - out[2] = (te4[(t2 >> 24) & 0xff] & 0xff000000) - ^ (te4[(t3 >> 16) & 0xff] & 0x00ff0000) - ^ (te4[(t0 >> 8) & 0xff] & 0x0000ff00) - ^ (te4[(t1 >> 0) & 0xff] & 0x000000ff) - ^ rek[42]; - - out[3] = (te4[(t3 >> 24) & 0xff] & 0xff000000) - ^ (te4[(t0 >> 16) & 0xff] & 0x00ff0000) - ^ (te4[(t1 >> 8) & 0xff] & 0x0000ff00) - ^ (te4[(t2 >> 0) & 0xff] & 0x000000ff) - ^ rek[43]; - - out[0] = byte_swap_32 (out[0]); - out[1] = byte_swap_32 (out[1]); - out[2] = byte_swap_32 (out[2]); - out[3] = byte_swap_32 (out[3]); -} - -static void AES128_decrypt (const u32 *in, u32 *out, const u32 *rdk) -{ - u32 in_s[4]; - - in_s[0] = byte_swap_32 (in[0]); - in_s[1] = byte_swap_32 (in[1]); - in_s[2] = byte_swap_32 (in[2]); - in_s[3] = byte_swap_32 (in[3]); - - u32 s0 = in_s[0] ^ rdk[0]; - u32 s1 = in_s[1] ^ rdk[1]; - u32 s2 = in_s[2] ^ rdk[2]; - u32 s3 = in_s[3] ^ rdk[3]; - - u32 t0; - u32 t1; - u32 t2; - u32 t3; - - t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[ 4]; - t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[ 5]; - t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[ 6]; - t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[ 7]; - s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[ 8]; - s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[ 9]; - s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[10]; - s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[11]; - t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[12]; - t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[13]; - t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[14]; - t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[15]; - s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[16]; - s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[17]; - s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[18]; - s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[19]; - t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[20]; - t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[21]; - t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[22]; - t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[23]; - s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[24]; - s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[25]; - s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[26]; - s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[27]; - t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[28]; - t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[29]; - t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[30]; - t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[31]; - s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[32]; - s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[33]; - s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[34]; - s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[35]; - t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[36]; - t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[37]; - t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[38]; - t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[39]; - - out[0] = (td4[(t0 >> 24) & 0xff] & 0xff000000) - ^ (td4[(t3 >> 16) & 0xff] & 0x00ff0000) - ^ (td4[(t2 >> 8) & 0xff] & 0x0000ff00) - ^ (td4[(t1 >> 0) & 0xff] & 0x000000ff) - ^ rdk[40]; - - out[1] = (td4[(t1 >> 24) & 0xff] & 0xff000000) - ^ (td4[(t0 >> 16) & 0xff] & 0x00ff0000) - ^ (td4[(t3 >> 8) & 0xff] & 0x0000ff00) - ^ (td4[(t2 >> 0) & 0xff] & 0x000000ff) - ^ rdk[41]; - - out[2] = (td4[(t2 >> 24) & 0xff] & 0xff000000) - ^ (td4[(t1 >> 16) & 0xff] & 0x00ff0000) - ^ (td4[(t0 >> 8) & 0xff] & 0x0000ff00) - ^ (td4[(t3 >> 0) & 0xff] & 0x000000ff) - ^ rdk[42]; - - out[3] = (td4[(t3 >> 24) & 0xff] & 0xff000000) - ^ (td4[(t2 >> 16) & 0xff] & 0x00ff0000) - ^ (td4[(t1 >> 8) & 0xff] & 0x0000ff00) - ^ (td4[(t0 >> 0) & 0xff] & 0x000000ff) - ^ rdk[43]; - - out[0] = byte_swap_32 (out[0]); - out[1] = byte_swap_32 (out[1]); - out[2] = byte_swap_32 (out[2]); - out[3] = byte_swap_32 (out[3]); -} - -// 256 bit - -static void AES256_ExpandKey (const u32 *userkey, u32 *rek) -{ - u32 userkey_s[8]; - - userkey_s[0] = byte_swap_32 (userkey[0]); - userkey_s[1] = byte_swap_32 (userkey[1]); - userkey_s[2] = byte_swap_32 (userkey[2]); - userkey_s[3] = byte_swap_32 (userkey[3]); - userkey_s[4] = byte_swap_32 (userkey[4]); - userkey_s[5] = byte_swap_32 (userkey[5]); - userkey_s[6] = byte_swap_32 (userkey[6]); - userkey_s[7] = byte_swap_32 (userkey[7]); - - rek[0] = userkey_s[0]; - rek[1] = userkey_s[1]; - rek[2] = userkey_s[2]; - rek[3] = userkey_s[3]; - rek[4] = userkey_s[4]; - rek[5] = userkey_s[5]; - rek[6] = userkey_s[6]; - rek[7] = userkey_s[7]; - - int i; - int j; - - i = 0; - j = 0; - - while (1) - { - u32 temp = rek[j + 7]; - - rek[j + 8] = rek[j + 0] - ^ (te2[(temp >> 16) & 0xff] & 0xff000000) - ^ (te3[(temp >> 8) & 0xff] & 0x00ff0000) - ^ (te0[(temp >> 0) & 0xff] & 0x0000ff00) - ^ (te1[(temp >> 24) & 0xff] & 0x000000ff) - ^ rcon[i]; - - rek[j + 9] = rek[j + 1] ^ rek[j + 8]; - rek[j + 10] = rek[j + 2] ^ rek[j + 9]; - rek[j + 11] = rek[j + 3] ^ rek[j + 10]; - - if (++i == 7) break; - - temp = rek[j + 11]; - - rek[j + 12] = rek[j + 4] - ^ (te2[(temp >> 24) & 0xff] & 0xff000000) - ^ (te3[(temp >> 16) & 0xff] & 0x00ff0000) - ^ (te0[(temp >> 8) & 0xff] & 0x0000ff00) - ^ (te1[(temp >> 0) & 0xff] & 0x000000ff); - - rek[j + 13] = rek[j + 5] ^ rek[j + 12]; - rek[j + 14] = rek[j + 6] ^ rek[j + 13]; - rek[j + 15] = rek[j + 7] ^ rek[j + 14]; - - j += 8; - } -} - -static void AES256_InvertKey (u32 *rdk) -{ - for (u32 i = 0, j = 56; i < j; i += 4, j -= 4) - { - u32 temp; - - temp = rdk[i + 0]; rdk[i + 0] = rdk[j + 0]; rdk[j + 0] = temp; - temp = rdk[i + 1]; rdk[i + 1] = rdk[j + 1]; rdk[j + 1] = temp; - temp = rdk[i + 2]; rdk[i + 2] = rdk[j + 2]; rdk[j + 2] = temp; - temp = rdk[i + 3]; rdk[i + 3] = rdk[j + 3]; rdk[j + 3] = temp; - } - - for (u32 i = 1, j = 4; i < 14; i += 1, j += 4) - { - rdk[j + 0] = - td0[te1[(rdk[j + 0] >> 24) & 0xff] & 0xff] ^ - td1[te1[(rdk[j + 0] >> 16) & 0xff] & 0xff] ^ - td2[te1[(rdk[j + 0] >> 8) & 0xff] & 0xff] ^ - td3[te1[(rdk[j + 0] >> 0) & 0xff] & 0xff]; - - rdk[j + 1] = - td0[te1[(rdk[j + 1] >> 24) & 0xff] & 0xff] ^ - td1[te1[(rdk[j + 1] >> 16) & 0xff] & 0xff] ^ - td2[te1[(rdk[j + 1] >> 8) & 0xff] & 0xff] ^ - td3[te1[(rdk[j + 1] >> 0) & 0xff] & 0xff]; - - rdk[j + 2] = - td0[te1[(rdk[j + 2] >> 24) & 0xff] & 0xff] ^ - td1[te1[(rdk[j + 2] >> 16) & 0xff] & 0xff] ^ - td2[te1[(rdk[j + 2] >> 8) & 0xff] & 0xff] ^ - td3[te1[(rdk[j + 2] >> 0) & 0xff] & 0xff]; - - rdk[j + 3] = - td0[te1[(rdk[j + 3] >> 24) & 0xff] & 0xff] ^ - td1[te1[(rdk[j + 3] >> 16) & 0xff] & 0xff] ^ - td2[te1[(rdk[j + 3] >> 8) & 0xff] & 0xff] ^ - td3[te1[(rdk[j + 3] >> 0) & 0xff] & 0xff]; - } -} - -static void AES256_encrypt (const u32 *in, u32 *out, const u32 *rek) -{ - u32 in_s[4]; - - in_s[0] = byte_swap_32 (in[0]); - in_s[1] = byte_swap_32 (in[1]); - in_s[2] = byte_swap_32 (in[2]); - in_s[3] = byte_swap_32 (in[3]); - - u32 s0 = in_s[0] ^ rek[0]; - u32 s1 = in_s[1] ^ rek[1]; - u32 s2 = in_s[2] ^ rek[2]; - u32 s3 = in_s[3] ^ rek[3]; - - u32 t0; - u32 t1; - u32 t2; - u32 t3; - - t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[ 4]; - t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[ 5]; - t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[ 6]; - t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[ 7]; - s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[ 8]; - s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[ 9]; - s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[10]; - s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[11]; - t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[12]; - t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[13]; - t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[14]; - t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[15]; - s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[16]; - s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[17]; - s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[18]; - s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[19]; - t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[20]; - t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[21]; - t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[22]; - t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[23]; - s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[24]; - s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[25]; - s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[26]; - s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[27]; - t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[28]; - t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[29]; - t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[30]; - t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[31]; - s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[32]; - s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[33]; - s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[34]; - s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[35]; - t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[36]; - t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[37]; - t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[38]; - t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[39]; - s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[40]; - s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[41]; - s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[42]; - s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[43]; - t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[44]; - t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[45]; - t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[46]; - t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[47]; - s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[48]; - s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[49]; - s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[50]; - s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[51]; - t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[52]; - t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[53]; - t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[54]; - t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[55]; - - out[0] = (te4[(t0 >> 24) & 0xff] & 0xff000000) - ^ (te4[(t1 >> 16) & 0xff] & 0x00ff0000) - ^ (te4[(t2 >> 8) & 0xff] & 0x0000ff00) - ^ (te4[(t3 >> 0) & 0xff] & 0x000000ff) - ^ rek[56]; - - out[1] = (te4[(t1 >> 24) & 0xff] & 0xff000000) - ^ (te4[(t2 >> 16) & 0xff] & 0x00ff0000) - ^ (te4[(t3 >> 8) & 0xff] & 0x0000ff00) - ^ (te4[(t0 >> 0) & 0xff] & 0x000000ff) - ^ rek[57]; - - out[2] = (te4[(t2 >> 24) & 0xff] & 0xff000000) - ^ (te4[(t3 >> 16) & 0xff] & 0x00ff0000) - ^ (te4[(t0 >> 8) & 0xff] & 0x0000ff00) - ^ (te4[(t1 >> 0) & 0xff] & 0x000000ff) - ^ rek[58]; - - out[3] = (te4[(t3 >> 24) & 0xff] & 0xff000000) - ^ (te4[(t0 >> 16) & 0xff] & 0x00ff0000) - ^ (te4[(t1 >> 8) & 0xff] & 0x0000ff00) - ^ (te4[(t2 >> 0) & 0xff] & 0x000000ff) - ^ rek[59]; - - out[0] = byte_swap_32 (out[0]); - out[1] = byte_swap_32 (out[1]); - out[2] = byte_swap_32 (out[2]); - out[3] = byte_swap_32 (out[3]); -} - -static void AES256_decrypt (const u32 *in, u32 *out, const u32 *rdk) -{ - u32 in_s[4]; - - in_s[0] = byte_swap_32 (in[0]); - in_s[1] = byte_swap_32 (in[1]); - in_s[2] = byte_swap_32 (in[2]); - in_s[3] = byte_swap_32 (in[3]); - - u32 s0 = in_s[0] ^ rdk[0]; - u32 s1 = in_s[1] ^ rdk[1]; - u32 s2 = in_s[2] ^ rdk[2]; - u32 s3 = in_s[3] ^ rdk[3]; - - u32 t0; - u32 t1; - u32 t2; - u32 t3; - - t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[ 4]; - t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[ 5]; - t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[ 6]; - t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[ 7]; - s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[ 8]; - s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[ 9]; - s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[10]; - s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[11]; - t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[12]; - t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[13]; - t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[14]; - t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[15]; - s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[16]; - s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[17]; - s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[18]; - s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[19]; - t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[20]; - t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[21]; - t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[22]; - t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[23]; - s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[24]; - s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[25]; - s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[26]; - s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[27]; - t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[28]; - t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[29]; - t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[30]; - t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[31]; - s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[32]; - s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[33]; - s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[34]; - s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[35]; - t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[36]; - t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[37]; - t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[38]; - t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[39]; - s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[40]; - s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[41]; - s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[42]; - s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[43]; - t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[44]; - t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[45]; - t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[46]; - t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[47]; - s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[48]; - s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[49]; - s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[50]; - s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[51]; - t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[52]; - t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[53]; - t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[54]; - t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[55]; - - out[0] = (td4[(t0 >> 24) & 0xff] & 0xff000000) - ^ (td4[(t3 >> 16) & 0xff] & 0x00ff0000) - ^ (td4[(t2 >> 8) & 0xff] & 0x0000ff00) - ^ (td4[(t1 >> 0) & 0xff] & 0x000000ff) - ^ rdk[56]; - - out[1] = (td4[(t1 >> 24) & 0xff] & 0xff000000) - ^ (td4[(t0 >> 16) & 0xff] & 0x00ff0000) - ^ (td4[(t3 >> 8) & 0xff] & 0x0000ff00) - ^ (td4[(t2 >> 0) & 0xff] & 0x000000ff) - ^ rdk[57]; - - out[2] = (td4[(t2 >> 24) & 0xff] & 0xff000000) - ^ (td4[(t1 >> 16) & 0xff] & 0x00ff0000) - ^ (td4[(t0 >> 8) & 0xff] & 0x0000ff00) - ^ (td4[(t3 >> 0) & 0xff] & 0x000000ff) - ^ rdk[58]; - - out[3] = (td4[(t3 >> 24) & 0xff] & 0xff000000) - ^ (td4[(t2 >> 16) & 0xff] & 0x00ff0000) - ^ (td4[(t1 >> 8) & 0xff] & 0x0000ff00) - ^ (td4[(t0 >> 0) & 0xff] & 0x000000ff) - ^ rdk[59]; - - out[0] = byte_swap_32 (out[0]); - out[1] = byte_swap_32 (out[1]); - out[2] = byte_swap_32 (out[2]); - out[3] = byte_swap_32 (out[3]); -} - -// wrappers - -void AES_set_encrypt_key (const u8 *key, int keysize, AES_KEY *aes_key) -{ - aes_key->bits = keysize; - - if (aes_key->bits == 128) - { - AES128_ExpandKey ((const u32 *) key, aes_key->rek); - } - else if (aes_key->bits == 256) - { - AES256_ExpandKey ((const u32 *) key, aes_key->rek); - } -} - -void AES_set_decrypt_key (const u8 *key, int keysize, AES_KEY *aes_key) -{ - aes_key->bits = keysize; - - if (aes_key->bits == 128) - { - AES128_ExpandKey ((const u32 *) key, aes_key->rdk); - - AES128_InvertKey (aes_key->rdk); - } - else if (aes_key->bits == 256) - { - AES256_ExpandKey ((const u32 *) key, aes_key->rdk); - - AES256_InvertKey (aes_key->rdk); - } -} - -void AES_encrypt (AES_KEY *aes_key, const u8 *input, u8 *output) -{ - if (aes_key->bits == 128) - { - AES128_encrypt ((const u32 *) input, (u32 *) output, aes_key->rek); - } - else if (aes_key->bits == 256) - { - AES256_encrypt ((const u32 *) input, (u32 *) output, aes_key->rek); - } -} - -void AES_decrypt (AES_KEY *aes_key, const u8 *input, u8 *output) -{ - if (aes_key->bits == 128) - { - AES128_decrypt ((const u32 *) input, (u32 *) output, aes_key->rdk); - } - else if (aes_key->bits == 256) - { - AES256_decrypt ((const u32 *) input, (u32 *) output, aes_key->rdk); - } -} +#define AES_KEY aes_ctx +void AES_set_encrypt_key (const u8 *key, int keysize, AES_KEY *aes_key); +void AES_set_decrypt_key (const u8 *key, int keysize, AES_KEY *aes_key); +void AES_encrypt (AES_KEY *aes_key, const u8 *input, u8 *output); +void AES_decrypt (AES_KEY *aes_key, const u8 *input, u8 *output); diff --git a/include/cpu-crc32.h b/include/cpu_crc32.h similarity index 99% rename from include/cpu-crc32.h rename to include/cpu_crc32.h index 22fca7179..86ddc71c3 100644 --- a/include/cpu-crc32.h +++ b/include/cpu_crc32.h @@ -3,6 +3,8 @@ * License.....: MIT */ +#pragma once + static const uint crc32tab[256] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, diff --git a/include/cpu-des.c b/include/cpu_des.h similarity index 86% rename from include/cpu-des.c rename to include/cpu_des.h index 018ba8c5d..0259b6dba 100644 --- a/include/cpu-des.c +++ b/include/cpu_des.h @@ -3,6 +3,46 @@ * License.....: MIT */ +#pragma once + +#define PERM_OP(a,b,tt,n,m) \ +{ \ + tt = a >> n; \ + tt = tt ^ b; \ + tt = tt & m; \ + b = b ^ tt; \ + tt = tt << n; \ + a = a ^ tt; \ +} + +#define HPERM_OP(a,tt,n,m) \ +{ \ + tt = a << (16 + n); \ + tt = tt ^ a; \ + tt = tt & m; \ + a = a ^ tt; \ + tt = tt >> (16 + n); \ + a = a ^ tt; \ +} + +#define IP(l,r,tt) \ +{ \ + PERM_OP (r, l, tt, 4, 0x0f0f0f0f); \ + PERM_OP (l, r, tt, 16, 0x0000ffff); \ + PERM_OP (r, l, tt, 2, 0x33333333); \ + PERM_OP (l, r, tt, 8, 0x00ff00ff); \ + PERM_OP (r, l, tt, 1, 0x55555555); \ +} + +#define FP(l,r,tt) \ +{ \ + PERM_OP (l, r, tt, 1, 0x55555555); \ + PERM_OP (r, l, tt, 8, 0x00ff00ff); \ + PERM_OP (l, r, tt, 2, 0x33333333); \ + PERM_OP (r, l, tt, 16, 0x0000ffff); \ + PERM_OP (l, r, tt, 4, 0x0f0f0f0f); \ +} + static const u32 c_SPtrans[8][64] = { { @@ -315,143 +355,5 @@ static const u32 c_skb[8][64] = } }; -#define PERM_OP(a,b,tt,n,m) \ -{ \ - tt = a >> n; \ - tt = tt ^ b; \ - tt = tt & m; \ - b = b ^ tt; \ - tt = tt << n; \ - a = a ^ tt; \ -} - -#define HPERM_OP(a,tt,n,m) \ -{ \ - tt = a << (16 + n); \ - tt = tt ^ a; \ - tt = tt & m; \ - a = a ^ tt; \ - tt = tt >> (16 + n); \ - a = a ^ tt; \ -} - -#define IP(l,r,tt) \ -{ \ - PERM_OP (r, l, tt, 4, 0x0f0f0f0f); \ - PERM_OP (l, r, tt, 16, 0x0000ffff); \ - PERM_OP (r, l, tt, 2, 0x33333333); \ - PERM_OP (l, r, tt, 8, 0x00ff00ff); \ - PERM_OP (r, l, tt, 1, 0x55555555); \ -} - -#define FP(l,r,tt) \ -{ \ - PERM_OP (l, r, tt, 1, 0x55555555); \ - PERM_OP (r, l, tt, 8, 0x00ff00ff); \ - PERM_OP (l, r, tt, 2, 0x33333333); \ - PERM_OP (r, l, tt, 16, 0x0000ffff); \ - PERM_OP (l, r, tt, 4, 0x0f0f0f0f); \ -} - -#define BOX(v,i,S) (S)[(i)][(v)] - -void _des_keysetup (u32 data[2], u32 Kc[16], u32 Kd[16], const u32 s_skb[8][64]) -{ - u32 c = data[0]; - u32 d = data[1]; - - u32 tt; - - PERM_OP (d, c, tt, 4, 0x0f0f0f0f); - HPERM_OP (c, tt, 2, 0xcccc0000); - HPERM_OP (d, tt, 2, 0xcccc0000); - PERM_OP (d, c, tt, 1, 0x55555555); - PERM_OP (c, d, tt, 8, 0x00ff00ff); - PERM_OP (d, c, tt, 1, 0x55555555); - - d = ((d & 0x000000ff) << 16) - | ((d & 0x0000ff00) << 0) - | ((d & 0x00ff0000) >> 16) - | ((c & 0xf0000000) >> 4); - - c = c & 0x0fffffff; - - int i; - - for (i = 0; i < 16; i++) - { - const u32 shifts3s0[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; - const u32 shifts3s1[16] = { 27, 27, 26, 26, 26, 26, 26, 26, 27, 26, 26, 26, 26, 26, 26, 27 }; - - c = c >> shifts3s0[i] | c << shifts3s1[i]; - d = d >> shifts3s0[i] | d << shifts3s1[i]; - - c = c & 0x0fffffff; - d = d & 0x0fffffff; - - u32 s = BOX ((( c >> 0) & 0x3f), 0, s_skb) - | BOX ((((c >> 6) & 0x03) - | ((c >> 7) & 0x3c)), 1, s_skb) - | BOX ((((c >> 13) & 0x0f) - | ((c >> 14) & 0x30)), 2, s_skb) - | BOX ((((c >> 20) & 0x01) - | ((c >> 21) & 0x06) - | ((c >> 22) & 0x38)), 3, s_skb); - - u32 t = BOX ((( d >> 0) & 0x3f), 4, s_skb) - | BOX ((((d >> 7) & 0x03) - | ((d >> 8) & 0x3c)), 5, s_skb) - | BOX ((((d >> 15) & 0x3f)), 6, s_skb) - | BOX ((((d >> 21) & 0x0f) - | ((d >> 22) & 0x30)), 7, s_skb); - - Kc[i] = ((t << 16) | (s & 0x0000ffff)); - Kd[i] = ((s >> 16) | (t & 0xffff0000)); - - Kc[i] = rotl32 (Kc[i], 2u); - Kd[i] = rotl32 (Kd[i], 2u); - } -} - -void _des_encrypt (u32 data[2], u32 Kc[16], u32 Kd[16], const u32 s_SPtrans[8][64]) -{ - u32 r = data[0]; - u32 l = data[1]; - - u32 tt; - - IP (r, l, tt); - - r = rotl32 (r, 3u); - l = rotl32 (l, 3u); - - int i; - - for (i = 0; i < 16; i++) - { - u32 u = Kc[i] ^ r; - u32 t = Kd[i] ^ rotl32 (r, 28u); - - l ^= BOX (((u >> 2) & 0x3f), 0, s_SPtrans) - | BOX (((u >> 10) & 0x3f), 2, s_SPtrans) - | BOX (((u >> 18) & 0x3f), 4, s_SPtrans) - | BOX (((u >> 26) & 0x3f), 6, s_SPtrans) - | BOX (((t >> 2) & 0x3f), 1, s_SPtrans) - | BOX (((t >> 10) & 0x3f), 3, s_SPtrans) - | BOX (((t >> 18) & 0x3f), 5, s_SPtrans) - | BOX (((t >> 26) & 0x3f), 7, s_SPtrans); - - tt = l; - l = r; - r = tt; - } - - l = rotl32 (l, 29u); - r = rotl32 (r, 29u); - - FP (r, l, tt); - - data[0] = l; - data[1] = r; -} - +void _des_keysetup (u32 data[2], u32 Kc[16], u32 Kd[16], const u32 s_skb[8][64]); +void _des_encrypt (u32 data[2], u32 Kc[16], u32 Kd[16], const u32 s_SPtrans[8][64]); diff --git a/include/cpu-md5.h b/include/cpu_md5.h similarity index 55% rename from include/cpu-md5.h rename to include/cpu_md5.h index 06683785a..f973fd1f1 100644 --- a/include/cpu-md5.h +++ b/include/cpu_md5.h @@ -1,2 +1,9 @@ +/** + * Author......: Jens Steube + * License.....: MIT + */ + +#pragma once + void md5_64 (uint block[16], uint digest[4]); void md5_complete_no_limit (uint digest[4], uint *plain, uint plain_len); diff --git a/include/cpu_sha1.h b/include/cpu_sha1.h new file mode 100644 index 000000000..2d217ce9f --- /dev/null +++ b/include/cpu_sha1.h @@ -0,0 +1,8 @@ +/** + * Author......: Jens Steube + * License.....: MIT + */ + +#pragma once + +void sha1_64 (uint block[16], uint digest[5]); diff --git a/include/cpu_sha256.h b/include/cpu_sha256.h new file mode 100644 index 000000000..d5c4c50e5 --- /dev/null +++ b/include/cpu_sha256.h @@ -0,0 +1,8 @@ +/** + * Author......: Jens Steube + * License.....: MIT + */ + +#pragma once + +void sha256_64 (uint block[16], uint digest[8]); diff --git a/include/dynloader.h b/include/dynloader.h new file mode 100644 index 000000000..0a87cbf62 --- /dev/null +++ b/include/dynloader.h @@ -0,0 +1,69 @@ +/** + * Author......: Jens Steube + * License.....: MIT + */ + +#pragma once + +#include + +#ifdef _POSIX +#include +#ifdef __APPLE__ +#include +#endif // __APPLE__ +#endif // _POSIX + +#ifdef _WIN +#include +#endif // _WIN + +#ifdef _WIN +#define hc_dlopen LoadLibrary +#define hc_dlclose FreeLibrary +#define hc_dlsym GetProcAddress +#else +#define hc_dlopen dlopen +#define hc_dlclose dlclose +#define hc_dlsym dlsym +#endif + +#define HC_LOAD_FUNC2(ptr,name,type,var,libname,noerr) \ + ptr->name = (type) hc_dlsym (ptr->var, #name); \ + if (noerr != -1) { \ + if (!ptr->name) { \ + if (noerr == 1) { \ + log_error ("ERROR: %s is missing from %s shared library.", #name, #libname); \ + exit (-1); \ + } else { \ + log_info ("WARNING: %s is missing from %s shared library.", #name, #libname); \ + return -1; \ + } \ + } \ + } + +#define HC_LOAD_FUNC(ptr,name,type,libname,noerr) \ + ptr->name = (type) hc_dlsym (ptr->lib, #name); \ + if (noerr != -1) { \ + if (!ptr->name) { \ + if (noerr == 1) { \ + log_error ("ERROR: %s is missing from %s shared library.", #name, #libname); \ + exit (-1); \ + } else { \ + log_info ("WARNING: %s is missing from %s shared library.", #name, #libname); \ + return -1; \ + } \ + } \ + } + +#define HC_LOAD_ADDR(ptr,name,type,func,addr,libname,noerr) \ + ptr->name = (type) (*ptr->func) (addr); \ + if (!ptr->name) { \ + if (noerr == 1) { \ + log_error ("ERROR: %s at address %08x is missing from %s shared library.", #name, addr, #libname); \ + exit (-1); \ + } else { \ + log_error ("WARNING: %s at address %08x is missing from %s shared library.", #name, addr, #libname); \ + return -1; \ + } \ + } diff --git a/include/ext_ADL.h b/include/ext_ADL.h index 6a2b00143..4e289a564 100644 --- a/include/ext_ADL.h +++ b/include/ext_ADL.h @@ -6,12 +6,9 @@ * License.....: MIT */ -#ifndef EXT_ADL_H -#define EXT_ADL_H +#pragma once -#if defined(HAVE_HWMON) - -#include +#include /** * Declarations from adl_sdk.h and subheaders @@ -264,8 +261,6 @@ typedef void* (ADL_API_CALL *ADL_MAIN_MALLOC_CALLBACK )( int ); typedef int HM_ADAPTER_ADL; -#include - typedef struct { ADLOD6StateInfo state; @@ -308,6 +303,12 @@ typedef int (ADL_API_CALL *ADL_OVERDRIVE6_TARGETTEMPERATUREDATA_GET) (int, int * typedef int (ADL_API_CALL *ADL_OVERDRIVE6_TARGETTEMPERATURERANGEINFO_GET) (int, ADLOD6ParameterRange *); typedef int (ADL_API_CALL *ADL_OVERDRIVE6_FANSPEED_RESET) (int); +#ifdef _POSIX +typedef void *ADL_LIB; +#else +typedef HINSTANCE ADL_LIB; +#endif + typedef struct { ADL_LIB lib; @@ -349,9 +350,9 @@ typedef struct } hm_adl_lib_t; -#define ADL_PTR hm_adl_lib_t +typedef hm_adl_lib_t ADL_PTR; -int adl_init (ADL_PTR *lib); +int adl_init (ADL_PTR *lib); void adl_close (ADL_PTR *lib); int hm_ADL_Main_Control_Destroy (ADL_PTR *adl); @@ -388,7 +389,3 @@ int hm_ADL_Overdrive6_PowerControl_Caps (ADL_PTR *adl, int iAdapterIndex, int *l int hm_ADL_Overdrive6_TargetTemperatureData_Get (ADL_PTR *adl, int iAdapterIndex, int *cur_temp, int *default_temp); int hm_ADL_Overdrive6_TargetTemperatureRangeInfo_Get (ADL_PTR *adl, int iAdapterIndex, ADLOD6ParameterRange *lpTargetTemperatureInfo); int hm_ADL_Overdrive6_FanSpeed_Reset (ADL_PTR *adl, int iAdapterIndex); - -#endif // HAVE_HWMON - -#endif // EXT_ADL_H diff --git a/include/ext_OpenCL.h b/include/ext_OpenCL.h index 49021a969..fdffe6428 100644 --- a/include/ext_OpenCL.h +++ b/include/ext_OpenCL.h @@ -5,10 +5,32 @@ * License.....: MIT */ -#ifndef EXT_OPENCL_H -#define EXT_OPENCL_H +#pragma once -#include +#define CL_PLATFORMS_MAX 16 + +static const char CL_VENDOR_AMD[] = "Advanced Micro Devices, Inc."; +static const char CL_VENDOR_AMD_USE_INTEL[] = "GenuineIntel"; +static const char CL_VENDOR_APPLE[] = "Apple"; +static const char CL_VENDOR_INTEL_BEIGNET[] = "Intel"; +static const char CL_VENDOR_INTEL_SDK[] = "Intel(R) Corporation"; +static const char CL_VENDOR_MESA[] = "Mesa"; +static const char CL_VENDOR_NV[] = "NVIDIA Corporation"; +static const char CL_VENDOR_POCL[] = "The pocl project"; + +typedef enum vendor_id +{ + VENDOR_ID_AMD = (1 << 0), + VENDOR_ID_APPLE = (1 << 1), + VENDOR_ID_INTEL_BEIGNET = (1 << 2), + VENDOR_ID_INTEL_SDK = (1 << 3), + VENDOR_ID_MESA = (1 << 4), + VENDOR_ID_NV = (1 << 5), + VENDOR_ID_POCL = (1 << 6), + VENDOR_ID_AMD_USE_INTEL = (1 << 7), + VENDOR_ID_GENERIC = (1 << 31) + +} vendor_id_t; #define CL_USE_DEPRECATED_OPENCL_1_2_APIS #define CL_USE_DEPRECATED_OPENCL_2_0_APIS @@ -29,8 +51,6 @@ #include #endif -#include - typedef cl_int (CL_API_CALL *OCL_CLBUILDPROGRAM) (cl_program, cl_uint, const cl_device_id *, const char *, void (CL_CALLBACK *)(cl_program, void *), void *); typedef cl_mem (CL_API_CALL *OCL_CLCREATEBUFFER) (cl_context, cl_mem_flags, size_t, void *, cl_int *); typedef cl_command_queue (CL_API_CALL *OCL_CLCREATECOMMANDQUEUE) (cl_context, cl_device_id, cl_command_queue_properties, cl_int *); @@ -64,6 +84,12 @@ typedef cl_int (CL_API_CALL *OCL_CLRELEASEPROGRAM) (cl_progra typedef cl_int (CL_API_CALL *OCL_CLSETKERNELARG) (cl_kernel, cl_uint, size_t, const void *); typedef cl_int (CL_API_CALL *OCL_CLWAITFOREVENTS) (cl_uint, const cl_event *); +#ifdef _POSIX +typedef void *OCL_LIB; +#else +typedef HINSTANCE OCL_LIB; +#endif + typedef struct { OCL_LIB lib; @@ -103,7 +129,7 @@ typedef struct } hc_opencl_lib_t; -#define OCL_PTR hc_opencl_lib_t +typedef hc_opencl_lib_t OCL_PTR; const char *val2cstr_cl (cl_int CL_err); @@ -142,5 +168,3 @@ cl_int hc_clReleaseMemObject (OCL_PTR *ocl, cl_mem mem); cl_int hc_clReleaseProgram (OCL_PTR *ocl, cl_program program); cl_int hc_clSetKernelArg (OCL_PTR *ocl, cl_kernel kernel, cl_uint arg_index, size_t arg_size, const void *arg_value); cl_int hc_clWaitForEvents (OCL_PTR *ocl, cl_uint num_events, const cl_event *event_list); - -#endif diff --git a/include/ext_nvapi.h b/include/ext_nvapi.h index 0258dadfa..51830c3b7 100644 --- a/include/ext_nvapi.h +++ b/include/ext_nvapi.h @@ -5,12 +5,7 @@ * License.....: MIT */ -#ifndef EXT_NVAPI_H -#define EXT_NVAPI_H - -#if defined(HAVE_HWMON) - -#include +#pragma once /** * Declarations from nvapi.h and subheaders @@ -205,7 +200,7 @@ typedef struct } NV_GPU_COOLER_LEVELS; -NVAPI_INTERFACE NvAPI_QueryInterface (uint offset); +NVAPI_INTERFACE NvAPI_QueryInterface (unsigned int offset); NVAPI_INTERFACE NvAPI_Initialize (); NVAPI_INTERFACE NvAPI_Unload (); NVAPI_INTERFACE NvAPI_GetErrorMessage (NvAPI_Status nr,NvAPI_ShortString szDesc); @@ -221,15 +216,13 @@ NVAPI_INTERFACE NvAPI_GPU_RestoreCoolerSettings (NvPhysicalGpuHandle hPhysicalGp typedef NvPhysicalGpuHandle HM_ADAPTER_NVAPI; -#include - #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) #define NVAPI_API_CALL __stdcall #else #define NVAPI_API_CALL #endif -typedef int *(*NVAPI_API_CALL NVAPI_QUERYINTERFACE) (uint); +typedef int *(*NVAPI_API_CALL NVAPI_QUERYINTERFACE) (unsigned int); typedef int (*NVAPI_API_CALL NVAPI_INITIALIZE) (void); typedef int (*NVAPI_API_CALL NVAPI_UNLOAD) (void); typedef int (*NVAPI_API_CALL NVAPI_GETERRORMESSAGE) (NvAPI_Status, NvAPI_ShortString); @@ -239,6 +232,12 @@ typedef int (*NVAPI_API_CALL NVAPI_GPU_GETPERFPOLICIESSTATUS) (NvPhysicalGpuHand typedef int (*NVAPI_API_CALL NVAPI_GPU_SETCOOLERLEVELS) (NvPhysicalGpuHandle, NvU32, NV_GPU_COOLER_LEVELS *); typedef int (*NVAPI_API_CALL NVAPI_GPU_RESTORECOOLERSETTINGS) (NvPhysicalGpuHandle, NvU32); +#ifdef _POSIX +typedef void *NVAPI_LIB; +#else +typedef HINSTANCE NVAPI_LIB; +#endif + typedef struct { NVAPI_LIB lib; @@ -255,12 +254,12 @@ typedef struct } hm_nvapi_lib_t; -#define NVAPI_PTR hm_nvapi_lib_t +typedef hm_nvapi_lib_t NVAPI_PTR; -int nvapi_init (NVAPI_PTR *nvapi); +int nvapi_init (NVAPI_PTR *nvapi); void nvapi_close (NVAPI_PTR *nvapi); -int hm_NvAPI_QueryInterface (NVAPI_PTR *nvapi, uint offset); +int hm_NvAPI_QueryInterface (NVAPI_PTR *nvapi, unsigned int offset); int hm_NvAPI_Initialize (NVAPI_PTR *nvapi); int hm_NvAPI_Unload (NVAPI_PTR *nvapi); int hm_NvAPI_GetErrorMessage (NVAPI_PTR *nvapi, NvAPI_Status nr, NvAPI_ShortString szDesc); @@ -269,7 +268,3 @@ int hm_NvAPI_GPU_GetPerfPoliciesInfo (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhy int hm_NvAPI_GPU_GetPerfPoliciesStatus (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1 *perfPolicies_status); int hm_NvAPI_GPU_SetCoolerLevels (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NvU32 coolerIndex, NV_GPU_COOLER_LEVELS *pCoolerLevels); int hm_NvAPI_GPU_RestoreCoolerSettings (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NvU32 coolerIndex); - -#endif // HAVE_HWMON - -#endif // EXT_NVAPI_H diff --git a/include/ext_nvml.h b/include/ext_nvml.h index 07425b3ef..a0697b67e 100644 --- a/include/ext_nvml.h +++ b/include/ext_nvml.h @@ -6,12 +6,7 @@ * License.....: MIT */ -#ifndef EXT_NVML_H -#define EXT_NVML_H - -#if defined(HAVE_HWMON) - -#include +#pragma once /** * Declarations from nvml.h @@ -166,8 +161,6 @@ typedef enum nvmlGom_enum typedef nvmlDevice_t HM_ADAPTER_NVML; -#include - #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) #define NVML_API_CALL __stdcall #else @@ -195,6 +188,12 @@ typedef nvmlReturn_t (*NVML_API_CALL NVML_DEVICE_GET_POWERMANAGEMENTLIMITCONSTRA typedef nvmlReturn_t (*NVML_API_CALL NVML_DEVICE_SET_POWERMANAGEMENTLIMIT) (nvmlDevice_t, unsigned int); typedef nvmlReturn_t (*NVML_API_CALL NVML_DEVICE_GET_POWERMANAGEMENTLIMIT) (nvmlDevice_t, unsigned int *); +#ifdef _POSIX +typedef void *NVML_LIB; +#else +typedef HINSTANCE NVML_LIB; +#endif + typedef struct { NVML_LIB lib; @@ -222,7 +221,7 @@ typedef struct } hm_nvml_lib_t; -#define NVML_PTR hm_nvml_lib_t +typedef hm_nvml_lib_t NVML_PTR; int nvml_init (NVML_PTR *lib); void nvml_close (NVML_PTR *lib); @@ -247,7 +246,3 @@ nvmlReturn_t hm_NVML_nvmlDeviceSetGpuOperationMode (NVML_PTR *nvml, int, nvmlDev nvmlReturn_t hm_NVML_nvmlDeviceGetPowerManagementLimitConstraints (NVML_PTR *nvml, int, nvmlDevice_t device, unsigned int *minLimit, unsigned int *maxLimit); nvmlReturn_t hm_NVML_nvmlDeviceSetPowerManagementLimit (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int limit); nvmlReturn_t hm_NVML_nvmlDeviceGetPowerManagementLimit (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *limit); - -#endif // HAVE_HWMON - -#endif // EXT_NVML_H diff --git a/include/ext_xnvctrl.h b/include/ext_xnvctrl.h index f608555d1..b616809a3 100644 --- a/include/ext_xnvctrl.h +++ b/include/ext_xnvctrl.h @@ -3,12 +3,7 @@ * License.....: MIT */ -#ifndef EXT_XNVCTRL_H -#define EXT_XNVCTRL_H - -#if defined(HAVE_HWMON) - -#include +#pragma once /** * Stuff from X11/Xlib.h @@ -44,8 +39,6 @@ typedef int (*XCLOSEDISPLAY) (void *); typedef int HM_ADAPTER_XNVCTRL; -#include - #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) #define XNVCTRL_API_CALL __stdcall #else @@ -55,6 +48,12 @@ typedef int HM_ADAPTER_XNVCTRL; typedef int (*XNVCTRL_API_CALL XNVCTRLQUERYTARGETATTRIBUTE) (void *, int, int, unsigned int, unsigned int, int *); typedef void (*XNVCTRL_API_CALL XNVCTRLSETTARGETATTRIBUTE) (void *, int, int, unsigned int, unsigned int, int); +#ifdef _POSIX +typedef void *XNVCTRL_LIB; +#else +typedef HINSTANCE XNVCTRL_LIB; +#endif + typedef struct { void *dpy; @@ -70,7 +69,7 @@ typedef struct } hm_xnvctrl_lib_t; -#define XNVCTRL_PTR hm_xnvctrl_lib_t +typedef hm_xnvctrl_lib_t XNVCTRL_PTR; int xnvctrl_init (XNVCTRL_PTR *xnvctrl); void xnvctrl_close (XNVCTRL_PTR *xnvctrl); @@ -86,7 +85,3 @@ int set_fan_control (XNVCTRL_PTR *xnvctrl, int gpu, int val); int get_fan_speed_current (XNVCTRL_PTR *xnvctrl, int gpu, int *val); int get_fan_speed_target (XNVCTRL_PTR *xnvctrl, int gpu, int *val); int set_fan_speed_target (XNVCTRL_PTR *xnvctrl, int gpu, int val); - -#endif // HAVE_HWMON - -#endif // EXT_XNVCTRL_H diff --git a/include/logging.h b/include/logging.h new file mode 100644 index 000000000..6f2b931a6 --- /dev/null +++ b/include/logging.h @@ -0,0 +1,17 @@ +/** + * Author......: Jens Steube + * License.....: MIT + */ + +#pragma once + +#include +#include + +int log_out_nn (FILE *fp, const char *fmt, ...); +int log_info_nn (const char *fmt, ...); +int log_error_nn (const char *fmt, ...); + +int log_out (FILE *fp, const char *fmt, ...); +int log_info (const char *fmt, ...); +int log_error (const char *fmt, ...); diff --git a/include/memory.h b/include/memory.h new file mode 100644 index 000000000..8514df105 --- /dev/null +++ b/include/memory.h @@ -0,0 +1,17 @@ +/** + * Author......: Jens Steube + * License.....: MIT + */ + +#pragma once + +#include +#include + +#define MSG_ENOMEM "Insufficient memory available" + +void *mycalloc (size_t nmemb, size_t size); +void myfree (void *ptr); +void *mymalloc (size_t size); +void *myrealloc (void *ptr, size_t oldsz, size_t add); +char *mystrdup (const char *s); diff --git a/include/rp_cpu.h b/include/rp_cpu.h index c5e25b870..2dc8c200f 100644 --- a/include/rp_cpu.h +++ b/include/rp_cpu.h @@ -1,8 +1,10 @@ /** - * Author......: Jens Steube + * Authors.....: Jens Steube * License.....: MIT */ +#pragma once + #define RP_RULE_BUFSIZ 0x100 #define RULE_RC_SYNTAX_ERROR -1 diff --git a/include/rp_kernel_on_cpu.h b/include/rp_kernel_on_cpu.h index a0fda1c61..49441c0c4 100644 --- a/include/rp_kernel_on_cpu.h +++ b/include/rp_kernel_on_cpu.h @@ -3,15 +3,9 @@ * License.....: MIT */ -#ifndef RP_CPU_H -#define RP_CPU_H +#pragma once #define swap_workaround(n) __builtin_bswap32(n) -#include "common.h" -#include "inc_rp.h" - u32 apply_rule (const u32 name, const u32 p0, const u32 p1, u32 buf0[4], u32 buf1[4], const u32 in_len); u32 apply_rules (u32 *cmds, u32 buf0[4], u32 buf1[4], const u32 len); - -#endif diff --git a/include/shared.h b/include/shared.h index 76baef51a..e60a8c089 100644 --- a/include/shared.h +++ b/include/shared.h @@ -6,45 +6,47 @@ * License.....: MIT */ -#ifndef SHARED_H -#define SHARED_H +#pragma once -#include "common.h" -#include "inc_hash_constants.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /** - * thread management + * OS specific includes */ -#ifdef _WIN -#define hc_timer_get(a,r) { hc_timer_t hr_freq; QueryPerformanceFrequency (&hr_freq); hc_timer_t hr_tmp; hc_timer_set (&hr_tmp); (r) = (double) ((double) (hr_tmp.QuadPart - (a).QuadPart) / (double) (hr_freq.QuadPart / 1000)); } -#define hc_timer_set(a) { QueryPerformanceCounter ((a)); } -#elif _POSIX -#define hc_timer_get(a,r) { hc_timer_t hr_tmp; hc_timer_set (&hr_tmp); (r) = (double) (((hr_tmp.tv_sec - (a).tv_sec) * 1000) + ((double) (hr_tmp.tv_usec - (a).tv_usec) / 1000)); } -#define hc_timer_set(a) { gettimeofday ((a), NULL); } -#endif +#ifdef _POSIX +#include +#include +#include +#include +#include +#include +#include +#endif // _POSIX + +#ifdef __APPLE__ +#include +#include +#endif // __APPLE__ #ifdef _WIN -#define hc_thread_create(t,f,a) t = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) &f, a, 0, NULL) -#define hc_thread_wait(n,a) for (uint i = 0; i < n; i++) WaitForSingleObject ((a)[i], INFINITE) -#define hc_thread_exit(t) ExitThread (t) +#include +#include +#include +#endif // _WIN -#define hc_thread_mutex_lock(m) EnterCriticalSection (&m) -#define hc_thread_mutex_unlock(m) LeaveCriticalSection (&m) -#define hc_thread_mutex_init(m) InitializeCriticalSection (&m) -#define hc_thread_mutex_delete(m) DeleteCriticalSection (&m) - -#elif _POSIX - -#define hc_thread_create(t,f,a) pthread_create (&t, NULL, f, a) -#define hc_thread_wait(n,a) for (uint i = 0; i < n; i++) pthread_join ((a)[i], NULL) -#define hc_thread_exit(t) pthread_exit (&t) - -#define hc_thread_mutex_lock(m) pthread_mutex_lock (&m) -#define hc_thread_mutex_unlock(m) pthread_mutex_unlock (&m) -#define hc_thread_mutex_init(m) pthread_mutex_init (&m, NULL) -#define hc_thread_mutex_delete(m) pthread_mutex_destroy (&m) -#endif +/** + * unsorted + */ #ifdef __APPLE__ typedef struct cpu_set @@ -58,63 +60,6 @@ static inline void CPU_SET (int num, cpu_set_t *cs) { cs->count |= (1 << num); static inline int CPU_ISSET (int num, cpu_set_t *cs) { return (cs->count & (1 << num)); } #endif -/** - * libraries stuff - */ - -#ifdef _WIN -#define hc_dlopen LoadLibrary -#define hc_dlclose FreeLibrary -#define hc_dlsym GetProcAddress -#else -#define hc_dlopen dlopen -#define hc_dlclose dlclose -#define hc_dlsym dlsym -#endif - -#define HC_LOAD_FUNC2(ptr,name,type,var,libname,noerr) \ - ptr->name = (type) hc_dlsym (ptr->var, #name); \ - if (noerr != -1) { \ - if (!ptr->name) { \ - if (noerr == 1) { \ - log_error ("ERROR: %s is missing from %s shared library.", #name, #libname); \ - exit (-1); \ - } else { \ - log_info ("WARNING: %s is missing from %s shared library.", #name, #libname); \ - return -1; \ - } \ - } \ - } - -#define HC_LOAD_FUNC(ptr,name,type,libname,noerr) \ - ptr->name = (type) hc_dlsym (ptr->lib, #name); \ - if (noerr != -1) { \ - if (!ptr->name) { \ - if (noerr == 1) { \ - log_error ("ERROR: %s is missing from %s shared library.", #name, #libname); \ - exit (-1); \ - } else { \ - log_info ("WARNING: %s is missing from %s shared library.", #name, #libname); \ - return -1; \ - } \ - } \ - } - -#define HC_LOAD_ADDR(ptr,name,type,func,addr,libname,noerr) \ - ptr->name = (type) (*ptr->func) (addr); \ - if (!ptr->name) { \ - if (noerr == 1) { \ - log_error ("ERROR: %s at address %08x is missing from %s shared library.", #name, addr, #libname); \ - exit (-1); \ - } else { \ - log_error ("WARNING: %s at address %08x is missing from %s shared library.", #name, addr, #libname); \ - return -1; \ - } \ - } - -/** - * system stuff - */ #ifdef _WIN #define hc_sleep(x) Sleep ((x) * 1000); @@ -122,50 +67,8 @@ static inline int CPU_ISSET (int num, cpu_set_t *cs) { return (cs->count & (1 < #define hc_sleep(x) sleep ((x)); #endif -#include "ext_OpenCL.h" - -/** - * temperature management - */ - -#include "ext_ADL.h" -#include "ext_nvapi.h" -#include "ext_nvml.h" -#include "ext_xnvctrl.h" - -/** - * shared stuff - */ - #define ETC_MAX (60 * 60 * 24 * 365 * 10) -#define DEVICES_MAX 128 - -#define CL_PLATFORMS_MAX 16 - -static const char CL_VENDOR_AMD[] = "Advanced Micro Devices, Inc."; -static const char CL_VENDOR_AMD_USE_INTEL[] = "GenuineIntel"; -static const char CL_VENDOR_APPLE[] = "Apple"; -static const char CL_VENDOR_INTEL_BEIGNET[] = "Intel"; -static const char CL_VENDOR_INTEL_SDK[] = "Intel(R) Corporation"; -static const char CL_VENDOR_MESA[] = "Mesa"; -static const char CL_VENDOR_NV[] = "NVIDIA Corporation"; -static const char CL_VENDOR_POCL[] = "The pocl project"; - -typedef enum vendor_id -{ - VENDOR_ID_AMD = (1 << 0), - VENDOR_ID_APPLE = (1 << 1), - VENDOR_ID_INTEL_BEIGNET = (1 << 2), - VENDOR_ID_INTEL_SDK = (1 << 3), - VENDOR_ID_MESA = (1 << 4), - VENDOR_ID_NV = (1 << 5), - VENDOR_ID_POCL = (1 << 6), - VENDOR_ID_AMD_USE_INTEL = (1 << 7), - VENDOR_ID_GENERIC = (1 << 31) - -} vendor_id_t; - #define BLOCK_SIZE 64 #define CHARSIZ 0x100 @@ -187,24 +90,6 @@ typedef enum vendor_id #define DICTSTAT_FILENAME "hashcat.dictstat" #define POTFILE_FILENAME "hashcat.pot" -/** - * types - */ - -#ifdef _WIN -typedef LARGE_INTEGER hc_timer_t; -typedef HANDLE hc_thread_t; -typedef CRITICAL_SECTION hc_thread_mutex_t; -#elif _POSIX -typedef struct timeval hc_timer_t; -typedef pthread_t hc_thread_t; -typedef pthread_mutex_t hc_thread_mutex_t; -#endif - -#include "types.h" -#include "rp_cpu.h" -#include "inc_rp.h" - /** * valid project specific global stuff */ @@ -217,8 +102,6 @@ extern const char *USAGE_BIG[]; extern const char *PROMPT; -extern int SUPPRESS_OUTPUT; - extern hc_thread_mutex_t mux_display; /** @@ -1481,12 +1364,6 @@ char *strhashtype (const uint hash_mode); char *strstatus (const uint threads_status); void status (); -void *mycalloc (size_t nmemb, size_t size); -void myfree (void *ptr); -void *mymalloc (size_t size); -void *myrealloc (void *ptr, size_t oldsz, size_t add); -char *mystrdup (const char *s); - char *logfile_generate_topid (void); char *logfile_generate_subid (void); void logfile_append (const char *fmt, ...); @@ -1803,21 +1680,3 @@ void *thread_runtime (void *p); void status_display (void); void status_display_machine_readable (void); - -/** - * checksum for use on cpu - */ - -#include "cpu-crc32.h" -#include "cpu-md5.h" -#include "cpu-sha1.h" -#include "cpu-sha256.h" - -/** - * ciphers for use on cpu - */ - -#include "cpu-aes.h" -#include "cpu-des.h" - -#endif // SHARED_H diff --git a/include/thread.h b/include/thread.h new file mode 100644 index 000000000..f2a36281a --- /dev/null +++ b/include/thread.h @@ -0,0 +1,46 @@ +/** + * Authors.....: Jens Steube + * License.....: MIT + */ + +#pragma once + +#ifdef _POSIX +#include +#include +#endif // _POSIX +#ifdef _WIN +#include +#endif // _WIN + +#ifdef _WIN +typedef HANDLE hc_thread_t; +typedef CRITICAL_SECTION hc_thread_mutex_t; +#elif _POSIX +typedef pthread_t hc_thread_t; +typedef pthread_mutex_t hc_thread_mutex_t; +#endif + +#ifdef _WIN + +#define hc_thread_create(t,f,a) t = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) &f, a, 0, NULL) +#define hc_thread_wait(n,a) for (uint i = 0; i < n; i++) WaitForSingleObject ((a)[i], INFINITE) +#define hc_thread_exit(t) ExitThread (t) + +#define hc_thread_mutex_lock(m) EnterCriticalSection (&m) +#define hc_thread_mutex_unlock(m) LeaveCriticalSection (&m) +#define hc_thread_mutex_init(m) InitializeCriticalSection (&m) +#define hc_thread_mutex_delete(m) DeleteCriticalSection (&m) + +#elif _POSIX + +#define hc_thread_create(t,f,a) pthread_create (&t, NULL, f, a) +#define hc_thread_wait(n,a) for (uint i = 0; i < n; i++) pthread_join ((a)[i], NULL) +#define hc_thread_exit(t) pthread_exit (&t) + +#define hc_thread_mutex_lock(m) pthread_mutex_lock (&m) +#define hc_thread_mutex_unlock(m) pthread_mutex_unlock (&m) +#define hc_thread_mutex_init(m) pthread_mutex_init (&m, NULL) +#define hc_thread_mutex_delete(m) pthread_mutex_destroy (&m) + +#endif diff --git a/include/timer.h b/include/timer.h new file mode 100644 index 000000000..4e5bf1578 --- /dev/null +++ b/include/timer.h @@ -0,0 +1,29 @@ +/** + * Authors.....: Jens Steube + * License.....: MIT + */ + +#pragma once + +#ifdef _POSIX +#include +#else +#endif + +#ifdef _WIN +typedef LARGE_INTEGER hc_timer_t; +#elif _POSIX +typedef struct timeval hc_timer_t; +#endif + +#ifdef _WIN + +#define hc_timer_get(a,r) { hc_timer_t hr_freq; QueryPerformanceFrequency (&hr_freq); hc_timer_t hr_tmp; hc_timer_set (&hr_tmp); (r) = (double) ((double) (hr_tmp.QuadPart - (a).QuadPart) / (double) (hr_freq.QuadPart / 1000)); } +#define hc_timer_set(a) { QueryPerformanceCounter ((a)); } + +#elif _POSIX + +#define hc_timer_get(a,r) { hc_timer_t hr_tmp; hc_timer_set (&hr_tmp); (r) = (double) (((hr_tmp.tv_sec - (a).tv_sec) * 1000) + ((double) (hr_tmp.tv_usec - (a).tv_usec) / 1000)); } +#define hc_timer_set(a) { gettimeofday ((a), NULL); } + +#endif diff --git a/include/types.h b/include/types.h index a03be3830..6cf56f897 100644 --- a/include/types.h +++ b/include/types.h @@ -3,8 +3,11 @@ * License.....: MIT */ -#ifndef TYPES_H -#define TYPES_H +#pragma once + +#include +#include +#include #ifdef _WIN #define EOL "\r\n" @@ -1363,6 +1366,3 @@ typedef struct } hc_global_data_t; extern hc_global_data_t data; - -#endif - diff --git a/include/types_int.h b/include/types_int.h new file mode 100644 index 000000000..b5d0ce1a4 --- /dev/null +++ b/include/types_int.h @@ -0,0 +1,31 @@ +/** + * Authors.....: Jens Steube + * License.....: MIT + */ + +#pragma once + +#include +#include + +#ifdef _WIN +#include +#ifdef _BASETSD_H +#else +typedef UINT8 uint8_t; +typedef UINT16 uint16_t; +typedef UINT32 uint32_t; +typedef UINT64 uint64_t; +typedef INT8 int8_t; +typedef INT16 int16_t; +typedef INT32 int32_t; +typedef INT64 int64_t; +#endif +#endif // _WIN + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef uint32_t uint; // we need to get rid of this sooner or later, for consistency diff --git a/src/Makefile b/src/Makefile index ed1a84d5b..a410a85c0 100644 --- a/src/Makefile +++ b/src/Makefile @@ -165,7 +165,7 @@ LFLAGS_CROSS_WIN := -lpsapi ## Objects ## -NATIVE_OBJS := obj/bitops.NATIVE.o obj/ext_OpenCL.NATIVE.o obj/shared.NATIVE.o obj/rp_kernel_on_cpu.NATIVE.o +NATIVE_OBJS := obj/memory.NATIVE.o obj/logging.NATIVE.o obj/cpu_aes.NATIVE.o obj/cpu_des.NATIVE.o obj/cpu_md5.NATIVE.o obj/cpu_sha1.NATIVE.o obj/cpu_sha256.NATIVE.o obj/bitops.NATIVE.o obj/bitops.NATIVE.o obj/ext_OpenCL.NATIVE.o obj/shared.NATIVE.o obj/rp_kernel_on_cpu.NATIVE.o ifeq ($(UNAME),Linux) NATIVE_OBJS += obj/ext_ADL.NATIVE.o @@ -174,8 +174,8 @@ NATIVE_OBJS += obj/ext_nvml.NATIVE.o NATIVE_OBJS += obj/ext_xnvctrl.NATIVE.o endif -LINUX_32_OBJS := obj/bitops.LINUX.32.o obj/ext_OpenCL.LINUX.32.o obj/shared.LINUX.32.o obj/rp_kernel_on_cpu.LINUX.32.o obj/ext_ADL.LINUX.32.o obj/ext_nvml.LINUX.32.o obj/ext_nvapi.LINUX.32.o obj/ext_xnvctrl.LINUX.32.o -LINUX_64_OBJS := obj/bitops.LINUX.64.o obj/ext_OpenCL.LINUX.64.o obj/shared.LINUX.64.o obj/rp_kernel_on_cpu.LINUX.64.o obj/ext_ADL.LINUX.64.o obj/ext_nvml.LINUX.64.o obj/ext_nvapi.LINUX.64.o obj/ext_xnvctrl.LINUX.64.o +LINUX_32_OBJS := obj/memory.LINUX.32.o obj/logging.LINUX.32.o obj/cpu_aes.LINUX.32.o obj/cpu_des.LINUX.32.o obj/cpu_md5.LINUX.32.o obj/cpu_sha1.LINUX.32.o obj/cpu_sha256.LINUX.32.o obj/bitops.LINUX.32.o obj/ext_OpenCL.LINUX.32.o obj/shared.LINUX.32.o obj/rp_kernel_on_cpu.LINUX.32.o obj/ext_ADL.LINUX.32.o obj/ext_nvml.LINUX.32.o obj/ext_nvapi.LINUX.32.o obj/ext_xnvctrl.LINUX.32.o +LINUX_64_OBJS := obj/memory.LINUX.64.o obj/logging.LINUX.64.o obj/cpu_aes.LINUX.64.o obj/cpu_des.LINUX.64.o obj/cpu_md5.LINUX.64.o obj/cpu_sha1.LINUX.64.o obj/cpu_sha256.LINUX.64.o obj/bitops.LINUX.64.o obj/ext_OpenCL.LINUX.64.o obj/shared.LINUX.64.o obj/rp_kernel_on_cpu.LINUX.64.o obj/ext_ADL.LINUX.64.o obj/ext_nvml.LINUX.64.o obj/ext_nvapi.LINUX.64.o obj/ext_xnvctrl.LINUX.64.o # Windows CRT file globbing: @@ -185,8 +185,8 @@ CRT_GLOB_INCLUDE_FOLDER := $(dir $(lastword $(MAKEFILE_LIST))) include $(CRT_GLOB_INCLUDE_FOLDER)/win_file_globbing.mk -WIN_32_OBJS := obj/bitops.WIN.32.o obj/ext_OpenCL.WIN.32.o obj/shared.WIN.32.o obj/rp_kernel_on_cpu.WIN.32.o obj/ext_ADL.WIN.32.o obj/ext_nvml.WIN.32.o obj/ext_nvapi.WIN.32.o obj/ext_xnvctrl.WIN.32.o $(CRT_GLOB_32) -WIN_64_OBJS := obj/bitops.WIN.64.o obj/ext_OpenCL.WIN.64.o obj/shared.WIN.64.o obj/rp_kernel_on_cpu.WIN.64.o obj/ext_ADL.WIN.64.o obj/ext_nvml.WIN.64.o obj/ext_nvapi.WIN.64.o obj/ext_xnvctrl.WIN.64.o $(CRT_GLOB_64) +WIN_32_OBJS := obj/memory.WIN.32.o obj/logging.WIN.32.o obj/cpu_aes.WIN.32.o obj/cpu_des.WIN.32.o obj/cpu_md5.WIN.32.o obj/cpu_sha1.WIN.32.o obj/cpu_sha256.WIN.32.o obj/bitops.WIN.32.o obj/ext_OpenCL.WIN.32.o obj/shared.WIN.32.o obj/rp_kernel_on_cpu.WIN.32.o obj/ext_ADL.WIN.32.o obj/ext_nvml.WIN.32.o obj/ext_nvapi.WIN.32.o obj/ext_xnvctrl.WIN.32.o $(CRT_GLOB_32) +WIN_64_OBJS := obj/memory.WIN.64.o obj/logging.WIN.64.o obj/cpu_aes.WIN.64.o obj/cpu_des.WIN.64.o obj/cpu_md5.WIN.64.o obj/cpu_sha1.WIN.64.o obj/cpu_sha256.WIN.64.o obj/bitops.WIN.64.o obj/ext_OpenCL.WIN.64.o obj/shared.WIN.64.o obj/rp_kernel_on_cpu.WIN.64.o obj/ext_ADL.WIN.64.o obj/ext_nvml.WIN.64.o obj/ext_nvapi.WIN.64.o obj/ext_xnvctrl.WIN.64.o $(CRT_GLOB_64) ## ## Targets: Global diff --git a/src/bitops.c b/src/bitops.c index 0f2537916..d4e46d7c8 100644 --- a/src/bitops.c +++ b/src/bitops.c @@ -3,6 +3,8 @@ * License.....: MIT */ +#include "common.h" +#include "types_int.h" #include "bitops.h" u32 is_power_of_2 (const u32 v) diff --git a/src/cpu_aes.c b/src/cpu_aes.c new file mode 100644 index 000000000..730532e84 --- /dev/null +++ b/src/cpu_aes.c @@ -0,0 +1,631 @@ +/** + * Author......: Jens Steube + * License.....: MIT + */ + +#define IS_GENERIC + +#include "common.h" +#include "types_int.h" +#include "bitops.h" +#include "inc_hash_constants.h" +#include "inc_hash_functions.cl" +#include "cpu_aes.h" + +// 128 bit + +static void AES128_ExpandKey (const u32 *userkey, u32 *rek) +{ + u32 userkey_s[4]; + + userkey_s[0] = byte_swap_32 (userkey[0]); + userkey_s[1] = byte_swap_32 (userkey[1]); + userkey_s[2] = byte_swap_32 (userkey[2]); + userkey_s[3] = byte_swap_32 (userkey[3]); + + rek[0] = userkey_s[0]; + rek[1] = userkey_s[1]; + rek[2] = userkey_s[2]; + rek[3] = userkey_s[3]; + + int i; + int j; + + for (i = 0, j = 0; i < 10; i += 1, j += 4) + { + u32 temp = rek[j + 3]; + + temp = (te2[(temp >> 16) & 0xff] & 0xff000000) + ^ (te3[(temp >> 8) & 0xff] & 0x00ff0000) + ^ (te0[(temp >> 0) & 0xff] & 0x0000ff00) + ^ (te1[(temp >> 24) & 0xff] & 0x000000ff); + + rek[j + 4] = rek[j + 0] + ^ temp + ^ rcon[i]; + + rek[j + 5] = rek[j + 1] ^ rek[j + 4]; + rek[j + 6] = rek[j + 2] ^ rek[j + 5]; + rek[j + 7] = rek[j + 3] ^ rek[j + 6]; + } +} + +static void AES128_InvertKey (u32 *rdk) +{ + int i; + int j; + + for (i = 0, j = 40; i < j; i += 4, j -= 4) + { + u32 temp; + + temp = rdk[i + 0]; rdk[i + 0] = rdk[j + 0]; rdk[j + 0] = temp; + temp = rdk[i + 1]; rdk[i + 1] = rdk[j + 1]; rdk[j + 1] = temp; + temp = rdk[i + 2]; rdk[i + 2] = rdk[j + 2]; rdk[j + 2] = temp; + temp = rdk[i + 3]; rdk[i + 3] = rdk[j + 3]; rdk[j + 3] = temp; + } + + for (i = 1, j = 4; i < 10; i += 1, j += 4) + { + rdk[j + 0] = + td0[te1[(rdk[j + 0] >> 24) & 0xff] & 0xff] ^ + td1[te1[(rdk[j + 0] >> 16) & 0xff] & 0xff] ^ + td2[te1[(rdk[j + 0] >> 8) & 0xff] & 0xff] ^ + td3[te1[(rdk[j + 0] >> 0) & 0xff] & 0xff]; + + rdk[j + 1] = + td0[te1[(rdk[j + 1] >> 24) & 0xff] & 0xff] ^ + td1[te1[(rdk[j + 1] >> 16) & 0xff] & 0xff] ^ + td2[te1[(rdk[j + 1] >> 8) & 0xff] & 0xff] ^ + td3[te1[(rdk[j + 1] >> 0) & 0xff] & 0xff]; + + rdk[j + 2] = + td0[te1[(rdk[j + 2] >> 24) & 0xff] & 0xff] ^ + td1[te1[(rdk[j + 2] >> 16) & 0xff] & 0xff] ^ + td2[te1[(rdk[j + 2] >> 8) & 0xff] & 0xff] ^ + td3[te1[(rdk[j + 2] >> 0) & 0xff] & 0xff]; + + rdk[j + 3] = + td0[te1[(rdk[j + 3] >> 24) & 0xff] & 0xff] ^ + td1[te1[(rdk[j + 3] >> 16) & 0xff] & 0xff] ^ + td2[te1[(rdk[j + 3] >> 8) & 0xff] & 0xff] ^ + td3[te1[(rdk[j + 3] >> 0) & 0xff] & 0xff]; + } +} + +static void AES128_encrypt (const u32 *in, u32 *out, const u32 *rek) +{ + u32 in_s[4]; + + in_s[0] = byte_swap_32 (in[0]); + in_s[1] = byte_swap_32 (in[1]); + in_s[2] = byte_swap_32 (in[2]); + in_s[3] = byte_swap_32 (in[3]); + + u32 s0 = in_s[0] ^ rek[0]; + u32 s1 = in_s[1] ^ rek[1]; + u32 s2 = in_s[2] ^ rek[2]; + u32 s3 = in_s[3] ^ rek[3]; + + u32 t0; + u32 t1; + u32 t2; + u32 t3; + + t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[ 4]; + t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[ 5]; + t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[ 6]; + t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[ 7]; + s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[ 8]; + s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[ 9]; + s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[10]; + s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[11]; + t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[12]; + t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[13]; + t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[14]; + t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[15]; + s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[16]; + s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[17]; + s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[18]; + s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[19]; + t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[20]; + t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[21]; + t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[22]; + t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[23]; + s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[24]; + s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[25]; + s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[26]; + s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[27]; + t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[28]; + t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[29]; + t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[30]; + t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[31]; + s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[32]; + s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[33]; + s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[34]; + s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[35]; + t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[36]; + t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[37]; + t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[38]; + t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[39]; + + out[0] = (te4[(t0 >> 24) & 0xff] & 0xff000000) + ^ (te4[(t1 >> 16) & 0xff] & 0x00ff0000) + ^ (te4[(t2 >> 8) & 0xff] & 0x0000ff00) + ^ (te4[(t3 >> 0) & 0xff] & 0x000000ff) + ^ rek[40]; + + out[1] = (te4[(t1 >> 24) & 0xff] & 0xff000000) + ^ (te4[(t2 >> 16) & 0xff] & 0x00ff0000) + ^ (te4[(t3 >> 8) & 0xff] & 0x0000ff00) + ^ (te4[(t0 >> 0) & 0xff] & 0x000000ff) + ^ rek[41]; + + out[2] = (te4[(t2 >> 24) & 0xff] & 0xff000000) + ^ (te4[(t3 >> 16) & 0xff] & 0x00ff0000) + ^ (te4[(t0 >> 8) & 0xff] & 0x0000ff00) + ^ (te4[(t1 >> 0) & 0xff] & 0x000000ff) + ^ rek[42]; + + out[3] = (te4[(t3 >> 24) & 0xff] & 0xff000000) + ^ (te4[(t0 >> 16) & 0xff] & 0x00ff0000) + ^ (te4[(t1 >> 8) & 0xff] & 0x0000ff00) + ^ (te4[(t2 >> 0) & 0xff] & 0x000000ff) + ^ rek[43]; + + out[0] = byte_swap_32 (out[0]); + out[1] = byte_swap_32 (out[1]); + out[2] = byte_swap_32 (out[2]); + out[3] = byte_swap_32 (out[3]); +} + +static void AES128_decrypt (const u32 *in, u32 *out, const u32 *rdk) +{ + u32 in_s[4]; + + in_s[0] = byte_swap_32 (in[0]); + in_s[1] = byte_swap_32 (in[1]); + in_s[2] = byte_swap_32 (in[2]); + in_s[3] = byte_swap_32 (in[3]); + + u32 s0 = in_s[0] ^ rdk[0]; + u32 s1 = in_s[1] ^ rdk[1]; + u32 s2 = in_s[2] ^ rdk[2]; + u32 s3 = in_s[3] ^ rdk[3]; + + u32 t0; + u32 t1; + u32 t2; + u32 t3; + + t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[ 4]; + t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[ 5]; + t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[ 6]; + t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[ 7]; + s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[ 8]; + s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[ 9]; + s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[10]; + s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[11]; + t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[12]; + t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[13]; + t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[14]; + t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[15]; + s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[16]; + s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[17]; + s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[18]; + s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[19]; + t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[20]; + t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[21]; + t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[22]; + t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[23]; + s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[24]; + s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[25]; + s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[26]; + s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[27]; + t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[28]; + t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[29]; + t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[30]; + t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[31]; + s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[32]; + s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[33]; + s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[34]; + s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[35]; + t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[36]; + t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[37]; + t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[38]; + t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[39]; + + out[0] = (td4[(t0 >> 24) & 0xff] & 0xff000000) + ^ (td4[(t3 >> 16) & 0xff] & 0x00ff0000) + ^ (td4[(t2 >> 8) & 0xff] & 0x0000ff00) + ^ (td4[(t1 >> 0) & 0xff] & 0x000000ff) + ^ rdk[40]; + + out[1] = (td4[(t1 >> 24) & 0xff] & 0xff000000) + ^ (td4[(t0 >> 16) & 0xff] & 0x00ff0000) + ^ (td4[(t3 >> 8) & 0xff] & 0x0000ff00) + ^ (td4[(t2 >> 0) & 0xff] & 0x000000ff) + ^ rdk[41]; + + out[2] = (td4[(t2 >> 24) & 0xff] & 0xff000000) + ^ (td4[(t1 >> 16) & 0xff] & 0x00ff0000) + ^ (td4[(t0 >> 8) & 0xff] & 0x0000ff00) + ^ (td4[(t3 >> 0) & 0xff] & 0x000000ff) + ^ rdk[42]; + + out[3] = (td4[(t3 >> 24) & 0xff] & 0xff000000) + ^ (td4[(t2 >> 16) & 0xff] & 0x00ff0000) + ^ (td4[(t1 >> 8) & 0xff] & 0x0000ff00) + ^ (td4[(t0 >> 0) & 0xff] & 0x000000ff) + ^ rdk[43]; + + out[0] = byte_swap_32 (out[0]); + out[1] = byte_swap_32 (out[1]); + out[2] = byte_swap_32 (out[2]); + out[3] = byte_swap_32 (out[3]); +} + +// 256 bit + +static void AES256_ExpandKey (const u32 *userkey, u32 *rek) +{ + u32 userkey_s[8]; + + userkey_s[0] = byte_swap_32 (userkey[0]); + userkey_s[1] = byte_swap_32 (userkey[1]); + userkey_s[2] = byte_swap_32 (userkey[2]); + userkey_s[3] = byte_swap_32 (userkey[3]); + userkey_s[4] = byte_swap_32 (userkey[4]); + userkey_s[5] = byte_swap_32 (userkey[5]); + userkey_s[6] = byte_swap_32 (userkey[6]); + userkey_s[7] = byte_swap_32 (userkey[7]); + + rek[0] = userkey_s[0]; + rek[1] = userkey_s[1]; + rek[2] = userkey_s[2]; + rek[3] = userkey_s[3]; + rek[4] = userkey_s[4]; + rek[5] = userkey_s[5]; + rek[6] = userkey_s[6]; + rek[7] = userkey_s[7]; + + int i; + int j; + + i = 0; + j = 0; + + while (1) + { + u32 temp = rek[j + 7]; + + rek[j + 8] = rek[j + 0] + ^ (te2[(temp >> 16) & 0xff] & 0xff000000) + ^ (te3[(temp >> 8) & 0xff] & 0x00ff0000) + ^ (te0[(temp >> 0) & 0xff] & 0x0000ff00) + ^ (te1[(temp >> 24) & 0xff] & 0x000000ff) + ^ rcon[i]; + + rek[j + 9] = rek[j + 1] ^ rek[j + 8]; + rek[j + 10] = rek[j + 2] ^ rek[j + 9]; + rek[j + 11] = rek[j + 3] ^ rek[j + 10]; + + if (++i == 7) break; + + temp = rek[j + 11]; + + rek[j + 12] = rek[j + 4] + ^ (te2[(temp >> 24) & 0xff] & 0xff000000) + ^ (te3[(temp >> 16) & 0xff] & 0x00ff0000) + ^ (te0[(temp >> 8) & 0xff] & 0x0000ff00) + ^ (te1[(temp >> 0) & 0xff] & 0x000000ff); + + rek[j + 13] = rek[j + 5] ^ rek[j + 12]; + rek[j + 14] = rek[j + 6] ^ rek[j + 13]; + rek[j + 15] = rek[j + 7] ^ rek[j + 14]; + + j += 8; + } +} + +static void AES256_InvertKey (u32 *rdk) +{ + for (u32 i = 0, j = 56; i < j; i += 4, j -= 4) + { + u32 temp; + + temp = rdk[i + 0]; rdk[i + 0] = rdk[j + 0]; rdk[j + 0] = temp; + temp = rdk[i + 1]; rdk[i + 1] = rdk[j + 1]; rdk[j + 1] = temp; + temp = rdk[i + 2]; rdk[i + 2] = rdk[j + 2]; rdk[j + 2] = temp; + temp = rdk[i + 3]; rdk[i + 3] = rdk[j + 3]; rdk[j + 3] = temp; + } + + for (u32 i = 1, j = 4; i < 14; i += 1, j += 4) + { + rdk[j + 0] = + td0[te1[(rdk[j + 0] >> 24) & 0xff] & 0xff] ^ + td1[te1[(rdk[j + 0] >> 16) & 0xff] & 0xff] ^ + td2[te1[(rdk[j + 0] >> 8) & 0xff] & 0xff] ^ + td3[te1[(rdk[j + 0] >> 0) & 0xff] & 0xff]; + + rdk[j + 1] = + td0[te1[(rdk[j + 1] >> 24) & 0xff] & 0xff] ^ + td1[te1[(rdk[j + 1] >> 16) & 0xff] & 0xff] ^ + td2[te1[(rdk[j + 1] >> 8) & 0xff] & 0xff] ^ + td3[te1[(rdk[j + 1] >> 0) & 0xff] & 0xff]; + + rdk[j + 2] = + td0[te1[(rdk[j + 2] >> 24) & 0xff] & 0xff] ^ + td1[te1[(rdk[j + 2] >> 16) & 0xff] & 0xff] ^ + td2[te1[(rdk[j + 2] >> 8) & 0xff] & 0xff] ^ + td3[te1[(rdk[j + 2] >> 0) & 0xff] & 0xff]; + + rdk[j + 3] = + td0[te1[(rdk[j + 3] >> 24) & 0xff] & 0xff] ^ + td1[te1[(rdk[j + 3] >> 16) & 0xff] & 0xff] ^ + td2[te1[(rdk[j + 3] >> 8) & 0xff] & 0xff] ^ + td3[te1[(rdk[j + 3] >> 0) & 0xff] & 0xff]; + } +} + +static void AES256_encrypt (const u32 *in, u32 *out, const u32 *rek) +{ + u32 in_s[4]; + + in_s[0] = byte_swap_32 (in[0]); + in_s[1] = byte_swap_32 (in[1]); + in_s[2] = byte_swap_32 (in[2]); + in_s[3] = byte_swap_32 (in[3]); + + u32 s0 = in_s[0] ^ rek[0]; + u32 s1 = in_s[1] ^ rek[1]; + u32 s2 = in_s[2] ^ rek[2]; + u32 s3 = in_s[3] ^ rek[3]; + + u32 t0; + u32 t1; + u32 t2; + u32 t3; + + t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[ 4]; + t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[ 5]; + t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[ 6]; + t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[ 7]; + s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[ 8]; + s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[ 9]; + s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[10]; + s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[11]; + t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[12]; + t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[13]; + t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[14]; + t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[15]; + s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[16]; + s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[17]; + s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[18]; + s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[19]; + t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[20]; + t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[21]; + t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[22]; + t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[23]; + s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[24]; + s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[25]; + s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[26]; + s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[27]; + t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[28]; + t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[29]; + t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[30]; + t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[31]; + s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[32]; + s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[33]; + s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[34]; + s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[35]; + t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[36]; + t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[37]; + t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[38]; + t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[39]; + s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[40]; + s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[41]; + s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[42]; + s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[43]; + t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[44]; + t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[45]; + t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[46]; + t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[47]; + s0 = te0[t0 >> 24] ^ te1[(t1 >> 16) & 0xff] ^ te2[(t2 >> 8) & 0xff] ^ te3[t3 & 0xff] ^ rek[48]; + s1 = te0[t1 >> 24] ^ te1[(t2 >> 16) & 0xff] ^ te2[(t3 >> 8) & 0xff] ^ te3[t0 & 0xff] ^ rek[49]; + s2 = te0[t2 >> 24] ^ te1[(t3 >> 16) & 0xff] ^ te2[(t0 >> 8) & 0xff] ^ te3[t1 & 0xff] ^ rek[50]; + s3 = te0[t3 >> 24] ^ te1[(t0 >> 16) & 0xff] ^ te2[(t1 >> 8) & 0xff] ^ te3[t2 & 0xff] ^ rek[51]; + t0 = te0[s0 >> 24] ^ te1[(s1 >> 16) & 0xff] ^ te2[(s2 >> 8) & 0xff] ^ te3[s3 & 0xff] ^ rek[52]; + t1 = te0[s1 >> 24] ^ te1[(s2 >> 16) & 0xff] ^ te2[(s3 >> 8) & 0xff] ^ te3[s0 & 0xff] ^ rek[53]; + t2 = te0[s2 >> 24] ^ te1[(s3 >> 16) & 0xff] ^ te2[(s0 >> 8) & 0xff] ^ te3[s1 & 0xff] ^ rek[54]; + t3 = te0[s3 >> 24] ^ te1[(s0 >> 16) & 0xff] ^ te2[(s1 >> 8) & 0xff] ^ te3[s2 & 0xff] ^ rek[55]; + + out[0] = (te4[(t0 >> 24) & 0xff] & 0xff000000) + ^ (te4[(t1 >> 16) & 0xff] & 0x00ff0000) + ^ (te4[(t2 >> 8) & 0xff] & 0x0000ff00) + ^ (te4[(t3 >> 0) & 0xff] & 0x000000ff) + ^ rek[56]; + + out[1] = (te4[(t1 >> 24) & 0xff] & 0xff000000) + ^ (te4[(t2 >> 16) & 0xff] & 0x00ff0000) + ^ (te4[(t3 >> 8) & 0xff] & 0x0000ff00) + ^ (te4[(t0 >> 0) & 0xff] & 0x000000ff) + ^ rek[57]; + + out[2] = (te4[(t2 >> 24) & 0xff] & 0xff000000) + ^ (te4[(t3 >> 16) & 0xff] & 0x00ff0000) + ^ (te4[(t0 >> 8) & 0xff] & 0x0000ff00) + ^ (te4[(t1 >> 0) & 0xff] & 0x000000ff) + ^ rek[58]; + + out[3] = (te4[(t3 >> 24) & 0xff] & 0xff000000) + ^ (te4[(t0 >> 16) & 0xff] & 0x00ff0000) + ^ (te4[(t1 >> 8) & 0xff] & 0x0000ff00) + ^ (te4[(t2 >> 0) & 0xff] & 0x000000ff) + ^ rek[59]; + + out[0] = byte_swap_32 (out[0]); + out[1] = byte_swap_32 (out[1]); + out[2] = byte_swap_32 (out[2]); + out[3] = byte_swap_32 (out[3]); +} + +static void AES256_decrypt (const u32 *in, u32 *out, const u32 *rdk) +{ + u32 in_s[4]; + + in_s[0] = byte_swap_32 (in[0]); + in_s[1] = byte_swap_32 (in[1]); + in_s[2] = byte_swap_32 (in[2]); + in_s[3] = byte_swap_32 (in[3]); + + u32 s0 = in_s[0] ^ rdk[0]; + u32 s1 = in_s[1] ^ rdk[1]; + u32 s2 = in_s[2] ^ rdk[2]; + u32 s3 = in_s[3] ^ rdk[3]; + + u32 t0; + u32 t1; + u32 t2; + u32 t3; + + t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[ 4]; + t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[ 5]; + t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[ 6]; + t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[ 7]; + s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[ 8]; + s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[ 9]; + s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[10]; + s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[11]; + t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[12]; + t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[13]; + t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[14]; + t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[15]; + s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[16]; + s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[17]; + s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[18]; + s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[19]; + t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[20]; + t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[21]; + t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[22]; + t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[23]; + s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[24]; + s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[25]; + s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[26]; + s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[27]; + t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[28]; + t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[29]; + t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[30]; + t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[31]; + s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[32]; + s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[33]; + s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[34]; + s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[35]; + t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[36]; + t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[37]; + t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[38]; + t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[39]; + s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[40]; + s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[41]; + s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[42]; + s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[43]; + t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[44]; + t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[45]; + t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[46]; + t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[47]; + s0 = td0[t0 >> 24] ^ td1[(t3 >> 16) & 0xff] ^ td2[(t2 >> 8) & 0xff] ^ td3[t1 & 0xff] ^ rdk[48]; + s1 = td0[t1 >> 24] ^ td1[(t0 >> 16) & 0xff] ^ td2[(t3 >> 8) & 0xff] ^ td3[t2 & 0xff] ^ rdk[49]; + s2 = td0[t2 >> 24] ^ td1[(t1 >> 16) & 0xff] ^ td2[(t0 >> 8) & 0xff] ^ td3[t3 & 0xff] ^ rdk[50]; + s3 = td0[t3 >> 24] ^ td1[(t2 >> 16) & 0xff] ^ td2[(t1 >> 8) & 0xff] ^ td3[t0 & 0xff] ^ rdk[51]; + t0 = td0[s0 >> 24] ^ td1[(s3 >> 16) & 0xff] ^ td2[(s2 >> 8) & 0xff] ^ td3[s1 & 0xff] ^ rdk[52]; + t1 = td0[s1 >> 24] ^ td1[(s0 >> 16) & 0xff] ^ td2[(s3 >> 8) & 0xff] ^ td3[s2 & 0xff] ^ rdk[53]; + t2 = td0[s2 >> 24] ^ td1[(s1 >> 16) & 0xff] ^ td2[(s0 >> 8) & 0xff] ^ td3[s3 & 0xff] ^ rdk[54]; + t3 = td0[s3 >> 24] ^ td1[(s2 >> 16) & 0xff] ^ td2[(s1 >> 8) & 0xff] ^ td3[s0 & 0xff] ^ rdk[55]; + + out[0] = (td4[(t0 >> 24) & 0xff] & 0xff000000) + ^ (td4[(t3 >> 16) & 0xff] & 0x00ff0000) + ^ (td4[(t2 >> 8) & 0xff] & 0x0000ff00) + ^ (td4[(t1 >> 0) & 0xff] & 0x000000ff) + ^ rdk[56]; + + out[1] = (td4[(t1 >> 24) & 0xff] & 0xff000000) + ^ (td4[(t0 >> 16) & 0xff] & 0x00ff0000) + ^ (td4[(t3 >> 8) & 0xff] & 0x0000ff00) + ^ (td4[(t2 >> 0) & 0xff] & 0x000000ff) + ^ rdk[57]; + + out[2] = (td4[(t2 >> 24) & 0xff] & 0xff000000) + ^ (td4[(t1 >> 16) & 0xff] & 0x00ff0000) + ^ (td4[(t0 >> 8) & 0xff] & 0x0000ff00) + ^ (td4[(t3 >> 0) & 0xff] & 0x000000ff) + ^ rdk[58]; + + out[3] = (td4[(t3 >> 24) & 0xff] & 0xff000000) + ^ (td4[(t2 >> 16) & 0xff] & 0x00ff0000) + ^ (td4[(t1 >> 8) & 0xff] & 0x0000ff00) + ^ (td4[(t0 >> 0) & 0xff] & 0x000000ff) + ^ rdk[59]; + + out[0] = byte_swap_32 (out[0]); + out[1] = byte_swap_32 (out[1]); + out[2] = byte_swap_32 (out[2]); + out[3] = byte_swap_32 (out[3]); +} + +// wrappers + +void AES_set_encrypt_key (const u8 *key, int keysize, AES_KEY *aes_key) +{ + aes_key->bits = keysize; + + if (aes_key->bits == 128) + { + AES128_ExpandKey ((const u32 *) key, aes_key->rek); + } + else if (aes_key->bits == 256) + { + AES256_ExpandKey ((const u32 *) key, aes_key->rek); + } +} + +void AES_set_decrypt_key (const u8 *key, int keysize, AES_KEY *aes_key) +{ + aes_key->bits = keysize; + + if (aes_key->bits == 128) + { + AES128_ExpandKey ((const u32 *) key, aes_key->rdk); + + AES128_InvertKey (aes_key->rdk); + } + else if (aes_key->bits == 256) + { + AES256_ExpandKey ((const u32 *) key, aes_key->rdk); + + AES256_InvertKey (aes_key->rdk); + } +} + +void AES_encrypt (AES_KEY *aes_key, const u8 *input, u8 *output) +{ + if (aes_key->bits == 128) + { + AES128_encrypt ((const u32 *) input, (u32 *) output, aes_key->rek); + } + else if (aes_key->bits == 256) + { + AES256_encrypt ((const u32 *) input, (u32 *) output, aes_key->rek); + } +} + +void AES_decrypt (AES_KEY *aes_key, const u8 *input, u8 *output) +{ + if (aes_key->bits == 128) + { + AES128_decrypt ((const u32 *) input, (u32 *) output, aes_key->rdk); + } + else if (aes_key->bits == 256) + { + AES256_decrypt ((const u32 *) input, (u32 *) output, aes_key->rdk); + } +} diff --git a/src/cpu_des.c b/src/cpu_des.c new file mode 100644 index 000000000..2bbd8cb96 --- /dev/null +++ b/src/cpu_des.c @@ -0,0 +1,116 @@ +/** + * Author......: Jens Steube + * License.....: MIT + */ + +#define IS_GENERIC + +#include "common.h" +#include "types_int.h" +#include "bitops.h" +#include "inc_hash_constants.h" +#include "inc_hash_functions.cl" +#include "cpu_des.h" + +#define BOX(v,i,S) (S)[(i)][(v)] + +void _des_keysetup (u32 data[2], u32 Kc[16], u32 Kd[16], const u32 s_skb[8][64]) +{ + u32 c = data[0]; + u32 d = data[1]; + + u32 tt; + + PERM_OP (d, c, tt, 4, 0x0f0f0f0f); + HPERM_OP (c, tt, 2, 0xcccc0000); + HPERM_OP (d, tt, 2, 0xcccc0000); + PERM_OP (d, c, tt, 1, 0x55555555); + PERM_OP (c, d, tt, 8, 0x00ff00ff); + PERM_OP (d, c, tt, 1, 0x55555555); + + d = ((d & 0x000000ff) << 16) + | ((d & 0x0000ff00) << 0) + | ((d & 0x00ff0000) >> 16) + | ((c & 0xf0000000) >> 4); + + c = c & 0x0fffffff; + + int i; + + for (i = 0; i < 16; i++) + { + const u32 shifts3s0[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; + const u32 shifts3s1[16] = { 27, 27, 26, 26, 26, 26, 26, 26, 27, 26, 26, 26, 26, 26, 26, 27 }; + + c = c >> shifts3s0[i] | c << shifts3s1[i]; + d = d >> shifts3s0[i] | d << shifts3s1[i]; + + c = c & 0x0fffffff; + d = d & 0x0fffffff; + + u32 s = BOX ((( c >> 0) & 0x3f), 0, s_skb) + | BOX ((((c >> 6) & 0x03) + | ((c >> 7) & 0x3c)), 1, s_skb) + | BOX ((((c >> 13) & 0x0f) + | ((c >> 14) & 0x30)), 2, s_skb) + | BOX ((((c >> 20) & 0x01) + | ((c >> 21) & 0x06) + | ((c >> 22) & 0x38)), 3, s_skb); + + u32 t = BOX ((( d >> 0) & 0x3f), 4, s_skb) + | BOX ((((d >> 7) & 0x03) + | ((d >> 8) & 0x3c)), 5, s_skb) + | BOX ((((d >> 15) & 0x3f)), 6, s_skb) + | BOX ((((d >> 21) & 0x0f) + | ((d >> 22) & 0x30)), 7, s_skb); + + Kc[i] = ((t << 16) | (s & 0x0000ffff)); + Kd[i] = ((s >> 16) | (t & 0xffff0000)); + + Kc[i] = rotl32 (Kc[i], 2u); + Kd[i] = rotl32 (Kd[i], 2u); + } +} + +void _des_encrypt (u32 data[2], u32 Kc[16], u32 Kd[16], const u32 s_SPtrans[8][64]) +{ + u32 r = data[0]; + u32 l = data[1]; + + u32 tt; + + IP (r, l, tt); + + r = rotl32 (r, 3u); + l = rotl32 (l, 3u); + + int i; + + for (i = 0; i < 16; i++) + { + u32 u = Kc[i] ^ r; + u32 t = Kd[i] ^ rotl32 (r, 28u); + + l ^= BOX (((u >> 2) & 0x3f), 0, s_SPtrans) + | BOX (((u >> 10) & 0x3f), 2, s_SPtrans) + | BOX (((u >> 18) & 0x3f), 4, s_SPtrans) + | BOX (((u >> 26) & 0x3f), 6, s_SPtrans) + | BOX (((t >> 2) & 0x3f), 1, s_SPtrans) + | BOX (((t >> 10) & 0x3f), 3, s_SPtrans) + | BOX (((t >> 18) & 0x3f), 5, s_SPtrans) + | BOX (((t >> 26) & 0x3f), 7, s_SPtrans); + + tt = l; + l = r; + r = tt; + } + + l = rotl32 (l, 29u); + r = rotl32 (r, 29u); + + FP (r, l, tt); + + data[0] = l; + data[1] = r; +} + diff --git a/src/cpu_md5.c b/src/cpu_md5.c new file mode 100644 index 000000000..09e1b7122 --- /dev/null +++ b/src/cpu_md5.c @@ -0,0 +1,297 @@ +/** + * Author......: Jens Steube + * License.....: MIT + */ + +#define IS_GENERIC + +#include "common.h" +#include "types_int.h" +#include "bitops.h" +#include "inc_hash_constants.h" +#include "inc_hash_functions.cl" +#include "cpu_md5.h" + +void md5_64 (u32 block[16], u32 digest[4]) +{ + u32 w0[4]; + u32 w1[4]; + u32 w2[4]; + u32 w3[4]; + + w0[0] = block[ 0]; + w0[1] = block[ 1]; + w0[2] = block[ 2]; + w0[3] = block[ 3]; + w1[0] = block[ 4]; + w1[1] = block[ 5]; + w1[2] = block[ 6]; + w1[3] = block[ 7]; + w2[0] = block[ 8]; + w2[1] = block[ 9]; + w2[2] = block[10]; + w2[3] = block[11]; + w3[0] = block[12]; + w3[1] = block[13]; + w3[2] = block[14]; + w3[3] = block[15]; + + u32 a = digest[0]; + u32 b = digest[1]; + u32 c = digest[2]; + u32 d = digest[3]; + + MD5_STEP (MD5_Fo, a, b, c, d, w0[0], MD5C00, MD5S00); + MD5_STEP (MD5_Fo, d, a, b, c, w0[1], MD5C01, MD5S01); + MD5_STEP (MD5_Fo, c, d, a, b, w0[2], MD5C02, MD5S02); + MD5_STEP (MD5_Fo, b, c, d, a, w0[3], MD5C03, MD5S03); + MD5_STEP (MD5_Fo, a, b, c, d, w1[0], MD5C04, MD5S00); + MD5_STEP (MD5_Fo, d, a, b, c, w1[1], MD5C05, MD5S01); + MD5_STEP (MD5_Fo, c, d, a, b, w1[2], MD5C06, MD5S02); + MD5_STEP (MD5_Fo, b, c, d, a, w1[3], MD5C07, MD5S03); + MD5_STEP (MD5_Fo, a, b, c, d, w2[0], MD5C08, MD5S00); + MD5_STEP (MD5_Fo, d, a, b, c, w2[1], MD5C09, MD5S01); + MD5_STEP (MD5_Fo, c, d, a, b, w2[2], MD5C0a, MD5S02); + MD5_STEP (MD5_Fo, b, c, d, a, w2[3], MD5C0b, MD5S03); + MD5_STEP (MD5_Fo, a, b, c, d, w3[0], MD5C0c, MD5S00); + MD5_STEP (MD5_Fo, d, a, b, c, w3[1], MD5C0d, MD5S01); + MD5_STEP (MD5_Fo, c, d, a, b, w3[2], MD5C0e, MD5S02); + MD5_STEP (MD5_Fo, b, c, d, a, w3[3], MD5C0f, MD5S03); + + MD5_STEP (MD5_Go, a, b, c, d, w0[1], MD5C10, MD5S10); + MD5_STEP (MD5_Go, d, a, b, c, w1[2], MD5C11, MD5S11); + MD5_STEP (MD5_Go, c, d, a, b, w2[3], MD5C12, MD5S12); + MD5_STEP (MD5_Go, b, c, d, a, w0[0], MD5C13, MD5S13); + MD5_STEP (MD5_Go, a, b, c, d, w1[1], MD5C14, MD5S10); + MD5_STEP (MD5_Go, d, a, b, c, w2[2], MD5C15, MD5S11); + MD5_STEP (MD5_Go, c, d, a, b, w3[3], MD5C16, MD5S12); + MD5_STEP (MD5_Go, b, c, d, a, w1[0], MD5C17, MD5S13); + MD5_STEP (MD5_Go, a, b, c, d, w2[1], MD5C18, MD5S10); + MD5_STEP (MD5_Go, d, a, b, c, w3[2], MD5C19, MD5S11); + MD5_STEP (MD5_Go, c, d, a, b, w0[3], MD5C1a, MD5S12); + MD5_STEP (MD5_Go, b, c, d, a, w2[0], MD5C1b, MD5S13); + MD5_STEP (MD5_Go, a, b, c, d, w3[1], MD5C1c, MD5S10); + MD5_STEP (MD5_Go, d, a, b, c, w0[2], MD5C1d, MD5S11); + MD5_STEP (MD5_Go, c, d, a, b, w1[3], MD5C1e, MD5S12); + MD5_STEP (MD5_Go, b, c, d, a, w3[0], MD5C1f, MD5S13); + + MD5_STEP (MD5_H , a, b, c, d, w1[1], MD5C20, MD5S20); + MD5_STEP (MD5_H , d, a, b, c, w2[0], MD5C21, MD5S21); + MD5_STEP (MD5_H , c, d, a, b, w2[3], MD5C22, MD5S22); + MD5_STEP (MD5_H , b, c, d, a, w3[2], MD5C23, MD5S23); + MD5_STEP (MD5_H , a, b, c, d, w0[1], MD5C24, MD5S20); + MD5_STEP (MD5_H , d, a, b, c, w1[0], MD5C25, MD5S21); + MD5_STEP (MD5_H , c, d, a, b, w1[3], MD5C26, MD5S22); + MD5_STEP (MD5_H , b, c, d, a, w2[2], MD5C27, MD5S23); + MD5_STEP (MD5_H , a, b, c, d, w3[1], MD5C28, MD5S20); + MD5_STEP (MD5_H , d, a, b, c, w0[0], MD5C29, MD5S21); + MD5_STEP (MD5_H , c, d, a, b, w0[3], MD5C2a, MD5S22); + MD5_STEP (MD5_H , b, c, d, a, w1[2], MD5C2b, MD5S23); + MD5_STEP (MD5_H , a, b, c, d, w2[1], MD5C2c, MD5S20); + MD5_STEP (MD5_H , d, a, b, c, w3[0], MD5C2d, MD5S21); + MD5_STEP (MD5_H , c, d, a, b, w3[3], MD5C2e, MD5S22); + MD5_STEP (MD5_H , b, c, d, a, w0[2], MD5C2f, MD5S23); + + MD5_STEP (MD5_I , a, b, c, d, w0[0], MD5C30, MD5S30); + MD5_STEP (MD5_I , d, a, b, c, w1[3], MD5C31, MD5S31); + MD5_STEP (MD5_I , c, d, a, b, w3[2], MD5C32, MD5S32); + MD5_STEP (MD5_I , b, c, d, a, w1[1], MD5C33, MD5S33); + MD5_STEP (MD5_I , a, b, c, d, w3[0], MD5C34, MD5S30); + MD5_STEP (MD5_I , d, a, b, c, w0[3], MD5C35, MD5S31); + MD5_STEP (MD5_I , c, d, a, b, w2[2], MD5C36, MD5S32); + MD5_STEP (MD5_I , b, c, d, a, w0[1], MD5C37, MD5S33); + MD5_STEP (MD5_I , a, b, c, d, w2[0], MD5C38, MD5S30); + MD5_STEP (MD5_I , d, a, b, c, w3[3], MD5C39, MD5S31); + MD5_STEP (MD5_I , c, d, a, b, w1[2], MD5C3a, MD5S32); + MD5_STEP (MD5_I , b, c, d, a, w3[1], MD5C3b, MD5S33); + MD5_STEP (MD5_I , a, b, c, d, w1[0], MD5C3c, MD5S30); + MD5_STEP (MD5_I , d, a, b, c, w2[3], MD5C3d, MD5S31); + MD5_STEP (MD5_I , c, d, a, b, w0[2], MD5C3e, MD5S32); + MD5_STEP (MD5_I , b, c, d, a, w2[1], MD5C3f, MD5S33); + + digest[0] += a; + digest[1] += b; + digest[2] += c; + digest[3] += d; +} + +// only use this when really, really needed, SLOW + +#include + +void md5_complete_no_limit (u32 digest[4], u32 *plain, u32 plain_len) +{ + u32 a = MD5M_A; + u32 b = MD5M_B; + u32 c = MD5M_C; + u32 d = MD5M_D; + + digest[0] = a; + digest[1] = b; + digest[2] = c; + digest[3] = d; + + u32 r_a = digest[0]; + u32 r_b = digest[1]; + u32 r_c = digest[2]; + u32 r_d = digest[3]; + + u32 block[16]; + int block_total_len = 16 * 4; // sizeof (block) + + char *block_ptr = (char *) block; + char *plain_ptr = (char *) plain; + + // init + + int remaining_len = plain_len; + + // loop + + u32 loop = 1; + + while (loop) + { + loop = (remaining_len > 55); + + int cur_len = MIN (block_total_len, remaining_len); + int copy_len = MAX (cur_len, 0); // should never be negative of course + + memcpy (block_ptr, plain_ptr, copy_len); + + // clear the remaining bytes of the block + + memset (block_ptr + copy_len, 0, block_total_len - copy_len); + + /* + * final block + */ + + // set 0x80 if neeeded + + if (cur_len >= 0) + { + if (cur_len != block_total_len) + { + block_ptr[copy_len] = 0x80; + } + } + + // set block[14] set to total_len + + if (! loop) block[14] = plain_len * 8; + + /* + * md5 () + */ + + u32 w0[4]; + u32 w1[4]; + u32 w2[4]; + u32 w3[4]; + + w0[0] = block[ 0]; + w0[1] = block[ 1]; + w0[2] = block[ 2]; + w0[3] = block[ 3]; + w1[0] = block[ 4]; + w1[1] = block[ 5]; + w1[2] = block[ 6]; + w1[3] = block[ 7]; + w2[0] = block[ 8]; + w2[1] = block[ 9]; + w2[2] = block[10]; + w2[3] = block[11]; + w3[0] = block[12]; + w3[1] = block[13]; + w3[2] = block[14]; + w3[3] = block[15]; + + MD5_STEP (MD5_Fo, a, b, c, d, w0[0], MD5C00, MD5S00); + MD5_STEP (MD5_Fo, d, a, b, c, w0[1], MD5C01, MD5S01); + MD5_STEP (MD5_Fo, c, d, a, b, w0[2], MD5C02, MD5S02); + MD5_STEP (MD5_Fo, b, c, d, a, w0[3], MD5C03, MD5S03); + MD5_STEP (MD5_Fo, a, b, c, d, w1[0], MD5C04, MD5S00); + MD5_STEP (MD5_Fo, d, a, b, c, w1[1], MD5C05, MD5S01); + MD5_STEP (MD5_Fo, c, d, a, b, w1[2], MD5C06, MD5S02); + MD5_STEP (MD5_Fo, b, c, d, a, w1[3], MD5C07, MD5S03); + MD5_STEP (MD5_Fo, a, b, c, d, w2[0], MD5C08, MD5S00); + MD5_STEP (MD5_Fo, d, a, b, c, w2[1], MD5C09, MD5S01); + MD5_STEP (MD5_Fo, c, d, a, b, w2[2], MD5C0a, MD5S02); + MD5_STEP (MD5_Fo, b, c, d, a, w2[3], MD5C0b, MD5S03); + MD5_STEP (MD5_Fo, a, b, c, d, w3[0], MD5C0c, MD5S00); + MD5_STEP (MD5_Fo, d, a, b, c, w3[1], MD5C0d, MD5S01); + MD5_STEP (MD5_Fo, c, d, a, b, w3[2], MD5C0e, MD5S02); + MD5_STEP (MD5_Fo, b, c, d, a, w3[3], MD5C0f, MD5S03); + + MD5_STEP (MD5_Go, a, b, c, d, w0[1], MD5C10, MD5S10); + MD5_STEP (MD5_Go, d, a, b, c, w1[2], MD5C11, MD5S11); + MD5_STEP (MD5_Go, c, d, a, b, w2[3], MD5C12, MD5S12); + MD5_STEP (MD5_Go, b, c, d, a, w0[0], MD5C13, MD5S13); + MD5_STEP (MD5_Go, a, b, c, d, w1[1], MD5C14, MD5S10); + MD5_STEP (MD5_Go, d, a, b, c, w2[2], MD5C15, MD5S11); + MD5_STEP (MD5_Go, c, d, a, b, w3[3], MD5C16, MD5S12); + MD5_STEP (MD5_Go, b, c, d, a, w1[0], MD5C17, MD5S13); + MD5_STEP (MD5_Go, a, b, c, d, w2[1], MD5C18, MD5S10); + MD5_STEP (MD5_Go, d, a, b, c, w3[2], MD5C19, MD5S11); + MD5_STEP (MD5_Go, c, d, a, b, w0[3], MD5C1a, MD5S12); + MD5_STEP (MD5_Go, b, c, d, a, w2[0], MD5C1b, MD5S13); + MD5_STEP (MD5_Go, a, b, c, d, w3[1], MD5C1c, MD5S10); + MD5_STEP (MD5_Go, d, a, b, c, w0[2], MD5C1d, MD5S11); + MD5_STEP (MD5_Go, c, d, a, b, w1[3], MD5C1e, MD5S12); + MD5_STEP (MD5_Go, b, c, d, a, w3[0], MD5C1f, MD5S13); + + MD5_STEP (MD5_H , a, b, c, d, w1[1], MD5C20, MD5S20); + MD5_STEP (MD5_H , d, a, b, c, w2[0], MD5C21, MD5S21); + MD5_STEP (MD5_H , c, d, a, b, w2[3], MD5C22, MD5S22); + MD5_STEP (MD5_H , b, c, d, a, w3[2], MD5C23, MD5S23); + MD5_STEP (MD5_H , a, b, c, d, w0[1], MD5C24, MD5S20); + MD5_STEP (MD5_H , d, a, b, c, w1[0], MD5C25, MD5S21); + MD5_STEP (MD5_H , c, d, a, b, w1[3], MD5C26, MD5S22); + MD5_STEP (MD5_H , b, c, d, a, w2[2], MD5C27, MD5S23); + MD5_STEP (MD5_H , a, b, c, d, w3[1], MD5C28, MD5S20); + MD5_STEP (MD5_H , d, a, b, c, w0[0], MD5C29, MD5S21); + MD5_STEP (MD5_H , c, d, a, b, w0[3], MD5C2a, MD5S22); + MD5_STEP (MD5_H , b, c, d, a, w1[2], MD5C2b, MD5S23); + MD5_STEP (MD5_H , a, b, c, d, w2[1], MD5C2c, MD5S20); + MD5_STEP (MD5_H , d, a, b, c, w3[0], MD5C2d, MD5S21); + MD5_STEP (MD5_H , c, d, a, b, w3[3], MD5C2e, MD5S22); + MD5_STEP (MD5_H , b, c, d, a, w0[2], MD5C2f, MD5S23); + + MD5_STEP (MD5_I , a, b, c, d, w0[0], MD5C30, MD5S30); + MD5_STEP (MD5_I , d, a, b, c, w1[3], MD5C31, MD5S31); + MD5_STEP (MD5_I , c, d, a, b, w3[2], MD5C32, MD5S32); + MD5_STEP (MD5_I , b, c, d, a, w1[1], MD5C33, MD5S33); + MD5_STEP (MD5_I , a, b, c, d, w3[0], MD5C34, MD5S30); + MD5_STEP (MD5_I , d, a, b, c, w0[3], MD5C35, MD5S31); + MD5_STEP (MD5_I , c, d, a, b, w2[2], MD5C36, MD5S32); + MD5_STEP (MD5_I , b, c, d, a, w0[1], MD5C37, MD5S33); + MD5_STEP (MD5_I , a, b, c, d, w2[0], MD5C38, MD5S30); + MD5_STEP (MD5_I , d, a, b, c, w3[3], MD5C39, MD5S31); + MD5_STEP (MD5_I , c, d, a, b, w1[2], MD5C3a, MD5S32); + MD5_STEP (MD5_I , b, c, d, a, w3[1], MD5C3b, MD5S33); + MD5_STEP (MD5_I , a, b, c, d, w1[0], MD5C3c, MD5S30); + MD5_STEP (MD5_I , d, a, b, c, w2[3], MD5C3d, MD5S31); + MD5_STEP (MD5_I , c, d, a, b, w0[2], MD5C3e, MD5S32); + MD5_STEP (MD5_I , b, c, d, a, w2[1], MD5C3f, MD5S33); + + remaining_len -= block_total_len; + + plain_ptr += 64; + + a += r_a; + b += r_b; + c += r_c; + d += r_d; + + digest[0] = a; + digest[1] = b; + digest[2] = c; + digest[3] = d; + + r_a = digest[0]; + r_b = digest[1]; + r_c = digest[2]; + r_d = digest[3]; + } +} diff --git a/include/cpu-sha1.c b/src/cpu_sha1.c similarity index 59% rename from include/cpu-sha1.c rename to src/cpu_sha1.c index 973344bb5..d21e8e755 100644 --- a/include/cpu-sha1.c +++ b/src/cpu_sha1.c @@ -3,26 +3,22 @@ * License.....: MIT */ -#define SHA1_F0(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) -#define SHA1_F1(x,y,z) ((x) ^ (y) ^ (z)) -#define SHA1_F2(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y)))) +#define IS_GENERIC -#define SHA1_STEP(f,a,b,c,d,e,x) \ -{ \ - e += K; \ - e += x; \ - e += f (b, c, d); \ - e += rotl32 (a, 5u); \ - b = rotl32 (b, 30u); \ -} +#include "common.h" +#include "types_int.h" +#include "bitops.h" +#include "inc_hash_constants.h" +#include "inc_hash_functions.cl" +#include "cpu_sha1.h" -void sha1_64 (uint block[16], uint digest[5]) +void sha1_64 (u32 block[16], u32 digest[5]) { - u32 A = digest[0]; - u32 B = digest[1]; - u32 C = digest[2]; - u32 D = digest[3]; - u32 E = digest[4]; + u32 a = SHA1M_A; + u32 b = SHA1M_B; + u32 c = SHA1M_C; + u32 d = SHA1M_D; + u32 e = SHA1M_E; u32 w0_t = block[ 0]; u32 w1_t = block[ 1]; @@ -44,102 +40,102 @@ void sha1_64 (uint block[16], uint digest[5]) #undef K #define K SHA1C00 - SHA1_STEP (SHA1_F0 , A, B, C, D, E, w0_t); - SHA1_STEP (SHA1_F0 , E, A, B, C, D, w1_t); - SHA1_STEP (SHA1_F0 , D, E, A, B, C, w2_t); - SHA1_STEP (SHA1_F0 , C, D, E, A, B, w3_t); - SHA1_STEP (SHA1_F0 , B, C, D, E, A, w4_t); - SHA1_STEP (SHA1_F0 , A, B, C, D, E, w5_t); - SHA1_STEP (SHA1_F0 , E, A, B, C, D, w6_t); - SHA1_STEP (SHA1_F0 , D, E, A, B, C, w7_t); - SHA1_STEP (SHA1_F0 , C, D, E, A, B, w8_t); - SHA1_STEP (SHA1_F0 , B, C, D, E, A, w9_t); - SHA1_STEP (SHA1_F0 , A, B, C, D, E, wa_t); - SHA1_STEP (SHA1_F0 , E, A, B, C, D, wb_t); - SHA1_STEP (SHA1_F0 , D, E, A, B, C, wc_t); - SHA1_STEP (SHA1_F0 , C, D, E, A, B, wd_t); - SHA1_STEP (SHA1_F0 , B, C, D, E, A, we_t); - SHA1_STEP (SHA1_F0 , A, B, C, D, E, wf_t); - w0_t = rotl32 ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP (SHA1_F0 , E, A, B, C, D, w0_t); - w1_t = rotl32 ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP (SHA1_F0 , D, E, A, B, C, w1_t); - w2_t = rotl32 ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP (SHA1_F0 , C, D, E, A, B, w2_t); - w3_t = rotl32 ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP (SHA1_F0 , B, C, D, E, A, w3_t); + SHA1_STEP (SHA1_F0o, a, b, c, d, e, w0_t); + SHA1_STEP (SHA1_F0o, e, a, b, c, d, w1_t); + SHA1_STEP (SHA1_F0o, d, e, a, b, c, w2_t); + SHA1_STEP (SHA1_F0o, c, d, e, a, b, w3_t); + SHA1_STEP (SHA1_F0o, b, c, d, e, a, w4_t); + SHA1_STEP (SHA1_F0o, a, b, c, d, e, w5_t); + SHA1_STEP (SHA1_F0o, e, a, b, c, d, w6_t); + SHA1_STEP (SHA1_F0o, d, e, a, b, c, w7_t); + SHA1_STEP (SHA1_F0o, c, d, e, a, b, w8_t); + SHA1_STEP (SHA1_F0o, b, c, d, e, a, w9_t); + SHA1_STEP (SHA1_F0o, a, b, c, d, e, wa_t); + SHA1_STEP (SHA1_F0o, e, a, b, c, d, wb_t); + SHA1_STEP (SHA1_F0o, d, e, a, b, c, wc_t); + SHA1_STEP (SHA1_F0o, c, d, e, a, b, wd_t); + SHA1_STEP (SHA1_F0o, b, c, d, e, a, we_t); + SHA1_STEP (SHA1_F0o, a, b, c, d, e, wf_t); + w0_t = rotl32 ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP (SHA1_F0o, e, a, b, c, d, w0_t); + w1_t = rotl32 ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP (SHA1_F0o, d, e, a, b, c, w1_t); + w2_t = rotl32 ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP (SHA1_F0o, c, d, e, a, b, w2_t); + w3_t = rotl32 ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP (SHA1_F0o, b, c, d, e, a, w3_t); #undef K #define K SHA1C01 - w4_t = rotl32 ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP (SHA1_F1, A, B, C, D, E, w4_t); - w5_t = rotl32 ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP (SHA1_F1, E, A, B, C, D, w5_t); - w6_t = rotl32 ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP (SHA1_F1, D, E, A, B, C, w6_t); - w7_t = rotl32 ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP (SHA1_F1, C, D, E, A, B, w7_t); - w8_t = rotl32 ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP (SHA1_F1, B, C, D, E, A, w8_t); - w9_t = rotl32 ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP (SHA1_F1, A, B, C, D, E, w9_t); - wa_t = rotl32 ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP (SHA1_F1, E, A, B, C, D, wa_t); - wb_t = rotl32 ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP (SHA1_F1, D, E, A, B, C, wb_t); - wc_t = rotl32 ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP (SHA1_F1, C, D, E, A, B, wc_t); - wd_t = rotl32 ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP (SHA1_F1, B, C, D, E, A, wd_t); - we_t = rotl32 ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP (SHA1_F1, A, B, C, D, E, we_t); - wf_t = rotl32 ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP (SHA1_F1, E, A, B, C, D, wf_t); - w0_t = rotl32 ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP (SHA1_F1, D, E, A, B, C, w0_t); - w1_t = rotl32 ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP (SHA1_F1, C, D, E, A, B, w1_t); - w2_t = rotl32 ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP (SHA1_F1, B, C, D, E, A, w2_t); - w3_t = rotl32 ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP (SHA1_F1, A, B, C, D, E, w3_t); - w4_t = rotl32 ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP (SHA1_F1, E, A, B, C, D, w4_t); - w5_t = rotl32 ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP (SHA1_F1, D, E, A, B, C, w5_t); - w6_t = rotl32 ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP (SHA1_F1, C, D, E, A, B, w6_t); - w7_t = rotl32 ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP (SHA1_F1, B, C, D, E, A, w7_t); + w4_t = rotl32 ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, w4_t); + w5_t = rotl32 ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, w5_t); + w6_t = rotl32 ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, w6_t); + w7_t = rotl32 ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, w7_t); + w8_t = rotl32 ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, w8_t); + w9_t = rotl32 ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, w9_t); + wa_t = rotl32 ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, wa_t); + wb_t = rotl32 ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, wb_t); + wc_t = rotl32 ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, wc_t); + wd_t = rotl32 ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, wd_t); + we_t = rotl32 ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, we_t); + wf_t = rotl32 ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, wf_t); + w0_t = rotl32 ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, w0_t); + w1_t = rotl32 ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, w1_t); + w2_t = rotl32 ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, w2_t); + w3_t = rotl32 ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, w3_t); + w4_t = rotl32 ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, w4_t); + w5_t = rotl32 ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, w5_t); + w6_t = rotl32 ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, w6_t); + w7_t = rotl32 ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, w7_t); #undef K #define K SHA1C02 - w8_t = rotl32 ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP (SHA1_F2 , A, B, C, D, E, w8_t); - w9_t = rotl32 ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP (SHA1_F2 , E, A, B, C, D, w9_t); - wa_t = rotl32 ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP (SHA1_F2 , D, E, A, B, C, wa_t); - wb_t = rotl32 ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP (SHA1_F2 , C, D, E, A, B, wb_t); - wc_t = rotl32 ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP (SHA1_F2 , B, C, D, E, A, wc_t); - wd_t = rotl32 ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP (SHA1_F2 , A, B, C, D, E, wd_t); - we_t = rotl32 ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP (SHA1_F2 , E, A, B, C, D, we_t); - wf_t = rotl32 ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP (SHA1_F2 , D, E, A, B, C, wf_t); - w0_t = rotl32 ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP (SHA1_F2 , C, D, E, A, B, w0_t); - w1_t = rotl32 ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP (SHA1_F2 , B, C, D, E, A, w1_t); - w2_t = rotl32 ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP (SHA1_F2 , A, B, C, D, E, w2_t); - w3_t = rotl32 ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP (SHA1_F2 , E, A, B, C, D, w3_t); - w4_t = rotl32 ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP (SHA1_F2 , D, E, A, B, C, w4_t); - w5_t = rotl32 ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP (SHA1_F2 , C, D, E, A, B, w5_t); - w6_t = rotl32 ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP (SHA1_F2 , B, C, D, E, A, w6_t); - w7_t = rotl32 ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP (SHA1_F2 , A, B, C, D, E, w7_t); - w8_t = rotl32 ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP (SHA1_F2 , E, A, B, C, D, w8_t); - w9_t = rotl32 ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP (SHA1_F2 , D, E, A, B, C, w9_t); - wa_t = rotl32 ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP (SHA1_F2 , C, D, E, A, B, wa_t); - wb_t = rotl32 ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP (SHA1_F2 , B, C, D, E, A, wb_t); + w8_t = rotl32 ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP (SHA1_F2o, a, b, c, d, e, w8_t); + w9_t = rotl32 ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP (SHA1_F2o, e, a, b, c, d, w9_t); + wa_t = rotl32 ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP (SHA1_F2o, d, e, a, b, c, wa_t); + wb_t = rotl32 ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP (SHA1_F2o, c, d, e, a, b, wb_t); + wc_t = rotl32 ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP (SHA1_F2o, b, c, d, e, a, wc_t); + wd_t = rotl32 ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP (SHA1_F2o, a, b, c, d, e, wd_t); + we_t = rotl32 ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP (SHA1_F2o, e, a, b, c, d, we_t); + wf_t = rotl32 ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP (SHA1_F2o, d, e, a, b, c, wf_t); + w0_t = rotl32 ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP (SHA1_F2o, c, d, e, a, b, w0_t); + w1_t = rotl32 ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP (SHA1_F2o, b, c, d, e, a, w1_t); + w2_t = rotl32 ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP (SHA1_F2o, a, b, c, d, e, w2_t); + w3_t = rotl32 ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP (SHA1_F2o, e, a, b, c, d, w3_t); + w4_t = rotl32 ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP (SHA1_F2o, d, e, a, b, c, w4_t); + w5_t = rotl32 ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP (SHA1_F2o, c, d, e, a, b, w5_t); + w6_t = rotl32 ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP (SHA1_F2o, b, c, d, e, a, w6_t); + w7_t = rotl32 ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP (SHA1_F2o, a, b, c, d, e, w7_t); + w8_t = rotl32 ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP (SHA1_F2o, e, a, b, c, d, w8_t); + w9_t = rotl32 ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP (SHA1_F2o, d, e, a, b, c, w9_t); + wa_t = rotl32 ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP (SHA1_F2o, c, d, e, a, b, wa_t); + wb_t = rotl32 ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP (SHA1_F2o, b, c, d, e, a, wb_t); #undef K #define K SHA1C03 - wc_t = rotl32 ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP (SHA1_F1, A, B, C, D, E, wc_t); - wd_t = rotl32 ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP (SHA1_F1, E, A, B, C, D, wd_t); - we_t = rotl32 ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP (SHA1_F1, D, E, A, B, C, we_t); - wf_t = rotl32 ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP (SHA1_F1, C, D, E, A, B, wf_t); - w0_t = rotl32 ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP (SHA1_F1, B, C, D, E, A, w0_t); - w1_t = rotl32 ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP (SHA1_F1, A, B, C, D, E, w1_t); - w2_t = rotl32 ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP (SHA1_F1, E, A, B, C, D, w2_t); - w3_t = rotl32 ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP (SHA1_F1, D, E, A, B, C, w3_t); - w4_t = rotl32 ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP (SHA1_F1, C, D, E, A, B, w4_t); - w5_t = rotl32 ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP (SHA1_F1, B, C, D, E, A, w5_t); - w6_t = rotl32 ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP (SHA1_F1, A, B, C, D, E, w6_t); - w7_t = rotl32 ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP (SHA1_F1, E, A, B, C, D, w7_t); - w8_t = rotl32 ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP (SHA1_F1, D, E, A, B, C, w8_t); - w9_t = rotl32 ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP (SHA1_F1, C, D, E, A, B, w9_t); - wa_t = rotl32 ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP (SHA1_F1, B, C, D, E, A, wa_t); - wb_t = rotl32 ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP (SHA1_F1, A, B, C, D, E, wb_t); - wc_t = rotl32 ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP (SHA1_F1, E, A, B, C, D, wc_t); - wd_t = rotl32 ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP (SHA1_F1, D, E, A, B, C, wd_t); - we_t = rotl32 ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP (SHA1_F1, C, D, E, A, B, we_t); - wf_t = rotl32 ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP (SHA1_F1, B, C, D, E, A, wf_t); + wc_t = rotl32 ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, wc_t); + wd_t = rotl32 ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, wd_t); + we_t = rotl32 ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, we_t); + wf_t = rotl32 ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, wf_t); + w0_t = rotl32 ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, w0_t); + w1_t = rotl32 ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, w1_t); + w2_t = rotl32 ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, w2_t); + w3_t = rotl32 ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, w3_t); + w4_t = rotl32 ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, w4_t); + w5_t = rotl32 ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, w5_t); + w6_t = rotl32 ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, w6_t); + w7_t = rotl32 ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, w7_t); + w8_t = rotl32 ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, w8_t); + w9_t = rotl32 ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, w9_t); + wa_t = rotl32 ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, wa_t); + wb_t = rotl32 ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, wb_t); + wc_t = rotl32 ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, wc_t); + wd_t = rotl32 ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, wd_t); + we_t = rotl32 ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, we_t); + wf_t = rotl32 ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, wf_t); - digest[0] += A; - digest[1] += B; - digest[2] += C; - digest[3] += D; - digest[4] += E; + digest[0] += a; + digest[1] += b; + digest[2] += c; + digest[3] += d; + digest[4] += e; } diff --git a/include/cpu-sha256.c b/src/cpu_sha256.c similarity index 81% rename from include/cpu-sha256.c rename to src/cpu_sha256.c index 17299eb44..e9a283bb1 100644 --- a/include/cpu-sha256.c +++ b/src/cpu_sha256.c @@ -3,59 +3,42 @@ * License.....: MIT */ -#define SHIFT_RIGHT_32(x,n) ((x) >> (n)) +#define IS_GENERIC -#define SHA256_S0(x) (rotl32 ((x), 25u) ^ rotl32 ((x), 14u) ^ SHIFT_RIGHT_32 ((x), 3u)) -#define SHA256_S1(x) (rotl32 ((x), 15u) ^ rotl32 ((x), 13u) ^ SHIFT_RIGHT_32 ((x), 10u)) -#define SHA256_S2(x) (rotl32 ((x), 30u) ^ rotl32 ((x), 19u) ^ rotl32 ((x), 10u)) -#define SHA256_S3(x) (rotl32 ((x), 26u) ^ rotl32 ((x), 21u) ^ rotl32 ((x), 7u)) +#include "common.h" +#include "types_int.h" +#include "bitops.h" +#include "inc_hash_constants.h" +#include "inc_hash_functions.cl" +#include "cpu_sha256.h" -#define SHA256_F0(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y)))) -#define SHA256_F1(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) - -#define SHA256_F0o(x,y,z) (SHA256_F0 ((x), (y), (z))) -#define SHA256_F1o(x,y,z) (SHA256_F1 ((x), (y), (z))) - -#define SHA256_STEP(F0,F1,a,b,c,d,e,f,g,h,x,K) \ -{ \ - h += K; \ - h += x; \ - h += SHA256_S3 (e); \ - h += F1 (e,f,g); \ - d += h; \ - h += SHA256_S2 (a); \ - h += F0 (a,b,c); \ -} - -#define SHA256_EXPAND(x,y,z,w) (SHA256_S1 (x) + y + SHA256_S0 (z) + w) - -void sha256_64 (uint block[16], uint digest[8]) +void sha256_64 (u32 block[16], u32 digest[8]) { - uint w0_t = block[ 0]; - uint w1_t = block[ 1]; - uint w2_t = block[ 2]; - uint w3_t = block[ 3]; - uint w4_t = block[ 4]; - uint w5_t = block[ 5]; - uint w6_t = block[ 6]; - uint w7_t = block[ 7]; - uint w8_t = block[ 8]; - uint w9_t = block[ 9]; - uint wa_t = block[10]; - uint wb_t = block[11]; - uint wc_t = block[12]; - uint wd_t = block[13]; - uint we_t = block[14]; - uint wf_t = block[15]; + u32 w0_t = block[ 0]; + u32 w1_t = block[ 1]; + u32 w2_t = block[ 2]; + u32 w3_t = block[ 3]; + u32 w4_t = block[ 4]; + u32 w5_t = block[ 5]; + u32 w6_t = block[ 6]; + u32 w7_t = block[ 7]; + u32 w8_t = block[ 8]; + u32 w9_t = block[ 9]; + u32 wa_t = block[10]; + u32 wb_t = block[11]; + u32 wc_t = block[12]; + u32 wd_t = block[13]; + u32 we_t = block[14]; + u32 wf_t = block[15]; - uint a = digest[0]; - uint b = digest[1]; - uint c = digest[2]; - uint d = digest[3]; - uint e = digest[4]; - uint f = digest[5]; - uint g = digest[6]; - uint h = digest[7]; + u32 a = digest[0]; + u32 b = digest[1]; + u32 c = digest[2]; + u32 d = digest[3]; + u32 e = digest[4]; + u32 f = digest[5]; + u32 g = digest[6]; + u32 h = digest[7]; SHA256_STEP (SHA256_F0o, SHA256_F1o, a, b, c, d, e, f, g, h, w0_t, SHA256C00); SHA256_STEP (SHA256_F0o, SHA256_F1o, h, a, b, c, d, e, f, g, w1_t, SHA256C01); diff --git a/src/ext_ADL.c b/src/ext_ADL.c index 545eb90cb..db9537b66 100644 --- a/src/ext_ADL.c +++ b/src/ext_ADL.c @@ -3,7 +3,11 @@ * License.....: MIT */ -#include +#include "common.h" +#include "logging.h" +#include "memory.h" +#include "dynloader.h" +#include "ext_ADL.h" int adl_init (ADL_PTR *adl) { @@ -11,14 +15,14 @@ int adl_init (ADL_PTR *adl) memset (adl, 0, sizeof (ADL_PTR)); - #if defined( _WIN) + #if defined (_WIN) adl->lib = hc_dlopen ("atiadlxx.dll"); if (!adl->lib) { adl->lib = hc_dlopen ("atiadlxy.dll"); } - #elif defined(_POSIX) + #elif defined (_POSIX) adl->lib = hc_dlopen ("libatiadlxx.so", RTLD_NOW); #endif diff --git a/src/ext_OpenCL.c b/src/ext_OpenCL.c index 2e44b2802..743782e8f 100644 --- a/src/ext_OpenCL.c +++ b/src/ext_OpenCL.c @@ -5,7 +5,11 @@ * License.....: MIT */ -#include +#include "common.h" +#include "logging.h" +#include "memory.h" +#include "dynloader.h" +#include "ext_OpenCL.h" const char *val2cstr_cl (cl_int CL_err) { diff --git a/src/ext_nvapi.c b/src/ext_nvapi.c index 0cc1465d9..3f7a7d60b 100644 --- a/src/ext_nvapi.c +++ b/src/ext_nvapi.c @@ -5,7 +5,11 @@ * License.....: MIT */ -#include +#include "common.h" +#include "logging.h" +#include "memory.h" +#include "dynloader.h" +#include "ext_nvapi.h" int nvapi_init (NVAPI_PTR *nvapi) { @@ -14,9 +18,9 @@ int nvapi_init (NVAPI_PTR *nvapi) memset (nvapi, 0, sizeof (NVAPI_PTR)); #ifdef _WIN - #if defined(WIN64) + #if defined (WIN64) nvapi->lib = hc_dlopen ("nvapi64.dll"); - #elif defined(WIN32) + #elif defined (WIN32) nvapi->lib = hc_dlopen ("nvapi.dll"); #endif #else diff --git a/src/ext_nvml.c b/src/ext_nvml.c index 03e378ffe..526775cc1 100644 --- a/src/ext_nvml.c +++ b/src/ext_nvml.c @@ -5,7 +5,11 @@ * License.....: MIT */ -#include +#include "common.h" +#include "logging.h" +#include "memory.h" +#include "dynloader.h" +#include "ext_nvml.h" int nvml_init (NVML_PTR *nvml) { @@ -34,8 +38,8 @@ int nvml_init (NVML_PTR *nvml) } else { - if (data.quiet == 0) - log_info ("WARNING: NVML library load failed, proceed without NVML HWMon enabled."); + //if (data.quiet == 0) + // log_info ("WARNING: NVML library load failed, proceed without NVML HWMon enabled."); return -1; } @@ -44,8 +48,8 @@ int nvml_init (NVML_PTR *nvml) } else { - if (data.quiet == 0) - log_info ("WARNING: NVML library load failed, proceed without NVML HWMon enabled."); + //if (data.quiet == 0) + // log_info ("WARNING: NVML library load failed, proceed without NVML HWMon enabled."); return -1; } @@ -63,8 +67,8 @@ int nvml_init (NVML_PTR *nvml) if (!nvml->lib) { - if (data.quiet == 0) - log_info ("WARNING: NVML library load failed, proceed without NVML HWMon enabled."); + //if (data.quiet == 0) + // log_info ("WARNING: NVML library load failed, proceed without NVML HWMon enabled."); return -1; } diff --git a/src/ext_xnvctrl.c b/src/ext_xnvctrl.c index a55b5dd16..eebe0a141 100644 --- a/src/ext_xnvctrl.c +++ b/src/ext_xnvctrl.c @@ -3,7 +3,11 @@ * License.....: MIT */ -#include +#include "common.h" +#include "logging.h" +#include "memory.h" +#include "dynloader.h" +#include "ext_xnvctrl.h" int xnvctrl_init (XNVCTRL_PTR *xnvctrl) { @@ -11,20 +15,20 @@ int xnvctrl_init (XNVCTRL_PTR *xnvctrl) memset (xnvctrl, 0, sizeof (XNVCTRL_PTR)); - #if defined(_WIN) + #if defined (_WIN) // unsupport platform? return -1; - #elif defined(_POSIX) + #elif defined (_POSIX) xnvctrl->lib_x11 = dlopen ("libX11.so", RTLD_LAZY); if (xnvctrl->lib_x11 == NULL) { - if (data.quiet == 0) log_info ("WARNING: Failed loading the X11 library: %s", dlerror()); - if (data.quiet == 0) log_info (" Please install libx11-dev package."); - if (data.quiet == 0) log_info (""); + //if (data.quiet == 0) log_info ("WARNING: Failed loading the X11 library: %s", dlerror()); + //if (data.quiet == 0) log_info (" Please install libx11-dev package."); + //if (data.quiet == 0) log_info (""); return -1; } @@ -33,9 +37,9 @@ int xnvctrl_init (XNVCTRL_PTR *xnvctrl) if (xnvctrl->lib_xnvctrl == NULL) { - if (data.quiet == 0) log_info ("WARNING: Failed loading the XNVCTRL library: %s", dlerror()); - if (data.quiet == 0) log_info (" Please install libxnvctrl-dev package."); - if (data.quiet == 0) log_info (""); + //if (data.quiet == 0) log_info ("WARNING: Failed loading the XNVCTRL library: %s", dlerror()); + //if (data.quiet == 0) log_info (" Please install libxnvctrl-dev package."); + //if (data.quiet == 0) log_info (""); return -1; } diff --git a/src/hashcat.c b/src/hashcat.c index 7f0b41998..62c6c93b8 100644 --- a/src/hashcat.c +++ b/src/hashcat.c @@ -6,19 +6,29 @@ * License.....: MIT */ -#ifdef __APPLE__ -#include -#endif - -#ifdef __FreeBSD__ -#include -#endif - #include "common.h" +#include "types_int.h" #include "bitops.h" -#include "shared.h" +#include "memory.h" +#include "logging.h" +#include "ext_OpenCL.h" +#include "ext_ADL.h" +#include "ext_nvapi.h" +#include "ext_nvml.h" +#include "ext_xnvctrl.h" +#include "cpu_aes.h" +#include "cpu_crc32.h" +#include "cpu_des.h" +#include "cpu_md5.h" +#include "cpu_sha1.h" +#include "cpu_sha256.h" +#include "thread.h" +#include "timer.h" +#include "types.h" +#include "rp_cpu.h" #include "rp_kernel_on_cpu.h" -#include "getopt.h" +#include "inc_hash_constants.h" +#include "shared.h" static const char *PROGNAME = "hashcat"; @@ -178,8 +188,10 @@ typedef enum combinator_mode } combinator_mode_t; -#define MIN(a,b) (((a) < (b)) ? (a) : (b)) -#define MAX(a,b) (((a) > (b)) ? (a) : (b)) + +#ifdef _WIN +#define mkdir(name,mode) mkdir (name) +#endif #define MAX_CUT_TRIES 4 @@ -372,7 +384,7 @@ static void (*get_next_word_func) (char *, u32, u32 *, u32 *); static unsigned int full01 = 0x01010101; static unsigned int full80 = 0x80808080; -int SUPPRESS_OUTPUT = 0; +extern int SUPPRESS_OUTPUT; static hc_thread_mutex_t mux_adl; static hc_thread_mutex_t mux_counter; diff --git a/src/logging.c b/src/logging.c new file mode 100644 index 000000000..6d9ac7c69 --- /dev/null +++ b/src/logging.c @@ -0,0 +1,148 @@ +/** + * Author......: Jens Steube + * License.....: MIT + */ + +#include "common.h" +#include "logging.h" + +int SUPPRESS_OUTPUT = 0; + +static int last_len = 0; + +static int log_final (FILE *fp, const char *fmt, va_list ap) +{ + if (last_len) + { + fputc ('\r', fp); + + for (int i = 0; i < last_len; i++) + { + fputc (' ', fp); + } + + fputc ('\r', fp); + } + + char s[4096] = { 0 }; + + int max_len = (int) sizeof (s); + + int len = vsnprintf (s, (size_t)max_len, fmt, ap); + + if (len > max_len) len = max_len; + + fwrite (s, len, 1, fp); + + fflush (fp); + + last_len = len; + + return len; +} + +int log_out_nn (FILE *fp, const char *fmt, ...) +{ + if (SUPPRESS_OUTPUT) return 0; + + va_list ap; + + va_start (ap, fmt); + + const int len = log_final (fp, fmt, ap); + + va_end (ap); + + return len; +} + +int log_info_nn (const char *fmt, ...) +{ + if (SUPPRESS_OUTPUT) return 0; + + va_list ap; + + va_start (ap, fmt); + + const int len = log_final (stdout, fmt, ap); + + va_end (ap); + + return len; +} + +int log_error_nn (const char *fmt, ...) +{ + if (SUPPRESS_OUTPUT) return 0; + + va_list ap; + + va_start (ap, fmt); + + const int len = log_final (stderr, fmt, ap); + + va_end (ap); + + return len; +} + +int log_out (FILE *fp, const char *fmt, ...) +{ + if (SUPPRESS_OUTPUT) return 0; + + va_list ap; + + va_start (ap, fmt); + + const int len = log_final (fp, fmt, ap); + + va_end (ap); + + fputc ('\n', fp); + + last_len = 0; + + return len; +} + +int log_info (const char *fmt, ...) +{ + if (SUPPRESS_OUTPUT) return 0; + + va_list ap; + + va_start (ap, fmt); + + const int len = log_final (stdout, fmt, ap); + + va_end (ap); + + fputc ('\n', stdout); + + last_len = 0; + + return len; +} + +int log_error (const char *fmt, ...) +{ + if (SUPPRESS_OUTPUT) return 0; + + fputc ('\n', stderr); + fputc ('\n', stderr); + + va_list ap; + + va_start (ap, fmt); + + const int len = log_final (stderr, fmt, ap); + + va_end (ap); + + fputc ('\n', stderr); + fputc ('\n', stderr); + + last_len = 0; + + return len; +} diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 000000000..cb07f7d33 --- /dev/null +++ b/src/memory.c @@ -0,0 +1,72 @@ +/** + * Author......: Jens Steube + * License.....: MIT + */ + +#include "common.h" +#include "logging.h" +#include "memory.h" + +void *mycalloc (size_t nmemb, size_t size) +{ + void *p = calloc (nmemb, size); + + if (p == NULL) + { + log_error ("ERROR: %s", MSG_ENOMEM); + + exit (-1); + } + + return (p); +} + +void *mymalloc (size_t size) +{ + void *p = malloc (size); + + if (p == NULL) + { + log_error ("ERROR: %s", MSG_ENOMEM); + + exit (-1); + } + + memset (p, 0, size); + + return (p); +} + +void myfree (void *ptr) +{ + if (ptr == NULL) return; + + free (ptr); +} + +void *myrealloc (void *ptr, size_t oldsz, size_t add) +{ + void *p = realloc (ptr, oldsz + add); + + if (p == NULL) + { + log_error ("ERROR: %s", MSG_ENOMEM); + + exit (-1); + } + + memset ((char *) p + oldsz, 0, add); + + return (p); +} + +char *mystrdup (const char *s) +{ + const size_t len = strlen (s); + + char *b = (char *) mymalloc (len + 1); + + memcpy (b, s, len); + + return (b); +} diff --git a/src/rp_kernel_on_cpu.c b/src/rp_kernel_on_cpu.c index 8c8a7ecb4..15ee947db 100644 --- a/src/rp_kernel_on_cpu.c +++ b/src/rp_kernel_on_cpu.c @@ -6,7 +6,10 @@ #pragma GCC diagnostic ignored "-Wunused-parameter" #pragma GCC diagnostic ignored "-Wunused-function" -#include +#include "common.h" +#include "types_int.h" +#include "rp_cpu.h" +#include "rp_kernel_on_cpu.h" static u32 generate_cmask (u32 buf) { diff --git a/src/shared.c b/src/shared.c index 0058eb201..6e6ba81ae 100644 --- a/src/shared.c +++ b/src/shared.c @@ -6,177 +6,29 @@ * License.....: MIT */ -#ifdef __APPLE__ -#include -#endif - -#ifdef __FreeBSD__ -#include -#include -#endif - -#include "shared.h" +#include "common.h" +#include "types_int.h" #include "bitops.h" - -#include - -/** - * ciphers for use on cpu - */ - -#include "cpu-des.c" -#include "cpu-aes.c" - -/** - * hashes for use on cpu - */ - -#include "cpu-md5.c" -#include "cpu-sha1.c" -#include "cpu-sha256.c" - -/** - * logging - */ - -static int last_len = 0; - -static int log_final (FILE *fp, const char *fmt, va_list ap) -{ - if (last_len) - { - fputc ('\r', fp); - - for (int i = 0; i < last_len; i++) - { - fputc (' ', fp); - } - - fputc ('\r', fp); - } - - char s[4096] = { 0 }; - - int max_len = (int) sizeof (s); - - int len = vsnprintf (s, (size_t)max_len, fmt, ap); - - if (len > max_len) len = max_len; - - fwrite (s, len, 1, fp); - - fflush (fp); - - last_len = len; - - return len; -} - -int log_out_nn (FILE *fp, const char *fmt, ...) -{ - if (SUPPRESS_OUTPUT) return 0; - - va_list ap; - - va_start (ap, fmt); - - const int len = log_final (fp, fmt, ap); - - va_end (ap); - - return len; -} - -int log_info_nn (const char *fmt, ...) -{ - if (SUPPRESS_OUTPUT) return 0; - - va_list ap; - - va_start (ap, fmt); - - const int len = log_final (stdout, fmt, ap); - - va_end (ap); - - return len; -} - -int log_error_nn (const char *fmt, ...) -{ - if (SUPPRESS_OUTPUT) return 0; - - va_list ap; - - va_start (ap, fmt); - - const int len = log_final (stderr, fmt, ap); - - va_end (ap); - - return len; -} - -int log_out (FILE *fp, const char *fmt, ...) -{ - if (SUPPRESS_OUTPUT) return 0; - - va_list ap; - - va_start (ap, fmt); - - const int len = log_final (fp, fmt, ap); - - va_end (ap); - - fputc ('\n', fp); - - last_len = 0; - - return len; -} - -int log_info (const char *fmt, ...) -{ - if (SUPPRESS_OUTPUT) return 0; - - va_list ap; - - va_start (ap, fmt); - - const int len = log_final (stdout, fmt, ap); - - va_end (ap); - - fputc ('\n', stdout); - - last_len = 0; - - return len; -} - -int log_error (const char *fmt, ...) -{ - if (SUPPRESS_OUTPUT) return 0; - - fputc ('\n', stderr); - fputc ('\n', stderr); - - va_list ap; - - va_start (ap, fmt); - - const int len = log_final (stderr, fmt, ap); - - va_end (ap); - - fputc ('\n', stderr); - fputc ('\n', stderr); - - last_len = 0; - - return len; -} +#include "memory.h" +#include "logging.h" +#include "ext_OpenCL.h" +#include "ext_ADL.h" +#include "ext_nvapi.h" +#include "ext_nvml.h" +#include "ext_xnvctrl.h" +#include "cpu_aes.h" +#include "cpu_crc32.h" +#include "cpu_des.h" +#include "cpu_md5.h" +#include "cpu_sha1.h" +#include "cpu_sha256.h" +#include "thread.h" +#include "timer.h" +#include "types.h" +#include "rp_cpu.h" +#include "rp_kernel_on_cpu.h" +#include "inc_hash_constants.h" +#include "shared.h" /** * converter @@ -2455,75 +2307,9 @@ static int tty_fix() #endif /** - * mem alloc + * logfile */ -#define MSG_ENOMEM "Insufficient memory available" - -void *mycalloc (size_t nmemb, size_t size) -{ - void *p = calloc (nmemb, size); - - if (p == NULL) - { - log_error ("ERROR: %s", MSG_ENOMEM); - - exit (-1); - } - - return (p); -} - -void *mymalloc (size_t size) -{ - void *p = malloc (size); - - if (p == NULL) - { - log_error ("ERROR: %s", MSG_ENOMEM); - - exit (-1); - } - - memset (p, 0, size); - - return (p); -} - -void myfree (void *ptr) -{ - if (ptr == NULL) return; - - free (ptr); -} - -void *myrealloc (void *ptr, size_t oldsz, size_t add) -{ - void *p = realloc (ptr, oldsz + add); - - if (p == NULL) - { - log_error ("ERROR: %s", MSG_ENOMEM); - - exit (-1); - } - - memset ((char *) p + oldsz, 0, add); - - return (p); -} - -char *mystrdup (const char *s) -{ - const size_t len = strlen (s); - - char *b = (char *) mymalloc (len + 1); - - memcpy (b, s, len); - - return (b); -} - static FILE *logfile_open (char *logfile) { FILE *fp = fopen (logfile, "ab"); @@ -4335,8 +4121,6 @@ char *get_exec_path () #elif __FreeBSD__ - #include - int mib[4]; mib[0] = CTL_KERN; mib[1] = KERN_PROC; @@ -11478,10 +11262,10 @@ int netntlmv1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf) uint dgst[4] = { 0 }; - dgst[0] = MAGIC_A; - dgst[1] = MAGIC_B; - dgst[2] = MAGIC_C; - dgst[3] = MAGIC_D; + dgst[0] = MD5M_A; + dgst[1] = MD5M_B; + dgst[2] = MD5M_C; + dgst[3] = MD5M_D; md5_64 (w, dgst); @@ -12032,7 +11816,7 @@ int ipb2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf) salt_pc_block[14] = salt_len * 8; - uint salt_pc_digest[4] = { MAGIC_A, MAGIC_B, MAGIC_C, MAGIC_D }; + uint salt_pc_digest[4] = { MD5M_A, MD5M_B, MD5M_C, MD5M_D }; md5_64 (salt_pc_block, salt_pc_digest);