mirror of
https://github.com/zhigang1992/shadowsocks-iOS.git
synced 2026-04-24 04:54:51 +08:00
move table encryption to another file
This commit is contained in:
@@ -37,6 +37,7 @@
|
||||
628693F816DA2817008B1A26 /* ProxySettingsTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 628693F416DA2817008B1A26 /* ProxySettingsTableViewController.m */; };
|
||||
628693FE16DA2983008B1A26 /* ev.c in Sources */ = {isa = PBXBuildFile; fileRef = 628693FD16DA2983008B1A26 /* ev.c */; };
|
||||
62CD119A175E3BD7008C4D0C /* libcrypto.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 62CD1199175E3BD7008C4D0C /* libcrypto.a */; };
|
||||
62CD119C175E3E17008C4D0C /* table.m in Sources */ = {isa = PBXBuildFile; fileRef = 62CD119B175E3E17008C4D0C /* table.m */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
@@ -93,6 +94,8 @@
|
||||
628693F416DA2817008B1A26 /* ProxySettingsTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ProxySettingsTableViewController.m; sourceTree = "<group>"; };
|
||||
628693FD16DA2983008B1A26 /* ev.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ev.c; path = libev/ev.c; sourceTree = "<group>"; };
|
||||
62CD1199175E3BD7008C4D0C /* libcrypto.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libcrypto.a; path = "OpenSSL-for-iPhone/lib/libcrypto.a"; sourceTree = "<group>"; };
|
||||
62CD119B175E3E17008C4D0C /* table.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = table.m; sourceTree = "<group>"; };
|
||||
62CD119D175E3E23008C4D0C /* table.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = table.h; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
@@ -255,10 +258,12 @@
|
||||
628693FA16DA2823008B1A26 /* Shadowsocks */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
628693EE16DA2815008B1A26 /* encrypt.m */,
|
||||
628693EF16DA2816008B1A26 /* encrypt.h */,
|
||||
628693F016DA2816008B1A26 /* local.m */,
|
||||
628693EE16DA2815008B1A26 /* encrypt.m */,
|
||||
62CD119D175E3E23008C4D0C /* table.h */,
|
||||
62CD119B175E3E17008C4D0C /* table.m */,
|
||||
628693F116DA2816008B1A26 /* local.h */,
|
||||
628693F016DA2816008B1A26 /* local.m */,
|
||||
628693F216DA2816008B1A26 /* socks5.h */,
|
||||
628693F316DA2817008B1A26 /* ProxySettingsTableViewController.h */,
|
||||
628693F416DA2817008B1A26 /* ProxySettingsTableViewController.m */,
|
||||
@@ -353,6 +358,7 @@
|
||||
628693F716DA2817008B1A26 /* local.m in Sources */,
|
||||
628693F816DA2817008B1A26 /* ProxySettingsTableViewController.m in Sources */,
|
||||
628693FE16DA2983008B1A26 /* ev.c in Sources */,
|
||||
62CD119C175E3E17008C4D0C /* table.m in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
||||
@@ -1,19 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
unsigned char encrypt_table[256];
|
||||
unsigned char decrypt_table[256];
|
||||
__deprecated void get_table(const char* key);
|
||||
void init_encryption(const char* password, const char*method);
|
||||
void encrypt(char *buf, int len);
|
||||
void decrypt(char *buf, int len);
|
||||
void init_encryption(const char* password, const char*method);
|
||||
|
||||
int send_encrypt(int sock, char *buf, int len, int flags);
|
||||
int recv_decrypt(int sock, char *buf, int len, int flags);
|
||||
|
||||
unsigned int _i;
|
||||
unsigned long long _a;
|
||||
|
||||
int recv_decrypt(int sock, char *buf, int len, int flags);
|
||||
@@ -1,133 +1,15 @@
|
||||
#import <stdint.h>
|
||||
#import <openssl/md5.h>
|
||||
#import <openssl/evp.h>
|
||||
#include "encrypt.h"
|
||||
|
||||
#import <sys/socket.h>
|
||||
|
||||
static int random_compare(const void *_x, const void *_y) {
|
||||
uint32_t i = _i;
|
||||
uint64_t a = _a;
|
||||
uint8_t x = *((uint8_t *) _x);
|
||||
uint8_t y = *((uint8_t*) _y);
|
||||
return (a % (x + i) - a % (y + i));
|
||||
}
|
||||
|
||||
|
||||
static void merge(uint8_t *left, int llength, uint8_t *right, int rlength)
|
||||
{
|
||||
/* Temporary memory locations for the 2 segments of the array to merge. */
|
||||
uint8_t *ltmp = (uint8_t *) malloc(llength * sizeof(uint8_t));
|
||||
uint8_t *rtmp = (uint8_t *) malloc(rlength * sizeof(uint8_t));
|
||||
|
||||
/*
|
||||
* Pointers to the elements being sorted in the temporary memory locations.
|
||||
*/
|
||||
uint8_t *ll = ltmp;
|
||||
uint8_t *rr = rtmp;
|
||||
|
||||
uint8_t *result = left;
|
||||
|
||||
/*
|
||||
* Copy the segment of the array to be merged into the temporary memory
|
||||
* locations.
|
||||
*/
|
||||
memcpy(ltmp, left, llength * sizeof(uint8_t));
|
||||
memcpy(rtmp, right, rlength * sizeof(uint8_t));
|
||||
|
||||
while (llength > 0 && rlength > 0) {
|
||||
if (random_compare(ll, rr) <= 0) {
|
||||
/*
|
||||
* Merge the first element from the left back into the main array
|
||||
* if it is smaller or equal to the one on the right.
|
||||
*/
|
||||
*result = *ll;
|
||||
++ll;
|
||||
--llength;
|
||||
} else {
|
||||
/*
|
||||
* Merge the first element from the right back into the main array
|
||||
* if it is smaller than the one on the left.
|
||||
*/
|
||||
*result = *rr;
|
||||
++rr;
|
||||
--rlength;
|
||||
}
|
||||
++result;
|
||||
}
|
||||
/*
|
||||
* All the elements from either the left or the right temporary array
|
||||
* segment have been merged back into the main array. Append the remaining
|
||||
* elements from the other temporary array back into the main array.
|
||||
*/
|
||||
if (llength > 0)
|
||||
while (llength > 0) {
|
||||
/* Appending the rest of the left temporary array. */
|
||||
*result = *ll;
|
||||
++result;
|
||||
++ll;
|
||||
--llength;
|
||||
}
|
||||
else
|
||||
while (rlength > 0) {
|
||||
/* Appending the rest of the right temporary array. */
|
||||
*result = *rr;
|
||||
++result;
|
||||
++rr;
|
||||
--rlength;
|
||||
}
|
||||
|
||||
/* Release the memory used for the temporary arrays. */
|
||||
free(ltmp);
|
||||
free(rtmp);
|
||||
}
|
||||
|
||||
static void merge_sort(uint8_t array[], int length)
|
||||
{
|
||||
/* This is the middle index and also the length of the right array. */
|
||||
uint8_t middle;
|
||||
|
||||
/*
|
||||
* Pointers to the beginning of the left and right segment of the array
|
||||
* to be merged.
|
||||
*/
|
||||
uint8_t *left, *right;
|
||||
|
||||
/* Length of the left segment of the array to be merged. */
|
||||
int llength;
|
||||
|
||||
if (length <= 1)
|
||||
return;
|
||||
|
||||
/* Let integer division truncate the value. */
|
||||
middle = length / 2;
|
||||
|
||||
llength = length - middle;
|
||||
|
||||
/*
|
||||
* Set the pointers to the appropriate segments of the array to be merged.
|
||||
*/
|
||||
left = array;
|
||||
right = array + llength;
|
||||
|
||||
merge_sort(left, llength);
|
||||
merge_sort(right, middle);
|
||||
merge(left, llength, right, middle);
|
||||
}
|
||||
#import "table.h"
|
||||
#import "encrypt.h"
|
||||
|
||||
void encrypt(char *buf, int len) {
|
||||
char *end = buf + len;
|
||||
while (buf < end) {
|
||||
*buf = (char)encrypt_table[(unsigned char)*buf];
|
||||
buf++;
|
||||
}
|
||||
table_encrypt(buf, len);
|
||||
}
|
||||
|
||||
void decrypt(char *buf, int len) {
|
||||
char *end = buf + len;
|
||||
while (buf < end) {
|
||||
*buf = (char)decrypt_table[(unsigned char)*buf];
|
||||
buf++;
|
||||
}
|
||||
table_decrypt(buf, len);
|
||||
}
|
||||
|
||||
int send_encrypt(int sock, char *buf, int len, int flags) {
|
||||
@@ -145,24 +27,6 @@ int recv_decrypt(int sock, char *buf, int len, int flags) {
|
||||
return result;
|
||||
}
|
||||
|
||||
void get_table(const char* key) {
|
||||
unsigned char *table = encrypt_table;
|
||||
unsigned char tmp_hash[16];
|
||||
// tmp_hash = MD5((const unsigned char*)key, strlen(key), NULL);
|
||||
MD5(key, strlen(key), tmp_hash);
|
||||
_a = *(unsigned long long *)tmp_hash;
|
||||
_a = *(uint64_t *)tmp_hash;
|
||||
uint32_t i;
|
||||
void init_encryption(const char* password, const char*method) {
|
||||
|
||||
for(i = 0; i < 256; ++i) {
|
||||
table[i] = i;
|
||||
}
|
||||
for(i = 1; i < 1024; ++i) {
|
||||
_i = i;
|
||||
merge_sort(table, 256);
|
||||
}
|
||||
for(i = 0; i < 256; ++i) {
|
||||
// gen decrypt table from encrypt table
|
||||
decrypt_table[encrypt_table[i]] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
ShadowWeb/table.h
Normal file
11
ShadowWeb/table.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
unsigned char encrypt_table[256];
|
||||
unsigned char decrypt_table[256];
|
||||
__deprecated void get_table(const char* key);
|
||||
void table_encrypt(char *buf, int len);
|
||||
void table_decrypt(char *buf, int len);
|
||||
|
||||
unsigned int _i;
|
||||
unsigned long long _a;
|
||||
153
ShadowWeb/table.m
Normal file
153
ShadowWeb/table.m
Normal file
@@ -0,0 +1,153 @@
|
||||
#import <stdint.h>
|
||||
#import <openssl/md5.h>
|
||||
#import <openssl/evp.h>
|
||||
#include "table.h"
|
||||
|
||||
|
||||
static int random_compare(const void *_x, const void *_y) {
|
||||
uint32_t i = _i;
|
||||
uint64_t a = _a;
|
||||
uint8_t x = *((uint8_t *) _x);
|
||||
uint8_t y = *((uint8_t*) _y);
|
||||
return (a % (x + i) - a % (y + i));
|
||||
}
|
||||
|
||||
|
||||
static void merge(uint8_t *left, int llength, uint8_t *right, int rlength)
|
||||
{
|
||||
/* Temporary memory locations for the 2 segments of the array to merge. */
|
||||
uint8_t *ltmp = (uint8_t *) malloc(llength * sizeof(uint8_t));
|
||||
uint8_t *rtmp = (uint8_t *) malloc(rlength * sizeof(uint8_t));
|
||||
|
||||
/*
|
||||
* Pointers to the elements being sorted in the temporary memory locations.
|
||||
*/
|
||||
uint8_t *ll = ltmp;
|
||||
uint8_t *rr = rtmp;
|
||||
|
||||
uint8_t *result = left;
|
||||
|
||||
/*
|
||||
* Copy the segment of the array to be merged into the temporary memory
|
||||
* locations.
|
||||
*/
|
||||
memcpy(ltmp, left, llength * sizeof(uint8_t));
|
||||
memcpy(rtmp, right, rlength * sizeof(uint8_t));
|
||||
|
||||
while (llength > 0 && rlength > 0) {
|
||||
if (random_compare(ll, rr) <= 0) {
|
||||
/*
|
||||
* Merge the first element from the left back into the main array
|
||||
* if it is smaller or equal to the one on the right.
|
||||
*/
|
||||
*result = *ll;
|
||||
++ll;
|
||||
--llength;
|
||||
} else {
|
||||
/*
|
||||
* Merge the first element from the right back into the main array
|
||||
* if it is smaller than the one on the left.
|
||||
*/
|
||||
*result = *rr;
|
||||
++rr;
|
||||
--rlength;
|
||||
}
|
||||
++result;
|
||||
}
|
||||
/*
|
||||
* All the elements from either the left or the right temporary array
|
||||
* segment have been merged back into the main array. Append the remaining
|
||||
* elements from the other temporary array back into the main array.
|
||||
*/
|
||||
if (llength > 0)
|
||||
while (llength > 0) {
|
||||
/* Appending the rest of the left temporary array. */
|
||||
*result = *ll;
|
||||
++result;
|
||||
++ll;
|
||||
--llength;
|
||||
}
|
||||
else
|
||||
while (rlength > 0) {
|
||||
/* Appending the rest of the right temporary array. */
|
||||
*result = *rr;
|
||||
++result;
|
||||
++rr;
|
||||
--rlength;
|
||||
}
|
||||
|
||||
/* Release the memory used for the temporary arrays. */
|
||||
free(ltmp);
|
||||
free(rtmp);
|
||||
}
|
||||
|
||||
static void merge_sort(uint8_t array[], int length)
|
||||
{
|
||||
/* This is the middle index and also the length of the right array. */
|
||||
uint8_t middle;
|
||||
|
||||
/*
|
||||
* Pointers to the beginning of the left and right segment of the array
|
||||
* to be merged.
|
||||
*/
|
||||
uint8_t *left, *right;
|
||||
|
||||
/* Length of the left segment of the array to be merged. */
|
||||
int llength;
|
||||
|
||||
if (length <= 1)
|
||||
return;
|
||||
|
||||
/* Let integer division truncate the value. */
|
||||
middle = length / 2;
|
||||
|
||||
llength = length - middle;
|
||||
|
||||
/*
|
||||
* Set the pointers to the appropriate segments of the array to be merged.
|
||||
*/
|
||||
left = array;
|
||||
right = array + llength;
|
||||
|
||||
merge_sort(left, llength);
|
||||
merge_sort(right, middle);
|
||||
merge(left, llength, right, middle);
|
||||
}
|
||||
|
||||
void table_encrypt(char *buf, int len) {
|
||||
char *end = buf + len;
|
||||
while (buf < end) {
|
||||
*buf = (char)encrypt_table[(unsigned char)*buf];
|
||||
buf++;
|
||||
}
|
||||
}
|
||||
|
||||
void table_decrypt(char *buf, int len) {
|
||||
char *end = buf + len;
|
||||
while (buf < end) {
|
||||
*buf = (char)decrypt_table[(unsigned char)*buf];
|
||||
buf++;
|
||||
}
|
||||
}
|
||||
|
||||
void get_table(const char* key) {
|
||||
unsigned char *table = encrypt_table;
|
||||
unsigned char tmp_hash[16];
|
||||
// tmp_hash = MD5((const unsigned char*)key, strlen(key), NULL);
|
||||
MD5((const unsigned char*)key, strlen(key), tmp_hash);
|
||||
_a = *(unsigned long long *)tmp_hash;
|
||||
_a = *(uint64_t *)tmp_hash;
|
||||
uint32_t i;
|
||||
|
||||
for(i = 0; i < 256; ++i) {
|
||||
table[i] = i;
|
||||
}
|
||||
for(i = 1; i < 1024; ++i) {
|
||||
_i = i;
|
||||
merge_sort(table, 256);
|
||||
}
|
||||
for(i = 0; i < 256; ++i) {
|
||||
// gen decrypt table from encrypt table
|
||||
decrypt_table[encrypt_table[i]] = i;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user