Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
cx_crc32.c
Go to the documentation of this file.
1
2/*******************************************************************************
3 * Ledger Nano S - Secure firmware
4 * (c) 2022 Ledger
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 ********************************************************************************/
18
19#ifdef HAVE_CRC
20
21#include "lcx_crc.h"
22#include "cx_ram.h"
23#include "ox_crc.h"
24
25static uint32_t reverse_32_bits(uint32_t value)
26{
27 uint32_t reverse_val = 0;
28
29 for (uint8_t i = 0; i < 32; i++) {
30 if ((value & (1 << i))) {
31 reverse_val |= 1 << ((32 - 1) - i);
32 }
33 }
34 return reverse_val;
35}
36
37// x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
38uint32_t cx_crc32(const void *buf, size_t len)
39{
40 return cx_crc_hw(CRC_TYPE_CRC32, CX_CRC32_INIT, buf, len);
41}
42
43uint32_t cx_crc32_update(uint32_t crc_state, const void *buf, size_t len)
44{
45 crc_state = reverse_32_bits(crc_state);
46 crc_state ^= 0xFFFFFFFF;
47 return cx_crc_hw(CRC_TYPE_CRC32, crc_state, buf, len);
48}
49#endif // HAVE_CRC
CRC (Cyclic Redundancy Check).