diff options
author | Ron Yorston <rmy@pobox.com> | 2014-10-06 12:50:22 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2014-10-06 12:50:22 +0100 |
commit | b04d11dcbadda2620743a1dd923938f2f3043a38 (patch) | |
tree | 971afe425a81304b79e44122e220c7a69efe2616 /libbb | |
parent | 124bbf02948b7ac0babb4ead04acd1559db182d3 (diff) | |
parent | 760d035699c4a878f9109544c1d35ea0d5f6b76c (diff) | |
download | busybox-w32-b04d11dcbadda2620743a1dd923938f2f3043a38.tar.gz busybox-w32-b04d11dcbadda2620743a1dd923938f2f3043a38.tar.bz2 busybox-w32-b04d11dcbadda2620743a1dd923938f2f3043a38.zip |
Merge branch 'busybox' into merge
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/Kbuild.src | 1 | ||||
-rw-r--r-- | libbb/appletlib.c | 29 | ||||
-rw-r--r-- | libbb/bb_askpass.c | 5 | ||||
-rw-r--r-- | libbb/correct_password.c | 96 | ||||
-rw-r--r-- | libbb/hash_md5_sha.c | 317 | ||||
-rw-r--r-- | libbb/xfuncs.c | 10 |
6 files changed, 395 insertions, 63 deletions
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src index d7ab1b129..1f2ed36d9 100644 --- a/libbb/Kbuild.src +++ b/libbb/Kbuild.src | |||
@@ -152,6 +152,7 @@ lib-$(CONFIG_VLOCK) += pw_encrypt.o correct_password.o | |||
152 | lib-$(CONFIG_SU) += pw_encrypt.o correct_password.o | 152 | lib-$(CONFIG_SU) += pw_encrypt.o correct_password.o |
153 | lib-$(CONFIG_LOGIN) += pw_encrypt.o correct_password.o | 153 | lib-$(CONFIG_LOGIN) += pw_encrypt.o correct_password.o |
154 | lib-$(CONFIG_FEATURE_HTTPD_AUTH_MD5) += pw_encrypt.o | 154 | lib-$(CONFIG_FEATURE_HTTPD_AUTH_MD5) += pw_encrypt.o |
155 | lib-$(CONFIG_FEATURE_FTP_AUTHENTICATION) += pw_encrypt.o | ||
155 | 156 | ||
156 | lib-$(CONFIG_DF) += find_mount_point.o | 157 | lib-$(CONFIG_DF) += find_mount_point.o |
157 | lib-$(CONFIG_MKFS_MINIX) += find_mount_point.o | 158 | lib-$(CONFIG_MKFS_MINIX) += find_mount_point.o |
diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 1d0a8f711..2c85062b4 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c | |||
@@ -184,8 +184,7 @@ void lbb_prepare(const char *applet | |||
184 | #endif | 184 | #endif |
185 | applet_name = applet; | 185 | applet_name = applet; |
186 | 186 | ||
187 | /* Set locale for everybody except 'init' */ | 187 | if (ENABLE_LOCALE_SUPPORT) |
188 | if (ENABLE_LOCALE_SUPPORT && getpid() != 1) | ||
189 | setlocale(LC_ALL, ""); | 188 | setlocale(LC_ALL, ""); |
190 | 189 | ||
191 | #if ENABLE_PLATFORM_MINGW32 | 190 | #if ENABLE_PLATFORM_MINGW32 |
@@ -759,15 +758,25 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv) | |||
759 | 758 | ||
760 | /* Reinit some shared global data */ | 759 | /* Reinit some shared global data */ |
761 | xfunc_error_retval = EXIT_FAILURE; | 760 | xfunc_error_retval = EXIT_FAILURE; |
762 | |||
763 | applet_name = APPLET_NAME(applet_no); | 761 | applet_name = APPLET_NAME(applet_no); |
764 | if (argc == 2 && strcmp(argv[1], "--help") == 0) { | 762 | |
765 | /* Special case. POSIX says "test --help" | 763 | #if defined APPLET_NO_test |
766 | * should be no different from e.g. "test --foo". */ | 764 | /* Special case. POSIX says "test --help" |
767 | //TODO: just compare applet_no with APPLET_NO_test | 765 | * should be no different from e.g. "test --foo". |
768 | if (!ENABLE_TEST || strcmp(applet_name, "test") != 0) { | 766 | * Thus for "test", we skip --help check. |
769 | /* If you want "foo --help" to return 0: */ | 767 | */ |
770 | xfunc_error_retval = 0; | 768 | if (applet_no != APPLET_NO_test) |
769 | #endif | ||
770 | { | ||
771 | if (argc == 2 && strcmp(argv[1], "--help") == 0) { | ||
772 | #if defined APPLET_NO_false | ||
773 | /* Someone insisted that "false --help" must exit 1. Sigh */ | ||
774 | if (applet_no != APPLET_NO_false) | ||
775 | #endif | ||
776 | { | ||
777 | /* Make "foo --help" exit with 0: */ | ||
778 | xfunc_error_retval = 0; | ||
779 | } | ||
771 | bb_show_usage(); | 780 | bb_show_usage(); |
772 | } | 781 | } |
773 | } | 782 | } |
diff --git a/libbb/bb_askpass.c b/libbb/bb_askpass.c index 77c1bcd95..1927ba9e9 100644 --- a/libbb/bb_askpass.c +++ b/libbb/bb_askpass.c | |||
@@ -30,9 +30,12 @@ char* FAST_FUNC bb_ask(const int fd, int timeout, const char *prompt) | |||
30 | struct sigaction sa, oldsa; | 30 | struct sigaction sa, oldsa; |
31 | struct termios tio, oldtio; | 31 | struct termios tio, oldtio; |
32 | 32 | ||
33 | tcflush(fd, TCIFLUSH); | ||
34 | /* Was buggy: was printing prompt *before* flushing input, | ||
35 | * which was upsetting "expect" based scripts of some users. | ||
36 | */ | ||
33 | fputs(prompt, stdout); | 37 | fputs(prompt, stdout); |
34 | fflush_all(); | 38 | fflush_all(); |
35 | tcflush(fd, TCIFLUSH); | ||
36 | 39 | ||
37 | tcgetattr(fd, &oldtio); | 40 | tcgetattr(fd, &oldtio); |
38 | tio = oldtio; | 41 | tio = oldtio; |
diff --git a/libbb/correct_password.c b/libbb/correct_password.c index acadf3914..513c93028 100644 --- a/libbb/correct_password.c +++ b/libbb/correct_password.c | |||
@@ -30,6 +30,63 @@ | |||
30 | 30 | ||
31 | #include "libbb.h" | 31 | #include "libbb.h" |
32 | 32 | ||
33 | #define SHADOW_BUFSIZE 256 | ||
34 | |||
35 | /* Retrieve encrypted password string for pw. | ||
36 | * If pw == NULL, return a string which fails password check against any | ||
37 | * password. | ||
38 | */ | ||
39 | #if !ENABLE_FEATURE_SHADOWPASSWDS | ||
40 | #define get_passwd(pw, buffer) get_passwd(pw) | ||
41 | #endif | ||
42 | static const char *get_passwd(const struct passwd *pw, char buffer[SHADOW_BUFSIZE]) | ||
43 | { | ||
44 | const char *pass; | ||
45 | |||
46 | if (!pw) | ||
47 | return "aa"; /* "aa" will never match */ | ||
48 | |||
49 | pass = pw->pw_passwd; | ||
50 | #if ENABLE_FEATURE_SHADOWPASSWDS | ||
51 | /* Using _r function to avoid pulling in static buffers */ | ||
52 | if ((pass[0] == 'x' || pass[0] == '*') && !pass[1]) { | ||
53 | struct spwd spw; | ||
54 | int r; | ||
55 | /* getspnam_r may return 0 yet set result to NULL. | ||
56 | * At least glibc 2.4 does this. Be extra paranoid here. */ | ||
57 | struct spwd *result = NULL; | ||
58 | r = getspnam_r(pw->pw_name, &spw, buffer, SHADOW_BUFSIZE, &result); | ||
59 | pass = (r || !result) ? "aa" : result->sp_pwdp; | ||
60 | } | ||
61 | #endif | ||
62 | return pass; | ||
63 | } | ||
64 | |||
65 | /* | ||
66 | * Return 1 if PW has an empty password. | ||
67 | * Return 1 if the user gives the correct password for entry PW, | ||
68 | * 0 if not. | ||
69 | * NULL pw means "just fake it for login with bad username" | ||
70 | */ | ||
71 | int FAST_FUNC check_password(const struct passwd *pw, const char *plaintext) | ||
72 | { | ||
73 | IF_FEATURE_SHADOWPASSWDS(char buffer[SHADOW_BUFSIZE];) | ||
74 | char *encrypted; | ||
75 | const char *pw_pass; | ||
76 | int r; | ||
77 | |||
78 | pw_pass = get_passwd(pw, buffer); | ||
79 | if (!pw_pass[0]) { /* empty password field? */ | ||
80 | return 1; | ||
81 | } | ||
82 | |||
83 | encrypted = pw_encrypt(plaintext, /*salt:*/ pw_pass, 1); | ||
84 | r = (strcmp(encrypted, pw_pass) == 0); | ||
85 | free(encrypted); | ||
86 | return r; | ||
87 | } | ||
88 | |||
89 | |||
33 | /* Ask the user for a password. | 90 | /* Ask the user for a password. |
34 | * Return 1 without asking if PW has an empty password. | 91 | * Return 1 without asking if PW has an empty password. |
35 | * Return -1 on EOF, error while reading input, or timeout. | 92 | * Return -1 on EOF, error while reading input, or timeout. |
@@ -41,42 +98,23 @@ | |||
41 | int FAST_FUNC ask_and_check_password_extended(const struct passwd *pw, | 98 | int FAST_FUNC ask_and_check_password_extended(const struct passwd *pw, |
42 | int timeout, const char *prompt) | 99 | int timeout, const char *prompt) |
43 | { | 100 | { |
44 | char *unencrypted, *encrypted; | 101 | IF_FEATURE_SHADOWPASSWDS(char buffer[SHADOW_BUFSIZE];) |
45 | const char *correct; | 102 | char *plaintext; |
103 | const char *pw_pass; | ||
46 | int r; | 104 | int r; |
47 | /* fake salt. crypt() can choke otherwise. */ | ||
48 | correct = "aa"; | ||
49 | if (!pw) { | ||
50 | /* "aa" will never match */ | ||
51 | goto fake_it; | ||
52 | } | ||
53 | correct = pw->pw_passwd; | ||
54 | #if ENABLE_FEATURE_SHADOWPASSWDS | ||
55 | /* Using _r function to avoid pulling in static buffers */ | ||
56 | if ((correct[0] == 'x' || correct[0] == '*') && !correct[1]) { | ||
57 | struct spwd spw; | ||
58 | char buffer[256]; | ||
59 | /* getspnam_r may return 0 yet set result to NULL. | ||
60 | * At least glibc 2.4 does this. Be extra paranoid here. */ | ||
61 | struct spwd *result = NULL; | ||
62 | r = getspnam_r(pw->pw_name, &spw, buffer, sizeof(buffer), &result); | ||
63 | correct = (r || !result) ? "aa" : result->sp_pwdp; | ||
64 | } | ||
65 | #endif | ||
66 | 105 | ||
67 | if (!correct[0]) /* empty password field? */ | 106 | pw_pass = get_passwd(pw, buffer); |
107 | if (!pw_pass[0]) /* empty password field? */ | ||
68 | return 1; | 108 | return 1; |
69 | 109 | ||
70 | fake_it: | 110 | plaintext = bb_ask(STDIN_FILENO, timeout, prompt); |
71 | unencrypted = bb_ask(STDIN_FILENO, timeout, prompt); | 111 | if (!plaintext) { |
72 | if (!unencrypted) { | ||
73 | /* EOF (such as ^D) or error (such as ^C) or timeout */ | 112 | /* EOF (such as ^D) or error (such as ^C) or timeout */ |
74 | return -1; | 113 | return -1; |
75 | } | 114 | } |
76 | encrypted = pw_encrypt(unencrypted, correct, 1); | 115 | |
77 | r = (strcmp(encrypted, correct) == 0); | 116 | r = check_password(pw, plaintext); |
78 | free(encrypted); | 117 | nuke_str(plaintext); |
79 | nuke_str(unencrypted); | ||
80 | return r; | 118 | return r; |
81 | } | 119 | } |
82 | 120 | ||
diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c index 3f743ac75..1f63ccdee 100644 --- a/libbb/hash_md5_sha.c +++ b/libbb/hash_md5_sha.c | |||
@@ -926,10 +926,81 @@ 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 OPTIMIZE_SHA3_FOR_32 0 | ||
930 | /* | ||
931 | * SHA3 can be optimized for 32-bit CPUs with bit-slicing: | ||
932 | * every 64-bit word of state[] can be split into two 32-bit words | ||
933 | * by even/odd bits. In this form, all rotations of sha3 round | ||
934 | * are 32-bit - and there are lots of them. | ||
935 | * However, it requires either splitting/combining state words | ||
936 | * before/after sha3 round (code does this now) | ||
937 | * or shuffling bits before xor'ing them into state and in sha3_end. | ||
938 | * Without shuffling, bit-slicing results in -130 bytes of code | ||
939 | * and marginal speedup (but of course it gives wrong result). | ||
940 | * With shuffling it works, but +260 code bytes, and slower. | ||
941 | * Disabled for now: | ||
942 | */ | ||
943 | #if 0 /* LONG_MAX == 0x7fffffff */ | ||
944 | # undef OPTIMIZE_SHA3_FOR_32 | ||
945 | # define OPTIMIZE_SHA3_FOR_32 1 | ||
946 | #endif | ||
947 | |||
929 | enum { | 948 | enum { |
930 | SHA3_IBLK_BYTES = 72, /* 576 bits / 8 */ | 949 | SHA3_IBLK_BYTES = 72, /* 576 bits / 8 */ |
931 | }; | 950 | }; |
932 | 951 | ||
952 | #if OPTIMIZE_SHA3_FOR_32 | ||
953 | /* This splits every 64-bit word into a pair of 32-bit words, | ||
954 | * even bits go into first word, odd bits go to second one. | ||
955 | * The conversion is done in-place. | ||
956 | */ | ||
957 | static void split_halves(uint64_t *state) | ||
958 | { | ||
959 | /* Credit: Henry S. Warren, Hacker's Delight, Addison-Wesley, 2002 */ | ||
960 | uint32_t *s32 = (uint32_t*)state; | ||
961 | uint32_t t, x0, x1; | ||
962 | int i; | ||
963 | for (i = 24; i >= 0; --i) { | ||
964 | x0 = s32[0]; | ||
965 | t = (x0 ^ (x0 >> 1)) & 0x22222222; x0 = x0 ^ t ^ (t << 1); | ||
966 | t = (x0 ^ (x0 >> 2)) & 0x0C0C0C0C; x0 = x0 ^ t ^ (t << 2); | ||
967 | t = (x0 ^ (x0 >> 4)) & 0x00F000F0; x0 = x0 ^ t ^ (t << 4); | ||
968 | t = (x0 ^ (x0 >> 8)) & 0x0000FF00; x0 = x0 ^ t ^ (t << 8); | ||
969 | x1 = s32[1]; | ||
970 | t = (x1 ^ (x1 >> 1)) & 0x22222222; x1 = x1 ^ t ^ (t << 1); | ||
971 | t = (x1 ^ (x1 >> 2)) & 0x0C0C0C0C; x1 = x1 ^ t ^ (t << 2); | ||
972 | t = (x1 ^ (x1 >> 4)) & 0x00F000F0; x1 = x1 ^ t ^ (t << 4); | ||
973 | t = (x1 ^ (x1 >> 8)) & 0x0000FF00; x1 = x1 ^ t ^ (t << 8); | ||
974 | *s32++ = (x0 & 0x0000FFFF) | (x1 << 16); | ||
975 | *s32++ = (x0 >> 16) | (x1 & 0xFFFF0000); | ||
976 | } | ||
977 | } | ||
978 | /* The reverse operation */ | ||
979 | static void combine_halves(uint64_t *state) | ||
980 | { | ||
981 | uint32_t *s32 = (uint32_t*)state; | ||
982 | uint32_t t, x0, x1; | ||
983 | int i; | ||
984 | for (i = 24; i >= 0; --i) { | ||
985 | x0 = s32[0]; | ||
986 | x1 = s32[1]; | ||
987 | t = (x0 & 0x0000FFFF) | (x1 << 16); | ||
988 | x1 = (x0 >> 16) | (x1 & 0xFFFF0000); | ||
989 | x0 = t; | ||
990 | t = (x0 ^ (x0 >> 8)) & 0x0000FF00; x0 = x0 ^ t ^ (t << 8); | ||
991 | t = (x0 ^ (x0 >> 4)) & 0x00F000F0; x0 = x0 ^ t ^ (t << 4); | ||
992 | t = (x0 ^ (x0 >> 2)) & 0x0C0C0C0C; x0 = x0 ^ t ^ (t << 2); | ||
993 | t = (x0 ^ (x0 >> 1)) & 0x22222222; x0 = x0 ^ t ^ (t << 1); | ||
994 | *s32++ = x0; | ||
995 | t = (x1 ^ (x1 >> 8)) & 0x0000FF00; x1 = x1 ^ t ^ (t << 8); | ||
996 | t = (x1 ^ (x1 >> 4)) & 0x00F000F0; x1 = x1 ^ t ^ (t << 4); | ||
997 | t = (x1 ^ (x1 >> 2)) & 0x0C0C0C0C; x1 = x1 ^ t ^ (t << 2); | ||
998 | t = (x1 ^ (x1 >> 1)) & 0x22222222; x1 = x1 ^ t ^ (t << 1); | ||
999 | *s32++ = x1; | ||
1000 | } | ||
1001 | } | ||
1002 | #endif | ||
1003 | |||
933 | /* | 1004 | /* |
934 | * In the crypto literature this function is usually called Keccak-f(). | 1005 | * In the crypto literature this function is usually called Keccak-f(). |
935 | */ | 1006 | */ |
@@ -937,12 +1008,179 @@ static void sha3_process_block72(uint64_t *state) | |||
937 | { | 1008 | { |
938 | enum { NROUNDS = 24 }; | 1009 | enum { NROUNDS = 24 }; |
939 | 1010 | ||
940 | /* Elements should be 64-bit, but top half is always zero or 0x80000000. | 1011 | #if OPTIMIZE_SHA3_FOR_32 |
941 | * We encode 63rd bits in a separate word below. | 1012 | /* |
942 | * Same is true for 31th bits, which lets us use 16-bit table instead of 64-bit. | 1013 | static const uint32_t IOTA_CONST_0[NROUNDS] = { |
943 | * The speed penalty is lost in the noise. | 1014 | 0x00000001UL, |
944 | */ | 1015 | 0x00000000UL, |
1016 | 0x00000000UL, | ||
1017 | 0x00000000UL, | ||
1018 | 0x00000001UL, | ||
1019 | 0x00000001UL, | ||
1020 | 0x00000001UL, | ||
1021 | 0x00000001UL, | ||
1022 | 0x00000000UL, | ||
1023 | 0x00000000UL, | ||
1024 | 0x00000001UL, | ||
1025 | 0x00000000UL, | ||
1026 | 0x00000001UL, | ||
1027 | 0x00000001UL, | ||
1028 | 0x00000001UL, | ||
1029 | 0x00000001UL, | ||
1030 | 0x00000000UL, | ||
1031 | 0x00000000UL, | ||
1032 | 0x00000000UL, | ||
1033 | 0x00000000UL, | ||
1034 | 0x00000001UL, | ||
1035 | 0x00000000UL, | ||
1036 | 0x00000001UL, | ||
1037 | 0x00000000UL, | ||
1038 | }; | ||
1039 | ** bits are in lsb: 0101 0000 1111 0100 1111 0001 | ||
1040 | */ | ||
1041 | uint32_t IOTA_CONST_0bits = (uint32_t)(0x0050f4f1); | ||
1042 | static const uint32_t IOTA_CONST_1[NROUNDS] = { | ||
1043 | 0x00000000UL, | ||
1044 | 0x00000089UL, | ||
1045 | 0x8000008bUL, | ||
1046 | 0x80008080UL, | ||
1047 | 0x0000008bUL, | ||
1048 | 0x00008000UL, | ||
1049 | 0x80008088UL, | ||
1050 | 0x80000082UL, | ||
1051 | 0x0000000bUL, | ||
1052 | 0x0000000aUL, | ||
1053 | 0x00008082UL, | ||
1054 | 0x00008003UL, | ||
1055 | 0x0000808bUL, | ||
1056 | 0x8000000bUL, | ||
1057 | 0x8000008aUL, | ||
1058 | 0x80000081UL, | ||
1059 | 0x80000081UL, | ||
1060 | 0x80000008UL, | ||
1061 | 0x00000083UL, | ||
1062 | 0x80008003UL, | ||
1063 | 0x80008088UL, | ||
1064 | 0x80000088UL, | ||
1065 | 0x00008000UL, | ||
1066 | 0x80008082UL, | ||
1067 | }; | ||
1068 | |||
1069 | uint32_t *const s32 = (uint32_t*)state; | ||
1070 | unsigned round; | ||
1071 | |||
1072 | split_halves(state); | ||
1073 | |||
1074 | for (round = 0; round < NROUNDS; round++) { | ||
1075 | unsigned x; | ||
1076 | |||
1077 | /* Theta */ | ||
1078 | { | ||
1079 | uint32_t BC[20]; | ||
1080 | for (x = 0; x < 10; ++x) { | ||
1081 | BC[x+10] = BC[x] = s32[x]^s32[x+10]^s32[x+20]^s32[x+30]^s32[x+40]; | ||
1082 | } | ||
1083 | for (x = 0; x < 10; x += 2) { | ||
1084 | uint32_t ta, tb; | ||
1085 | ta = BC[x+8] ^ rotl32(BC[x+3], 1); | ||
1086 | tb = BC[x+9] ^ BC[x+2]; | ||
1087 | s32[x+0] ^= ta; | ||
1088 | s32[x+1] ^= tb; | ||
1089 | s32[x+10] ^= ta; | ||
1090 | s32[x+11] ^= tb; | ||
1091 | s32[x+20] ^= ta; | ||
1092 | s32[x+21] ^= tb; | ||
1093 | s32[x+30] ^= ta; | ||
1094 | s32[x+31] ^= tb; | ||
1095 | s32[x+40] ^= ta; | ||
1096 | s32[x+41] ^= tb; | ||
1097 | } | ||
1098 | } | ||
1099 | /* RhoPi */ | ||
1100 | { | ||
1101 | uint32_t t0a,t0b, t1a,t1b; | ||
1102 | t1a = s32[1*2+0]; | ||
1103 | t1b = s32[1*2+1]; | ||
1104 | |||
1105 | #define RhoPi(PI_LANE, ROT_CONST) \ | ||
1106 | t0a = s32[PI_LANE*2+0];\ | ||
1107 | t0b = s32[PI_LANE*2+1];\ | ||
1108 | if (ROT_CONST & 1) {\ | ||
1109 | s32[PI_LANE*2+0] = rotl32(t1b, ROT_CONST/2+1);\ | ||
1110 | s32[PI_LANE*2+1] = ROT_CONST == 1 ? t1a : rotl32(t1a, ROT_CONST/2+0);\ | ||
1111 | } else {\ | ||
1112 | s32[PI_LANE*2+0] = rotl32(t1a, ROT_CONST/2);\ | ||
1113 | s32[PI_LANE*2+1] = rotl32(t1b, ROT_CONST/2);\ | ||
1114 | }\ | ||
1115 | t1a = t0a; t1b = t0b; | ||
1116 | |||
1117 | RhoPi(10, 1) | ||
1118 | RhoPi( 7, 3) | ||
1119 | RhoPi(11, 6) | ||
1120 | RhoPi(17,10) | ||
1121 | RhoPi(18,15) | ||
1122 | RhoPi( 3,21) | ||
1123 | RhoPi( 5,28) | ||
1124 | RhoPi(16,36) | ||
1125 | RhoPi( 8,45) | ||
1126 | RhoPi(21,55) | ||
1127 | RhoPi(24, 2) | ||
1128 | RhoPi( 4,14) | ||
1129 | RhoPi(15,27) | ||
1130 | RhoPi(23,41) | ||
1131 | RhoPi(19,56) | ||
1132 | RhoPi(13, 8) | ||
1133 | RhoPi(12,25) | ||
1134 | RhoPi( 2,43) | ||
1135 | RhoPi(20,62) | ||
1136 | RhoPi(14,18) | ||
1137 | RhoPi(22,39) | ||
1138 | RhoPi( 9,61) | ||
1139 | RhoPi( 6,20) | ||
1140 | RhoPi( 1,44) | ||
1141 | #undef RhoPi | ||
1142 | } | ||
1143 | /* Chi */ | ||
1144 | for (x = 0; x <= 40;) { | ||
1145 | uint32_t BC0, BC1, BC2, BC3, BC4; | ||
1146 | BC0 = s32[x + 0*2]; | ||
1147 | BC1 = s32[x + 1*2]; | ||
1148 | BC2 = s32[x + 2*2]; | ||
1149 | s32[x + 0*2] = BC0 ^ ((~BC1) & BC2); | ||
1150 | BC3 = s32[x + 3*2]; | ||
1151 | s32[x + 1*2] = BC1 ^ ((~BC2) & BC3); | ||
1152 | BC4 = s32[x + 4*2]; | ||
1153 | s32[x + 2*2] = BC2 ^ ((~BC3) & BC4); | ||
1154 | s32[x + 3*2] = BC3 ^ ((~BC4) & BC0); | ||
1155 | s32[x + 4*2] = BC4 ^ ((~BC0) & BC1); | ||
1156 | x++; | ||
1157 | BC0 = s32[x + 0*2]; | ||
1158 | BC1 = s32[x + 1*2]; | ||
1159 | BC2 = s32[x + 2*2]; | ||
1160 | s32[x + 0*2] = BC0 ^ ((~BC1) & BC2); | ||
1161 | BC3 = s32[x + 3*2]; | ||
1162 | s32[x + 1*2] = BC1 ^ ((~BC2) & BC3); | ||
1163 | BC4 = s32[x + 4*2]; | ||
1164 | s32[x + 2*2] = BC2 ^ ((~BC3) & BC4); | ||
1165 | s32[x + 3*2] = BC3 ^ ((~BC4) & BC0); | ||
1166 | s32[x + 4*2] = BC4 ^ ((~BC0) & BC1); | ||
1167 | x += 9; | ||
1168 | } | ||
1169 | /* Iota */ | ||
1170 | s32[0] ^= IOTA_CONST_0bits & 1; | ||
1171 | IOTA_CONST_0bits >>= 1; | ||
1172 | s32[1] ^= IOTA_CONST_1[round]; | ||
1173 | } | ||
1174 | |||
1175 | combine_halves(state); | ||
1176 | #else | ||
1177 | /* Native 64-bit algorithm */ | ||
945 | static const uint16_t IOTA_CONST[NROUNDS] = { | 1178 | static const uint16_t IOTA_CONST[NROUNDS] = { |
1179 | /* Elements should be 64-bit, but top half is always zero | ||
1180 | * or 0x80000000. We encode 63rd bits in a separate word below. | ||
1181 | * Same is true for 31th bits, which lets us use 16-bit table | ||
1182 | * instead of 64-bit. The speed penalty is lost in the noise. | ||
1183 | */ | ||
946 | 0x0001, | 1184 | 0x0001, |
947 | 0x8082, | 1185 | 0x8082, |
948 | 0x808a, | 1186 | 0x808a, |
@@ -983,7 +1221,7 @@ static void sha3_process_block72(uint64_t *state) | |||
983 | }; | 1221 | }; |
984 | /*static const uint8_t MOD5[10] = { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, };*/ | 1222 | /*static const uint8_t MOD5[10] = { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, };*/ |
985 | 1223 | ||
986 | unsigned x, y; | 1224 | unsigned x; |
987 | unsigned round; | 1225 | unsigned round; |
988 | 1226 | ||
989 | if (BB_BIG_ENDIAN) { | 1227 | if (BB_BIG_ENDIAN) { |
@@ -1045,22 +1283,62 @@ static void sha3_process_block72(uint64_t *state) | |||
1045 | RhoPi_twice(20); RhoPi_twice(22); | 1283 | RhoPi_twice(20); RhoPi_twice(22); |
1046 | #undef RhoPi_twice | 1284 | #undef RhoPi_twice |
1047 | } | 1285 | } |
1048 | |||
1049 | /* Chi */ | 1286 | /* Chi */ |
1050 | for (y = 0; y <= 20; y += 5) { | 1287 | # if LONG_MAX > 0x7fffffff |
1288 | for (x = 0; x <= 20; x += 5) { | ||
1051 | uint64_t BC0, BC1, BC2, BC3, BC4; | 1289 | uint64_t BC0, BC1, BC2, BC3, BC4; |
1052 | BC0 = state[y + 0]; | 1290 | BC0 = state[x + 0]; |
1053 | BC1 = state[y + 1]; | 1291 | BC1 = state[x + 1]; |
1054 | BC2 = state[y + 2]; | 1292 | BC2 = state[x + 2]; |
1055 | state[y + 0] = BC0 ^ ((~BC1) & BC2); | 1293 | state[x + 0] = BC0 ^ ((~BC1) & BC2); |
1056 | BC3 = state[y + 3]; | 1294 | BC3 = state[x + 3]; |
1057 | state[y + 1] = BC1 ^ ((~BC2) & BC3); | 1295 | state[x + 1] = BC1 ^ ((~BC2) & BC3); |
1058 | BC4 = state[y + 4]; | 1296 | BC4 = state[x + 4]; |
1059 | state[y + 2] = BC2 ^ ((~BC3) & BC4); | 1297 | state[x + 2] = BC2 ^ ((~BC3) & BC4); |
1060 | state[y + 3] = BC3 ^ ((~BC4) & BC0); | 1298 | state[x + 3] = BC3 ^ ((~BC4) & BC0); |
1061 | state[y + 4] = BC4 ^ ((~BC0) & BC1); | 1299 | state[x + 4] = BC4 ^ ((~BC0) & BC1); |
1062 | } | 1300 | } |
1063 | 1301 | # else | |
1302 | /* Reduced register pressure version | ||
1303 | * for register-starved 32-bit arches | ||
1304 | * (i386: -95 bytes, and it is _faster_) | ||
1305 | */ | ||
1306 | for (x = 0; x <= 40;) { | ||
1307 | uint32_t BC0, BC1, BC2, BC3, BC4; | ||
1308 | uint32_t *const s32 = (uint32_t*)state; | ||
1309 | # if SHA3_SMALL | ||
1310 | do_half: | ||
1311 | # endif | ||
1312 | BC0 = s32[x + 0*2]; | ||
1313 | BC1 = s32[x + 1*2]; | ||
1314 | BC2 = s32[x + 2*2]; | ||
1315 | s32[x + 0*2] = BC0 ^ ((~BC1) & BC2); | ||
1316 | BC3 = s32[x + 3*2]; | ||
1317 | s32[x + 1*2] = BC1 ^ ((~BC2) & BC3); | ||
1318 | BC4 = s32[x + 4*2]; | ||
1319 | s32[x + 2*2] = BC2 ^ ((~BC3) & BC4); | ||
1320 | s32[x + 3*2] = BC3 ^ ((~BC4) & BC0); | ||
1321 | s32[x + 4*2] = BC4 ^ ((~BC0) & BC1); | ||
1322 | x++; | ||
1323 | # if SHA3_SMALL | ||
1324 | if (x & 1) | ||
1325 | goto do_half; | ||
1326 | x += 8; | ||
1327 | # else | ||
1328 | BC0 = s32[x + 0*2]; | ||
1329 | BC1 = s32[x + 1*2]; | ||
1330 | BC2 = s32[x + 2*2]; | ||
1331 | s32[x + 0*2] = BC0 ^ ((~BC1) & BC2); | ||
1332 | BC3 = s32[x + 3*2]; | ||
1333 | s32[x + 1*2] = BC1 ^ ((~BC2) & BC3); | ||
1334 | BC4 = s32[x + 4*2]; | ||
1335 | s32[x + 2*2] = BC2 ^ ((~BC3) & BC4); | ||
1336 | s32[x + 3*2] = BC3 ^ ((~BC4) & BC0); | ||
1337 | s32[x + 4*2] = BC4 ^ ((~BC0) & BC1); | ||
1338 | x += 9; | ||
1339 | # endif | ||
1340 | } | ||
1341 | # endif /* long is 32-bit */ | ||
1064 | /* Iota */ | 1342 | /* Iota */ |
1065 | state[0] ^= IOTA_CONST[round] | 1343 | state[0] ^= IOTA_CONST[round] |
1066 | | (uint32_t)((IOTA_CONST_bit31 << round) & 0x80000000) | 1344 | | (uint32_t)((IOTA_CONST_bit31 << round) & 0x80000000) |
@@ -1072,6 +1350,7 @@ static void sha3_process_block72(uint64_t *state) | |||
1072 | state[x] = SWAP_LE64(state[x]); | 1350 | state[x] = SWAP_LE64(state[x]); |
1073 | } | 1351 | } |
1074 | } | 1352 | } |
1353 | #endif | ||
1075 | } | 1354 | } |
1076 | 1355 | ||
1077 | void FAST_FUNC sha3_begin(sha3_ctx_t *ctx) | 1356 | void FAST_FUNC sha3_begin(sha3_ctx_t *ctx) |
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 23f27516f..f25ce9446 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -25,20 +25,22 @@ | |||
25 | #include "libbb.h" | 25 | #include "libbb.h" |
26 | 26 | ||
27 | /* Turn on nonblocking I/O on a fd */ | 27 | /* Turn on nonblocking I/O on a fd */ |
28 | void FAST_FUNC ndelay_on(int fd) | 28 | int FAST_FUNC ndelay_on(int fd) |
29 | { | 29 | { |
30 | int flags = fcntl(fd, F_GETFL); | 30 | int flags = fcntl(fd, F_GETFL); |
31 | if (flags & O_NONBLOCK) | 31 | if (flags & O_NONBLOCK) |
32 | return; | 32 | return flags; |
33 | fcntl(fd, F_SETFL, flags | O_NONBLOCK); | 33 | fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
34 | return flags; | ||
34 | } | 35 | } |
35 | 36 | ||
36 | void FAST_FUNC ndelay_off(int fd) | 37 | int FAST_FUNC ndelay_off(int fd) |
37 | { | 38 | { |
38 | int flags = fcntl(fd, F_GETFL); | 39 | int flags = fcntl(fd, F_GETFL); |
39 | if (!(flags & O_NONBLOCK)) | 40 | if (!(flags & O_NONBLOCK)) |
40 | return; | 41 | return flags; |
41 | fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); | 42 | fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); |
43 | return flags; | ||
42 | } | 44 | } |
43 | 45 | ||
44 | void FAST_FUNC close_on_exec_on(int fd) | 46 | void FAST_FUNC close_on_exec_on(int fd) |