| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_DES_H |
| 5 |
#define OPENSSL_HEADER_DES_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#if defined(__cplusplus) |
| 10 |
extern "C" { |
| 11 |
#endif |
| 12 |
|
| 13 |
|
| 14 |
// DES. |
| 15 |
// |
| 16 |
// This module is deprecated and retained for legacy reasons only. It is slow |
| 17 |
// and may leak key material with timing or cache side channels. Moreover, |
| 18 |
// single-keyed DES is broken and can be brute-forced in under a day. |
| 19 |
// |
| 20 |
// Use a modern cipher, such as AES-GCM or ChaCha20-Poly1305, instead. |
| 21 |
|
| 22 |
|
| 23 |
typedef struct DES_cblock_st { |
| 24 |
uint8_t bytes[8]; |
| 25 |
} DES_cblock; |
| 26 |
|
| 27 |
typedef struct DES_cblock_st const_DES_cblock; |
| 28 |
|
| 29 |
typedef struct DES_ks { |
| 30 |
uint32_t subkeys[16][2]; |
| 31 |
} DES_key_schedule; |
| 32 |
|
| 33 |
|
| 34 |
#define DES_KEY_SZ (sizeof(DES_cblock)) |
| 35 |
#define DES_SCHEDULE_SZ (sizeof(DES_key_schedule)) |
| 36 |
|
| 37 |
#define DES_ENCRYPT 1 |
| 38 |
#define DES_DECRYPT 0 |
| 39 |
|
| 40 |
#define DES_CBC_MODE 0 |
| 41 |
#define DES_PCBC_MODE 1 |
| 42 |
|
| 43 |
// DES_is_weak_key checks if |key| is a weak or semi-weak key, see SP 800-67r2 |
| 44 |
// section 3.3.2 |
| 45 |
OPENSSL_EXPORT int DES_is_weak_key(const DES_cblock *key); |
| 46 |
|
| 47 |
// DES_set_key checks that |key| is not weak and the parity before calling |
| 48 |
// |DES_set_key_unchecked|. The key schedule is always initialized, the checks |
| 49 |
// only affect the return value: |
| 50 |
// 0: key is not weak and has odd parity |
| 51 |
// -1: key is not odd |
| 52 |
// -2: key is a weak key, the parity might also be even |
| 53 |
OPENSSL_EXPORT int DES_set_key(const DES_cblock *key, DES_key_schedule *schedule); |
| 54 |
|
| 55 |
// DES_set_key_unchecked performs a key schedule and initialises |schedule| with |key|. |
| 56 |
OPENSSL_EXPORT void DES_set_key_unchecked(const DES_cblock *key, DES_key_schedule *schedule); |
| 57 |
|
| 58 |
// DES_key_sched calls |DES_set_key|. |
| 59 |
OPENSSL_EXPORT int DES_key_sched(const DES_cblock *key, DES_key_schedule *schedule); |
| 60 |
|
| 61 |
// DES_set_odd_parity sets the parity bits (the least-significant bits in each |
| 62 |
// byte) of |key| given the other bits in each byte. |
| 63 |
OPENSSL_EXPORT void DES_set_odd_parity(DES_cblock *key); |
| 64 |
|
| 65 |
// DES_ecb_encrypt encrypts (or decrypts, if |is_encrypt| is |DES_DECRYPT|) a |
| 66 |
// single DES block (8 bytes) from in to out, using the key configured in |
| 67 |
// |schedule|. |
| 68 |
OPENSSL_EXPORT void DES_ecb_encrypt(const DES_cblock *in, DES_cblock *out, |
| 69 |
const DES_key_schedule *schedule, |
| 70 |
int is_encrypt); |
| 71 |
|
| 72 |
// DES_ncbc_encrypt encrypts (or decrypts, if |enc| is |DES_DECRYPT|) |len| |
| 73 |
// bytes from |in| to |out| with DES in CBC mode. |
| 74 |
OPENSSL_EXPORT void DES_ncbc_encrypt(const uint8_t *in, uint8_t *out, |
| 75 |
size_t len, |
| 76 |
const DES_key_schedule *schedule, |
| 77 |
DES_cblock *ivec, int enc); |
| 78 |
|
| 79 |
// DES_ecb3_encrypt encrypts (or decrypts, if |enc| is |DES_DECRYPT|) a single |
| 80 |
// block (8 bytes) of data from |input| to |output| using 3DES. |
| 81 |
OPENSSL_EXPORT void DES_ecb3_encrypt(const DES_cblock *input, |
| 82 |
DES_cblock *output, |
| 83 |
const DES_key_schedule *ks1, |
| 84 |
const DES_key_schedule *ks2, |
| 85 |
const DES_key_schedule *ks3, |
| 86 |
int enc); |
| 87 |
|
| 88 |
// DES_ede3_cbc_encrypt encrypts (or decrypts, if |enc| is |DES_DECRYPT|) |len| |
| 89 |
// bytes from |in| to |out| with 3DES in CBC mode. 3DES uses three keys, thus |
| 90 |
// the function takes three different |DES_key_schedule|s. |
| 91 |
OPENSSL_EXPORT void DES_ede3_cbc_encrypt(const uint8_t *in, uint8_t *out, |
| 92 |
size_t len, |
| 93 |
const DES_key_schedule *ks1, |
| 94 |
const DES_key_schedule *ks2, |
| 95 |
const DES_key_schedule *ks3, |
| 96 |
DES_cblock *ivec, int enc); |
| 97 |
|
| 98 |
// DES_ede2_cbc_encrypt encrypts (or decrypts, if |enc| is |DES_DECRYPT|) |len| |
| 99 |
// bytes from |in| to |out| with 3DES in CBC mode. With this keying option, the |
| 100 |
// first and third 3DES keys are identical. Thus, this function takes only two |
| 101 |
// different |DES_key_schedule|s. |
| 102 |
OPENSSL_EXPORT void DES_ede2_cbc_encrypt(const uint8_t *in, uint8_t *out, |
| 103 |
size_t len, |
| 104 |
const DES_key_schedule *ks1, |
| 105 |
const DES_key_schedule *ks2, |
| 106 |
DES_cblock *ivec, int enc); |
| 107 |
|
| 108 |
|
| 109 |
#if defined(__cplusplus) |
| 110 |
} // extern C |
| 111 |
#endif |
| 112 |
|
| 113 |
#endif // OPENSSL_HEADER_DES_H |