crypto_mem.c
Go to the documentation of this file.
1 /**
2  * @file crypto_mem.c
3  * @brief Secure memory operations
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.8
29  **/
30 
31 //Dependencies
32 #include "crypto.h"
33 #include "crypto_mem.h"
34 
35 //Volatile pointer to the memset function
36 void * (* const volatile volatile_memset) (void *, int, size_t) = memset;
37 
38 
39 /**
40  * @brief Securely erase a memory block
41  * @param[out] p Pointer to the memory block to be erased
42  * @param[in] n Number of bytes to erase
43  **/
44 
45 __weak_func void cryptoZeroizeMem(void *p, size_t n)
46 {
47  //Zeroize memory
48  volatile_memset(p, 0, n);
49 }
50 
51 
52 /**
53  * @brief Constant-time comparison of two memory blocks
54  * @param[in] p1 Pointer to the first memory block
55  * @param[in] p2 Pointer to the second memory block
56  * @param[in] n Length of the memory blocks, in bytes
57  * @return 0 if the two blocks are identical, a non-zero value otherwise
58  **/
59 
60 uint_t cryptoCompMem(const void *p1, const void *p2, size_t n)
61 {
62  size_t i;
63  uint8_t mask;
64  const uint8_t *a;
65  const uint8_t *b;
66 
67  //Point to the memory blocks to compare
68  a = (const uint8_t *) p1;
69  b = (const uint8_t *) p2;
70 
71  //Compare the two memory blocks byte by byte, without any early exit
72  for(mask = 0, i = 0; i < n; i++)
73  {
74  mask |= a[i] ^ b[i];
75  }
76 
77  //The result is zero if and only if the two blocks are identical
78  return mask;
79 }
uint8_t b
Definition: nbns_common.h:122
uint8_t a
Definition: ndp.h:411
uint8_t p
Definition: ndp.h:300
General definitions for cryptographic library.
uint8_t mask
Definition: web_socket.h:319
void *(*constvolatile volatile_memset)(void *, int, size_t)
Definition: crypto_mem.c:36
Secure memory operations.
uint8_t n
__weak_func void cryptoZeroizeMem(void *p, size_t n)
Securely erase a memory block.
Definition: crypto_mem.c:45
unsigned int uint_t
Definition: compiler_port.h:57
uint_t cryptoCompMem(const void *p1, const void *p2, size_t n)
Constant-time comparison of two memory blocks.
Definition: crypto_mem.c:60