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
11 @file ocb3_encrypt_last.c
12 OCB implementation, internal helper, by Karel Miko
19 Finish an OCB (encryption) stream
20 @param ocb The OCB state
21 @param pt The remaining plaintext
22 @param ptlen The length of the plaintext (octets)
23 @param ct [out] The output buffer
24 @return CRYPT_OK if successful
26 int ocb3_encrypt_last(ocb3_state *ocb, const unsigned char *pt, unsigned long ptlen, unsigned char *ct)
28 unsigned char iOffset_star[MAXBLOCKSIZE];
29 unsigned char iPad[MAXBLOCKSIZE];
30 int err, x, full_blocks, full_blocks_len, last_block_len;
32 LTC_ARGCHK(ocb != NULL);
33 if (pt == NULL) LTC_ARGCHK(ptlen == 0);
35 LTC_ARGCHK(pt != NULL);
36 LTC_ARGCHK(ct != NULL);
39 if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
43 full_blocks = ptlen/ocb->block_len;
44 full_blocks_len = full_blocks * ocb->block_len;
45 last_block_len = ptlen - full_blocks_len;
47 /* process full blocks first */
49 if ((err = ocb3_encrypt(ocb, pt, full_blocks_len, ct)) != CRYPT_OK) {
54 /* at this point: m = ocb->block_index (last block index), Offset_m = ocb->Offset_current */
56 if (last_block_len>0) {
57 /* Offset_* = Offset_m xor L_* */
58 ocb3_int_xor_blocks(iOffset_star, ocb->Offset_current, ocb->L_star, ocb->block_len);
60 /* Pad = ENCIPHER(K, Offset_*) */
61 if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(iOffset_star, iPad, &ocb->key)) != CRYPT_OK) {
65 /* C_* = P_* xor Pad[1..bitlen(P_*)] */
66 ocb3_int_xor_blocks(ct+full_blocks_len, pt+full_blocks_len, iPad, last_block_len);
68 /* Checksum_* = Checksum_m xor (P_* || 1 || zeros(127-bitlen(P_*))) */
69 ocb3_int_xor_blocks(ocb->checksum, ocb->checksum, pt+full_blocks_len, last_block_len);
70 for(x=last_block_len; x<ocb->block_len; x++) {
71 if (x == last_block_len)
72 ocb->checksum[x] ^= 0x80;
74 ocb->checksum[x] ^= 0x00;
77 /* Tag = ENCIPHER(K, Checksum_* xor Offset_* xor L_$) xor HASH(K,A) */
78 /* at this point we calculate only: Tag_part = ENCIPHER(K, Checksum_* xor Offset_* xor L_$) */
79 for(x=0; x<ocb->block_len; x++) {
80 ocb->tag_part[x] = (ocb->checksum[x] ^ iOffset_star[x]) ^ ocb->L_dollar[x];
82 if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {
87 /* Tag = ENCIPHER(K, Checksum_m xor Offset_m xor L_$) xor HASH(K,A) */
88 /* at this point we calculate only: Tag_part = ENCIPHER(K, Checksum_m xor Offset_m xor L_$) */
89 for(x=0; x<ocb->block_len; x++) {
90 ocb->tag_part[x] = (ocb->checksum[x] ^ ocb->Offset_current[x]) ^ ocb->L_dollar[x];
92 if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {
100 #ifdef LTC_CLEAN_STACK
101 zeromem(iOffset_star, MAXBLOCKSIZE);
102 zeromem(iPad, MAXBLOCKSIZE);
110 /* ref: $Format:%D$ */
111 /* git commit: $Format:%H$ */
112 /* commit time: $Format:%ai$ */