linux/crypto/md4.c
<<
>>
Prefs
   1/* 
   2 * Cryptographic API.
   3 *
   4 * MD4 Message Digest Algorithm (RFC1320).
   5 *
   6 * Implementation derived from Andrew Tridgell and Steve French's
   7 * CIFS MD4 implementation, and the cryptoapi implementation
   8 * originally based on the public domain implementation written
   9 * by Colin Plumb in 1993.
  10 *
  11 * Copyright (c) Andrew Tridgell 1997-1998.
  12 * Modified by Steve French (sfrench@us.ibm.com) 2002
  13 * Copyright (c) Cryptoapi developers.
  14 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
  15 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  16 *
  17 * This program is free software; you can redistribute it and/or modify
  18 * it under the terms of the GNU General Public License as published by
  19 * the Free Software Foundation; either version 2 of the License, or
  20 * (at your option) any later version.
  21 *
  22 */
  23#include <crypto/internal/hash.h>
  24#include <linux/init.h>
  25#include <linux/kernel.h>
  26#include <linux/module.h>
  27#include <linux/string.h>
  28#include <linux/types.h>
  29#include <asm/byteorder.h>
  30
  31#define MD4_DIGEST_SIZE         16
  32#define MD4_HMAC_BLOCK_SIZE     64
  33#define MD4_BLOCK_WORDS         16
  34#define MD4_HASH_WORDS          4
  35
  36struct md4_ctx {
  37        u32 hash[MD4_HASH_WORDS];
  38        u32 block[MD4_BLOCK_WORDS];
  39        u64 byte_count;
  40};
  41
  42static inline u32 lshift(u32 x, unsigned int s)
  43{
  44        x &= 0xFFFFFFFF;
  45        return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
  46}
  47
  48static inline u32 F(u32 x, u32 y, u32 z)
  49{
  50        return (x & y) | ((~x) & z);
  51}
  52
  53static inline u32 G(u32 x, u32 y, u32 z)
  54{
  55        return (x & y) | (x & z) | (y & z);
  56}
  57
  58static inline u32 H(u32 x, u32 y, u32 z)
  59{
  60        return x ^ y ^ z;
  61}
  62
  63#define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
  64#define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (u32)0x5A827999,s))
  65#define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (u32)0x6ED9EBA1,s))
  66
  67/* XXX: this stuff can be optimized */
  68static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
  69{
  70        while (words--) {
  71                __le32_to_cpus(buf);
  72                buf++;
  73        }
  74}
  75
  76static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
  77{
  78        while (words--) {
  79                __cpu_to_le32s(buf);
  80                buf++;
  81        }
  82}
  83
  84static void md4_transform(u32 *hash, u32 const *in)
  85{
  86        u32 a, b, c, d;
  87
  88        a = hash[0];
  89        b = hash[1];
  90        c = hash[2];
  91        d = hash[3];
  92
  93        ROUND1(a, b, c, d, in[0], 3);
  94        ROUND1(d, a, b, c, in[1], 7);
  95        ROUND1(c, d, a, b, in[2], 11);
  96        ROUND1(b, c, d, a, in[3], 19);
  97        ROUND1(a, b, c, d, in[4], 3);
  98        ROUND1(d, a, b, c, in[5], 7);
  99        ROUND1(c, d, a, b, in[6], 11);
 100        ROUND1(b, c, d, a, in[7], 19);
 101        ROUND1(a, b, c, d, in[8], 3);
 102        ROUND1(d, a, b, c, in[9], 7);
  93        ROUND1(c, d, a, b, in[6], 11);
  94        ROUND1(b, c, d, a, in[7], 19);
  95        ROUND1(a, b, c, d, in[8], 3);
  96        ROUND1(d, a, b, c, in[9], 7);
  97        ROUND1(c, d, a, b, in[6], 11);
  98        ROUND1(b, c, d, a, in[7], 19);
9 92
        ROUND2(a, b, c, d, in[0], 3);
        ROUND2(d, a, b, c, in[65, 3);
        ROUND2(c, d, a, b, in[8, 19);
        ROUND2(b, c, d, a, in[2]], 3);
        ROUND2(a, b, c, d, in[1], 3);
        ROUND2(d, a, b, c, in[65, 3);
        ROUND2(c, d, a, b, in[8, 19);
        ROUND2(b, c, d, a, in[2]], 3);
        ROUND2(a, b, c, d, in[8], 3);
        ROUND2(d, a, b, c, in[65, 3);
ROUND2(c, d, a, b, in[6, 19);
ROUND2(b, c, d, a, in[2]], 3);
ROUND2(a, b, c, d, in[8], 3);
ROUND2(d, a, b, c, in[65, 3);
ROUND2(c, d, a, b, in[6, 19);
ROUND2(b, c, d, a, in[2]], 3);
ROUND3(a, b, c, d, in[0], 3);
ROUND3(d, a, b, c, in[8, 19);
ROUND3(c, d, a, b, in[6], 11);
c, ROUND2(c, c, d, a, in[2]], 3);
ROUND2(a, b, c, d, in[0], 3);
 fine d, a, b, c, in[8, 19);
ROUND2(c, d, a, b, in[6], 11);
 100 fine ROUND2(c, c, d, a, in[2]], 3);
ROUND2(a, b, c, d, in[0], 3);
 uct d, a, b, c, in[8, 19);
      132 c, d, a, b, in[6], 11);
 100 f      132 c, c, d, a, in[2]], 3);
164 a, b, c, d, in[0], 3);
ROUND2(c, a, b, c, in[8, 19);
ROUND2(a, d, a, b, in[6], 11);
 100 tic ROUND2(d, c, d, a, in[2]], 3);
  84sta1     x<1a> &= 0xFFFFFFFF;
