1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 /*
11  * DES low level APIs are deprecated for public use, but still ok for internal
12  * use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <openssl/crypto.h>
17 #include "des_local.h"
18 
DES_string_to_key(const char * str,DES_cblock * key)19 void DES_string_to_key(const char *str, DES_cblock *key)
20 {
21     DES_key_schedule ks;
22     int i;
23     size_t length;
24 
25     memset(key, 0, 8);
26     length = strlen(str);
27     if (length > INT_MAX)
28         length = INT_MAX;
29     for (i = 0; i < (int)length; i++) {
30         register unsigned char j = str[i];
31 
32         if ((i % 16) < 8)
33             (*key)[i % 8] ^= (j << 1);
34         else {
35             /* Reverse the bit order 05/05/92 eay */
36             j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f);
37             j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33);
38             j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55);
39             (*key)[7 - (i % 8)] ^= j;
40         }
41     }
42     DES_set_odd_parity(key);
43     DES_set_key_unchecked(key, &ks);
44     DES_cbc_cksum((const unsigned char *)str, key, (int)length, &ks, key);
45     OPENSSL_cleanse(&ks, sizeof(ks));
46     DES_set_odd_parity(key);
47 }
48 
DES_string_to_2keys(const char * str,DES_cblock * key1,DES_cblock * key2)49 void DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2)
50 {
51     DES_key_schedule ks;
52     int i;
53     size_t length;
54 
55     memset(key1, 0, 8);
56     memset(key2, 0, 8);
57     length = strlen(str);
58     if (length > INT_MAX)
59         length = INT_MAX;
60     for (i = 0; i < (int)length; i++) {
61         register unsigned char j = str[i];
62 
63         if ((i % 32) < 16) {
64             if ((i % 16) < 8)
65                 (*key1)[i % 8] ^= (j << 1);
66             else
67                 (*key2)[i % 8] ^= (j << 1);
68         } else {
69             j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f);
70             j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33);
71             j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55);
72             if ((i % 16) < 8)
73                 (*key1)[7 - (i % 8)] ^= j;
74             else
75                 (*key2)[7 - (i % 8)] ^= j;
76         }
77     }
78     if (length <= 8)
79         memcpy(key2, key1, 8);
80     DES_set_odd_parity(key1);
81     DES_set_odd_parity(key2);
82     DES_set_key_unchecked(key1, &ks);
83     DES_cbc_cksum((const unsigned char *)str, key1, (int)length, &ks, key1);
84     DES_set_key_unchecked(key2, &ks);
85     DES_cbc_cksum((const unsigned char *)str, key2, (int)length, &ks, key2);
86     OPENSSL_cleanse(&ks, sizeof(ks));
87     DES_set_odd_parity(key1);
88     DES_set_odd_parity(key2);
89 }
90