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 for (i = 0; i < length; i++) { 21 register unsigned char j = str[i]; 22 23 if ((i % 16) < 8) 24 (*key)[i % 8] ^= (j << 1); 25 else { 26 /* Reverse the bit order 05/05/92 eay */ 27 j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f); 28 j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33); 29 j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55); 30 (*key)[7 - (i % 8)] ^= j; 31 } 32 } 33 DES_set_odd_parity(key); 34 DES_set_key_unchecked(key, &ks); 35 DES_cbc_cksum((const unsigned char *)str, key, length, &ks, key); 36 OPENSSL_cleanse(&ks, sizeof(ks)); 37 DES_set_odd_parity(key); 38 } 39 40 void DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2) 41 { 42 DES_key_schedule ks; 43 int i, length; 44 45 memset(key1, 0, 8); 46 memset(key2, 0, 8); 47 length = strlen(str); 48 for (i = 0; i < length; i++) { 49 register unsigned char j = str[i]; 50 51 if ((i % 32) < 16) { 52 if ((i % 16) < 8) 53 (*key1)[i % 8] ^= (j << 1); 54 else 55 (*key2)[i % 8] ^= (j << 1); 56 } else { 57 j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f); 58 j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33); 59 j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55); 60 if ((i % 16) < 8) 61 (*key1)[7 - (i % 8)] ^= j; 62 else 63 (*key2)[7 - (i % 8)] ^= j; 64 } 65 } 66 if (length <= 8) 67 memcpy(key2, key1, 8); 68 DES_set_odd_parity(key1); 69 DES_set_odd_parity(key2); 70 DES_set_key_unchecked(key1, &ks); 71 DES_cbc_cksum((const unsigned char *)str, key1, length, &ks, key1); 72 DES_set_key_unchecked(key2, &ks); 73 DES_cbc_cksum((const unsigned char *)str, key2, length, &ks, key2); 74 OPENSSL_cleanse(&ks, sizeof(ks)); 75 DES_set_odd_parity(key1); 76 DES_set_odd_parity(key2); 77 } 78