Embedded SDK
Embedded SDK
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 
24 static uint32_t reverse_32_bits(uint32_t value)
25 {
26  uint32_t reverse_val = 0;
27 
28  for (uint8_t i = 0; i < 32; i++) {
29  if ((value & (1 << i))) {
30  reverse_val |= 1 << ((32 - 1) - i);
31  }
32  }
33  return reverse_val;
34 }
35 
36 // 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
37 uint32_t cx_crc32(const void *buf, size_t len)
38 {
39  return cx_crc_hw(CRC_TYPE_CRC32, CX_CRC32_INIT, buf, len);
40 }
41 
42 uint32_t cx_crc32_update(uint32_t crc_state, const void *buf, size_t len)
43 {
44  crc_state = reverse_32_bits(crc_state);
45  crc_state ^= 0xFFFFFFFF;
46  return cx_crc_hw(CRC_TYPE_CRC32, crc_state, buf, len);
47 }
48 #endif // HAVE_CRC
CRC (Cyclic Redundancy Check).
unsigned char uint8_t
Definition: usbd_conf.h:53