11cb0ef41Sopenharmony_ci/* 21cb0ef41Sopenharmony_ci * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. 31cb0ef41Sopenharmony_ci * 41cb0ef41Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 51cb0ef41Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 61cb0ef41Sopenharmony_ci * in the file LICENSE in the source distribution or at 71cb0ef41Sopenharmony_ci * https://www.openssl.org/source/license.html 81cb0ef41Sopenharmony_ci */ 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci/* 111cb0ef41Sopenharmony_ci * DES low level APIs are deprecated for public use, but still ok for internal 121cb0ef41Sopenharmony_ci * use. 131cb0ef41Sopenharmony_ci */ 141cb0ef41Sopenharmony_ci#include "internal/deprecated.h" 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci#include <openssl/crypto.h> 171cb0ef41Sopenharmony_ci#include "des_local.h" 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_civoid DES_string_to_key(const char *str, DES_cblock *key) 201cb0ef41Sopenharmony_ci{ 211cb0ef41Sopenharmony_ci DES_key_schedule ks; 221cb0ef41Sopenharmony_ci int i, length; 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci memset(key, 0, 8); 251cb0ef41Sopenharmony_ci length = strlen(str); 261cb0ef41Sopenharmony_ci for (i = 0; i < length; i++) { 271cb0ef41Sopenharmony_ci register unsigned char j = str[i]; 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci if ((i % 16) < 8) 301cb0ef41Sopenharmony_ci (*key)[i % 8] ^= (j << 1); 311cb0ef41Sopenharmony_ci else { 321cb0ef41Sopenharmony_ci /* Reverse the bit order 05/05/92 eay */ 331cb0ef41Sopenharmony_ci j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f); 341cb0ef41Sopenharmony_ci j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33); 351cb0ef41Sopenharmony_ci j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55); 361cb0ef41Sopenharmony_ci (*key)[7 - (i % 8)] ^= j; 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci DES_set_odd_parity(key); 401cb0ef41Sopenharmony_ci DES_set_key_unchecked(key, &ks); 411cb0ef41Sopenharmony_ci DES_cbc_cksum((const unsigned char *)str, key, length, &ks, key); 421cb0ef41Sopenharmony_ci OPENSSL_cleanse(&ks, sizeof(ks)); 431cb0ef41Sopenharmony_ci DES_set_odd_parity(key); 441cb0ef41Sopenharmony_ci} 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_civoid DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2) 471cb0ef41Sopenharmony_ci{ 481cb0ef41Sopenharmony_ci DES_key_schedule ks; 491cb0ef41Sopenharmony_ci int i, length; 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci memset(key1, 0, 8); 521cb0ef41Sopenharmony_ci memset(key2, 0, 8); 531cb0ef41Sopenharmony_ci length = strlen(str); 541cb0ef41Sopenharmony_ci for (i = 0; i < length; i++) { 551cb0ef41Sopenharmony_ci register unsigned char j = str[i]; 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci if ((i % 32) < 16) { 581cb0ef41Sopenharmony_ci if ((i % 16) < 8) 591cb0ef41Sopenharmony_ci (*key1)[i % 8] ^= (j << 1); 601cb0ef41Sopenharmony_ci else 611cb0ef41Sopenharmony_ci (*key2)[i % 8] ^= (j << 1); 621cb0ef41Sopenharmony_ci } else { 631cb0ef41Sopenharmony_ci j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f); 641cb0ef41Sopenharmony_ci j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33); 651cb0ef41Sopenharmony_ci j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55); 661cb0ef41Sopenharmony_ci if ((i % 16) < 8) 671cb0ef41Sopenharmony_ci (*key1)[7 - (i % 8)] ^= j; 681cb0ef41Sopenharmony_ci else 691cb0ef41Sopenharmony_ci (*key2)[7 - (i % 8)] ^= j; 701cb0ef41Sopenharmony_ci } 711cb0ef41Sopenharmony_ci } 721cb0ef41Sopenharmony_ci if (length <= 8) 731cb0ef41Sopenharmony_ci memcpy(key2, key1, 8); 741cb0ef41Sopenharmony_ci DES_set_odd_parity(key1); 751cb0ef41Sopenharmony_ci DES_set_odd_parity(key2); 761cb0ef41Sopenharmony_ci DES_set_key_unchecked(key1, &ks); 771cb0ef41Sopenharmony_ci DES_cbc_cksum((const unsigned char *)str, key1, length, &ks, key1); 781cb0ef41Sopenharmony_ci DES_set_key_unchecked(key2, &ks); 791cb0ef41Sopenharmony_ci DES_cbc_cksum((const unsigned char *)str, key2, length, &ks, key2); 801cb0ef41Sopenharmony_ci OPENSSL_cleanse(&ks, sizeof(ks)); 811cb0ef41Sopenharmony_ci DES_set_odd_parity(key1); 821cb0ef41Sopenharmony_ci DES_set_odd_parity(key2); 831cb0ef41Sopenharmony_ci} 84