1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4
5 /**
6 @file ecb_encrypt.c
7 ECB implementation, encrypt a block, Tom St Denis
8 */
9
10 #ifdef LTC_ECB_MODE
11
12 /**
13 ECB encrypt
14 @param pt Plaintext
15 @param ct [out] Ciphertext
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_encrypt(const unsigned char * pt,unsigned char * ct,unsigned long len,symmetric_ECB * ecb)20 int ecb_encrypt(const unsigned char *pt, unsigned char *ct, 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_encrypt != NULL) {
35 return cipher_descriptor[ecb->cipher]->accel_ecb_encrypt(pt, ct, len / cipher_descriptor[ecb->cipher]->block_length, &ecb->key);
36 }
37 while (len) {
38 if ((err = cipher_descriptor[ecb->cipher]->ecb_encrypt(pt, ct, &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