1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file ecb_decrypt.c
14   ECB implementation, decrypt a block, Tom St Denis
15 */
16 
17 #ifdef LTC_ECB_MODE
18 
19 /**
20   ECB decrypt
21   @param ct     Ciphertext
22   @param pt     [out] Plaintext
23   @param len    The number of octets to process (must be multiple of the cipher block size)
24   @param ecb    ECB state
25   @return CRYPT_OK if successful
26 */
ecb_decrypt(const unsigned char * ct,unsigned char * pt,unsigned long len,symmetric_ECB * ecb)27 int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_ECB *ecb)
28 {
29    int err;
30    LTC_ARGCHK(pt != NULL);
31    LTC_ARGCHK(ct != NULL);
32    LTC_ARGCHK(ecb != NULL);
33    if ((err = cipher_is_valid(ecb->cipher)) != CRYPT_OK) {
34        return err;
35    }
36    if (len % cipher_descriptor[ecb->cipher]->block_length) {
37       return CRYPT_INVALID_ARG;
38    }
39 
40    /* check for accel */
41    if (cipher_descriptor[ecb->cipher]->accel_ecb_decrypt != NULL) {
42       return cipher_descriptor[ecb->cipher]->accel_ecb_decrypt(ct, pt, len / cipher_descriptor[ecb->cipher]->block_length, &ecb->key);
43    }
44    while (len) {
45       if ((err = cipher_descriptor[ecb->cipher]->ecb_decrypt(ct, pt, &ecb->key)) != CRYPT_OK) {
46          return err;
47       }
48       pt  += cipher_descriptor[ecb->cipher]->block_length;
49       ct  += cipher_descriptor[ecb->cipher]->block_length;
50       len -= cipher_descriptor[ecb->cipher]->block_length;
51    }
52    return CRYPT_OK;
53 }
54 
55 #endif
56 
57 /* ref:         $Format:%D$ */
58 /* git commit:  $Format:%H$ */
59 /* commit time: $Format:%ai$ */
60