summaryrefslogtreecommitdiff
path: root/crypt-common.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypt-common.c')
-rw-r--r--crypt-common.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/crypt-common.c b/crypt-common.c
new file mode 100644
index 0000000..ac54976
--- /dev/null
+++ b/crypt-common.c
@@ -0,0 +1,50 @@
+/* Copyright (C) 2018 Björn Esser <besser82@fedoraproject.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* Simple commonly used helper functions. */
+
+#include "crypt-port.h"
+
+/* Provide a safe way to copy strings with the guarantee src,
+ including its terminating '\0', will fit d_size bytes.
+ The trailing bytes of d_size will be filled with '\0'.
+ dst and src must not be NULL. Returns strlen (src). */
+size_t
+_crypt_strcpy_or_abort (void *dst, const size_t d_size,
+ const void *src)
+{
+ assert (dst != NULL);
+ assert (src != NULL);
+ const size_t s_size = strlen ((const char *) src);
+ assert (d_size >= s_size + 1);
+ memcpy (dst, src, s_size);
+ XCRYPT_SECURE_MEMSET ((char *) dst + s_size, d_size - s_size);
+ return s_size;
+}
+
+#if INCLUDE_XCRYPT_SECURE_MEMSET
+/* The best hope we without any other implementation to
+ securely wipe data stored in memory. */
+void
+_crypt_secure_memset (void *s, size_t len)
+{
+ volatile unsigned char *c = s;
+ while (len--)
+ *c++ = 0x00;
+}
+#endif