xref: /optee_os/core/lib/libtomcrypt/src/modes/ecb/ecb_decrypt.c (revision 8411e6ad673d20c4742ed30c785e3f5cdea54dfa)
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file ecb_decrypt.c
7   ECB implementation, decrypt a block, Tom St Denis
8 */
9 
10 #ifdef LTC_ECB_MODE
11 
12 /**
13   ECB decrypt
14   @param ct     Ciphertext
15   @param pt     [out] Plaintext
16   @param len    The number of octets to process (must be multiple of the cipher block size)
17   @param ecb    ECB state
18   @return CRYPT_OK if successful
19 */
ecb_decrypt(const unsigned char * ct,unsigned char * pt,unsigned long len,symmetric_ECB * ecb)20 int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_ECB *ecb)
21 {
22    int err;
23    LTC_ARGCHK(pt != NULL);
24    LTC_ARGCHK(ct != NULL);
25    LTC_ARGCHK(ecb != NULL);
26    if ((err = cipher_is_valid(ecb->cipher)) != CRYPT_OK) {
27        return err;
28    }
29    if (len % cipher_descriptor[ecb->cipher]->block_length) {
30       return CRYPT_INVALID_ARG;
31    }
32 
33    /* check for accel */
34    if (cipher_descriptor[ecb->cipher]->accel_ecb_decrypt != NULL) {
35       return cipher_descriptor[ecb->cipher]->accel_ecb_decrypt(ct, pt, len / cipher_descriptor[ecb->cipher]->block_length, &ecb->key);
36    }
37    while (len) {
38       if ((err = cipher_descriptor[ecb->cipher]->ecb_decrypt(ct, pt, &ecb->key)) != CRYPT_OK) {
39          return err;
40       }
41       pt  += cipher_descriptor[ecb->cipher]->block_length;
42       ct  += cipher_descriptor[ecb->cipher]->block_length;
43       len -= cipher_descriptor[ecb->cipher]->block_length;
44    }
45    return CRYPT_OK;
46 }
47 
48 #endif
49