Anope IRC Services  Version 1.8
enc_sha1.c
Go to the documentation of this file.
1 /*
2 SHA-1 in C
3 By Steve Reid <steve@edmweb.com>
4 100% Public Domain
5 
6 Test Vectors (from FIPS PUB 180-1)
7 "abc"
8  A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
9 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
10  84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
11 A million repetitions of "a"
12  34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
13 */
14 
15 /* #define LITTLE_ENDIAN * This should be #define'd if true. */
16 /* #define SHA1HANDSOFF * Copies data before messing with it. */
17 
18 #include "module.h"
19 #include <stdio.h>
20 #include <string.h>
21 
22 typedef struct {
23  uint32 state[5];
24  uint32 count[2];
25  unsigned char buffer[64];
26 } SHA1_CTX;
27 
28 static void SHA1Transform(uint32 state[5], unsigned char const buffer[64]);
29 static void SHA1Init(SHA1_CTX* context);
30 static void SHA1Update(SHA1_CTX* context, unsigned char const * data, uint32 len);
31 static void SHA1Final(unsigned char digest[20], SHA1_CTX* context);
32 
33 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
34 
35 /* blk0() and blk() perform the initial expand. */
36 /* I got the idea of expanding during the round function from SSLeay */
37 #ifdef LITTLE_ENDIAN
38 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
39  |(rol(block->l[i],8)&0x00FF00FF))
40 #else
41 #define blk0(i) block->l[i]
42 #endif
43 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
44  ^block->l[(i+2)&15]^block->l[i&15],1))
45 
46 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
47 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
48 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
49 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
50 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
51 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
52 
53 
54 /* Hash a single 512-bit block. This is the core of the algorithm. */
55 
56 static void SHA1Transform(uint32 state[5], unsigned char const buffer[64])
57 {
58 uint32 a, b, c, d, e;
59 typedef union {
60  unsigned char c[64];
61  uint32 l[16];
62 } CHAR64LONG16;
63 CHAR64LONG16* block;
64 #ifdef SHA1HANDSOFF
65 static unsigned char workspace[64];
66  block = (CHAR64LONG16*)workspace;
67  memcpy(block, buffer, 64);
68 #else
69  block = (CHAR64LONG16*)buffer;
70 #endif
71  /* Copy context->state[] to working vars */
72  a = state[0];
73  b = state[1];
74  c = state[2];
75  d = state[3];
76  e = state[4];
77  /* 4 rounds of 20 operations each. Loop unrolled. */
78  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);
79  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);
80  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);
81  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);
82  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);
83  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);
84  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);
85  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);
86  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);
87  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);
88  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);
89  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);
90  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);
91  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);
92  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);
93  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);
94  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);
95  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);
96  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);
97  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);
98  /* Add the working vars back into context.state[] */
99  state[0] += a;
100  state[1] += b;
101  state[2] += c;
102  state[3] += d;
103  state[4] += e;
104  /* Wipe variables */
105  a = b = c = d = e = 0;
106 }
107 
108 
109 /* SHA1Init - Initialize new context */
110 
111 static void SHA1Init(SHA1_CTX* context)
112 {
113  /* SHA1 initialization constants */
114  context->state[0] = 0x67452301;
115  context->state[1] = 0xEFCDAB89;
116  context->state[2] = 0x98BADCFE;
117  context->state[3] = 0x10325476;
118  context->state[4] = 0xC3D2E1F0;
119  context->count[0] = context->count[1] = 0;
120 }
121 
122 
123 /* Run your data through this. */
124 
125 static void SHA1Update(SHA1_CTX* context, unsigned char const * data, uint32 len)
126 {
127  uint32 i, j;
128 
129  j = (context->count[0] >> 3) & 63;
130  if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
131  context->count[1] += (len >> 29);
132  if ((j + len) > 63) {
133  memcpy(&context->buffer[j], data, (i = 64-j));
134  SHA1Transform(context->state, context->buffer);
135  for ( ; i + 63 < len; i += 64) {
136  SHA1Transform(context->state, &data[i]);
137  }
138  j = 0;
139  }
140  else i = 0;
141  memcpy(&context->buffer[j], &data[i], len - i);
142 }
143 
144 
145 /* Add padding and return the message digest. */
146 
147 static void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
148 {
149  uint32 i;
150  unsigned char finalcount[8];
151 
152  for (i = 0; i < 8; i++) {
153  finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
154  >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
155  }
156  SHA1Update(context, (unsigned char *)"\200", 1);
157  while ((context->count[0] & 504) != 448) {
158  SHA1Update(context, (unsigned char *)"\0", 1);
159  }
160  SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
161  for (i = 0; i < 20; i++) {
162  digest[i] = (unsigned char)
163  ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
164  }
165  /* Wipe variables */
166  i = 0;
167  memset(context->buffer, 0, 64);
168  memset(context->state, 0, 20);
169  memset(context->count, 0, 8);
170  memset(&finalcount, 0, 8);
171 #ifdef SHA1HANDSOFF /* make SHA1Transform overwrite it's own static vars */
172  SHA1Transform(context->state, context->buffer);
173 #endif
174 }
175 
176 /*****************************************************************************/
177 
178 static int sha1_encrypt(const char *src, int len, char *dest, int size)
179 {
180  SHA1_CTX context;
181  char tmp[41];
182 
183  if (size < 20)
184  return -1;
185 
186  memset(dest,0,size);
187 
188  SHA1Init(&context);
189  SHA1Update(&context, (const unsigned char *) src, len);
190  SHA1Final((unsigned char *) dest, &context);
191 
192  if(debug) {
193  memset(tmp,0,41);
194  binary_to_hex((unsigned char *) dest,tmp,20);
195  /* Dont log source if we were encrypting in place :) */
196  if (memcmp(src, dest, 20) != 0) {
197  alog("enc_sha1: hashed from [%s] to [%s]",src,tmp);
198  } else {
199  alog("enc_sha1: hashed password to [%s]",tmp);
200  }
201  }
202 
203  return 0;
204 }
205 
206 
207 static int sha1_encrypt_check_len(int passlen, int bufsize)
208 {
209  if (bufsize < 20)
210  fatal("enc_sha1: sha1_check_len(): buffer too small (%d)", bufsize);
211  return 0;
212 }
213 
214 
215 static int sha1_decrypt(const char *src, char *dest, int size)
216 {
217  return 0;
218 }
219 
220 
221 static int sha1_check_password(const char *plaintext, const char *password)
222 {
223  char buf[BUFSIZE];
224 
225  if (sha1_encrypt(plaintext, strlen(plaintext), buf, sizeof(buf)) < 0)
226  return -1;
227  if (memcmp(buf, password, 20) == 0)
228  return 1;
229 
230  return 0;
231 }
232 
233 /*************************************************************************/
234 
235 /* Module stuff. */
236 
237 int AnopeInit(int argc, char **argv) {
238 
239  moduleAddAuthor("Anope");
240  moduleAddVersion(VERSION_STRING);
242 
247 
248  return MOD_CONT;
249 }
250 
251 void AnopeFini(void) {
252  encmodule_encrypt(NULL);
254  encmodule_decrypt(NULL);
256 }
257 
258 /* EOF */
259 
#define R1(v, w, x, y, z, i)
Definition: enc_sha1.c:48
E void encmodule_encrypt_check_len(int(*func)(int passlen, int bufsize))
Definition: encrypt.c:26
#define R4(v, w, x, y, z, i)
Definition: enc_sha1.c:51
#define R2(v, w, x, y, z, i)
Definition: enc_sha1.c:49
static int sha1_decrypt(const char *src, char *dest, int size)
Definition: enc_sha1.c:215
E void encmodule_encrypt(int(*func)(const char *src, int len, char *dest, int size))
Definition: encrypt.c:20
E void encmodule_decrypt(int(*func)(const char *src, char *dest, int size))
Definition: encrypt.c:31
E void encmodule_check_password(int(*func)(const char *plaintext, const char *password))
Definition: encrypt.c:36
static int sha1_encrypt(const char *src, int len, char *dest, int size)
Definition: enc_sha1.c:178
MDE void moduleAddAuthor(const char *author)
Definition: modules.c:1772
#define R3(v, w, x, y, z, i)
Definition: enc_sha1.c:50
MDE void moduleSetType(MODType type)
Definition: modules.c:818
void AnopeFini(void)
Definition: enc_sha1.c:251
E void E void E void fatal(const char *fmt,...) FORMAT(printf
static int sha1_check_password(const char *plaintext, const char *password)
Definition: enc_sha1.c:221
uint32 state[5]
Definition: enc_sha1.c:23
MDE void moduleAddVersion(const char *version)
Definition: modules.c:1760
Command * c
Definition: ns_recover.c:17
u_int32_t uint32
Definition: db-merger.c:123
E void alog(const char *fmt,...) FORMAT(printf
#define MOD_CONT
Definition: modules.h:54
static void SHA1Init(SHA1_CTX *context)
Definition: enc_sha1.c:111
E int debug
Definition: extern.h:775
static void SHA1Final(unsigned char digest[20], SHA1_CTX *context)
Definition: enc_sha1.c:147
uint32 count[2]
Definition: enc_sha1.c:24
int AnopeInit(int argc, char **argv)
Definition: enc_sha1.c:237
E void binary_to_hex(unsigned char *bin, char *hex, int length)
Definition: misc.c:67
#define R0(v, w, x, y, z, i)
Definition: enc_sha1.c:47
static void SHA1Transform(uint32 state[5], unsigned char const buffer[64])
Definition: enc_sha1.c:56
static int sha1_encrypt_check_len(int passlen, int bufsize)
Definition: enc_sha1.c:207
#define BUFSIZE
Definition: config.h:47
static void SHA1Update(SHA1_CTX *context, unsigned char const *data, uint32 len)
Definition: enc_sha1.c:125
unsigned char buffer[64]
Definition: enc_sha1.c:25