Ejemplo n.º 1
0
int main(void)
{
   EC_PAIRING p;
   EC_POINT a, b, c;
   Element d;

   pairing_init(p, "ECBN254");

   point_init(a, p->g1);
   point_init(b, p->g2);
   point_init(c, p->g1);

   element_init(d, p->g3);

   point_set_str(a,
      "["
         "0000000000000000000000000000000000000000000000000000000000000001,"
         "0D45589B158FAAF6AB0E4AD38D998E9982E7FF63964EE1460342A592677CCCB0"
      "]"
   );

   point_set_str(b,
      "["
         "19850140BC38957238BDEB56EC7B97FE30A6A65D15C4BA07CEF54DB5026C7210 "
         "1DEB7F4B6C1AEFAEBD0EB750B841BD8ABF916EB750FDF7291F99DFD290C28CE0,"
         "14C164D6D18CBC7F64559076E00789C75FF001D1BE0968D210C19FB0D3AD649A "
         "059A2ABA101B7A3C1FA3CAF4DF6B38F2CB4976287488E33F526FA7E8C5441B4B"
      "]"
   );

   pairing_map(d, a, b, p);

   point_print("a", a);
   point_print("b", b);

   element_print("d", d);

   char msg[] = "abc";

   point_map_to_point(c, msg, sizeof(msg), 80);

   point_print("c", c);

   point_clear(a);
   point_clear(b);
   point_clear(c);

   element_clear(d);
   pairing_clear(p);

   return 0;
}
Ejemplo n.º 2
0
int tfel_decrypt_cp(char *inputfile, char *keyfile, char *output) {
    int i;
    FILE *fp;
    //pubkey input //共通
    tfel_pubkey *pubkey = NULL;
    pubkey = (tfel_pubkey*)malloc(sizeof(tfel_pubkey));
    if (pubkey == NULL) return -1;
    tfel_param_G *param_G;
    param_G = (tfel_param_G*)malloc(sizeof(tfel_param_G));
    if (param_G == NULL) {
        tfel_clear_pk(pubkey->num_att, pubkey);
        free(pubkey);
        return -1;
    }
    memset(param_G, 0, sizeof(tfel_param_G));
    pairing_init(param_G->p, CURVE);
    importpub(pubkey, *param_G);
    //pubkey input
    
    
    //sk_Gamma input //CP
    secretkey sk_Gamma;
    sk_Gamma.sk_Delta = (attribute_set*)malloc(sizeof(attribute_set));
    sk_Gamma.sk_AS = NULL;
    sk_Gamma.sk_kStar = (basis*)malloc(sizeof(basis));
    import_sk(sk_Gamma.sk_Delta, sk_Gamma.sk_kStar, *param_G, keyfile);
    //sk_Gamma input

    
    //ciphertext input //共通
    char *input_buf = NULL;//,*keyfile_buf = NULL;
    char *aes_blob64 = NULL, *abe_blob64 = NULL, *iv_blob64 = NULL;
    ssize_t input_len;
    size_t key_len;
    
    fp = fopen(inputfile, "r");
    if(fp != NULL) {
        if((input_len = read_file(fp, &input_buf)) > 0) {
            tokenize_inputfile(input_buf, &abe_blob64, &aes_blob64, &iv_blob64);
            if(aes_blob64 == NULL || abe_blob64 == NULL || iv_blob64 == NULL) {
                fprintf(stderr, "Input file either not well-formed or not encrypted.\n");
                
                fclose(fp);
                return -1;
            }
            
            free(input_buf);
        }
    }
    else {
        fprintf(stderr, "Could not load input file: %s\n", inputfile);
        return FALSE;
    }
    fclose(fp);
    //ciphertext input
    
    //decode ciphertext //共通
    tfel_ciphertext ct;
    memset(&ct, 0, sizeof(tfel_ciphertext));
    size_t abeLength;
    unsigned char *data = NewBase64Decode((const char *) abe_blob64, strlen(abe_blob64), &abeLength);
    ct.data = data;
    ct.data_len = abeLength;
    ct.max_len = abeLength;
    //decode ciphertext
    
    //deserialize ciphertext
    /*printf("abe_blob64 = %zd\n", strlen(abe_blob64));
     printf("abeLength = %zd\n", abeLength);
     printf("ct.data = %s\n", ct.data);
     printf("ct.data_len = %zd\n", ct.data_len);
     printf("ct.max_len = %zd\n", ct.max_len);*/
    
    basis *c_i = NULL;
    c_i = (basis*)malloc(sizeof(basis));
    if (c_i == NULL){
        //error処理
    }

    AccessStructure *AS = NULL;
    AS = (AccessStructure*)malloc(sizeof(AccessStructure));
    tfel_deserialize_ciphertext_cp(AS, c_i, ct.data, ct.max_len, param_G->p);
    
    //deserialize ciphertext
    
    
    
    //decode iv
    size_t ivLength;
    char *ivec = NewBase64Decode((const char *) iv_blob64, strlen(iv_blob64), &ivLength);
    //debug("IV: ");
    //print_buffer_as_hex((uint8 *) ivec, AES_BLOCK_SIZE);
    //decode iv
    
    //decode aes
    /* decode the aesblob64 */
    size_t aesLength;
    char *aesblob = NewBase64Decode((const char *) aes_blob64, strlen(aes_blob64), &aesLength);
    //printf("sizeof(aesblob) = %zd\n", aesLength);
    //decode aes
    
    
    
    //拡大係数行列のチェック
    printf("check\n");
    AccessStructure *aAS;

    aAS = check_attribute_to_matrix(sk_Gamma.sk_Delta, AS); //errorならNULLを返す
    
    
    //alpha_iを生成
    mpz_t order;
    mpz_init(order);
    mpz_set(order, *pairing_get_order(param_G->p));
    mpz_t *alpha_i;
    mpz_t temp;
    mpz_init(temp);
    Element *K;
    Element *temp_E;
    Element temp_0;
    element_init(temp_0, param_G->p->g3);

    alpha_i = calc_alpha_i(aAS, order);
    
    
    
    rho_i *r_ptr;
    r_ptr = aAS->rho;
    
    int t; //search_tで使用
    
    for (i = 0; i < aAS->num_policy+1; i++) { //generate K
        if (i == 0) {
            K = pairing_c_k(param_G->p, NULL, c_i->M[0], sk_Gamma.sk_kStar->M[0], NULL);
        }
        else {
            t = search_t(r_ptr, sk_Gamma.sk_Delta); //r_ptrからtを持ってくる関数
            temp_E = pairing_c_k(param_G->p, r_ptr, c_i->M[i], sk_Gamma.sk_kStar->M[t], &alpha_i[i-1]);
            //temp_E = pairing_c_k(param_G->p, r_ptr, c_i->M[t], sk_Gamma.sk_kStar->M[i], &alpha_i[i-1]);
            
            element_mul(temp_0, *temp_E, *K);
            element_set(*K, temp_0);
            element_clear(*temp_E);
            free(temp_E);
            r_ptr = r_ptr->next;
        }
    }
    //printf("K generate\n");
    
    
    //Kのバイト列を16ビットのハッシュに
    key_len = element_get_oct_length(*K); // size of K
    unsigned char *K_oct;
    K_oct = (unsigned char*)malloc(sizeof(unsigned char)*key_len);
    element_to_oct(K_oct, &key_len, *K); // bytes of K
    
    unsigned char *session_key;
    session_key = (unsigned char *)malloc(sizeof(unsigned char)*16);
    //int d_len;
    
    hash_to_bytes(K_oct, key_len, SESSION_KEY_LEN, session_key, 2);
    //Kのバイト列を16ビットのハッシュに
    
    //復号
    AES_KEY sk;
    //char aes_result[aesLength+1];
    char *aes_result;
    aes_result = (char*)malloc(sizeof(char)*aesLength+1);
    AES_set_decrypt_key((uint8 *) session_key, 8*SESSION_KEY_LEN, &sk);
    memset(aes_result, 0, aesLength+1);
    AES_cbc_encrypt((uint8 *) aesblob, (uint8 *) aes_result, aesLength, &sk, (uint8 *) ivec, AES_DECRYPT);
    char magic[strlen(MAGIC)+1];
    memset(magic, 0, strlen(MAGIC)+1);
    strncpy(magic, aes_result, strlen(MAGIC));
    
    
    if(strcmp(magic, MAGIC) == 0) {
        //printf("Recovered magic: '%s'\n", magic);
        //printf("Plaintext: %s\n", (char *) (aes_result + strlen(MAGIC)));
        if ((fp = fopen(output, "w")) == NULL) {
            fprintf(stderr, "output open error\n");
        }
        else {
            fprintf(fp, "%s\n", (char*)(aes_result + strlen(MAGIC)));
            fclose(fp);
        }
        //magic_failed = FALSE;
    }
    else {
        printf("error decryption\n");
        //printf(stderr, "ERROR: ABE decryption unsuccessful!!\n");
        //magic_failed = TRUE;
    }
    //復号
    
    free(aesblob);
    free(aes_blob64);
    free(ivec);
    free(iv_blob64);
    free(data);
    free(abe_blob64);
    free(aes_result);
    //free(&input_buf);
    free(session_key);
    free(K_oct);
    element_clear(*K);
    free(K);
    if (aAS != NULL) {
        for (i = 0; i < aAS->S->row; i++) {
            mpz_clear(alpha_i[i]);
        }
        free(alpha_i);
        AccessStructure_clear(aAS);//error
    }
    element_clear(temp_0);
    mpz_clear(order);
    mpz_clear(temp);
    
    for (i = 0; i < c_i->dim; i++) { //memsetとか追加?
        free(c_i->M[i]);
    }
    free(c_i->M);
    free(c_i);
    
    Spanprogram_clear(AS->S);
    free(AS);
    
    //AccessStructure_clear(AS);//すでにAS->rhoがAccessStructure_clear(aAS)により解放されているため
    
    return 0;
}