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