OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uC-libc/] [misc/] [crypt.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
#include <features.h>
2
#include <stdlib.h>
3
/* TEA based crypt(), version 0.0 <ndf@linux.mit.edu>
4
 * It looks like there are problems with key bits carrying through
5
 * to the encryted data, and I want to get rid of that libc call..
6
 * This is just so rob could see it ;) */
7
char *
8
crypt(const char * key, const char * salt)
9
{
10
  /* n is the number of rounds, delta is a golden # derivative,
11
   k is the key, v is the data to be encrypted. */
12
  unsigned long v[2], sum=0, delta=0x9e3779b9, n=64, k[4];
13
  static char rkey[16];
14
  unsigned char i;
15
 
16
  /* Our constant string will be a string of zeros .. */
17
  v[0]=v[1]=k[0]=k[1]=k[2]=k[3]=0;
18
  for(i=0;i<16;i++) rkey[i]=0;
19
  rkey[0]=*salt;
20
  rkey[1]=salt[1];
21
  for (i=0;key[i];i++) rkey[i+1]=key[i];
22
  memcpy(k, rkey, 4*sizeof(long));
23
 
24
  while (n-->0)  {
25
    sum += delta;
26
    v[0] += (v[1]<<4)+k[0] ^ v[1]+sum ^ (v[1]>>5)+k[1];
27
    v[1] += (v[0]<<4)+k[2] ^ v[0]+sum ^ (v[0]>>5)+k[3];
28
  }
29
 
30
  *rkey=*salt; rkey[1]=salt[1];
31
 
32
  /* Now we need to unpack the bits and map it to "A-Za-z0-9./" for printing
33
     in /etc/passwd */
34
  for (i=2;i<13;i++)
35
    {
36
      /* This unpacks the 6 bit data, each cluster into its own byte */
37
      if (i==8) v[0]|=v[1]>>28;
38
      rkey[i]=v[(i-2)/6]&0x3F;
39
      v[(i-2)/6]>>=6;
40
 
41
      /* Now we map to the proper chars */
42
      if (rkey[i]>=0 && rkey[i]<12) rkey[i]+=46;
43
      else if (rkey[i]>11 && rkey[i]<38) rkey[i]+=53;
44
      else if (rkey[i]>37 && rkey[i]<64) rkey[i]+=59;
45
      else return NULL;
46
    }
47
 
48
  rkey[13]='\0';
49
  return rkey;
50
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.