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 <openssl/des.h> 17 #include "cipher_tdes_default.h" 18 19 /* 20 * Note the PROV_TDES_CTX has been used for the DESX cipher, just to reduce 21 * code size. 22 */ 23 #define ks1 tks.ks[0] 24 #define ks2 tks.ks[1].ks[0].cblock 25 #define ks3 tks.ks[2].ks[0].cblock 26 27 static int cipher_hw_desx_cbc_initkey(PROV_CIPHER_CTX *ctx, 28 const unsigned char *key, size_t keylen) 29 { 30 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx; 31 DES_cblock *deskey = (DES_cblock *)key; 32 33 DES_set_key_unchecked(deskey, &tctx->ks1); 34 memcpy(&tctx->ks2, &key[8], 8); 35 memcpy(&tctx->ks3, &key[16], 8); 36 37 return 1; 38 } 39 40 static void cipher_hw_desx_copyctx(PROV_CIPHER_CTX *dst, 41 const PROV_CIPHER_CTX *src) 42 { 43 PROV_TDES_CTX *sctx = (PROV_TDES_CTX *)src; 44 PROV_TDES_CTX *dctx = (PROV_TDES_CTX *)dst; 45 46 *dctx = *sctx; 47 dst->ks = &dctx->tks.ks; 48 } 49 50 static int cipher_hw_desx_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out, 51 const unsigned char *in, size_t inl) 52 { 53 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx; 54 55 while (inl >= MAXCHUNK) { 56 DES_xcbc_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1, 57 (DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3, 58 ctx->enc); 59 inl -= MAXCHUNK; 60 in += MAXCHUNK; 61 out += MAXCHUNK; 62 } 63 if (inl > 0) 64 DES_xcbc_encrypt(in, out, (long)inl, &tctx->ks1, 65 (DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3, 66 ctx->enc); 67 return 1; 68 } 69 70 static const PROV_CIPHER_HW desx_cbc = 71 { 72 cipher_hw_desx_cbc_initkey, 73 cipher_hw_desx_cbc, 74 cipher_hw_desx_copyctx 75 }; 76 77 const PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_desx_cbc(void) 78 { 79 return &desx_cbc; 80 } 81