xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/src/crypt/ossl/TpmToOsslDesSupport.c (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
1 /* Microsoft Reference Implementation for TPM 2.0
2  *
3  *  The copyright in this software is being made available under the BSD License,
4  *  included below. This software may be subject to other third party and
5  *  contributor rights, including patent rights, and no such rights are granted
6  *  under this license.
7  *
8  *  Copyright (c) Microsoft Corporation
9  *
10  *  All rights reserved.
11  *
12  *  BSD License
13  *
14  *  Redistribution and use in source and binary forms, with or without modification,
15  *  are permitted provided that the following conditions are met:
16  *
17  *  Redistributions of source code must retain the above copyright notice, this list
18  *  of conditions and the following disclaimer.
19  *
20  *  Redistributions in binary form must reproduce the above copyright notice, this
21  *  list of conditions and the following disclaimer in the documentation and/or
22  *  other materials provided with the distribution.
23  *
24  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 //** Introduction
36 //
37 // The functions in this file are used for initialization of the interface to the
38 // OpenSSL library.
39 
40 //** Defines and Includes
41 
42 #include "Tpm.h"
43 
44 #if (defined SYM_LIB_OSSL) && ALG_TDES
45 
46 //**Functions
47 //*** TDES_set_encyrpt_key()
48 // This function makes creation of a TDES key look like the creation of a key for
49 // any of the other OpenSSL block ciphers. It will create three key schedules,
50 // one for each of the DES keys. If there are only two keys, then the third schedule
51 // is a copy of the first.
52 void
TDES_set_encrypt_key(const BYTE * key,UINT16 keySizeInBits,tpmKeyScheduleTDES * keySchedule)53 TDES_set_encrypt_key(
54     const BYTE                  *key,
55     UINT16                       keySizeInBits,
56     tpmKeyScheduleTDES          *keySchedule
57     )
58 {
59     DES_set_key_unchecked((const_DES_cblock *)key, &keySchedule[0]);
60     DES_set_key_unchecked((const_DES_cblock *)&key[8], &keySchedule[1]);
61     // If is two-key, copy the schedule for K1 into K3, otherwise, compute the
62     // the schedule for K3
63     if(keySizeInBits == 128)
64         keySchedule[2] = keySchedule[0];
65     else
66         DES_set_key_unchecked((const_DES_cblock *)&key[16],
67                               &keySchedule[2]);
68 }
69 
70 
71 //*** TDES_encyrpt()
72 // The TPM code uses one key schedule. For TDES, the schedule contains three
73 // schedules. OpenSSL wants the schedules referenced separately. This function
74 // does that.
TDES_encrypt(const BYTE * in,BYTE * out,tpmKeyScheduleTDES * ks)75 void TDES_encrypt(
76     const BYTE              *in,
77     BYTE                    *out,
78     tpmKeyScheduleTDES      *ks
79     )
80 {
81     DES_ecb3_encrypt((const_DES_cblock *)in, (DES_cblock *)out,
82                      &ks[0], &ks[1], &ks[2],
83                      DES_ENCRYPT);
84 }
85 
86 //*** TDES_decrypt()
87 // As with TDES_encypt() this function bridges between the TPM single schedule
88 // model and the OpenSSL three schedule model.
TDES_decrypt(const BYTE * in,BYTE * out,tpmKeyScheduleTDES * ks)89 void TDES_decrypt(
90     const BYTE          *in,
91     BYTE                *out,
92     tpmKeyScheduleTDES   *ks
93     )
94 {
95     DES_ecb3_encrypt((const_DES_cblock *)in, (DES_cblock *)out,
96                      &ks[0], &ks[1], &ks[2],
97                      DES_DECRYPT);
98 }
99 
100 #endif // SYM_LIB_OSSL
101