Skip to content
This repository was archived by the owner on Mar 5, 2024. It is now read-only.

Commit 3ab37e5

Browse files
author
Constanza Heath
committed
Adding ecc_platform_secific files.
Signed-off-by: Constanza Heath <[email protected]>
1 parent c209ffc commit 3ab37e5

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* uECC_platform_specific.h - Interface to platform specific functions*/
2+
3+
/* Copyright (c) 2014, Kenneth MacKay
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.*/
25+
26+
/*
27+
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
28+
*
29+
* Redistribution and use in source and binary forms, with or without
30+
* modification, are permitted provided that the following conditions are met:
31+
*
32+
* - Redistributions of source code must retain the above copyright notice,
33+
* this list of conditions and the following disclaimer.
34+
*
35+
* - Redistributions in binary form must reproduce the above copyright
36+
* notice, this list of conditions and the following disclaimer in the
37+
* documentation and/or other materials provided with the distribution.
38+
*
39+
* - Neither the name of Intel Corporation nor the names of its contributors
40+
* may be used to endorse or promote products derived from this software
41+
* without specific prior written permission.
42+
*
43+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
44+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
47+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
53+
* POSSIBILITY OF SUCH DAMAGE.
54+
*
55+
* uECC_platform_specific.h -- Interface to platform specific functions
56+
*/
57+
58+
#ifndef __UECC_PLATFORM_SPECIFIC_H_
59+
#define __UECC_PLATFORM_SPECIFIC_H_
60+
61+
/*
62+
* The RNG function should fill 'size' random bytes into 'dest'. It should
63+
* return 1 if 'dest' was filled with random data, or 0 if the random data could
64+
* not be generated. The filled-in values should be either truly random, or from
65+
* a cryptographically-secure PRNG.
66+
*
67+
* A cryptographically-secure PRNG function must be set (using uECC_set_rng())
68+
* before calling uECC_make_key() or uECC_sign().
69+
*
70+
* Setting a cryptographically-secure PRNG function improves the resistance to
71+
* side-channel attacks for uECC_shared_secret().
72+
*
73+
* A correct PRNG function is set by default (default_RNG_defined = 1) and works
74+
* for some platforms, such as Unix and Linux. For other platforms, you may need
75+
* to provide another PRNG function.
76+
*/
77+
#define default_RNG_defined 1
78+
79+
int default_CSPRNG(uint8_t *dest, unsigned int size);
80+
81+
#endif /* __UECC_PLATFORM_SPECIFIC_H_ */

lib/source/ecc_platform_specific.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* uECC_platform_specific.c - Implementation of platform specific functions*/
2+
3+
/* Copyright (c) 2014, Kenneth MacKay
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.*/
25+
26+
/*
27+
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
28+
*
29+
* Redistribution and use in source and binary forms, with or without
30+
* modification, are permitted provided that the following conditions are met:
31+
*
32+
* - Redistributions of source code must retain the above copyright notice,
33+
* this list of conditions and the following disclaimer.
34+
*
35+
* - Redistributions in binary form must reproduce the above copyright
36+
* notice, this list of conditions and the following disclaimer in the
37+
* documentation and/or other materials provided with the distribution.
38+
*
39+
* - Neither the name of Intel Corporation nor the names of its contributors
40+
* may be used to endorse or promote products derived from this software
41+
* without specific prior written permission.
42+
*
43+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
44+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
47+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
53+
* POSSIBILITY OF SUCH DAMAGE.
54+
*
55+
* uECC_platform_specific.c -- Implementation of platform specific functions
56+
*/
57+
58+
59+
#if defined(unix) || defined(__linux__) || defined(__unix__) || \
60+
defined(__unix) | (defined(__APPLE__) && defined(__MACH__)) || \
61+
defined(uECC_POSIX)
62+
63+
/* Some POSIX-like system with /dev/urandom or /dev/random. */
64+
#include <sys/types.h>
65+
#include <fcntl.h>
66+
#include <unistd.h>
67+
68+
#include <stdint.h>
69+
70+
#ifndef O_CLOEXEC
71+
#define O_CLOEXEC 0
72+
#endif
73+
74+
int default_CSPRNG(uint8_t *dest, unsigned int size) {
75+
76+
/* input sanity check: */
77+
if (dest == (uint8_t *) 0 || (size <= 0))
78+
return 0;
79+
80+
int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
81+
if (fd == -1) {
82+
fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
83+
if (fd == -1) {
84+
return 0;
85+
}
86+
}
87+
88+
char *ptr = (char *)dest;
89+
size_t left = (size_t) size;
90+
while (left > 0) {
91+
ssize_t bytes_read = read(fd, ptr, left);
92+
if (bytes_read <= 0) { // read failed
93+
close(fd);
94+
return 0;
95+
}
96+
left -= bytes_read;
97+
ptr += bytes_read;
98+
}
99+
100+
close(fd);
101+
return 1;
102+
}
103+
104+
#endif /* platform */
105+

tests/pseudo-random-data.bin

512 KB
Binary file not shown.

0 commit comments

Comments
 (0)