| 1 |
|
| /*- |
| 2 |
|
| * Copyright 2013-2018 Alexander Peslyak |
| 3 |
|
| * All rights reserved. |
| 4 |
|
| * |
| 5 |
|
| * Redistribution and use in source and binary forms, with or without |
| 6 |
|
| * modification, are permitted. |
| 7 |
|
| * |
| 8 |
|
| * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 9 |
|
| * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 10 |
|
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 11 |
|
| * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 12 |
|
| * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 13 |
|
| * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 14 |
|
| * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 15 |
|
| * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 16 |
|
| * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 17 |
|
| * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 18 |
|
| * SUCH DAMAGE. |
| 19 |
|
| */ |
| 20 |
|
| |
| 21 |
|
| /* |
| 22 |
|
| * cpx: only the portable malloc()/free() allocation path is compiled (upstream |
| 23 |
|
| * uses it when mmap() is unavailable). No mmap, MAP_HUGETLB or posix_memalign, |
| 24 |
|
| * so behavior is the same on every platform, including Win32. |
| 25 |
|
| */ |
| 26 |
|
| |
| 27 |
20 |
| static void *alloc_region(yescrypt_region_t *region, size_t size) |
| 28 |
|
| { |
| 29 |
|
| uint8_t *base, *aligned; |
| 30 |
|
| |
| 31 |
20 |
| base = aligned = NULL; |
| 32 |
20 |
▶ | if (size + 63 < size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size + 63 < size) { |
0 |
20 |
20 |
|
| 33 |
0 |
| errno = ENOMEM; |
| 34 |
20 |
▶ | } else if ((base = malloc(size + 63)) != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ((base = malloc(size + 63)) != NULL) { |
20 |
0 |
20 |
|
| 35 |
20 |
| aligned = base + 63; |
| 36 |
20 |
| aligned -= (uintptr_t)aligned & 63; |
| 37 |
|
| } |
| 38 |
20 |
| region->base = base; |
| 39 |
20 |
| region->aligned = aligned; |
| 40 |
20 |
▶ | region->base_size = base ? size + 63 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| region->base_size = base ? size + 63 : 0; |
20 |
0 |
20 |
|
| 41 |
20 |
▶ | region->aligned_size = base ? size : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| region->aligned_size = base ? size : 0; |
20 |
0 |
20 |
|
| 42 |
20 |
| return aligned; |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
144 |
| static inline void init_region(yescrypt_region_t *region) |
| 46 |
|
| { |
| 47 |
144 |
| region->base = region->aligned = NULL; |
| 48 |
144 |
| region->base_size = region->aligned_size = 0; |
| 49 |
144 |
| } |
| 50 |
|
| |
| 51 |
80 |
| static int free_region(yescrypt_region_t *region) |
| 52 |
|
| { |
| 53 |
80 |
▶ | if (region->base) |
Branch: 1 branch
| Branch | True | False | Total |
| if (region->base) |
20 |
60 |
80 |
|
| 54 |
20 |
| free(region->base); |
| 55 |
80 |
| init_region(region); |
| 56 |
80 |
| return 0; |
| 57 |
|
| } |