Path: blob/main/contrib/bearssl/src/int/i15_decmod.c
39483 views
/*1* Copyright (c) 2017 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526/* see inner.h */27uint32_t28br_i15_decode_mod(uint16_t *x, const void *src, size_t len, const uint16_t *m)29{30/*31* Two-pass algorithm: in the first pass, we determine whether the32* value fits; in the second pass, we do the actual write.33*34* During the first pass, 'r' contains the comparison result so35* far:36* 0x00000000 value is equal to the modulus37* 0x00000001 value is greater than the modulus38* 0xFFFFFFFF value is lower than the modulus39*40* Since we iterate starting with the least significant bytes (at41* the end of src[]), each new comparison overrides the previous42* except when the comparison yields 0 (equal).43*44* During the second pass, 'r' is either 0xFFFFFFFF (value fits)45* or 0x00000000 (value does not fit).46*47* We must iterate over all bytes of the source, _and_ possibly48* some extra virtual bytes (with value 0) so as to cover the49* complete modulus as well. We also add 4 such extra bytes beyond50* the modulus length because it then guarantees that no accumulated51* partial word remains to be processed.52*/53const unsigned char *buf;54size_t mlen, tlen;55int pass;56uint32_t r;5758buf = src;59mlen = (m[0] + 15) >> 4;60tlen = (mlen << 1);61if (tlen < len) {62tlen = len;63}64tlen += 4;65r = 0;66for (pass = 0; pass < 2; pass ++) {67size_t u, v;68uint32_t acc;69int acc_len;7071v = 1;72acc = 0;73acc_len = 0;74for (u = 0; u < tlen; u ++) {75uint32_t b;7677if (u < len) {78b = buf[len - 1 - u];79} else {80b = 0;81}82acc |= (b << acc_len);83acc_len += 8;84if (acc_len >= 15) {85uint32_t xw;8687xw = acc & (uint32_t)0x7FFF;88acc_len -= 15;89acc = b >> (8 - acc_len);90if (v <= mlen) {91if (pass) {92x[v] = r & xw;93} else {94uint32_t cc;9596cc = (uint32_t)CMP(xw, m[v]);97r = MUX(EQ(cc, 0), r, cc);98}99} else {100if (!pass) {101r = MUX(EQ(xw, 0), r, 1);102}103}104v ++;105}106}107108/*109* When we reach this point at the end of the first pass:110* r is either 0, 1 or -1; we want to set r to 0 if it111* is equal to 0 or 1, and leave it to -1 otherwise.112*113* When we reach this point at the end of the second pass:114* r is either 0 or -1; we want to leave that value115* untouched. This is a subcase of the previous.116*/117r >>= 1;118r |= (r << 1);119}120121x[0] = m[0];122return r & (uint32_t)1;123}124125126