ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
sha1.c File Reference

Go to the source code of this file.

Macros

#define SHA1HANDSOFF
 
#define BYTE_ORDER   1234
 
#define LITTLE_ENDIAN   1234
 
#define BIG_ENDIAN   4321
 
#define rol(value, bits)   (((value) << (bits)) | ((value) >> (32 - (bits))))
 
#define blk0(i)   (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | (rol(block->l[i], 8) & 0x00FF00FF))
 
#define blk(i)
 
#define R0(v, w, x, y, z, i)
 
#define R1(v, w, x, y, z, i)
 
#define R2(v, w, x, y, z, i)
 
#define R3(v, w, x, y, z, i)
 
#define R4(v, w, x, y, z, i)
 

Functions

void SHA1Transform (uint32_t state[5], const unsigned char buffer[SHA1_BLOCK_LENGTH])
 
void SHA1Init (SHA1_CTX *context)
 
void SHA1Update (SHA1_CTX *context, const void *dataptr, unsigned int len)
 
void SHA1Final (unsigned char digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
 

Macro Definition Documentation

◆ BIG_ENDIAN

#define BIG_ENDIAN   4321

Definition at line 34 of file sha1.c.

◆ blk

#define blk (   i)
Value:
(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))

Definition at line 47 of file sha1.c.

◆ blk0

#define blk0 (   i)    (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | (rol(block->l[i], 8) & 0x00FF00FF))

Definition at line 43 of file sha1.c.

◆ BYTE_ORDER

#define BYTE_ORDER   1234

Definition at line 32 of file sha1.c.

◆ LITTLE_ENDIAN

#define LITTLE_ENDIAN   1234

Definition at line 33 of file sha1.c.

◆ R0

#define R0 (   v,
  w,
  x,
  y,
  z,
  i 
)
Value:
z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
w = rol(w, 30);
#define blk0(i)
Definition sha1.c:43
#define rol(value, bits)
Definition sha1.c:38

Definition at line 52 of file sha1.c.

◆ R1

#define R1 (   v,
  w,
  x,
  y,
  z,
  i 
)
Value:
z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
w = rol(w, 30);
#define blk(i)
Definition sha1.c:47

Definition at line 55 of file sha1.c.

◆ R2

#define R2 (   v,
  w,
  x,
  y,
  z,
  i 
)
Value:
z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
w = rol(w, 30);

Definition at line 58 of file sha1.c.

◆ R3

#define R3 (   v,
  w,
  x,
  y,
  z,
  i 
)
Value:
z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
w = rol(w, 30);

Definition at line 61 of file sha1.c.

◆ R4

#define R4 (   v,
  w,
  x,
  y,
  z,
  i 
)
Value:
z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
w = rol(w, 30);

Definition at line 64 of file sha1.c.

◆ rol

#define rol (   value,
  bits 
)    (((value) << (bits)) | ((value) >> (32 - (bits))))

Definition at line 38 of file sha1.c.

◆ SHA1HANDSOFF

#define SHA1HANDSOFF

Definition at line 20 of file sha1.c.

Function Documentation

◆ SHA1Final()

void SHA1Final ( unsigned char  digest[SHA1_DIGEST_LENGTH],
SHA1_CTX *  context 
)

Definition at line 219 of file sha1.c.

