* changed the auth_md5.c - code to the openbsd-version (public domain)
* added auth_sha1.c - also from openbsd-codebase, also public domain * added two stand-alone programs to calculate md5/sha1 hashs: + src/asha1 - reads from stdin, EOF ends + src/amd5 - reads from stdin, EOF ends (just in case your system DOESNT have md5/md5sum or sha1/sha1sum * minor changes to aklock.c --HG-- extra : convert_revision : svn%3Aeebe1cee-a9af-4fe4-bd26-ad572b19c5ab/trunk%408
This commit is contained in:
parent
e199e274c3
commit
ec57a11c64
8 changed files with 633 additions and 307 deletions
10
CHANGELOG
10
CHANGELOG
|
@ -1,5 +1,15 @@
|
|||
Version 0.5
|
||||
|
||||
2005-05-08:
|
||||
|
||||
* changed the auth_md5.c - code to the openbsd-version (public domain)
|
||||
* added auth_sha1.c - also from openbsd-codebase, also public domain
|
||||
* added two stand-alone programs to calculate md5/sha1 hashs:
|
||||
+ src/asha1 - reads from stdin, EOF ends
|
||||
+ src/amd5 - reads from stdin, EOF ends
|
||||
(just in case your system DOESNT have md5/md5sum or sha1/sha1sum
|
||||
* minor changes to aklock.c
|
||||
|
||||
2005-05-07:
|
||||
|
||||
* big restructering + feature enhancements
|
||||
|
|
42
SConstruct
42
SConstruct
|
@ -7,28 +7,30 @@
|
|||
|
||||
import sys
|
||||
|
||||
aklock_version = '0.2'
|
||||
aklock_version = '0.5'
|
||||
aklock_optfile = [ 'scons.opts', 'user.opts' ]
|
||||
|
||||
aklock_target = 'src/aklock'
|
||||
|
||||
Default(aklock_target)
|
||||
|
||||
|
||||
aklock_options = Options(aklock_optfile)
|
||||
aklock_options.AddOptions(
|
||||
BoolOption('debug', 'build debug version', 0),
|
||||
BoolOption('passwd', 'support for classic passwd', 0),
|
||||
BoolOption('shadow', 'support for shadowpasswords', 0),
|
||||
BoolOption('pam', 'support for pam', 1),
|
||||
BoolOption('md5', 'support for md5', 1),
|
||||
BoolOption('hash', 'support for hashs(sha1,md5)', 1),
|
||||
|
||||
BoolOption('xcursor', 'support xcursor-themes', 1),
|
||||
|
||||
|
||||
BoolOption('amd5', 'build a little md5-helper', 0),
|
||||
BoolOption('asha1', 'build a little sha1-helper', 0),
|
||||
|
||||
PathOption('prefix', 'install-path base', '/usr/local')
|
||||
)
|
||||
|
||||
aklock_env = Environment(options = aklock_options,
|
||||
aklock_env = Environment(options = aklock_options,
|
||||
TARFLAGS = '-c -z',
|
||||
TARSUFFIX = '.tgz')
|
||||
aklock_options.Update(aklock_env)
|
||||
|
@ -59,7 +61,7 @@ if aklock_env['passwd']:
|
|||
aklock_env.AppendUnique(
|
||||
CPPDEFINES = [ 'PASSWD_PWD' ],
|
||||
LIBS = [ 'crypt' ])
|
||||
|
||||
|
||||
if aklock_env['pam']:
|
||||
aklock_env.AppendUnique(
|
||||
CPPDEFINES = [ 'PAM_PWD' ],
|
||||
|
@ -67,16 +69,16 @@ if aklock_env['pam']:
|
|||
|
||||
if sys.platform == 'linux2' or sys.platform == 'linux-i386':
|
||||
aklock_env.AppendUnique(LIBS = ['pam_misc'])
|
||||
|
||||
|
||||
|
||||
if aklock_env['shadow']:
|
||||
aklock_env.AppendUnique(
|
||||
CPPDEFINES = [ 'SHADOW_PWD' ])
|
||||
|
||||
if aklock_env['md5']:
|
||||
if aklock_env['hash']:
|
||||
aklock_env.AppendUnique(
|
||||
CPPDEFINES = [ 'MD5_PWD' ])
|
||||
|
||||
CPPDEFINES = [ 'HASH_PWD' ])
|
||||
|
||||
if aklock_env['xcursor']:
|
||||
conf = aklock_env.Configure()
|
||||
if conf.CheckLib('Xcursor', 'XcursorSupportsARGB', 1):
|
||||
|
@ -87,21 +89,31 @@ if aklock_env['xcursor']:
|
|||
aklock_env['xcursor'] = 0
|
||||
print "sorry, no xcursor-support found."
|
||||
conf.Finish()
|
||||
|
||||
|
||||
|
||||
default_targets = [ aklock_target ]
|
||||
if aklock_env['amd5']:
|
||||
default_targets += [ 'src/amd5' ]
|
||||
|
||||
if aklock_env['asha1']:
|
||||
default_targets += [ 'src/asha1' ]
|
||||
|
||||
Default(default_targets)
|
||||
|
||||
aklock_options.Save('scons.opts', aklock_env)
|
||||
|
||||
|
||||
aklock_program = SConscript(
|
||||
'src/SConscript',
|
||||
'src/SConscript',
|
||||
exports = ['aklock_env']
|
||||
)
|
||||
|
||||
aklock_env.Install(
|
||||
aklock_env['prefix']+'/bin',
|
||||
aklock_env['prefix']+'/bin',
|
||||
aklock_program
|
||||
)
|
||||
|
||||
aklock_env.Alias(
|
||||
'install',
|
||||
'install',
|
||||
aklock_env['prefix']+'/bin'
|
||||
)
|
||||
|
||||
|
|
|
@ -13,14 +13,23 @@ build = aklock_env.Copy()
|
|||
|
||||
if build['passwd']:
|
||||
aklock_sources += [ 'auth_passwd.c' ]
|
||||
if build['md5']:
|
||||
aklock_sources += [ 'auth_md5.c' ]
|
||||
if build['hash']:
|
||||
aklock_sources += [ 'auth_md5.c', 'auth_sha1.c' ]
|
||||
if build['pam']:
|
||||
aklock_sources += [ 'auth_pam.c' ]
|
||||
|
||||
|
||||
aklock = build.Program('aklock', aklock_sources)
|
||||
build.AddPostAction(aklock, Chmod(aklock, 0755))
|
||||
|
||||
if build['amd5']:
|
||||
md5 = aklock_env.Copy()
|
||||
md5.AppendUnique(CPPDEFINES = [ 'STAND_ALONE' ])
|
||||
md5.Program('amd5', md5.StaticObject(target = 'amd5.o', source = 'auth_md5.c'))
|
||||
|
||||
if build['asha1']:
|
||||
sha1 = aklock_env.Copy()
|
||||
sha1.AppendUnique(CPPDEFINES = [ 'STAND_ALONE' ])
|
||||
sha1.Program('asha1', sha1.StaticObject(target = 'asha1', source = 'auth_sha1.c'))
|
||||
|
||||
# vim:ft=python
|
||||
|
|
29
src/aklock.c
29
src/aklock.c
|
@ -48,14 +48,14 @@
|
|||
#include "aklock.h"
|
||||
|
||||
struct akXInfo {
|
||||
|
||||
|
||||
Display* display;
|
||||
Window root;
|
||||
Window window;
|
||||
Colormap colormap;
|
||||
|
||||
Cursor cursor;
|
||||
|
||||
|
||||
int width;
|
||||
int height;
|
||||
};
|
||||
|
@ -76,9 +76,10 @@ struct akXInfo {
|
|||
|
||||
static struct akAuth* ak_authmodules[] = {
|
||||
&aklock_auth_none,
|
||||
#ifdef MD5_PWD
|
||||
#ifdef HASH_PWD
|
||||
&aklock_auth_md5,
|
||||
#endif /* MD5_PWD */
|
||||
&aklock_auth_sha1,
|
||||
#endif /* HASH_PWD */
|
||||
#ifdef PASSWD_PWD
|
||||
&aklock_auth_passwd,
|
||||
#endif /* PASSWD_PWD */
|
||||
|
@ -92,8 +93,19 @@ static struct akAuth* ak_authmodules[] = {
|
|||
\*------------------------------------------------------------------*/
|
||||
|
||||
void displayUsage() {
|
||||
printf("\naklock [-h] [-v] [-blank] [-cursor <theme|xcursor:file>]\n");
|
||||
printf(" [-auth list|<none|passwd|pam|md5:pwdhash>]\n");
|
||||
printf("\n");
|
||||
printf("aklock [-h] [-v] [-blank] [-cursor <theme|xcursor:file>]\n");
|
||||
printf(" [-auth list|<none");
|
||||
#ifdef PASSWD_PWD
|
||||
printf("|passwd");
|
||||
#endif /* PASSWD_PWD */
|
||||
#ifdef PAM_PWD
|
||||
printf("|pam");
|
||||
#endif /* PAM_PWD */
|
||||
#ifdef HASH_PWD
|
||||
printf("|md5:pwdhash|sha1:pwdhash");
|
||||
#endif /* HASH_PWD */
|
||||
printf(">]\n");
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*\
|
||||
|
@ -223,7 +235,7 @@ int main(int argc, char **argv) {
|
|||
opts.use_blank = 1;
|
||||
} else if (!strcmp(argv[arg - 1], "-auth")) {
|
||||
if (arg < argc) {
|
||||
|
||||
|
||||
char* char_tmp;
|
||||
struct akAuth* auth_tmp = NULL;
|
||||
struct akAuth** i;
|
||||
|
@ -243,6 +255,7 @@ int main(int argc, char **argv) {
|
|||
exit(1);
|
||||
}
|
||||
opts.auth = auth_tmp;
|
||||
++arg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -251,7 +264,7 @@ int main(int argc, char **argv) {
|
|||
fprintf(stderr, "aklock: error, couldnt find the auth module you specified.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
fprintf(stderr, "aklock, error, missing argument\n");
|
||||
displayUsage();
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
copyr : copyright (c) 2005 by m. gumz
|
||||
|
||||
license : see LICENSE
|
||||
|
||||
|
||||
start : Sa 30 Apr 2005 11:51:52 CEST
|
||||
|
||||
\* ---------------------------------------------------------------- */
|
||||
|
@ -48,7 +48,7 @@ struct akOpts {
|
|||
char use_blank;
|
||||
|
||||
char* cursor_name;
|
||||
|
||||
|
||||
char* color_fg;
|
||||
char* color_bg;
|
||||
};
|
||||
|
@ -62,9 +62,10 @@ extern struct akCursor ak_cursors[];
|
|||
/*------------------------------------------------------------------*\
|
||||
\*------------------------------------------------------------------*/
|
||||
extern struct akAuth aklock_auth_none;
|
||||
#ifdef MD5_PWD
|
||||
#ifdef HASH_PWD
|
||||
extern struct akAuth aklock_auth_md5;
|
||||
#endif /* MD5_PWD */
|
||||
extern struct akAuth aklock_auth_sha1;
|
||||
#endif /* HASH_PWD */
|
||||
#ifdef PASSWD_PWD
|
||||
extern struct akAuth aklock_auth_passwd;
|
||||
#endif /* PASSWD_PWD */
|
||||
|
|
524
src/auth_md5.c
524
src/auth_md5.c
|
@ -4,31 +4,18 @@
|
|||
author : m. gumz <akira at fluxbox dot org>
|
||||
copyr : copyright (c) 2005 by m. gumz
|
||||
|
||||
license : based on:
|
||||
|
||||
MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
|
||||
license : based on: openbsd md5.c/h
|
||||
|
||||
Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
|
||||
rights reserved.
|
||||
This code implements the MD5 message-digest algorithm.
|
||||
The algorithm is due to Ron Rivest. This code was
|
||||
written by Colin Plumb in 1993, no copyright is claimed.
|
||||
This code is in the public domain; do with it what you wish.
|
||||
|
||||
License to copy and use this software is granted provided that it
|
||||
is identified as the "RSA Data Security, Inc. MD5 Message-Digest
|
||||
Algorithm" in all material mentioning or referencing this software
|
||||
or this function.
|
||||
Equivalent code is available from RSA Data Security, Inc.
|
||||
This code has been tested against that, and is equivalent,
|
||||
except that you don't need to include two pages of legalese
|
||||
with every copy.
|
||||
|
||||
License is also granted to make and use derivative works provided
|
||||
that such works are identified as "derived from the RSA Data
|
||||
Security, Inc. MD5 Message-Digest Algorithm" in all material
|
||||
mentioning or referencing the derived work.
|
||||
|
||||
RSA Data Security, Inc. makes no representations concerning either
|
||||
the merchantability of this software or the suitability of this
|
||||
software for any particular purpose. It is provided "as is"
|
||||
without express or implied warranty of any kind.
|
||||
|
||||
These notices must be retained in any copies of any part of this
|
||||
documentation and/or software.
|
||||
|
||||
start : Sa 07 Mai 2005 13:21:45 CEST
|
||||
|
||||
\* ---------------------------------------------------------------- */
|
||||
|
@ -41,301 +28,261 @@
|
|||
/* ---------------------------------------------------------------- *\
|
||||
includes
|
||||
\* ---------------------------------------------------------------- */
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#ifndef STAND_ALONE
|
||||
#include "aklock.h"
|
||||
#endif /* STAND_ALONE */
|
||||
/*------------------------------------------------------------------*\
|
||||
\*------------------------------------------------------------------*/
|
||||
|
||||
enum {
|
||||
S11 = 7,
|
||||
S12 = 12,
|
||||
S13 = 17,
|
||||
S14 = 22,
|
||||
S21 = 5,
|
||||
S22 = 9,
|
||||
S23 = 14,
|
||||
S24 = 20,
|
||||
S31 = 4,
|
||||
S32 = 11,
|
||||
S33 = 16,
|
||||
S34 = 23,
|
||||
S41 = 6,
|
||||
S42 = 10,
|
||||
S43 = 15,
|
||||
S44 = 21
|
||||
MD5_BLOCK_LENGTH = 64,
|
||||
MD5_DIGEST_LENGTH = 16,
|
||||
MD5_DIGEST_STRING_LENGTH = (MD5_DIGEST_LENGTH * 2 + 1)
|
||||
};
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
typedef struct {
|
||||
unsigned long int state[4]; /* state (ABCD) */
|
||||
unsigned long int count[2]; /* number of bits, modulo 2^64 (lsb first) */
|
||||
unsigned char buffer[64]; /* input buffer */
|
||||
u_int32_t state[4]; /* state */
|
||||
u_int64_t count; /* number of bits, mod 2^64 */
|
||||
u_int8_t buffer[MD5_BLOCK_LENGTH]; /* input buffer */
|
||||
} md5Context;
|
||||
|
||||
|
||||
static void md5_transform(unsigned long int[4], unsigned char [64]);
|
||||
static void md5_encode(unsigned char*, unsigned long int*, unsigned int);
|
||||
static void md5_decode(unsigned long int*, unsigned char*, unsigned int);
|
||||
|
||||
static void md5_init(md5Context*);
|
||||
static void md5_update(md5Context*, unsigned char *, unsigned int);
|
||||
static void md5_final(unsigned char[16], md5Context *);
|
||||
void md5_init(md5Context*);
|
||||
void md5_update(md5Context*, const u_int8_t*, size_t);
|
||||
void md5_pad(md5Context*);
|
||||
void md5_final(u_int8_t [MD5_DIGEST_LENGTH], md5Context*);
|
||||
void md5_transform(u_int32_t [4], const u_int8_t [MD5_BLOCK_LENGTH]);
|
||||
|
||||
/*------------------------------------------------------------------*\
|
||||
\*------------------------------------------------------------------*/
|
||||
static unsigned char padding[64] = {
|
||||
|
||||
#define PUT_64BIT_LE(cp, value) do { \
|
||||
(cp)[7] = (value) >> 56; \
|
||||
(cp)[6] = (value) >> 48; \
|
||||
(cp)[5] = (value) >> 40; \
|
||||
(cp)[4] = (value) >> 32; \
|
||||
(cp)[3] = (value) >> 24; \
|
||||
(cp)[2] = (value) >> 16; \
|
||||
(cp)[1] = (value) >> 8; \
|
||||
(cp)[0] = (value); } while (0)
|
||||
|
||||
#define PUT_32BIT_LE(cp, value) do { \
|
||||
(cp)[3] = (value) >> 24; \
|
||||
(cp)[2] = (value) >> 16; \
|
||||
(cp)[1] = (value) >> 8; \
|
||||
(cp)[0] = (value); } while (0)
|
||||
|
||||
static u_int8_t PADDING[MD5_BLOCK_LENGTH] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/* F, G, H and I are basic MD5 functions. */
|
||||
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
|
||||
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
|
||||
#define H(x, y, z) ((x) ^ (y) ^ (z))
|
||||
#define I(x, y, z) ((y) ^ ((x) | (~z)))
|
||||
|
||||
/* ROTATE_LEFT rotates x left n bits. */
|
||||
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
|
||||
|
||||
/*--------------------------------------------------------*\
|
||||
FF, GG, HH, and II transformations for rounds 1, 2, 3,
|
||||
and 4. Rotation is separate from addition to prevent
|
||||
recomputation.
|
||||
\*--------------------------------------------------------*/
|
||||
#define FF(a, b, c, d, x, s, ac) { \
|
||||
(a) += F ((b), (c), (d)) + (x) + (unsigned long int)(ac); \
|
||||
(a) = ROTATE_LEFT ((a), (s)); \
|
||||
(a) += (b); \
|
||||
}
|
||||
|
||||
#define GG(a, b, c, d, x, s, ac) { \
|
||||
(a) += G ((b), (c), (d)) + (x) + (unsigned long int)(ac); \
|
||||
(a) = ROTATE_LEFT ((a), (s)); \
|
||||
(a) += (b); \
|
||||
}
|
||||
|
||||
|
||||
#define HH(a, b, c, d, x, s, ac) { \
|
||||
(a) += H ((b), (c), (d)) + (x) + (unsigned long int)(ac); \
|
||||
(a) = ROTATE_LEFT ((a), (s)); \
|
||||
(a) += (b); \
|
||||
}
|
||||
|
||||
#define II(a, b, c, d, x, s, ac) { \
|
||||
(a) += I ((b), (c), (d)) + (x) + (unsigned long int)(ac); \
|
||||
(a) = ROTATE_LEFT ((a), (s)); \
|
||||
(a) += (b); \
|
||||
/*------------------------------------------------------------------*\
|
||||
Start MD5 accumulation. Set bit count to 0 and buffer to
|
||||
mysterious initialization constants.
|
||||
\*------------------------------------------------------------------*/
|
||||
void md5_init(md5Context* ctx) {
|
||||
ctx->count = 0;
|
||||
ctx->state[0] = 0x67452301;
|
||||
ctx->state[1] = 0xefcdab89;
|
||||
ctx->state[2] = 0x98badcfe;
|
||||
ctx->state[3] = 0x10325476;
|
||||
}
|
||||
/*------------------------------------------------------------------*\
|
||||
Update context to reflect the concatenation of another buffer
|
||||
full of bytes.
|
||||
\*------------------------------------------------------------------*/
|
||||
void md5_update(md5Context* ctx, const unsigned char* input, size_t len) {
|
||||
|
||||
/*--------------------------------------------------------*\
|
||||
MD5 initialization. Begins an MD5 operation, writing
|
||||
a new context.
|
||||
\*--------------------------------------------------------*/
|
||||
static void md5_init(md5Context* context) {
|
||||
context->count[0] = context->count[1] = 0;
|
||||
/* Load magic initialization constants.*/
|
||||
context->state[0] = 0x67452301;
|
||||
context->state[1] = 0xefcdab89;
|
||||
context->state[2] = 0x98badcfe;
|
||||
context->state[3] = 0x10325476;
|
||||
}
|
||||
size_t have, need;
|
||||
|
||||
/*--------------------------------------------------------*\
|
||||
MD5 block update operation. Continues an MD5
|
||||
message-digest operation, processing another message
|
||||
block, and updating the context.
|
||||
\*--------------------------------------------------------*/
|
||||
/* Check how many bytes we already have and how many more we need. */
|
||||
have = (size_t)((ctx->count >> 3) & (MD5_BLOCK_LENGTH - 1));
|
||||
need = MD5_BLOCK_LENGTH - have;
|
||||
|
||||
static void md5_update(md5Context* context, unsigned char* input, unsigned int inputLen) {
|
||||
|
||||
unsigned int i, index, partLen;
|
||||
/* Update bitcount */
|
||||
ctx->count += (u_int64_t)len << 3;
|
||||
|
||||
/* Compute number of bytes mod 64 */
|
||||
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
|
||||
if (len >= need) {
|
||||
if (have != 0) {
|
||||
memcpy(ctx->buffer + have, input, need);
|
||||
md5_transform(ctx->state, ctx->buffer);
|
||||
input += need;
|
||||
len -= need;
|
||||
have = 0;
|
||||
}
|
||||
|
||||
/* Update number of bits */
|
||||
if ((context->count[0] += ((unsigned long int)inputLen << 3)) < ((unsigned long int)inputLen << 3))
|
||||
context->count[1]++;
|
||||
|
||||
context->count[1] += ((unsigned long int)inputLen >> 29);
|
||||
|
||||
partLen = 64 - index;
|
||||
|
||||
/* Transform as many times as possible. */
|
||||
if (inputLen >= partLen) {
|
||||
memcpy((unsigned char*)&context->buffer[index],
|
||||
(unsigned char*)input,
|
||||
partLen);
|
||||
md5_transform(context->state, context->buffer);
|
||||
|
||||
for (i = partLen; i + 63 < inputLen; i += 64)
|
||||
md5_transform(context->state, &input[i]);
|
||||
|
||||
index = 0;
|
||||
/* Process data in MD5_BLOCK_LENGTH-byte chunks. */
|
||||
while (len >= MD5_BLOCK_LENGTH) {
|
||||
md5_transform(ctx->state, input);
|
||||
input += MD5_BLOCK_LENGTH;
|
||||
len -= MD5_BLOCK_LENGTH;
|
||||
}
|
||||
}
|
||||
else
|
||||
i = 0;
|
||||
|
||||
/* Buffer remaining input */
|
||||
memcpy((unsigned char*)&context->buffer[index],
|
||||
(unsigned char*)&input[i],
|
||||
inputLen-i);
|
||||
/* Handle any remaining bytes of data. */
|
||||
if (len != 0)
|
||||
memcpy(ctx->buffer + have, input, len);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------*\
|
||||
MD5 finalization. Ends an MD5 message-digest operation,
|
||||
writing the the message digest and zeroizing the context.
|
||||
\*--------------------------------------------------------*/
|
||||
static void md5_final(unsigned char digest[16], md5Context* context) {
|
||||
unsigned char bits[8];
|
||||
unsigned int index;
|
||||
unsigned int padLen;
|
||||
/*------------------------------------------------------------------*\
|
||||
Pad pad to 64-byte boundary with the bit pattern
|
||||
1 0* (64-bit count of bits processed, MSB-first)
|
||||
\*------------------------------------------------------------------*/
|
||||
void md5_pad(md5Context *ctx)
|
||||
{
|
||||
u_int8_t count[8];
|
||||
size_t padlen;
|
||||
|
||||
/* Save number of bits */
|
||||
md5_encode(bits, context->count, 8);
|
||||
/* Convert count to 8 bytes in little endian order. */
|
||||
PUT_64BIT_LE(count, ctx->count);
|
||||
|
||||
/* Pad out to 56 mod 64. */
|
||||
index = (unsigned int)((context->count[0] >> 3) & 0x3f);
|
||||
padLen = (index < 56) ? (56 - index) : (120 - index);
|
||||
md5_update(context, padding, padLen);
|
||||
|
||||
/* Append length (before padding) */
|
||||
md5_update(context, bits, 8);
|
||||
/* Store state in digest */
|
||||
md5_encode(digest, context->state, 16);
|
||||
|
||||
/* Zeroize sensitive information. */
|
||||
memset((unsigned char*)context, 0, sizeof (*context));
|
||||
padlen = MD5_BLOCK_LENGTH -
|
||||
((ctx->count >> 3) & (MD5_BLOCK_LENGTH - 1));
|
||||
if (padlen < 1 + 8)
|
||||
padlen += MD5_BLOCK_LENGTH;
|
||||
md5_update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
|
||||
md5_update(ctx, count, 8);
|
||||
}
|
||||
/* MD5 basic transformation. Transforms state based on block.
|
||||
*/
|
||||
static void md5_transform(unsigned long int state[4], unsigned char block[64]) {
|
||||
unsigned long int a = state[0];
|
||||
unsigned long int b = state[1];
|
||||
unsigned long int c = state[2];
|
||||
unsigned long int d = state[3];
|
||||
unsigned long int x[16];
|
||||
|
||||
md5_decode(x, block, 64);
|
||||
/*------------------------------------------------------------------*\
|
||||
Final wrapup--call MD5Pad, fill in digest and zero out ctx.
|
||||
\*------------------------------------------------------------------*/
|
||||
void md5_final(unsigned char digest[MD5_DIGEST_LENGTH], md5Context *ctx) {
|
||||
int i;
|
||||
|
||||
/* Round 1 */
|
||||
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
|
||||
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
|
||||
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
|
||||
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
|
||||
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
|
||||
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
|
||||
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
|
||||
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
|
||||
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
|
||||
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
|
||||
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
|
||||
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
|
||||
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
|
||||
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
|
||||
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
|
||||
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
|
||||
md5_pad(ctx);
|
||||
if (digest != NULL) {
|
||||
for (i = 0; i < 4; i++)
|
||||
PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
}
|
||||
}
|
||||
|
||||
/* Round 2 */
|
||||
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
|
||||
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
|
||||
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
|
||||
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
|
||||
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
|
||||
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
|
||||
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
|
||||
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
|
||||
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
|
||||
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
|
||||
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
|
||||
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
|
||||
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
|
||||
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
|
||||
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
|
||||
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
|
||||
|
||||
/* Round 3 */
|
||||
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
|
||||
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
|
||||
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
|
||||
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
|
||||
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
|
||||
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
|
||||
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
|
||||
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
|
||||
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
|
||||
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
|
||||
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
|
||||
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
|
||||
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
|
||||
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
|
||||
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
|
||||
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
|
||||
/* The four core functions - F1 is optimized somewhat */
|
||||
|
||||
/* Round 4 */
|
||||
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
|
||||
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
|
||||
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
|
||||
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
|
||||
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
|
||||
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
|
||||
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
|
||||
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
|
||||
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
|
||||
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
|
||||
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
|
||||
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
|
||||
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
|
||||
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
|
||||
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
|
||||
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
|
||||
/* #define F1(x, y, z) (x & y | ~x & z) */
|
||||
#define F1(x, y, z) (z ^ (x & (y ^ z)))
|
||||
#define F2(x, y, z) F1(z, x, y)
|
||||
#define F3(x, y, z) (x ^ y ^ z)
|
||||
#define F4(x, y, z) (y ^ (x | ~z))
|
||||
|
||||
/* This is the central step in the MD5 algorithm. */
|
||||
#define MD5STEP(f, w, x, y, z, data, s) \
|
||||
( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
|
||||
/*------------------------------------------------------------------*\
|
||||
The core of the MD5 algorithm, this alters an existing MD5 hash to
|
||||
reflect the addition of 16 longwords of new data. MD5Update blocks
|
||||
the data and converts bytes into longwords for this routine.
|
||||
|
||||
\*------------------------------------------------------------------*/
|
||||
void md5_transform(u_int32_t state[4], const u_int8_t block[MD5_BLOCK_LENGTH]) {
|
||||
u_int32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4];
|
||||
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
memcpy(in, block, sizeof(in));
|
||||
#else
|
||||
for (a = 0; a < MD5_BLOCK_LENGTH / 4; a++) {
|
||||
in[a] = (u_int32_t)(
|
||||
(u_int32_t)(block[a * 4 + 0]) |
|
||||
(u_int32_t)(block[a * 4 + 1]) << 8 |
|
||||
(u_int32_t)(block[a * 4 + 2]) << 16 |
|
||||
(u_int32_t)(block[a * 4 + 3]) << 24);
|
||||
}
|
||||
#endif
|
||||
|
||||
a = state[0];
|
||||
b = state[1];
|
||||
c = state[2];
|
||||
d = state[3];
|
||||
|
||||
MD5STEP(F1, a, b, c, d, in[ 0] + 0xd76aa478, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[ 1] + 0xe8c7b756, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[ 2] + 0x242070db, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[ 3] + 0xc1bdceee, 22);
|
||||
MD5STEP(F1, a, b, c, d, in[ 4] + 0xf57c0faf, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[ 5] + 0x4787c62a, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[ 6] + 0xa8304613, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[ 7] + 0xfd469501, 22);
|
||||
MD5STEP(F1, a, b, c, d, in[ 8] + 0x698098d8, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[ 9] + 0x8b44f7af, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
|
||||
MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
|
||||
|
||||
MD5STEP(F2, a, b, c, d, in[ 1] + 0xf61e2562, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[ 6] + 0xc040b340, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[ 0] + 0xe9b6c7aa, 20);
|
||||
MD5STEP(F2, a, b, c, d, in[ 5] + 0xd62f105d, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[ 4] + 0xe7d3fbc8, 20);
|
||||
MD5STEP(F2, a, b, c, d, in[ 9] + 0x21e1cde6, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[ 3] + 0xf4d50d87, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[ 8] + 0x455a14ed, 20);
|
||||
MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[ 2] + 0xfcefa3f8, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[ 7] + 0x676f02d9, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
|
||||
|
||||
MD5STEP(F3, a, b, c, d, in[ 5] + 0xfffa3942, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[ 8] + 0x8771f681, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
|
||||
MD5STEP(F3, a, b, c, d, in[ 1] + 0xa4beea44, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[ 4] + 0x4bdecfa9, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[ 7] + 0xf6bb4b60, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
|
||||
MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[ 0] + 0xeaa127fa, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[ 3] + 0xd4ef3085, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[ 6] + 0x04881d05, 23);
|
||||
MD5STEP(F3, a, b, c, d, in[ 9] + 0xd9d4d039, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[2 ] + 0xc4ac5665, 23);
|
||||
|
||||
MD5STEP(F4, a, b, c, d, in[ 0] + 0xf4292244, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[7 ] + 0x432aff97, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[5 ] + 0xfc93a039, 21);
|
||||
MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[3 ] + 0x8f0ccc92, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[1 ] + 0x85845dd1, 21);
|
||||
MD5STEP(F4, a, b, c, d, in[8 ] + 0x6fa87e4f, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[6 ] + 0xa3014314, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
|
||||
MD5STEP(F4, a, b, c, d, in[4 ] + 0xf7537e82, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[2 ] + 0x2ad7d2bb, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[9 ] + 0xeb86d391, 21);
|
||||
|
||||
state[0] += a;
|
||||
state[1] += b;
|
||||
state[2] += c;
|
||||
state[3] += d;
|
||||
|
||||
/* Zeroize sensitive information. */
|
||||
memset((unsigned char*)x, 0, sizeof (x));
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------*\
|
||||
Encodes input into output. Assumes len is a multiple
|
||||
of 4.
|
||||
\*--------------------------------------------------------*/
|
||||
static void md5_encode(unsigned char* output, unsigned long int* input, unsigned int len) {
|
||||
|
||||
unsigned int i, j;
|
||||
for (i = 0, j = 0; j < len; i++, j += 4) {
|
||||
output[j] = (unsigned char)(input[i] & 0xff);
|
||||
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
|
||||
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
|
||||
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------*\
|
||||
Decodes input into output. Assumes len is a multiple
|
||||
of 4.
|
||||
\*--------------------------------------------------------*/
|
||||
static void md5_decode(unsigned long int* output, unsigned char* input, unsigned int len) {
|
||||
|
||||
unsigned int i, j;
|
||||
for (i = 0, j = 0; j < len; i++, j += 4)
|
||||
output[i] = ((unsigned long int)input[j]) |
|
||||
(((unsigned long int)input[j+1]) << 8) |
|
||||
(((unsigned long int)input[j+2]) << 16) |
|
||||
(((unsigned long int)input[j+3]) << 24);
|
||||
}
|
||||
|
||||
|
||||
/* ---------------------------------------------------------------- *\
|
||||
\* ---------------------------------------------------------------- */
|
||||
|
||||
#ifndef STAND_ALONE
|
||||
|
||||
static const char* userhash = NULL;
|
||||
|
||||
static int init(const char* args) {
|
||||
|
@ -346,7 +293,7 @@ static int init(const char* args) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
if (strlen(&args[4]) != 32) {
|
||||
if (strlen(&args[4]) != MD5_DIGEST_STRING_LENGTH - 1) {
|
||||
fprintf(stderr, "aklock: error, invalid md5-hash.\n");
|
||||
return 0;
|
||||
}
|
||||
|
@ -364,8 +311,8 @@ static int deinit() {
|
|||
|
||||
static int auth(const char* pass) {
|
||||
|
||||
unsigned char tmpdigest[16];
|
||||
unsigned char digest[32];
|
||||
unsigned char digest[MD5_DIGEST_LENGTH];
|
||||
unsigned char stringdigest[MD5_DIGEST_STRING_LENGTH];
|
||||
unsigned int i;
|
||||
md5Context md5;
|
||||
|
||||
|
@ -374,23 +321,50 @@ static int auth(const char* pass) {
|
|||
|
||||
md5_init(&md5);
|
||||
md5_update(&md5, (unsigned char*)pass, strlen(pass));
|
||||
md5_final(tmpdigest, &md5);
|
||||
md5_final(digest, &md5);
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
sprintf(&digest[i*2], "%02x", tmpdigest[i]);
|
||||
memset(stringdigest, 0, MD5_DIGEST_STRING_LENGTH);
|
||||
for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
|
||||
sprintf(&stringdigest[i*2], "%02x", digest[i]);
|
||||
}
|
||||
|
||||
return !strcmp(digest, userhash);
|
||||
|
||||
return !strcmp(stringdigest, userhash);
|
||||
}
|
||||
|
||||
struct akAuth aklock_auth_md5 = {
|
||||
"md5",
|
||||
init,
|
||||
init,
|
||||
auth,
|
||||
deinit
|
||||
};
|
||||
|
||||
/* ---------------------------------------------------------------- *\
|
||||
\* ---------------------------------------------------------------- */
|
||||
#else
|
||||
|
||||
int main() {
|
||||
|
||||
unsigned char digest[MD5_DIGEST_LENGTH];
|
||||
unsigned int i;
|
||||
unsigned char c;
|
||||
md5Context md5;
|
||||
|
||||
md5_init(&md5);
|
||||
while((c = fgetc(stdin)) != 255) {
|
||||
md5_update(&md5, &c, 1);
|
||||
}
|
||||
md5_final(digest, &md5);
|
||||
|
||||
printf("\n");
|
||||
for(i = 0; i < MD5_DIGEST_LENGTH; ++i)
|
||||
printf("%02x", digest[i]);
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* STAND_ALONE */
|
||||
/* ---------------------------------------------------------------- *\
|
||||
\* ---------------------------------------------------------------- */
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
copyr : copyright (c) 2005 by m. gumz
|
||||
|
||||
license : see LICENSE
|
||||
|
||||
|
||||
start : Sa 07 Mai 2005 16:41:28 CEST
|
||||
|
||||
\* ---------------------------------------------------------------- */
|
||||
|
@ -23,7 +23,7 @@
|
|||
/* ---------------------------------------------------------------- *\
|
||||
\* ---------------------------------------------------------------- */
|
||||
|
||||
static int init(const unsigned char* args) {
|
||||
static int init(const char* args) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ static int deinit() {
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int auth(const unsigned char* passwd) {
|
||||
static int auth(const char* passwd) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
307
src/auth_sha1.c
Normal file
307
src/auth_sha1.c
Normal file
|
@ -0,0 +1,307 @@
|
|||
/* ---------------------------------------------------------------- *\
|
||||
|
||||
file : auth_sha1.c
|
||||
author : m. gumz <akira at fluxbox dot org>
|
||||
copyr : copyright (c) 2005 by m. gumz
|
||||
|
||||
license : based on: openbsd sha1.c/h
|
||||
|
||||
SHA-1 in C
|
||||
By Steve Reid <steve@edmweb.com>
|
||||
100% Public Domain
|
||||
|
||||
start : So 08 Mai 2005 13:21:45 CEST
|
||||
|
||||
\* ---------------------------------------------------------------- */
|
||||
/* ---------------------------------------------------------------- *\
|
||||
|
||||
about :
|
||||
|
||||
\* ---------------------------------------------------------------- */
|
||||
|
||||
/* ---------------------------------------------------------------- *\
|
||||
includes
|
||||
\* ---------------------------------------------------------------- */
|
||||
#include <sys/types.h>
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/param.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#ifndef STAND_ALONE
|
||||
#include "aklock.h"
|
||||
#endif /* STAND_ALONE */
|
||||
/*------------------------------------------------------------------*\
|
||||
\*------------------------------------------------------------------*/
|
||||
|
||||
enum {
|
||||
SHA1_BLOCK_LENGTH = 64,
|
||||
SHA1_DIGEST_LENGTH = 20,
|
||||
SHA1_DIGEST_STRING_LENGTH = (SHA1_DIGEST_LENGTH * 2 + 1)
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
u_int32_t state[5];
|
||||
u_int64_t count;
|
||||
u_int8_t buffer[SHA1_BLOCK_LENGTH];
|
||||
} sha1Context;
|
||||
|
||||
|
||||
void sha1_init(sha1Context *);
|
||||
void sha1_pad(sha1Context *);
|
||||
void sha1_transform(u_int32_t [5], const u_int8_t [SHA1_BLOCK_LENGTH]);
|
||||
void sha1_update(sha1Context *, const u_int8_t *, size_t);
|
||||
void sha1_final(u_int8_t [SHA1_DIGEST_LENGTH], sha1Context *);
|
||||
|
||||
#define HTONDIGEST(x) do { \
|
||||
x[0] = htonl(x[0]); \
|
||||
x[1] = htonl(x[1]); \
|
||||
x[2] = htonl(x[2]); \
|
||||
x[3] = htonl(x[3]); \
|
||||
x[4] = htonl(x[4]); } while (0)
|
||||
|
||||
#define NTOHDIGEST(x) do { \
|
||||
x[0] = ntohl(x[0]); \
|
||||
x[1] = ntohl(x[1]); \
|
||||
x[2] = ntohl(x[2]); \
|
||||
x[3] = ntohl(x[3]); \
|
||||
x[4] = ntohl(x[4]); } while (0)
|
||||
|
||||
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
|
||||
|
||||
/*------------------------------------------------------------------*\
|
||||
blk0() and blk() perform the initial expand.
|
||||
I got the idea of expanding during the round function from SSLeay
|
||||
\*------------------------------------------------------------------*/
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
|
||||
|(rol(block->l[i],8)&0x00FF00FF))
|
||||
#else
|
||||
# define blk0(i) block->l[i]
|
||||
#endif /* LITTLE_ENDIAN */
|
||||
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
|
||||
^block->l[(i+2)&15]^block->l[i&15],1))
|
||||
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*\
|
||||
(R0+R1), R2, R3, R4 are the different operations (rounds) used
|
||||
in SHA1
|
||||
\*------------------------------------------------------------------*/
|
||||
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
|
||||
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
|
||||
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
|
||||
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
|
||||
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
|
||||
|
||||
/*------------------------------------------------------------------*\
|
||||
Hash a single 512-bit block. This is the core of the algorithm.
|
||||
\*------------------------------------------------------------------*/
|
||||
void sha1_transform(u_int32_t state[5], const u_int8_t buffer[SHA1_BLOCK_LENGTH]) {
|
||||
u_int32_t a, b, c, d, e;
|
||||
u_int8_t workspace[SHA1_BLOCK_LENGTH];
|
||||
typedef union {
|
||||
u_int8_t c[64];
|
||||
u_int32_t l[16];
|
||||
} CHAR64LONG16;
|
||||
CHAR64LONG16 *block = (CHAR64LONG16 *)workspace;
|
||||
|
||||
(void)memcpy(block, buffer, SHA1_BLOCK_LENGTH);
|
||||
|
||||
/* Copy context->state[] to working vars */
|
||||
a = state[0];
|
||||
b = state[1];
|
||||
c = state[2];
|
||||
d = state[3];
|
||||
e = state[4];
|
||||
|
||||
/* 4 rounds of 20 operations each. Loop unrolled. */
|
||||
R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
|
||||
R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
|
||||
R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
|
||||
R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
|
||||
R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
|
||||
R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
|
||||
R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
|
||||
R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
|
||||
R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
|
||||
R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
|
||||
R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
|
||||
R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
|
||||
R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
|
||||
R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
|
||||
R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
|
||||
R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
|
||||
R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
|
||||
R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
|
||||
R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
|
||||
R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
|
||||
|
||||
/* Add the working vars back into context.state[] */
|
||||
state[0] += a;
|
||||
state[1] += b;
|
||||
state[2] += c;
|
||||
state[3] += d;
|
||||
state[4] += e;
|
||||
|
||||
/* Wipe variables */
|
||||
a = b = c = d = e = 0;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*\
|
||||
Initialize new context
|
||||
\*------------------------------------------------------------------*/
|
||||
void sha1_init(sha1Context *context) {
|
||||
|
||||
/* SHA1 initialization constants */
|
||||
context->count = 0;
|
||||
context->state[0] = 0x67452301;
|
||||
context->state[1] = 0xEFCDAB89;
|
||||
context->state[2] = 0x98BADCFE;
|
||||
context->state[3] = 0x10325476;
|
||||
context->state[4] = 0xC3D2E1F0;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*\
|
||||
Run your data through this.
|
||||
\*------------------------------------------------------------------*/
|
||||
void sha1_update(sha1Context *context, const u_int8_t *data, size_t len) {
|
||||
|
||||
size_t i, j;
|
||||
|
||||
j = (size_t)((context->count >> 3) & 63);
|
||||
context->count += (len << 3);
|
||||
if ((j + len) > 63) {
|
||||
(void)memcpy(&context->buffer[j], data, (i = 64-j));
|
||||
sha1_transform(context->state, context->buffer);
|
||||
for ( ; i + 63 < len; i += 64)
|
||||
sha1_transform(context->state, (u_int8_t *)&data[i]);
|
||||
j = 0;
|
||||
} else {
|
||||
i = 0;
|
||||
}
|
||||
(void)memcpy(&context->buffer[j], &data[i], len - i);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*\
|
||||
Add padding and return the message digest.
|
||||
\*------------------------------------------------------------------*/
|
||||
void sha1_pad(sha1Context *context) {
|
||||
|
||||
u_int8_t finalcount[8];
|
||||
u_int i;
|
||||
|
||||
for (i = 0; i < 8; i++) {
|
||||
finalcount[i] = (u_int8_t)((context->count >>
|
||||
((7 - (i & 7)) * 8)) & 255); /* Endian independent */
|
||||
}
|
||||
sha1_update(context, (u_int8_t *)"\200", 1);
|
||||
while ((context->count & 504) != 448)
|
||||
sha1_update(context, (u_int8_t *)"\0", 1);
|
||||
sha1_update(context, finalcount, 8); /* rsShould cause a sha1_transform() */
|
||||
}
|
||||
|
||||
void sha1_final(u_int8_t digest[SHA1_DIGEST_LENGTH], sha1Context *context) {
|
||||
|
||||
u_int i;
|
||||
|
||||
sha1_pad(context);
|
||||
if (digest) {
|
||||
for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
|
||||
digest[i] = (u_int8_t)
|
||||
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
|
||||
}
|
||||
memset(context, 0, sizeof(*context));
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *\
|
||||
\* ---------------------------------------------------------------- */
|
||||
#ifndef STAND_ALONE
|
||||
|
||||
static const char* userhash = NULL;
|
||||
|
||||
static int init(const char* args) {
|
||||
if (args) {
|
||||
char* check = strstr(args, "sha1:");
|
||||
if (!check || check != args) {
|
||||
fprintf(stderr, "aklock: error, missing arguments for [sha1].\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strlen(&args[5]) != SHA1_DIGEST_STRING_LENGTH - 1) {
|
||||
fprintf(stderr, "aklock: error, invalid sha1-hash.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
userhash = &args[5];
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int deinit() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int auth(const char* pass) {
|
||||
|
||||
unsigned char digest[SHA1_DIGEST_LENGTH];
|
||||
unsigned char stringdigest[SHA1_DIGEST_STRING_LENGTH];
|
||||
unsigned int i;
|
||||
sha1Context sha1;
|
||||
|
||||
if (!pass || !userhash)
|
||||
return 0;
|
||||
|
||||
sha1_init(&sha1);
|
||||
sha1_update(&sha1, (unsigned char*)pass, strlen(pass));
|
||||
sha1_final(digest, &sha1);
|
||||
|
||||
memset(stringdigest, 0, SHA1_DIGEST_STRING_LENGTH);
|
||||
for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
|
||||
sprintf(&stringdigest[i*2], "%02x", digest[i]);
|
||||
}
|
||||
|
||||
return !strcmp(stringdigest, userhash);
|
||||
}
|
||||
|
||||
struct akAuth aklock_auth_sha1 = {
|
||||
"sha1",
|
||||
init,
|
||||
auth,
|
||||
deinit
|
||||
};
|
||||
|
||||
/* ---------------------------------------------------------------- *\
|
||||
\* ---------------------------------------------------------------- */
|
||||
#else
|
||||
|
||||
int main() {
|
||||
|
||||
unsigned char digest[SHA1_DIGEST_LENGTH];
|
||||
unsigned int i;
|
||||
unsigned char c;
|
||||
sha1Context sha1;
|
||||
|
||||
sha1_init(&sha1);
|
||||
while((c = fgetc(stdin)) != 255) {
|
||||
sha1_update(&sha1, &c, 1);
|
||||
}
|
||||
sha1_final(digest, &sha1);
|
||||
|
||||
printf("\n");
|
||||
for(i = 0; i < SHA1_DIGEST_LENGTH; ++i)
|
||||
printf("%02x", digest[i]);
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* STAND_ALONE */
|
||||
|
||||
/* ---------------------------------------------------------------- *\
|
||||
\* ---------------------------------------------------------------- */
|
||||
|
Reference in a new issue