aboutsummaryrefslogtreecommitdiff
path: root/src/lib/crypto/builtin/des/string2key.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/crypto/builtin/des/string2key.c')
-rw-r--r--src/lib/crypto/builtin/des/string2key.c195
1 files changed, 98 insertions, 97 deletions
diff --git a/src/lib/crypto/builtin/des/string2key.c b/src/lib/crypto/builtin/des/string2key.c
index c817806..7ddee27 100644
--- a/src/lib/crypto/builtin/des/string2key.c
+++ b/src/lib/crypto/builtin/des/string2key.c
@@ -1,3 +1,4 @@
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* lib/crypto/des/des_s2k.c
*
@@ -32,13 +33,13 @@
krb5_error_code
mit_des_string_to_key_int (krb5_keyblock *key,
- const krb5_data *pw, const krb5_data *salt)
+ const krb5_data *pw, const krb5_data *salt)
{
union {
- /* 8 "forward" bytes, 8 "reverse" bytes */
- unsigned char uc[16];
- krb5_ui_4 ui[4];
- mit_des_cblock cb;
+ /* 8 "forward" bytes, 8 "reverse" bytes */
+ unsigned char uc[16];
+ krb5_ui_4 ui[4];
+ mit_des_cblock cb;
} temp;
unsigned int i;
krb5_ui_4 x, y, z;
@@ -53,30 +54,30 @@ mit_des_string_to_key_int (krb5_keyblock *key,
current algorithm is dependent on having four 8-bit char values
exactly overlay a 32-bit integral type. */
if (sizeof(temp.uc) != sizeof(temp.ui)
- || (unsigned char)~0 != 0xFF
- || (krb5_ui_4)~(krb5_ui_4)0 != 0xFFFFFFFF
- || (temp.uc[0] = 1, temp.uc[1] = 2, temp.uc[2] = 3, temp.uc[3] = 4,
- !(temp.ui[0] == 0x01020304
- || temp.ui[0] == 0x04030201)))
- abort();
-#define FETCH4(VAR, IDX) VAR = temp.ui[IDX/4]
-#define PUT4(VAR, IDX) temp.ui[IDX/4] = VAR
+ || (unsigned char)~0 != 0xFF
+ || (krb5_ui_4)~(krb5_ui_4)0 != 0xFFFFFFFF
+ || (temp.uc[0] = 1, temp.uc[1] = 2, temp.uc[2] = 3, temp.uc[3] = 4,
+ !(temp.ui[0] == 0x01020304
+ || temp.ui[0] == 0x04030201)))
+ abort();
+#define FETCH4(VAR, IDX) VAR = temp.ui[IDX/4]
+#define PUT4(VAR, IDX) temp.ui[IDX/4] = VAR
if (salt
- && (salt->length == SALT_TYPE_AFS_LENGTH
- /* XXX Yuck! Aren't we done with this yet? */
- || salt->length == (unsigned) -1)) {
- krb5_data afssalt;
- char *at;
+ && (salt->length == SALT_TYPE_AFS_LENGTH
+ /* XXX Yuck! Aren't we done with this yet? */
+ || salt->length == (unsigned) -1)) {
+ krb5_data afssalt;
+ char *at;
- afssalt.data = salt->data;
- at = strchr(afssalt.data, '@');
- if (at) {
- *at = 0;
- afssalt.length = at - afssalt.data;
- } else
- afssalt.length = strlen(afssalt.data);
- return mit_afs_string_to_key(key, pw, &afssalt);
+ afssalt.data = salt->data;
+ at = strchr(afssalt.data, '@');
+ if (at) {
+ *at = 0;
+ afssalt.length = at - afssalt.data;
+ } else
+ afssalt.length = strlen(afssalt.data);
+ return mit_afs_string_to_key(key, pw, &afssalt);
}
copylen = pw->length + (salt ? salt->length : 0);
@@ -84,10 +85,10 @@ mit_des_string_to_key_int (krb5_keyblock *key,
a byte array, not a string. */
copy = malloc(copylen);
if (copy == NULL)
- return ENOMEM;
+ return ENOMEM;
memcpy(copy, pw->data, pw->length);
if (salt)
- memcpy(copy + pw->length, salt->data, salt->length);
+ memcpy(copy + pw->length, salt->data, salt->length);
memset(&temp, 0, sizeof(temp));
p = temp.uc;
@@ -95,34 +96,34 @@ mit_des_string_to_key_int (krb5_keyblock *key,
forward and reverse sections, and combine them later, rather
than having to do the reversal over and over again. */
for (i = 0; i < copylen; i++) {
- *p++ ^= copy[i];
- if (p == temp.uc+16) {
- p = temp.uc;
+ *p++ ^= copy[i];
+ if (p == temp.uc+16) {
+ p = temp.uc;
#ifdef PRINT_TEST_VECTORS
- {
- int j;
- printf("after %d input bytes:\nforward block:\t", i+1);
- for (j = 0; j < 8; j++)
- printf(" %02x", temp.uc[j] & 0xff);
- printf("\nreverse block:\t");
- for (j = 8; j < 16; j++)
- printf(" %02x", temp.uc[j] & 0xff);
- printf("\n");
- }
+ {
+ int j;
+ printf("after %d input bytes:\nforward block:\t", i+1);
+ for (j = 0; j < 8; j++)
+ printf(" %02x", temp.uc[j] & 0xff);
+ printf("\nreverse block:\t");
+ for (j = 8; j < 16; j++)
+ printf(" %02x", temp.uc[j] & 0xff);
+ printf("\n");
+ }
#endif
- }
+ }
}
#ifdef PRINT_TEST_VECTORS
if (p != temp.uc) {
- int j;
- printf("at end, after %d input bytes:\nforward block:\t", i);
- for (j = 0; j < 8; j++)
- printf(" %02x", temp.uc[j] & 0xff);
- printf("\nreverse block:\t");
- for (j = 8; j < 16; j++)
- printf(" %02x", temp.uc[j] & 0xff);
- printf("\n");
+ int j;
+ printf("at end, after %d input bytes:\nforward block:\t", i);
+ for (j = 0; j < 8; j++)
+ printf(" %02x", temp.uc[j] & 0xff);
+ printf("\nreverse block:\t");
+ for (j = 8; j < 16; j++)
+ printf(" %02x", temp.uc[j] & 0xff);
+ printf("\n");
}
#endif
#if 0
@@ -137,24 +138,24 @@ mit_des_string_to_key_int (krb5_keyblock *key,
If we could rely on 64-bit math, another 7 ops would save us
from having to do double the work. */
-#define REVERSE_STEP(VAR, SHIFT, MASK) \
+#define REVERSE_STEP(VAR, SHIFT, MASK) \
VAR = ((VAR >> SHIFT) & MASK) | ((VAR << SHIFT) & (0xFFFFFFFFUL & ~MASK))
-#define REVERSE(VAR) \
- REVERSE_STEP (VAR, 1, 0x55555555UL); /* swap odd/even bits */ \
- REVERSE_STEP (VAR, 2, 0x33333333UL); /* swap bitpairs */ \
- REVERSE_STEP (VAR, 4, 0x0F0F0F0FUL); /* swap nibbles, etc */ \
- REVERSE_STEP (VAR, 8, 0x00FF00FFUL); \
+#define REVERSE(VAR) \
+ REVERSE_STEP (VAR, 1, 0x55555555UL); /* swap odd/even bits */ \
+ REVERSE_STEP (VAR, 2, 0x33333333UL); /* swap bitpairs */ \
+ REVERSE_STEP (VAR, 4, 0x0F0F0F0FUL); /* swap nibbles, etc */ \
+ REVERSE_STEP (VAR, 8, 0x00FF00FFUL); \
REVERSE_STEP (VAR, 16, 0x0000FFFFUL);
#else /* shorter */
-#define REVERSE(VAR) \
- { \
- krb5_ui_4 old = VAR, temp1 = 0; \
- int j; \
- for (j = 0; j < 32; j++) { \
- temp1 = (temp1 << 1) | (old & 1); \
- old >>= 1; \
- } \
- VAR = temp1; \
+#define REVERSE(VAR) \
+ { \
+ krb5_ui_4 old = VAR, temp1 = 0; \
+ int j; \
+ for (j = 0; j < 32; j++) { \
+ temp1 = (temp1 << 1) | (old & 1); \
+ old >>= 1; \
+ } \
+ VAR = temp1; \
}
#endif
@@ -168,16 +169,16 @@ mit_des_string_to_key_int (krb5_keyblock *key,
REVERSE (y);
#ifdef PRINT_TEST_VECTORS
{
- int j;
- union { unsigned char uc[4]; krb5_ui_4 ui; } t2;
- printf("after reversal, reversed block:\n\t\t");
- t2.ui = y;
- for (j = 0; j < 4; j++)
- printf(" %02x", t2.uc[j] & 0xff);
- t2.ui = x;
- for (j = 0; j < 4; j++)
- printf(" %02x", t2.uc[j] & 0xff);
- printf("\n");
+ int j;
+ union { unsigned char uc[4]; krb5_ui_4 ui; } t2;
+ printf("after reversal, reversed block:\n\t\t");
+ t2.ui = y;
+ for (j = 0; j < 4; j++)
+ printf(" %02x", t2.uc[j] & 0xff);
+ t2.ui = x;
+ for (j = 0; j < 4; j++)
+ printf(" %02x", t2.uc[j] & 0xff);
+ printf("\n");
}
#endif
/* Ignored bits are now at the bottom of each byte, where we'll
@@ -200,16 +201,16 @@ mit_des_string_to_key_int (krb5_keyblock *key,
#ifdef PRINT_TEST_VECTORS
{
- int j;
- printf("after reversal, combined block:\n\t\t");
- for (j = 0; j < 8; j++)
- printf(" %02x", temp.uc[j] & 0xff);
- printf("\n");
+ int j;
+ printf("after reversal, combined block:\n\t\t");
+ for (j = 0; j < 8; j++)
+ printf(" %02x", temp.uc[j] & 0xff);
+ printf("\n");
}
#endif
-#define FIXUP(K) \
- (mit_des_fixup_key_parity(K), \
+#define FIXUP(K) \
+ (mit_des_fixup_key_parity(K), \
mit_des_is_weak_key(K) ? (K[7] ^= 0xF0) : 0)
/* Now temp.cb is the temporary key, with invalid parity. */
@@ -217,11 +218,11 @@ mit_des_string_to_key_int (krb5_keyblock *key,
#ifdef PRINT_TEST_VECTORS
{
- int j;
- printf("after fixing parity and weak keys:\n\t\t");
- for (j = 0; j < 8; j++)
- printf(" %02x", temp.uc[j] & 0xff);
- printf("\n");
+ int j;
+ printf("after fixing parity and weak keys:\n\t\t");
+ for (j = 0; j < 8; j++)
+ printf(" %02x", temp.uc[j] & 0xff);
+ printf("\n");
}
#endif
@@ -233,11 +234,11 @@ mit_des_string_to_key_int (krb5_keyblock *key,
#ifdef PRINT_TEST_VECTORS
{
- int j;
- printf("cbc checksum:\n\t\t");
- for (j = 0; j < 8; j++)
- printf(" %02x", temp.uc[j] & 0xff);
- printf("\n");
+ int j;
+ printf("cbc checksum:\n\t\t");
+ for (j = 0; j < 8; j++)
+ printf(" %02x", temp.uc[j] & 0xff);
+ printf("\n");
}
#endif
@@ -246,11 +247,11 @@ mit_des_string_to_key_int (krb5_keyblock *key,
#ifdef PRINT_TEST_VECTORS
{
- int j;
- printf("after fixing parity and weak keys:\n\t\t");
- for (j = 0; j < 8; j++)
- printf(" %02x", temp.uc[j] & 0xff);
- printf("\n");
+ int j;
+ printf("after fixing parity and weak keys:\n\t\t");
+ for (j = 0; j < 8; j++)
+ printf(" %02x", temp.uc[j] & 0xff);
+ printf("\n");
}
#endif