| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| |
| 3 |
|
| #include "EXTERN.h" |
| 4 |
|
| #include "perl.h" |
| 5 |
|
| #include "XSUB.h" |
| 6 |
|
| #include "ppport.h" |
| 7 |
|
| |
| 8 |
|
| #include "src/sha3nist.c" |
| 9 |
|
| #include "src/jh.c" |
| 10 |
|
| |
| 11 |
|
| static int |
| 12 |
275 |
| hex_encode (char *dest, const unsigned char *src, int len) { |
| 13 |
|
| static const char hex[] = "0123456789abcdef"; |
| 14 |
275 |
| char *p = dest; |
| 15 |
275 |
| const unsigned char *s = src; |
| 16 |
12055 |
▶ | for (; len--; s++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; len--; s++) { |
11780 |
275 |
12055 |
|
| 17 |
11780 |
| *p++ = hex[s[0] >> 4]; |
| 18 |
11780 |
| *p++ = hex[s[0] & 0x0f]; |
| 19 |
|
| } |
| 20 |
275 |
| return (int)(p - dest); |
| 21 |
|
| } |
| 22 |
|
| |
| 23 |
|
| static int |
| 24 |
0 |
| base64_encode (char *dest, const unsigned char *src, int len) { |
| 25 |
|
| static const char b64[] = |
| 26 |
|
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 27 |
0 |
| char *p = dest; |
| 28 |
0 |
| const unsigned char *s = src; |
| 29 |
0 |
| const unsigned char *end = src + len - 2; |
| 30 |
|
| |
| 31 |
0 |
▶ | for (; s < end; s += 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; s < end; s += 3) { |
0 |
0 |
0 |
|
| 32 |
0 |
| *p++ = b64[s[0] >> 2]; |
| 33 |
0 |
| *p++ = b64[((s[0] & 3) << 4) + (s[1] >> 4)]; |
| 34 |
0 |
| *p++ = b64[((s[1] & 0xf) << 2) + (s[2] >> 6)]; |
| 35 |
0 |
| *p++ = b64[s[2] & 0x3f]; |
| 36 |
|
| } |
| 37 |
0 |
| switch (len % 3) { |
| 38 |
0 |
| case 1: |
| 39 |
0 |
| *p++ = b64[s[0] >> 2]; |
| 40 |
0 |
| *p++ = b64[(s[0] & 3) << 4]; |
| 41 |
0 |
| break; |
| 42 |
0 |
| case 2: |
| 43 |
0 |
| *p++ = b64[s[0] >> 2]; |
| 44 |
0 |
| *p++ = b64[((s[0] & 3) << 4) + (s[1] >> 4)]; |
| 45 |
0 |
| *p++ = b64[((s[1] & 0xf) << 2)]; |
| 46 |
0 |
| break; |
| 47 |
|
| } |
| 48 |
0 |
| return (int)(p - dest); |
| 49 |
|
| } |
| 50 |
|
| |
| 51 |
|
| static SV * |
| 52 |
303 |
| make_mortal_sv(pTHX_ const unsigned char *src, int bitlen, int enc) { |
| 53 |
|
| char result[128]; |
| 54 |
|
| char *ret; |
| 55 |
303 |
| int len = bitlen >> 3; |
| 56 |
|
| |
| 57 |
303 |
| switch (enc) { |
| 58 |
28 |
| case 0: |
| 59 |
28 |
| ret = (char *)src; |
| 60 |
28 |
| break; |
| 61 |
275 |
| case 1: |
| 62 |
275 |
| len = hex_encode(result, src, len); |
| 63 |
275 |
| ret = result; |
| 64 |
275 |
| break; |
| 65 |
0 |
| case 2: |
| 66 |
0 |
| len = base64_encode(result, src, len); |
| 67 |
0 |
| ret = result; |
| 68 |
0 |
| break; |
| 69 |
|
| } |
| 70 |
303 |
| return sv_2mortal(newSVpv(ret, len)); |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
|
| typedef hashState *Digest__JH; |
| 74 |
|
| |
| 75 |
|
| MODULE = Digest::JH PACKAGE = Digest::JH |
| 76 |
|
| |
| 77 |
|
| PROTOTYPES: ENABLE |
| 78 |
|
| |
| 79 |
|
| void |
| 80 |
|
| jh_224 (...) |
| 81 |
|
| ALIAS: |
| 82 |
|
| jh_224 = 0 |
| 83 |
|
| jh_224_hex = 1 |
| 84 |
|
| jh_224_base64 = 2 |
| 85 |
|
| jh_256 = 3 |
| 86 |
|
| jh_256_hex = 4 |
| 87 |
|
| jh_256_base64 = 5 |
| 88 |
|
| jh_384 = 6 |
| 89 |
|
| jh_384_hex = 7 |
| 90 |
|
| jh_384_base64 = 8 |
| 91 |
|
| jh_512 = 9 |
| 92 |
|
| jh_512_hex = 10 |
| 93 |
|
| jh_512_base64 = 11 |
| 94 |
|
| PREINIT: |
| 95 |
|
| hashState ctx; |
| 96 |
|
| int bitlen, i; |
| 97 |
|
| unsigned char *data; |
| 98 |
|
| STRLEN len; |
| 99 |
|
| unsigned char result[64]; |
| 100 |
|
| CODE: |
| 101 |
|
| static const int ix2bits[] = |
| 102 |
|
| {224, 224, 224, 256, 256, 256, 384, 384, 384, 512, 512, 512}; |
| 103 |
48 |
| bitlen = ix2bits[ix]; |
| 104 |
48 |
▶ | if (Init(&ctx, bitlen) != SUCCESS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (Init(&ctx, bitlen) != SUCCESS) |
0 |
48 |
48 |
|
| 105 |
0 |
| XSRETURN_UNDEF; |
| 106 |
96 |
▶ | for (i = 0; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < items; i++) { |
48 |
48 |
96 |
|
| 107 |
48 |
| data = (unsigned char *)(SvPV(ST(i), len)); |
| 108 |
48 |
▶ | if (Update(&ctx, data, len << 3) != SUCCESS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (Update(&ctx, data, len << 3) != SUCCESS) |
0 |
48 |
48 |
|
| 109 |
0 |
| XSRETURN_UNDEF; |
| 110 |
|
| } |
| 111 |
48 |
▶ | if (Final(&ctx, result) != SUCCESS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (Final(&ctx, result) != SUCCESS) |
0 |
48 |
48 |
|
| 112 |
0 |
| XSRETURN_UNDEF; |
| 113 |
48 |
| ST(0) = make_mortal_sv(aTHX_ result, bitlen, ix % 3); |
| 114 |
48 |
| XSRETURN(1); |
| 115 |
|
| |
| 116 |
|
| Digest::JH |
| 117 |
|
| new (class, hashsize) |
| 118 |
|
| SV *class |
| 119 |
|
| int hashsize |
| 120 |
|
| CODE: |
| 121 |
238 |
| Newx(RETVAL, 1, hashState); |
| 122 |
238 |
▶ | if (Init(RETVAL, hashsize) != SUCCESS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (Init(RETVAL, hashsize) != SUCCESS) |
1 |
237 |
238 |
|
| 123 |
1 |
| XSRETURN_UNDEF; |
| 124 |
|
| OUTPUT: |
| 125 |
|
| RETVAL |
| 126 |
|
| |
| 127 |
|
| Digest::JH |
| 128 |
|
| clone (self) |
| 129 |
|
| Digest::JH self |
| 130 |
|
| CODE: |
| 131 |
4 |
| Newx(RETVAL, 1, hashState); |
| 132 |
4 |
| Copy(self, RETVAL, 1, hashState); |
| 133 |
|
| OUTPUT: |
| 134 |
|
| RETVAL |
| 135 |
|
| |
| 136 |
|
| void |
| 137 |
|
| reset (self) |
| 138 |
|
| Digest::JH self |
| 139 |
|
| PPCODE: |
| 140 |
16 |
▶ | if (Init(self, self->hashbitlen) != SUCCESS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (Init(self, self->hashbitlen) != SUCCESS) |
0 |
16 |
16 |
|
| 141 |
0 |
| XSRETURN_UNDEF; |
| 142 |
16 |
| XSRETURN(1); |
| 143 |
|
| |
| 144 |
|
| int |
| 145 |
|
| hashsize(self) |
| 146 |
|
| Digest::JH self |
| 147 |
|
| ALIAS: |
| 148 |
|
| algorithm = 1 |
| 149 |
|
| CODE: |
| 150 |
0 |
▶ | RETVAL = self->hashbitlen; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->hashbitlen; |
0 |
0 |
0 |
|
| 151 |
|
| OUTPUT: |
| 152 |
|
| RETVAL |
| 153 |
|
| |
| 154 |
|
| void |
| 155 |
|
| add (self, ...) |
| 156 |
|
| Digest::JH self |
| 157 |
|
| PREINIT: |
| 158 |
|
| int i; |
| 159 |
|
| unsigned char *data; |
| 160 |
|
| STRLEN len; |
| 161 |
|
| PPCODE: |
| 162 |
90 |
▶ | for (i = 1; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) { |
45 |
45 |
90 |
|
| 163 |
45 |
| data = (unsigned char *)(SvPV(ST(i), len)); |
| 164 |
45 |
▶ | if (Update(self, data, len << 3) != SUCCESS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (Update(self, data, len << 3) != SUCCESS) |
0 |
45 |
45 |
|
| 165 |
0 |
| XSRETURN_UNDEF; |
| 166 |
|
| } |
| 167 |
45 |
| XSRETURN(1); |
| 168 |
|
| |
| 169 |
|
| void |
| 170 |
|
| _add_bits (self, msg, bitlen) |
| 171 |
|
| Digest::JH self |
| 172 |
|
| SV *msg |
| 173 |
|
| int bitlen |
| 174 |
|
| PREINIT: |
| 175 |
|
| int i; |
| 176 |
|
| unsigned char *data; |
| 177 |
|
| STRLEN len; |
| 178 |
|
| PPCODE: |
| 179 |
204 |
▶ | if (! bitlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (! bitlen) |
4 |
200 |
204 |
|
| 180 |
4 |
| XSRETURN(1); |
| 181 |
200 |
| data = (unsigned char *)(SvPV(msg, len)); |
| 182 |
200 |
▶ | if (bitlen > len << 3) |
Branch: 1 branch
| Branch | True | False | Total |
| if (bitlen > len << 3) |
0 |
200 |
200 |
|
| 183 |
0 |
| bitlen = len << 3; |
| 184 |
200 |
▶ | if (Update(self, data, bitlen) != SUCCESS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (Update(self, data, bitlen) != SUCCESS) |
1 |
199 |
200 |
|
| 185 |
1 |
| XSRETURN_UNDEF; |
| 186 |
199 |
| XSRETURN(1); |
| 187 |
|
| |
| 188 |
|
| void * |
| 189 |
|
| digest (self) |
| 190 |
|
| Digest::JH self |
| 191 |
|
| ALIAS: |
| 192 |
|
| digest = 0 |
| 193 |
|
| hexdigest = 1 |
| 194 |
|
| b64digest = 2 |
| 195 |
|
| PREINIT: |
| 196 |
|
| unsigned char result[64]; |
| 197 |
|
| CODE: |
| 198 |
255 |
▶ | if (Final(self, result) != SUCCESS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (Final(self, result) != SUCCESS) |
0 |
255 |
255 |
|
| 199 |
0 |
| XSRETURN_UNDEF; |
| 200 |
255 |
| Init(self, self->hashbitlen); |
| 201 |
255 |
| ST(0) = make_mortal_sv(aTHX_ result, self->hashbitlen, ix); |
| 202 |
255 |
| XSRETURN(1); |
| 203 |
|
| |
| 204 |
|
| void |
| 205 |
|
| DESTROY (self) |
| 206 |
|
| Digest::JH self |
| 207 |
|
| CODE: |
| 208 |
241 |
| Safefree(self); |