this unit test covers the following algorithms on crypt.c [1] [2]:
1 - MD5
5 - SHA256 (glibc >= 2.7, musl >= 0.9.8)
6 - SHA512 (glibc >= 2.7, musl >= 0.9.8)

the following algorithms are not tested [1] [2]:
2  - blowfish on musl >= 0.9.8
2a - blowfish on glibc but not in mainline glibc distribution

encrypt.c is also covered [3]

[1] http://man7.org/linux/man-pages/man3/crypt.3.html
[2] https://www.musl-libc.org/releases/musl-0.9.8.tar.gz 
(musl-0.9.8/src/crypt/crypt_r.c)
[3] http://man7.org/linux/man-pages/man3/encrypt.3.html

Signed-off-by: geraldo netto <geraldone...@gmail.com>
---
 modules/tests/Makefile |  2 +-
 tests/tst-crypt.c      | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 1 deletion(-)
 create mode 100644 tests/tst-crypt.c

diff --git a/modules/tests/Makefile b/modules/tests/Makefile
index b8348dd..3398e4a 100644
--- a/modules/tests/Makefile
+++ b/modules/tests/Makefile
@@ -87,7 +87,7 @@ tests := tst-pthread.so misc-ramdisk.so tst-vblk.so 
tst-bsd-evh.so \
        tst-ifaddrs.so tst-pthread-affinity-inherit.so tst-sem-timed-wait.so \
        tst-ttyname.so tst-pthread-barrier.so tst-feexcept.so tst-math.so \
        tst-sigaltstack.so tst-fread.so tst-tcp-cork.so tst-tcp-v6.so \
-       tst-calloc.so
+       tst-calloc.so tst-crypt.so
 
 #      libstatic-thread-variable.so tst-static-thread-variable.so \
 
diff --git a/tests/tst-crypt.c b/tests/tst-crypt.c
new file mode 100644
index 0000000..ffc5293
--- /dev/null
+++ b/tests/tst-crypt.c
@@ -0,0 +1,71 @@
+#define _XOPEN_SOURCE
+#include <stdio.h>
+#include <string.h>
+#include <crypt.h>
+
+char* concat_str(char* first_block, char* second_block) {
+       static char tmp[10];
+       memset(tmp, 0, sizeof(tmp));
+       strncat(tmp, first_block, sizeof(first_block));
+       strncat(tmp, second_block, sizeof(second_block));
+       return tmp;
+}
+
+int main(void) {
+       // http://man7.org/linux/man-pages/man3/crypt.3.html
+       char password[] = "Ab(d3";
+       char salt[] = "201802221723";
+       char result1[] = "20338EPKt5xtY";
+       char result2[] = "$1$Ab(d3$.SgUCw1AqIVAiXBaS6kzU.";
+       char result3[] = "$5$Ab(d3$00nkgDrj2dM68IbqFTZPJ.a3mgai49mY.Ezq5ey0xY0";
+       char result4[] = 
"$6$Ab(d3$TB6UIPV7sprvpcQh2esPr2/ye4FTp9lLft8yAj.2x/HcTXPwzGDxdK/tIF10DdVdV9Z2hhc3MaosUBS3fdueZ1";
+
+       // 20338EPKt5xtY
+       if (strcmp(result1, crypt(password, salt)) != 0) return -1;
+
+       // $1$Ab(d3$.SgUCw1AqIVAiXBaS6kzU.
+       if (strcmp(result2, crypt(password, concat_str((char*) "$1$", 
password))) != 0) return -2;
+
+       // $5$Ab(d3$00nkgDrj2dM68IbqFTZPJ.a3mgai49mY.Ezq5ey0xY0
+       if (strcmp(result3, crypt(password, concat_str((char*) "$5$", 
password))) != 0) return -3;
+
+       // 
$6$Ab(d3$TB6UIPV7sprvpcQh2esPr2/ye4FTp9lLft8yAj.2x/HcTXPwzGDxdK/tIF10DdVdV9Z2hhc3MaosUBS3fdueZ1
+       if (strcmp(result4, crypt(password, concat_str((char*) "$6$", 
password))) !=0) return -4;
+
+       // encrypt
+       // http://man7.org/linux/man-pages/man3/encrypt.3.html
+       char key[64];
+       char buf[64];
+       char txt[9];
+       int i, j;
+
+       for (i = 0; i < 8; i++) {
+               for (j = 0; j < 8; j++) {
+                       buf[i * 8 + j] = password[i] >> j & 1;
+               }
+
+               setkey(key);
+       }
+
+       encrypt(buf, 0);
+       for (i = 0; i < 8; i++) {
+               for (j = 0, txt[i] = '\0'; j < 8; j++) {
+                       txt[i] |= buf[i * 8 + j] << j;
+               }
+
+               txt[8] = '\0';
+       }
+
+       if (strcmp(password, txt) == 0) return -5;
+
+       encrypt(buf, 1);
+       for (i = 0; i < 8; i++) {
+               for (j = 0, txt[i] = '\0'; j < 8; j++) {
+                       txt[i] |= buf[i * 8 + j] << j;
+               }
+
+               txt[8] = '\0';
+       }
+
+       return (strcmp(password, txt) == 0) ? 0 : -6;
+}
\ No newline at end of file
-- 
2.7.4

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to