-/*
- * Replacement for memset(s, 0, n) on stack that can be optimized out
- * Also used in safe allocations for explicit memory wipe.
- */
-void crypt_memzero(void *s, size_t n)
-{
- volatile uint8_t *p = (volatile uint8_t *)s;
-
- while(n--)
- *p++ = 0;
-}
-
-/* safe allocations */
-void *crypt_safe_alloc(size_t size)
-{
- struct safe_allocation *alloc;
-
- if (!size)
- return NULL;
-
- alloc = malloc(size + offsetof(struct safe_allocation, data));
- if (!alloc)
- return NULL;
-
- alloc->size = size;
- crypt_memzero(&alloc->data, size);
-
- /* coverity[leaked_storage] */
- return &alloc->data;
-}
-
-void crypt_safe_free(void *data)
-{
- struct safe_allocation *alloc;
-
- if (!data)
- return;
-
- alloc = (struct safe_allocation *)
- ((char *)data - offsetof(struct safe_allocation, data));
-
- crypt_memzero(data, alloc->size);
-
- alloc->size = 0x55aa55aa;
- free(alloc);
-}
-
-void *crypt_safe_realloc(void *data, size_t size)
-{
- struct safe_allocation *alloc;
- void *new_data;
-
- new_data = crypt_safe_alloc(size);
-
- if (new_data && data) {
-
- alloc = (struct safe_allocation *)
- ((char *)data - offsetof(struct safe_allocation, data));
-
- if (size > alloc->size)
- size = alloc->size;
-
- memcpy(new_data, data, size);
- }
-
- crypt_safe_free(data);
- return new_data;
-}
-
-/* Password reading helpers */
-static int untimed_read(int fd, char *pass, size_t maxlen)
-{
- ssize_t i;
-
- i = read(fd, pass, maxlen);
- if (i > 0) {
- pass[i-1] = '\0';
- i = 0;
- } else if (i == 0) { /* EOF */
- *pass = 0;
- i = -1;
- }
- return i;
-}
-
-static int timed_read(int fd, char *pass, size_t maxlen, long timeout)
-{
- struct timeval t;
- fd_set fds = {}; /* Just to avoid scan-build false report for FD_SET */
- int failed = -1;
-
- FD_ZERO(&fds);
- FD_SET(fd, &fds);
- t.tv_sec = timeout;
- t.tv_usec = 0;
-
- if (select(fd+1, &fds, NULL, NULL, &t) > 0)
- failed = untimed_read(fd, pass, maxlen);
-
- return failed;
-}
-
-static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
- long timeout)