aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2013-01-15 19:52:30 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2013-01-15 19:52:30 +0100
commit5b7f50f37210706c0f508788991de88244c7b29b (patch)
treeb6692562f85df20279dcf74d13c17e23ac656c85
parentac4100e103ca2b4e6e782c5814b1f43cef58c00b (diff)
downloadbusybox-w32-5b7f50f37210706c0f508788991de88244c7b29b.tar.gz
busybox-w32-5b7f50f37210706c0f508788991de88244c7b29b.tar.bz2
busybox-w32-5b7f50f37210706c0f508788991de88244c7b29b.zip
sha3: cosmetic tweaks to various names, comments. No logic changes.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--libbb/hash_md5_sha.c124
1 files changed, 63 insertions, 61 deletions
diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c
index 4cd2244a1..18e426079 100644
--- a/libbb/hash_md5_sha.c
+++ b/libbb/hash_md5_sha.c
@@ -926,66 +926,67 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf)
926# define SHA3_SMALL CONFIG_SHA3_SMALL 926# define SHA3_SMALL CONFIG_SHA3_SMALL
927#endif 927#endif
928 928
929#define ARCH_IS_64BIT (sizeof(long) >= sizeof(uint64_t))
930
931enum { 929enum {
932 cKeccakR_SizeInBytes = 576 / 8, 930 KECCAK_IBLK_BYTES = 576 / 8,
933 cKeccakNumberOfRounds = 24, 931 KECCAK_NROUNDS = 24,
934}; 932};
935 933
936/* Elements should be 64-bit, but top half is always zero or 0x80000000. 934/* Elements should be 64-bit, but top half is always zero or 0x80000000.
937 * It is encoded as a separate word below. 935 * We encode 63rd bits in a separate word below.
938 * Same is true for 31th bits. 936 * Same is true for 31th bits, which lets us use 16-bit table instead of 64-bit.
937 * The speed penalty is lost in the noise.
939 */ 938 */
940static const uint16_t KeccakF_RoundConstants[cKeccakNumberOfRounds] = { 939static const uint16_t KECCAK_IOTA_CONST[KECCAK_NROUNDS] = {
941 0x0001UL, 940 0x0001U,
942 0x8082UL, 941 0x8082U,
943 0x808aUL, 942 0x808aU,
944 0x8000UL, 943 0x8000U,
945 0x808bUL, 944 0x808bU,
946 0x0001UL, 945 0x0001U,
947 0x8081UL, 946 0x8081U,
948 0x8009UL, 947 0x8009U,
949 0x008aUL, 948 0x008aU,
950 0x0088UL, 949 0x0088U,
951 0x8009UL, 950 0x8009U,
952 0x000aUL, 951 0x000aU,
953 0x808bUL, 952 0x808bU,
954 0x008bUL, 953 0x008bU,
955 0x8089UL, 954 0x8089U,
956 0x8003UL, 955 0x8003U,
957 0x8002UL, 956 0x8002U,
958 0x0080UL, 957 0x0080U,
959 0x800aUL, 958 0x800aU,
960 0x000aUL, 959 0x000aU,
961 0x8081UL, 960 0x8081U,
962 0x8080UL, 961 0x8080U,
963 0x0001UL, 962 0x0001U,
964 0x8008UL 963 0x8008U,
965}; 964};
966/* 0th first - 0011 0011 0000 0111 1101 1101: */ 965/* bit from CONST[0] is msb: 0011 0011 0000 0111 1101 1101 */
967#define KeccakF_RoundConstantBit63 ((uint32_t)(0x3307dd00)) 966#define KECCAK_IOTA_CONST_bit63 ((uint32_t)(0x3307dd00))
968/* 0th first - 0001 0110 0011 1000 0001 1011: */ 967/* bit from CONST[0] is msb: 0001 0110 0011 1000 0001 1011 */
969#define KeccakF_RoundConstantBit31 ((uint32_t)(0x16381b00)) 968#define KECCAK_IOTA_CONST_bit31 ((uint32_t)(0x16381b00))
970 969
971static const uint8_t KeccakF_RotationConstants[25] = { 970static const uint8_t KECCAK_ROT_CONST[25] = {
972 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, 971 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62,
973 18, 39, 61, 20, 44 972 18, 39, 61, 20, 44
974}; 973};
975 974
976static const uint8_t KeccakF_PiLane[25] = { 975static const uint8_t KECCAK_PI_LANE[25] = {
977 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 976 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20,
978 14, 22, 9, 6, 1 977 14, 22, 9, 6, 1
979}; 978};
980 979
981static const uint8_t KeccakF_Mod5[10] = { 980static const uint8_t MOD5[10] = {
982 0, 1, 2, 3, 4, 0, 1, 2, 3, 4 981 0, 1, 2, 3, 4, 0, 1, 2, 3, 4
983}; 982};
984 983
984#define ARCH_IS_64BIT (sizeof(long) >= sizeof(uint64_t))
985
985static void KeccakF(uint64_t *state) 986static void KeccakF(uint64_t *state)
986{ 987{
987 uint8_t x, y; 988 unsigned x, y;
988 int round; 989 unsigned round;
989 990
990 if (BB_BIG_ENDIAN) { 991 if (BB_BIG_ENDIAN) {
991 for (x = 0; x < 25; x++) { 992 for (x = 0; x < 25; x++) {
@@ -993,7 +994,7 @@ static void KeccakF(uint64_t *state)
993 } 994 }
994 } 995 }
995 996
996 for (round = 0; round < cKeccakNumberOfRounds; ++round) { 997 for (round = 0; round < KECCAK_NROUNDS; ++round) {
997 /* Theta */ 998 /* Theta */
998 { 999 {
999 uint64_t BC[10]; 1000 uint64_t BC[10];
@@ -1003,7 +1004,7 @@ static void KeccakF(uint64_t *state)
1003 ^ state[x + 15] ^ state[x + 20]; 1004 ^ state[x + 15] ^ state[x + 20];
1004 } 1005 }
1005 /* Using 2x5 vector above eliminates the need to use 1006 /* Using 2x5 vector above eliminates the need to use
1006 * [Mod5[x+N]] index trick below to calculate (x+N) % 5, 1007 * BC[MOD5[x+N]] trick below to fetch BC[(x+N) % 5],
1007 * and the code is a bit _smaller_. 1008 * and the code is a bit _smaller_.
1008 */ 1009 */
1009 for (x = 0; x < 5; ++x) { 1010 for (x = 0; x < 5; ++x) {
@@ -1027,22 +1028,24 @@ static void KeccakF(uint64_t *state)
1027 if (SHA3_SMALL) { 1028 if (SHA3_SMALL) {
1028 uint64_t t1 = state[1]; 1029 uint64_t t1 = state[1];
1029 for (x = 0; x < 24; ++x) { 1030 for (x = 0; x < 24; ++x) {
1030 uint64_t t0 = state[KeccakF_PiLane[x]]; 1031 uint64_t t0 = state[KECCAK_PI_LANE[x]];
1031 state[KeccakF_PiLane[x]] = rotl64(t1, KeccakF_RotationConstants[x]); 1032 state[KECCAK_PI_LANE[x]] = rotl64(t1, KECCAK_ROT_CONST[x]);
1032 t1 = t0; 1033 t1 = t0;
1033 } 1034 }
1034 } else { 1035 } else {
1035 /* Especially large benefit for 32-bit arch (75% faster): 1036 /* Especially large benefit for 32-bit arch (75% faster):
1036 * 64-bit rotations by non-constant usually are SLOW on those. 1037 * 64-bit rotations by non-constant usually are SLOW on those.
1037 * We resort to unrolling here. 1038 * We resort to unrolling here.
1038 * This optimizes out KeccakF_PiLane[] and KeccakF_RotationConstants[], 1039 * This optimizes out KECCAK_PI_LANE[] and KECCAK_ROT_CONST[],
1039 * but generates 300-500 more bytes of code. 1040 * but generates 300-500 more bytes of code.
1040 */ 1041 */
1041 uint64_t t0; 1042 uint64_t t0;
1042 uint64_t t1 = state[1]; 1043 uint64_t t1 = state[1];
1043#define RhoPi_twice(x) \ 1044#define RhoPi_twice(x) \
1044 t0 = state[KeccakF_PiLane[x ]]; state[KeccakF_PiLane[x ]] = rotl64(t1, KeccakF_RotationConstants[x ]); \ 1045 t0 = state[KECCAK_PI_LANE[x ]]; \
1045 t1 = state[KeccakF_PiLane[x+1]]; state[KeccakF_PiLane[x+1]] = rotl64(t0, KeccakF_RotationConstants[x+1]); 1046 state[KECCAK_PI_LANE[x ]] = rotl64(t1, KECCAK_ROT_CONST[x ]); \
1047 t1 = state[KECCAK_PI_LANE[x+1]]; \
1048 state[KECCAK_PI_LANE[x+1]] = rotl64(t0, KECCAK_ROT_CONST[x+1]);
1046 RhoPi_twice(0); RhoPi_twice(2); 1049 RhoPi_twice(0); RhoPi_twice(2);
1047 RhoPi_twice(4); RhoPi_twice(6); 1050 RhoPi_twice(4); RhoPi_twice(6);
1048 RhoPi_twice(8); RhoPi_twice(10); 1051 RhoPi_twice(8); RhoPi_twice(10);
@@ -1063,8 +1066,8 @@ static void KeccakF(uint64_t *state)
1063 BC[4] = state[y + 4]; 1066 BC[4] = state[y + 4];
1064 for (x = 0; x < 5; ++x) { 1067 for (x = 0; x < 5; ++x) {
1065 state[y + x] = 1068 state[y + x] =
1066 BC[x] ^ ((~BC[KeccakF_Mod5[x + 1]]) & 1069 BC[x] ^ ((~BC[MOD5[x + 1]]) &
1067 BC[KeccakF_Mod5[x + 2]]); 1070 BC[MOD5[x + 2]]);
1068 } 1071 }
1069 } else { 1072 } else {
1070 /* 32-bit x86: +50 bytes code, 10% faster */ 1073 /* 32-bit x86: +50 bytes code, 10% faster */
@@ -1083,9 +1086,9 @@ static void KeccakF(uint64_t *state)
1083 } 1086 }
1084 1087
1085 /* Iota */ 1088 /* Iota */
1086 state[0] ^= KeccakF_RoundConstants[round] 1089 state[0] ^= KECCAK_IOTA_CONST[round]
1087 | (uint32_t)((KeccakF_RoundConstantBit31 << round) & 0x80000000) 1090 | (uint32_t)((KECCAK_IOTA_CONST_bit31 << round) & 0x80000000)
1088 | (uint64_t)((KeccakF_RoundConstantBit63 << round) & 0x80000000) << 32; 1091 | (uint64_t)((KECCAK_IOTA_CONST_bit63 << round) & 0x80000000) << 32;
1089 } 1092 }
1090 1093
1091 if (BB_BIG_ENDIAN) { 1094 if (BB_BIG_ENDIAN) {
@@ -1095,6 +1098,8 @@ static void KeccakF(uint64_t *state)
1095 } 1098 }
1096} 1099}
1097 1100
1101#undef ARCH_IS_64BIT
1102
1098void FAST_FUNC sha3_begin(sha3_ctx_t *ctx) 1103void FAST_FUNC sha3_begin(sha3_ctx_t *ctx)
1099{ 1104{
1100 memset(ctx, 0, sizeof(*ctx)); 1105 memset(ctx, 0, sizeof(*ctx));
@@ -1110,19 +1115,19 @@ void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buf, size_t bytes)
1110 buffer[ctx->bytes_queued] ^= *data++; 1115 buffer[ctx->bytes_queued] ^= *data++;
1111 bytes--; 1116 bytes--;
1112 ctx->bytes_queued++; 1117 ctx->bytes_queued++;
1113 if (ctx->bytes_queued == cKeccakR_SizeInBytes) { 1118 if (ctx->bytes_queued == KECCAK_IBLK_BYTES) {
1114 KeccakF(ctx->state); 1119 KeccakF(ctx->state);
1115 ctx->bytes_queued = 0; 1120 ctx->bytes_queued = 0;
1116 } 1121 }
1117 } 1122 }
1118 1123
1119 /* Absorb complete blocks */ 1124 /* Absorb complete blocks */
1120 while (bytes >= cKeccakR_SizeInBytes) { 1125 while (bytes >= KECCAK_IBLK_BYTES) {
1121 /* XOR data onto beginning of state[]. 1126 /* XOR data onto beginning of state[].
1122 * We try to be efficient - operate on word at a time, not byte. 1127 * We try to be efficient - operate on word at a time, not byte.
1123 * Yet safe wrt unaligned access: can't just use "*(long*)data"... 1128 * Yet safe wrt unaligned access: can't just use "*(long*)data"...
1124 */ 1129 */
1125 unsigned count = cKeccakR_SizeInBytes / sizeof(long); 1130 unsigned count = KECCAK_IBLK_BYTES / sizeof(long);
1126 long *buffer = (long*)ctx->state; 1131 long *buffer = (long*)ctx->state;
1127 do { 1132 do {
1128 long v; 1133 long v;
@@ -1132,7 +1137,7 @@ void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buf, size_t bytes)
1132 } while (--count); 1137 } while (--count);
1133 1138
1134 KeccakF(ctx->state); 1139 KeccakF(ctx->state);
1135 bytes -= cKeccakR_SizeInBytes; 1140 bytes -= KECCAK_IBLK_BYTES;
1136 } 1141 }
1137 1142
1138 /* Queue remaining data bytes */ 1143 /* Queue remaining data bytes */
@@ -1148,11 +1153,8 @@ void FAST_FUNC sha3_end(sha3_ctx_t *ctx, uint8_t *hashval)
1148{ 1153{
1149 /* Padding */ 1154 /* Padding */
1150 uint8_t *buffer = (uint8_t*)ctx->state; 1155 uint8_t *buffer = (uint8_t*)ctx->state;
1151 /* 0 is the number of bits in last, incomplete byte 1156 buffer[ctx->bytes_queued] ^= 1;
1152 * (that is, zero: we never have incomplete bytes): 1157 buffer[KECCAK_IBLK_BYTES - 1] ^= 0x80;
1153 */
1154 buffer[ctx->bytes_queued] ^= 1 << 0;
1155 buffer[cKeccakR_SizeInBytes - 1] ^= 0x80;
1156 1158
1157 KeccakF(ctx->state); 1159 KeccakF(ctx->state);
1158 1160