aboutsummaryrefslogtreecommitdiff
path: root/libbb/pw_encrypt_sha.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-11-13 13:25:18 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-11-13 13:25:18 +0000
commit3470f9297ef179dd327031351a2ac1feccce32f6 (patch)
treeb2b6b6974f150ce9c9b2824f43defca8f66a1c6f /libbb/pw_encrypt_sha.c
parent6bd54d48f44b65464dd637add13292ca8189e1ef (diff)
downloadbusybox-w32-3470f9297ef179dd327031351a2ac1feccce32f6.tar.gz
busybox-w32-3470f9297ef179dd327031351a2ac1feccce32f6.tar.bz2
busybox-w32-3470f9297ef179dd327031351a2ac1feccce32f6.zip
libbb: sha_crypt -123 bytes
Diffstat (limited to 'libbb/pw_encrypt_sha.c')
-rw-r--r--libbb/pw_encrypt_sha.c64
1 files changed, 34 insertions, 30 deletions
diff --git a/libbb/pw_encrypt_sha.c b/libbb/pw_encrypt_sha.c
index 3dbaeabc9..3173506f5 100644
--- a/libbb/pw_encrypt_sha.c
+++ b/libbb/pw_encrypt_sha.c
@@ -26,16 +26,22 @@ sha_crypt(/*const*/ char *key_data, /*const*/ char *salt_data)
26 char *result, *resptr; 26 char *result, *resptr;
27 27
28 /* btw, sha256 needs [32] and uint32_t only */ 28 /* btw, sha256 needs [32] and uint32_t only */
29 unsigned char alt_result[64] __attribute__((__aligned__(__alignof__(uint64_t)))); 29 struct {
30 unsigned char temp_result[64] __attribute__((__aligned__(__alignof__(uint64_t)))); 30 unsigned char alt_result[64];
31 union { 31 unsigned char temp_result[64];
32 sha256_ctx_t x; 32 union {
33 sha512_ctx_t y; 33 sha256_ctx_t x;
34 } ctx; 34 sha512_ctx_t y;
35 union { 35 } ctx;
36 sha256_ctx_t x; 36 union {
37 sha512_ctx_t y; 37 sha256_ctx_t x;
38 } alt_ctx; 38 sha512_ctx_t y;
39 } alt_ctx;
40 } L __attribute__((__aligned__(__alignof__(uint64_t))));
41#define alt_result (L.alt_result )
42#define temp_result (L.temp_result)
43#define ctx (L.ctx )
44#define alt_ctx (L.alt_ctx )
39 unsigned salt_len; 45 unsigned salt_len;
40 unsigned key_len; 46 unsigned key_len;
41 unsigned cnt; 47 unsigned cnt;
@@ -186,7 +192,6 @@ sha_crypt(/*const*/ char *key_data, /*const*/ char *salt_data)
186 sha_end(alt_result, &ctx); 192 sha_end(alt_result, &ctx);
187 } 193 }
188 194
189
190 /* Append encrypted password to result buffer */ 195 /* Append encrypted password to result buffer */
191//TODO: replace with something like 196//TODO: replace with something like
192// bb_uuencode(cp, src, length, bb_uuenc_tbl_XXXbase64); 197// bb_uuencode(cp, src, length, bb_uuenc_tbl_XXXbase64);
@@ -196,18 +201,16 @@ do { \
196 resptr = to64(resptr, w, N); \ 201 resptr = to64(resptr, w, N); \
197} while (0) 202} while (0)
198 if (is_sha512 == '5') { 203 if (is_sha512 == '5') {
199 int i = 0; 204 unsigned i = 0;
200 int j = 10;
201 int k = 20;
202 while (1) { 205 while (1) {
206 unsigned j = i + 10;
207 unsigned k = i + 20;
208 if (j >= 30) j -= 30;
209 if (k >= 30) k -= 30;
203 b64_from_24bit(alt_result[i], alt_result[j], alt_result[k], 4); 210 b64_from_24bit(alt_result[i], alt_result[j], alt_result[k], 4);
204 if (i == 9) 211 if (k == 29)
205 break; 212 break;
206 /* if x - 9 produces < 0, subtract 2 more: 213 i = k + 1;
207 * ((i >> 8) << 1) is either 0 or binary 111111...1110 */
208 i -= 9; i = (i & 0x1f) + ((i >> 8) << 1);
209 j -= 9; j = (j & 0x1f) + ((j >> 8) << 1);
210 k -= 9; k = (k & 0x1f) + ((k >> 8) << 1);
211 } 214 }
212 b64_from_24bit(0, alt_result[31], alt_result[30], 3); 215 b64_from_24bit(0, alt_result[31], alt_result[30], 3);
213 /* was: 216 /* was:
@@ -225,15 +228,15 @@ do { \
225 */ 228 */
226 } else { 229 } else {
227 unsigned i = 0; 230 unsigned i = 0;
228 unsigned j = 21;
229 unsigned k = 42;
230 while (1) { 231 while (1) {
232 unsigned j = i + 21;
233 unsigned k = i + 42;
234 if (j >= 63) j -= 63;
235 if (k >= 63) k -= 63;
231 b64_from_24bit(alt_result[i], alt_result[j], alt_result[k], 4); 236 b64_from_24bit(alt_result[i], alt_result[j], alt_result[k], 4);
232 if (i == 62) 237 if (j == 20)
233 break; 238 break;
234 i += 22; i = ((i >> 6) + i) & 0x3f; 239 i = j + 1;
235 j += 22; j = ((j >> 6) + j) & 0x3f;
236 k += 22; k = ((k >> 6) + k) & 0x3f;
237 } 240 }
238 b64_from_24bit(0, 0, alt_result[63], 2); 241 b64_from_24bit(0, 0, alt_result[63], 2);
239 /* was: 242 /* was:
@@ -267,10 +270,7 @@ do { \
267 /* Clear the buffer for the intermediate result so that people 270 /* Clear the buffer for the intermediate result so that people
268 attaching to processes or reading core dumps cannot get any 271 attaching to processes or reading core dumps cannot get any
269 information. */ 272 information. */
270 memset(temp_result, 0, sizeof(temp_result)); 273 memset(&L, 0, sizeof(L)); /* [alt]_ctx and XXX_result buffers */
271 memset(alt_result, 0, sizeof(alt_result));
272 memset(&ctx, 0, sizeof(ctx));
273 memset(&alt_ctx, 0, sizeof(alt_ctx));
274 memset(key_data, 0, key_len); /* also p_bytes */ 274 memset(key_data, 0, key_len); /* also p_bytes */
275 memset(salt_data, 0, salt_len); /* also s_bytes */ 275 memset(salt_data, 0, salt_len); /* also s_bytes */
276 free(key_data); 276 free(key_data);
@@ -279,4 +279,8 @@ do { \
279#undef s_bytes 279#undef s_bytes
280 280
281 return result; 281 return result;
282#undef alt_result
283#undef temp_result
284#undef ctx
285#undef alt_ctx
282} 286}