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