1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
14 Process plaintext/ciphertext through CCM
15 @param ccm The CCM state
16 @param pt The plaintext
17 @param ptlen The plaintext length (ciphertext length is the same)
18 @param ct The ciphertext
19 @param direction Encrypt or Decrypt mode (CCM_ENCRYPT or CCM_DECRYPT)
20 @return CRYPT_OK on success
22 int ccm_process(ccm_state *ccm,
23 unsigned char *pt, unsigned long ptlen,
31 LTC_ARGCHK(ccm != NULL);
33 /* Check aad has been correctly added */
34 if (ccm->aadlen != ccm->current_aadlen) {
38 /* Check we do not process too much data */
39 if (ccm->ptlen < ccm->current_ptlen + ptlen) {
42 ccm->current_ptlen += ptlen;
44 /* now handle the PT */
46 LTC_ARGCHK(pt != NULL);
47 LTC_ARGCHK(ct != NULL);
49 for (y = 0; y < ptlen; y++) {
50 /* increment the ctr? */
51 if (ccm->CTRlen == 16) {
52 for (z = 15; z > 15-ccm->L; z--) {
53 ccm->ctr[z] = (ccm->ctr[z] + 1) & 255;
54 if (ccm->ctr[z]) break;
56 if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) {
62 /* if we encrypt we add the bytes to the MAC first */
63 if (direction == CCM_ENCRYPT) {
65 ct[y] = b ^ ccm->CTRPAD[ccm->CTRlen++];
67 b = ct[y] ^ ccm->CTRPAD[ccm->CTRlen++];
72 if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
77 ccm->PAD[ccm->x++] ^= b;
86 /* ref: $Format:%D$ */
87 /* git commit: $Format:%H$ */
88 /* commit time: $Format:%ai$ */