xref: /third_party/openssl/crypto/des/str2key.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * DES low level APIs are deprecated for public use, but still ok for internal
12e1051a39Sopenharmony_ci * use.
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci#include "internal/deprecated.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci#include <openssl/crypto.h>
17e1051a39Sopenharmony_ci#include "des_local.h"
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_civoid DES_string_to_key(const char *str, DES_cblock *key)
20e1051a39Sopenharmony_ci{
21e1051a39Sopenharmony_ci    DES_key_schedule ks;
22e1051a39Sopenharmony_ci    int i, length;
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ci    memset(key, 0, 8);
25e1051a39Sopenharmony_ci    length = strlen(str);
26e1051a39Sopenharmony_ci    for (i = 0; i < length; i++) {
27e1051a39Sopenharmony_ci        register unsigned char j = str[i];
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_ci        if ((i % 16) < 8)
30e1051a39Sopenharmony_ci            (*key)[i % 8] ^= (j << 1);
31e1051a39Sopenharmony_ci        else {
32e1051a39Sopenharmony_ci            /* Reverse the bit order 05/05/92 eay */
33e1051a39Sopenharmony_ci            j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f);
34e1051a39Sopenharmony_ci            j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33);
35e1051a39Sopenharmony_ci            j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55);
36e1051a39Sopenharmony_ci            (*key)[7 - (i % 8)] ^= j;
37e1051a39Sopenharmony_ci        }
38e1051a39Sopenharmony_ci    }
39e1051a39Sopenharmony_ci    DES_set_odd_parity(key);
40e1051a39Sopenharmony_ci    DES_set_key_unchecked(key, &ks);
41e1051a39Sopenharmony_ci    DES_cbc_cksum((const unsigned char *)str, key, length, &ks, key);
42e1051a39Sopenharmony_ci    OPENSSL_cleanse(&ks, sizeof(ks));
43e1051a39Sopenharmony_ci    DES_set_odd_parity(key);
44e1051a39Sopenharmony_ci}
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_civoid DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2)
47e1051a39Sopenharmony_ci{
48e1051a39Sopenharmony_ci    DES_key_schedule ks;
49e1051a39Sopenharmony_ci    int i, length;
50e1051a39Sopenharmony_ci
51e1051a39Sopenharmony_ci    memset(key1, 0, 8);
52e1051a39Sopenharmony_ci    memset(key2, 0, 8);
53e1051a39Sopenharmony_ci    length = strlen(str);
54e1051a39Sopenharmony_ci    for (i = 0; i < length; i++) {
55e1051a39Sopenharmony_ci        register unsigned char j = str[i];
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ci        if ((i % 32) < 16) {
58e1051a39Sopenharmony_ci            if ((i % 16) < 8)
59e1051a39Sopenharmony_ci                (*key1)[i % 8] ^= (j << 1);
60e1051a39Sopenharmony_ci            else
61e1051a39Sopenharmony_ci                (*key2)[i % 8] ^= (j << 1);
62e1051a39Sopenharmony_ci        } else {
63e1051a39Sopenharmony_ci            j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f);
64e1051a39Sopenharmony_ci            j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33);
65e1051a39Sopenharmony_ci            j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55);
66e1051a39Sopenharmony_ci            if ((i % 16) < 8)
67e1051a39Sopenharmony_ci                (*key1)[7 - (i % 8)] ^= j;
68e1051a39Sopenharmony_ci            else
69e1051a39Sopenharmony_ci                (*key2)[7 - (i % 8)] ^= j;
70e1051a39Sopenharmony_ci        }
71e1051a39Sopenharmony_ci    }
72e1051a39Sopenharmony_ci    if (length <= 8)
73e1051a39Sopenharmony_ci        memcpy(key2, key1, 8);
74e1051a39Sopenharmony_ci    DES_set_odd_parity(key1);
75e1051a39Sopenharmony_ci    DES_set_odd_parity(key2);
76e1051a39Sopenharmony_ci    DES_set_key_unchecked(key1, &ks);
77e1051a39Sopenharmony_ci    DES_cbc_cksum((const unsigned char *)str, key1, length, &ks, key1);
78e1051a39Sopenharmony_ci    DES_set_key_unchecked(key2, &ks);
79e1051a39Sopenharmony_ci    DES_cbc_cksum((const unsigned char *)str, key2, length, &ks, key2);
80e1051a39Sopenharmony_ci    OPENSSL_cleanse(&ks, sizeof(ks));
81e1051a39Sopenharmony_ci    DES_set_odd_parity(key1);
82e1051a39Sopenharmony_ci    DES_set_odd_parity(key2);
83e1051a39Sopenharmony_ci}
84