660b443c |
1 | # ---?--- |
2 | # |
a24b5cfd |
3 | # $Id: algorithms.xs,v 1.2 2004/04/08 01:36:21 mdw Exp $ |
660b443c |
4 | # |
5 | # Interface to crypto algorithms |
6 | # |
7 | # (c) 2001 Straylight/Edgeware |
8 | # |
9 | |
10 | #----- Licensing notice ----------------------------------------------------- |
11 | # |
12 | # This file is part of the Perl interface to Catacomb. |
13 | # |
14 | # Catacomb/Perl is free software; you can redistribute it and/or modify |
15 | # it under the terms of the GNU General Public License as published by |
16 | # the Free Software Foundation; either version 2 of the License, or |
17 | # (at your option) any later version. |
18 | # |
19 | # Catacomb/Perl is distributed in the hope that it will be useful, |
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | # GNU General Public License for more details. |
23 | # |
24 | # You should have received a copy of the GNU General Public License |
25 | # along with Catacomb/Perl; if not, write to the Free Software Foundation, |
26 | # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | |
660b443c |
28 | MODULE = Catacomb PACKAGE = Catacomb::CipherClass |
29 | |
30 | PROTOTYPES: DISABLE |
31 | |
32 | gccipher * |
33 | find(me, name) |
34 | SV *me |
35 | char *name |
36 | PREINIT: |
37 | const gccipher **cc; |
38 | CODE: |
39 | RETVAL = 0; |
40 | for (cc = ciphertab; *cc; cc++) { |
41 | if (strcmp((*cc)->name, name) == 0) { |
42 | RETVAL = (gccipher *)*cc; |
43 | break; |
44 | } |
45 | } |
46 | OUTPUT: |
47 | RETVAL |
48 | |
49 | SV * |
50 | list(me) |
51 | SV *me |
52 | PREINIT: |
53 | const gccipher **cc; |
54 | SV *sv; |
55 | PPCODE: |
56 | for (cc = ciphertab; *cc; cc++) |
57 | XPUSHs(RET(*cc, "Catacomb::CipherClass")); |
58 | |
59 | keysize * |
60 | keysz(cc) |
61 | gccipher *cc |
62 | CODE: |
63 | RETVAL = cc->keysz; |
64 | OUTPUT: |
65 | RETVAL |
66 | |
67 | char * |
68 | name(cc) |
69 | gccipher *cc |
70 | CODE: |
71 | RETVAL = (char *)cc->name; |
72 | OUTPUT: |
73 | RETVAL |
74 | |
75 | size_t |
76 | blksz(cc) |
77 | gccipher *cc |
78 | CODE: |
79 | RETVAL = cc->blksz; |
80 | OUTPUT: |
81 | RETVAL |
82 | |
83 | gcipher * |
84 | init(cc, k) |
85 | gccipher *cc |
86 | SV *k |
87 | PREINIT: |
88 | STRLEN len; |
89 | char *p; |
90 | CODE: |
91 | p = SvPV(k, len); |
92 | if (keysz(len, cc->keysz) != len) { |
93 | croak("bad key size %lu for cipher `%s'", |
94 | (unsigned long)len, cc->name); |
95 | } |
96 | RETVAL = cc->init(p, len); |
97 | OUTPUT: |
98 | RETVAL |
99 | |
100 | MODULE = Catacomb PACKAGE = Catacomb::Cipher |
101 | |
102 | SV * |
103 | DESTROY(c) |
104 | gcipher *c |
105 | CODE: |
106 | c->ops->destroy(c); |
107 | XSRETURN_UNDEF; |
108 | |
109 | SV * |
110 | encrypt(c, plain) |
111 | gcipher *c |
112 | SV *plain |
113 | PREINIT: |
114 | STRLEN len; |
115 | char *p; |
116 | CODE: |
117 | p = SvPV(plain, len); |
118 | RETVAL = NEWSV(0, len ? len : 1); |
119 | c->ops->encrypt(c, p, SvPVX(RETVAL), len); |
120 | SvCUR_set(RETVAL, len); |
121 | SvPOK_on(RETVAL); |
122 | OUTPUT: |
123 | RETVAL |
124 | |
125 | SV * |
126 | decrypt(c, cipher) |
127 | gcipher *c |
128 | SV *cipher |
129 | PREINIT: |
130 | STRLEN len; |
131 | char *p; |
132 | CODE: |
133 | p = SvPV(cipher, len); |
134 | RETVAL = NEWSV(0, len ? len : 1); |
135 | c->ops->decrypt(c, p, SvPVX(RETVAL), len); |
136 | SvCUR_set(RETVAL, len); |
137 | SvPOK_on(RETVAL); |
138 | OUTPUT: |
139 | RETVAL |
140 | |
141 | SV * |
142 | setiv(c, iv) |
143 | gcipher *c |
144 | SV *iv |
145 | PREINIT: |
146 | STRLEN len; |
147 | char *p; |
148 | CODE: |
149 | p = SvPV(iv, len); |
150 | if (c->ops->c->blksz && len != c->ops->c->blksz) { |
151 | croak("IV for block cipher `%s' must be %lu", |
152 | c->ops->c->name, (unsigned long)c->ops->c->blksz); |
153 | } |
154 | c->ops->setiv(c, p); |
155 | XSRETURN_UNDEF; |
156 | |
157 | SV * |
158 | bdry(c) |
159 | gcipher *c |
160 | CODE: |
161 | c->ops->bdry(c); |
162 | XSRETURN_UNDEF; |
163 | |
164 | gccipher * |
165 | class(c) |
166 | gcipher *c |
167 | CODE: |
168 | RETVAL = (gccipher *)c->ops->c; |
169 | OUTPUT: |
170 | RETVAL |
171 | |
172 | MODULE = Catacomb PACKAGE = Catacomb::HashClass |
173 | |
174 | gchash * |
175 | find(me, name) |
176 | SV *me |
177 | char *name |
178 | PREINIT: |
179 | const gchash **hc; |
180 | CODE: |
181 | RETVAL = 0; |
182 | for (hc = hashtab; *hc; hc++) { |
183 | if (strcmp((*hc)->name, name) == 0) { |
184 | RETVAL = (gchash *)*hc; |
185 | break; |
186 | } |
187 | } |
188 | OUTPUT: |
189 | RETVAL |
190 | |
191 | SV * |
192 | list(me) |
193 | SV *me |
194 | PREINIT: |
195 | const gchash **hc; |
196 | SV *sv; |
197 | PPCODE: |
198 | for (hc = hashtab; *hc; hc++) |
199 | XPUSHs(RET(*hc, "Catacomb::HashClass")); |
200 | |
201 | size_t |
202 | hashsz(hc) |
203 | gchash *hc |
204 | CODE: |
205 | RETVAL = hc->hashsz; |
206 | OUTPUT: |
207 | RETVAL |
208 | |
209 | char * |
210 | name(hc) |
211 | gchash *hc |
212 | CODE: |
213 | RETVAL = (char *)hc->name; |
214 | OUTPUT: |
215 | RETVAL |
216 | |
217 | ghash * |
218 | init(hc) |
219 | gchash *hc |
220 | CODE: |
221 | RETVAL = hc->init(); |
222 | OUTPUT: |
223 | RETVAL |
224 | |
225 | MODULE = Catacomb PACKAGE = Catacomb::Hash |
226 | |
227 | SV * |
228 | DESTROY(h) |
229 | ghash *h |
230 | CODE: |
231 | h->ops->destroy(h); |
232 | XSRETURN_UNDEF; |
233 | |
234 | SV * |
235 | hash(h, sv) |
236 | ghash *h |
237 | SV *sv |
238 | PREINIT: |
239 | STRLEN len; |
240 | char *p; |
241 | CODE: |
242 | p = SvPV(sv, len); |
243 | h->ops->hash(h, p, len); |
244 | XSRETURN_UNDEF; |
245 | |
246 | SV * |
247 | done(h) |
248 | ghash *h |
249 | CODE: |
250 | RETVAL = NEWSV(0, h->ops->c->hashsz); |
251 | h->ops->done(h, SvPVX(RETVAL)); |
252 | SvCUR_set(RETVAL, h->ops->c->hashsz); |
253 | SvPOK_on(RETVAL); |
254 | OUTPUT: |
255 | RETVAL |
256 | |
257 | ghash * |
258 | copy(h) |
259 | ghash *h |
260 | CODE: |
261 | RETVAL = h->ops->copy(h); |
262 | OUTPUT: |
263 | RETVAL |
264 | |
265 | gchash * |
266 | class(h) |
267 | ghash *h |
268 | CODE: |
269 | RETVAL = (gchash *)h->ops->c; |
270 | OUTPUT: |
271 | RETVAL |
272 | |
273 | MODULE = Catacomb PACKAGE = Catacomb::MACClass |
274 | |
275 | gcMAC * |
276 | find(me, name) |
277 | SV *me |
278 | char *name |
279 | PREINIT: |
280 | const gcMAC **mc; |
281 | CODE: |
282 | RETVAL = 0; |
283 | for (mc = mactab; *mc; mc++) { |
284 | if (strcmp((*mc)->name, name) == 0) { |
285 | RETVAL = (gcMAC *)*mc; |
286 | break; |
287 | } |
288 | } |
289 | OUTPUT: |
290 | RETVAL |
291 | |
292 | SV * |
293 | list(me) |
294 | SV *me |
295 | PREINIT: |
296 | const gcMAC **mc; |
297 | SV *sv; |
298 | PPCODE: |
299 | for (mc = mactab; *mc; mc++) |
300 | XPUSHs(RET(*mc, "Catacomb::MACClass")); |
301 | |
302 | size_t |
303 | hashsz(mc) |
304 | gcMAC *mc |
305 | CODE: |
306 | RETVAL = mc->hashsz; |
307 | OUTPUT: |
308 | RETVAL |
309 | |
310 | keysize * |
311 | keysz(mc) |
312 | gcMAC *mc |
313 | CODE: |
314 | RETVAL = mc->keysz; |
315 | OUTPUT: |
316 | RETVAL |
317 | |
318 | char * |
319 | name(mc) |
320 | gcMAC *mc |
321 | CODE: |
322 | RETVAL = (char *)mc->name; |
323 | OUTPUT: |
324 | RETVAL |
325 | |
326 | gMAC * |
327 | key(mc, k) |
328 | gcMAC *mc |
329 | SV *k |
330 | PREINIT: |
331 | STRLEN len; |
332 | char *p; |
333 | CODE: |
334 | p = SvPV(k, len); |
335 | if (keysz(len, mc->keysz) != len) { |
336 | croak("bad key size %lu for mac `%s'", |
337 | (unsigned long)len, mc->name); |
338 | } |
339 | RETVAL = mc->key(p, len); |
340 | OUTPUT: |
341 | RETVAL |
342 | |
343 | MODULE = Catacomb PACKAGE = Catacomb::MAC |
344 | |
345 | SV * |
346 | DESTROY(m) |
347 | gMAC *m |
348 | CODE: |
349 | m->ops->destroy(m); |
350 | XSRETURN_UNDEF; |
351 | |
352 | ghash * |
353 | init(m) |
354 | gMAC *m |
355 | CODE: |
356 | RETVAL = m->ops->init(m); |
357 | OUTPUT: |
358 | RETVAL |
359 | |
360 | gcMAC * |
361 | class(m) |
362 | gMAC *m |
363 | CODE: |
364 | RETVAL = (gcMAC *)m->ops->c; |
365 | OUTPUT: |
366 | RETVAL |
367 | |
368 | MODULE = Catacomb PACKAGE = Catacomb::Rand |
369 | |
370 | SV * |
371 | seedint(r, seed) |
372 | grand *r |
373 | U32 seed |
374 | CODE: |
375 | if (!r->ops->misc(r, GRAND_CHECK, GRAND_SEEDUINT32)) |
376 | croak("rng `%s' doesn't support `seedint'", r->ops->name); |
377 | r->ops->misc(r, GRAND_SEEDUINT32, seed); |
378 | XSRETURN_UNDEF; |
379 | |
380 | SV * |
381 | seedblock(r, seed) |
382 | grand *r |
383 | SV *seed |
384 | PREINIT: |
385 | STRLEN len; |
386 | char *p; |
387 | CODE: |
388 | if (!r->ops->misc(r, GRAND_CHECK, GRAND_SEEDBLOCK)) |
389 | croak("rng `%s' doesn't support `seedblock'", r->ops->name); |
390 | p = SvPV(seed, len); |
391 | r->ops->misc(r, GRAND_SEEDBLOCK, p, len); |
392 | XSRETURN_UNDEF; |
393 | |
394 | SV * |
395 | seedmp(r, seed) |
396 | grand *r |
397 | mp *seed |
398 | CODE: |
399 | if (!r->ops->misc(r, GRAND_CHECK, GRAND_SEEDMP)) |
400 | croak("rng `%s' doesn't support `seedmp'", r->ops->name); |
401 | r->ops->misc(r, GRAND_SEEDMP, seed); |
402 | XSRETURN_UNDEF; |
403 | |
404 | SV * |
405 | seedrand(r, seed) |
406 | grand *r |
407 | grand *seed |
408 | CODE: |
409 | if (!r->ops->misc(r, GRAND_CHECK, GRAND_SEEDRAND)) |
410 | croak("rng `%s' doesn't support `seedrand'", r->ops->name); |
411 | r->ops->misc(r, GRAND_SEEDRAND, seed); |
412 | XSRETURN_UNDEF; |
413 | |
414 | U32 |
415 | raw(r) |
416 | grand *r |
417 | CODE: |
418 | RETVAL = r->ops->raw(r); |
419 | OUTPUT: |
420 | RETVAL |
421 | |
422 | U32 |
423 | word(r) |
424 | grand *r |
425 | CODE: |
426 | RETVAL = r->ops->word(r); |
427 | OUTPUT: |
428 | RETVAL |
429 | |
430 | U8 |
431 | byte(r) |
432 | grand *r |
433 | CODE: |
434 | RETVAL = r->ops->byte(r); |
435 | OUTPUT: |
436 | RETVAL |
437 | |
438 | char |
439 | char(r) |
440 | grand *r |
441 | CODE: |
442 | RETVAL = r->ops->byte(r); |
443 | OUTPUT: |
444 | RETVAL |
445 | |
446 | U32 |
447 | range(r, limit) |
448 | grand *r |
449 | U32 limit |
450 | CODE: |
451 | RETVAL = r->ops->range(r, limit); |
452 | OUTPUT: |
453 | RETVAL |
454 | |
455 | mp * |
456 | mp(r, bits, or = 0) |
457 | grand *r |
458 | unsigned or |
459 | unsigned bits |
460 | CODE: |
461 | RETVAL = mprand(MP_NEW, bits, r, or); |
462 | OUTPUT: |
463 | RETVAL |
464 | |
465 | mp * |
466 | mprange(r, limit) |
467 | grand *r |
468 | mp *limit |
469 | CODE: |
470 | RETVAL = mprand_range(MP_NEW, limit, r, 0); |
471 | OUTPUT: |
472 | RETVAL |
473 | |
474 | SV * |
475 | fill(r, n) |
476 | grand *r |
477 | size_t n |
478 | CODE: |
479 | RETVAL = NEWSV(0, n ? n : 1); |
480 | r->ops->fill(r, SvPVX(RETVAL), n); |
481 | SvCUR_set(RETVAL, n); |
482 | SvPOK_on(RETVAL); |
483 | OUTPUT: |
484 | RETVAL |
485 | |
486 | char * |
487 | name(r) |
488 | grand *r |
489 | CODE: |
490 | RETVAL = (char *)r->ops->name; |
491 | OUTPUT: |
492 | RETVAL |
493 | |
494 | U32 |
495 | flags(r) |
496 | grand *r |
497 | CODE: |
498 | RETVAL = r->ops->f; |
499 | OUTPUT: |
500 | RETVAL |
501 | |
502 | U32 |
503 | max(r) |
504 | grand *r |
505 | CODE: |
506 | RETVAL = r->ops->max; |
507 | OUTPUT: |
508 | RETVAL |
509 | |
510 | MODULE = Catacomb PACKAGE = Catacomb::Rand::True |
511 | |
512 | Rand_True * |
513 | _global(me) |
514 | SV *me |
515 | CODE: |
516 | RETVAL = &rand_global; |
517 | OUTPUT: |
518 | RETVAL |
519 | |
520 | Rand_True * |
521 | rand(me) |
522 | SV *me |
523 | CODE: |
524 | RETVAL = rand_create(); |
525 | OUTPUT: |
526 | RETVAL |
527 | |
528 | SV * |
529 | gate(r) |
530 | Rand_True *r |
531 | CODE: |
532 | r->ops->misc(r, RAND_GATE); |
533 | XSRETURN_UNDEF; |
534 | |
535 | SV * |
536 | stretch(r) |
537 | Rand_True *r |
538 | CODE: |
539 | r->ops->misc(r, RAND_STRETCH); |
540 | XSRETURN_UNDEF; |
541 | |
542 | SV * |
543 | key(r, k) |
544 | Rand_True *r |
545 | SV *k |
546 | PREINIT: |
547 | STRLEN len; |
548 | char *p; |
549 | CODE: |
550 | p = SvPV(k, len); |
551 | r->ops->misc(r, RAND_KEY, p, len); |
552 | XSRETURN_UNDEF; |
553 | |
554 | SV * |
555 | noisesrc(r) |
556 | Rand_True *r |
557 | CODE: |
558 | r->ops->misc(r, RAND_NOISESRC, &noise_source); |
559 | XSRETURN_UNDEF; |
560 | |
561 | SV * |
562 | seed(r, bits = 160) |
563 | Rand_True *r |
564 | int bits |
565 | CODE: |
566 | r->ops->misc(r, RAND_SEED, bits); |
567 | XSRETURN_UNDEF; |
568 | |
569 | MODULE = Catacomb PACKAGE = Catacomb::Rand::Fib |
570 | |
571 | SV * |
572 | new(me, seed) |
573 | SV *me |
574 | U32 seed |
575 | CODE: |
576 | RETVAL = MAKE(fibrand_create(seed), "Catacomb::Rand::Fib"); |
577 | OUTPUT: |
578 | RETVAL |
579 | |
580 | MODULE = Catacomb PACKAGE = Catacomb::Rand::LC |
581 | |
582 | SV * |
583 | new(me, seed) |
584 | SV *me |
585 | U32 seed |
586 | CODE: |
587 | RETVAL = MAKE(lcrand_create(seed), "Catacomb::Rand::LC"); |
588 | OUTPUT: |
589 | RETVAL |
590 | |
591 | MODULE = Catacomb PACKAGE = Catacomb::Rand::DSA |
592 | |
593 | Rand_DSA * |
594 | new(me, k) |
595 | SV *me |
596 | SV *k |
597 | PREINIT: |
598 | STRLEN len; |
599 | char *p; |
600 | CODE: |
601 | p = SvPV(k, len); |
602 | RETVAL = dsarand_create(p, len); |
603 | OUTPUT: |
604 | RETVAL |
605 | |
606 | SV * |
607 | passes(r, n) |
608 | Rand_DSA *r |
609 | unsigned n |
610 | CODE: |
611 | r->ops->misc(r, DSARAND_PASSES, n); |
612 | XSRETURN_UNDEF; |
613 | |
614 | SV * |
615 | seed(r) |
616 | Rand_DSA *r |
617 | PREINIT: |
618 | size_t sz; |
619 | CODE: |
620 | sz = r->ops->misc(r, DSARAND_SEEDSZ); |
621 | RETVAL = NEWSV(0, sz ? sz : 1); |
622 | r->ops->misc(r, DSARAND_GETSEED, SvPVX(RETVAL)); |
623 | SvCUR_set(RETVAL, sz); |
624 | SvPOK_on(RETVAL); |
625 | OUTPUT: |
626 | RETVAL |
627 | |
628 | MODULE = Catacomb PACKAGE = Catacomb::Rand::RC4 |
629 | |
630 | SV * |
631 | new(me, k) |
632 | SV *me |
633 | SV *k |
634 | PREINIT: |
635 | STRLEN len; |
636 | char *p; |
637 | CODE: |
638 | p = SvPV(k, len); |
639 | RETVAL = MAKE(rc4_rand(p, len), "Catacomb::Rand::RC4"); |
640 | OUTPUT: |
641 | RETVAL |
642 | |
643 | MODULE = Catacomb PACKAGE = Catacomb::Rand::SEAL |
644 | |
645 | SV * |
646 | new(me, k, n = 0) |
647 | SV *me |
648 | SV *k |
649 | U32 n |
650 | PREINIT: |
651 | STRLEN len; |
652 | char *p; |
653 | CODE: |
654 | p = SvPV(k, len); |
655 | RETVAL = MAKE(seal_rand(p, len, n), "Catacomb::Rand::SEAL"); |
656 | OUTPUT: |
657 | RETVAL |
658 | |
659 | MODULE = Catacomb PACKAGE = Catacomb::Rand::MGF |
660 | |
661 | SV * |
662 | new(name, k) |
663 | char *name |
664 | SV *k |
665 | CODE: |
666 | RETVAL = findrand(mgftab, "Catacomb::Rand::MGF", name, k); |
667 | OUTPUT: |
668 | RETVAL |
669 | |
670 | void |
671 | list(me) |
672 | SV *me |
673 | PPCODE: |
674 | listrand(mgftab); |
675 | |
676 | MODULE = Catacomb PACKAGE = Catacomb::Rand::Counter |
677 | |
678 | SV * |
679 | new(name, k) |
680 | char *name |
681 | SV *k |
682 | CODE: |
683 | RETVAL = findrand(ctrtab, "Catacomb::Rand::Counter", name, k); |
684 | OUTPUT: |
685 | RETVAL |
686 | |
687 | void |
688 | list(me) |
689 | SV *me |
690 | PPCODE: |
691 | listrand(ctrtab); |
692 | |
693 | MODULE = Catacomb PACKAGE = Catacomb::Rand::OFB |
694 | |
695 | SV * |
696 | new(name, k) |
697 | char *name |
698 | SV *k |
699 | CODE: |
700 | RETVAL = findrand(ofbtab, "Catacomb::Rand::OFB", name, k); |
701 | OUTPUT: |
702 | RETVAL |
703 | |
704 | void |
705 | list(me) |
706 | SV *me |
707 | PPCODE: |
708 | listrand(ofbtab); |
709 | |
710 | MODULE = Catacomb PACKAGE = Catacomb::Rand::Magic |
711 | |
712 | SV * |
713 | DESTROY(r) |
714 | grand *r |
715 | CODE: |
716 | XSRETURN_UNDEF; |
717 | |
718 | #----- That's all, folks ---------------------------------------------------- |