<!-- canonical: https://0xsimao.com/findings/keyring-ii-pkcs1-sha256-modified-original -->

# RsaVerifyOptimized::pkcs1Sha256() modified the original code incorrectly in one instance

Medium · Three Sigma · Zero-knowledge compliance · 10th July, 2024

Finding 3S-Keyring-M02 of the Keyring Credentials security review.

- Protocol: https://www.keyring.network/
- Report: /reports/keyring-ii
- Source: https://cdn.sanity.io/files/qoqld077/production/75b68b74f4b0dc6fbcd94892d934547d8259b57a.pdf

---

### Description

RsaVerifyOptimized::pkcs1Sha256() [code](https://github.com/adria0/SolRsaVerify/tree/master?tab=readme-ov-file#usage-with-openssl-openssl-311) has slight changes to the original code. [There](https://github.com/Keyring-Network/core-v2/blob/master/src/lib/RsaVerifyOptimized.sol#L273)
is one modification that is different from the original, which is checking if the first bytes of
decipher are 0x00 and 0x01, respectively. As can be seen in the following code snippet, the
first 2 bytes of decipher may be for example 0x0101 and it will not set the result to false.

```solidity
// if (decipher[0] != 0 || decipher[1] != 0x01) {
    // return false;
    // }
if iszero(and(mload(add(decipher, 32)), 0x0001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)) {
    result := false
}
```

Additionally, here should be 111, but the code is not reachable as it only accepts
digestAlgoWithParamLen == 17 == sha256ExplicitNullParamByteLen.

### Recommendation

Use the previous optimized assembly code.

### Status

Addressed in [#02b0fa2](https://github.com/Keyring-Network/core-v2/commit/02b0fa29316ae970c4707dc7696fbc6ed5c6d9c5#diff-fb7295f993ba1bb3e38c525e603217eaf763c5e5515a40f365f1135404dbb8f4).

---

Related findings:

- [Total cds deposited amount is incorrectly modified when cds depositor is at a loss, leading to stuck USDa](https://0xsimao.com/findings/autonomint-deposited-modified-depositor-stuck): Autonomint
- [`FluidLocker::_getUnlockingPercentage()` incorrectly divides one of the components of the formula by `S`, leading to always having `80%` penalty](https://0xsimao.com/findings/superfluid-locker-system-unlocking-components-formula-penalty): Superfluid Locker System
