Anope IRC Services  Version 2.0
enc_sha1.cpp
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 
17 #include "module.h"
18 #include "modules/encryption.h"
19 
21 {
22  unsigned char c[64];
23  uint32_t l[16];
24 };
25 
26 inline static uint32_t rol(uint32_t value, uint32_t bits) { return (value << bits) | (value >> (32 - bits)); }
27 
28 /* blk0() and blk() perform the initial expand. */
29 /* I got the idea of expanding during the round function from SSLeay */
30 inline static uint32_t blk0(CHAR64LONG16 &block, uint32_t i)
31 {
32 #ifdef LITTLE_ENDIAN
33  return block.l[i] = (rol(block.l[i], 24) & 0xFF00FF00) | (rol(block.l[i], 8) & 0x00FF00FF);
34 #else
35  return block.l[i];
36 #endif
37 }
38 inline static uint32_t blk(CHAR64LONG16 &block, uint32_t i) { return 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); }
39 
40 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
41 inline static void R0(CHAR64LONG16 &block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i) { z += ((w & (x ^ y)) ^ y) + blk0(block, i) + 0x5A827999 + rol(v, 5); w = rol(w, 30); }
42 inline static void R1(CHAR64LONG16 &block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i) { z += ((w & (x ^ y)) ^ y) + blk(block, i) + 0x5A827999 + rol(v, 5); w = rol(w, 30); }
43 inline static void R2(CHAR64LONG16 &block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i) { z += (w ^ x ^ y) + blk(block, i) + 0x6ED9EBA1 + rol(v, 5); w = rol(w, 30); }
44 inline static void R3(CHAR64LONG16 &block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i) { z += (((w | x) & y) | (w & x)) + blk(block, i) + 0x8F1BBCDC + rol(v, 5); w = rol(w, 30); }
45 inline static void R4(CHAR64LONG16 &block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i) { z += (w ^ x ^ y) + blk(block, i) + 0xCA62C1D6 + rol(v, 5); w = rol(w, 30); }
46 
47 static const uint32_t sha1_iv[5] =
48 {
49  0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0
50 };
51 
53 {
54  uint32_t state[5];
55  uint32_t count[2];
56  unsigned char buffer[64];
57  unsigned char digest[20];
58 
59  void Transform(const unsigned char buf[64])
60  {
61  uint32_t a, b, c, d, e;
62 
63  CHAR64LONG16 block;
64  memcpy(block.c, buf, 64);
65 
66  /* Copy context->state[] to working vars */
67  a = this->state[0];
68  b = this->state[1];
69  c = this->state[2];
70  d = this->state[3];
71  e = this->state[4];
72 
73  /* 4 rounds of 20 operations each. Loop unrolled. */
74  R0(block, a, b, c, d, e, 0); R0(block, e, a, b, c, d, 1); R0(block, d, e, a, b, c, 2); R0(block, c, d, e, a, b, 3);
75  R0(block, b, c, d, e, a, 4); R0(block, a, b, c, d, e, 5); R0(block, e, a, b, c, d, 6); R0(block, d, e, a, b, c, 7);
76  R0(block, c, d, e, a, b, 8); R0(block, b, c, d, e, a, 9); R0(block, a, b, c, d, e, 10); R0(block, e, a, b, c, d, 11);
77  R0(block, d, e, a, b, c, 12); R0(block, c, d, e, a, b, 13); R0(block, b, c, d, e, a, 14); R0(block, a, b, c, d, e, 15);
78  R1(block, e, a, b, c, d, 16); R1(block, d, e, a, b, c, 17); R1(block, c, d, e, a, b, 18); R1(block, b, c, d, e, a, 19);
79  R2(block, a, b, c, d, e, 20); R2(block, e, a, b, c, d, 21); R2(block, d, e, a, b, c, 22); R2(block, c, d, e, a, b, 23);
80  R2(block, b, c, d, e, a, 24); R2(block, a, b, c, d, e, 25); R2(block, e, a, b, c, d, 26); R2(block, d, e, a, b, c, 27);
81  R2(block, c, d, e, a, b, 28); R2(block, b, c, d, e, a, 29); R2(block, a, b, c, d, e, 30); R2(block, e, a, b, c, d, 31);
82  R2(block, d, e, a, b, c, 32); R2(block, c, d, e, a, b, 33); R2(block, b, c, d, e, a, 34); R2(block, a, b, c, d, e, 35);
83  R2(block, e, a, b, c, d, 36); R2(block, d, e, a, b, c, 37); R2(block, c, d, e, a, b, 38); R2(block, b, c, d, e, a, 39);
84  R3(block, a, b, c, d, e, 40); R3(block, e, a, b, c, d, 41); R3(block, d, e, a, b, c, 42); R3(block, c, d, e, a, b, 43);
85  R3(block, b, c, d, e, a, 44); R3(block, a, b, c, d, e, 45); R3(block, e, a, b, c, d, 46); R3(block, d, e, a, b, c, 47);
86  R3(block, c, d, e, a, b, 48); R3(block, b, c, d, e, a, 49); R3(block, a, b, c, d, e, 50); R3(block, e, a, b, c, d, 51);
87  R3(block, d, e, a, b, c, 52); R3(block, c, d, e, a, b, 53); R3(block, b, c, d, e, a, 54); R3(block, a, b, c, d, e, 55);
88  R3(block, e, a, b, c, d, 56); R3(block, d, e, a, b, c, 57); R3(block, c, d, e, a, b, 58); R3(block, b, c, d, e, a, 59);
89  R4(block, a, b, c, d, e, 60); R4(block, e, a, b, c, d, 61); R4(block, d, e, a, b, c, 62); R4(block, c, d, e, a, b, 63);
90  R4(block, b, c, d, e, a, 64); R4(block, a, b, c, d, e, 65); R4(block, e, a, b, c, d, 66); R4(block, d, e, a, b, c, 67);
91  R4(block, c, d, e, a, b, 68); R4(block, b, c, d, e, a, 69); R4(block, a, b, c, d, e, 70); R4(block, e, a, b, c, d, 71);
92  R4(block, d, e, a, b, c, 72); R4(block, c, d, e, a, b, 73); R4(block, b, c, d, e, a, 74); R4(block, a, b, c, d, e, 75);
93  R4(block, e, a, b, c, d, 76); R4(block, d, e, a, b, c, 77); R4(block, c, d, e, a, b, 78); R4(block, b, c, d, e, a, 79);
94  /* Add the working vars back into context.state[] */
95  this->state[0] += a;
96  this->state[1] += b;
97  this->state[2] += c;
98  this->state[3] += d;
99  this->state[4] += e;
100  /* Wipe variables */
101  a = b = c = d = e = 0;
102  }
103 
104  public:
106  {
107  if (iv != NULL)
108  {
109  if (iv->second != 5)
110  throw CoreException("Invalid IV size");
111  for (int i = 0; i < 5; ++i)
112  this->state[i] = iv->first[i];
113  }
114  else
115  for (int i = 0; i < 5; ++i)
116  this->state[i] = sha1_iv[i];
117 
118  this->count[0] = this->count[1] = 0;
119  memset(this->buffer, 0, sizeof(this->buffer));
120  memset(this->digest, 0, sizeof(this->digest));
121  }
122 
123  void Update(const unsigned char *data, size_t len) anope_override
124  {
125  uint32_t i, j;
126 
127  j = (this->count[0] >> 3) & 63;
128  if ((this->count[0] += len << 3) < (len << 3))
129  ++this->count[1];
130  this->count[1] += len >> 29;
131  if (j + len > 63)
132  {
133  memcpy(&this->buffer[j], data, (i = 64 - j));
134  this->Transform(this->buffer);
135  for (; i + 63 < len; i += 64)
136  this->Transform(&data[i]);
137  j = 0;
138  }
139  else
140  i = 0;
141  memcpy(&this->buffer[j], &data[i], len - i);
142  }
143 
145  {
146  uint32_t i;
147  unsigned char finalcount[8];
148 
149  for (i = 0; i < 8; ++i)
150  finalcount[i] = static_cast<unsigned char>((this->count[i >= 4 ? 0 : 1] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
151  this->Update(reinterpret_cast<const unsigned char *>("\200"), 1);
152  while ((this->count[0] & 504) != 448)
153  this->Update(reinterpret_cast<const unsigned char *>("\0"), 1);
154  this->Update(finalcount, 8); /* Should cause a SHA1Transform() */
155  for (i = 0; i < 20; ++i)
156  this->digest[i] = static_cast<unsigned char>((this->state[i>>2] >> ((3 - (i & 3)) * 8)) & 255);
157 
158  /* Wipe variables */
159  memset(this->buffer, 0, sizeof(this->buffer));
160  memset(this->state, 0, sizeof(this->state));
161  memset(this->count, 0, sizeof(this->count));
162  memset(&finalcount, 0, sizeof(finalcount));
163 
164  this->Transform(this->buffer);
165  }
166 
168  {
169  Encryption::Hash hash;
170  hash.first = this->digest;
171  hash.second = sizeof(this->digest);
172  return hash;
173  }
174 };
175 
177 {
178  public:
179  SHA1Provider(Module *creator) : Encryption::Provider(creator, "sha1") { }
180 
182  {
183  return new SHA1Context(iv);
184  }
185 
187  {
188  Encryption::IV iv;
189  iv.first = sha1_iv;
190  iv.second = sizeof(sha1_iv) / sizeof(uint32_t);
191  return iv;
192  }
193 };
194 
195 class ESHA1 : public Module
196 {
198 
199  public:
200  ESHA1(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, ENCRYPTION | VENDOR),
201  sha1provider(this)
202  {
203 
204  }
205 
207  {
208  SHA1Context context;
209 
210  context.Update(reinterpret_cast<const unsigned char *>(src.c_str()), src.length());
211  context.Finalize();
212 
213  Encryption::Hash hash = context.GetFinalizedHash();
214 
215  Anope::string buf = "sha1:" + Anope::Hex(reinterpret_cast<const char *>(hash.first), hash.second);
216 
217  Log(LOG_DEBUG_2) << "(enc_sha1) hashed password from [" << src << "] to [" << buf << "]";
218  dest = buf;
219  return EVENT_ALLOW;
220  }
221 
223  {
224  const NickAlias *na = NickAlias::Find(req->GetAccount());
225  if (na == NULL)
226  return;
227  NickCore *nc = na->nc;
228 
229  size_t pos = nc->pass.find(':');
230  if (pos == Anope::string::npos)
231  return;
232  Anope::string hash_method(nc->pass.begin(), nc->pass.begin() + pos);
233  if (!hash_method.equals_cs("sha1"))
234  return;
235 
236  Anope::string buf;
237  this->OnEncrypt(req->GetPassword(), buf);
238  if (nc->pass.equals_cs(buf))
239  {
241  Anope::Encrypt(req->GetPassword(), nc->pass);
242  req->Success(this);
243  }
244  }
245 };
246 
Serialize::Reference< NickCore > nc
Definition: account.h:47
unsigned char digest[20]
Definition: enc_sha1.cpp:57
static NickAlias * Find(const Anope::string &nick)
Definition: nickalias.cpp:121
bool equals_cs(const char *_str) const
Definition: anope.h:74
void OnCheckAuthentication(User *, IdentifyRequest *req) anope_override
Definition: enc_sha1.cpp:222
EventReturn OnEncrypt(const Anope::string &src, Anope::string &dest) anope_override
Definition: enc_sha1.cpp:206
static uint32_t blk0(CHAR64LONG16 &block, uint32_t i)
Definition: enc_sha1.cpp:30
static void R3(CHAR64LONG16 &block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i)
Definition: enc_sha1.cpp:44
static uint32_t rol(uint32_t value, uint32_t bits)
Definition: enc_sha1.cpp:26
unsigned char c[64]
Definition: enc_sha1.cpp:22
ESHA1(const Anope::string &modname, const Anope::string &creator)
Definition: enc_sha1.cpp:200
void Transform(const unsigned char buf[64])
Definition: enc_sha1.cpp:59
static uint32_t blk(CHAR64LONG16 &block, uint32_t i)
Definition: enc_sha1.cpp:38
SHA1Provider(Module *creator)
Definition: enc_sha1.cpp:179
Encryption::Context * CreateContext(Encryption::IV *iv) anope_override
Definition: enc_sha1.cpp:181
Definition: users.h:34
void Update(const unsigned char *data, size_t len) anope_override
Definition: enc_sha1.cpp:123
SHA1Provider sha1provider
Definition: enc_sha1.cpp:197
static const uint32_t sha1_iv[5]
Definition: enc_sha1.cpp:47
Anope::string pass
Definition: account.h:115
CoreExport string Hex(const string &data)
Definition: misc.cpp:546
static void R0(CHAR64LONG16 &block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i)
Definition: enc_sha1.cpp:41
std::pair< const unsigned char *, size_t > Hash
Definition: encryption.h:15
SHA1Context(Encryption::IV *iv=NULL)
Definition: enc_sha1.cpp:105
static const size_type npos
Definition: anope.h:44
Encryption::IV GetDefaultIV() anope_override
Definition: enc_sha1.cpp:186
uint32_t count[2]
Definition: enc_sha1.cpp:55
Encryption::Hash GetFinalizedHash() anope_override
Definition: enc_sha1.cpp:167
std::pair< const uint32_t *, size_t > IV
Definition: encryption.h:16
static void R2(CHAR64LONG16 &block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i)
Definition: enc_sha1.cpp:43
#define anope_override
Definition: services.h:56
EventReturn
Definition: modules.h:129
#define MODULE_INIT(x)
Definition: modules.h:45
static Module * FindFirstOf(ModType type)
uint32_t state[5]
Definition: enc_sha1.cpp:54
static void R4(CHAR64LONG16 &block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i)
Definition: enc_sha1.cpp:45
void Finalize() anope_override
Definition: enc_sha1.cpp:144
static void R1(CHAR64LONG16 &block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i)
Definition: enc_sha1.cpp:42
uint32_t l[16]
Definition: enc_sha1.cpp:23
CoreExport void Encrypt(const Anope::string &src, Anope::string &dest)
Definition: misc.cpp:511
Definition: logger.h:53
unsigned char buffer[64]
Definition: enc_sha1.cpp:56
iterator begin()
Definition: anope.h:282
size_type find(const string &_str, size_type pos=0) const
Definition: anope.h:192
Provider(Module *creator, const Anope::string &sname)
Definition: encryption.h:30