2 * authusekey - decode a key from ascii and use it
8 #include "ntp_string.h"
9 #include "ntp_stdlib.h"
12 * Types of ascii representations for keys. "Standard" means a 64 bit
13 * hex number in NBS format, i.e. with the low order bit of each byte
14 * a parity bit. "NTP" means a 64 bit key in NTP format, with the
15 * high order bit of each byte a parity bit. "Ascii" means a 1-to-8
16 * character string whose ascii representation is used as the key.
19 #define KEY_TYPE_STD 1
20 #define KEY_TYPE_NTP 2
21 #define KEY_TYPE_ASCII 3
23 #define STD_PARITY_BITS ((unsigned)0x01010101)
27 #define KEY_TYPE_MD5 4
41 static const char *hex = "0123456789abcdef";
47 len = strlen((const char *)cp);
55 if (len != 16) /* Lazy. Should define constant */
62 for (i = 0; i < 16; i++) {
65 xdigit = strchr(hex, isupper(*cp) ? tolower(*cp) : *cp);
70 key[i>>3] |= (u_int32)(xdigit - hex) & 0xf;
74 * If this is an NTP format key, put it into NBS format
76 if (keytype == KEY_TYPE_NTP) {
77 for (i = 0; i < 2; i++)
78 key[i] = ((key[i] << 1) & ~STD_PARITY_BITS)
79 | ((key[i] >> 7) & STD_PARITY_BITS);
83 * Check the parity, reject the key if the check fails
85 if (!DESauth_parity(key)) {
90 * We can't find a good reason not to use this key.
93 DESauth_setkey(keyno, key);
98 * Make up key from ascii representation
100 memset((char *) keybytes, 0, sizeof(keybytes));
101 for (i = 0; i < 8 && i < len; i++)
102 keybytes[i] = *cp++ << 1;
103 key[0] = (u_int32)keybytes[0] << 24 | (u_int32)keybytes[1] << 16
104 | (u_int32)keybytes[2] << 8 | (u_int32)keybytes[3];
105 key[1] = (u_int32)keybytes[4] << 24 | (u_int32)keybytes[5] << 16
106 | (u_int32)keybytes[6] << 8 | (u_int32)keybytes[7];
111 (void)DESauth_parity(key);
116 DESauth_setkey(keyno, key);
121 MD5auth_setkey(keyno, str, (int)strlen((const char *)str));