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