1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/* $Id: password.C,v 1.6 2002/09/28 21:28:15 max Exp $ */
/*
*
* Copyright (C) 1999 David Mazieres (dm@uun.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*/
#include "crypt.h"
#include "password.h"
#include "rxx.h"
#include "parseopt.h"
inline void
hashptext (char *dst, size_t dstlen, const str &src)
{
sha1oracle ora (dstlen);
ora.update (src.cstr (), src.len ());
ora.final (reinterpret_cast<u_char *> (dst));
}
str
pw_armorsalt (u_int cost, str bsalt, str ptext)
{
return strbuf ("%d$", cost) << armor64 (bsalt) << "$" << ptext;
}
#define A64STR "[A-Za-z0-9+/]+={0,2}"
static rxx saltrx ("^(\\d+)\\$(" A64STR ")\\$(.*)$");
bool
pw_dearmorsalt (u_int *costp, str *bsaltp, str *ptextp, str armor)
{
if (!(armor / saltrx))
return false;
str s = dearmor64 (saltrx[2]);
if (!s)
return false;
if (bsaltp)
*bsaltp = s;
if (costp)
*costp = strtoi64 (saltrx[1]);
if (ptextp)
*ptextp = saltrx[3];
return true;
}
str
pw_dorawcrypt (str ptext, size_t outsize, eksblowfish *eksb)
{
wmstr m (outsize + 7 & ~7);
hashptext (m, m.len (), ptext);
cbc64iv iv (*eksb);
for (int i = 0; i < 64; i++) {
iv.setiv (0, 0);
iv.encipher_bytes (m, m.len ());
}
return m;
}
str
pw_rawcrypt (u_int cost, str pwd, str bsalt,
str ptext, size_t outsize, eksblowfish *eksb)
{
u_int maxlen = 56;
if (!outsize)
outsize = ptext.len ();
eksblowfish *eksbdel = NULL;
if (!eksb)
eksbdel = eksb = New eksblowfish;
if (pwd.len () > maxlen) {
char hsh[2 * sha1::hashsize];
sha1_hash (reinterpret_cast <u_char *> (hsh), pwd.cstr (), pwd.len ());
sha1_hash (reinterpret_cast <u_char *> (hsh + sha1::hashsize),
str (hsh, sha1::hashsize), sha1::hashsize);
pwd = str (hsh, (2*sha1::hashsize < maxlen) ? 2*sha1::hashsize : maxlen);
}
eksb->eksetkey (cost, pwd.cstr (), pwd.len (), bsalt.cstr (), bsalt.len ());
str ret = pw_dorawcrypt (ptext, outsize, eksb);
delete eksbdel;
return ret;
}
str
pw_gensalt (u_int cost, str ptext)
{
mstr m (16);
rnd.getbytes (m, m.len ());
str bsalt (m);
return pw_armorsalt (cost, bsalt, ptext);
}
str
pw_getptext (str salt)
{
if (salt / saltrx)
return saltrx[3];
return NULL;
}
str
pw_crypt (str pwd, str salt, size_t outsize, eksblowfish *eksb)
{
u_int cost;
str bsalt, ptext;
if (!pw_dearmorsalt (&cost, &bsalt, &ptext, salt))
return NULL;
return pw_rawcrypt (cost, pwd, bsalt, ptext, outsize, eksb);
}
bigint
pw_getint (str pwd, str salt, size_t nbits, eksblowfish *eksb)
{
str raw = pw_crypt (pwd, salt, nbits + 7 >> 3, eksb);
if (!raw)
return 0;
bigint res;
mpz_set_rawmag_le (&res, raw.cstr (), raw.len ());
res.trunc (nbits);
return res;
}
|