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
12 OCB implementation, encrypt data, by Tom St Denis
19 Encrypt blocks of data with OCB
20 @param ocb The OCB state
21 @param pt The plaintext (length multiple of the block size of the block cipher)
22 @param ptlen The length of the input (octets)
23 @param ct [out] The ciphertext (same size as the pt)
24 @return CRYPT_OK if successful
26 int ocb3_encrypt(ocb3_state *ocb, const unsigned char *pt, unsigned long ptlen, unsigned char *ct)
28 unsigned char tmp[MAXBLOCKSIZE];
29 int err, i, full_blocks;
30 unsigned char *pt_b, *ct_b;
32 LTC_ARGCHK(ocb != NULL);
33 if (ptlen == 0) return CRYPT_OK; /* no data, nothing to do */
34 LTC_ARGCHK(pt != NULL);
35 LTC_ARGCHK(ct != NULL);
37 if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
40 if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) {
41 return CRYPT_INVALID_ARG;
44 if (ptlen % ocb->block_len) { /* ptlen has to bu multiple of block_len */
45 return CRYPT_INVALID_ARG;
48 full_blocks = ptlen/ocb->block_len;
49 for(i=0; i<full_blocks; i++) {
50 pt_b = (unsigned char *)pt+i*ocb->block_len;
51 ct_b = (unsigned char *)ct+i*ocb->block_len;
53 /* ocb->Offset_current[] = ocb->Offset_current[] ^ Offset_{ntz(block_index)} */
54 ocb3_int_xor_blocks(ocb->Offset_current, ocb->Offset_current, ocb->L_[ocb3_int_ntz(ocb->block_index)], ocb->block_len);
56 /* tmp[] = pt[] XOR ocb->Offset_current[] */
57 ocb3_int_xor_blocks(tmp, pt_b, ocb->Offset_current, ocb->block_len);
60 if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, tmp, &ocb->key)) != CRYPT_OK) {
64 /* ct[] = tmp[] XOR ocb->Offset_current[] */
65 ocb3_int_xor_blocks(ct_b, tmp, ocb->Offset_current, ocb->block_len);
67 /* ocb->checksum[] = ocb->checksum[] XOR pt[] */
68 ocb3_int_xor_blocks(ocb->checksum, ocb->checksum, pt_b, ocb->block_len);
76 #ifdef LTC_CLEAN_STACK
77 zeromem(tmp, sizeof(tmp));
84 /* ref: $Format:%D$ */
85 /* git commit: $Format:%H$ */
86 /* commit time: $Format:%ai$ */