1 | /*
|
---|
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
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
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include <openssl/crypto.h>
|
---|
11 | #include "des_locl.h"
|
---|
12 |
|
---|
13 | void DES_string_to_key(const char *str, DES_cblock *key)
|
---|
14 | {
|
---|
15 | DES_key_schedule ks;
|
---|
16 | int i, length;
|
---|
17 |
|
---|
18 | memset(key, 0, 8);
|
---|
19 | length = strlen(str);
|
---|
20 | #ifdef OLD_STR_TO_KEY
|
---|
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++) {
|
---|
25 | register unsigned char j = str[i];
|
---|
26 |
|
---|
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 | }
|
---|
37 | #endif
|
---|
38 | DES_set_odd_parity(key);
|
---|
39 | DES_set_key_unchecked(key, &ks);
|
---|
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 | }
|
---|
44 |
|
---|
45 | void DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2)
|
---|
46 | {
|
---|
47 | DES_key_schedule ks;
|
---|
48 | int i, length;
|
---|
49 |
|
---|
50 | memset(key1, 0, 8);
|
---|
51 | memset(key2, 0, 8);
|
---|
52 | length = strlen(str);
|
---|
53 | #ifdef OLD_STR_TO_KEY
|
---|
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++) {
|
---|
68 | register unsigned char j = str[i];
|
---|
69 |
|
---|
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);
|
---|
87 | #endif
|
---|
88 | DES_set_odd_parity(key1);
|
---|
89 | DES_set_odd_parity(key2);
|
---|
90 | DES_set_key_unchecked(key1, &ks);
|
---|
91 | DES_cbc_cksum((const unsigned char *)str, key1, length, &ks, key1);
|
---|
92 | DES_set_key_unchecked(key2, &ks);
|
---|
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 | }
|
---|