summaryrefslogtreecommitdiff
path: root/src/backends/shss/shss.c
blob: 601fc01b4045df7cfb73daa7442cc8f02b8a17f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Copyright(c) 2015 NTT corp. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.  THIS SOFTWARE IS PROVIDED BY
 * THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE,
 * DATA, OR PROFITS;OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * liberasurecode shss backend
 *
 * Please contact us if you are insterested in the NTT backend (welcome!):
 * Kota Tsuyuzaki <tsuyuzaki.kota@lab.ntt.co.jp>
 *
 * vi: set noai tw=79 ts=4 sw=4:
 */

#include <stdio.h>
#include <stdlib.h>

#include "erasurecode.h"
#include "erasurecode_helpers.h"
#include "erasurecode_backend.h"

/* Forward declarations */
struct ec_backend shss;
struct ec_backend_op_stubs shss_ops;
struct ec_backend_common backend_shss;

typedef int (*shss_encode_func)(char **, size_t, int, int, int, int, long long *);
typedef int (*shss_decode_func)(char **, size_t, int *, int, int, int, int, int, long long *);
typedef int (*shss_reconstruct_func)(char **, size_t, int *, int, int *, int, int, int, int, int, long long *);

struct shss_descriptor {
    /* calls required for init */
    shss_encode_func ssencode;
    shss_decode_func ssdecode;
    shss_reconstruct_func ssreconst;

    /* fields needed to hold state */
    int k;
    int m;
    int n;
    int w;
    int aes_bit_length;
};

#define SHSS_LIB_VER_STR "1.0"
#define SHSS_LIB_NAME "shss"
#if defined(__MACOS__) || defined(__MACOSX__) || defined(__OSX__) || defined(__APPLE__)
#define SHSS_SO_NAME "libshss.dylib"
#else
#define SHSS_SO_NAME "libshss.so"
#endif
#define DEFAULT_W 128
#define METADATA 32

static int shss_encode(void *desc, char **data, char **parity,
        int blocksize)
{
    int i;
    int ret = 0;
    int priv_bitnum = 128; // privacy bit number 0 or 128(default) or 256
    int chksum = 0; // chksum 0 or 64
    char **encoded;
    long long einfo;
    struct shss_descriptor *xdesc =
        (struct shss_descriptor *) desc;

    if (xdesc->aes_bit_length != -1) {
        priv_bitnum = xdesc->aes_bit_length;
    }

    encoded = alloca(sizeof(char*)*xdesc->n);

    for (i = 0; i<xdesc->k; i++) encoded[i] = (char*)data[i];
    for (i = 0; i<xdesc->m; i++) encoded[i+xdesc->k] = (char*)parity[i];

    ret = xdesc->ssencode((char**)encoded, (size_t)blocksize,
                        xdesc->k, xdesc->m, priv_bitnum, chksum, &einfo);

    if (ret > 0) {
        return -ret;
    }

    return 0;
}

static int shss_decode(void *desc, char **data, char **parity,
        int *missing_idxs, int blocksize)
{
    int i;
    int missing_size = 0;
    int ret = 0;
    int priv_bitnum = 128; // privacy bit number 0 or 128(default) or 256
    int chksum = 0; // chksum 0 or 64
    char **decoded;
    long long einfo;
    struct shss_descriptor *xdesc =
        (struct shss_descriptor *) desc;

    if (xdesc->aes_bit_length != -1) {
        priv_bitnum = xdesc->aes_bit_length;
    }

    decoded = alloca(sizeof(char*)*xdesc->n);

    for (i = 0; i<xdesc->k; i++) decoded[i] = (char*)data[i];
    for (i = 0; i<xdesc->m; i++) decoded[i+xdesc->k] = (char*)parity[i];
    for (i = 0; i<xdesc->n; i++) {
        if (i == missing_idxs[missing_size]) {
            missing_size++;
        }
    }

    ret = xdesc->ssdecode((char**)decoded, (size_t)blocksize, missing_idxs, missing_size,
                         xdesc->k, xdesc->m, priv_bitnum, chksum, &einfo);

    if (ret > 0) {
        return -ret;
    }

    return 0;
}

