91718147cf510491f12b7f609e27b825fa21a01a
[platform/upstream/cryptsetup.git] / lib / verity / rs.h
1 /*
2  * Reed-Solomon codecs, based on libfec
3  *
4  * Copyright (C) 2004 Phil Karn, KA9Q
5  * libcryptsetup modifications
6  *   Copyright (C) 2017-2020 Red Hat, Inc. All rights reserved.
7  *
8  * This file is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This file is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this file; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifndef _LIBFEC_RS_H
24 #define _LIBFEC_RS_H
25
26 /* Special reserved value encoding zero in index form. */
27 #define A0 (rs->nn)
28
29 #define RS_MIN(a, b) ((a) < (b) ? (a) : (b))
30
31 typedef unsigned char data_t;
32
33 /* Reed-Solomon codec control block */
34 struct rs {
35         int mm;          /* Bits per symbol */
36         int nn;          /* Symbols per block (= (1<<mm)-1) */
37         data_t *alpha_to;/* log lookup table */
38         data_t *index_of;/* Antilog lookup table */
39         data_t *genpoly; /* Generator polynomial */
40         int nroots;      /* Number of generator roots = number of parity symbols */
41         int fcr;         /* First consecutive root, index form */
42         int prim;        /* Primitive element, index form */
43         int iprim;       /* prim-th root of 1, index form */
44         int pad;         /* Padding bytes in shortened block */
45 };
46
47 static inline int modnn(struct rs *rs, int x)
48 {
49         while (x >= rs->nn) {
50                 x -= rs->nn;
51                 x = (x >> rs->mm) + (x & rs->nn);
52         }
53         return x;
54 }
55
56 struct rs *init_rs_char(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad);
57 void free_rs_char(struct rs *rs);
58
59 /* General purpose RS codec, 8-bit symbols */
60 void encode_rs_char(struct rs *rs, data_t *data, data_t *parity);
61 int decode_rs_char(struct rs *rs, data_t *data);
62
63 #endif