1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "ppport.h"
5 #include "_shark.c"
6 
7 typedef struct shark {
8     ddword roundkey_enc[14];
9     ddword roundkey_dec[14];
10 }* Crypt__Shark;
11 
12 MODULE = Crypt::Shark		PACKAGE = Crypt::Shark
13 PROTOTYPES: DISABLE
14 
15 int
16 keysize(...)
17     CODE:
18         RETVAL = 16;
19     OUTPUT:
20         RETVAL
21 
22 int
23 blocksize(...)
24     CODE:
25         RETVAL = 8;
26     OUTPUT:
27         RETVAL
28 
29 Crypt::Shark
new(class,rawkey)30 new(class, rawkey)
31     SV* class
32     SV* rawkey
33     CODE:
34     {
35         STRLEN keyLength;
36         if (! SvPOK(rawkey))
37             croak("Key setup error: Key must be a string scalar!");
38 
39         keyLength = SvCUR(rawkey);
40         if (keyLength != 16)
41             croak("Key setup error: Key must be 16 bytes long!");
42 
43         Newz(0, RETVAL, 1, struct shark);
44         init();
45         key_init(SvPV_nolen(rawkey), RETVAL->roundkey_enc);
46         box_init(RETVAL->roundkey_enc, RETVAL->roundkey_dec);
47     }
48 
49     OUTPUT:
50         RETVAL
51 
52 SV*
encrypt(self,input)53 encrypt(self, input)
54     Crypt::Shark self
55     SV* input
56     CODE:
57     {
58         STRLEN blockSize;
59         unsigned char* intext = SvPV(input, blockSize);
60         if (blockSize != 8) {
61             croak("Encryption error: Block size must be 8 bytes long!");
62         } else {
63             RETVAL = newSVpv("", blockSize);
64             encryption(intext, self->roundkey_enc, SvPV_nolen(RETVAL));
65         }
66     }
67 
68     OUTPUT:
69         RETVAL
70 
71 SV*
decrypt(self,input)72 decrypt(self, input)
73     Crypt::Shark self
74     SV* input
75     CODE:
76     {
77         STRLEN blockSize;
78         unsigned char* intext = SvPV(input, blockSize);
79         if (blockSize != 8) {
80             croak("Decryption error: Block size must be 8 bytes long!");
81         } else {
82             RETVAL = newSVpv("", blockSize);
83             decryption(intext, self->roundkey_dec, SvPV_nolen(RETVAL));
84         }
85     }
86 
87     OUTPUT:
88         RETVAL
89 
90 void
91 DESTROY(self)
92     Crypt::Shark self
93     CODE:
94         Safefree(self);
95 
96