summaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'crypto')
-rw-r--r--crypto/cryptfs/Android.mk54
-rw-r--r--crypto/cryptfs/cryptfs.c74
-rw-r--r--crypto/cryptsettings/Android.mk14
-rw-r--r--crypto/cryptsettings/cryptsettings.c51
-rw-r--r--crypto/fs_mgr/Android.mk34
-rw-r--r--crypto/fs_mgr/fs_mgr.c611
-rw-r--r--crypto/fs_mgr/fs_mgr_main.c109
-rw-r--r--crypto/fs_mgr/fs_mgr_priv.h80
-rw-r--r--crypto/fs_mgr/include/fs_mgr.h27
-rw-r--r--crypto/ics/Android.mk26
-rw-r--r--crypto/ics/cryptfs.c707
-rw-r--r--crypto/ics/cryptfs.h85
-rw-r--r--crypto/jb/Android.mk15
-rw-r--r--crypto/jb/cryptfs.c1152
-rw-r--r--crypto/jb/cryptfs.h89
-rw-r--r--crypto/libcrypt_samsung/Android.mk11
-rw-r--r--crypto/libcrypt_samsung/include/libcrypt_samsung.h144
-rw-r--r--crypto/libcrypt_samsung/libcrypt_samsung.c65
18 files changed, 3348 insertions, 0 deletions
diff --git a/crypto/cryptfs/Android.mk b/crypto/cryptfs/Android.mk
new file mode 100644
index 000000000..62713fed7
--- /dev/null
+++ b/crypto/cryptfs/Android.mk
@@ -0,0 +1,54 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+ifeq ($(TW_INCLUDE_CRYPTO), true)
+LOCAL_SRC_FILES:= \
+ cryptfs.c
+
+LOCAL_CFLAGS:= -g -c -W -I../fs_mgr/include
+LOCAL_CFLAGS += -DTW_INCLUDE_CRYPTO
+LOCAL_CFLAGS += -DCRYPTO_FS_TYPE=\"$(TW_CRYPTO_FS_TYPE)\"
+LOCAL_CFLAGS += -DCRYPTO_REAL_BLKDEV=\"$(TW_CRYPTO_REAL_BLKDEV)\"
+LOCAL_CFLAGS += -DCRYPTO_MNT_POINT=\"$(TW_CRYPTO_MNT_POINT)\"
+LOCAL_CFLAGS += -DCRYPTO_FS_OPTIONS=\"$(TW_CRYPTO_FS_OPTIONS)\"
+LOCAL_CFLAGS += -DCRYPTO_FS_FLAGS=\"$(TW_CRYPTO_FS_FLAGS)\"
+LOCAL_CFLAGS += -DCRYPTO_KEY_LOC=\"$(TW_CRYPTO_KEY_LOC)\"
+ifdef TW_CRYPTO_SD_REAL_BLKDEV
+ LOCAL_CFLAGS += -DCRYPTO_SD_REAL_BLKDEV=\"$(TW_CRYPTO_SD_REAL_BLKDEV)\"
+ LOCAL_CFLAGS += -DCRYPTO_SD_FS_TYPE=\"$(TW_CRYPTO_SD_FS_TYPE)\"
+endif
+ifneq ($(TW_INTERNAL_STORAGE_PATH),)
+ LOCAL_CFLAGS += -DTW_INTERNAL_STORAGE_PATH=$(TW_INTERNAL_STORAGE_PATH)
+endif
+ifneq ($(TW_INTERNAL_STORAGE_MOUNT_POINT),)
+ LOCAL_CFLAGS += -DTW_INTERNAL_STORAGE_MOUNT_POINT=$(TW_INTERNAL_STORAGE_MOUNT_POINT)
+endif
+ifneq ($(TW_EXTERNAL_STORAGE_PATH),)
+ LOCAL_CFLAGS += -DTW_EXTERNAL_STORAGE_PATH=$(TW_EXTERNAL_STORAGE_PATH)
+endif
+ifneq ($(TW_EXTERNAL_STORAGE_MOUNT_POINT),)
+ LOCAL_CFLAGS += -DTW_EXTERNAL_STORAGE_MOUNT_POINT=$(TW_EXTERNAL_STORAGE_MOUNT_POINT)
+endif
+
+LOCAL_C_INCLUDES += system/extras/ext4_utils external/openssl/include
+LOCAL_MODULE:=cryptfs
+LOCAL_MODULE_TAGS:= eng
+LOCAL_SHARED_LIBRARIES += libc libcutils
+LOCAL_SHARED_LIBRARIES += libcrypto
+
+
+#LOCAL_LDFLAGS += -L$(TARGET_OUT_SHARED_LIBRARIES) -lsec_km -lsec_ecryptfs -ldl
+LOCAL_LDFLAGS += -ldl
+
+LOCAL_STATIC_LIBRARIES += libmtdutils
+LOCAL_STATIC_LIBRARIES += libminadbd libminzip libunz
+LOCAL_STATIC_LIBRARIES += libminuitwrp libpixelflinger_static libpng libjpegtwrp libgui
+LOCAL_SHARED_LIBRARIES += libz libc libstlport libcutils libstdc++ libmincrypt libext4_utils
+LOCAL_STATIC_LIBRARIES += libcrypt_samsung
+
+
+LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UI_LIB)
+#LOCAL_STATIC_LIBRARIES += libfs_mgrtwrp
+LOCAL_MODULE_CLASS := UTILITY_EXECUTABLES
+LOCAL_MODULE_PATH := $(PRODUCT_OUT)/utilities
+include $(BUILD_EXECUTABLE)
+endif \ No newline at end of file
diff --git a/crypto/cryptfs/cryptfs.c b/crypto/cryptfs/cryptfs.c
new file mode 100644
index 000000000..59e7added
--- /dev/null
+++ b/crypto/cryptfs/cryptfs.c
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2013 a3955269 all rights reversed, no rights reserved.
+ */
+
+#define TW_INCLUDE_CRYPTO_SAMSUNG
+#include "../ics/cryptfs.c"
+
+int dm_remove_device(const char *name)
+{
+ int r;
+ r = delete_crypto_blk_dev(name);
+ if(!r)
+ printf("crypto block device '%s' deleted.\n", name);
+ else
+ printf("deleting crypto block device '%s' failed. [%d - %s]\n", name, r, strerror(errno));
+ return r;
+}
+
+int ecryptfs_test(const char *pw)
+{
+ char pwbuf[256];
+ int r;
+
+ strcpy(pwbuf, pw);
+ // 0: building options without file encryption filtering.
+ // 1: building options with media files filtering.
+ // 2: building options with all new files filtering.
+ r = mount_ecryptfs_drive(pwbuf, "/emmc", "/emmc", 0);
+ printf("mount_ecryptfs_drive: %d\n", r);
+ r = mount("/dev/block/mmcblk1", "/emmc", "vfat", MS_RDONLY, "");
+ printf("mount: %d\n", r);
+
+ r = umount("/emmc");///dev/block/mmcblk1");
+ printf("umount: %d\n", r);
+
+ //r = unmount_ecryptfs_drive("/emmc");
+ //printf("unmount_ecryptfs_drive: %d\n", r);
+
+ return r;
+}
+
+int main(int argc, char* argv[])
+{
+ if(argc < 2)
+ {
+ printf("no args!\n");
+ return 1;
+ }
+
+ property_set("ro.crypto.state", "encrypted");
+
+ property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
+ property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
+ property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
+ property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
+ property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
+ property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
+
+#ifdef CRYPTO_SD_FS_TYPE
+ property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
+ property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
+ property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
+#endif
+
+ property_set("rw.km_fips_status", "ready");
+
+ delete_crypto_blk_dev("userdata");
+ delete_crypto_blk_dev("sdcard");
+ delete_crypto_blk_dev("emmc");
+
+ cryptfs_check_passwd(argv[1]);
+
+ return 0;
+};
diff --git a/crypto/cryptsettings/Android.mk b/crypto/cryptsettings/Android.mk
new file mode 100644
index 000000000..73b6b4a8b
--- /dev/null
+++ b/crypto/cryptsettings/Android.mk
@@ -0,0 +1,14 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+ifeq ($(TW_INCLUDE_CRYPTO), true)
+LOCAL_SRC_FILES:= \
+ cryptsettings.c
+LOCAL_CFLAGS:= -g -c -W
+LOCAL_MODULE:=cryptsettings
+LOCAL_MODULE_TAGS:= eng
+LOCAL_SHARED_LIBRARIES += libc libcutils
+LOCAL_STATIC_LIBRARIES += libfs_mgrtwrp
+LOCAL_MODULE_CLASS := UTILITY_EXECUTABLES
+LOCAL_MODULE_PATH := $(PRODUCT_OUT)/utilities
+include $(BUILD_EXECUTABLE)
+endif \ No newline at end of file
diff --git a/crypto/cryptsettings/cryptsettings.c b/crypto/cryptsettings/cryptsettings.c
new file mode 100644
index 000000000..79fad7198
--- /dev/null
+++ b/crypto/cryptsettings/cryptsettings.c
@@ -0,0 +1,51 @@
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include "../crypto/fs_mgr/include/fs_mgr.h"
+
+#include "cutils/properties.h"
+
+#ifndef PROPERTY_VALUE_MAX
+#define PROPERTY_VALUE_MAX 255
+#endif
+#ifndef FSTAB_PREFIX
+#define FSTAB_PREFIX "/fstab."
+#endif
+
+int main(void)
+{
+ char prop[PROPERTY_VALUE_MAX];
+ char key_loc[PROPERTY_VALUE_MAX];
+ char blk_dev[PROPERTY_VALUE_MAX];
+ char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
+
+ printf("This tool will gather the build flags needed for decryption support for TWRP.\n");
+ printf("This tool comes with no warranties whatsoever.\n");
+ printf("http://teamw.in\n\n");
+ property_get("ro.crypto.state", prop, "encrypted");
+ if (strcmp(prop, "encrypted") != 0)
+ printf("Your device is not encrypted, continuing anyway.\n\nTW_INCLUDE_CRYPTO := true\n");
+ property_get("ro.crypto.fs_type", prop, "ERROR");
+ printf("TW_CRYPTO_FS_TYPE := \"%s\"\n", prop);
+ property_get("ro.crypto.fs_real_blkdev", prop, "ERROR");
+ printf("TW_CRYPTO_REAL_BLKDEV := \"%s\"\n", prop);
+ property_get("ro.crypto.fs_mnt_point", prop, "ERROR");
+ printf("TW_CRYPTO_MNT_POINT := \"%s\"\n", prop);
+ property_get("ro.crypto.fs_options", prop, "ERROR");
+ printf("TW_CRYPTO_FS_OPTIONS := \"%s\"\n", prop);
+ property_get("ro.crypto.fs_flags", prop, "ERROR");
+ printf("TW_CRYPTO_FS_FLAGS := \"%s\"\n", prop);
+ property_get("ro.crypto.keyfile.userdata", prop, "footer");
+ printf("TW_CRYPTO_KEY_LOC := \"%s\"\n", prop);
+ printf("\n*** NEW FOR JELLY BEAN:\n");
+ strcpy(fstab_filename, FSTAB_PREFIX);
+ property_get("ro.hardware", fstab_filename + sizeof(FSTAB_PREFIX) - 1, "");
+ fs_mgr_get_crypt_info(fstab_filename, key_loc, blk_dev, sizeof(key_loc));
+ printf("fstab file location: '%s'\n\nTW_INCLUDE_JB_CRYPTO := true\n", fstab_filename);
+
+ return 0;
+}
diff --git a/crypto/fs_mgr/Android.mk b/crypto/fs_mgr/Android.mk
new file mode 100644
index 000000000..b2a341f36
--- /dev/null
+++ b/crypto/fs_mgr/Android.mk
@@ -0,0 +1,34 @@
+# Copyright 2011 The Android Open Source Project
+ifeq ($(TW_INCLUDE_JB_CRYPTO), true)
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= fs_mgr.c
+
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+
+LOCAL_MODULE:= libfs_mgrtwrp
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+
+include $(BUILD_STATIC_LIBRARY)
+
+
+
+#include $(CLEAR_VARS)
+
+#LOCAL_SRC_FILES:= fs_mgr_main.c
+
+#LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+
+#LOCAL_MODULE:= fs_mgr
+
+#LOCAL_MODULE_TAGS := optional
+#LOCAL_FORCE_STATIC_EXECUTABLE := true
+#LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)/sbin
+#LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED)
+
+#LOCAL_STATIC_LIBRARIES := libfs_mgr libcutils libc
+
+#include $(BUILD_EXECUTABLE)
+
+endif
diff --git a/crypto/fs_mgr/fs_mgr.c b/crypto/fs_mgr/fs_mgr.c
new file mode 100644
index 000000000..02f3ac57b
--- /dev/null
+++ b/crypto/fs_mgr/fs_mgr.c
@@ -0,0 +1,611 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* TO DO:
+ * 1. Re-direct fsck output to the kernel log?
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <libgen.h>
+#include <time.h>
+
+#include <private/android_filesystem_config.h>
+#include <cutils/partition_utils.h>
+#include <cutils/properties.h>
+
+#include "fs_mgr_priv.h"
+
+#define KEY_LOC_PROP "ro.crypto.keyfile.userdata"
+#define KEY_IN_FOOTER "footer"
+
+#define E2FSCK_BIN "/system/bin/e2fsck"
+
+struct flag_list {
+ const char *name;
+ unsigned flag;
+};
+
+static struct flag_list mount_flags[] = {
+ { "noatime", MS_NOATIME },
+ { "noexec", MS_NOEXEC },
+ { "nosuid", MS_NOSUID },
+ { "nodev", MS_NODEV },
+ { "nodiratime", MS_NODIRATIME },
+ { "ro", MS_RDONLY },
+ { "rw", 0 },
+ { "remount", MS_REMOUNT },
+ { "defaults", 0 },
+ { 0, 0 },
+};
+
+static struct flag_list fs_mgr_flags[] = {
+ { "wait", MF_WAIT },
+ { "check", MF_CHECK },
+ { "encryptable=",MF_CRYPT },
+ { "defaults", 0 },
+ { 0, 0 },
+};
+
+/*
+ * gettime() - returns the time in seconds of the system's monotonic clock or
+ * zero on error.
+ */
+static time_t gettime(void)
+{
+ struct timespec ts;
+ int ret;
+
+ ret = clock_gettime(CLOCK_MONOTONIC, &ts);
+ if (ret < 0) {
+ ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno));
+ return 0;
+ }
+
+ return ts.tv_sec;
+}
+
+static int wait_for_file(const char *filename, int timeout)
+{
+ struct stat info;
+ time_t timeout_time = gettime() + timeout;
+ int ret = -1;
+
+ while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0))
+ usleep(10000);
+
+ return ret;
+}
+
+static int parse_flags(char *flags, struct flag_list *fl, char **key_loc,
+ char *fs_options, int fs_options_len)
+{
+ int f = 0;
+ int i;
+ char *p;
+ char *savep;
+
+ /* initialize key_loc to null, if we find an MF_CRYPT flag,
+ * then we'll set key_loc to the proper value */
+ if (key_loc) {
+ *key_loc = NULL;
+ }
+ /* initialize fs_options to the null string */
+ if (fs_options && (fs_options_len > 0)) {
+ fs_options[0] = '\0';
+ }
+
+ p = strtok_r(flags, ",", &savep);
+ while (p) {
+ /* Look for the flag "p" in the flag list "fl"
+ * If not found, the loop exits with fl[i].name being null.
+ */
+ for (i = 0; fl[i].name; i++) {
+ if (!strncmp(p, fl[i].name, strlen(fl[i].name))) {
+ f |= fl[i].flag;
+ if ((fl[i].flag == MF_CRYPT) && key_loc) {
+ /* The encryptable flag is followed by an = and the
+ * location of the keys. Get it and return it.
+ */
+ *key_loc = strdup(strchr(p, '=') + 1);
+ }
+ break;
+ }
+ }
+
+ if (!fl[i].name) {
+ if (fs_options) {
+ /* It's not a known flag, so it must be a filesystem specific
+ * option. Add it to fs_options if it was passed in.
+ */
+ strlcat(fs_options, p, fs_options_len);
+ strlcat(fs_options, ",", fs_options_len);
+ } else {
+ /* fs_options was not passed in, so if the flag is unknown
+ * it's an error.
+ */
+ ERROR("Warning: unknown flag %s\n", p);
+ }
+ }
+ p = strtok_r(NULL, ",", &savep);
+ }
+
+out:
+ if (fs_options && fs_options[0]) {
+ /* remove the last trailing comma from the list of options */
+ fs_options[strlen(fs_options) - 1] = '\0';
+ }
+
+ return f;
+}
+
+/* Read a line of text till the next newline character.
+ * If no newline is found before the buffer is full, continue reading till a new line is seen,
+ * then return an empty buffer. This effectively ignores lines that are too long.
+ * On EOF, return null.
+ */
+static char *fs_mgr_getline(char *buf, int size, FILE *file)
+{
+ int cnt = 0;
+ int eof = 0;
+ int eol = 0;
+ int c;
+
+ if (size < 1) {
+ return NULL;
+ }
+
+ while (cnt < (size - 1)) {
+ c = getc(file);
+ if (c == EOF) {
+ eof = 1;
+ break;
+ }
+
+ *(buf + cnt) = c;
+ cnt++;
+
+ if (c == '\n') {
+ eol = 1;
+ break;
+ }
+ }
+
+ /* Null terminate what we've read */
+ *(buf + cnt) = '\0';
+
+ if (eof) {
+ if (cnt) {
+ return buf;
+ } else {
+ return NULL;
+ }
+ } else if (eol) {
+ return buf;
+ } else {
+ /* The line is too long. Read till a newline or EOF.
+ * If EOF, return null, if newline, return an empty buffer.
+ */
+ while(1) {
+ c = getc(file);
+ if (c == EOF) {
+ return NULL;
+ } else if (c == '\n') {
+ *buf = '\0';
+ return buf;
+ }
+ }
+ }
+}
+
+static struct fstab_rec *read_fstab(char *fstab_path)
+{
+ FILE *fstab_file;
+ int cnt, entries;
+ int len;
+ char line[256];
+ const char *delim = " \t";
+ char *save_ptr, *p;
+ struct fstab_rec *fstab;
+ char *key_loc;
+#define FS_OPTIONS_LEN 1024
+ char tmp_fs_options[FS_OPTIONS_LEN];
+
+ fstab_file = fopen(fstab_path, "r");
+ if (!fstab_file) {
+ ERROR("Cannot open file %s\n", fstab_path);
+ return 0;
+ }
+
+ entries = 0;
+ while (fs_mgr_getline(line, sizeof(line), fstab_file)) {
+ /* if the last character is a newline, shorten the string by 1 byte */
+ len = strlen(line);
+ if (line[len - 1] == '\n') {
+ line[len - 1] = '\0';
+ }
+ /* Skip any leading whitespace */
+ p = line;
+ while (isspace(*p)) {
+ p++;
+ }
+ /* ignore comments or empty lines */
+ if (*p == '#' || *p == '\0')
+ continue;
+ entries++;
+ }
+
+ if (!entries) {
+ ERROR("No entries found in fstab\n");
+ return 0;
+ }
+
+ fstab = calloc(entries + 1, sizeof(struct fstab_rec));
+
+ fseek(fstab_file, 0, SEEK_SET);
+
+ cnt = 0;
+ while (fs_mgr_getline(line, sizeof(line), fstab_file)) {
+ /* if the last character is a newline, shorten the string by 1 byte */
+ len = strlen(line);
+ if (line[len - 1] == '\n') {
+ line[len - 1] = '\0';
+ }
+
+ /* Skip any leading whitespace */
+ p = line;
+ while (isspace(*p)) {
+ p++;
+ }
+ /* ignore comments or empty lines */
+ if (*p == '#' || *p == '\0')
+ continue;
+
+ /* If a non-comment entry is greater than the size we allocated, give an
+ * error and quit. This can happen in the unlikely case the file changes
+ * between the two reads.
+ */
+ if (cnt >= entries) {
+ ERROR("Tried to process more entries than counted\n");
+ break;
+ }
+
+ if (!(p = strtok_r(line, delim, &save_ptr))) {
+ ERROR("Error parsing mount source\n");
+ return 0;
+ }
+ fstab[cnt].blk_dev = strdup(p);
+
+ if (!(p = strtok_r(NULL, delim, &save_ptr))) {
+ ERROR("Error parsing mnt_point\n");
+ return 0;
+ }
+ fstab[cnt].mnt_point = strdup(p);
+
+ if (!(p = strtok_r(NULL, delim, &save_ptr))) {
+ ERROR("Error parsing fs_type\n");
+ return 0;
+ }
+ fstab[cnt].type = strdup(p);
+
+ if (!(p = strtok_r(NULL, delim, &save_ptr))) {
+ ERROR("Error parsing mount_flags\n");
+ return 0;
+ }
+ tmp_fs_options[0] = '\0';
+ fstab[cnt].flags = parse_flags(p, mount_flags, 0, tmp_fs_options, FS_OPTIONS_LEN);
+
+ /* fs_options are optional */
+ if (tmp_fs_options[0]) {
+ fstab[cnt].fs_options = strdup(tmp_fs_options);
+ } else {
+ fstab[cnt].fs_options = NULL;
+ }
+
+ if (!(p = strtok_r(NULL, delim, &save_ptr))) {
+ ERROR("Error parsing fs_mgr_options\n");
+ return 0;
+ }
+ fstab[cnt].fs_mgr_flags = parse_flags(p, fs_mgr_flags, &key_loc, 0, 0);
+ fstab[cnt].key_loc = key_loc;
+
+ cnt++;
+ }
+ fclose(fstab_file);
+
+ return fstab;
+}
+
+static void free_fstab(struct fstab_rec *fstab)
+{
+ int i = 0;
+
+ while (fstab[i].blk_dev) {
+ /* Free the pointers return by strdup(3) */
+ free(fstab[i].blk_dev);
+ free(fstab[i].mnt_point);
+ free(fstab[i].type);
+ free(fstab[i].fs_options);
+ free(fstab[i].key_loc);
+
+ i++;
+ }
+
+ /* Free the actual fstab array created by calloc(3) */
+ free(fstab);
+}
+
+static void check_fs(char *blk_dev, char *type)
+{
+ pid_t pid;
+ int status;
+
+ /* Check for the types of filesystems we know how to check */
+ if (!strcmp(type, "ext2") || !strcmp(type, "ext3") || !strcmp(type, "ext4")) {
+ INFO("Running %s on %s\n", E2FSCK_BIN, blk_dev);
+ pid = fork();
+ if (pid > 0) {
+ /* Parent, wait for the child to return */
+ waitpid(pid, &status, 0);
+ } else if (pid == 0) {
+ /* child, run checker */
+ execlp(E2FSCK_BIN, E2FSCK_BIN, "-y", blk_dev, (char *)NULL);
+
+ /* Only gets here on error */
+ ERROR("Cannot run fs_mgr binary %s\n", E2FSCK_BIN);
+ } else {
+ /* No need to check for error in fork, we can't really handle it now */
+ ERROR("Fork failed trying to run %s\n", E2FSCK_BIN);
+ }
+ }
+
+ return;
+}
+
+static void remove_trailing_slashes(char *n)
+{
+ int len;
+
+ len = strlen(n) - 1;
+ while ((*(n + len) == '/') && len) {
+ *(n + len) = '\0';
+ len--;
+ }
+}
+
+static int fs_match(char *in1, char *in2)
+{
+ char *n1;
+ char *n2;
+ int ret;
+
+ n1 = strdup(in1);
+ n2 = strdup(in2);
+
+ remove_trailing_slashes(n1);
+ remove_trailing_slashes(n2);
+
+ ret = !strcmp(n1, n2);
+
+ free(n1);
+ free(n2);
+
+ return ret;
+}
+
+int fs_mgr_mount_all(char *fstab_file)
+{
+ int i = 0;
+ int encrypted = 0;
+ int ret = -1;
+ int mret;
+ struct fstab_rec *fstab = 0;
+
+ if (!(fstab = read_fstab(fstab_file))) {
+ return ret;
+ }
+
+ for (i = 0; fstab[i].blk_dev; i++) {
+ if (fstab[i].fs_mgr_flags & MF_WAIT) {
+ wait_for_file(fstab[i].blk_dev, WAIT_TIMEOUT);
+ }
+
+ if (fstab[i].fs_mgr_flags & MF_CHECK) {
+ check_fs(fstab[i].blk_dev, fstab[i].type);
+ }
+
+ mret = mount(fstab[i].blk_dev, fstab[i].mnt_point, fstab[i].type,
+ fstab[i].flags, fstab[i].fs_options);
+ if (!mret) {
+ /* Success! Go get the next one */
+ continue;
+ }
+
+ /* mount(2) returned an error, check if it's encrypted and deal with it */
+ if ((fstab[i].fs_mgr_flags & MF_CRYPT) && !partition_wiped(fstab[i].blk_dev)) {
+ /* Need to mount a tmpfs at this mountpoint for now, and set
+ * properties that vold will query later for decrypting
+ */
+ if (mount("tmpfs", fstab[i].mnt_point, "tmpfs",
+ MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS) < 0) {
+ ERROR("Cannot mount tmpfs filesystem for encrypted fs at %s\n",
+ fstab[i].mnt_point);
+ goto out;
+ }
+ encrypted = 1;
+ } else {
+ ERROR("Cannot mount filesystem on %s at %s\n",
+ fstab[i].blk_dev, fstab[i].mnt_point);
+ goto out;
+ }
+ }
+
+ if (encrypted) {
+ ret = 1;
+ } else {
+ ret = 0;
+ }
+
+out:
+ free_fstab(fstab);
+ return ret;
+}
+
+/* If tmp_mnt_point is non-null, mount the filesystem there. This is for the
+ * tmp mount we do to check the user password
+ */
+int fs_mgr_do_mount(char *fstab_file, char *n_name, char *n_blk_dev, char *tmp_mnt_point)
+{
+ int i = 0;
+ int ret = -1;
+ struct fstab_rec *fstab = 0;
+ char *m;
+
+ if (!(fstab = read_fstab(fstab_file))) {
+ return ret;
+ }
+
+ for (i = 0; fstab[i].blk_dev; i++) {
+ if (!fs_match(fstab[i].mnt_point, n_name)) {
+ continue;
+ }
+
+ /* We found our match */
+ /* First check the filesystem if requested */
+ if (fstab[i].fs_mgr_flags & MF_WAIT) {
+ wait_for_file(fstab[i].blk_dev, WAIT_TIMEOUT);
+ }
+
+ if ((fstab[i].fs_mgr_flags & MF_CHECK) && strcmp("ext4", fstab[i].type) != 0) {
+ check_fs(fstab[i].blk_dev, fstab[i].type);
+ }
+
+ /* Now mount it where requested */
+ if (tmp_mnt_point) {
+ m = tmp_mnt_point;
+ } else {
+ m = fstab[i].mnt_point;
+ }
+ if (mount(n_blk_dev, m, fstab[i].type,
+ fstab[i].flags, fstab[i].fs_options)) {
+ ERROR("Cannot mount filesystem on %s at %s\n",
+ n_blk_dev, m);
+ goto out;
+ } else {
+ ret = 0;
+ goto out;
+ }
+ }
+
+ /* We didn't find a match, say so and return an error */
+ ERROR("Cannot find mount point %s in fstab\n", fstab[i].mnt_point);
+
+out:
+ free_fstab(fstab);
+ return ret;
+}
+
+/*
+ * mount a tmpfs filesystem at the given point.
+ * return 0 on success, non-zero on failure.
+ */
+int fs_mgr_do_tmpfs_mount(char *n_name)
+{
+ int ret;
+
+ ret = mount("tmpfs", n_name, "tmpfs",
+ MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS);
+ if (ret < 0) {
+ ERROR("Cannot mount tmpfs filesystem at %s\n", n_name);
+ return -1;
+ }
+
+ /* Success */
+ return 0;
+}
+
+int fs_mgr_unmount_all(char *fstab_file)
+{
+ int i = 0;
+ int ret = 0;
+ struct fstab_rec *fstab = 0;
+
+ if (!(fstab = read_fstab(fstab_file))) {
+ return -1;
+ }
+
+ while (fstab[i].blk_dev) {
+ if (umount(fstab[i].mnt_point)) {
+ ERROR("Cannot unmount filesystem at %s\n", fstab[i].mnt_point);
+ ret = -1;
+ }
+ i++;
+ }
+
+ free_fstab(fstab);
+ return ret;
+}
+/*
+ * key_loc must be at least PROPERTY_VALUE_MAX bytes long
+ *
+ * real_blk_dev must be at least PROPERTY_VALUE_MAX bytes long
+ */
+int fs_mgr_get_crypt_info(char *fstab_file, char *key_loc, char *real_blk_dev, int size)
+{
+ int i = 0;
+ struct fstab_rec *fstab = 0;
+
+ if (!(fstab = read_fstab(fstab_file))) {
+ return -1;
+ }
+ /* Initialize return values to null strings */
+ if (key_loc) {
+ *key_loc = '\0';
+ }
+ if (real_blk_dev) {
+ *real_blk_dev = '\0';
+ }
+
+ /* Look for the encryptable partition to find the data */
+ for (i = 0; fstab[i].blk_dev; i++) {
+ if (!(fstab[i].fs_mgr_flags & MF_CRYPT)) {
+ continue;
+ }
+
+ /* We found a match */
+ if (key_loc) {
+ strlcpy(key_loc, fstab[i].key_loc, size);
+ }
+ if (real_blk_dev) {
+ strlcpy(real_blk_dev, fstab[i].blk_dev, size);
+ }
+ break;
+ }
+
+ free_fstab(fstab);
+ return 0;
+}
+
diff --git a/crypto/fs_mgr/fs_mgr_main.c b/crypto/fs_mgr/fs_mgr_main.c
new file mode 100644
index 000000000..81febf124
--- /dev/null
+++ b/crypto/fs_mgr/fs_mgr_main.c
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <libgen.h>
+#include "fs_mgr_priv.h"
+
+char *me = "";
+
+static void usage(void)
+{
+ ERROR("%s: usage: %s <-a | -n mnt_point blk_dev | -u> <fstab_file>\n", me, me);
+ exit(1);
+}
+
+/* Parse the command line. If an error is encountered, print an error message
+ * and exit the program, do not return to the caller.
+ * Return the number of argv[] entries consumed.
+ */
+static void parse_options(int argc, char *argv[], int *a_flag, int *u_flag, int *n_flag,
+ char **n_name, char **n_blk_dev)
+{
+ me = basename(strdup(argv[0]));
+
+ if (argc <= 1) {
+ usage();
+ }
+
+ if (!strcmp(argv[1], "-a")) {
+ if (argc != 3) {
+ usage();
+ }
+ *a_flag = 1;
+ }
+ if (!strcmp(argv[1], "-n")) {
+ if (argc != 5) {
+ usage();
+ }
+ *n_flag = 1;
+ *n_name = argv[2];
+ *n_blk_dev = argv[3];
+ }
+ if (!strcmp(argv[1], "-u")) {
+ if (argc != 3) {
+ usage();
+ }
+ *u_flag = 1;
+ }
+
+ /* If no flag is specified, it's an error */
+ if (!(*a_flag | *n_flag | *u_flag)) {
+ usage();
+ }
+
+ /* If more than one flag is specified, it's an error */
+ if ((*a_flag + *n_flag + *u_flag) > 1) {
+ usage();
+ }
+
+ return;
+}
+
+int main(int argc, char *argv[])
+{
+ int a_flag=0;
+ int u_flag=0;
+ int n_flag=0;
+ char *n_name;
+ char *n_blk_dev;
+ char *fstab;
+
+ klog_init();
+ klog_set_level(6);
+
+ parse_options(argc, argv, &a_flag, &u_flag, &n_flag, &n_name, &n_blk_dev);
+
+ /* The name of the fstab file is last, after the option */
+ fstab = argv[argc - 1];
+
+ if (a_flag) {
+ return fs_mgr_mount_all(fstab);
+ } else if (n_flag) {
+ return fs_mgr_do_mount(fstab, n_name, n_blk_dev, 0);
+ } else if (u_flag) {
+ return fs_mgr_unmount_all(fstab);
+ } else {
+ ERROR("%s: Internal error, unknown option\n", me);
+ exit(1);
+ }
+
+ /* Should not get here */
+ exit(1);
+}
+
diff --git a/crypto/fs_mgr/fs_mgr_priv.h b/crypto/fs_mgr/fs_mgr_priv.h
new file mode 100644
index 000000000..175fdabd2
--- /dev/null
+++ b/crypto/fs_mgr/fs_mgr_priv.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CORE_FS_MGR_PRIV_H
+#define __CORE_FS_MGR_PRIV_H
+
+#include <cutils/klog.h>
+#include <fs_mgr.h>
+
+#define INFO(x...) KLOG_INFO("fs_mgr", x)
+#define ERROR(x...) KLOG_ERROR("fs_mgr", x)
+
+#define CRYPTO_TMPFS_OPTIONS "size=128m,mode=0771,uid=1000,gid=1000"
+
+struct fstab_rec {
+ char *blk_dev;
+ char *mnt_point;
+ char *type;
+ unsigned long flags;
+ char *fs_options;
+ int fs_mgr_flags;
+ char *key_loc;
+};
+
+#define WAIT_TIMEOUT 5
+
+/* fstab has the following format:
+ *
+ * Any line starting with a # is a comment and ignored
+ *
+ * Any blank line is ignored
+ *
+ * All other lines must be in this format:
+ * <source> <mount_point> <fs_type> <mount_flags> <fs_options> <fs_mgr_options>
+ *
+ * <mount_flags> is a comma separated list of flags that can be passed to the
+ * mount command. The list includes noatime, nosuid, nodev, nodiratime,
+ * ro, rw, remount, defaults.
+ *
+ * <fs_options> is a comma separated list of options accepted by the filesystem being
+ * mounted. It is passed directly to mount without being parsed
+ *
+ * <fs_mgr_options> is a comma separated list of flags that control the operation of
+ * the fs_mgr program. The list includes "wait", which will wait till
+ * the <source> file exists, and "check", which requests that the fs_mgr
+ * run an fscheck program on the <source> before mounting the filesystem.
+ * If check is specifed on a read-only filesystem, it is ignored.
+ * Also, "encryptable" means that filesystem can be encrypted.
+ * The "encryptable" flag _MUST_ be followed by a : and a string which
+ * is the location of the encryption keys. I can either be a path
+ * to a file or partition which contains the keys, or the word "footer"
+ * which means the keys are in the last 16 Kbytes of the partition
+ * containing the filesystem.
+ *
+ * When the fs_mgr is requested to mount all filesystems, it will first mount all the
+ * filesystems that do _NOT_ specify check (including filesystems that are read-only and
+ * specify check, because check is ignored in that case) and then it will check and mount
+ * filesystem marked with check.
+ *
+ */
+
+#define MF_WAIT 0x1
+#define MF_CHECK 0x2
+#define MF_CRYPT 0x4
+
+#endif /* __CORE_FS_MGR_PRIV_H */
+
diff --git a/crypto/fs_mgr/include/fs_mgr.h b/crypto/fs_mgr/include/fs_mgr.h
new file mode 100644
index 000000000..76abb8332
--- /dev/null
+++ b/crypto/fs_mgr/include/fs_mgr.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CORE_FS_MGR_H
+#define __CORE_FS_MGR_H
+
+int fs_mgr_mount_all(char *fstab_file);
+int fs_mgr_do_mount(char *fstab_file, char *n_name, char *n_blk_dev, char *tmp_mnt_point);
+int fs_mgr_do_tmpfs_mount(char *n_name);
+int fs_mgr_unmount_all(char *fstab_file);
+int fs_mgr_get_crypt_info(char *fstab_file, char *key_loc, char *real_blk_dev, int size);
+
+#endif /* __CORE_FS_MGR_H */
+
diff --git a/crypto/ics/Android.mk b/crypto/ics/Android.mk
new file mode 100644
index 000000000..fb8449db2
--- /dev/null
+++ b/crypto/ics/Android.mk
@@ -0,0 +1,26 @@
+LOCAL_PATH := $(call my-dir)
+ifeq ($(TW_INCLUDE_CRYPTO), true)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libcryptfsics
+LOCAL_MODULE_TAGS := eng
+LOCAL_MODULES_TAGS = optional
+LOCAL_CFLAGS =
+LOCAL_CFLAGS += -DCRYPTO_FS_TYPE=\"$(TW_CRYPTO_FS_TYPE)\"
+ifeq ($(TW_INCLUDE_CRYPTO_SAMSUNG), true)
+ LOCAL_CFLAGS += -DTW_INCLUDE_CRYPTO_SAMSUNG=\"$(TW_INCLUDE_CRYPTO_SAMSUNG)\"
+ LOCAL_LDFLAGS += -ldl
+ LOCAL_STATIC_LIBRARIES += libcrypt_samsung
+endif
+ifneq ($(TW_INTERNAL_STORAGE_PATH),)
+ LOCAL_CFLAGS += -DTW_INTERNAL_STORAGE_PATH=$(TW_INTERNAL_STORAGE_PATH)
+endif
+ifneq ($(TW_EXTERNAL_STORAGE_PATH),)
+ LOCAL_CFLAGS += -DTW_EXTERNAL_STORAGE_PATH=$(TW_EXTERNAL_STORAGE_PATH)
+endif
+LOCAL_SRC_FILES = cryptfs.c
+LOCAL_C_INCLUDES += system/extras/ext4_utils external/openssl/include
+LOCAL_SHARED_LIBRARIES += libc liblog libcutils libcrypto
+
+include $(BUILD_SHARED_LIBRARY)
+endif
diff --git a/crypto/ics/cryptfs.c b/crypto/ics/cryptfs.c
new file mode 100644
index 000000000..193339ec9
--- /dev/null
+++ b/crypto/ics/cryptfs.c
@@ -0,0 +1,707 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* TO DO:
+ * 1. Perhaps keep several copies of the encrypted key, in case something
+ * goes horribly wrong?
+ *
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <linux/dm-ioctl.h>
+#include <libgen.h>
+#include <stdlib.h>
+#include <sys/param.h>
+#include <string.h>
+#include <sys/mount.h>
+#include <openssl/evp.h>
+#include <openssl/sha.h>
+#include <errno.h>
+#include <cutils/android_reboot.h>
+#include <ext4.h>
+#include <linux/kdev_t.h>
+#include "cryptfs.h"
+#define LOG_TAG "Cryptfs"
+#include "cutils/log.h"
+#include "cutils/properties.h"
+#include "hardware_legacy/power.h"
+//#include "VolumeManager.h"
+
+#define DM_CRYPT_BUF_SIZE 4096
+#define DATA_MNT_POINT "/data"
+
+#define HASH_COUNT 2000
+#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
+#define KEY_LEN_BYTES_SAMSUNG (sizeof(edk_t))
+#endif
+#define KEY_LEN_BYTES 16
+#define IV_LEN_BYTES 16
+
+#define KEY_LOC_PROP "ro.crypto.keyfile.userdata"
+#define KEY_IN_FOOTER "footer"
+
+#define EXT4_FS 1
+#define FAT_FS 2
+
+#ifndef EXPAND
+#define STRINGIFY(x) #x
+#define EXPAND(x) STRINGIFY(x)
+#endif
+
+char *me = "cryptfs";
+
+static char *saved_data_blkdev;
+static char *saved_mount_point;
+static int master_key_saved = 0;
+#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
+static int using_samsung_encryption = 0;
+//static edk_t saved_master_key;
+static unsigned char saved_master_key[KEY_LEN_BYTES_SAMSUNG];
+edk_payload_t edk_payload;
+#else
+static unsigned char saved_master_key[KEY_LEN_BYTES];
+#endif
+
+int cryptfs_setup_volume(const char *label, const char *real_blkdev, char *crypto_blkdev);
+
+
+static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
+{
+ memset(io, 0, dataSize);
+ io->data_size = dataSize;
+ io->data_start = sizeof(struct dm_ioctl);
+ io->version[0] = 4;
+ io->version[1] = 0;
+ io->version[2] = 0;
+ io->flags = flags;
+ if (name) {
+ strncpy(io->name, name, sizeof(io->name));
+ }
+}
+
+static unsigned int get_blkdev_size(int fd)
+{
+ unsigned int nr_sec;
+
+ if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
+ nr_sec = 0;
+ }
+
+ return nr_sec;
+}
+
+/* key or salt can be NULL, in which case just skip writing that value. Useful to
+ * update the failed mount count but not change the key.
+ */
+static int put_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
+ unsigned char *key, unsigned char *salt)
+{
+ // we don't need to update it...
+ return 0;
+}
+
+static int get_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
+ unsigned char *key, unsigned char *salt)
+{
+ int fd;
+ unsigned int nr_sec, cnt;
+ off64_t off;
+ int rc = -1;
+ char key_loc[PROPERTY_VALUE_MAX];
+ char *fname;
+ struct stat statbuf;
+
+ property_get(KEY_LOC_PROP, key_loc, KEY_IN_FOOTER);
+
+ if (!strcmp(key_loc, KEY_IN_FOOTER)) {
+ fname = real_blk_name;
+ if ( (fd = open(fname, O_RDONLY)) < 0) {
+ printf("Cannot open real block device %s\n", fname);
+ return -1;
+ }
+
+ if ( (nr_sec = get_blkdev_size(fd)) == 0) {
+ SLOGE("Cannot get size of block device %s\n", fname);
+ goto errout;
+ }
+
+ /* If it's an encrypted Android partition, the last 16 Kbytes contain the
+ * encryption info footer and key, and plenty of bytes to spare for future
+ * growth.
+ */
+ off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
+
+ if (lseek64(fd, off, SEEK_SET) == -1) {
+ printf("Cannot seek to real block device footer\n");
+ goto errout;
+ }
+ } else if (key_loc[0] == '/') {
+ fname = key_loc;
+ if ( (fd = open(fname, O_RDONLY)) < 0) {
+ printf("Cannot open footer file %s\n", fname);
+ return -1;
+ }
+
+ /* Make sure it's 16 Kbytes in length */
+ fstat(fd, &statbuf);
+ if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000
+#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
+ && statbuf.st_size != 0x8000
+#endif
+ )) {
+ printf("footer file %s is not the expected size!\n", fname);
+ goto errout;
+ }
+ } else {
+ printf("Unexpected value for" KEY_LOC_PROP "\n");
+ return -1;;
+ }
+
+ if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
+ printf("Cannot read real block device footer\n");
+ goto errout;
+ }
+
+ if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
+#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
+ if (crypt_ftr->magic != CRYPT_MNT_MAGIC_SAMSUNG) {
+ printf("Bad magic for real block device %s\n", fname);
+ goto errout;
+ } else {
+ printf("Using Samsung encryption.\n");
+ using_samsung_encryption = 1;
+ if ( (cnt = read(fd, &edk_payload, sizeof(edk_payload_t))) != sizeof(edk_payload_t)) {
+ printf("Cannot read EDK payload from real block device footer\n");
+ goto errout;
+ }
+ if (lseek64(fd, sizeof(__le32), SEEK_CUR) == -1) {
+ printf("Cannot seek past unknown data from real block device footer\n");
+ goto errout;
+ }
+ memcpy(key, &edk_payload, sizeof(edk_payload_t));
+ }
+#else
+ printf("Bad magic for real block device %s\n", fname);
+ goto errout;
+#endif
+ }
+
+ if (crypt_ftr->major_version != 1) {
+ printf("Cannot understand major version %d real block device footer\n",
+ crypt_ftr->major_version);
+ goto errout;
+ }
+
+ if (crypt_ftr->minor_version != 0) {
+ printf("Warning: crypto footer minor version %d, expected 0, continuing...\n",
+ crypt_ftr->minor_version);
+ }
+
+ if (crypt_ftr->ftr_size > sizeof(struct crypt_mnt_ftr)) {
+ /* the footer size is bigger than we expected.
+ * Skip to it's stated end so we can read the key.
+ */
+ if (lseek64(fd, crypt_ftr->ftr_size - sizeof(struct crypt_mnt_ftr), SEEK_CUR) == -1) {
+ printf("Cannot seek to start of key\n");
+ goto errout;
+ }
+ }
+
+ if (crypt_ftr->keysize > sizeof(saved_master_key)) {
+ printf("Keysize of %d bits not supported for real block device %s\n",
+ crypt_ftr->keysize * 8, fname);
+ goto errout;
+ }
+
+ if ( (cnt = read(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
+ printf("Cannot read key for real block device %s\n", fname);
+ goto errout;
+ }
+
+ if (lseek64(fd, KEY_TO_SALT_PADDING, SEEK_CUR) == -1) {
+ printf("Cannot seek to real block device salt\n");
+ goto errout;
+ }
+
+ if ( (cnt = read(fd, salt, SALT_LEN)) != SALT_LEN) {
+ printf("Cannot read salt for real block device %s\n", fname);
+ goto errout;
+ }
+
+ /* Success! */
+ rc = 0;
+
+errout:
+ close(fd);
+ return rc;
+}
+
+/* Convert a binary key of specified length into an ascii hex string equivalent,
+ * without the leading 0x and with null termination
+ */
+void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
+ char *master_key_ascii)
+{
+ unsigned int i, a;
+ unsigned char nibble;
+
+ for (i=0, a=0; i<keysize; i++, a+=2) {
+ /* For each byte, write out two ascii hex digits */
+ nibble = (master_key[i] >> 4) & 0xf;
+ master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
+
+ nibble = master_key[i] & 0xf;
+ master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
+ }
+
+ /* Add the null termination */
+ master_key_ascii[a] = '\0';
+
+}
+
+static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
+ const char *real_blk_name, char *crypto_blk_name, const char *name)
+{
+ char buffer[DM_CRYPT_BUF_SIZE];
+ char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
+ char *crypt_params;
+ struct dm_ioctl *io;
+ struct dm_target_spec *tgt;
+ unsigned int minor;
+ int fd;
+ int retval = -1;
+
+ if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
+ printf("Cannot open device-mapper\n");
+ goto errout;
+ }
+
+ io = (struct dm_ioctl *) buffer;
+
+ ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
+ if (ioctl(fd, DM_DEV_CREATE, io)) {
+ printf("Cannot create dm-crypt device\n");
+ goto errout;
+ }
+
+ /* Get the device status, in particular, the name of it's device file */
+ ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
+ if (ioctl(fd, DM_DEV_STATUS, io)) {
+ printf("Cannot retrieve dm-crypt device status\n");
+ goto errout;
+ }
+ minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
+ snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
+
+ /* Load the mapping table for this device */
+ tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
+
+ ioctl_init(io, 4096, name, 0);
+ io->target_count = 1;
+ tgt->status = 0;
+ tgt->sector_start = 0;
+ tgt->length = crypt_ftr->fs_size;
+ strcpy(tgt->target_type, "crypt");
+
+ crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
+ convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
+ sprintf(crypt_params, "%s %s 0 %s 0", crypt_ftr->crypto_type_name,
+ master_key_ascii, real_blk_name);
+ //printf("cryptsetup params: '%s'\n", crypt_params);
+ crypt_params += strlen(crypt_params) + 1;
+ crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
+ tgt->next = crypt_params - buffer;
+
+ if (ioctl(fd, DM_TABLE_LOAD, io)) {
+ printf("Cannot load dm-crypt mapping table.\n");
+ goto errout;
+ }
+
+ /* Resume this device to activate it */
+ ioctl_init(io, 4096, name, 0);
+
+ if (ioctl(fd, DM_DEV_SUSPEND, io)) {
+ printf("Cannot resume the dm-crypt device\n");
+ goto errout;
+ }
+
+ /* We made it here with no errors. Woot! */
+ retval = 0;
+
+errout:
+ close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
+
+ return retval;
+}
+
+static int delete_crypto_blk_dev(const char *name)
+{
+ int fd;
+ char buffer[DM_CRYPT_BUF_SIZE];
+ struct dm_ioctl *io;
+ int retval = -1;
+
+ if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
+ printf("Cannot open device-mapper\n");
+ goto errout;
+ }
+
+ io = (struct dm_ioctl *) buffer;
+
+ ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
+ if (ioctl(fd, DM_DEV_REMOVE, io)) {
+ printf("Cannot remove dm-crypt device\n");
+ goto errout;
+ }
+
+ /* We made it here with no errors. Woot! */
+ retval = 0;
+
+errout:
+ close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
+
+ return retval;
+
+}
+
+static void pbkdf2(char *passwd, unsigned char *salt, unsigned char *ikey)
+{
+ /* Turn the password into a key and IV that can decrypt the master key */
+ PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
+ HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
+}
+
+static int decrypt_master_key(char *passwd, unsigned char *salt,
+ unsigned char *encrypted_master_key,
+ unsigned char *decrypted_master_key)
+{
+#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
+ if (using_samsung_encryption) {
+ property_set("rw.km_fips_status", "ready");
+ return decrypt_EDK((dek_t*)decrypted_master_key, (edk_payload_t*)encrypted_master_key, passwd);
+ }
+#endif
+
+ unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
+ EVP_CIPHER_CTX d_ctx;
+ int decrypted_len, final_len;
+
+ /* Turn the password into a key and IV that can decrypt the master key */
+ pbkdf2(passwd, salt, ikey);
+
+ /* Initialize the decryption engine */
+ if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
+ return -1;
+ }
+ EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
+ /* Decrypt the master key */
+ if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
+ encrypted_master_key, KEY_LEN_BYTES)) {
+ return -1;
+ }
+ if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
+ return -1;
+ }
+
+ if (decrypted_len + final_len != KEY_LEN_BYTES) {
+ return -1;
+ } else {
+ return 0;
+ }
+}
+
+static int get_orig_mount_parms(
+ const char *mount_point, char *fs_type, char *real_blkdev,
+ unsigned long *mnt_flags, char *fs_options)
+{
+ char mount_point2[PROPERTY_VALUE_MAX];
+ char fs_flags[PROPERTY_VALUE_MAX];
+
+ property_get("ro.crypto.fs_type", fs_type, "");
+ property_get("ro.crypto.fs_real_blkdev", real_blkdev, "");
+ property_get("ro.crypto.fs_mnt_point", mount_point2, "");
+ property_get("ro.crypto.fs_options", fs_options, "");
+ property_get("ro.crypto.fs_flags", fs_flags, "");
+ *mnt_flags = strtol(fs_flags, 0, 0);
+
+ if (strcmp(mount_point, mount_point2)) {
+ /* Consistency check. These should match. If not, something odd happened. */
+ return -1;
+ }
+
+ return 0;
+}
+
+static int get_orig_mount_parms_sd(
+ const char *mount_point, char *fs_type, char *real_blkdev)
+{
+ char mount_point2[PROPERTY_VALUE_MAX];
+
+ property_get("ro.crypto.sd_fs_type", fs_type, "");
+ property_get("ro.crypto.sd_fs_real_blkdev", real_blkdev, "");
+ property_get("ro.crypto.sd_fs_mnt_point", mount_point2, "");
+
+ if (strcmp(mount_point, mount_point2)) {
+ /* Consistency check. These should match. If not, something odd happened. */
+ return -1;
+ }
+
+ return 0;
+}
+
+static int test_mount_encrypted_fs(
+ char *passwd, char *mount_point, char *label, char *crypto_blkdev)
+{
+ struct crypt_mnt_ftr crypt_ftr;
+ /* Allocate enough space for a 256 bit key, but we may use less */
+ unsigned char encrypted_master_key[256], decrypted_master_key[32];
+ unsigned char salt[SALT_LEN];
+ char real_blkdev[MAXPATHLEN];
+ char fs_type[PROPERTY_VALUE_MAX];
+ char fs_options[PROPERTY_VALUE_MAX];
+ char tmp_mount_point[MAXPATHLEN];
+ unsigned long mnt_flags;
+ unsigned int orig_failed_decrypt_count;
+ char encrypted_state[PROPERTY_VALUE_MAX];
+ int rc;
+
+ property_get("ro.crypto.state", encrypted_state, "");
+ if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
+ printf("encrypted fs already validated or not running with encryption, aborting %s\n", encrypted_state);
+ return -1;
+ }
+
+ if (get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) {
+ printf("Error reading original mount parms for mount point %s\n", mount_point);
+ return -1;
+ }
+
+ if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
+ printf("Error getting crypt footer and key\n");
+ return -1;
+ }
+
+ //printf("crypt_ftr->fs_size = %lld\n", crypt_ftr.fs_size);
+ orig_failed_decrypt_count = crypt_ftr.failed_decrypt_count;
+
+ if (! (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
+ decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key);
+ }
+
+ if (create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev,
+ crypto_blkdev, label)) {
+ printf("Error creating decrypted block device\n");
+ return -1;
+ }
+
+ /* If init detects an encrypted filesystme, it writes a file for each such
+ * encrypted fs into the tmpfs /data filesystem, and then the framework finds those
+ * files and passes that data to me */
+ /* Create a tmp mount point to try mounting the decryptd fs
+ * Since we're here, the mount_point should be a tmpfs filesystem, so make
+ * a directory in it to test mount the decrypted filesystem.
+ */
+ sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
+ mkdir(tmp_mount_point, 0755);
+ if ( mount(crypto_blkdev, tmp_mount_point, fs_type, MS_RDONLY, "") ) {
+ printf("Error temp mounting decrypted block device\n");
+ delete_crypto_blk_dev(label);
+ crypt_ftr.failed_decrypt_count++;
+ } else {
+ /* Success, so just umount and we'll mount it properly when we restart
+ * the framework.
+ */
+ umount(tmp_mount_point);
+ crypt_ftr.failed_decrypt_count = 0;
+ }
+
+ rmdir(tmp_mount_point);
+
+ if (crypt_ftr.failed_decrypt_count) {
+ /* We failed to mount the device, so return an error */
+ rc = crypt_ftr.failed_decrypt_count;
+
+ } else {
+ /* Woot! Success! Save the name of the crypto block device
+ * so we can mount it when restarting the framework.
+ */
+ property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
+
+ /* Also save a the master key so we can reencrypted the key
+ * the key when we want to change the password on it.
+ */
+ memcpy(saved_master_key, decrypted_master_key, sizeof(saved_master_key));
+ saved_data_blkdev = strdup(real_blkdev);
+ saved_mount_point = strdup(mount_point);
+ master_key_saved = 1;
+ rc = 0;
+ }
+
+ return rc;
+}
+
+static int test_mount_encrypted_fs_sd(
+ const char *passwd, const char *mount_point, const char *label)
+{
+ char real_blkdev[MAXPATHLEN];
+ char crypto_blkdev[MAXPATHLEN];
+ char tmp_mount_point[MAXPATHLEN];
+ char encrypted_state[PROPERTY_VALUE_MAX];
+ char fs_type[PROPERTY_VALUE_MAX];
+ int rc;
+
+ property_get("ro.crypto.state", encrypted_state, "");
+ if ( !master_key_saved || strcmp(encrypted_state, "encrypted") ) {
+ printf("encrypted fs not yet validated or not running with encryption, aborting\n");
+ return -1;
+ }
+
+ if (get_orig_mount_parms_sd(mount_point, fs_type, real_blkdev)) {
+ printf("Error reading original mount parms for mount point %s\n", mount_point);
+ return -1;
+ }
+
+ rc = cryptfs_setup_volume(label, real_blkdev, crypto_blkdev);
+ if(rc){
+ printf("Error setting up cryptfs volume %s\n", real_blkdev);
+ return -1;
+ }
+
+ sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
+ mkdir(tmp_mount_point, 0755);
+ if ( mount(crypto_blkdev, tmp_mount_point, fs_type, MS_RDONLY, "") ) {
+ printf("Error temp mounting decrypted block device\n");
+ delete_crypto_blk_dev(label);
+ } else {
+ /* Success, so just umount and we'll mount it properly when we restart
+ * the framework.
+ */
+ umount(tmp_mount_point);
+
+ property_set("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev);
+ }
+
+ rmdir(tmp_mount_point);
+
+ return rc;
+}
+
+/*
+ * Called by vold when it's asked to mount an encrypted, nonremovable volume.
+ * Setup a dm-crypt mapping, use the saved master key from
+ * setting up the /data mapping, and return the new device path.
+ */
+int cryptfs_setup_volume(const char *label, const char *real_blkdev, char *crypto_blkdev)
+{
+ struct crypt_mnt_ftr sd_crypt_ftr;
+ unsigned char key[256], salt[32];
+ struct stat statbuf;
+ int nr_sec, fd, rc;
+
+ /* Just want the footer, but gotta get it all */
+ get_crypt_ftr_and_key(saved_data_blkdev, &sd_crypt_ftr, key, salt);
+
+ /* Update the fs_size field to be the size of the volume */
+ fd = open(real_blkdev, O_RDONLY);
+ nr_sec = get_blkdev_size(fd);
+ close(fd);
+ if (nr_sec == 0) {
+ SLOGE("Cannot get size of volume %s\n", real_blkdev);
+ return -1;
+ }
+
+#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
+ if(using_samsung_encryption) {
+ if(!access("/efs/essiv", R_OK)){
+ strcpy(sd_crypt_ftr.crypto_type_name, "aes-cbc-plain:sha1");
+ }
+ else if(!access("/efs/cryptprop_essiv", R_OK)){
+ strcpy(sd_crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
+ }
+ }
+#endif
+
+ sd_crypt_ftr.fs_size = nr_sec;
+ rc = create_crypto_blk_dev(
+ &sd_crypt_ftr, saved_master_key, real_blkdev, crypto_blkdev, label);
+
+ stat(crypto_blkdev, &statbuf);
+
+ return rc;
+}
+
+int cryptfs_crypto_complete(void)
+{
+ return -1;
+}
+
+int cryptfs_check_passwd(const char *passwd)
+{
+ char pwbuf[256];
+ char crypto_blkdev_data[MAXPATHLEN];
+ int rc = -1;
+
+ strcpy(pwbuf, passwd);
+ rc = test_mount_encrypted_fs(pwbuf, DATA_MNT_POINT, "userdata", crypto_blkdev_data);
+
+#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
+ if(using_samsung_encryption) {
+
+ int rc2 = 1;
+#ifndef RECOVERY_SDCARD_ON_DATA
+#ifdef TW_INTERNAL_STORAGE_PATH
+ // internal storage for non data/media devices
+ if(!rc) {
+ strcpy(pwbuf, passwd);
+ rc2 = test_mount_encrypted_fs_sd(
+ pwbuf, EXPAND(TW_INTERNAL_STORAGE_PATH),
+ EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT));
+ }
+#endif
+#endif
+#ifdef TW_EXTERNAL_STORAGE_PATH
+ printf("Temp mounting /data\n");
+ // mount data so mount_ecryptfs_drive can access edk in /data/system/
+ rc2 = mount(crypto_blkdev_data, DATA_MNT_POINT, CRYPTO_FS_TYPE, MS_RDONLY, "");
+ // external sd
+ char decrypt_external[256], external_blkdev[256];
+ property_get("ro.crypto.external_encrypted", decrypt_external, "0");
+ // Mount the external storage as ecryptfs so that ecryptfs can act as a pass-through
+ if (!rc2 && strcmp(decrypt_external, "1") == 0) {
+ printf("Mounting external with ecryptfs...\n");
+ strcpy(pwbuf, passwd);
+ rc2 = mount_ecryptfs_drive(
+ pwbuf, EXPAND(TW_EXTERNAL_STORAGE_PATH),
+ EXPAND(TW_EXTERNAL_STORAGE_PATH), 0);
+ if (rc2 == 0)
+ property_set("ro.crypto.external_use_ecryptfs", "1");
+ else
+ property_set("ro.crypto.external_use_ecryptfs", "0");
+ } else {
+ printf("Unable to mount external storage with ecryptfs.\n");
+ umount(EXPAND(TW_EXTERNAL_STORAGE_PATH));
+ }
+ umount(DATA_MNT_POINT);
+ }
+#endif // #ifdef TW_EXTERNAL_STORAGE_PATH
+#endif // #ifdef TW_INCLUDE_CRYPTO_SAMSUNG
+ return rc;
+}
diff --git a/crypto/ics/cryptfs.h b/crypto/ics/cryptfs.h
new file mode 100644
index 000000000..eee43b3bc
--- /dev/null
+++ b/crypto/ics/cryptfs.h
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* This structure starts 16,384 bytes before the end of a hardware
+ * partition that is encrypted.
+ * Immediately following this structure is the encrypted key.
+ * The keysize field tells how long the key is, in bytes.
+ * Then there is 32 bytes of padding,
+ * Finally there is the salt used with the user password.
+ * The salt is fixed at 16 bytes long.
+ * Obviously, the filesystem does not include the last 16 kbytes
+ * of the partition.
+ */
+
+#ifndef __CRYPTFS_H__
+#define __CRYPTFS_H__
+
+#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
+#include "../libcrypt_samsung/include/libcrypt_samsung.h"
+#endif
+
+#define CRYPT_FOOTER_OFFSET 0x4000
+
+#define MAX_CRYPTO_TYPE_NAME_LEN 64
+
+#define SALT_LEN 16
+#define KEY_TO_SALT_PADDING 32
+
+/* definitions of flags in the structure below */
+#define CRYPT_MNT_KEY_UNENCRYPTED 0x1 /* The key for the partition is not encrypted. */
+#define CRYPT_ENCRYPTION_IN_PROGRESS 0x2 /* Set when starting encryption,
+ * clear when done before rebooting */
+
+#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
+#define CRYPT_MNT_MAGIC_SAMSUNG 0xD0B5B1C5
+#endif
+#define CRYPT_MNT_MAGIC 0xD0B5B1C4
+
+#define __le32 unsigned int
+#define __le16 unsigned short int
+
+#pragma pack(1)
+
+struct crypt_mnt_ftr {
+ __le32 magic; /* See above */
+ __le16 major_version;
+ __le16 minor_version;
+ __le32 ftr_size; /* in bytes, not including key following */
+ __le32 flags; /* See above */
+ __le32 keysize; /* in bytes */
+ __le32 spare1; /* ignored */
+ __le64 fs_size; /* Size of the encrypted fs, in 512 byte sectors */
+ __le32 failed_decrypt_count; /* count of # of failed attempts to decrypt and
+ mount, set to 0 on successful mount */
+ char crypto_type_name[MAX_CRYPTO_TYPE_NAME_LEN]; /* The type of encryption
+ needed to decrypt this
+ partition, null terminated */
+};
+
+#pragma pack()
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ int cryptfs_check_passwd(const char *pw);
+#ifdef __cplusplus
+}
+#endif
+
+#endif // __CRYPTFS_H__
+
diff --git a/crypto/jb/Android.mk b/crypto/jb/Android.mk
new file mode 100644
index 000000000..eebfcf0bd
--- /dev/null
+++ b/crypto/jb/Android.mk
@@ -0,0 +1,15 @@
+LOCAL_PATH := $(call my-dir)
+ifeq ($(TW_INCLUDE_JB_CRYPTO), true)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libcryptfsjb
+LOCAL_MODULE_TAGS := eng
+LOCAL_MODULES_TAGS = optional
+LOCAL_CFLAGS =
+LOCAL_SRC_FILES = cryptfs.c
+LOCAL_C_INCLUDES += system/extras/ext4_utils external/openssl/include
+LOCAL_SHARED_LIBRARIES += libc liblog libcutils libcrypto
+LOCAL_STATIC_LIBRARIES += libfs_mgrtwrp
+
+include $(BUILD_SHARED_LIBRARY)
+endif \ No newline at end of file
diff --git a/crypto/jb/cryptfs.c b/crypto/jb/cryptfs.c
new file mode 100644
index 000000000..be6c476b8
--- /dev/null
+++ b/crypto/jb/cryptfs.c
@@ -0,0 +1,1152 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* TO DO:
+ * 1. Perhaps keep several copies of the encrypted key, in case something
+ * goes horribly wrong?
+ *
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <linux/dm-ioctl.h>
+#include <libgen.h>
+#include <stdlib.h>
+#include <sys/param.h>
+#include <string.h>
+#include <sys/mount.h>
+#include <openssl/evp.h>
+#include <openssl/sha.h>
+#include <errno.h>
+#include <cutils/android_reboot.h>
+#include <ext4.h>
+#include <linux/kdev_t.h>
+#include "../fs_mgr/include/fs_mgr.h"
+#include "cryptfs.h"
+#define LOG_TAG "Cryptfs"
+#include "cutils/android_reboot.h"
+#include "cutils/log.h"
+#include "cutils/properties.h"
+#include "hardware_legacy/power.h"
+//#include "VolumeManager.h"
+
+#define DM_CRYPT_BUF_SIZE 4096
+#define DATA_MNT_POINT "/data"
+
+#define HASH_COUNT 2000
+#define KEY_LEN_BYTES 16
+#define IV_LEN_BYTES 16
+
+#define KEY_IN_FOOTER "footer"
+
+#define EXT4_FS 1
+#define FAT_FS 2
+
+char *me = "cryptfs";
+
+static unsigned char saved_master_key[KEY_LEN_BYTES];
+static char *saved_data_blkdev;
+static char *saved_mount_point;
+static int master_key_saved = 0;
+#define FSTAB_PREFIX "/fstab."
+static char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
+
+static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
+{
+ memset(io, 0, dataSize);
+ io->data_size = dataSize;
+ io->data_start = sizeof(struct dm_ioctl);
+ io->version[0] = 4;
+ io->version[1] = 0;
+ io->version[2] = 0;
+ io->flags = flags;
+ if (name) {
+ strncpy(io->name, name, sizeof(io->name));
+ }
+}
+
+static unsigned int get_fs_size(char *dev)
+{
+ int fd, block_size;
+ struct ext4_super_block sb;
+ off64_t len;
+
+ if ((fd = open(dev, O_RDONLY)) < 0) {
+ SLOGE("Cannot open device to get filesystem size ");
+ return 0;
+ }
+
+ if (lseek64(fd, 1024, SEEK_SET) < 0) {
+ SLOGE("Cannot seek to superblock");
+ return 0;
+ }
+
+ if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
+ SLOGE("Cannot read superblock");
+ return 0;
+ }
+
+ close(fd);
+
+ block_size = 1024 << sb.s_log_block_size;
+ /* compute length in bytes */
+ len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
+
+ /* return length in sectors */
+ return (unsigned int) (len / 512);
+}
+
+static unsigned int get_blkdev_size(int fd)
+{
+ unsigned int nr_sec;
+
+ if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
+ nr_sec = 0;
+ }
+
+ return nr_sec;
+}
+
+/* Get and cache the name of the fstab file so we don't
+ * keep talking over the socket to the property service.
+ */
+static char *get_fstab_filename(void)
+{
+ if (fstab_filename[0] == 0) {
+ strcpy(fstab_filename, FSTAB_PREFIX);
+ property_get("ro.hardware", fstab_filename + sizeof(FSTAB_PREFIX) - 1, "");
+ }
+
+ return fstab_filename;
+}
+
+/* key or salt can be NULL, in which case just skip writing that value. Useful to
+ * update the failed mount count but not change the key.
+ */
+static int put_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
+ unsigned char *key, unsigned char *salt)
+{
+ int fd;
+ unsigned int nr_sec, cnt;
+ off64_t off;
+ int rc = -1;
+ char *fname;
+ char key_loc[PROPERTY_VALUE_MAX];
+ struct stat statbuf;
+
+ fs_mgr_get_crypt_info(get_fstab_filename(), key_loc, 0, sizeof(key_loc));
+
+ if (!strcmp(key_loc, KEY_IN_FOOTER)) {
+ fname = real_blk_name;
+ if ( (fd = open(fname, O_RDWR)) < 0) {
+ SLOGE("Cannot open real block device %s\n", fname);
+ return -1;
+ }
+
+ if ( (nr_sec = get_blkdev_size(fd)) == 0) {
+ SLOGE("Cannot get size of block device %s\n", fname);
+ goto errout;
+ }
+
+ /* If it's an encrypted Android partition, the last 16 Kbytes contain the
+ * encryption info footer and key, and plenty of bytes to spare for future
+ * growth.
+ */
+ off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
+
+ if (lseek64(fd, off, SEEK_SET) == -1) {
+ SLOGE("Cannot seek to real block device footer\n");
+ goto errout;
+ }
+ } else if (key_loc[0] == '/') {
+ fname = key_loc;
+ if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) {
+ SLOGE("Cannot open footer file %s\n", fname);
+ return -1;
+ }
+ } else {
+ SLOGE("Unexpected value for crypto key location\n");
+ return -1;;
+ }
+
+ if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
+ SLOGE("Cannot write real block device footer\n");
+ goto errout;
+ }
+
+ if (key) {
+ if (crypt_ftr->keysize != KEY_LEN_BYTES) {
+ SLOGE("Keysize of %d bits not supported for real block device %s\n",
+ crypt_ftr->keysize*8, fname);
+ goto errout;
+ }
+
+ if ( (cnt = write(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
+ SLOGE("Cannot write key for real block device %s\n", fname);
+ goto errout;
+ }
+ }
+
+ if (salt) {
+ /* Compute the offset from the last write to the salt */
+ off = KEY_TO_SALT_PADDING;
+ if (! key)
+ off += crypt_ftr->keysize;
+
+ if (lseek64(fd, off, SEEK_CUR) == -1) {
+ SLOGE("Cannot seek to real block device salt \n");
+ goto errout;
+ }
+
+ if ( (cnt = write(fd, salt, SALT_LEN)) != SALT_LEN) {
+ SLOGE("Cannot write salt for real block device %s\n", fname);
+ goto errout;
+ }
+ }
+
+ fstat(fd, &statbuf);
+ /* If the keys are kept on a raw block device, do not try to truncate it. */
+ if (S_ISREG(statbuf.st_mode) && (key_loc[0] == '/')) {
+ if (ftruncate(fd, 0x4000)) {
+ SLOGE("Cannot set footer file size\n", fname);
+ goto errout;
+ }
+ }
+
+ /* Success! */
+ rc = 0;
+
+errout:
+ close(fd);
+ return rc;
+
+}
+
+static int get_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
+ unsigned char *key, unsigned char *salt)
+{
+ int fd;
+ unsigned int nr_sec, cnt;
+ off64_t off;
+ int rc = -1;
+ char key_loc[PROPERTY_VALUE_MAX];
+ char *fname;
+ struct stat statbuf;
+
+ fs_mgr_get_crypt_info(get_fstab_filename(), key_loc, 0, sizeof(key_loc));
+
+ if (!strcmp(key_loc, KEY_IN_FOOTER)) {
+ fname = real_blk_name;
+ if ( (fd = open(fname, O_RDONLY)) < 0) {
+ SLOGE("Cannot open real block device %s\n", fname);
+ return -1;
+ }
+
+ if ( (nr_sec = get_blkdev_size(fd)) == 0) {
+ SLOGE("Cannot get size of block device %s\n", fname);
+ goto errout;
+ }
+
+ /* If it's an encrypted Android partition, the last 16 Kbytes contain the
+ * encryption info footer and key, and plenty of bytes to spare for future
+ * growth.
+ */
+ off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
+
+ if (lseek64(fd, off, SEEK_SET) == -1) {
+ SLOGE("Cannot seek to real block device footer\n");
+ goto errout;
+ }
+ } else if (key_loc[0] == '/') {
+ fname = key_loc;
+ if ( (fd = open(fname, O_RDONLY)) < 0) {
+ SLOGE("Cannot open footer file %s\n", fname);
+ return -1;
+ }
+
+ /* Make sure it's 16 Kbytes in length */
+ fstat(fd, &statbuf);
+ if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
+ SLOGE("footer file %s is not the expected size!\n", fname);
+ goto errout;
+ }
+ } else {
+ SLOGE("Unexpected value for crypto key location\n");
+ return -1;;
+ }
+
+ if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
+ SLOGE("Cannot read real block device footer\n");
+ goto errout;
+ }
+
+ if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
+ SLOGE("Bad magic for real block device %s\n", fname);
+ goto errout;
+ }
+
+ if (crypt_ftr->major_version != 1) {
+ SLOGE("Cannot understand major version %d real block device footer\n",
+ crypt_ftr->major_version);
+ goto errout;
+ }
+
+ if (crypt_ftr->minor_version != 0) {
+ SLOGW("Warning: crypto footer minor version %d, expected 0, continuing...\n",
+ crypt_ftr->minor_version);
+ }
+
+ if (crypt_ftr->ftr_size > sizeof(struct crypt_mnt_ftr)) {
+ /* the footer size is bigger than we expected.
+ * Skip to it's stated end so we can read the key.
+ */
+ if (lseek64(fd, crypt_ftr->ftr_size - sizeof(struct crypt_mnt_ftr), SEEK_CUR) == -1) {
+ SLOGE("Cannot seek to start of key\n");
+ goto errout;
+ }
+ }
+
+ if (crypt_ftr->keysize != KEY_LEN_BYTES) {
+ SLOGE("Keysize of %d bits not supported for real block device %s\n",
+ crypt_ftr->keysize * 8, fname);
+ goto errout;
+ }
+
+ if ( (cnt = read(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
+ SLOGE("Cannot read key for real block device %s\n", fname);
+ goto errout;
+ }
+
+ if (lseek64(fd, KEY_TO_SALT_PADDING, SEEK_CUR) == -1) {
+ SLOGE("Cannot seek to real block device salt\n");
+ goto errout;
+ }
+
+ if ( (cnt = read(fd, salt, SALT_LEN)) != SALT_LEN) {
+ SLOGE("Cannot read salt for real block device %s\n", fname);
+ goto errout;
+ }
+
+ /* Success! */
+ rc = 0;
+
+errout:
+ close(fd);
+ return rc;
+}
+
+/* Convert a binary key of specified length into an ascii hex string equivalent,
+ * without the leading 0x and with null termination
+ */
+void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
+ char *master_key_ascii)
+{
+ unsigned int i, a;
+ unsigned char nibble;
+
+ for (i=0, a=0; i<keysize; i++, a+=2) {
+ /* For each byte, write out two ascii hex digits */
+ nibble = (master_key[i] >> 4) & 0xf;
+ master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
+
+ nibble = master_key[i] & 0xf;
+ master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
+ }
+
+ /* Add the null termination */
+ master_key_ascii[a] = '\0';
+
+}
+
+static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
+ char *real_blk_name, char *crypto_blk_name, const char *name)
+{
+ char buffer[DM_CRYPT_BUF_SIZE];
+ char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
+ char *crypt_params;
+ struct dm_ioctl *io;
+ struct dm_target_spec *tgt;
+ unsigned int minor;
+ int fd;
+ int retval = -1;
+
+ if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
+ SLOGE("Cannot open device-mapper\n");
+ goto errout;
+ }
+
+ io = (struct dm_ioctl *) buffer;
+
+ ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
+ if (ioctl(fd, DM_DEV_CREATE, io)) {
+ SLOGE("Cannot create dm-crypt device\n");
+ goto errout;
+ }
+
+ /* Get the device status, in particular, the name of it's device file */
+ ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
+ if (ioctl(fd, DM_DEV_STATUS, io)) {
+ SLOGE("Cannot retrieve dm-crypt device status\n");
+ goto errout;
+ }
+ minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
+ snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
+
+ /* Load the mapping table for this device */
+ tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
+
+ ioctl_init(io, 4096, name, 0);
+ io->target_count = 1;
+ tgt->status = 0;
+ tgt->sector_start = 0;
+ tgt->length = crypt_ftr->fs_size;
+ strcpy(tgt->target_type, "crypt");
+
+ crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
+ convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
+ sprintf(crypt_params, "%s %s 0 %s 0", crypt_ftr->crypto_type_name,
+ master_key_ascii, real_blk_name);
+ crypt_params += strlen(crypt_params) + 1;
+ crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
+ tgt->next = crypt_params - buffer;
+
+ if (ioctl(fd, DM_TABLE_LOAD, io)) {
+ SLOGE("Cannot load dm-crypt mapping table.\n");
+ goto errout;
+ }
+
+ /* Resume this device to activate it */
+ ioctl_init(io, 4096, name, 0);
+
+ if (ioctl(fd, DM_DEV_SUSPEND, io)) {
+ SLOGE("Cannot resume the dm-crypt device\n");
+ goto errout;
+ }
+
+ /* We made it here with no errors. Woot! */
+ retval = 0;
+
+errout:
+ close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
+
+ return retval;
+}
+
+static int delete_crypto_blk_dev(char *name)
+{
+ int fd;
+ char buffer[DM_CRYPT_BUF_SIZE];
+ struct dm_ioctl *io;
+ int retval = -1;
+
+ if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
+ SLOGE("Cannot open device-mapper\n");
+ goto errout;
+ }
+
+ io = (struct dm_ioctl *) buffer;
+
+ ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
+ if (ioctl(fd, DM_DEV_REMOVE, io)) {
+ SLOGE("Cannot remove dm-crypt device\n");
+ goto errout;
+ }
+
+ /* We made it here with no errors. Woot! */
+ retval = 0;
+
+errout:
+ close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
+
+ return retval;
+
+}
+
+static void pbkdf2(char *passwd, unsigned char *salt, unsigned char *ikey)
+{
+ /* Turn the password into a key and IV that can decrypt the master key */
+ PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
+ HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
+}
+
+static int encrypt_master_key(char *passwd, unsigned char *salt,
+ unsigned char *decrypted_master_key,
+ unsigned char *encrypted_master_key)
+{
+ unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
+ EVP_CIPHER_CTX e_ctx;
+ int encrypted_len, final_len;
+
+ /* Turn the password into a key and IV that can decrypt the master key */
+ pbkdf2(passwd, salt, ikey);
+
+ /* Initialize the decryption engine */
+ if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
+ SLOGE("EVP_EncryptInit failed\n");
+ return -1;
+ }
+ EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
+
+ /* Encrypt the master key */
+ if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
+ decrypted_master_key, KEY_LEN_BYTES)) {
+ SLOGE("EVP_EncryptUpdate failed\n");
+ return -1;
+ }
+ if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
+ SLOGE("EVP_EncryptFinal failed\n");
+ return -1;
+ }
+
+ if (encrypted_len + final_len != KEY_LEN_BYTES) {
+ SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
+ return -1;
+ } else {
+ return 0;
+ }
+}
+
+static int decrypt_master_key(char *passwd, unsigned char *salt,
+ unsigned char *encrypted_master_key,
+ unsigned char *decrypted_master_key)
+{
+ unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
+ EVP_CIPHER_CTX d_ctx;
+ int decrypted_len, final_len;
+
+ /* Turn the password into a key and IV that can decrypt the master key */
+ pbkdf2(passwd, salt, ikey);
+
+ /* Initialize the decryption engine */
+ if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
+ return -1;
+ }
+ EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
+ /* Decrypt the master key */
+ if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
+ encrypted_master_key, KEY_LEN_BYTES)) {
+ return -1;
+ }
+ if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
+ return -1;
+ }
+
+ if (decrypted_len + final_len != KEY_LEN_BYTES) {
+ return -1;
+ } else {
+ return 0;
+ }
+}
+
+static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt)
+{
+ int fd;
+ unsigned char key_buf[KEY_LEN_BYTES];
+ EVP_CIPHER_CTX e_ctx;
+ int encrypted_len, final_len;
+
+ /* Get some random bits for a key */
+ fd = open("/dev/urandom", O_RDONLY);
+ read(fd, key_buf, sizeof(key_buf));
+ read(fd, salt, SALT_LEN);
+ close(fd);
+
+ /* Now encrypt it with the password */
+ return encrypt_master_key(passwd, salt, key_buf, master_key);
+}
+
+static int wait_and_unmount(char *mountpoint)
+{
+ int i, rc;
+#define WAIT_UNMOUNT_COUNT 20
+
+ /* Now umount the tmpfs filesystem */
+ for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
+ if (umount(mountpoint)) {
+ if (errno == EINVAL) {
+ /* EINVAL is returned if the directory is not a mountpoint,
+ * i.e. there is no filesystem mounted there. So just get out.
+ */
+ break;
+ }
+ sleep(1);
+ i++;
+ } else {
+ break;
+ }
+ }
+
+ if (i < WAIT_UNMOUNT_COUNT) {
+ SLOGD("unmounting %s succeeded\n", mountpoint);
+ rc = 0;
+ } else {
+ SLOGE("unmounting %s failed\n", mountpoint);
+ rc = -1;
+ }
+
+ return rc;
+}
+
+#define DATA_PREP_TIMEOUT 100
+static int prep_data_fs(void)
+{
+ int i;
+
+ /* Do the prep of the /data filesystem */
+ property_set("vold.post_fs_data_done", "0");
+ property_set("vold.decrypt", "trigger_post_fs_data");
+ SLOGD("Just triggered post_fs_data\n");
+
+ /* Wait a max of 25 seconds, hopefully it takes much less */
+ for (i=0; i<DATA_PREP_TIMEOUT; i++) {
+ char p[PROPERTY_VALUE_MAX];
+
+ property_get("vold.post_fs_data_done", p, "0");
+ if (*p == '1') {
+ break;
+ } else {
+ usleep(250000);
+ }
+ }
+ if (i == DATA_PREP_TIMEOUT) {
+ /* Ugh, we failed to prep /data in time. Bail. */
+ return -1;
+ } else {
+ SLOGD("post_fs_data done\n");
+ return 0;
+ }
+}
+
+int cryptfs_restart(void)
+{
+ char fs_type[32];
+ char real_blkdev[MAXPATHLEN];
+ char crypto_blkdev[MAXPATHLEN];
+ char fs_options[256];
+ unsigned long mnt_flags;
+ struct stat statbuf;
+ int rc = -1, i;
+ static int restart_successful = 0;
+
+ /* Validate that it's OK to call this routine */
+ if (! master_key_saved) {
+ SLOGE("Encrypted filesystem not validated, aborting");
+ return -1;
+ }
+
+ if (restart_successful) {
+ SLOGE("System already restarted with encrypted disk, aborting");
+ return -1;
+ }
+
+ /* Here is where we shut down the framework. The init scripts
+ * start all services in one of three classes: core, main or late_start.
+ * On boot, we start core and main. Now, we stop main, but not core,
+ * as core includes vold and a few other really important things that
+ * we need to keep running. Once main has stopped, we should be able
+ * to umount the tmpfs /data, then mount the encrypted /data.
+ * We then restart the class main, and also the class late_start.
+ * At the moment, I've only put a few things in late_start that I know
+ * are not needed to bring up the framework, and that also cause problems
+ * with unmounting the tmpfs /data, but I hope to add add more services
+ * to the late_start class as we optimize this to decrease the delay
+ * till the user is asked for the password to the filesystem.
+ */
+
+ /* The init files are setup to stop the class main when vold.decrypt is
+ * set to trigger_reset_main.
+ */
+ property_set("vold.decrypt", "trigger_reset_main");
+ SLOGD("Just asked init to shut down class main\n");
+
+ /* Now that the framework is shutdown, we should be able to umount()
+ * the tmpfs filesystem, and mount the real one.
+ */
+
+ property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
+ if (strlen(crypto_blkdev) == 0) {
+ SLOGE("fs_crypto_blkdev not set\n");
+ return -1;
+ }
+
+ if (! (rc = wait_and_unmount(DATA_MNT_POINT)) ) {
+ /* If that succeeded, then mount the decrypted filesystem */
+ fs_mgr_do_mount(get_fstab_filename(), DATA_MNT_POINT, crypto_blkdev, 0);
+
+ property_set("vold.decrypt", "trigger_load_persist_props");
+ /* Create necessary paths on /data */
+ if (prep_data_fs()) {
+ return -1;
+ }
+
+ /* startup service classes main and late_start */
+ property_set("vold.decrypt", "trigger_restart_framework");
+ SLOGD("Just triggered restart_framework\n");
+
+ /* Give it a few moments to get started */
+ sleep(1);
+ }
+
+ if (rc == 0) {
+ restart_successful = 1;
+ }
+
+ return rc;
+}
+
+static int do_crypto_complete(char *mount_point)
+{
+ struct crypt_mnt_ftr crypt_ftr;
+ unsigned char encrypted_master_key[32];
+ unsigned char salt[SALT_LEN];
+ char real_blkdev[MAXPATHLEN];
+ char encrypted_state[PROPERTY_VALUE_MAX];
+ char key_loc[PROPERTY_VALUE_MAX];
+
+ property_get("ro.crypto.state", encrypted_state, "");
+ if (strcmp(encrypted_state, "encrypted") ) {
+ SLOGE("not running with encryption, aborting");
+ return 1;
+ }
+
+ fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev));
+
+ if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
+ fs_mgr_get_crypt_info(get_fstab_filename(), key_loc, 0, sizeof(key_loc));
+
+ /*
+ * Only report this error if key_loc is a file and it exists.
+ * If the device was never encrypted, and /data is not mountable for
+ * some reason, returning 1 should prevent the UI from presenting the
+ * a "enter password" screen, or worse, a "press button to wipe the
+ * device" screen.
+ */
+ if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
+ SLOGE("master key file does not exist, aborting");
+ return 1;
+ } else {
+ SLOGE("Error getting crypt footer and key\n");
+ return -1;
+ }
+ }
+
+ if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
+ SLOGE("Encryption process didn't finish successfully\n");
+ return -2; /* -2 is the clue to the UI that there is no usable data on the disk,
+ * and give the user an option to wipe the disk */
+ }
+
+ /* We passed the test! We shall diminish, and return to the west */
+ return 0;
+}
+
+static int test_mount_encrypted_fs(char *passwd, char *mount_point, char *label)
+{
+ struct crypt_mnt_ftr crypt_ftr;
+ /* Allocate enough space for a 256 bit key, but we may use less */
+ unsigned char encrypted_master_key[32], decrypted_master_key[32];
+ unsigned char salt[SALT_LEN];
+ char crypto_blkdev[MAXPATHLEN];
+ char real_blkdev[MAXPATHLEN];
+ char tmp_mount_point[64];
+ unsigned int orig_failed_decrypt_count;
+ char encrypted_state[PROPERTY_VALUE_MAX];
+ int rc;
+
+ property_get("ro.crypto.state", encrypted_state, "");
+ if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
+ SLOGE("encrypted fs already validated or not running with encryption, aborting");
+ return -1;
+ }
+
+ fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev));
+
+ if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
+ SLOGE("Error getting crypt footer and key\n");
+ return -1;
+ }
+
+ SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr.fs_size);
+ orig_failed_decrypt_count = crypt_ftr.failed_decrypt_count;
+
+ if (! (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
+ decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key);
+ }
+
+ if (create_crypto_blk_dev(&crypt_ftr, decrypted_master_key,
+ real_blkdev, crypto_blkdev, label)) {
+ SLOGE("Error creating decrypted block device\n");
+ return -1;
+ }
+
+ /* If init detects an encrypted filesystme, it writes a file for each such
+ * encrypted fs into the tmpfs /data filesystem, and then the framework finds those
+ * files and passes that data to me */
+ /* Create a tmp mount point to try mounting the decryptd fs
+ * Since we're here, the mount_point should be a tmpfs filesystem, so make
+ * a directory in it to test mount the decrypted filesystem.
+ */
+ sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
+ mkdir(tmp_mount_point, 0755);
+ if (fs_mgr_do_mount(get_fstab_filename(), DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
+ SLOGE("Error temp mounting decrypted block device\n");
+ delete_crypto_blk_dev(label);
+ crypt_ftr.failed_decrypt_count++;
+ } else {
+ /* Success, so just umount and we'll mount it properly when we restart
+ * the framework.
+ */
+ umount(tmp_mount_point);
+ crypt_ftr.failed_decrypt_count = 0;
+ }
+
+ if (orig_failed_decrypt_count != crypt_ftr.failed_decrypt_count) {
+ put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0);
+ }
+
+ if (crypt_ftr.failed_decrypt_count) {
+ /* We failed to mount the device, so return an error */
+ rc = crypt_ftr.failed_decrypt_count;
+
+ } else {
+ /* Woot! Success! Save the name of the crypto block device
+ * so we can mount it when restarting the framework.
+ */
+ property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
+
+ /* Also save a the master key so we can reencrypted the key
+ * the key when we want to change the password on it.
+ */
+ memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
+ saved_data_blkdev = strdup(real_blkdev);
+ saved_mount_point = strdup(mount_point);
+ master_key_saved = 1;
+ rc = 0;
+ }
+
+ return rc;
+}
+
+/* Called by vold when it wants to undo the crypto mapping of a volume it
+ * manages. This is usually in response to a factory reset, when we want
+ * to undo the crypto mapping so the volume is formatted in the clear.
+ */
+int cryptfs_revert_volume(const char *label)
+{
+ return delete_crypto_blk_dev((char *)label);
+}
+
+/*
+ * Called by vold when it's asked to mount an encrypted, nonremovable volume.
+ * Setup a dm-crypt mapping, use the saved master key from
+ * setting up the /data mapping, and return the new device path.
+ */
+int cryptfs_setup_volume(const char *label, int major, int minor,
+ char *crypto_sys_path, unsigned int max_path,
+ int *new_major, int *new_minor)
+{
+ char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN];
+ struct crypt_mnt_ftr sd_crypt_ftr;
+ unsigned char key[32], salt[32];
+ struct stat statbuf;
+ int nr_sec, fd;
+
+ sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor);
+
+ /* Just want the footer, but gotta get it all */
+ get_crypt_ftr_and_key(saved_data_blkdev, &sd_crypt_ftr, key, salt);
+
+ /* Update the fs_size field to be the size of the volume */
+ fd = open(real_blkdev, O_RDONLY);
+ nr_sec = get_blkdev_size(fd);
+ close(fd);
+ if (nr_sec == 0) {
+ SLOGE("Cannot get size of volume %s\n", real_blkdev);
+ return -1;
+ }
+
+ sd_crypt_ftr.fs_size = nr_sec;
+ create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev,
+ crypto_blkdev, label);
+
+ stat(crypto_blkdev, &statbuf);
+ *new_major = MAJOR(statbuf.st_rdev);
+ *new_minor = MINOR(statbuf.st_rdev);
+
+ /* Create path to sys entry for this block device */
+ snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1);
+
+ return 0;
+}
+
+int cryptfs_crypto_complete(void)
+{
+ return do_crypto_complete("/data");
+}
+
+int cryptfs_check_passwd(char *passwd)
+{
+ int rc = -1;
+
+ rc = test_mount_encrypted_fs(passwd, DATA_MNT_POINT, "userdata");
+
+ return rc;
+}
+
+int cryptfs_verify_passwd(char *passwd)
+{
+ struct crypt_mnt_ftr crypt_ftr;
+ /* Allocate enough space for a 256 bit key, but we may use less */
+ unsigned char encrypted_master_key[32], decrypted_master_key[32];
+ unsigned char salt[SALT_LEN];
+ char real_blkdev[MAXPATHLEN];
+ char encrypted_state[PROPERTY_VALUE_MAX];
+ int rc;
+
+ property_get("ro.crypto.state", encrypted_state, "");
+ if (strcmp(encrypted_state, "encrypted") ) {
+ SLOGE("device not encrypted, aborting");
+ return -2;
+ }
+
+ if (!master_key_saved) {
+ SLOGE("encrypted fs not yet mounted, aborting");
+ return -1;
+ }
+
+ if (!saved_mount_point) {
+ SLOGE("encrypted fs failed to save mount point, aborting");
+ return -1;
+ }
+
+ fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev));
+
+ if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
+ SLOGE("Error getting crypt footer and key\n");
+ return -1;
+ }
+
+ if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
+ /* If the device has no password, then just say the password is valid */
+ rc = 0;
+ } else {
+ decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key);
+ if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
+ /* They match, the password is correct */
+ rc = 0;
+ } else {
+ /* If incorrect, sleep for a bit to prevent dictionary attacks */
+ sleep(1);
+ rc = 1;
+ }
+ }
+
+ return rc;
+}
+
+/* Initialize a crypt_mnt_ftr structure. The keysize is
+ * defaulted to 16 bytes, and the filesystem size to 0.
+ * Presumably, at a minimum, the caller will update the
+ * filesystem size and crypto_type_name after calling this function.
+ */
+static void cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
+{
+ ftr->magic = CRYPT_MNT_MAGIC;
+ ftr->major_version = 1;
+ ftr->minor_version = 0;
+ ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
+ ftr->flags = 0;
+ ftr->keysize = KEY_LEN_BYTES;
+ ftr->spare1 = 0;
+ ftr->fs_size = 0;
+ ftr->failed_decrypt_count = 0;
+ ftr->crypto_type_name[0] = '\0';
+}
+
+static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
+{
+ char cmdline[256];
+ int rc = -1;
+
+ if (type == EXT4_FS) {
+ snprintf(cmdline, sizeof(cmdline), "/system/bin/make_ext4fs -a /data -l %lld %s",
+ size * 512, crypto_blkdev);
+ SLOGI("Making empty filesystem with command %s\n", cmdline);
+ } else if (type== FAT_FS) {
+ snprintf(cmdline, sizeof(cmdline), "/system/bin/newfs_msdos -F 32 -O android -c 8 -s %lld %s",
+ size, crypto_blkdev);
+ SLOGI("Making empty filesystem with command %s\n", cmdline);
+ } else {
+ SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
+ return -1;
+ }
+
+ if (system(cmdline)) {
+ SLOGE("Error creating empty filesystem on %s\n", crypto_blkdev);
+ } else {
+ SLOGD("Successfully created empty filesystem on %s\n", crypto_blkdev);
+ rc = 0;
+ }
+
+ return rc;
+}
+
+static inline int unix_read(int fd, void* buff, int len)
+{
+ int ret;
+ do { ret = read(fd, buff, len); } while (ret < 0 && errno == EINTR);
+ return ret;
+}
+
+static inline int unix_write(int fd, const void* buff, int len)
+{
+ int ret;
+ do { ret = write(fd, buff, len); } while (ret < 0 && errno == EINTR);
+ return ret;
+}
+
+#define CRYPT_INPLACE_BUFSIZE 4096
+#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / 512)
+static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev, off64_t size,
+ off64_t *size_already_done, off64_t tot_size)
+{
+ int realfd, cryptofd;
+ char *buf[CRYPT_INPLACE_BUFSIZE];
+ int rc = -1;
+ off64_t numblocks, i, remainder;
+ off64_t one_pct, cur_pct, new_pct;
+ off64_t blocks_already_done, tot_numblocks;
+
+ if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
+ SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
+ return -1;
+ }
+
+ if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
+ SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
+ close(realfd);
+ return -1;
+ }
+
+ /* This is pretty much a simple loop of reading 4K, and writing 4K.
+ * The size passed in is the number of 512 byte sectors in the filesystem.
+ * So compute the number of whole 4K blocks we should read/write,
+ * and the remainder.
+ */
+ numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
+ remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
+ tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
+ blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
+
+ SLOGE("Encrypting filesystem in place...");
+
+ one_pct = tot_numblocks / 100;
+ cur_pct = 0;
+ /* process the majority of the filesystem in blocks */
+ for (i=0; i<numblocks; i++) {
+ new_pct = (i + blocks_already_done) / one_pct;
+ if (new_pct > cur_pct) {
+ char buf[8];
+
+ cur_pct = new_pct;
+ snprintf(buf, sizeof(buf), "%lld", cur_pct);
+ property_set("vold.encrypt_progress", buf);
+ }
+ if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
+ SLOGE("Error reading real_blkdev %s for inplace encrypt\n", crypto_blkdev);
+ goto errout;
+ }
+ if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
+ SLOGE("Error writing crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
+ goto errout;
+ }
+ }
+
+ /* Do any remaining sectors */
+ for (i=0; i<remainder; i++) {
+ if (unix_read(realfd, buf, 512) <= 0) {
+ SLOGE("Error reading rival sectors from real_blkdev %s for inplace encrypt\n", crypto_blkdev);
+ goto errout;
+ }
+ if (unix_write(cryptofd, buf, 512) <= 0) {
+ SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
+ goto errout;
+ }
+ }
+
+ *size_already_done += size;
+ rc = 0;
+
+errout:
+ close(realfd);
+ close(cryptofd);
+
+ return rc;
+}
+
+#define CRYPTO_ENABLE_WIPE 1
+#define CRYPTO_ENABLE_INPLACE 2
+
+#define FRAMEWORK_BOOT_WAIT 60
+
+static inline int should_encrypt(struct volume_info *volume)
+{
+ return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) ==
+ (VOL_ENCRYPTABLE | VOL_NONREMOVABLE);
+}
+
+int cryptfs_enable(char *howarg, char *passwd)
+{
+ // Code removed because it needs other parts of vold that aren't needed for decryption
+ return -1;
+}
+
+int cryptfs_changepw(char *newpw)
+{
+ struct crypt_mnt_ftr crypt_ftr;
+ unsigned char encrypted_master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES];
+ unsigned char salt[SALT_LEN];
+ char real_blkdev[MAXPATHLEN];
+
+ /* This is only allowed after we've successfully decrypted the master key */
+ if (! master_key_saved) {
+ SLOGE("Key not saved, aborting");
+ return -1;
+ }
+
+ fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev));
+ if (strlen(real_blkdev) == 0) {
+ SLOGE("Can't find real blkdev");
+ return -1;
+ }
+
+ /* get key */
+ if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
+ SLOGE("Error getting crypt footer and key");
+ return -1;
+ }
+
+ encrypt_master_key(newpw, salt, saved_master_key, encrypted_master_key);
+
+ /* save the key */
+ put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt);
+
+ return 0;
+}
diff --git a/crypto/jb/cryptfs.h b/crypto/jb/cryptfs.h
new file mode 100644
index 000000000..1c1bc1aea
--- /dev/null
+++ b/crypto/jb/cryptfs.h
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* This structure starts 16,384 bytes before the end of a hardware
+ * partition that is encrypted.
+ * Immediately following this structure is the encrypted key.
+ * The keysize field tells how long the key is, in bytes.
+ * Then there is 32 bytes of padding,
+ * Finally there is the salt used with the user password.
+ * The salt is fixed at 16 bytes long.
+ * Obviously, the filesystem does not include the last 16 kbytes
+ * of the partition.
+ */
+
+#define CRYPT_FOOTER_OFFSET 0x4000
+
+#define MAX_CRYPTO_TYPE_NAME_LEN 64
+
+#define SALT_LEN 16
+#define KEY_TO_SALT_PADDING 32
+
+/* definitions of flags in the structure below */
+#define CRYPT_MNT_KEY_UNENCRYPTED 0x1 /* The key for the partition is not encrypted. */
+#define CRYPT_ENCRYPTION_IN_PROGRESS 0x2 /* Set when starting encryption,
+ * clear when done before rebooting */
+
+#define CRYPT_MNT_MAGIC 0xD0B5B1C4
+
+#define __le32 unsigned int
+#define __le16 unsigned short int
+
+struct crypt_mnt_ftr {
+ __le32 magic; /* See above */
+ __le16 major_version;
+ __le16 minor_version;
+ __le32 ftr_size; /* in bytes, not including key following */
+ __le32 flags; /* See above */
+ __le32 keysize; /* in bytes */
+ __le32 spare1; /* ignored */
+ __le64 fs_size; /* Size of the encrypted fs, in 512 byte sectors */
+ __le32 failed_decrypt_count; /* count of # of failed attempts to decrypt and
+ mount, set to 0 on successful mount */
+ unsigned char crypto_type_name[MAX_CRYPTO_TYPE_NAME_LEN]; /* The type of encryption
+ needed to decrypt this
+ partition, null terminated */
+};
+
+struct volume_info {
+ unsigned int size;
+ unsigned int flags;
+ struct crypt_mnt_ftr crypt_ftr;
+ char mnt_point[256];
+ char blk_dev[256];
+ char crypto_blkdev[256];
+ char label[256];
+};
+#define VOL_NONREMOVABLE 0x1
+#define VOL_ENCRYPTABLE 0x2
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ int cryptfs_crypto_complete(void);
+ int cryptfs_check_passwd(char *pw);
+ int cryptfs_verify_passwd(char *newpw);
+ int cryptfs_restart(void);
+ int cryptfs_enable(char *flag, char *passwd);
+ int cryptfs_changepw(char *newpw);
+ int cryptfs_setup_volume(const char *label, int major, int minor,
+ char *crypto_dev_path, unsigned int max_pathlen,
+ int *new_major, int *new_minor);
+ int cryptfs_revert_volume(const char *label);
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/crypto/libcrypt_samsung/Android.mk b/crypto/libcrypt_samsung/Android.mk
new file mode 100644
index 000000000..6e0e86903
--- /dev/null
+++ b/crypto/libcrypt_samsung/Android.mk
@@ -0,0 +1,11 @@
+LOCAL_PATH := $(call my-dir)
+
+ifneq ($(TARGET_SIMULATOR),true)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libcrypt_samsung
+LOCAL_SRC_FILES := $(LOCAL_MODULE).c
+LOCAL_MODULE_TAGS := eng
+include $(BUILD_STATIC_LIBRARY)
+
+endif
diff --git a/crypto/libcrypt_samsung/include/libcrypt_samsung.h b/crypto/libcrypt_samsung/include/libcrypt_samsung.h
new file mode 100644
index 000000000..2fb6f2c4f
--- /dev/null
+++ b/crypto/libcrypt_samsung/include/libcrypt_samsung.h
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2013 a3955269 all rights reversed, no rights reserved.
+ */
+
+#ifndef __LIBCRYPT_SAMSUNG_H__
+#define __LIBCRYPT_SAMSUNG_H__
+
+//////////////////////////////////////////////////////////////////////////////
+// Name Address Ordinal
+// ---- ------- -------
+// SECKM_AES_set_encrypt_key 000010D8
+// SECKM_AES_set_decrypt_key 00001464
+// SECKM_AES_encrypt 00001600
+// SECKM_AES_decrypt 00001A10
+// SECKM_aes_selftest 00001D94
+// verify_EDK 00001F7C
+// encrypt_dek 00001FC8
+// decrypt_EDK 000020D4
+// change_EDK 0000218C
+// generate_dek_salt 000022A4
+// create_EDK 000023A0
+// free_DEK 000024DC
+// alloc_DEK 000024F4
+// SECKM_HMAC_SHA256 00002500
+// SECKM_HMAC_SHA256_selftest 00002690
+// pbkdf 000026FC
+// pbkdf_selftest 00002898
+// _SECKM_PRNG_get16 00002958
+// SECKM_PRNG_get16 00002C48
+// _SECKM_PRNG_init 00002C54
+// SECKM_PRNG_selftest 00002F38
+// SECKM_PRNG_set_seed 00002FF0
+// SECKM_PRNG_init 00002FF8
+// SECKM_SHA256_Transform 00003004
+// SECKM_SHA256_Final 000031D8
+// SECKM_SHA256_Update 00003330
+// SECKM_SHA256_Init 000033FC
+// SECKM_SHA2_selftest 00003430
+// integrity_check 00003488
+// update_system_property 00003580
+// setsec_km_fips_status 00003630
+// _all_checks 00003684
+// get_fips_status 000036D4
+
+
+// EDK Payload is defined as:
+// Encrypted DEK – EDK itself
+// HMAC of EDK (32 bytes ???)
+// Salt 16 bytes
+
+#define EDK_MAGIC 0x1001e4b1
+
+#pragma pack(1)
+
+typedef struct {
+ unsigned int magic; // EDK_MAGIC
+ unsigned int flags; // 2
+ unsigned int zeros[6];
+} dek_t;
+
+typedef struct {
+ unsigned char data[32];
+} edk_t;
+
+
+// size 0x70 -> 112
+typedef struct {
+ dek_t dek;
+ edk_t edk;
+ unsigned char hmac[32];
+ unsigned char salt[16];
+} edk_payload_t;
+
+#pragma pack()
+
+//////////////////////////////////////////////////////////////////////////////
+
+int decrypt_EDK(
+ dek_t *dek, const edk_payload_t *edk, /*const*/ char *passwd);
+
+typedef int (*decrypt_EDK_t)(
+ dek_t *dek, const edk_payload_t *edk, /*const*/ char *passwd);
+
+
+int verify_EDK(const edk_payload_t *edk, const char *passwd);
+//change_EDK()
+//create_EDK()
+
+// internally just mallocs 32 bytes
+dek_t *alloc_DEK();
+void free_DEK(dek_t *dek);
+//encrypt_dek()
+//generate_dek_salt()
+
+//pbkdf(_buf_, "passwordPASSWORDpassword", 0x18, "saltSALTsaltSALTsaltSALTsaltSALTsalt", 0x24, 0x1000, 0x140);
+int pbkdf(
+ void *buf, void *pw, int pwlen, void *salt, int saltlen, int hashcnt,
+ int keylen);
+
+// getprop("rw.km_fips_status")
+// "ready, undefined, error_selftest, error_integrity"
+int get_fips_status();
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// libsec_ecryptfs.so (internally uses libkeyutils.so)
+//
+// Name Address Ordinal
+// ---- ------- -------
+// unmount_ecryptfs_drive 00000A78
+// mount_ecryptfs_drive 00000B48
+// fips_read_edk 00000E44
+// fips_save_edk 00000EA4
+// fips_create_edk 00000F20
+// fips_change_password 00001018
+// fips_delete_edk 00001124
+//
+
+// might depend on /data beeing mounted for reading /data/system/edk_p_sd
+//
+// filter
+// 0: building options without file encryption filtering.
+// 1: building options with media files filtering.
+// 2: building options with all new files filtering.
+
+int mount_ecryptfs_drive(
+ const char *passwd, const char *source, const char *target, int filter);
+
+typedef int (*mount_ecryptfs_drive_t)(
+ const char *passwd, const char *source, const char *target, int filter);
+
+// calls 2 times umount2(source, MNT_EXPIRE)
+int unmount_ecryptfs_drive(
+ const char *source);
+
+typedef int (*unmount_ecryptfs_drive_t)(
+ const char *source);
+
+//////////////////////////////////////////////////////////////////////////////
+
+#endif // #ifndef __LIBCRYPT_SAMSUNG_H__
+
+//////////////////////////////////////////////////////////////////////////////
+
diff --git a/crypto/libcrypt_samsung/libcrypt_samsung.c b/crypto/libcrypt_samsung/libcrypt_samsung.c
new file mode 100644
index 000000000..cd3a17804
--- /dev/null
+++ b/crypto/libcrypt_samsung/libcrypt_samsung.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2013 a3955269 all rights reversed, no rights reserved.
+ */
+
+//////////////////////////////////////////////////////////////////////////////
+
+#include <string.h>
+#include <stdio.h>
+#include <dlfcn.h>
+
+#include "include/libcrypt_samsung.h"
+
+//////////////////////////////////////////////////////////////////////////////
+
+int decrypt_EDK(
+ dek_t *dek, const edk_payload_t *edk, /*const*/ char *passwd)
+{
+ void *lib = dlopen("libsec_km.so", RTLD_LAZY);
+
+ if(!lib)
+ return -100;
+
+ int r = -101;
+ decrypt_EDK_t sym = (decrypt_EDK_t)dlsym(lib, "decrypt_EDK");
+ if(sym)
+ r = sym(dek, edk, passwd);
+
+ dlclose(lib);
+
+ return r;
+}
+
+int mount_ecryptfs_drive(
+ const char *passwd, const char *source, const char *target, int filter)
+{
+ void *lib = dlopen("libsec_ecryptfs.so", RTLD_LAZY);
+ if(!lib)
+ return -100;
+
+ int r = -101;
+ mount_ecryptfs_drive_t sym = (mount_ecryptfs_drive_t)dlsym(lib, "mount_ecryptfs_drive");
+ if(sym)
+ r = sym(passwd, source, target, filter);
+
+ dlclose(lib);
+
+ return r;
+}
+
+int unmount_ecryptfs_drive(
+ const char *source)
+{
+ void *lib = dlopen("libsec_ecryptfs.so", RTLD_LAZY);
+ if(!lib)
+ return -100;
+
+ int r = -101;
+ unmount_ecryptfs_drive_t sym = (unmount_ecryptfs_drive_t)dlsym(lib, "unmount_ecryptfs_drive");
+ if(sym)
+ r = sym(source);
+
+ dlclose(lib);
+
+ return r;
+} \ No newline at end of file