static int shss_reconstruct(void *desc, char **data, char **parity,
        int *missing_idxs, int destination_idx, int blocksize)
{
    int i;
    int missing_size = 0;
    int ret = 0;
    int priv_bitnum = 128; // privacy bit number 0 or 128(default) or 256
    int chksum = 0; // chksum 0 or 64
    int dst_size = 1;
    char **reconstructed;
    long long einfo;
    struct shss_descriptor *xdesc =
        (struct shss_descriptor *) desc;

    if (xdesc->aes_bit_length != -1) {
        priv_bitnum = xdesc->aes_bit_length;
    }

    reconstructed = alloca(sizeof(char*)*xdesc->n);

    for (i = 0; i<xdesc->k; i++) reconstructed[i] = (char*)data[i];
    for (i = 0; i<xdesc->m; i++) reconstructed[i+xdesc->k] = (char*)parity[i];
    for (i = 0; i<xdesc->n; i++) {
        if (i == missing_idxs[missing_size]) {
            missing_size++;
        }
    }

    ret = xdesc->ssreconst((char**)reconstructed, (size_t)blocksize,
                          &destination_idx, dst_size, missing_idxs, missing_size, xdesc->k,
                          xdesc->m, priv_bitnum, chksum, &einfo);

    if (ret > 0) {
        return -ret;
    }

    return 0;
}

static int shss_fragments_needed(void *desc, int *missing_idxs,
        int *fragments_to_exclude, int *fragments_needed)
{
    struct shss_descriptor *xdesc =
        (struct shss_descriptor *) desc;
    uint64_t exclude_bm = convert_list_to_bitmap(fragments_to_exclude);
    uint64_t missing_bm = convert_list_to_bitmap(missing_idxs) | exclude_bm;
    int i;
    int j = 0;
    int ret = -101;

    for (i = 0; i < xdesc->n; i++) {
        if (!(missing_bm & (1 << i))) {
            fragments_needed[j] = i;
            j++;
        }
        if (j == xdesc->k) {
            ret = 0;
            fragments_needed[j] = -1;
            break;
        }
    }

    return ret;
}

/**
 * Return the element-size, which is the number of bits stored
 * on a given device, per codeword.  This is usually just 'w'.
 */
static int shss_element_size(void* desc)
{
    return DEFAULT_W;
}

static void * shss_init(struct ec_backend_args *args, void *backend_sohandle)
{
    struct shss_descriptor *desc = NULL;

    desc = (struct shss_descriptor *)
           malloc(sizeof(struct shss_descriptor));
    if (NULL == desc) {
        return NULL;
    }

    desc->k = args->uargs.k;
    desc->m = args->uargs.m;
    desc->n = args->uargs.k + args->uargs.m;
    desc->w = DEFAULT_W;
    args->uargs.w = DEFAULT_W;

    /* Sample on how to pass extra args to the backend */
    // TODO: Need discussion how to pass extra args.
    // tentatively we could pass with priv_args2 as the bit_length
    int *priv = (int *)args->uargs.priv_args2;
    if(priv != NULL){
        desc->aes_bit_length = priv[0]; // AES bit number
    }else{
        desc->aes_bit_length = 128;
    }

    union {
        shss_encode_func encodep;
        shss_decode_func decodep;
        shss_reconstruct_func reconp;
        void *vptr;
    } func_handle;

    func_handle.vptr = NULL;
    func_handle.vptr = dlsym(backend_sohandle, "ssencode");
    desc->ssencode = func_handle.encodep;
    if (NULL == desc->ssencode) {
        goto error;
    }

    func_handle.vptr = NULL;
    func_handle.vptr = dlsym(backend_sohandle, "ssdecode");
    desc->ssdecode = func_handle.decodep;
    if (NULL == desc->ssdecode) {
        goto error;
    }

    func_handle.vptr = NULL;
    func_handle.vptr = dlsym(backend_sohandle, "ssreconst");
    desc->ssreconst = func_handle.reconp;
    if (NULL == desc->ssreconst) {
        goto error;
    }

    return desc;

error:
    free(desc);

    return NULL;
}

static int shss_exit(void *desc)
{
    if (desc != NULL) {
        free(desc);
    }
    return 0;
}

static bool shss_is_compatible_with(uint32_t version) {
    return version == backend_shss.ec_backend_version;
}

struct ec_backend_op_stubs shss_op_stubs = {
    .INIT                       = shss_init,
    .EXIT                       = shss_exit,
    .ENCODE                     = shss_encode,
    .DECODE                     = shss_decode,
    .FRAGSNEEDED                = shss_fragments_needed,
    .RECONSTRUCT                = shss_reconstruct,
    .ELEMENTSIZE                = shss_element_size,
    .ISCOMPATIBLEWITH           = shss_is_compatible_with,
};

struct ec_backend_common backend_shss = {
    .id                         = EC_BACKEND_SHSS,
    .name                       = SHSS_LIB_NAME,
    .soname                     = SHSS_SO_NAME,
    .soversion                  = SHSS_LIB_VER_STR,
    .ops                        = &shss_op_stubs,
    .backend_metadata_size      = METADATA,
};