1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "sm4_core.h"
5
rk_crypto_ofb128_encrypt(void * ctx,const unsigned char * in,unsigned char * out,int len,unsigned char * ivec,unsigned int * num,block128_f block)6 static void rk_crypto_ofb128_encrypt(void *ctx, const unsigned char *in, unsigned char *out,
7 int len, unsigned char *ivec, unsigned int *num,block128_f block)
8 {
9 int n, l=0;
10
11 n = *num;
12
13 while (l<len) {
14 if (n==0) {
15 (*block)(ivec, ivec, ctx);
16 }
17 out[l] = in[l] ^ ivec[n];
18 ++l;
19 n = (n+1) % SM4_BLOCK_SIZE;
20 }
21
22 *num=n;
23 }
24
25 /* XTS makes use of two different keys, usually generated by splitting
26 * the supplied block cipher's key in half.
27 * Because of the splitting, users wanting AES 256 and AES 128 encryption
28 * will need to choose key sizes of 512 bits and 256 bits respectively.
29 */
rk_sm4_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)30 int rk_sm4_ofb_encrypt(const unsigned char *in, unsigned char *out,
31 unsigned long length, const unsigned char *key, const int key_len,
32 unsigned char *ivec, const int enc)
33 {
34 sm4_context ctx;
35 unsigned int num = 0;
36
37 if(key_len != 16)
38 return -1;
39
40 if(length == 0)
41 return -1;
42
43 rk_sm4_setkey_enc(&ctx, key);
44 rk_crypto_ofb128_encrypt((void*)&ctx, in, out, length, ivec, &num, rk_rk_sm4_crypt_ecb);
45 return 0;
46 }
47
48
49