ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
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.

69 {
70 uint32_t a, b, c, d, e;
71 typedef union {
72 unsigned char c[64];
73 uint32_t l[16];
74 } CHAR64LONG16;
75 CHAR64LONG16 *block;
76#ifdef SHA1HANDSOFF
77 unsigned char workspace[SHA1_BLOCK_LENGTH];
78
79 block = (CHAR64LONG16 *)workspace;
80 memcpy(block, buffer, SHA1_BLOCK_LENGTH);
81#else
82 block = (CHAR64LONG16 *)buffer;
83#endif
84 /* Copy context->state[] to working vars */
85 a = state[0];
86 b = state[1];
87 c = state[2];
88 d = state[3];
89 e = state[4];
90
91 /* 4 rounds of 20 operations each. Loop unrolled. */
92 R0(a, b, c, d, e, 0);
93 R0(e, a, b, c, d, 1);
94 R0(d, e, a, b, c, 2);
95 R0(c, d, e, a, b, 3);
96 R0(b, c, d, e, a, 4);
97 R0(a, b, c, d, e, 5);
98 R0(e, a, b, c, d, 6);
99 R0(d, e, a, b, c, 7);
100 R0(c, d, e, a, b, 8);
101 R0(b, c, d, e, a, 9);
102 R0(a, b, c, d, e, 10);
103 R0(e, a, b, c, d, 11);
104 R0(d, e, a, b, c, 12);
105 R0(c, d, e, a, b, 13);
106 R0(b, c, d, e, a, 14);
107 R0(a, b, c, d, e, 15);
108 R1(e, a, b, c, d, 16);
109 R1(d, e, a, b, c, 17);
110 R1(c, d, e, a, b, 18);
111 R1(b, c, d, e, a, 19);
112 R2(a, b, c, d, e, 20);
113 R2(e, a, b, c, d, 21);
114 R2(d, e, a, b, c, 22);
115 R2(c, d, e, a, b, 23);
116 R2(b, c, d, e, a, 24);
117 R2(a, b, c, d, e, 25);
118 R2(e, a, b, c, d, 26);
119 R2(d, e, a, b, c, 27);
120 R2(c, d, e, a, b, 28);
121 R2(b, c, d, e, a, 29);
122 R2(a, b, c, d, e, 30);
123 R2(e, a, b, c, d, 31);
124 R2(d, e, a, b, c, 32);
125 R2(c, d, e, a, b, 33);
126 R2(b, c, d, e, a, 34);
127 R2(a, b, c, d, e, 35);
128 R2(e, a, b, c, d, 36);
129 R2(d, e, a, b, c, 37);
130 R2(c, d, e, a, b, 38);
131 R2(b, c, d, e, a, 39);
132 R3(a, b, c, d, e, 40);
133 R3(e, a, b, c, d, 41);
134 R3(d, e, a, b, c, 42);
135 R3(c, d, e, a, b, 43);
136 R3(b, c, d, e, a, 44);
137 R3(a, b, c, d, e, 45);
138 R3(e, a, b, c, d, 46);
139 R3(d, e, a, b, c, 47);
140 R3(c, d, e, a, b, 48);
141 R3(b, c, d, e, a, 49);
142 R3(a, b, c, d, e, 50);
143 R3(e, a, b, c, d, 51);
144 R3(d, e, a, b, c, 52);
145 R3(c, d, e, a, b, 53);
146 R3(b, c, d, e, a, 54);
147 R3(a, b, c, d, e, 55);
148 R3(e, a, b, c, d, 56);
149 R3(d, e, a, b, c, 57);
150 R3(c, d, e, a, b, 58);
151 R3(b, c, d, e, a, 59);
152 R4(a, b, c, d, e, 60);
153 R4(e, a, b, c, d, 61);
154 R4(d, e, a, b, c, 62);
155 R4(c, d, e, a, b, 63);
156 R4(b, c, d, e, a, 64);
157 R4(a, b, c, d, e, 65);
158 R4(e, a, b, c, d, 66);
159 R4(d, e, a, b, c, 67);
160 R4(c, d, e, a, b, 68);
161 R4(b, c, d, e, a, 69);
162 R4(a, b, c, d, e, 70);
163 R4(e, a, b, c, d, 71);
164 R4(d, e, a, b, c, 72);
165 R4(c, d, e, a, b, 73);
166 R4(b, c, d, e, a, 74);
167 R4(a, b, c, d, e, 75);
168 R4(e, a, b, c, d, 76);
169 R4(d, e, a, b, c, 77);
170 R4(c, d, e, a, b, 78);
171 R4(b, c, d, e, a, 79);
172
173 /* Add the working vars back into context.state[] */
174 state[0] += a;
175 state[1] += b;
176 state[2] += c;
177 state[3] += d;
178 state[4] += e;
179 /* Wipe variables */
180 a = b = c = d = e = 0;
181}
182
183/* SHA1Init - Initialize new context */
184
185void SHA1Init(SHA1_CTX *context) {
186 /* SHA1 initialization constants */
187 context->count = 0;
188 context->state[0] = 0x67452301;
189 context->state[1] = 0xEFCDAB89;
190 context->state[2] = 0x98BADCFE;
191 context->state[3] = 0x10325476;
192 context->state[4] = 0xC3D2E1F0;
193}
194
195/* Run your data through this. */
196
197void SHA1Update(SHA1_CTX *context, const void *dataptr, unsigned int len) {
198 const uint8_t *data = dataptr;
199 unsigned int i;
200 unsigned int j;
201
202 j = (uint32_t)((context->count >> 3) & 63);
203 context->count += (len << 3);
204 if ((j + len) > 63) {
205 memcpy(&context->buffer[j], data, (i = 64 - j));
206 SHA1Transform(context->state, context->buffer);
207 for (; i + 63 < len; i += 64) {
208 SHA1Transform(context->state, &data[i]);
209 }
210 j = 0;
211 } else
212 i = 0;
213 memcpy(&context->buffer[j], &data[i], len - i);
214}
215
216/* Add padding and return the message digest. */
217
218void SHA1Final(unsigned char digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context) {
219 unsigned int i;
220 unsigned char finalcount[8];
221
222 for (i = 0; i < 8; i++) {
223 finalcount[i] = (unsigned char)((context->count >> ((7 - (i & 7)) * 8)) & 255); /* Endian independent */
224 }
225 SHA1Update(context, "\200", 1);
226 while ((context->count & 504) != 448) {
227 SHA1Update(context, "\0", 1);
228 }
229 SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
230
231 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
232 digest[i] = (unsigned char)((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
233 }
234 memset(&finalcount, 0, sizeof(finalcount));
235 memset(context, 0, sizeof(*context));
236}
void SHA1Update(SHA1_CTX *context, const void *dataptr, unsigned int len)
Definition sha1.c:198
void SHA1Init(SHA1_CTX *context)
Definition sha1.c:186
#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
void SHA1Transform(uint32_t state[5], const unsigned char buffer[SHA1_BLOCK_LENGTH])
Definition sha1.c:70
#define R3(v, w, x, y, z, i)
Definition sha1.c:61
void SHA1Final(unsigned char digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
Definition sha1.c:219
#define R4(v, w, x, y, z, i)
Definition sha1.c:64

◆ 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,
 
)
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,
 
)
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,
 
)
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,
 
)
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,
 
)
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}

References SHA1Update().

◆ 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}

◆ 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}

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

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}

References SHA1Transform().

Referenced by SHA1Final().