Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
Practical-Cryptdb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Zhaozhen
Practical-Cryptdb
Commits
d70378b3
Commit
d70378b3
authored
Jan 08, 2018
by
yiwenshao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ASHE pass simplest test, however...
parent
af0babde
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
221 additions
and
32 deletions
+221
-32
ASHE.cc
crypto/ASHE.cc
+19
-17
ASHE.hh
crypto/ASHE.hh
+14
-6
test_ASHE.cc
debug/test_ASHE.cc
+13
-8
Makefrag
main/Makefrag
+1
-1
insert_conf_onion.cc
main/insert_conf_onion.cc
+174
-0
No files found.
crypto/ASHE.cc
View file @
d70378b3
#include"crypto/ASHE.hh"
const
unsigned
long
ASHE
::
ASHE_MAX
=
0xffffffffffffffff
;
const
std
::
string
ASHE
::
key
(
"11223344"
);
blowfish
ASHE
::
bf
(
ASHE
::
key
);
ASHE
::
ASHE
(
std
::
string
s
,
int
i
)
:
key
(
s
),
bf
(
s
),
IV
(
i
){
ASHE
::
ASHE
(
int
i
)
:
IV
(
i
){
}
std
::
pair
<
long
,
uint64_t
>
ASHE
::
encrypt
(
unsigned
long
plaintext
){
return
std
::
make_pair
((
plaintext
-
bf
.
encrypt
(
IV
)
+
bf
.
encrypt
(
IV
-
1
))
%
ASHE_MAX
,
IV
)
;
ciphertext
=
(
plaintext
-
Fi
(
IV
)
+
Fi_1
(
IV
))
%
ASHE_MAX
;
return
std
::
make_pair
(
ciphertext
,
IV
);
}
unsigned
long
ASHE
::
decrypt
(
long
ciphertext
){
return
(
ciphertext
+
bf
.
encrypt
(
IV
)
-
bf
.
encrypt
(
IV
-
1
))
%
ASHE_MAX
;
return
(
ciphertext
+
Fi
(
IV
)
-
Fi_1
(
IV
))
%
ASHE_MAX
;
}
/*Cannot declare member function ...to have static linkage*/
std
::
pair
<
long
,
std
::
vector
<
uint64_t
>>
ASHE
::
sum
(
std
::
vector
<
std
::
pair
<
long
,
uint64_t
>>
input
){
std
::
vector
<
uint64_t
>
IVs
;
long
res
=
0
;
std
::
pair
<
long
,
std
::
vector
<
uint64_t
>>
ASHE
::
sum
(
std
::
vector
<
ASHE
>
input
){
long
res
=
0
;
std
::
vector
<
uint64_t
>
ivs
;
for
(
auto
&
item
:
input
){
res
+=
item
.
first
;
IVs
.
push_back
(
item
.
second
);
res
+=
item
.
get_ciphertext
();
res
%=
ASHE_MAX
;
ivs
.
push_back
(
item
.
get_IV
());
}
return
std
::
make_pair
(
res
,
IV
s
);
return
std
::
make_pair
(
res
,
iv
s
);
}
u
nsigned
long
ASHE
::
decrypt_sum
(
std
::
pair
<
long
,
std
::
vector
<
uint64_t
>>
input
){
long
res
=
input
.
first
;
u
int64_t
ASHE
::
decrypt_sum
(
std
::
pair
<
long
,
std
::
vector
<
uint64_t
>>
input
){
uint64_t
res
=
input
.
first
;
for
(
auto
item
:
input
.
second
){
item
++
;
res
++
;
res
+=
(
Fi
(
item
)
-
Fi_1
(
item
))
;
res
%=
ASHE_MAX
;
}
return
res
;
}
crypto/ASHE.hh
View file @
d70378b3
...
...
@@ -2,16 +2,24 @@
#include <string>
#include "crypto/blowfish.hh"
class
ASHE
{
static
const
unsigned
long
ASHE_MAX
;
std
::
string
key
;
blowfish
bf
;
st
atic
const
st
d
::
string
key
;
static
blowfish
bf
;
uint64_t
IV
;
long
ciphertext
;
public
:
ASHE
(
std
::
string
s
,
int
i
);
static
uint64_t
Fi
(
uint64_t
IV
){
return
bf
.
encrypt
(
IV
);}
static
uint64_t
Fi_1
(
uint64_t
IV
){
return
bf
.
encrypt
(
IV
-
1
);}
long
get_ciphertext
(){
return
ciphertext
;}
ASHE
(
int
iv
);
std
::
pair
<
long
,
uint64_t
>
encrypt
(
unsigned
long
plaintext
);
int
getIV
()
;
uint64_t
get_IV
(){
return
IV
;}
;
unsigned
long
decrypt
(
long
ciphertext
);
static
std
::
pair
<
long
,
std
::
vector
<
uint64_t
>>
sum
(
std
::
vector
<
std
::
pair
<
long
,
uint64_t
>>
input
);
static
unsigned
long
decrypt_sum
(
std
::
pair
<
long
,
std
::
vector
<
uint64_t
>>
input
);
static
std
::
pair
<
long
,
std
::
vector
<
uint64_t
>>
sum
(
std
::
vector
<
ASHE
>
);
static
uint64_t
decrypt_sum
(
std
::
pair
<
long
,
std
::
vector
<
uint64_t
>>
);
};
debug/test_ASHE.cc
View file @
d70378b3
...
...
@@ -4,19 +4,24 @@
#include "util/util.cc"
int
main
(){
std
::
vector
<
unsigned
long
long
>
plain
{
1u
,
2u
,
3u
,
4u
,
5u
,
6u
,
7u
,
8u
,
9u
,
10u
};
std
::
vector
<
std
::
pair
<
long
,
uint64_t
>>
enc
;
std
::
vector
<
ASHE
*
>
ass
;
std
::
vector
<
unsigned
long
long
>
plain
{
1u
,
2u
,
3u
//,4u,5u,6u,7u,8u,9u,10u
}
;
std
::
vector
<
ASHE
>
ass
;
for
(
auto
item
:
plain
){
uint64_t
IV
=
randomValue
();
if
(
IV
==
0
)
IV
=
1
;
ass
.
push_back
(
new
ASHE
(
"111"
,
IV
));
enc
.
push_back
(
ass
.
back
()
->
encrypt
(
item
)
);
ass
.
push_back
(
ASHE
(
IV
));
ass
.
back
().
encrypt
(
item
);
}
std
::
cout
<<
"encs:plains"
<<
std
::
endl
;
for
(
auto
i
=
0u
;
i
<
enc
.
size
();
++
i
){
std
::
cout
<<
"enc:"
<<
enc
[
i
].
first
<<
"dec:"
<<
ass
[
i
]
->
decrypt
(
enc
[
i
].
first
)
<<
std
::
endl
;
for
(
auto
&
item
:
ass
){
std
::
cout
<<
item
.
get_ciphertext
()
<<
"::"
<<
item
.
decrypt
(
item
.
get_ciphertext
()
)
<<
std
::
endl
;
}
std
::
pair
<
long
,
std
::
vector
<
uint64_t
>>
enc_sum
=
ASHE
::
sum
(
ass
);
uint64_t
res
=
ASHE
::
decrypt_sum
(
enc_sum
);
std
::
cout
<<
enc_sum
.
first
<<
"::"
<<
res
<<
std
::
endl
;
return
0
;
}
main/Makefrag
View file @
d70378b3
...
...
@@ -7,7 +7,7 @@ CRYPTDB_SRCS := schema.cc Translator.cc Connect.cc \
rewrite_func.cc rewrite_sum.cc metadata_tables.cc \
error.cc stored_procedures.cc rewrite_ds.cc rewrite_main.cc big_proxy.cc
CRYPTDB_PROGS:= cdb_test load_and_store test_layer test_schema test_proxy change_test
CRYPTDB_PROGS:= cdb_test load_and_store test_layer test_schema test_proxy change_test
insert_conf_onion
CRYPTDBPROGOBJS := $(patsubst %,$(OBJDIR)/main/%,$(CRYPTDB_PROGS))
...
...
main/insert_conf_onion.cc
0 → 100644
View file @
d70378b3
#include <iostream>
#include <vector>
#include <functional>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <main/Connect.hh>
#include <main/rewrite_util.hh>
#include <main/sql_handler.hh>
#include <main/dml_handler.hh>
#include <main/ddl_handler.hh>
#include <main/CryptoHandlers.hh>
#include <main/rewrite_main.hh>
static
std
::
string
embeddedDir
=
"/t/cryt/shadow"
;
static
Item
*
my_encrypt_item_layers
(
const
Item
&
i
,
onion
o
,
const
OnionMeta
&
om
,
const
Analysis
&
a
,
uint64_t
IV
)
{
assert
(
!
RiboldMYSQL
::
is_null
(
i
));
const
auto
&
enc_layers
=
a
.
getEncLayers
(
om
);
assert_s
(
enc_layers
.
size
()
>
0
,
"onion must have at least one layer"
);
const
Item
*
enc
=
&
i
;
Item
*
new_enc
=
NULL
;
for
(
const
auto
&
it
:
enc_layers
)
{
new_enc
=
it
->
encrypt
(
*
enc
,
IV
);
assert
(
new_enc
);
enc
=
new_enc
;
}
assert
(
new_enc
&&
new_enc
!=
&
i
);
return
new_enc
;
}
static
void
my_encrypt_item_all_onions
(
const
Item
&
i
,
const
FieldMeta
&
fm
,
uint64_t
IV
,
Analysis
&
a
,
std
::
vector
<
Item
*>
*
l
){
for
(
auto
it
:
fm
.
orderedOnionMetas
())
{
const
onion
o
=
it
.
first
->
getValue
();
OnionMeta
*
const
om
=
it
.
second
;
if
(
om
!=
NULL
)
l
->
push_back
(
my_encrypt_item_layers
(
i
,
o
,
*
om
,
a
,
IV
));
else
l
->
push_back
(
NULL
);
}
}
static
void
my_typical_rewrite_insert_type
(
const
Item
&
i
,
const
FieldMeta
&
fm
,
Analysis
&
a
,
std
::
vector
<
Item
*>
*
l
)
{
const
uint64_t
salt
=
fm
.
getHasSalt
()
?
randomValue
()
:
0
;
my_encrypt_item_all_onions
(
i
,
fm
,
salt
,
a
,
l
);
if
(
fm
.
getHasSalt
())
{
l
->
push_back
(
new
Item_int
(
static_cast
<
ulonglong
>
(
salt
)));
}
}
template
<
typename
ContainerType
>
void
myRewriteInsertHelper
(
const
Item
&
i
,
const
FieldMeta
&
fm
,
Analysis
&
a
,
ContainerType
*
const
append_list
){
std
::
vector
<
Item
*>
l
;
my_typical_rewrite_insert_type
(
i
,
fm
,
a
,
&
l
);
for
(
auto
it
:
l
)
{
append_list
->
push_back
(
it
);
}
}
static
std
::
string
getInsertResults
(
Analysis
a
,
LEX
*
lex
){
LEX
*
const
new_lex
=
copyWithTHD
(
lex
);
const
std
::
string
&
table
=
lex
->
select_lex
.
table_list
.
first
->
table_name
;
const
std
::
string
&
db_name
=
lex
->
select_lex
.
table_list
.
first
->
db
;
//from databasemeta to tablemeta.
const
TableMeta
&
tm
=
a
.
getTableMeta
(
db_name
,
table
);
//rewrite table name
new_lex
->
select_lex
.
table_list
.
first
=
rewrite_table_list
(
lex
->
select_lex
.
table_list
.
first
,
a
);
std
::
vector
<
FieldMeta
*>
fmVec
;
std
::
vector
<
Item
*>
implicit_defaults
;
// No field list, use the table order.
assert
(
fmVec
.
empty
());
std
::
vector
<
FieldMeta
*>
fmetas
=
tm
.
orderedFieldMetas
();
fmVec
.
assign
(
fmetas
.
begin
(),
fmetas
.
end
());
if
(
lex
->
many_values
.
head
())
{
//start processing many values
auto
it
=
List_iterator
<
List_item
>
(
lex
->
many_values
);
List
<
List_item
>
newList
;
for
(;;)
{
List_item
*
const
li
=
it
++
;
if
(
!
li
)
{
break
;
}
List
<
Item
>
*
const
newList0
=
new
List
<
Item
>
();
if
(
li
->
elements
!=
fmVec
.
size
())
{
TEST_TextMessageError
(
0
==
li
->
elements
&&
NULL
==
lex
->
field_list
.
head
(),
"size mismatch between fields"
" and values!"
);
}
else
{
auto
it0
=
List_iterator
<
Item
>
(
*
li
);
auto
fmVecIt
=
fmVec
.
begin
();
for
(;;)
{
const
Item
*
const
i
=
it0
++
;
assert
(
!!
i
==
(
fmVec
.
end
()
!=
fmVecIt
));
if
(
!
i
)
{
break
;
}
//fetch values, and use fieldMeta to facilitate rewrite
//every filed should be encrypted with onions of encryption
myRewriteInsertHelper
(
*
i
,
**
fmVecIt
,
a
,
newList0
);
++
fmVecIt
;
}
for
(
auto
def_it
:
implicit_defaults
)
{
newList0
->
push_back
(
def_it
);
}
}
newList
.
push_back
(
newList0
);
}
new_lex
->
many_values
=
newList
;
}
return
lexToQuery
(
*
new_lex
);
}
static
void
testInsertHandler
(
std
::
string
query
){
std
::
unique_ptr
<
Connect
>
e_conn
(
Connect
::
getEmbedded
(
embeddedDir
));
std
::
unique_ptr
<
SchemaInfo
>
schema
(
new
SchemaInfo
());
std
::
function
<
DBMeta
*
(
DBMeta
*
const
)
>
loadChildren
=
[
&
loadChildren
,
&
e_conn
](
DBMeta
*
const
parent
)
{
auto
kids
=
parent
->
fetchChildren
(
e_conn
);
for
(
auto
it
:
kids
)
{
loadChildren
(
it
);
}
return
parent
;
};
//load all metadata and then store it in schema
loadChildren
(
schema
.
get
());
const
std
::
unique_ptr
<
AES_KEY
>
&
TK
=
std
::
unique_ptr
<
AES_KEY
>
(
getKey
(
std
::
string
(
"113341234"
)));
//just like what we do in Rewrite::rewrite,dispatchOnLex
Analysis
analysis
(
std
::
string
(
"tdb"
),
*
schema
,
TK
,
SECURITY_RATING
::
SENSITIVE
);
std
::
unique_ptr
<
query_parse
>
p
;
p
=
std
::
unique_ptr
<
query_parse
>
(
new
query_parse
(
"tdb"
,
query
));
LEX
*
const
lex
=
p
->
lex
();
std
::
cout
<<
getInsertResults
(
analysis
,
lex
)
<<
std
::
endl
;
}
int
main
()
{
char
*
buffer
;
if
((
buffer
=
getcwd
(
NULL
,
0
))
==
NULL
){
perror
(
"getcwd error"
);
}
embeddedDir
=
std
::
string
(
buffer
)
+
"/shadow"
;
const
std
::
string
master_key
=
"113341234"
;
ConnectionInfo
ci
(
"localhost"
,
"root"
,
"letmein"
,
3306
);
SharedProxyState
*
shared_ps
=
new
SharedProxyState
(
ci
,
embeddedDir
,
master_key
,
determineSecurityRating
());
assert
(
shared_ps
!=
NULL
);
std
::
string
query1
=
"insert into student values(1,
\"
ZHAOYUN
\"
)"
;
std
::
vector
<
std
::
string
>
querys
{
query1
};
for
(
auto
item
:
querys
){
std
::
cout
<<
item
<<
std
::
endl
;
testInsertHandler
(
item
);
std
::
cout
<<
std
::
endl
;
}
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment