24#include <winpr/crypto.h>
25#include <winpr/print.h>
31static BOOL encrypt_aes128cbc(
const BYTE* key,
size_t keylen,
const BYTE* iv,
size_t ivlen,
32 const BYTE* in,
size_t inlen, BYTE* out,
size_t* outlen)
34 WINPR_CIPHER_CTX* ctx =
35 winpr_Cipher_NewEx(WINPR_CIPHER_AES_128_CBC, WINPR_ENCRYPT, key, keylen, iv, ivlen);
42 if (!winpr_Cipher_Update(ctx, in, inlen, out, &o))
45 if (!winpr_Cipher_Final(ctx, out + o, &f))
49 winpr_Cipher_Free(ctx);
53 winpr_Cipher_Free(ctx);
57int TestCryptoCipherIV(
int argc,
char* argv[])
60 const BYTE kat_key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
61 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
62 const BYTE kat_iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
63 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
64 const BYTE kat_pt[16] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
65 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a };
66 const BYTE kat_ct[16] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
67 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d };
82 if (!winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT))
84 (void)fprintf(stderr,
"%s: winpr_InitializeSSL failed\n", __func__);
88 memset(iv1, 0x11,
sizeof(iv1));
89 memset(iv2, 0x22,
sizeof(iv2));
91 if (!encrypt_aes128cbc(kat_key,
sizeof(kat_key), kat_iv,
sizeof(kat_iv), kat_pt,
sizeof(kat_pt),
93 !encrypt_aes128cbc(kat_key,
sizeof(kat_key), iv1,
sizeof(iv1), kat_pt,
sizeof(kat_pt), c1,
95 !encrypt_aes128cbc(kat_key,
sizeof(kat_key), iv2,
sizeof(iv2), kat_pt,
sizeof(kat_pt), c2,
98 (void)fprintf(stderr,
"%s: encryption failed\n", __func__);
102 if ((l_kat !=
sizeof(kat_ct)) || (memcmp(c_kat, kat_ct,
sizeof(kat_ct)) != 0))
104 (void)fprintf(stderr,
105 "%s: AES-128-CBC known-answer test failed - the IV is not honored by the "
111 if ((l1 != l2) || (memcmp(c1, c2, l1) == 0))
113 (void)fprintf(stderr,
114 "%s: ciphertexts with different IVs are identical - the IV is not honored "
115 "by the cipher backend\n",
120 (void)printf(
"%s: IV handling OK\n", __func__);