/src/openssl/crypto/evp/e_xcbc_d.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2021 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 <stdio.h>  | 
17  |  | #include "internal/cryptlib.h"  | 
18  |  |  | 
19  |  | #ifndef OPENSSL_NO_DES  | 
20  |  |  | 
21  |  | # include <openssl/evp.h>  | 
22  |  | # include <openssl/objects.h>  | 
23  |  | # include "crypto/evp.h"  | 
24  |  | # include <openssl/des.h>  | 
25  |  | # include "evp_local.h"  | 
26  |  |  | 
27  |  | static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,  | 
28  |  |                              const unsigned char *iv, int enc);  | 
29  |  | static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,  | 
30  |  |                            const unsigned char *in, size_t inl);  | 
31  |  |  | 
32  |  | typedef struct { | 
33  |  |     DES_key_schedule ks;        /* key schedule */  | 
34  |  |     DES_cblock inw;  | 
35  |  |     DES_cblock outw;  | 
36  |  | } DESX_CBC_KEY;  | 
37  |  |  | 
38  | 0  | # define data(ctx) EVP_C_DATA(DESX_CBC_KEY,ctx)  | 
39  |  |  | 
40  |  | static const EVP_CIPHER d_xcbc_cipher = { | 
41  |  |     NID_desx_cbc,  | 
42  |  |     8, 24, 8,  | 
43  |  |     EVP_CIPH_CBC_MODE,  | 
44  |  |     EVP_ORIG_GLOBAL,  | 
45  |  |     desx_cbc_init_key,  | 
46  |  |     desx_cbc_cipher,  | 
47  |  |     NULL,  | 
48  |  |     sizeof(DESX_CBC_KEY),  | 
49  |  |     EVP_CIPHER_set_asn1_iv,  | 
50  |  |     EVP_CIPHER_get_asn1_iv,  | 
51  |  |     NULL,  | 
52  |  |     NULL  | 
53  |  | };  | 
54  |  |  | 
55  |  | const EVP_CIPHER *EVP_desx_cbc(void)  | 
56  | 16  | { | 
57  | 16  |     return &d_xcbc_cipher;  | 
58  | 16  | }  | 
59  |  |  | 
60  |  | static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,  | 
61  |  |                              const unsigned char *iv, int enc)  | 
62  | 0  | { | 
63  | 0  |     DES_cblock *deskey = (DES_cblock *)key;  | 
64  |  | 
  | 
65  | 0  |     DES_set_key_unchecked(deskey, &data(ctx)->ks);  | 
66  | 0  |     memcpy(&data(ctx)->inw[0], &key[8], 8);  | 
67  | 0  |     memcpy(&data(ctx)->outw[0], &key[16], 8);  | 
68  |  | 
  | 
69  | 0  |     return 1;  | 
70  | 0  | }  | 
71  |  |  | 
72  |  | static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,  | 
73  |  |                            const unsigned char *in, size_t inl)  | 
74  | 0  | { | 
75  | 0  |     while (inl >= EVP_MAXCHUNK) { | 
76  | 0  |         DES_xcbc_encrypt(in, out, (long)EVP_MAXCHUNK, &data(ctx)->ks,  | 
77  | 0  |                          (DES_cblock *)ctx->iv,  | 
78  | 0  |                          &data(ctx)->inw, &data(ctx)->outw,  | 
79  | 0  |                          EVP_CIPHER_CTX_is_encrypting(ctx));  | 
80  | 0  |         inl -= EVP_MAXCHUNK;  | 
81  | 0  |         in += EVP_MAXCHUNK;  | 
82  | 0  |         out += EVP_MAXCHUNK;  | 
83  | 0  |     }  | 
84  | 0  |     if (inl)  | 
85  | 0  |         DES_xcbc_encrypt(in, out, (long)inl, &data(ctx)->ks,  | 
86  | 0  |                          (DES_cblock *)ctx->iv,  | 
87  | 0  |                          &data(ctx)->inw, &data(ctx)->outw,  | 
88  | 0  |                          EVP_CIPHER_CTX_is_encrypting(ctx));  | 
89  | 0  |     return 1;  | 
90  | 0  | }  | 
91  |  | #endif  |