in[2]], 3ine" name1="L2412  94 a      ROUND2(c, ROUND2(d,    87
inline" name="L83">  83
      retu1rn (xe void cpu_to_le32_arraf">u32 *<_helperu_to_le32_arrayf">u32 *<_helperef="+ctx {
  85{
<1a href="c1rypto/md4.c#L52" id="L521" cla1s="line" name="L86">  86   1 href="cr1ypto/md4.c#L53" id="L53"1 clas15>ROUND2(u32 *MD);
MDine" name="L46">  46}
<1tic ROUND2(u32 *u32 const *MDie" name="L46">  46}
<1t      75
  76sta1a href="c1rypto/md4.c#L57" id="L571" cla15inline v)
u5" cu32" class="sref">u5" cef="+ctx {
d,  c_dess="lin="crypto/md4.c#Ldessref">d,   85{
<1a      78   1tic   {
 c_dess/md4.c#L37" id="L37sa> c_dess/md4ef="+code=d" class="sessref">d,   46}
<1t href="c1rypto/md4.c#L60" id="L601" cla15s="line" name=="L1011 100      retu1rn u32 constlin " cl67452301e" name="L46">  46}
<1a href="c1rypto/md4.c#L62" id="L621" cla16>ROUND2(u32 constl1n " clefcdab89e" name="L46">  46}
<1ahref="cr1ypto/md4.c#L63" id="L63"1 clas16>ROUND2(u32 constl2n " cl98badcfee" name="L46">  46}
<1aic u32 constl3n " cl10325476e" name="L46">  46}
<1a      46}
<1a    retu1ref="+code=ROUND3" class1="sre16"line" name="L76">  76sta1 href="cr1ypto/md4.c#L67" id="L67"1 clas16>ROUND2(< href="ce" name="L46">  46}
<1a    /* XXX: this s1tuff 1an be e" name="L75">  75
inlin" name="L75">  75
uupdatpu_to_le32_arrayf">uupdatpef="+ctx {
d,  c_dess="lin="crypto/md4.c#Ldessref">d, d, in[2dat"="li,)
  85{
<1     whil1e (  78   1         1     c_dess/md4.c#L37" id="L37sa> c_dess/md4ef="+code=d" class="sessref">d,   46}
<1         1    bufa href"crypto/md4.c#Lu="sref">a, in[2]vail="lin
MDi -) {
x  46}
<1 ic   84sta1a href="c1rypto/md4.c#L75" id="L751" cla17> &= 0xFFFFFFFF;
  46}
<1     retu1ypto/md4.c#L76" id="L76"1 clas1="line" name="L76">  76sta1tic ROUND2(in[2]vail="lin);
  79   1a href="c1rypto/md4.c#L78" id="L781" cla172 MDED9Esizeof+code=d" class=" md4.c#L37" id="L37"ass="li-class href="crypto/=BLOCK_WORDS" class="sref">MDi -)+code=lshift" clvailsref">in[2]vail="li)," name="L79">  79   1aic words<in[2dat"="li,< href="crypto/mle/md4.1c#L16" id=le/="liee" name="L46">  46}
<1  href="c1    __cp href="ce" name="L46">  46}
<1         1    bufe" name="L75">  75
  42sta1a href="c1rypto/md4.c#L83" id="L831" cla18>ROUND2(MDED9Esizeof+code=d" class=" md4.c#L37" id="L37"ass="li-class href="crypto/=BLOCK_WORDS" class="sref">MDi -)+code=lshift" clvailsref">in[2]vail="li)," name="L79">  79   1 href="cr1ypto/md4.c#L84" id="L84"1 clas183s" class="sref">__cFFFFFF;
in[2dat"="li,< href="crypto/mlvailsref">in[2]vail="li)e" name="L46">  46}
<1  href="c1  46}
<1     retu1rypto/md4.c#L86" id="L861" cla18>ROUND2(u32 *<_helperu_to_le32_arrayf">u32 *<_helperef="+code=d" class=" md4.c#L37" id="L37"ass="li)e" name="L46">  46}
<1 ic 132 in[2dat"="li +
in[2]vail="lie" name="L46">  46}
<1  href="c1ypto/md4.c#L88" id="L88"1 clas182 in[2]vail="lie" name="L46">  46}
<1 ic a<18inlin" name="L75">  75
b<1a> = MDiene" name="L79">  79   1     c<19sref">buf++;
MD"+code=a" class="sat"sref">in[2dat"="li,MDiee" name="L46">  46}
<1     d<1a> = u32 *<_helperu_to_le32_arrayf">u32 *<_helperef="+code=d" class=" md4.c#L37" id="L37"ass="li)e" name="L46">  46}
<1 href="cr1ypto/md4.c#L93" id="L93"1 clas19sref">buf++;
in[2dat"="li +
MDie" name="L46">  46}
<1 href="cr1ref="+code=ROUND1" class1="sre193s" class="sref">__cf="+code=hash" cle/md4.1c#L16" id=le/="li -
MDie" name="L46">  46}
<1  href="c1ref="+code=ROUND1" class1="sre1">ROUND1(  75
  76sta1     ROUND1(MD"+code=a" class="sat"sref">in[2dat"="li,< href="crypto/mle/md4.1c#L16" id=le/="liee" name="L46">  46}
<1       88   1     ROUND1(< href="ce" name="L46">  46}
<1     ROUNe" name="L75">  75
ROUN" name="L75">  75
ROUNe v)
ufinalsref">in[2f">ufinalef="+ctx {
d,  c_dess="lin="crypto/md4.c#Ldessref">d, d,   85{
<2       ROUNe" name="L79">  79   2       ROUND1( {
 c_dess/md4.c#L37" id="L37sa> c_dess/md4ef="+code=d" class="sessref">d,   46}
<2       ROUND1()
x  46}
<2 5     ROUND1(MDED9 href="crypto/moffsemd4.c#L40" id="Loffsem="lie" name="L46">  46}
<2 6     ROUND1(<)
 1002         88   2       ROUND1(<*s href="crypto/pd4.c#L40" id="Lp="li++ " cl8ce" name="L46">  46}
<2 =   79   2       buf++;
,  &amiee" name="L46">  46}
<2       (u32 *<_helperu_to_le32_arrayf">u32 *<_helperef="+code=d" class=" md4.c#L37" id="L37"ass="li)e" name="L46">  46}
<2       buf++;
MDe" name="L46">  46}
<2       __cf="+code=hash" cpaddingd4.c#L40" id="Lpadding="lin
<56e" name="L46">  46}
<2         75
  76sta2       ROUND2(  46}
<2       ROUND2(MD c4an
  46}
<2       ROUND2(MD c5an
  46}
<2       ROUND2(u32 *MD"+Esizeof+code=d" class=" md4.c#L37" id="L37"ass="li-class href="crypto/=BLOCK_WORDS" class="sref">MDi -" name="L46">  46}
<2       buf++;
,  &amie /hsizeof+code=d" class="o="sref">a,   46}
<2       ROUND2(u32 *u32 const,<"crypto/md4.c#L md4.c#L37" id="L37"ass="li-class href="crypto/=BLOCK_WORDS" class="sref">MDie" name="L46">  46}
<2       ROUND2(u32 *u32 const,<"crypto/md4.c#LARRAY64
u32 constiee" name="L46">  46}
<2       ROUND2(u32 const,u32 constiee" name="L46">  46}
<2       ROUND2(  46}
<2         76sta2ROUND2(< href="ce" name="L46">  46}
<2         75
  75
 vctx {
  79   2c, ROUND2(<.code=d" class="sigestsizeref">d, (  79   2c      ROUND2(<.code=d" class="5" cu32" class="sre5" cef="hhhhhhhhhhh=2(u5" cu32" class="sref">u5" cef="," name="L79">  79   2c      ROUND2(<.code=d" class="updatpu_to_le32_arrayupdatpef="hhhhhhhhh=2(uupdatpu_to_le32_arrayf">uupdatpef="," name="L79">  79   2c      ROUND2(<.code=d" class="finalsref">in[2finalef="hhhhhhhhhh=2(ufinalsref">in[2f">ufinalef="," name="L79">  79   2c      ROUND2(<.code=d" class="sescsizeref">d, (=2( {
  79   2f      ROUND2(<.code=d" class="baseref">d,   79   2ca href="2ef="+code=md4_ctx" class2="sre23>ROUND2((<.code=d" class="cra_" clode=u32" class="ra_" clROUND2(=2(<"  79   2c      232   79   2c      232 d,  <=2(  79   2c      232s" class="sref">__cp.code=d" class="cra_moduleref">d, (  79   2bufe" name="L75">  75
ROUN}e" name="L46">  46}
<2tic ROUN" name="L46">  46}
<2t       v)
umodu5" cef="+ef">e" name="L85">  85{
<2     x<2a> &ame" name="L79">  79   2a      ROUND2(< href="code=d" class="cr>  7_register_s class="sref">u32  7_register_s claef="+ef">x {
  46}
<2act ROUNe" name="L75">  75
  88   2tic inline vef">cpu_to_le32_arra__ex cu32" class="sre_uex cef="> {
umodufinief="+ef">e" name="L85">  85{
<2         79   2     retu2rn (  7_unregister_s class="sref">u32  7_unregister_s claef="+ef">x {
  46}
<2a href="c2rypto/md4.c#L52" id="L522" cla2s="line" name="L75">  75
ROUN" name="L46">  46}
<2tic ROUN {
umodu5" cef="ee" name="L46">  46}
<2a    umodufinief="ee" name="L46">  46}
<2a        76sta2a href="c2rypto/md4.c#L57" id="L572" cla25inlina href="+code=cMODULa_LICENSa href="crypto/mMODULa_LICENSaef="+cspande=u32" ctringd>"GPL"  46}
<2a      "MD4 Message Digest Algorithm"  46}
<2aic   75

The original LXR software by the  name="L7http://sourceforge.net/projects/lxr">LXR commu" cy="li, this experimental version by  name="L7mailto:lxr@#L6ux.no">lxr@#L6ux.no="li.
lxr.#L6ux.no kindly hosted by name="L7http://www.redpill-#L6pro.no">Redpill LL6pro AS="li, provider of LL6ux hr servicer sincee1">5.