Commit da991a28 authored by Casualet's avatar Casualet

add comments

parent b14ff9e7
...@@ -27,8 +27,10 @@ public: ...@@ -27,8 +27,10 @@ public:
private: private:
const bool is_salt; const bool is_salt;
//比如对于select 1+1, 这里的field_called就是"1+1" //for select 1+1, the field_called value is "1+1", so is the feild is not salt, this is the plaintext name
//of the field.
const std::string field_called; const std::string field_called;
//if the field is not salt, olk.key should get the fieldmeta
const OLK olk; // if !olk.key, field is not encrypted const OLK olk; // if !olk.key, field is not encrypted
const int salt_pos; // position of salt of this field in const int salt_pos; // position of salt of this field in
// the query results, or -1 if such // the query results, or -1 if such
......
...@@ -120,9 +120,6 @@ public: ...@@ -120,9 +120,6 @@ public:
}; };
class HOMFactory : public LayerFactory { class HOMFactory : public LayerFactory {
public: public:
static std::unique_ptr<EncLayer> static std::unique_ptr<EncLayer>
...@@ -177,7 +174,6 @@ EncLayerFactory::encLayer(onion o, SECLEVEL sl, const Create_field &cf, ...@@ -177,7 +174,6 @@ EncLayerFactory::encLayer(onion o, SECLEVEL sl, const Create_field &cf,
case SECLEVEL::PLAINVAL: { case SECLEVEL::PLAINVAL: {
return std::unique_ptr<EncLayer>(new PlainText()); return std::unique_ptr<EncLayer>(new PlainText());
} }
default:{} default:{}
} }
FAIL_TextMessageError("unknown or unimplemented security level"); FAIL_TextMessageError("unknown or unimplemented security level");
...@@ -186,8 +182,7 @@ EncLayerFactory::encLayer(onion o, SECLEVEL sl, const Create_field &cf, ...@@ -186,8 +182,7 @@ EncLayerFactory::encLayer(onion o, SECLEVEL sl, const Create_field &cf,
//recover from the database using lambda. //recover from the database using lambda.
std::unique_ptr<EncLayer> std::unique_ptr<EncLayer>
EncLayerFactory::deserializeLayer(unsigned int id, EncLayerFactory::deserializeLayer(unsigned int id,
const std::string &serial) const std::string &serial){
{
assert(id); assert(id);
const SerialLayer li = serial_unpack(serial); const SerialLayer li = serial_unpack(serial);
...@@ -266,8 +261,7 @@ static Create_field* ...@@ -266,8 +261,7 @@ static Create_field*
integerCreateFieldHelper(const Create_field &f, integerCreateFieldHelper(const Create_field &f,
enum enum_field_types type, enum enum_field_types type,
const std::string &anonname = "", const std::string &anonname = "",
CHARSET_INFO * const charset = NULL) CHARSET_INFO * const charset = NULL){
{
return lowLevelcreateFieldHelper(f, 0, type, anonname, charset); return lowLevelcreateFieldHelper(f, 0, type, anonname, charset);
} }
...@@ -749,24 +743,20 @@ public: ...@@ -749,24 +743,20 @@ public:
std::string doSerialize() const {return rawkey;} std::string doSerialize() const {return rawkey;}
DET_str(unsigned int id, const std::string &serial); DET_str(unsigned int id, const std::string &serial);
virtual SECLEVEL level() const {return SECLEVEL::DET;} virtual SECLEVEL level() const {return SECLEVEL::DET;}
std::string name() const {return "DET_str";} std::string name() const {return "DET_str";}
Create_field * newCreateField(const Create_field &cf, Create_field * newCreateField(const Create_field &cf,
const std::string &anonname = "") const std::string &anonname = "")
const; const;
Item *encrypt(const Item &ptext, uint64_t IV) const; Item *encrypt(const Item &ptext, uint64_t IV) const;
Item *decrypt(const Item &ctext, uint64_t IV) const; Item *decrypt(const Item &ctext, uint64_t IV) const;
Item * decryptUDF(Item * const col, Item * const ivcol = NULL) const; Item * decryptUDF(Item * const col, Item * const ivcol = NULL) const;
protected: protected:
const std::string rawkey; const std::string rawkey;
static const int key_bytes = 16; static const int key_bytes = 16;
static const bool do_pad = true; static const bool do_pad = true;
const std::unique_ptr<const AES_KEY> enckey; const std::unique_ptr<const AES_KEY> enckey;
const std::unique_ptr<const AES_KEY> deckey; const std::unique_ptr<const AES_KEY> deckey;
}; };
......
...@@ -492,7 +492,6 @@ main() { ...@@ -492,7 +492,6 @@ main() {
std::getline(std::cin,curQuery); std::getline(std::cin,curQuery);
std::unique_ptr<SchemaInfo> schema = myLoadSchemaInfo(); std::unique_ptr<SchemaInfo> schema = myLoadSchemaInfo();
processSchemaInfo(*schema); processSchemaInfo(*schema);
continue; continue;
} }
std::cout<<GREEN_BEGIN<<"curQuery: "<< std::cout<<GREEN_BEGIN<<"curQuery: "<<
......
...@@ -107,7 +107,7 @@ class InsertHandler : public DMLHandler { ...@@ -107,7 +107,7 @@ class InsertHandler : public DMLHandler {
std::vector<FieldMeta *> fmVec; std::vector<FieldMeta *> fmVec;
std::vector<Item *> implicit_defaults; std::vector<Item *> implicit_defaults;
//对于insert, 有可能出现指定field list的情况. //For insert, we can choose to specify field list or omit it.
if (lex->field_list.head()) { if (lex->field_list.head()) {
auto it = List_iterator<Item>(lex->field_list); auto it = List_iterator<Item>(lex->field_list);
List<Item> newList; List<Item> newList;
......
...@@ -198,6 +198,7 @@ ResType MygetResTypeFromLuaTable(bool isNULL,rawReturnValue *inRow = NULL,int in ...@@ -198,6 +198,7 @@ ResType MygetResTypeFromLuaTable(bool isNULL,rawReturnValue *inRow = NULL,int in
std::vector<std::string> names; std::vector<std::string> names;
std::vector<enum_field_types> types; std::vector<enum_field_types> types;
std::vector<std::vector<Item *> > rows; std::vector<std::vector<Item *> > rows;
//return NULL restype //return NULL restype
if(isNULL){ if(isNULL){
return ResType(true,0,0,std::move(names), return ResType(true,0,0,std::move(names),
...@@ -209,7 +210,6 @@ ResType MygetResTypeFromLuaTable(bool isNULL,rawReturnValue *inRow = NULL,int in ...@@ -209,7 +210,6 @@ ResType MygetResTypeFromLuaTable(bool isNULL,rawReturnValue *inRow = NULL,int in
for(auto inTypes:inRow->fieldTypes){ for(auto inTypes:inRow->fieldTypes){
types.push_back(static_cast<enum_field_types>(inTypes)); types.push_back(static_cast<enum_field_types>(inTypes));
} }
for(auto inRows:inRow->rowValues) { for(auto inRows:inRow->rowValues) {
std::vector<Item *> curTempRow = itemNullVector(types.size()); std::vector<Item *> curTempRow = itemNullVector(types.size());
for(int i=0;i< (int)(inRows.size());i++){ for(int i=0;i< (int)(inRows.size());i++){
...@@ -332,14 +332,17 @@ static std::unique_ptr<SchemaInfo> myLoadSchemaInfo() { ...@@ -332,14 +332,17 @@ static std::unique_ptr<SchemaInfo> myLoadSchemaInfo() {
static void static void
addToReturn(ReturnMeta *const rm, int pos, const OLK &constr, addToReturn(ReturnMeta *const rm, int pos, const OLK &constr,
bool has_salt, const std::string &name) { bool has_salt, const std::string &name) {
const bool test = static_cast<unsigned int>(pos) == rm->rfmeta.size(); const bool test = static_cast<unsigned int>(pos) == rm->rfmeta.size();
TEST_TextMessageError(test, "ReturnMeta has badly ordered" TEST_TextMessageError(test, "ReturnMeta has badly ordered"
" ReturnFields!"); " ReturnFields!");
const int salt_pos = has_salt ? pos + 1 : -1; const int salt_pos = has_salt ? pos + 1 : -1;
std::pair<int, ReturnField> std::pair<int, ReturnField>
pair(pos, ReturnField(false, name, constr, salt_pos)); pair(pos, ReturnField(false, name, constr, salt_pos));
rm->rfmeta.insert(pair); rm->rfmeta.insert(pair);
} }
...@@ -379,14 +382,20 @@ decrypt_item_layers(const Item &i, const FieldMeta *const fm, onion o, ...@@ -379,14 +382,20 @@ decrypt_item_layers(const Item &i, const FieldMeta *const fm, onion o,
return out_i; return out_i;
} }
/*
structure of return field.
map<int,returnField>, int is the index of names
returnField, represent a field, if the field is not salt, then fieldCalled is the plaintex name
*/
static static
ResType decryptResults(const ResType &dbres, const ReturnMeta &rmeta) { ResType decryptResults(const ResType &dbres, const ReturnMeta &rmeta) {
//num of rows
const unsigned int rows = dbres.rows.size(); const unsigned int rows = dbres.rows.size();
//num of names, to be decrypted
const unsigned int cols = dbres.names.size(); const unsigned int cols = dbres.names.size();
std::vector<std::string> dec_names; std::vector<std::string> dec_names;
for (auto it = dbres.names.begin(); for (auto it = dbres.names.begin();it != dbres.names.end(); it++){
it != dbres.names.end(); it++) {
const unsigned int index = it - dbres.names.begin(); const unsigned int index = it - dbres.names.begin();
//fetch rfmeta based on index //fetch rfmeta based on index
const ReturnField &rf = rmeta.rfmeta.at(index); const ReturnField &rf = rmeta.rfmeta.at(index);
...@@ -397,12 +406,16 @@ ResType decryptResults(const ResType &dbres, const ReturnMeta &rmeta) { ...@@ -397,12 +406,16 @@ ResType decryptResults(const ResType &dbres, const ReturnMeta &rmeta) {
} }
} }
const unsigned int real_cols = dec_names.size(); const unsigned int real_cols = dec_names.size();
std::vector<std::vector<Item *> > dec_rows(rows); std::vector<std::vector<Item *> > dec_rows(rows);
//real cols depends on plain text names.
for (unsigned int i = 0; i < rows; i++) { for (unsigned int i = 0; i < rows; i++) {
dec_rows[i] = std::vector<Item *>(real_cols); dec_rows[i] = std::vector<Item *>(real_cols);
} }
// //
unsigned int col_index = 0; unsigned int col_index = 0;
for (unsigned int c = 0; c < cols; c++) { for (unsigned int c = 0; c < cols; c++) {
...@@ -410,14 +423,16 @@ ResType decryptResults(const ResType &dbres, const ReturnMeta &rmeta) { ...@@ -410,14 +423,16 @@ ResType decryptResults(const ResType &dbres, const ReturnMeta &rmeta) {
if (rf.getIsSalt()) { if (rf.getIsSalt()) {
continue; continue;
} }
//the key is in fieldMeta //the key is in fieldMeta
FieldMeta *const fm = rf.getOLK().key; FieldMeta *const fm = rf.getOLK().key;
for (unsigned int r = 0; r < rows; r++) { for (unsigned int r = 0; r < rows; r++) {
//
if (!fm || dbres.rows[r][c]->is_null()) { if (!fm || dbres.rows[r][c]->is_null()) {
dec_rows[r][col_index] = dbres.rows[r][c]; dec_rows[r][col_index] = dbres.rows[r][c];
} else { } else {
uint64_t salt = 0; uint64_t salt = 0;
const int salt_pos = rf.getSaltPosition(); const int salt_pos = rf.getSaltPosition();
//read salt from remote datab for descrypting. //read salt from remote datab for descrypting.
...@@ -427,10 +442,11 @@ ResType decryptResults(const ResType &dbres, const ReturnMeta &rmeta) { ...@@ -427,10 +442,11 @@ ResType decryptResults(const ResType &dbres, const ReturnMeta &rmeta) {
assert_s(!salt_item->null_value, "salt item is null"); assert_s(!salt_item->null_value, "salt item is null");
salt = salt_item->value; salt = salt_item->value;
} }
//peel onion.
//specify fieldMeta, onion, and salt should be able to decrpyt
//peel onion
dec_rows[r][col_index] = dec_rows[r][col_index] =
decrypt_item_layers(*dbres.rows[r][c], decrypt_item_layers(*dbres.rows[r][c],fm,rf.getOLK().o,salt);
fm, rf.getOLK().o, salt);
} }
} }
col_index++; col_index++;
...@@ -442,22 +458,9 @@ ResType decryptResults(const ResType &dbres, const ReturnMeta &rmeta) { ...@@ -442,22 +458,9 @@ ResType decryptResults(const ResType &dbres, const ReturnMeta &rmeta) {
std::move(dec_rows)); std::move(dec_rows));
} }
/*static void split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
}
static std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}*/
//get returnMeta //get returnMeta
//for each filed, we have a fieldmeta. we can chosse one onion under that field to construct a return meta.
//in fact, a returnmeta can contain many fields.
static static
std::shared_ptr<ReturnMeta> getReturnMeta(std::vector<FieldMeta*> fms, std::vector<transField> &tfds){ std::shared_ptr<ReturnMeta> getReturnMeta(std::vector<FieldMeta*> fms, std::vector<transField> &tfds){
assert(fms.size()==tfds.size()); assert(fms.size()==tfds.size());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment