ECToken 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/ext/ECToken/ECToken.c +223 -0
- data/ext/ECToken/extconf.rb +7 -0
- data/lib/ECToken.rb +2 -0
- data/lib/ECToken/version.rb +3 -0
- metadata +50 -0
@@ -0,0 +1,223 @@
|
|
1
|
+
//
|
2
|
+
// ECToken.c
|
3
|
+
//
|
4
|
+
//
|
5
|
+
// Created by Darren Fung on 12-06-19.
|
6
|
+
// Copyright (c) 2012 Tunezy Inc. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
#include <openssl/blowfish.h>
|
10
|
+
#include <stdio.h>
|
11
|
+
#include <stdlib.h>
|
12
|
+
#include <string.h>
|
13
|
+
#include <ruby.h>
|
14
|
+
|
15
|
+
#define kMAX_TOKEN_LENGTH 512
|
16
|
+
|
17
|
+
#define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \
|
18
|
+
l|=((unsigned long)(*((c)++)))<<16L, \
|
19
|
+
l|=((unsigned long)(*((c)++)))<< 8L, \
|
20
|
+
l|=((unsigned long)(*((c)++))))
|
21
|
+
|
22
|
+
#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
|
23
|
+
*((c)++)=(unsigned char)(((l)>>16L)&0xff), \
|
24
|
+
*((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
|
25
|
+
*((c)++)=(unsigned char)(((l) )&0xff))
|
26
|
+
|
27
|
+
|
28
|
+
void cfb64_encrypt(const unsigned char* in, unsigned char* out, long length,
|
29
|
+
BF_KEY* schedule,
|
30
|
+
unsigned char* ivec,
|
31
|
+
int *num,
|
32
|
+
int encrypt)
|
33
|
+
{
|
34
|
+
register BF_LONG v0,v1,t;
|
35
|
+
register int n= *num;
|
36
|
+
register long l=length;
|
37
|
+
BF_LONG ti[2];
|
38
|
+
unsigned char *iv,c,cc;
|
39
|
+
|
40
|
+
iv=(unsigned char *)ivec;
|
41
|
+
while (l--)
|
42
|
+
{
|
43
|
+
if (n == 0)
|
44
|
+
{
|
45
|
+
n2l(iv,v0); ti[0]=v0;
|
46
|
+
n2l(iv,v1); ti[1]=v1;
|
47
|
+
BF_encrypt((BF_LONG*)ti,schedule);
|
48
|
+
iv=(unsigned char *)ivec;
|
49
|
+
t=ti[0]; l2n(t,iv);
|
50
|
+
t=ti[1]; l2n(t,iv);
|
51
|
+
iv=(unsigned char *)ivec;
|
52
|
+
}
|
53
|
+
c= *(in++)^iv[n];
|
54
|
+
*(out++)=c;
|
55
|
+
iv[n]=c;
|
56
|
+
n=(n+1)&0x07;
|
57
|
+
}
|
58
|
+
v0=v1=ti[0]=ti[1]=t=c=cc=0;
|
59
|
+
*num=n;
|
60
|
+
}
|
61
|
+
|
62
|
+
|
63
|
+
/************************************************** ****************
|
64
|
+
* ARGS:
|
65
|
+
* keydata == ascii text, the encryption passphrase
|
66
|
+
* keydatalen == how long keydata is
|
67
|
+
* in == the data to be encrypted
|
68
|
+
* out == the encrypted data.
|
69
|
+
* length(in) == length(out), apparently
|
70
|
+
* inlen == length of the in array
|
71
|
+
************************************************** ****************/
|
72
|
+
void bfencrypt(unsigned char *keydata, int keydatalen, const unsigned char *in,
|
73
|
+
unsigned char *out, unsigned int inlen) {
|
74
|
+
BF_KEY key;
|
75
|
+
unsigned char ivec[32];
|
76
|
+
int num=0;
|
77
|
+
// set up for encryption
|
78
|
+
BF_set_key(&key, keydatalen, keydata);
|
79
|
+
memset(ivec, '\0', 32);
|
80
|
+
cfb64_encrypt(in, out, inlen, &key, ivec, &num, BF_ENCRYPT);
|
81
|
+
}
|
82
|
+
|
83
|
+
void bfdecrypt(unsigned char *keydata, int keydatalen, const unsigned char *in,
|
84
|
+
unsigned char *out, unsigned int inlen) {
|
85
|
+
BF_KEY key;
|
86
|
+
unsigned char ivec[32];
|
87
|
+
int num=0;
|
88
|
+
// set up for decryption
|
89
|
+
BF_set_key(&key, keydatalen, keydata);
|
90
|
+
memset(ivec, '\0', 32);
|
91
|
+
BF_cfb64_encrypt(in, out, inlen, &key, ivec, &num, BF_DECRYPT);
|
92
|
+
}
|
93
|
+
|
94
|
+
static VALUE encrypt(VALUE self, VALUE k, VALUE t) {
|
95
|
+
char *key = StringValueCStr(k);
|
96
|
+
char *string = StringValueCStr(t);
|
97
|
+
char estr[kMAX_TOKEN_LENGTH*4];
|
98
|
+
|
99
|
+
if (strlen(string)+1 > kMAX_TOKEN_LENGTH) // this line should protect us from the sprintf
|
100
|
+
{
|
101
|
+
int token_length = kMAX_TOKEN_LENGTH;
|
102
|
+
printf("Only max of %i char is allowed\n", token_length);
|
103
|
+
exit(0);
|
104
|
+
}
|
105
|
+
|
106
|
+
// For backward compatibility, check if somebody already passed in ec_secure=1
|
107
|
+
|
108
|
+
// search for ec_secure in string
|
109
|
+
// delete any previous instance of ec_secure=1 that resides within the string
|
110
|
+
// this application will now prepend ec_secure=LENGTH_OF_STRING
|
111
|
+
// so a valid string may end up being encrypted as ec_secure=033&ec_clientip=1.1.1.1
|
112
|
+
char* ecsecure_check_ptr = strstr(string, "ec_secure=1");
|
113
|
+
|
114
|
+
// buffer we will hold the new modified string
|
115
|
+
char newBuff[(kMAX_TOKEN_LENGTH*2)-1];
|
116
|
+
memset(newBuff,0,sizeof(newBuff));
|
117
|
+
|
118
|
+
if(ecsecure_check_ptr > 0) // we found ec_secure within the string
|
119
|
+
{
|
120
|
+
if(string == ecsecure_check_ptr)
|
121
|
+
strcpy(newBuff,ecsecure_check_ptr+=12); // found at beginning, skip over and copy the rest of the string
|
122
|
+
else // it's somewhere else in the string, scrub it out
|
123
|
+
{
|
124
|
+
*ecsecure_check_ptr = 0; // break the string into two parts, first string null terminate where we found ec_secure
|
125
|
+
sprintf(newBuff,"%s%s", string,ecsecure_check_ptr+11);
|
126
|
+
// above we combine what was before ec_secure with what is after ec_secure's start position plus 11 octets
|
127
|
+
}
|
128
|
+
}
|
129
|
+
else // there was no ec_secure found within the string, so we just copy the string
|
130
|
+
strcpy(newBuff,string);
|
131
|
+
|
132
|
+
// setup the buffer we will pass off to blowfish
|
133
|
+
char newbuffer[(kMAX_TOKEN_LENGTH*2)-1];
|
134
|
+
memset(newbuffer,0,sizeof(newbuffer));
|
135
|
+
// prepend with ec_secure=032, for example
|
136
|
+
sprintf(newbuffer, "ec_secure=%03d&%s",(int)(strlen(newBuff)+14), newBuff);
|
137
|
+
|
138
|
+
// encrypt the new buffer
|
139
|
+
bfencrypt((unsigned char*)key, strlen(key), (unsigned char*)newbuffer,
|
140
|
+
(unsigned char*)estr, strlen(newbuffer)+1);
|
141
|
+
|
142
|
+
// convert to hex string
|
143
|
+
unsigned int i = 0;
|
144
|
+
char final_token[strlen(newbuffer)];
|
145
|
+
char* current_ptr = &final_token[0];
|
146
|
+
for(i=0; i<strlen(newbuffer); i++) {
|
147
|
+
sprintf(current_ptr,"%02x",estr[i]&0xff);
|
148
|
+
// increment pointer by 2 (2 chars in hex decimal)
|
149
|
+
current_ptr += 2 * sizeof(char);
|
150
|
+
}
|
151
|
+
|
152
|
+
return rb_str_new2(final_token);
|
153
|
+
|
154
|
+
}
|
155
|
+
|
156
|
+
void Init_ECToken(void) {
|
157
|
+
VALUE klass = rb_define_class("EdgeCastToken", rb_cObject);
|
158
|
+
rb_define_singleton_method(klass, "encrypt", encrypt, 2);
|
159
|
+
}
|
160
|
+
|
161
|
+
/*int main(int argc, char **argv)
|
162
|
+
{
|
163
|
+
if (argc < 3)
|
164
|
+
{
|
165
|
+
printf("Usage: ec_encrpyt <key> <text>\n");
|
166
|
+
exit(0);
|
167
|
+
}
|
168
|
+
|
169
|
+
char *key = argv[1];
|
170
|
+
char *string = argv[2];
|
171
|
+
char estr[kMAX_TOKEN_LENGTH*4];
|
172
|
+
|
173
|
+
if (strlen(string)+1 > kMAX_TOKEN_LENGTH) // this line should protect us from the sprintf
|
174
|
+
{
|
175
|
+
int token_length = kMAX_TOKEN_LENGTH;
|
176
|
+
printf("Only max of %i char is allowed\n", token_length);
|
177
|
+
exit(0);
|
178
|
+
}
|
179
|
+
|
180
|
+
// For backward compatibility, check if somebody already passed in ec_secure=1
|
181
|
+
|
182
|
+
// search for ec_secure in string
|
183
|
+
// delete any previous instance of ec_secure=1 that resides within the string
|
184
|
+
// this application will now prepend ec_secure=LENGTH_OF_STRING
|
185
|
+
// so a valid string may end up being encrypted as ec_secure=033&ec_clientip=1.1.1.1
|
186
|
+
char* ecsecure_check_ptr = strstr(string, "ec_secure=1");
|
187
|
+
|
188
|
+
// buffer we will hold the new modified string
|
189
|
+
char newBuff[(kMAX_TOKEN_LENGTH*2)-1];
|
190
|
+
memset(newBuff,0,sizeof(newBuff));
|
191
|
+
|
192
|
+
if(ecsecure_check_ptr > 0) // we found ec_secure within the string
|
193
|
+
{
|
194
|
+
if(string == ecsecure_check_ptr)
|
195
|
+
strcpy(newBuff,ecsecure_check_ptr+=12); // found at beginning, skip over and copy the rest of the string
|
196
|
+
else // it's somewhere else in the string, scrub it out
|
197
|
+
{
|
198
|
+
*ecsecure_check_ptr = 0; // break the string into two parts, first string null terminate where we found ec_secure
|
199
|
+
sprintf(newBuff,"%s%s", string,ecsecure_check_ptr+11);
|
200
|
+
// above we combine what was before ec_secure with what is after ec_secure's start position plus 11 octets
|
201
|
+
}
|
202
|
+
}
|
203
|
+
else // there was no ec_secure found within the string, so we just copy the string
|
204
|
+
strcpy(newBuff,string);
|
205
|
+
|
206
|
+
// setup the buffer we will pass off to blowfish
|
207
|
+
char newbuffer[(kMAX_TOKEN_LENGTH*2)-1];
|
208
|
+
memset(newbuffer,0,sizeof(newbuffer));
|
209
|
+
// prepend with ec_secure=032, for example
|
210
|
+
sprintf(newbuffer, "ec_secure=%03d&%s",(int)(strlen(newBuff)+14), newBuff);
|
211
|
+
|
212
|
+
// encrypt the new buffer
|
213
|
+
bfencrypt((unsigned char*)key, strlen(key), (unsigned char*)newbuffer,
|
214
|
+
(unsigned char*)estr, strlen(newbuffer)+1);
|
215
|
+
|
216
|
+
// convert to hex string
|
217
|
+
for(unsigned int i=0; i<strlen(newbuffer); i++)
|
218
|
+
printf("%02x",estr[i]&0xff);
|
219
|
+
printf("\n");
|
220
|
+
|
221
|
+
return 0;
|
222
|
+
}*/
|
223
|
+
|
data/lib/ECToken.rb
ADDED
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ECToken
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Darren Fung
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-20 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Encrypts using BF-CFB
|
15
|
+
email:
|
16
|
+
- darren@tunezy.com
|
17
|
+
executables: []
|
18
|
+
extensions:
|
19
|
+
- ext/ECToken/extconf.rb
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/ECToken/version.rb
|
23
|
+
- lib/ECToken.rb
|
24
|
+
- ext/ECToken/ECToken.c
|
25
|
+
- ext/ECToken/extconf.rb
|
26
|
+
homepage: ''
|
27
|
+
licenses: []
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project: ECToken
|
46
|
+
rubygems_version: 1.8.15
|
47
|
+
signing_key:
|
48
|
+
specification_version: 3
|
49
|
+
summary: Encrypts using BF-CFB
|
50
|
+
test_files: []
|