1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include "des_core.h"
6
rk_crypto_ofb128_encrypt(void * ctx,const unsigned char * in,unsigned char * out,int len,unsigned char * ivec,unsigned int * num,block128_f block)7 static void rk_crypto_ofb128_encrypt(void *ctx, const unsigned char *in, unsigned char *out,
8 int len, unsigned char *ivec, unsigned int *num,block128_f block)
9 {
10 int n, l=0;
11
12 n = *num;
13
14 while (l<len) {
15 if (n==0) {
16 (*block)(ivec, ivec, ctx);
17 }
18 out[l] = in[l] ^ ivec[n];
19 ++l;
20 n = (n+1) % DES_BLOCK_SIZE;
21 }
22
23 *num=n;
24 }
25
26 /* XTS makes use of two different keys, usually generated by splitting
27 * the supplied block cipher's key in half.
28 * Because of the splitting, users wanting AES 256 and AES 128 encryption
29 * will need to choose key sizes of 512 bits and 256 bits respectively.
30 */
rk_des_ofb_encrypt(const unsigned char * in,unsigned char * out,unsigned long length,const unsigned char * key,const int key_len,unsigned char * ivec,const int enc)31 int rk_des_ofb_encrypt(const unsigned char *in, unsigned char *out,
32 unsigned long length, const unsigned char *key, const int key_len,
33 unsigned char *ivec, const int enc)
34 {
35 rk_des_context ctx;
36 rk_des3_context ctx3;
37 unsigned int num = 0;
38
39 if(key_len != 8 && key_len != 16 && key_len != 24)
40 return -1;
41
42 if(length == 0)
43 return -1;
44
45 switch(key_len){
46 case 8:
47 rk_des_setkey_enc(&ctx, key);
48 rk_crypto_ofb128_encrypt((void*)&ctx, in, out, length, ivec, &num, rk_des_crypt_ecb);
49 break;
50 case 16:
51 rk_des3_set2key_enc(&ctx3, key);
52 rk_crypto_ofb128_encrypt((void*)&ctx3, in, out, length, ivec, &num, rk_des3_crypt_ecb);
53 break;
54 case 24:
55 rk_des3_set3key_enc(&ctx3, key);
56 rk_crypto_ofb128_encrypt((void*)&ctx3, in, out, length, ivec, &num, rk_des3_crypt_ecb);
57 break;
58 default:
59 return -1;
60 }
61
62 return 0;
63 }
64
65