736d6f0f1fff861e31dff9d2d61cfcc6e5fc7871
[strongswan.git] / Source / charon / testcases / aes_cbc_crypter_test.c
1 /**
2 * @file aes_cbc_crypter_test.c
3 *
4 * @brief Tests the aes_cbc_crypter_t class.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <string.h>
24
25 #include "aes_cbc_crypter_test.h"
26
27 #include <utils/allocator.h>
28 #include <globals.h>
29
30 void test_aes_cbc_crypter(tester_t *tester)
31 {
32 /*
33 * Test 1 of RFC3602
34 * Key : 0x06a9214036b8a15b512e03d534120006
35 * IV : 0x3dafba429d9eb430b422da802c9fac41
36 * Plaintext : "Single block msg"
37 * Ciphertext: 0xe353779c1079aeb82708942dbe77181a
38 */
39 crypter_t *crypter;
40 u_int8_t key1[] = {0x06,0xa9,0x21,0x40,0x36,0xb8,0xa1,0x5b,
41 0x51,0x2e,0x03,0xd5,0x34,0x12,0x00,0x06};
42 chunk_t key1_chunk = {ptr: key1, len : 16};
43 u_int8_t iv1[] = {0x3d,0xaf,0xba,0x42,0x9d,0x9e,0xb4,0x30,
44 0xb4,0x22,0xda,0x80,0x2c,0x9f,0xac,0x41};
45 chunk_t iv1_chunk = {ptr: iv1, len : 16};
46 u_int8_t ciphertext1[] = { 0xe3,0x53,0x77,0x9c,0x10,0x79,0xae,0xb8,
47 0x27,0x08,0x94,0x2d,0xbe,0x77,0x18,0x1a};
48
49 chunk_t expected_encrypted1 = {ptr: ciphertext1, len : 16};
50 char * plaintext1 = "Single block msg";
51 chunk_t data1 = {ptr: plaintext1, len : 16};
52 chunk_t encrypted1;
53 chunk_t decrypted1;
54 logger_t *logger;
55
56 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"AES CBC");
57
58 crypter = (crypter_t *) aes_cbc_crypter_create(16);
59 tester->assert_true(tester, (crypter != NULL), "create call test");
60
61 tester->assert_true(tester, (crypter->set_key(crypter,key1_chunk) == SUCCESS), "set_key call test");
62
63 tester->assert_true(tester, (crypter->encrypt(crypter,data1,iv1_chunk,&encrypted1) == SUCCESS), "encrypt call test");
64
65 tester->assert_true(tester, (memcmp(encrypted1.ptr, expected_encrypted1.ptr, 16) == 0), "Encrypted value");
66
67 logger->log_chunk(logger,RAW,"exptected encrypted :", &expected_encrypted1);
68 logger->log_chunk(logger,RAW,"encrypted :", &encrypted1);
69
70 allocator_free_chunk(&encrypted1);
71
72 tester->assert_true(tester, (crypter->decrypt(crypter,data1,iv1_chunk,&decrypted1) == SUCCESS), "decrypt call test");
73
74 tester->assert_true(tester, (memcmp(decrypted1.ptr, plaintext1, 16) == 0), "decrypted value");
75
76 logger->log_chunk(logger,RAW,"expected decrypted :", &data1);
77 logger->log_chunk(logger,RAW,"decrypted :", &decrypted1);
78
79 allocator_free_chunk(&encrypted1);
80
81 tester->assert_true(tester, (crypter->destroy(crypter) == SUCCESS), "destroy call test");
82
83
84
85 /*
86 * Test 2 of RFC3602
87 * Key : 0xc286696d887c9aa0611bbb3e2025a45a
88 * IV : 0x562e17996d093d28ddb3ba695a2e6f58
89 * Plaintext : 0x000102030405060708090a0b0c0d0e0f
90 * 101112131415161718191a1b1c1d1e1f
91 * Ciphertext: 0xd296cd94c2cccf8a3a863028b5e1dc0a
92 * 7586602d253cfff91b8266bea6d61ab1
93 */
94 u_int8_t key2[] = {0xc2,0x86,0x69,0x6d,0x88,0x7c,0x9a,0xa0,
95 0x61,0x1b,0xbb,0x3e,0x20,0x25,0xa4,0x5a};
96 chunk_t key2_chunk = {ptr: key2, len : 16};
97 u_int8_t iv2[] = {0x56,0x2e,0x17,0x99,0x6d,0x09,0x3d,0x28,
98 0xdd,0xb3,0xba,0x69,0x5a,0x2e,0x6f,0x58};
99 chunk_t iv2_chunk = {ptr: iv2, len : 16};
100 u_int8_t ciphertext2[] = { 0xd2,0x96,0xcd,0x94,0xc2,0xcc,0xcf,0x8a,
101 0x3a,0x86,0x30,0x28,0xb5,0xe1,0xdc,0x0a,
102 0x75,0x86,0x60,0x2d,0x25,0x3c,0xff,0xf9,
103 0x1b,0x82,0x66,0xbe,0xa6,0xd6,0x1a,0xb1};
104
105 chunk_t expected_encrypted2 = {ptr: ciphertext2, len : 32};
106 u_int8_t plaintext2[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
107 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
108 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
109 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f};
110 chunk_t data2 = {ptr: plaintext2, len : 32};
111 chunk_t encrypted2;
112 chunk_t decrypted2;
113
114 crypter = (crypter_t *) aes_cbc_crypter_create(16);
115 tester->assert_true(tester, (crypter != NULL), "create call test");
116
117 tester->assert_true(tester, (crypter->set_key(crypter,key2_chunk) == SUCCESS), "set_key call test");
118
119 tester->assert_true(tester, (crypter->encrypt(crypter,data2,iv2_chunk,&encrypted2) == SUCCESS), "encrypt call test");
120
121 tester->assert_true(tester, (memcmp(encrypted2.ptr, expected_encrypted2.ptr, 26) == 0), "Encrypted value");
122
123 logger->log_chunk(logger,RAW,"exptected encrypted :", &expected_encrypted2);
124 logger->log_chunk(logger,RAW,"encrypted :", &encrypted2);
125
126 allocator_free_chunk(&encrypted2);
127
128 tester->assert_true(tester, (crypter->decrypt(crypter,data2,iv2_chunk,&decrypted2) == SUCCESS), "decrypt call test");
129
130 tester->assert_true(tester, (memcmp(decrypted2.ptr, plaintext2, 26) == 0), "decrypted value");
131
132 logger->log_chunk(logger,RAW,"expected decrypted :", &data2);
133 logger->log_chunk(logger,RAW,"decrypted :", &decrypted2);
134
135 allocator_free_chunk(&encrypted2);
136
137 tester->assert_true(tester, (crypter->destroy(crypter) == SUCCESS), "destroy call test");
138
139
140
141 global_logger_manager->destroy_logger(global_logger_manager,logger);
142 }
143