Linux プラットフォームで c 言語の openssl コードを使用して 3gpp テスト データを暗号化しています。スタック オーバー フローの例を試してみましたが、最終的な暗号化出力ではゼロが表示されません。128 ビット キーで暗号化する必要があります。前もって感謝します。
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <openssl/hmac.h>
#include <openssl/buffer.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define u32 unsigned int
#define u8 unsigned char
#define bufferSize 16
struct ctr_state
{
unsigned char ivec[AES_BLOCK_SIZE];
unsigned int num;
unsigned char ecount[AES_BLOCK_SIZE];
};
AES_KEY key;
unsigned char indata[AES_BLOCK_SIZE];
unsigned char outdata[AES_BLOCK_SIZE];
unsigned char iv[AES_BLOCK_SIZE];
struct ctr_state state;
int init_ctr(struct ctr_state *state, const unsigned char iv[16])
{
/* aes_ctr128_encrypt requires 'num' and 'ecount' set to zero on the
* first call. */
state->num = 0;
memset(state->ecount, 0, AES_BLOCK_SIZE);
/* Initialise counter in 'ivec' to 0 */
memset(state->ivec + 8, 0, 8);
/* Copy IV into 'ivec' */
memcpy(state->ivec, iv, 8);
}
int main(int argc, char *argv[])
{
u8 key1[16] = {
0xd3,0xc5,0xd5,0x92,0x32,0x7f,0xb1,0x1c,
0x40,0x35,0xc6,0x68,0x0a,0xf8,0xc6,0xd1
};
u8 count[4] ={0x39,0x8a,0x59,0xb4};
u32 tempCount = 0;
u8 bearer = 0x15;
u8 dir =0x01,i;
u32 length = 253 ;
u8 indata[32] = {
0x98, 0x1b, 0xa6, 0x82, 0x4c, 0x1b, 0xfb, 0x1a,
0xb4, 0x85, 0x47, 0x20, 0x29, 0xb7, 0x1d, 0x80,
0x8c, 0xe3, 0x3e, 0x2c, 0xc3, 0xc0, 0xb5, 0xfc,
0x1f, 0x3d, 0xe8, 0xa6, 0xdc, 0x66, 0xb1, 0xf0
};
tempCount = htonl((count[0] | (count[1] << 8) | count[2]<< 16 | count[3] << 24)); /* Jyothi Added */
iv[0] = (tempCount >> 24) & 0xff ;
iv[1] = (tempCount >> 16) & 0xff ;
iv[2] = (tempCount >> 8) & 0xff;
iv[3] = tempCount & 0xff;
iv[4] = htonl((( (bearer << 27) | ((dir & 0x1) << 26))));
iv[5] = iv[6]= iv[7] = 0;
printf("iv=\n");
for(i=0;i<16;i++)
printf("%x",iv[i]);
printf("\n");
printf("key1=\n");
for(i=0;i<16;i++)
printf("%x",key1[i]);
printf("\n");
//Initializing the encryption KEY
if (AES_set_encrypt_key(key1, 128, &key) < 0)
{
fprintf(stderr, "Could not set decryption key.");
exit(1);
}
init_ctr(&state, iv);//Counter call
printf("state.ivec after call=\n");
for(i=0;i<16;i++)
printf("%x",state.ivec[i]);
printf("\n");
printf("indata=\n");
for(i=0;i<32;i++)
printf("%x",indata[i]);
printf("\n");
for(i=1;i<2;i++){
//Encrypting Blocks of 16 bytes
AES_ctr128_encrypt(indata, outdata,253, &key, state.ivec, state.ecount, &state.num);
printf("outdata\n");
for(i=0;i<32;i++)
printf("%x",outdata[i]);
printf("\n");
}
}
私は以下のように出力を得ています。
iv=
398a59b4ac00000000000
key1=
d3c5d592327fb11c4035c668af8c6d1
state.ivec after call=
398a59b4ac00000000000
indata=
981ba6824c1bfb1ab485472029b71d808ce33e2cc3c0b5fc1f3de8a6dc66b1f0
outdata
e9fed8a63d15534d71df2bf3e82214b2ed7dad2f233dc3c22d7bdeeed8e78
algorithm has to generate key stream as below:
71e57e24 710ea81e 6398b52b da5f3f94 3eede9f6 11328620 231f3f1b 328b3f88
but instead it is generating final encryption output without zero's
final expected output is as below:
e9fed8a6 3d155304 d71df20b f3e82214 b20ed7da d2f233dc 3c22d7bd eeed8e78