]>
Commit | Line | Data |
---|---|---|
62867571 | 1 | /* |
33388b44 | 2 | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
4a8b0c55 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
62867571 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
c6fec81b P |
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 | ||
d02b48c6 | 16 | #include <stdio.h> |
b39fc560 | 17 | #include "internal/cryptlib.h" |
9e9e8cb6 RL |
18 | |
19 | #ifndef OPENSSL_NO_DES | |
20 | ||
0f113f3e MC |
21 | # include <openssl/evp.h> |
22 | # include <openssl/objects.h> | |
25f2138b | 23 | # include "crypto/evp.h" |
0f113f3e | 24 | # include <openssl/des.h> |
acb30f4b | 25 | # include "evp_local.h" |
d02b48c6 | 26 | |
1921eaad | 27 | static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
0f113f3e | 28 | const unsigned char *iv, int enc); |
be06a934 | 29 | static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 30 | const unsigned char *in, size_t inl); |
dbad1690 | 31 | |
0f113f3e MC |
32 | typedef struct { |
33 | DES_key_schedule ks; /* key schedule */ | |
c2e4f17c RL |
34 | DES_cblock inw; |
35 | DES_cblock outw; | |
0f113f3e | 36 | } DESX_CBC_KEY; |
dbad1690 | 37 | |
6435f0f6 | 38 | # define data(ctx) EVP_C_DATA(DESX_CBC_KEY,ctx) |
dbad1690 | 39 | |
0f113f3e MC |
40 | static const EVP_CIPHER d_xcbc_cipher = { |
41 | NID_desx_cbc, | |
42 | 8, 24, 8, | |
43 | EVP_CIPH_CBC_MODE, | |
44 | desx_cbc_init_key, | |
45 | desx_cbc_cipher, | |
46 | NULL, | |
47 | sizeof(DESX_CBC_KEY), | |
48 | EVP_CIPHER_set_asn1_iv, | |
49 | EVP_CIPHER_get_asn1_iv, | |
50 | NULL, | |
51 | NULL | |
52 | }; | |
d02b48c6 | 53 | |
13588350 | 54 | const EVP_CIPHER *EVP_desx_cbc(void) |
0f113f3e | 55 | { |
26a7d938 | 56 | return &d_xcbc_cipher; |
0f113f3e MC |
57 | } |
58 | ||
1921eaad | 59 | static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
0f113f3e MC |
60 | const unsigned char *iv, int enc) |
61 | { | |
62 | DES_cblock *deskey = (DES_cblock *)key; | |
edf0bfb5 | 63 | |
0f113f3e MC |
64 | DES_set_key_unchecked(deskey, &data(ctx)->ks); |
65 | memcpy(&data(ctx)->inw[0], &key[8], 8); | |
66 | memcpy(&data(ctx)->outw[0], &key[16], 8); | |
360370d9 | 67 | |
0f113f3e MC |
68 | return 1; |
69 | } | |
d02b48c6 | 70 | |
be06a934 | 71 | static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e MC |
72 | const unsigned char *in, size_t inl) |
73 | { | |
74 | while (inl >= EVP_MAXCHUNK) { | |
75 | DES_xcbc_encrypt(in, out, (long)EVP_MAXCHUNK, &data(ctx)->ks, | |
acb30f4b | 76 | (DES_cblock *)ctx->iv, |
6435f0f6 RL |
77 | &data(ctx)->inw, &data(ctx)->outw, |
78 | EVP_CIPHER_CTX_encrypting(ctx)); | |
0f113f3e MC |
79 | inl -= EVP_MAXCHUNK; |
80 | in += EVP_MAXCHUNK; | |
81 | out += EVP_MAXCHUNK; | |
82 | } | |
83 | if (inl) | |
84 | DES_xcbc_encrypt(in, out, (long)inl, &data(ctx)->ks, | |
acb30f4b | 85 | (DES_cblock *)ctx->iv, |
6435f0f6 RL |
86 | &data(ctx)->inw, &data(ctx)->outw, |
87 | EVP_CIPHER_CTX_encrypting(ctx)); | |
0f113f3e MC |
88 | return 1; |
89 | } | |
d02f751c | 90 | #endif |