summaryrefslogtreecommitdiffstats
path: root/perl/cipher.xs
blob: 1044d7a4ef861f046b979f5c849897e58df77c6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "p5SSLeay.h"

int boot_cipher()
	{
        SSLeay_add_all_ciphers();
	return(1);
	}

MODULE =  SSLeay::Cipher	PACKAGE = SSLeay::Cipher PREFIX = p5_EVP_C_

VERSIONCHECK: DISABLE

void
p5_EVP_C_new(...)
	PREINIT:
		EVP_CIPHER_CTX *ctx;
		EVP_CIPHER *c;
		char *name;
	PPCODE:
		if ((items == 1) && SvPOK(ST(0)))
			name=SvPV(ST(0),na);
		else if ((items == 2) && SvPOK(ST(1)))
			name=SvPV(ST(1),na);
		else
			croak("Usage: SSLeay::Cipher::new(type)");
		PUSHs(sv_newmortal());
		c=EVP_get_cipherbyname(name);
		if (c != NULL)
			{
			ctx=malloc(sizeof(EVP_CIPHER_CTX));
			EVP_EncryptInit(ctx,c,NULL,NULL);
			sv_setref_pv(ST(0), "SSLeay::Cipher", (void*)ctx);
			}

datum
p5_EVP_C_name(ctx)
	EVP_CIPHER_CTX *ctx
	CODE:
		RETVAL.dptr=OBJ_nid2ln(EVP_CIPHER_CTX_nid(ctx));
		RETVAL.dsize=strlen(RETVAL.dptr);
	OUTPUT:
		RETVAL

int
p5_EVP_C_key_length(ctx)
	EVP_CIPHER_CTX *ctx
	CODE:
		RETVAL=EVP_CIPHER_CTX_key_length(ctx);
	OUTPUT:
		RETVAL

int
p5_EVP_C_iv_length(ctx)
	EVP_CIPHER_CTX *ctx
	CODE:
		RETVAL=EVP_CIPHER_CTX_iv_length(ctx);
	OUTPUT:
		RETVAL
	
int
p5_EVP_C_block_size(ctx)
	EVP_CIPHER_CTX *ctx
	CODE:
		RETVAL=EVP_CIPHER_CTX_block_size(ctx);
	OUTPUT:
		RETVAL
	
void
p5_EVP_C_init(ctx,key,iv,enc)
	EVP_CIPHER_CTX *ctx
	datum key
	datum iv
	int enc
	PREINIT:
		char loc_iv[EVP_MAX_IV_LENGTH];
		char loc_key[EVP_MAX_KEY_LENGTH];
		char *ip=loc_iv,*kp=loc_key;
		int i;
		memset(loc_iv,0,EVP_MAX_IV_LENGTH);
		memset(loc_key,0,EVP_MAX_KEY_LENGTH);
	CODE:
		i=key.dsize;
		if (key.dsize > EVP_CIPHER_CTX_key_length(ctx))
			i=EVP_CIPHER_CTX_key_length(ctx);
		if (i > 0)
			{
			memset(kp,0,EVP_MAX_KEY_LENGTH);
			memcpy(kp,key.dptr,i);
			}
		else
			kp=NULL;
		i=iv.dsize;
		if (iv.dsize > EVP_CIPHER_CTX_iv_length(ctx))
			i=EVP_CIPHER_CTX_iv_length(ctx);
		if (i > 0)
			{
			memcpy(ip,iv.dptr,i);
			memset(ip,0,EVP_MAX_IV_LENGTH);
			}
		else
			ip=NULL;
		EVP_CipherInit(ctx,EVP_CIPHER_CTX_cipher(ctx),kp,ip,enc);
		memset(loc_key,0,sizeof(loc_key));
		memset(loc_iv,0,sizeof(loc_iv));

SV *
p5_EVP_C_cipher(ctx,in)
	EVP_CIPHER_CTX *ctx;
	datum in;
	CODE:
		RETVAL=newSVpv("",0);
		SvGROW(RETVAL,in.dsize+EVP_CIPHER_CTX_block_size(ctx)+1);
		EVP_Cipher(ctx,SvPV(RETVAL,na),in.dptr,in.dsize);
		SvCUR_set(RETVAL,in.dsize);
	OUTPUT:
		RETVAL

SV *
p5_EVP_C_update(ctx, in)
	EVP_CIPHER_CTX *ctx
	datum in
	PREINIT:
	int i;
	CODE:
		RETVAL=newSVpv("",0);
		SvGROW(RETVAL,in.dsize+EVP_CIPHER_CTX_block_size(ctx)+1);
		EVP_CipherUpdate(ctx,SvPV(RETVAL,na),&i,in.dptr,in.dsize);
		SvCUR_set(RETVAL,i);
	OUTPUT:
		RETVAL

SV *
p5_EVP_C_final(ctx)
	EVP_CIPHER_CTX *ctx
	PREINIT:
	int i;
	CODE:
		RETVAL=newSVpv("",0);
		SvGROW(RETVAL,EVP_CIPHER_CTX_block_size(ctx)+1);
		if (!EVP_CipherFinal(ctx,SvPV(RETVAL,na),&i))
			sv_setpv(RETVAL,"BAD DECODE");
		else
			SvCUR_set(RETVAL,i);
	OUTPUT:
		RETVAL

void
p5_EVP_C_DESTROY(ctx)
	EVP_CIPHER_CTX *ctx
	CODE:
	free((char *)ctx);