| 1 |
|
| /* |
| 2 |
|
| * ---------------------------------------------------------------------------- |
| 3 |
|
| * "THE BEER-WARE LICENSE" (Revision 42): |
| 4 |
|
| * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you |
| 5 |
|
| * can do whatever you want with this stuff. If we meet some day, and you think |
| 6 |
|
| * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp |
| 7 |
|
| * ---------------------------------------------------------------------------- |
| 8 |
|
| * |
| 9 |
|
| * $FreeBSD: src/lib/libcrypt/crypt-md5.c,v 1.5.2.1 2001/05/24 12:20:02 markm Exp $ |
| 10 |
|
| * $DragonFly: src/lib/libcrypt/crypt-md5.c,v 1.3 2005/08/04 17:27:09 drhodus Exp $ |
| 11 |
|
| */ |
| 12 |
|
| |
| 13 |
|
| #include <unistd.h> |
| 14 |
|
| #include <stdio.h> |
| 15 |
|
| #include <string.h> |
| 16 |
|
| #include "md5.h" |
| 17 |
|
| #include "crypt_to64.h" |
| 18 |
|
| |
| 19 |
|
| /* |
| 20 |
|
| * UNIX password |
| 21 |
|
| */ |
| 22 |
|
| |
| 23 |
|
| #define MD5_SIZE 16 |
| 24 |
|
| |
| 25 |
|
| char * |
| 26 |
17 |
| _cpx_crypt_md5(const char *pw, const char *salt, const char* magic) |
| 27 |
80 |
| { |
| 28 |
|
| static char passwd[120], *p; |
| 29 |
|
| static const char *sp,*ep; |
| 30 |
|
| unsigned char final[MD5_SIZE]; |
| 31 |
|
| int sl,pl,i; |
| 32 |
|
| md5_state_t ctx,ctx1; |
| 33 |
|
| unsigned long l; |
| 34 |
|
| |
| 35 |
|
| /* Refine the Salt first */ |
| 36 |
17 |
| sp = salt; |
| 37 |
|
| |
| 38 |
|
| /* If it starts with the magic string, then skip that */ |
| 39 |
17 |
▶ | if(!strncmp(sp,magic,strlen(magic))) |
Branch: 1 branch
| Branch | True | False | Total |
| if(!strncmp(sp,magic,strlen(magic))) |
5 |
12 |
17 |
|
| 40 |
5 |
| sp += strlen(magic); |
| 41 |
|
| |
| 42 |
|
| /* It stops at the first '$', max 8 chars */ |
| 43 |
97 |
▶ | for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++) |
Branch: 3 branches
| Branch | True | False | Total |
| for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++) |
83 |
14 |
97 |
| for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++) |
80 |
3 |
83 |
| for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++) |
80 |
0 |
80 |
|
| 44 |
80 |
| continue; |
| 45 |
|
| |
| 46 |
|
| /* get the length of the true salt */ |
| 47 |
17 |
| sl = ep - sp; |
| 48 |
|
| |
| 49 |
17 |
| md5_init(&ctx); |
| 50 |
|
| |
| 51 |
|
| /* The password first, since that is what is most unknown */ |
| 52 |
17 |
| md5_append(&ctx,pw,strlen(pw)); |
| 53 |
|
| |
| 54 |
|
| /* Then our magic string */ |
| 55 |
17 |
| md5_append(&ctx,magic,strlen(magic)); |
| 56 |
|
| |
| 57 |
|
| /* Then the raw salt */ |
| 58 |
17 |
| md5_append(&ctx,sp,sl); |
| 59 |
|
| |
| 60 |
|
| /* Then just as many characters of the MD5(pw,salt,pw) */ |
| 61 |
17 |
| md5_init(&ctx1); |
| 62 |
17 |
| md5_append(&ctx1,pw,strlen(pw)); |
| 63 |
17 |
| md5_append(&ctx1,sp,sl); |
| 64 |
17 |
| md5_append(&ctx1,pw,strlen(pw)); |
| 65 |
17 |
| md5_finish(&ctx1, final); |
| 66 |
28 |
▶ | for(pl = strlen(pw); pl > 0; pl -= MD5_SIZE) |
Branch: 1 branch
| Branch | True | False | Total |
| for(pl = strlen(pw); pl > 0; pl -= MD5_SIZE) |
11 |
17 |
28 |
|
| 67 |
11 |
| md5_append(&ctx,final,pl>MD5_SIZE ? MD5_SIZE : pl); |
| 68 |
|
| |
| 69 |
|
| /* Don't leave anything around in vm they could use. */ |
| 70 |
17 |
| memset(final,0,sizeof final); |
| 71 |
|
| |
| 72 |
|
| /* Then something really weird... */ |
| 73 |
61 |
▶ | for (i = strlen(pw); i ; i >>= 1) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = strlen(pw); i ; i >>= 1) |
44 |
17 |
61 |
|
| 74 |
44 |
▶ | if(i&1) |
Branch: 1 branch
| Branch | True | False | Total |
| if(i&1) |
11 |
33 |
44 |
|
| 75 |
11 |
| md5_append(&ctx, final, 1); |
| 76 |
|
| else |
| 77 |
33 |
| md5_append(&ctx, pw, 1); |
| 78 |
|
| |
| 79 |
|
| /* Now make the output string */ |
| 80 |
17 |
| strcpy(passwd,magic); |
| 81 |
17 |
| strncat(passwd,sp,sl); |
| 82 |
17 |
| strcat(passwd,"$"); |
| 83 |
|
| |
| 84 |
17 |
| md5_finish(&ctx,final); |
| 85 |
|
| |
| 86 |
|
| /* |
| 87 |
|
| * and now, just to make sure things don't run too fast |
| 88 |
|
| * On a 60 Mhz Pentium this takes 34 msec, so you would |
| 89 |
|
| * need 30 seconds to build a 1000 entry dictionary... |
| 90 |
|
| */ |
| 91 |
17017 |
▶ | for(i=0;i<1000;i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<1000;i++) { |
17000 |
17 |
17017 |
|
| 92 |
17000 |
| md5_init(&ctx1); |
| 93 |
17000 |
▶ | if(i & 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if(i & 1) |
8500 |
8500 |
17000 |
|
| 94 |
8500 |
| md5_append(&ctx1,pw,strlen(pw)); |
| 95 |
|
| else |
| 96 |
8500 |
| md5_append(&ctx1,final,MD5_SIZE); |
| 97 |
|
| |
| 98 |
17000 |
▶ | if(i % 3) |
Branch: 1 branch
| Branch | True | False | Total |
| if(i % 3) |
11322 |
5678 |
17000 |
|
| 99 |
11322 |
| md5_append(&ctx1,sp,sl); |
| 100 |
|
| |
| 101 |
17000 |
▶ | if(i % 7) |
Branch: 1 branch
| Branch | True | False | Total |
| if(i % 7) |
14569 |
2431 |
17000 |
|
| 102 |
14569 |
| md5_append(&ctx1,pw,strlen(pw)); |
| 103 |
|
| |
| 104 |
17000 |
▶ | if(i & 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if(i & 1) |
8500 |
8500 |
17000 |
|
| 105 |
8500 |
| md5_append(&ctx1,final,MD5_SIZE); |
| 106 |
|
| else |
| 107 |
8500 |
| md5_append(&ctx1,pw,strlen(pw)); |
| 108 |
17000 |
| md5_finish(&ctx1,final); |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
17 |
| p = passwd + strlen(passwd); |
| 112 |
|
| |
| 113 |
17 |
| l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; |
| 114 |
17 |
| _crypt_to64(p,l,4); p += 4; |
| 115 |
17 |
| l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; |
| 116 |
17 |
| _crypt_to64(p,l,4); p += 4; |
| 117 |
17 |
| l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; |
| 118 |
17 |
| _crypt_to64(p,l,4); p += 4; |
| 119 |
17 |
| l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; |
| 120 |
17 |
| _crypt_to64(p,l,4); p += 4; |
| 121 |
17 |
| l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; |
| 122 |
17 |
| _crypt_to64(p,l,4); p += 4; |
| 123 |
17 |
| l = final[11] ; |
| 124 |
17 |
| _crypt_to64(p,l,2); p += 2; |
| 125 |
17 |
| *p = '\0'; |
| 126 |
|
| |
| 127 |
|
| /* Don't leave anything around in vm they could use. */ |
| 128 |
17 |
| memset(final,0,sizeof final); |
| 129 |
|
| |
| 130 |
17 |
| return passwd; |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
|
| /* For APR1 and MD5 crypt, the sole difference is the prefix string. */ |
| 134 |
|
| |
| 135 |
|
| static const char* md5_magic = "$1$"; |
| 136 |
|
| static const char* apr1_magic= "$apr1$"; |
| 137 |
|
| |
| 138 |
|
| char * |
| 139 |
8 |
| cpx_crypt_apr1(const char *pw, const char *salt) |
| 140 |
|
| { |
| 141 |
8 |
| return _cpx_crypt_md5(pw, salt, apr1_magic); |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
|
| char * |
| 145 |
9 |
| cpx_crypt_md5(const char *pw, const char *salt) |
| 146 |
|
| { |
| 147 |
9 |
| return _cpx_crypt_md5(pw, salt, md5_magic); |
| 148 |
|
| } |
| 149 |
|
| |