Be more explicit about RSAES-PKCS#1v1.5 error handling
[openssl.git] / crypto / evp / e_xcbc_d.c
1 /*
2  * Copyright 1995-2020 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
26 static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
27                              const unsigned char *iv, int enc);
28 static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
29                            const unsigned char *in, size_t inl);
30
31 typedef struct {
32     DES_key_schedule ks;        /* key schedule */
33     DES_cblock inw;
34     DES_cblock outw;
35 } DESX_CBC_KEY;
36
37 # define data(ctx) EVP_C_DATA(DESX_CBC_KEY,ctx)
38
39 static const EVP_CIPHER d_xcbc_cipher = {
40     NID_desx_cbc,
41     8, 24, 8,
42     EVP_CIPH_CBC_MODE,
43     desx_cbc_init_key,
44     desx_cbc_cipher,
45     NULL,
46     sizeof(DESX_CBC_KEY),
47     EVP_CIPHER_set_asn1_iv,
48     EVP_CIPHER_get_asn1_iv,
49     NULL,
50     NULL
51 };
52
53 const EVP_CIPHER *EVP_desx_cbc(void)
54 {
55     return &d_xcbc_cipher;
56 }
57
58 static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
59                              const unsigned char *iv, int enc)
60 {
61     DES_cblock *deskey = (DES_cblock *)key;
62
63     DES_set_key_unchecked(deskey, &data(ctx)->ks);
64     memcpy(&data(ctx)->inw[0], &key[8], 8);
65     memcpy(&data(ctx)->outw[0], &key[16], 8);
66
67     return 1;
68 }
69
70 static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
71                            const unsigned char *in, size_t inl)
72 {
73     while (inl >= EVP_MAXCHUNK) {
74         DES_xcbc_encrypt(in, out, (long)EVP_MAXCHUNK, &data(ctx)->ks,
75                          (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
76                          &data(ctx)->inw, &data(ctx)->outw,
77                          EVP_CIPHER_CTX_encrypting(ctx));
78         inl -= EVP_MAXCHUNK;
79         in += EVP_MAXCHUNK;
80         out += EVP_MAXCHUNK;
81     }
82     if (inl)
83         DES_xcbc_encrypt(in, out, (long)inl, &data(ctx)->ks,
84                          (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
85                          &data(ctx)->inw, &data(ctx)->outw,
86                          EVP_CIPHER_CTX_encrypting(ctx));
87     return 1;
88 }
89 #endif