Make sure that all test files are gone before starting the tests, or
[openssl.git] / perl / openssl_cipher.xs
1
2 #include "openssl.h"
3
4 int boot_cipher()
5         {
6         SSLeay_add_all_ciphers();
7         return(1);
8         }
9
10 MODULE =  OpenSSL::Cipher       PACKAGE = OpenSSL::Cipher PREFIX = p5_EVP_C_
11
12 PROTOTYPES: ENABLE
13 VERSIONCHECK: DISABLE
14
15 void
16 p5_EVP_C_new(...)
17         PREINIT:
18                 EVP_CIPHER_CTX *ctx;
19                 const EVP_CIPHER *c;
20                 char *name;
21         PPCODE:
22                 if ((items == 1) && SvPOK(ST(0)))
23                         name=SvPV(ST(0),na);
24                 else if ((items == 2) && SvPOK(ST(1)))
25                         name=SvPV(ST(1),na);
26                 else
27                         croak("Usage: OpenSSL::Cipher::new(type)");
28                 PUSHs(sv_newmortal());
29                 c=EVP_get_cipherbyname(name);
30                 if (c != NULL)
31                         {
32                         ctx=malloc(sizeof(EVP_CIPHER_CTX));
33                         EVP_EncryptInit(ctx,c,NULL,NULL);
34                         sv_setref_pv(ST(0), "OpenSSL::Cipher", (void*)ctx);
35                         }
36
37 datum
38 p5_EVP_C_name(ctx)
39         EVP_CIPHER_CTX *ctx
40         CODE:
41                 RETVAL.dptr=OBJ_nid2ln(EVP_CIPHER_CTX_nid(ctx));
42                 RETVAL.dsize=strlen(RETVAL.dptr);
43         OUTPUT:
44                 RETVAL
45
46 int
47 p5_EVP_C_key_length(ctx)
48         EVP_CIPHER_CTX *ctx
49         CODE:
50                 RETVAL=EVP_CIPHER_CTX_key_length(ctx);
51         OUTPUT:
52                 RETVAL
53
54 int
55 p5_EVP_C_iv_length(ctx)
56         EVP_CIPHER_CTX *ctx
57         CODE:
58                 RETVAL=EVP_CIPHER_CTX_iv_length(ctx);
59         OUTPUT:
60                 RETVAL
61         
62 int
63 p5_EVP_C_block_size(ctx)
64         EVP_CIPHER_CTX *ctx
65         CODE:
66                 RETVAL=EVP_CIPHER_CTX_block_size(ctx);
67         OUTPUT:
68                 RETVAL
69         
70 void
71 p5_EVP_C_init(ctx,key,iv,enc)
72         EVP_CIPHER_CTX *ctx
73         datum key
74         datum iv
75         int enc
76         PREINIT:
77                 char loc_iv[EVP_MAX_IV_LENGTH];
78                 char loc_key[EVP_MAX_KEY_LENGTH];
79                 char *ip=loc_iv,*kp=loc_key;
80                 int i;
81                 memset(loc_iv,0,EVP_MAX_IV_LENGTH);
82                 memset(loc_key,0,EVP_MAX_KEY_LENGTH);
83         CODE:
84                 i=key.dsize;
85                 if (key.dsize > EVP_CIPHER_CTX_key_length(ctx))
86                         i=EVP_CIPHER_CTX_key_length(ctx);
87                 if (i > 0)
88                         {
89                         memset(kp,0,EVP_MAX_KEY_LENGTH);
90                         memcpy(kp,key.dptr,i);
91                         }
92                 else
93                         kp=NULL;
94                 i=iv.dsize;
95                 if (iv.dsize > EVP_CIPHER_CTX_iv_length(ctx))
96                         i=EVP_CIPHER_CTX_iv_length(ctx);
97                 if (i > 0)
98                         {
99                         memcpy(ip,iv.dptr,i);
100                         memset(ip,0,EVP_MAX_IV_LENGTH);
101                         }
102                 else
103                         ip=NULL;
104                 EVP_CipherInit(ctx,EVP_CIPHER_CTX_cipher(ctx),kp,ip,enc);
105                 memset(loc_key,0,sizeof(loc_key));
106                 memset(loc_iv,0,sizeof(loc_iv));
107
108 SV *
109 p5_EVP_C_cipher(ctx,in)
110         EVP_CIPHER_CTX *ctx;
111         datum in;
112         CODE:
113                 RETVAL=newSVpv("",0);
114                 SvGROW(RETVAL,in.dsize+EVP_CIPHER_CTX_block_size(ctx)+1);
115                 EVP_Cipher(ctx,SvPV(RETVAL,na),in.dptr,in.dsize);
116                 SvCUR_set(RETVAL,in.dsize);
117         OUTPUT:
118                 RETVAL
119
120 SV *
121 p5_EVP_C_update(ctx, in)
122         EVP_CIPHER_CTX *ctx
123         datum in
124         PREINIT:
125         int i;
126         CODE:
127                 RETVAL=newSVpv("",0);
128                 SvGROW(RETVAL,in.dsize+EVP_CIPHER_CTX_block_size(ctx)+1);
129                 EVP_CipherUpdate(ctx,SvPV(RETVAL,na),&i,in.dptr,in.dsize);
130                 SvCUR_set(RETVAL,i);
131         OUTPUT:
132                 RETVAL
133
134 SV *
135 p5_EVP_C_final(ctx)
136         EVP_CIPHER_CTX *ctx
137         PREINIT:
138         int i;
139         CODE:
140                 RETVAL=newSVpv("",0);
141                 SvGROW(RETVAL,EVP_CIPHER_CTX_block_size(ctx)+1);
142                 if (!EVP_CipherFinal(ctx,SvPV(RETVAL,na),&i))
143                         sv_setpv(RETVAL,"BAD DECODE");
144                 else
145                         SvCUR_set(RETVAL,i);
146         OUTPUT:
147                 RETVAL
148
149 void
150 p5_EVP_C_DESTROY(ctx)
151         EVP_CIPHER_CTX *ctx
152         CODE:
153         free((char *)ctx);
154