Merge branch 'upstream' into tizen
[platform/upstream/cryptsetup.git] / lib / verity / rs_decode_char.c
1 /*
2  * Reed-Solomon decoder, based on libfec
3  *
4  * Copyright (C) 2002, Phil Karn, KA9Q
5  * libcryptsetup modifications
6  *   Copyright (C) 2017-2023 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 #include <string.h>
24 #include <stdlib.h>
25
26 #include "rs.h"
27
28 #define MAX_NR_BUF 256
29
30 int decode_rs_char(struct rs* rs, data_t* data)
31 {
32         int deg_lambda, el, deg_omega, syn_error, count;
33         int i, j, r, k;
34         data_t q, tmp, num1, num2, den, discr_r;
35         data_t lambda[MAX_NR_BUF], s[MAX_NR_BUF]; /* Err+Eras Locator poly and syndrome poly */
36         data_t b[MAX_NR_BUF], t[MAX_NR_BUF], omega[MAX_NR_BUF];
37         data_t root[MAX_NR_BUF], reg[MAX_NR_BUF], loc[MAX_NR_BUF];
38
39         if (rs->nroots >= MAX_NR_BUF)
40                 return -1;
41
42         memset(s, 0, rs->nroots * sizeof(data_t));
43         memset(b, 0, (rs->nroots + 1) * sizeof(data_t));
44
45         /* form the syndromes; i.e., evaluate data(x) at roots of g(x) */
46         for (i = 0; i < rs->nroots; i++)
47                 s[i] = data[0];
48
49         for (j = 1; j < rs->nn - rs->pad; j++) {
50                 for (i = 0; i < rs->nroots; i++) {
51                         if (s[i] == 0) {
52                                 s[i] = data[j];
53                         } else {
54                                 s[i] = data[j] ^ rs->alpha_to[modnn(rs, rs->index_of[s[i]] + (rs->fcr + i) * rs->prim)];
55                         }
56                 }
57         }
58
59         /* Convert syndromes to index form, checking for nonzero condition */
60         syn_error = 0;
61         for (i = 0; i < rs->nroots; i++) {
62                 syn_error |= s[i];
63                 s[i] = rs->index_of[s[i]];
64         }
65
66         /*
67          * if syndrome is zero, data[] is a codeword and there are no
68          * errors to correct. So return data[] unmodified
69          */
70         if (!syn_error)
71                 return 0;
72
73         memset(&lambda[1], 0, rs->nroots * sizeof(lambda[0]));
74         lambda[0] = 1;
75
76         for (i   = 0; i < rs->nroots + 1; i++)
77                 b[i] = rs->index_of[lambda[i]];
78
79         /*
80          * Begin Berlekamp-Massey algorithm to determine error+erasure
81          * locator polynomial
82          */
83         r  = 0;
84         el = 0;
85         while (++r <= rs->nroots) { /* r is the step number */
86                 /* Compute discrepancy at the r-th step in poly-form */
87                 discr_r = 0;
88                 for (i = 0; i < r; i++) {
89                         if ((lambda[i] != 0) && (s[r - i - 1] != A0)) {
90                                 discr_r ^= rs->alpha_to[modnn(rs, rs->index_of[lambda[i]] + s[r - i - 1])];
91                         }
92                 }
93                 discr_r = rs->index_of[discr_r]; /* Index form */
94                 if (discr_r == A0) {
95                         /* 2 lines below: B(x) <-- x*B(x) */
96                         memmove(&b[1], b, rs->nroots * sizeof(b[0]));
97                         b[0] = A0;
98                 } else {
99                         /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
100                         t[0] = lambda[0];
101                         for (i = 0; i < rs->nroots; i++) {
102                                 if (b[i] != A0)
103                                         t[i + 1] = lambda[i + 1] ^ rs->alpha_to[modnn(rs, discr_r + b[i])];
104                                 else
105                                         t[i + 1] = lambda[i + 1];
106                         }
107                         if (2 * el <= r - 1) {
108                                 el = r - el;
109                                 /*
110                                  * 2 lines below: B(x) <-- inv(discr_r) *
111                                  * lambda(x)
112                                  */
113                                 for (i   = 0; i <= rs->nroots; i++)
114                                         b[i] = (lambda[i] == 0) ? A0 : modnn(rs, rs->index_of[lambda[i]] - discr_r + rs->nn);
115                         } else {
116                                 /* 2 lines below: B(x) <-- x*B(x) */
117                                 memmove(&b[1], b, rs->nroots * sizeof(b[0]));
118                                 b[0] = A0;
119                         }
120                         memcpy(lambda, t, (rs->nroots + 1) * sizeof(t[0]));
121                 }
122         }
123
124         /* Convert lambda to index form and compute deg(lambda(x)) */
125         deg_lambda = 0;
126         for (i = 0; i < rs->nroots + 1; i++) {
127                 lambda[i] = rs->index_of[lambda[i]];
128                 if (lambda[i] != A0)
129                         deg_lambda = i;
130         }
131         /* Find roots of the error+erasure locator polynomial by Chien search */
132         memcpy(&reg[1], &lambda[1], rs->nroots * sizeof(reg[0]));
133         count = 0; /* Number of roots of lambda(x) */
134         for (i = 1, k = rs->iprim - 1; i <= rs->nn; i++, k = modnn(rs, k + rs->iprim)) {
135                 q = 1; /* lambda[0] is always 0 */
136                 for (j = deg_lambda; j > 0; j--) {
137                         if (reg[j] != A0) {
138                                 reg[j] = modnn(rs, reg[j] + j);
139                                 q ^= rs->alpha_to[reg[j]];
140                         }
141                 }
142                 if (q != 0)
143                         continue; /* Not a root */
144
145                 /* store root (index-form) and error location number */
146                 root[count] = i;
147                 loc[count]  = k;
148                 /* If we've already found max possible roots, abort the search to save time */
149                 if (++count == deg_lambda)
150                         break;
151         }
152
153         /*
154          * deg(lambda) unequal to number of roots => uncorrectable
155          * error detected
156          */
157         if (deg_lambda != count)
158                 return -1;
159
160         /*
161          * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
162          * x**rs->nroots). in index form. Also find deg(omega).
163          */
164         deg_omega = deg_lambda - 1;
165         for (i = 0; i <= deg_omega; i++) {
166                 tmp = 0;
167                 for (j = i; j >= 0; j--) {
168                         if ((s[i - j] != A0) && (lambda[j] != A0))
169                                 tmp ^= rs->alpha_to[modnn(rs, s[i - j] + lambda[j])];
170                 }
171                 omega[i] = rs->index_of[tmp];
172         }
173
174         /*
175          * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
176          * inv(X(l))**(rs->fcr-1) and den = lambda_pr(inv(X(l))) all in poly-form
177          */
178         for (j = count - 1; j >= 0; j--) {
179                 num1 = 0;
180                 for (i = deg_omega; i >= 0; i--) {
181                         if (omega[i] != A0)
182                                 num1 ^= rs->alpha_to[modnn(rs, omega[i] + i * root[j])];
183                 }
184                 num2 = rs->alpha_to[modnn(rs, root[j] * (rs->fcr - 1) + rs->nn)];
185                 den  = 0;
186
187                 /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
188                 for (i = RS_MIN(deg_lambda, rs->nroots - 1) & ~1; i >= 0; i -= 2) {
189                         if (lambda[i + 1] != A0)
190                                 den ^= rs->alpha_to[modnn(rs, lambda[i + 1] + i * root[j])];
191                 }
192
193                 /* Apply error to data */
194                 if (num1 != 0 && loc[j] >= rs->pad) {
195                         data[loc[j] - rs->pad] ^= rs->alpha_to[modnn(rs, rs->index_of[num1] +
196                                                   rs->index_of[num2] + rs->nn - rs->index_of[den])];
197                 }
198         }
199
200         return count;
201 }