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 Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects
17 static int _tweak_uncrypt(const unsigned char *C, unsigned char *P, unsigned char *T, symmetric_xts *xts)
22 /* tweak encrypt block i */
24 for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
25 *(LTC_FAST_TYPE_PTR_CAST(&P[x])) = *(LTC_FAST_TYPE_PTR_CAST(&C[x])) ^ *(LTC_FAST_TYPE_PTR_CAST(&T[x]));
28 for (x = 0; x < 16; x++) {
33 err = cipher_descriptor[xts->cipher].ecb_decrypt(P, P, &xts->key1);
36 for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
37 *(LTC_FAST_TYPE_PTR_CAST(&P[x])) ^= *(LTC_FAST_TYPE_PTR_CAST(&T[x]));
40 for (x = 0; x < 16; x++) {
52 @param ct [in] Ciphertext
53 @param ptlen Length of plaintext (and ciphertext)
54 @param pt [out] Plaintext
55 @param tweak [in] The 128--bit encryption tweak (e.g. sector number)
56 @param xts The XTS structure
57 Returns CRYPT_OK upon success
59 int xts_decrypt(const unsigned char *ct, unsigned long ptlen, unsigned char *pt, unsigned char *tweak,
62 unsigned char PP[16], CC[16], T[16];
63 unsigned long i, m, mo, lim;
67 LTC_ARGCHK(pt != NULL);
68 LTC_ARGCHK(ct != NULL);
69 LTC_ARGCHK(tweak != NULL);
70 LTC_ARGCHK(xts != NULL);
73 if ((err = cipher_is_valid(xts->cipher)) != CRYPT_OK) {
77 /* get number of blocks */
81 /* must have at least one full block */
83 return CRYPT_INVALID_ARG;
92 if (cipher_descriptor[xts->cipher].accel_xts_decrypt && lim > 0) {
94 /* use accelerated decryption for whole blocks */
95 if ((err = cipher_descriptor[xts->cipher].accel_xts_decrypt(ct, pt, lim, tweak, &xts->key1, &xts->key2)) !=
102 /* tweak is encrypted on output */
103 XMEMCPY(T, tweak, sizeof(T));
105 /* encrypt the tweak */
106 if ((err = cipher_descriptor[xts->cipher].ecb_encrypt(tweak, T, &xts->key2)) != CRYPT_OK) {
110 for (i = 0; i < lim; i++) {
111 if ((err = _tweak_uncrypt(ct, pt, T, xts)) != CRYPT_OK) {
119 /* if ptlen not divide 16 then */
124 /* PP = tweak decrypt block m-1 */
125 if ((err = _tweak_uncrypt(ct, PP, CC, xts)) != CRYPT_OK) {
129 /* Pm = first ptlen % 16 bytes of PP */
130 for (i = 0; i < mo; i++) {
134 for (; i < 16; i++) {
138 /* Pm-1 = Tweak uncrypt CC */
139 if ((err = _tweak_uncrypt(CC, pt, T, xts)) != CRYPT_OK) {
144 /* Decrypt the tweak back */
145 if ((err = cipher_descriptor[xts->cipher].ecb_decrypt(T, tweak, &xts->key2)) != CRYPT_OK) {
154 /* ref: $Format:%D$ */
155 /* git commit: $Format:%H$ */
156 /* commit time: $Format:%ai$ */