wrap_160.c

filter: Statementstmt 94.4% 34 / 36 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.6% 44 / 48 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/RIPEMD160.pm ↑ Summary RIPEMD160.xs »
1 #include <string.h>
2  
3 #include "rmd160.h"
4 #include "wrap_160.h"
5  
6 41 void RIPEMD160_init(RIPEMD160 ripemd160)
7 {
8 41 MDinit(ripemd160->MDbuf);
9 41 ripemd160->local = (dword) 0;
10 41 ripemd160->count_lo = (dword) 0;
11 41 ripemd160->count_hi = (dword) 0;
12 41 }
13  
14 7643 void RIPEMD160_update(RIPEMD160 ripemd160, byte *strptr, dword len)
15 {
16 dword
17 i;
18
19 7643 ▶ if (ripemd160->count_lo + len < ripemd160->count_lo) {
Branch: 1 branch
BranchTrueFalseTotal
if (ripemd160->count_lo + len < ripemd160->count_lo) { 0 7643 7643
20 0 ripemd160->count_hi++;
21 }
22 7643 ripemd160->count_lo += len;
23
24 7643 ▶ if (ripemd160->local > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ripemd160->local > 0) { 7479 164 7643
25 7479 i = RIPEMD160_BLOCKSIZE - ripemd160->local;
26 7479 ▶ if (i > len) {
Branch: 1 branch
BranchTrueFalseTotal
if (i > len) { 7152 327 7479
27 7152 i = len;
28 }
29 7479 memcpy(((byte *) ripemd160->X) + ripemd160->local, strptr, i);
30 7479 len -= i;
31 7479 strptr += i;
32 7479 ripemd160->local += i;
33 7479 ▶ if (ripemd160->local == RIPEMD160_BLOCKSIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ripemd160->local == RIPEMD160_BLOCKSIZE) { 327 7152 7479
34 327 compress(ripemd160->MDbuf, ripemd160->X);
35 } else {
36 7152 return;
37 }
38 }
39 17010 ▶ while (len >= RIPEMD160_BLOCKSIZE) {
Branch: 1 branch
BranchTrueFalseTotal
while (len >= RIPEMD160_BLOCKSIZE) { 16519 491 17010
40 16519 memcpy(ripemd160->X, strptr, RIPEMD160_BLOCKSIZE);
41 16519 strptr += RIPEMD160_BLOCKSIZE;
42 16519 len -= RIPEMD160_BLOCKSIZE;
43 16519 compress(ripemd160->MDbuf, ripemd160->X);
44 }
45 491 memcpy((byte *) ripemd160->X, strptr, len);
46 491 ripemd160->local = len;
47 }
48  
49 39 void RIPEMD160_final(RIPEMD160 ripemd160)
50 {
51 39 ▶ if (ripemd160->local != ripemd160->count_lo % 64) {
Branch: 1 branch
BranchTrueFalseTotal
if (ripemd160->local != ripemd160->count_lo % 64) { 0 39 39
52 0 printf("local != count \% 64\n");
53 }
54  
55 39 MDfinish(ripemd160->MDbuf,
56 39 (byte *) ripemd160->X,
57 39 (dword) ripemd160->count_lo,
58 39 (dword) ripemd160->count_hi);
59 39 }
60  
61 /* The HMAC_RIPEMD160 transform looks like:
62
63 RIPEMD160(K XOR opad, RIPEMD160(K XOR ipad, text))
64
65 where K is an n byte key
66 ipad is the byte 0x36 repeated 64 times
67 opad is the byte 0x5c repeated 64 times
68 and text is the data being protected
69 */
70  
71 #ifdef USE_MALICIOUS_MAC
72 void RIPEMD160_HMAC(RIPEMD160 ripemd160,
73 byte *input, /* pointer to data stream */
74 dword len, /* length of data stream */
75 byte *key, /* pointer to authentication key */
76 dword keylen) /* length of authentication key */
77 {
78 byte
79 k_ipad[65], /* inner padding - key XORd with ipad */
80 k_opad[65]; /* outer padding - key XORd with opad */
81 byte
82 tk[RMD160_DIGESTSIZE];
83 dword
84 i;
85
86 /* if key is longer than 64 bytes reset it to key=RIPEMD160(key) */
87 if (keylen > 64) {
88 RIPEMD160_INFO
89 tctx;
90
91 RIPEMD160_init(&tctx);
92 RIPEMD160_update(&tctx, key, keylen);
93 RIPEMD160_final(&tctx);
94
95 key = tctx.MDbuf;
96 keylen = RMD160_DIGESTSIZE;
97 }
98
99 /* start out by storing key in pads */
100 memset(k_ipad, 0x36, sizeof(k_ipad));
101 memset(k_opad, 0x5c, sizeof(k_opad));
102
103 /* XOR key with ipad and opad values */
104 for (i=0; i<keylen; i++) {
105 k_ipad[i] ^= key[i];
106 k_opad[i] ^= key[i];
107 }
108
109 /* perform inner RIPEMD-160 */
110 RIPEMD160_init(ripemd160); /* init ripemd160 for 1st pass */
111 RIPEMD160_update(ripemd160, k_ipad, 64); /* start with inner pad */
112 RIPEMD160_update(ripemd160, input, len); /* then text of datagram */
113 RIPEMD160_final(ripemd160); /* finish up 1st pass */
114 memcpy(digest, ripemd160->MDbuf, RMD160_DIGESTSIZE);
115
116 /* perform outer RIPEMD-160 */
117 RIPEMD160_init(ripemd160); /* init ripemd160 for 2nd pass */
118 RIPEMD160_update(ripemd160, k_opad, 64); /* start with outer pad */
119 RIPEMD160_update(ripemd160, digest, RMD160_DIGESTSIZE);
120 RIPEMD160_final(ripemd160); /* finish up 2nd pass */
121 memcpy(digest, ripemd160->MDbuf, RMD160_DIGESTSIZE);
122
123 /* clean up secret keys */
124 memset(k_ipad, 0x00, sizeof(k_ipad));
125 memset(k_opad, 0x00, sizeof(k_opad));
126 }
127 #endif
128 /* ************************************************************************* */
129  
130 /* #define MAINTEST */
131  
132 #ifdef MAINTEST
133 #include <stdio.h>
134 #include <stdlib.h>
135  
136 void print_hash(RIPEMD160 ripemd160)
137 {
138 byte hashcode[RMDsize/8];
139 int i;
140  
141 for (i=0; i<RMDsize/8; i+=4) {
142 hashcode[i] = ripemd160->MDbuf[i>>2];
143 hashcode[i+1] = (ripemd160->MDbuf[i>>2] >> 8);
144 hashcode[i+2] = (ripemd160->MDbuf[i>>2] >> 16);
145 hashcode[i+3] = (ripemd160->MDbuf[i>>2] >> 24);
146 }
147 printf("hashcode: ");
148 for (i=0; i<RMDsize/8; i++)
149 printf("%02x", hashcode[i]);
150 printf("\n");
151 }
152  
153 int main (void)
154 {
155 RIPEMD160_INFO ripemd160_info;
156 byte a[1000001];
157  
158 int i;
159 long L;
160  
161 RIPEMD160_init(&ripemd160_info);
162  
163 /*
164 RIPEMD160_update(&ripemd160_info, (byte *) "a", (dword) strlen("a"));
165 RIPEMD160_update(&ripemd160_info, (byte *) "b", (dword) strlen("b"));
166 RIPEMD160_update(&ripemd160_info, (byte *) "c", (dword) strlen("c"));
167 */
168  
169 /*
170 memset(a, 'a', 1000000);
171 a[1000000] = 0;
172 RIPEMD160_update(&ripemd160_info, (byte *) a, (dword) 1000000);
173 */
174  
175
176 for (L = 0; L<1000000; L++) {
177 RIPEMD160_update(&ripemd160_info, (byte *) "a", (dword) strlen("a"));
178 }
179
180 /*
181 for (i = 0; i<8; i++) {
182 RIPEMD160_update(&ripemd160_info_info,
183 (byte *) "1234567890",
184 (dword) strlen("1234567890"));
185 }
186 */
187
188 RIPEMD160_final(&ripemd160_info);
189  
190 print_hash(&ripemd160_info);
191
192 return(0);
193 }
194 #endif
« blib/lib/RIPEMD160.pm ↑ Summary RIPEMD160.xs »