/* * Copyright (c) 2017 Thomas Pornin * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include "inner.h" /* see bearssl_rsa.h */ uint32_t br_rsa_pkcs1_sig_unpad(const unsigned char *sig, size_t sig_len, const unsigned char *hash_oid, size_t hash_len, unsigned char *hash_out) { static const unsigned char pad1[] = {0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; unsigned char pad2[43]; size_t u, x2, x3, pad_len, zlen; if (sig_len < 11) { return 0; } /* * Expected format: * 00 01 FF ... FF 00 30 x1 30 x2 06 x3 OID [ 05 00 ] 04 x4 HASH * * with the following rules: * * -- Total length is that of the modulus and the signature * (this was already verified by br_rsa_i31_public()). * * -- There are at least eight bytes of value 0xFF. * * -- x4 is equal to the hash length (hash_len). * * -- x3 is equal to the encoded OID value length (so x3 is the * first byte of hash_oid[]). * * -- If the "05 00" is present, then x2 == x3 + 4; otherwise, * x2 == x3 + 2. * * -- x1 == x2 + x4 + 4. * * So the total length after the last "FF" is either x3 + x4 + 11 * (with the "05 00") or x3 + x4 + 9 (without the "05 00"). */ /* * Constant-time padding verification. * Accumulate errors without early exits. */ uint32_t bad = 0; /* Check "00 01 FF FF FF FF FF FF FF FF" header */ for (u = 0; u < sizeof pad1; u++) bad |= sig[u] ^ pad1[u]; /* * Find the end of FF bytes: scan all remaining bytes, * track position of first non-FF byte in constant time. */ size_t ff_end = sig_len; /* sentinel: no non-FF found */ for (u = sizeof pad1; u < sig_len; u++) { /* ff_end = min(ff_end, u) when sig[u] != 0xFF */ uint32_t is_not_ff = ((uint32_t)(sig[u] ^ 0xFF) + 0xFF) >> 8; /* 1 if != FF, 0 if == FF */ uint32_t mask = -(ff_end > u) & -is_not_ff; /* pick u only on first non-FF */ ff_end = (ff_end & ~(size_t)mask) | (u & (size_t)mask); } u = ff_end; if (hash_oid == NULL) { bad |= (sig_len - u != hash_len + 1); bad |= sig[u < sig_len ? u : 0]; /* sig[u] must be 0x00 */ } else { x3 = hash_oid[0]; pad_len = x3 + 9; memset(pad2, 0, pad_len); zlen = sig_len - u - hash_len; /* Accept either pad_len (no NULL params) or pad_len+2 (with "05 00") */ uint32_t has_null = (zlen == pad_len + 2); bad |= (zlen != pad_len) & !has_null; if (has_null) { x2 = x3 + 4; pad_len = zlen; pad2[pad_len - 4] = 0x05; } else { x2 = x3 + 2; } pad2[0] = 0x00; pad2[1] = 0x30; pad2[2] = x2 + hash_len + 4; pad2[3] = 0x30; pad2[4] = x2; pad2[5] = 0x06; memcpy(pad2 + 6, hash_oid, x3 + 1); pad2[pad_len - 2] = 0x04; pad2[pad_len - 1] = hash_len; /* Constant-time comparison of padding structure */ for (size_t i = 0; i < pad_len && u + i < sig_len; i++) bad |= pad2[i] ^ sig[u + i]; } if (bad) return 0; memcpy(hash_out, sig + sig_len - hash_len, hash_len); return 1; }