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