]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/des/str2key.c
Remove trailing whitespace from some files.
[thirdparty/openssl.git] / crypto / des / str2key.c
CommitLineData
d2e9e320
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
d2e9e320
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
d02b48c6
RE
8 */
9
298a2f9e 10#include <openssl/crypto.h>
d749e108 11#include "des_locl.h"
d02b48c6 12
c2e4f17c 13void DES_string_to_key(const char *str, DES_cblock *key)
0f113f3e
MC
14{
15 DES_key_schedule ks;
16 int i, length;
d02b48c6 17
0f113f3e
MC
18 memset(key, 0, 8);
19 length = strlen(str);
d02b48c6 20#ifdef OLD_STR_TO_KEY
0f113f3e
MC
21 for (i = 0; i < length; i++)
22 (*key)[i % 8] ^= (str[i] << 1);
23#else /* MIT COMPATIBLE */
24 for (i = 0; i < length; i++) {
56989dcd
RS
25 register unsigned char j = str[i];
26
0f113f3e
MC
27 if ((i % 16) < 8)
28 (*key)[i % 8] ^= (j << 1);
29 else {
30 /* Reverse the bit order 05/05/92 eay */
31 j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f);
32 j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33);
33 j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55);
34 (*key)[7 - (i % 8)] ^= j;
35 }
36 }
d02b48c6 37#endif
0f113f3e 38 DES_set_odd_parity(key);
0f113f3e 39 DES_set_key_unchecked(key, &ks);
0f113f3e
MC
40 DES_cbc_cksum((const unsigned char *)str, key, length, &ks, key);
41 OPENSSL_cleanse(&ks, sizeof(ks));
42 DES_set_odd_parity(key);
43}
d02b48c6 44
c2e4f17c 45void DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2)
0f113f3e
MC
46{
47 DES_key_schedule ks;
48 int i, length;
d02b48c6 49
0f113f3e
MC
50 memset(key1, 0, 8);
51 memset(key2, 0, 8);
52 length = strlen(str);
d02b48c6 53#ifdef OLD_STR_TO_KEY
0f113f3e
MC
54 if (length <= 8) {
55 for (i = 0; i < length; i++) {
56 (*key2)[i] = (*key1)[i] = (str[i] << 1);
57 }
58 } else {
59 for (i = 0; i < length; i++) {
60 if ((i / 8) & 1)
61 (*key2)[i % 8] ^= (str[i] << 1);
62 else
63 (*key1)[i % 8] ^= (str[i] << 1);
64 }
65 }
66#else /* MIT COMPATIBLE */
67 for (i = 0; i < length; i++) {
56989dcd
RS
68 register unsigned char j = str[i];
69
0f113f3e
MC
70 if ((i % 32) < 16) {
71 if ((i % 16) < 8)
72 (*key1)[i % 8] ^= (j << 1);
73 else
74 (*key2)[i % 8] ^= (j << 1);
75 } else {
76 j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f);
77 j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33);
78 j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55);
79 if ((i % 16) < 8)
80 (*key1)[7 - (i % 8)] ^= j;
81 else
82 (*key2)[7 - (i % 8)] ^= j;
83 }
84 }
85 if (length <= 8)
86 memcpy(key2, key1, 8);
d02b48c6 87#endif
0f113f3e
MC
88 DES_set_odd_parity(key1);
89 DES_set_odd_parity(key2);
0f113f3e 90 DES_set_key_unchecked(key1, &ks);
0f113f3e 91 DES_cbc_cksum((const unsigned char *)str, key1, length, &ks, key1);
0f113f3e 92 DES_set_key_unchecked(key2, &ks);
0f113f3e
MC
93 DES_cbc_cksum((const unsigned char *)str, key2, length, &ks, key2);
94 OPENSSL_cleanse(&ks, sizeof(ks));
95 DES_set_odd_parity(key1);
96 DES_set_odd_parity(key2);
97}