219 {
220 unsigned int i;
221 unsigned char finalcount[8];
222
223 for (i = 0; i < 8; i++) {
224 finalcount[i] = (unsigned char)((context->count >> ((7 - (i & 7)) * 8)) & 255); /* Endian independent */
225 }
226 SHA1Update(context, "\200", 1);
227 while ((context->count & 504) != 448) {
228 SHA1Update(context, "\0", 1);
229 }
230 SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
231
232 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
233 digest[i] = (unsigned char)((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
234 }
235 memset(&finalcount, 0, sizeof(finalcount));
236 memset(context, 0, sizeof(*context));
237}
void SHA1Update(SHA1_CTX *context, const void *dataptr, unsigned int len)
Definition sha1.c:198
#define SHA1_DIGEST_LENGTH
Definition sha1.h:17
uint64_t count
Definition sha1.h:21
uint32_t state[5]
Definition sha1.h:20

References SHA1_CTX::count, SHA1_DIGEST_LENGTH, SHA1Update(), and SHA1_CTX::state.

◆ SHA1Init()

void SHA1Init ( SHA1_CTX *  context)

Definition at line 186 of file sha1.c.

186 {
187 /* SHA1 initialization constants */
188 context->count = 0;
189 context->state[0] = 0x67452301;
190 context->state[1] = 0xEFCDAB89;
191 context->state[2] = 0x98BADCFE;
192 context->state[3] = 0x10325476;
193 context->state[4] = 0xC3D2E1F0;
194}

References SHA1_CTX::count, and SHA1_CTX::state.

◆ SHA1Transform()

void SHA1Transform ( uint32_t  state[5],
const unsigned char  buffer[SHA1_BLOCK_LENGTH] 
)

Definition at line 70 of file sha1.c.

70 {
71 uint32_t a, b, c, d, e;
72 typedef union {
73 unsigned char c[64];
74 uint32_t l[16];
75 } CHAR64LONG16;
76 CHAR64LONG16 *block;
77#ifdef SHA1HANDSOFF
78 unsigned char workspace[SHA1_BLOCK_LENGTH];
79
80 block = (CHAR64LONG16 *)workspace;
81 memcpy(block, buffer, SHA1_BLOCK_LENGTH);
82#else
83 block = (CHAR64LONG16 *)buffer;
84#endif
85 /* Copy context->state[] to working vars */
86 a = state[0];
87 b = state[1];
88 c = state[2];
89 d = state[3];
90 e = state[4];
91
92 /* 4 rounds of 20 operations each. Loop unrolled. */
93 R0(a, b, c, d, e, 0);
94 R0(e, a, b, c, d, 1);
95 R0(d, e, a, b, c, 2);
96 R0(c, d, e, a, b, 3);
97 R0(b, c, d, e, a, 4);
98 R0(a, b, c, d, e, 5);
99 R0(e, a, b, c, d, 6);
100 R0(d, e, a, b, c, 7);
101 R0(c, d, e, a, b, 8);
102 R0(b, c, d, e, a, 9);
103 R0(a, b, c, d, e, 10);
104 R0(e, a, b, c, d, 11);
105 R0(d, e, a, b, c, 12);
106 R0(c, d, e, a, b, 13);
107 R0(b, c, d, e, a, 14);
108 R0(a, b, c, d, e, 15);
109 R1(e, a, b, c, d, 16);
110 R1(d, e, a, b, c, 17);
111 R1(c, d, e, a, b, 18);
112 R1(b, c, d, e, a, 19);
113 R2(a, b, c, d, e, 20);
114 R2(e, a, b, c, d, 21);
115 R2(d, e, a, b, c, 22);
116 R2(c, d, e, a, b, 23);
117 R2(b, c, d, e, a, 24);
118 R2(a, b, c, d, e, 25);
119 R2(e, a, b, c, d, 26);
120 R2(d, e, a, b, c, 27);
121 R2(c, d, e, a, b, 28);
122 R2(b, c, d, e, a, 29);
123 R2(a, b, c, d, e, 30);
124 R2(e, a, b, c, d, 31);
125 R2(d, e, a, b, c, 32);
126 R2(c, d, e, a, b, 33);
127 R2(b, c, d, e, a, 34);
128 R2(a, b, c, d, e, 35);
129 R2(e, a, b, c, d, 36);
130 R2(d, e, a, b, c, 37);
131 R2(c, d, e, a, b, 38);
132 R2(b, c, d, e, a, 39);
133 R3(a, b, c, d, e, 40);
134 R3(e, a, b, c, d, 41);
135 R3(d, e, a, b, c, 42);
136 R3(c, d, e, a, b, 43);
137 R3(b, c, d, e, a, 44);
138 R3(a, b, c, d, e, 45);
139 R3(e, a, b, c, d, 46);
140 R3(d, e, a, b, c, 47);
141 R3(c, d, e, a, b, 48);
142 R3(b, c, d, e, a, 49);
143 R3(a, b, c, d, e, 50);
144 R3(e, a, b, c, d, 51);
145 R3(d, e, a, b, c, 52);
146 R3(c, d, e, a, b, 53);
147 R3(b, c, d, e, a, 54);
148 R3(a, b, c, d, e, 55);
149 R3(e, a, b, c, d, 56);
150 R3(d, e, a, b, c, 57);
151 R3(c, d, e, a, b, 58);
152 R3(b, c, d, e, a, 59);
153 R4(a, b, c, d, e, 60);
154 R4(e, a, b, c, d, 61);
155 R4(d, e, a, b, c, 62);
156 R4(c, d, e, a, b, 63);
157 R4(b, c, d, e, a, 64);
158 R4(a, b, c, d, e, 65);
159 R4(e, a, b, c, d, 66);
160 R4(d, e, a, b, c, 67);
161 R4(c, d, e, a, b, 68);
162 R4(b, c, d, e, a, 69);
163 R4(a, b, c, d, e, 70);
164 R4(e, a, b, c, d, 71);
165 R4(d, e, a, b, c, 72);
166 R4(c, d, e, a, b, 73);
167 R4(b, c, d, e, a, 74);
168 R4(a, b, c, d, e, 75);
169 R4(e, a, b, c, d, 76);
170 R4(d, e, a, b, c, 77);
171 R4(c, d, e, a, b, 78);
172 R4(b, c, d, e, a, 79);
173
174 /* Add the working vars back into context.state[] */
175 state[0] += a;
176 state[1] += b;
177 state[2] += c;
178 state[3] += d;
179 state[4] += e;
180 /* Wipe variables */
181 a = b = c = d = e = 0;
182}
unsigned int uint32_t
Definition common.h:58
#define R1(v, w, x, y, z, i)
Definition sha1.c:55
#define R2(v, w, x, y, z, i)
Definition sha1.c:58
#define R0(v, w, x, y, z, i)
Definition sha1.c:52
#define R3(v, w, x, y, z, i)
Definition sha1.c:61
#define R4(v, w, x, y, z, i)
Definition sha1.c:64
#define SHA1_BLOCK_LENGTH
Definition sha1.h:16

References R0, R1, R2, R3, R4, and SHA1_BLOCK_LENGTH.

Referenced by SHA1Update().

◆ SHA1Update()

void SHA1Update ( SHA1_CTX *  context,
const void *  dataptr,
unsigned int  len 
)

Definition at line 198 of file sha1.c.

198 {
199 const uint8_t *data = dataptr;
200 unsigned int i;
201 unsigned int j;
202
203 j = (uint32_t)((context->count >> 3) & 63);
204 context->count += (len << 3);
205 if ((j + len) > 63) {
206 memcpy(&context->buffer[j], data, (i = 64 - j));
207 SHA1Transform(context->state, context->buffer);
208 for (; i + 63 < len; i += 64) {
209 SHA1Transform(context->state, &data[i]);
210 }
211 j = 0;
212 } else
213 i = 0;
214 memcpy(&context->buffer[j], &data[i], len - i);
215}
unsigned char uint8_t
Definition common.h:56
void SHA1Transform(uint32_t state[5], const unsigned char buffer[SHA1_BLOCK_LENGTH])
Definition sha1.c:70
unsigned char buffer[64]
Definition sha1.h:22

References SHA1_CTX::buffer, SHA1_CTX::count, SHA1Transform(), and SHA1_CTX::state.

Referenced by SHA1Final().