Bug 120075 - [libcrypt] Incompatible EOS of key in crypt(3)
Summary: [libcrypt] Incompatible EOS of key in crypt(3)
Status: Closed Overcome By Events
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 6.2-RELEASE
Hardware: Any Any
: Normal Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-01-28 07:30 UTC by Takumi Nakamura
Modified: 2017-08-27 04:17 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Takumi Nakamura 2008-01-28 07:30:00 UTC
In, crypt_des(key,salt)
0x80(not \0) in key is treated as end of string.

I expect the result below differs;

crypt("\x8B\xAE\xAE\xE4\x9a\x9F\x80\x82", "..")
and
crypt("\x8B\xAE\xAE\xE4\x9a\x9F", "..")

It is imcompatibility among other OSes, GNU/Linux(glibc), NetBSD, Darwin, &c.
There is no way for kludge to keep compatibility on FreeBSD.

Fix: 

quoted from crypt_des(), /src/secure/lib/libcrypt/crypt-des.c

	/*
	 * Copy the key, shifting each character up by one bit
	 * and padding with zeros.
	 */
	q = (u_char *)keybuf;
	while (q - (u_char *)keybuf - 8) {
		*q++ = *key << 1;
		if (*(q - 1))
			key++;
	}

for example with minimal modification;

	q = (u_char *)keybuf;
	while (q - (u_char *)keybuf - 8) {
		*q++ = *key << 1;
		if (*key)
			key++;
	}

(I guess it will be also bad code because it is not aliasing-safe)



Or, refer the specific limitation of EOS in the manpage.
(I don't hope)
How-To-Repeat: /*

FreeBSD 6.2-RELEASE-p9
crypt( 8B AE AE E4 9A 9F 80 82 )=..cSVY0lhu7BA
crypt( 8B AE AE E4 9A 9F 80 00 )=..cSVY0lhu7BA
crypt( 8B AE AE E4 9A 9F 00 82 )=..cSVY0lhu7BA
crypt( 8B AE AE E4 9A 9F 00 80 )=..cSVY0lhu7BA

GNU/Linux glibc x86
crypt( 8B AE AE E4 9A 9F 80 82 )=..riUAaAAAAAA
crypt( 8B AE AE E4 9A 9F 80 00 )=..cSVY0lhu7BA
crypt( 8B AE AE E4 9A 9F 00 82 )=..cSVY0lhu7BA
crypt( 8B AE AE E4 9A 9F 00 80 )=..cSVY0lhu7BA

Cygwin
crypt( 8B AE AE E4 9A 9F 80 82 )=..riUAaAAAAAA
crypt( 8B AE AE E4 9A 9F 80 00 )=..cSVY0lhu7BA
crypt( 8B AE AE E4 9A 9F 00 82 )=..cSVY0lhu7BA
crypt( 8B AE AE E4 9A 9F 00 80 )=..cSVY0lhu7BA

Darwin 8.11.0 (MacOSX PPC)
crypt( 8B AE AE E4 9A 9F 80 82 )=..riUAaAAAAAA
crypt( 8B AE AE E4 9A 9F 80 00 )=..cSVY0lhu7BA
crypt( 8B AE AE E4 9A 9F 00 82 )=..cSVY0lhu7BA
crypt( 8B AE AE E4 9A 9F 00 80 )=..cSVY0lhu7BA

*/

#include <stdio.h>
#include <unistd.h>

static void report(char const *key)
{
        int i;
        printf("crypt(");
        for (i = 0; i < 8; i++) printf(" %02X", key[i] & 0xFF);
        printf(" )=%s\n", crypt(key, ".."));
}

int main()
{
	report("\x8B\xAE\xAE\xE4\x9a\x9F\x80\x82");	/* Incompatible */
        report("\x8B\xAE\xAE\xE4\x9a\x9F\x80\x00");
        report("\x8B\xAE\xAE\xE4\x9a\x9F\x00\x82");
        report("\x8B\xAE\xAE\xE4\x9a\x9F\x00\x80");
        return 0;
}