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