示例#1
0
文件: Dec.c 项目: FE-Lib/fe-project
/*
 * AS, c_iの順にシリアライズされている
 * まずint*3(num_policy, M->row, M->column)
 * その後、行列M,rho(i)
 * 最後にc_i
 */
void tfel_deserialize_ciphertext_cp(AccessStructure *AS, basis *c_i, unsigned char *bin_ct_buf, size_t max_len, EC_PAIRING p) {
    int i, j, t;
    unsigned char *buf_ptr = NULL;
    size_t buf_len;
    size_t *result_len = NULL;
    result_len = &buf_len;
    *result_len = 0;
    buf_ptr = bin_ct_buf;
    size_t length;
    
    //引数の初期化
    init_AccessStructure(AS);
    
    //num_policy, 行列のrow,columnのdeserialize
    AS->num_policy = *((int*)buf_ptr);
    *result_len += sizeof(int);
    buf_ptr = (unsigned char*)(bin_ct_buf + *result_len);
    AS->S->row = *((int*)buf_ptr);
    *result_len += sizeof(int);
    buf_ptr = (unsigned char*)(bin_ct_buf + *result_len);
    AS->S->column = *((int*)buf_ptr);
    *result_len += sizeof(int);

    //Mのdeserialize
    char *mpz_temp;
    // 不具合あったら下の0428のコメント解除して個々の部分消す↓
    mpz_temp = (char*)malloc(sizeof(char)*MAX_STRING);
    
    AS->S->M = (mpz_t**)malloc(sizeof(mpz_t*)*AS->S->row);
    for (i = 0; i < AS->S->row; i++) {
        AS->S->M[i] = (mpz_t*)malloc(sizeof(mpz_t)*AS->S->column);
        for (j = 0; j < AS->S->column; j++) {
            buf_ptr = (unsigned char*)(bin_ct_buf + *result_len);
            memset(mpz_temp, '\0', sizeof(char)*MAX_STRING);//4はEnc.hのシリアライズの方に合わせてる
            strncpy(mpz_temp, (char*)buf_ptr, MAX_STRING); //1は変えたほうがいいかも
            mpz_init_set_str(AS->S->M[i][j], mpz_temp, 16);
            *result_len += sizeof(char)*MAX_STRING;
        }
    }
    free(mpz_temp);

    rho_i *rho_ptr;
    rho_ptr = (rho_i*)malloc(sizeof(rho_i));
    init_rho_i(rho_ptr);
    AS->rho = rho_ptr;
    for (i = 0; i < AS->num_policy; i++) { //ここの記述だるすぎでしょ……
        buf_ptr = (unsigned char*)(bin_ct_buf + *result_len);
        rho_ptr->t = *((int*)buf_ptr);
        buf_ptr += sizeof(int);
        rho_ptr->v_t[0] = *((int*)buf_ptr);
        buf_ptr += sizeof(int);
        rho_ptr->v_t[1] = *((int*)buf_ptr);
        buf_ptr += sizeof(int);
        rho_ptr->is_negated = *(Bool*)buf_ptr;
        buf_ptr += sizeof(int);
        *result_len += sizeof(int)*3 + sizeof(Bool);
        if (i < AS->num_policy -1) {
            rho_ptr->next = (rho_i*)malloc(sizeof(rho_i));
            init_rho_i(rho_ptr->next);
            rho_ptr = rho_ptr->next;
        }
    }
    rho_ptr->next = NULL;
    rho_ptr = AS->rho;
    
    //c(i)のdeserialize
    c_i->dim = AS->num_policy+1;
    c_i->M = (EC_POINT**)malloc(sizeof(EC_POINT*)*c_i->dim);
    for(i = 0; i < c_i->dim; i++) {
        //printf("i = %d\n", i);
        if(*(int*)result_len > max_len) {
            printf("import c(i) error\n");
            exit(1);
        }
        if(i == 0) t = 5;
        else t = 7;
        c_i->M[i] = (EC_POINT*)malloc(sizeof(EC_POINT)*t);
        for(j = 0; j < t; j++) {
            point_init(c_i->M[i][j], p->g1);
            //printf("j = %d\n", j);
            buf_ptr = (unsigned char*)(bin_ct_buf + *result_len);
            length = *(int*)buf_ptr;
            //printf("length = %zd\n", length);
            *result_len += sizeof(int);
            buf_ptr = (unsigned char*)(bin_ct_buf + *result_len);
            point_from_oct(c_i->M[i][j], buf_ptr, length);
            *result_len += length;
        }
    }
    
    return;
}
示例#2
0
文件: Dec.c 项目: FE-Lib/fe-project
int tfel_deserialize_ciphertext_kp(attribute_set *Delta, basis *c_i, unsigned char *bin_ct_buf, size_t max_len, EC_PAIRING p) {
    int i, j, t;
    unsigned char *buf_ptr = NULL;
    size_t buf_len;
    size_t *result_len = NULL;
    result_len = &buf_len;
    *result_len = 0;
    buf_ptr = bin_ct_buf;
    size_t length;
    
    /*
     KPでやること
     Deltaの初期化→Delta->valueを作るためにDelta.numが必要なので最初にdを引き出す
     その後、Delta->valueをmallocしvalueを引き出す
     */
    //dをつくる
    v_vector *v_ptr = NULL;
    v_ptr = (v_vector*)malloc(sizeof(v_vector));
    if (v_ptr == NULL) return -1;
    Delta->value = v_ptr;
    Delta->num = *((int*)buf_ptr);
    *result_len += sizeof(int);
    //x_tを引き出す
    buf_ptr = (unsigned char*)(bin_ct_buf + *result_len);
    for (i = 0; i < Delta->num; i++) {
        init_vector(v_ptr, i);
        v_ptr->x_t[1] = *((int*)buf_ptr);
        *result_len += sizeof(int);
        buf_ptr = (unsigned char*)(bin_ct_buf + *result_len);
        v_ptr->next = (v_vector*)malloc(sizeof(v_vector));
        if (v_ptr->next == NULL) {
            clear_att_set_value(Delta->value);
            return -1;
        }
        if (i == Delta->num-1) {
            free(v_ptr->next);
            v_ptr->next = NULL;
        }
        else v_ptr = v_ptr->next;
    }
    
    //c(i)のdeserialize
    c_i->dim = Delta->num+1;
    c_i->M = (EC_POINT**)malloc(sizeof(EC_POINT*)*c_i->dim);
    if (c_i->M == NULL) {
        //error処理
    }
    for(i = 0; i < c_i->dim; i++) {
        //printf("i = %d\n", i);
        if(*(int*)result_len > max_len) {
            printf("import c(i) error\n");
            exit(1);
        }
        if(i == 0) t = 5;
        else t = 7;
        c_i->M[i] = (EC_POINT*)malloc(sizeof(EC_POINT)*t);
        for(j = 0; j < t; j++) {
            point_init(c_i->M[i][j], p->g1);
            //printf("j = %d\n", j);
            buf_ptr = (unsigned char*)(bin_ct_buf + *result_len);
            length = *(int*)buf_ptr;
            //printf("length = %zd\n", length);
            *result_len += sizeof(int);
            buf_ptr = (unsigned char*)(bin_ct_buf + *result_len);
            point_from_oct(c_i->M[i][j], buf_ptr, length);
            *result_len += length;
        }
    }
    
    return 0;
}
示例#3
0
//============================================
//  楕円曲線の入出力テスト
//============================================
void test_io(const EC_GROUP ec)
{
    int i;
    unsigned long long int t1, t2;
    EC_POINT P, Q, R;

    size_t osize;
    unsigned char os[130];

    char str[262];

    point_init(P, ec);
    point_init(Q, ec);
    point_init(R, ec);

    //---------------------
    //  octet string
    //---------------------
    point_set_infinity(R);

    point_to_oct(os, &osize, R);
    point_from_oct(Q, os, osize);

    assert(point_is_infinity(Q));

    for (i = 0; i < 100; i++)
    {
        point_random(P);

        point_to_oct(os, &osize, P);
        point_from_oct(Q, os, osize);

        assert(point_cmp(P, Q) == 0);
    }

    t1 = rdtsc();
    for (i = 0; i < N; i++) { point_to_oct(os, &osize, P); }
    t2 = rdtsc();

    printf("point to octet string: %.2lf [clock]\n", (double)(t2 - t1) / N);

    t1 = rdtsc();
    for (i = 0; i < N; i++) { point_from_oct(Q, os, osize); }
    t2 = rdtsc();

    printf("point from octet string: %.2lf [clock]\n", (double)(t2 - t1) / N);

    //---------------------
    //  string
    //---------------------
    point_set_infinity(R);

    point_get_str(str, R);
    point_set_str(Q, str);

    assert(point_is_infinity(Q));

    for (i = 0; i < 100; i++)
    {
        point_get_str(str, P);
        point_set_str(Q, str);

        assert(point_cmp(P, Q) == 0);
    }

    t1 = rdtsc();
    for (i = 0; i < N; i++) { point_get_str(str, P); }
    t2 = rdtsc();

    printf("point get string: %.2lf [clock]\n", (double)(t2 - t1) / N);

    t1 = rdtsc();
    for (i = 0; i < N; i++) { point_set_str(Q, str); }
    t2 = rdtsc();

    printf("point set string: %.2lf [clock]\n", (double)(t2 - t1) / N);

    point_clear(P);
    point_clear(Q);
    point_clear(R);
}