DES.xs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "EXTERN.h"
  2. #include "perl.h"
  3. #include "XSUB.h"
  4. #include "des.h"
  5. #define deschar char
  6. static STRLEN len;
  7. static int
  8. not_here(s)
  9. char *s;
  10. {
  11. croak("%s not implemented on this architecture", s);
  12. return -1;
  13. }
  14. MODULE = DES PACKAGE = DES PREFIX = des_
  15. char *
  16. des_crypt(buf,salt)
  17. char * buf
  18. char * salt
  19. void
  20. des_set_odd_parity(key)
  21. des_cblock * key
  22. PPCODE:
  23. {
  24. SV *s;
  25. s=sv_newmortal();
  26. sv_setpvn(s,(char *)key,8);
  27. des_set_odd_parity((des_cblock *)SvPV(s,na));
  28. PUSHs(s);
  29. }
  30. int
  31. des_is_weak_key(key)
  32. des_cblock * key
  33. des_key_schedule
  34. des_set_key(key)
  35. des_cblock * key
  36. CODE:
  37. des_set_key(key,RETVAL);
  38. OUTPUT:
  39. RETVAL
  40. des_cblock
  41. des_ecb_encrypt(input,ks,encrypt)
  42. des_cblock * input
  43. des_key_schedule * ks
  44. int encrypt
  45. CODE:
  46. des_ecb_encrypt(input,&RETVAL,*ks,encrypt);
  47. OUTPUT:
  48. RETVAL
  49. void
  50. des_cbc_encrypt(input,ks,ivec,encrypt)
  51. char * input
  52. des_key_schedule * ks
  53. des_cblock * ivec
  54. int encrypt
  55. PPCODE:
  56. {
  57. SV *s;
  58. STRLEN len,l;
  59. char *c;
  60. l=SvCUR(ST(0));
  61. len=((((unsigned long)l)+7)/8)*8;
  62. s=sv_newmortal();
  63. sv_setpvn(s,"",0);
  64. SvGROW(s,len);
  65. SvCUR_set(s,len);
  66. c=(char *)SvPV(s,na);
  67. des_cbc_encrypt((des_cblock *)input,(des_cblock *)c,
  68. l,*ks,ivec,encrypt);
  69. sv_setpvn(ST(2),(char *)c[len-8],8);
  70. PUSHs(s);
  71. }
  72. void
  73. des_cbc3_encrypt(input,ks1,ks2,ivec1,ivec2,encrypt)
  74. char * input
  75. des_key_schedule * ks1
  76. des_key_schedule * ks2
  77. des_cblock * ivec1
  78. des_cblock * ivec2
  79. int encrypt
  80. PPCODE:
  81. {
  82. SV *s;
  83. STRLEN len,l;
  84. l=SvCUR(ST(0));
  85. len=((((unsigned long)l)+7)/8)*8;
  86. s=sv_newmortal();
  87. sv_setpvn(s,"",0);
  88. SvGROW(s,len);
  89. SvCUR_set(s,len);
  90. des_3cbc_encrypt((des_cblock *)input,(des_cblock *)SvPV(s,na),
  91. l,*ks1,*ks2,ivec1,ivec2,encrypt);
  92. sv_setpvn(ST(3),(char *)ivec1,8);
  93. sv_setpvn(ST(4),(char *)ivec2,8);
  94. PUSHs(s);
  95. }
  96. void
  97. des_cbc_cksum(input,ks,ivec)
  98. char * input
  99. des_key_schedule * ks
  100. des_cblock * ivec
  101. PPCODE:
  102. {
  103. SV *s1,*s2;
  104. STRLEN len,l;
  105. des_cblock c;
  106. unsigned long i1,i2;
  107. s1=sv_newmortal();
  108. s2=sv_newmortal();
  109. l=SvCUR(ST(0));
  110. des_cbc_cksum((des_cblock *)input,(des_cblock *)c,
  111. l,*ks,ivec);
  112. i1=c[4]|(c[5]<<8)|(c[6]<<16)|(c[7]<<24);
  113. i2=c[0]|(c[1]<<8)|(c[2]<<16)|(c[3]<<24);
  114. sv_setiv(s1,i1);
  115. sv_setiv(s2,i2);
  116. sv_setpvn(ST(2),(char *)c,8);
  117. PUSHs(s1);
  118. PUSHs(s2);
  119. }
  120. void
  121. des_cfb_encrypt(input,numbits,ks,ivec,encrypt)
  122. char * input
  123. int numbits
  124. des_key_schedule * ks
  125. des_cblock * ivec
  126. int encrypt
  127. PPCODE:
  128. {
  129. SV *s;
  130. STRLEN len;
  131. char *c;
  132. len=SvCUR(ST(0));
  133. s=sv_newmortal();
  134. sv_setpvn(s,"",0);
  135. SvGROW(s,len);
  136. SvCUR_set(s,len);
  137. c=(char *)SvPV(s,na);
  138. des_cfb_encrypt((unsigned char *)input,(unsigned char *)c,
  139. (int)numbits,(long)len,*ks,ivec,encrypt);
  140. sv_setpvn(ST(3),(char *)ivec,8);
  141. PUSHs(s);
  142. }
  143. des_cblock *
  144. des_ecb3_encrypt(input,ks1,ks2,encrypt)
  145. des_cblock * input
  146. des_key_schedule * ks1
  147. des_key_schedule * ks2
  148. int encrypt
  149. CODE:
  150. {
  151. des_cblock c;
  152. des_ecb3_encrypt((des_cblock *)input,(des_cblock *)&c,
  153. *ks1,*ks2,encrypt);
  154. RETVAL= &c;
  155. }
  156. OUTPUT:
  157. RETVAL
  158. void
  159. des_ofb_encrypt(input,numbits,ks,ivec)
  160. unsigned char * input
  161. int numbits
  162. des_key_schedule * ks
  163. des_cblock * ivec
  164. PPCODE:
  165. {
  166. SV *s;
  167. STRLEN len,l;
  168. unsigned char *c;
  169. len=SvCUR(ST(0));
  170. s=sv_newmortal();
  171. sv_setpvn(s,"",0);
  172. SvGROW(s,len);
  173. SvCUR_set(s,len);
  174. c=(unsigned char *)SvPV(s,na);
  175. des_ofb_encrypt((unsigned char *)input,(unsigned char *)c,
  176. numbits,len,*ks,ivec);
  177. sv_setpvn(ST(3),(char *)ivec,8);
  178. PUSHs(s);
  179. }
  180. void
  181. des_pcbc_encrypt(input,ks,ivec,encrypt)
  182. char * input
  183. des_key_schedule * ks
  184. des_cblock * ivec
  185. int encrypt
  186. PPCODE:
  187. {
  188. SV *s;
  189. STRLEN len,l;
  190. char *c;
  191. l=SvCUR(ST(0));
  192. len=((((unsigned long)l)+7)/8)*8;
  193. s=sv_newmortal();
  194. sv_setpvn(s,"",0);
  195. SvGROW(s,len);
  196. SvCUR_set(s,len);
  197. c=(char *)SvPV(s,na);
  198. des_pcbc_encrypt((des_cblock *)input,(des_cblock *)c,
  199. l,*ks,ivec,encrypt);
  200. sv_setpvn(ST(2),(char *)c[len-8],8);
  201. PUSHs(s);
  202. }
  203. des_cblock *
  204. des_random_key()
  205. CODE:
  206. {
  207. des_cblock c;
  208. des_random_key(c);
  209. RETVAL=&c;
  210. }
  211. OUTPUT:
  212. RETVAL
  213. des_cblock *
  214. des_string_to_key(str)
  215. char * str
  216. CODE:
  217. {
  218. des_cblock c;
  219. des_string_to_key(str,&c);
  220. RETVAL=&c;
  221. }
  222. OUTPUT:
  223. RETVAL
  224. void
  225. des_string_to_2keys(str)
  226. char * str
  227. PPCODE:
  228. {
  229. des_cblock c1,c2;
  230. SV *s1,*s2;
  231. des_string_to_2keys(str,&c1,&c2);
  232. EXTEND(sp,2);
  233. s1=sv_newmortal();
  234. sv_setpvn(s1,(char *)c1,8);
  235. s2=sv_newmortal();
  236. sv_setpvn(s2,(char *)c2,8);
  237. PUSHs(s1);
  238. PUSHs(s2);
  239. }