summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Libraries/CMakeLists.txt1
-rw-r--r--Libraries/LibC/unistd.h8
-rw-r--r--Libraries/LibCrypt/CMakeLists.txt6
-rw-r--r--Libraries/LibCrypt/crypt.cpp78
4 files changed, 93 insertions, 0 deletions
diff --git a/Libraries/CMakeLists.txt b/Libraries/CMakeLists.txt
index 41e3fedb56..1d460d9f84 100644
--- a/Libraries/CMakeLists.txt
+++ b/Libraries/CMakeLists.txt
@@ -1,6 +1,7 @@
add_subdirectory(LibAudio)
add_subdirectory(LibC)
add_subdirectory(LibCore)
+add_subdirectory(LibCrypt)
add_subdirectory(LibCrypto)
add_subdirectory(LibDebug)
add_subdirectory(LibDesktop)
diff --git a/Libraries/LibC/unistd.h b/Libraries/LibC/unistd.h
index ab6542b43f..93225ebb41 100644
--- a/Libraries/LibC/unistd.h
+++ b/Libraries/LibC/unistd.h
@@ -179,4 +179,12 @@ enum {
};
long sysconf(int name);
+struct crypt_data {
+ int initialized;
+ char result[65];
+};
+
+char* crypt(const char* key, const char* salt);
+char* crypt_r(const char* key, const char* salt, struct crypt_data* data);
+
__END_DECLS
diff --git a/Libraries/LibCrypt/CMakeLists.txt b/Libraries/LibCrypt/CMakeLists.txt
new file mode 100644
index 0000000000..f24d3f318a
--- /dev/null
+++ b/Libraries/LibCrypt/CMakeLists.txt
@@ -0,0 +1,6 @@
+set(SOURCES
+ crypt.cpp
+)
+
+serenity_libc(LibCrypt crypt)
+target_link_libraries(LibCrypt LibC LibCrypto)
diff --git a/Libraries/LibCrypt/crypt.cpp b/Libraries/LibCrypt/crypt.cpp
new file mode 100644
index 0000000000..3431383fee
--- /dev/null
+++ b/Libraries/LibCrypt/crypt.cpp
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2020, Peter Elliott <pelliott@ualberta.ca>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
+ */
+#include <AK/Base64.h>
+#include <AK/Types.h>
+#include <LibCrypto/Hash/SHA2.h>
+#include <string.h>
+#include <unistd.h>
+
+extern "C" {
+
+static struct crypt_data crypt_data;
+
+char* crypt(const char* key, const char* salt)
+{
+ crypt_data.initialized = true;
+ return crypt_r(key, salt, &crypt_data);
+}
+
+static constexpr size_t crypt_salt_max = 16;
+static constexpr size_t sha_string_length = 44;
+
+char* crypt_r(const char* key, const char* salt, struct crypt_data* data)
+{
+ if (!data->initialized) {
+ errno = EINVAL;
+ return nullptr;
+ }
+
+ if (salt[0] == '$') {
+ if (salt[1] == '5') {
+ const char* salt_value = salt + 3;
+ size_t salt_len = min(strcspn(salt_value, "$"), crypt_salt_max);
+ size_t header_len = salt_len + 3;
+
+ strncpy(data->result, salt, header_len);
+ data->result[header_len] = '$';
+
+ Crypto::Hash::SHA256 sha;
+ sha.update(key);
+ sha.update((const u8*)salt_value, salt_len);
+
+ auto digest = sha.digest();
+ auto string = encode_base64(ReadonlyBytes(digest.immutable_data(), digest.data_length()));
+
+ strncpy(data->result + header_len + 1, string.characters(), sha_string_length);
+
+ return data->result;
+ }
+ }
+
+ // DES crypt is not available.
+ errno = EINVAL;
+ return nullptr;
+}
+}