Properly cleanup on interrupt in api-test.
[platform/upstream/cryptsetup.git] / tests / api-test.c
index dd57c22..15f95ed 100644 (file)
@@ -2,10 +2,12 @@
  * cryptsetup library API check functions
  *
  * Copyright (C) 2009-2012 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2009-2012, Milan Broz
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
- * version 2 as published by the Free Software Foundation.
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -25,6 +27,7 @@
 #include <linux/fs.h>
 #include <errno.h>
 #include <assert.h>
+#include <signal.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <libdevmapper.h>
@@ -82,6 +85,9 @@
 
 static int _debug   = 0;
 static int _verbose = 1;
+static int _fips_mode = 0;
+
+static int _quit = 0;
 
 static char global_log[4096];
 static int global_lines = 0;
@@ -112,6 +118,24 @@ static int device_size(const char *device, uint64_t *size)
        return r;
 }
 
+static int fips_mode(void)
+{
+       int fd;
+       char buf = 0;
+
+       fd = open("/proc/sys/crypto/fips_enabled", O_RDONLY);
+
+       if (fd < 0)
+               return 0;
+
+       if (read(fd, &buf, 1) != 1)
+               buf = '0';
+
+       close(fd);
+
+       return (buf == '1');
+}
+
 static int get_luks_offsets(int metadata_device,
                            size_t keylength,
                            unsigned int alignpayload_sec,
@@ -449,6 +473,11 @@ static int _setup(void)
 
        _system("modprobe dm-crypt", 0);
        _system("modprobe dm-verity", 0);
+
+       _fips_mode = fips_mode();
+       if (_debug)
+               printf("FIPS MODE: %d\n", _fips_mode);
+
        return 0;
 }
 
@@ -494,6 +523,12 @@ static void xlog(const char *msg, const char *tst, const char *func, int line, c
                else
                        printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
        }
+       if (_quit) {
+               if (_verbose)
+                       printf("Interrupted by a signal.\n");
+               _cleanup();
+               exit(-1);
+       }
 }
 
 /* crypt_device context must be "cd" to parse error properly here */
@@ -698,7 +733,7 @@ static void AddDevicePlain(void)
                EQ_(r_size, size);
 
        // size overlaps
-       FAIL_(crypt_resize(cd, CDEVICE_1, ULLONG_MAX),"Backing device is too small");
+       FAIL_(crypt_resize(cd, CDEVICE_1, (uint64_t)-1),"Backing device is too small");
        FAIL_(crypt_resize(cd, CDEVICE_1, (size>>SECTOR_SHIFT)+1),"crypt device overlaps backing device");
 
        // resize ok
@@ -758,14 +793,16 @@ static void AddDevicePlain(void)
        EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
 
        // retrieve volume key check
-       memset(key2, 0, key_size);
-       key_size--;
-       // small buffer
-       FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
-       key_size++;
-       OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
-
-       OK_(memcmp(key, key2, key_size));
+       if (!_fips_mode) {
+               memset(key2, 0, key_size);
+               key_size--;
+               // small buffer
+               FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
+               key_size++;
+               OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
+
+               OK_(memcmp(key, key2, key_size));
+       }
        OK_(strcmp(cipher, crypt_get_cipher(cd)));
        OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
        EQ_((int)key_size, crypt_get_volume_key_size(cd));
@@ -897,16 +934,18 @@ static void UseLuksDevice(void)
        EQ_((int)key_size, crypt_get_volume_key_size(cd));
        EQ_(1032, crypt_get_data_offset(cd));
 
-       EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
-       OK_(crypt_volume_key_verify(cd, key, key_size));
-       OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
-       OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
-       EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
-       OK_(crypt_deactivate(cd, CDEVICE_1));
-
-       key[1] = ~key[1];
-       FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
-       FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
+       if (!_fips_mode) {
+               EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
+               OK_(crypt_volume_key_verify(cd, key, key_size));
+               OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
+               OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
+               EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+               OK_(crypt_deactivate(cd, CDEVICE_1));
+
+               key[1] = ~key[1];
+               FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
+               FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
+       }
        crypt_free(cd);
 }
 
@@ -1128,10 +1167,12 @@ static void AddDeviceLuks(void)
        EQ_(7, crypt_activate_by_passphrase(cd, NULL, 7, passphrase2, strlen(passphrase2), 0));
        EQ_(6, crypt_keyslot_change_by_passphrase(cd, CRYPT_ANY_SLOT, 6, passphrase2, strlen(passphrase2), passphrase, strlen(passphrase)));
 
-       EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
-       OK_(crypt_volume_key_verify(cd, key2, key_size));
+       if (!_fips_mode) {
+               EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
+               OK_(crypt_volume_key_verify(cd, key2, key_size));
 
-       OK_(memcmp(key, key2, key_size));
+               OK_(memcmp(key, key2, key_size));
+       }
        OK_(strcmp(cipher, crypt_get_cipher(cd)));
        OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
        EQ_((int)key_size, crypt_get_volume_key_size(cd));
@@ -1741,12 +1782,14 @@ static void TcryptTest(void)
        EQ_(256, crypt_get_data_offset(cd));
 
        memset(key, 0, key_size);
-       key_size--;
-       // small buffer
-       FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0), "small buffer");
-       key_size++;
-       OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0));
-       OK_(memcmp(key, key_def, key_size));
+       if (!_fips_mode) {
+               key_size--;
+               // small buffer
+               FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0), "small buffer");
+               key_size++;
+               OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0));
+               OK_(memcmp(key, key_def, key_size));
+       }
 
        reset_log();
        crypt_set_log_callback(cd, &new_log, NULL);
@@ -1815,8 +1858,14 @@ static void NonFIPSAlg(void)
        crypt_free(cd);
 }
 
+static void int_handler(int sig __attribute__((__unused__)))
+{
+       _quit++;
+}
+
 int main(int argc, char *argv[])
 {
+       struct sigaction sa = { .sa_handler = int_handler };
        int i;
 
        if (getuid() != 0) {
@@ -1831,6 +1880,10 @@ int main(int argc, char *argv[])
                        _debug = _verbose = 1;
        }
 
+       /* Handle interrupt properly */
+       sigaction(SIGINT, &sa, NULL);
+       sigaction(SIGTERM, &sa, NULL);
+
        _cleanup();
        if (_setup())
                goto out;