summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/cts128/cts128test.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libcrypto/cts128/cts128test.c')
-rw-r--r--src/regress/lib/libcrypto/cts128/cts128test.c160
1 files changed, 160 insertions, 0 deletions
diff --git a/src/regress/lib/libcrypto/cts128/cts128test.c b/src/regress/lib/libcrypto/cts128/cts128test.c
new file mode 100644
index 0000000000..84a3e0d46b
--- /dev/null
+++ b/src/regress/lib/libcrypto/cts128/cts128test.c
@@ -0,0 +1,160 @@
1/* ====================================================================
2 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
3 *
4 * Rights for redistribution and usage in source and binary
5 * forms are granted according to the OpenSSL license.
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include <openssl/aes.h>
13#include <openssl/modes.h>
14
15/* test vectors from RFC 3962 */
16static const unsigned char test_key[16] = "chicken teriyaki";
17static const unsigned char test_input[64] =
18 "I would like the" " General Gau's C"
19 "hicken, please, " "and wonton soup.";
20static const unsigned char test_iv[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
21
22static const unsigned char vector_17[17] =
23{0xc6,0x35,0x35,0x68,0xf2,0xbf,0x8c,0xb4, 0xd8,0xa5,0x80,0x36,0x2d,0xa7,0xff,0x7f,
24 0x97};
25static const unsigned char vector_31[31] =
26{0xfc,0x00,0x78,0x3e,0x0e,0xfd,0xb2,0xc1, 0xd4,0x45,0xd4,0xc8,0xef,0xf7,0xed,0x22,
27 0x97,0x68,0x72,0x68,0xd6,0xec,0xcc,0xc0, 0xc0,0x7b,0x25,0xe2,0x5e,0xcf,0xe5};
28static const unsigned char vector_32[32] =
29{0x39,0x31,0x25,0x23,0xa7,0x86,0x62,0xd5, 0xbe,0x7f,0xcb,0xcc,0x98,0xeb,0xf5,0xa8,
30 0x97,0x68,0x72,0x68,0xd6,0xec,0xcc,0xc0, 0xc0,0x7b,0x25,0xe2,0x5e,0xcf,0xe5,0x84};
31static const unsigned char vector_47[47] =
32{0x97,0x68,0x72,0x68,0xd6,0xec,0xcc,0xc0, 0xc0,0x7b,0x25,0xe2,0x5e,0xcf,0xe5,0x84,
33 0xb3,0xff,0xfd,0x94,0x0c,0x16,0xa1,0x8c, 0x1b,0x55,0x49,0xd2,0xf8,0x38,0x02,0x9e,
34 0x39,0x31,0x25,0x23,0xa7,0x86,0x62,0xd5, 0xbe,0x7f,0xcb,0xcc,0x98,0xeb,0xf5};
35static const unsigned char vector_48[48] =
36{0x97,0x68,0x72,0x68,0xd6,0xec,0xcc,0xc0, 0xc0,0x7b,0x25,0xe2,0x5e,0xcf,0xe5,0x84,
37 0x9d,0xad,0x8b,0xbb,0x96,0xc4,0xcd,0xc0, 0x3b,0xc1,0x03,0xe1,0xa1,0x94,0xbb,0xd8,
38 0x39,0x31,0x25,0x23,0xa7,0x86,0x62,0xd5, 0xbe,0x7f,0xcb,0xcc,0x98,0xeb,0xf5,0xa8};
39static const unsigned char vector_64[64] =
40{0x97,0x68,0x72,0x68,0xd6,0xec,0xcc,0xc0, 0xc0,0x7b,0x25,0xe2,0x5e,0xcf,0xe5,0x84,
41 0x39,0x31,0x25,0x23,0xa7,0x86,0x62,0xd5, 0xbe,0x7f,0xcb,0xcc,0x98,0xeb,0xf5,0xa8,
42 0x48,0x07,0xef,0xe8,0x36,0xee,0x89,0xa5, 0x26,0x73,0x0d,0xbc,0x2f,0x7b,0xc8,0x40,
43 0x9d,0xad,0x8b,0xbb,0x96,0xc4,0xcd,0xc0, 0x3b,0xc1,0x03,0xe1,0xa1,0x94,0xbb,0xd8};
44
45static AES_KEY encks, decks;
46
47void test_vector(const unsigned char *vector,size_t len)
48{ unsigned char iv[sizeof(test_iv)];
49 unsigned char cleartext[64],ciphertext[64];
50 size_t tail;
51
52 printf("vector_%d\n",len); fflush(stdout);
53
54 if ((tail=len%16) == 0) tail = 16;
55 tail += 16;
56
57 /* test block-based encryption */
58 memcpy(iv,test_iv,sizeof(test_iv));
59 CRYPTO_cts128_encrypt_block(test_input,ciphertext,len,&encks,iv,(block128_f)AES_encrypt);
60 if (memcmp(ciphertext,vector,len))
61 fprintf(stderr,"output_%d mismatch\n",len), exit(1);
62 if (memcmp(iv,vector+len-tail,sizeof(iv)))
63 fprintf(stderr,"iv_%d mismatch\n",len), exit(1);
64
65 /* test block-based decryption */
66 memcpy(iv,test_iv,sizeof(test_iv));
67 CRYPTO_cts128_decrypt_block(ciphertext,cleartext,len,&decks,iv,(block128_f)AES_decrypt);
68 if (memcmp(cleartext,test_input,len))
69 fprintf(stderr,"input_%d mismatch\n",len), exit(2);
70 if (memcmp(iv,vector+len-tail,sizeof(iv)))
71 fprintf(stderr,"iv_%d mismatch\n",len), exit(2);
72
73 /* test streamed encryption */
74 memcpy(iv,test_iv,sizeof(test_iv));
75 CRYPTO_cts128_encrypt(test_input,ciphertext,len,&encks,iv,(cbc128_f)AES_cbc_encrypt);
76 if (memcmp(ciphertext,vector,len))
77 fprintf(stderr,"output_%d mismatch\n",len), exit(3);
78 if (memcmp(iv,vector+len-tail,sizeof(iv)))
79 fprintf(stderr,"iv_%d mismatch\n",len), exit(3);
80
81 /* test streamed decryption */
82 memcpy(iv,test_iv,sizeof(test_iv));
83 CRYPTO_cts128_decrypt(ciphertext,cleartext,len,&decks,iv,(cbc128_f)AES_cbc_encrypt);
84 if (memcmp(cleartext,test_input,len))
85 fprintf(stderr,"input_%d mismatch\n",len), exit(4);
86 if (memcmp(iv,vector+len-tail,sizeof(iv)))
87 fprintf(stderr,"iv_%d mismatch\n",len), exit(4);
88}
89
90void test_nistvector(const unsigned char *vector,size_t len)
91{ unsigned char iv[sizeof(test_iv)];
92 unsigned char cleartext[64],ciphertext[64],nistvector[64];
93 size_t tail;
94
95 printf("nistvector_%d\n",len); fflush(stdout);
96
97 if ((tail=len%16) == 0) tail = 16;
98
99 len -= 16 + tail;
100 memcpy(nistvector,vector,len);
101 /* flip two last blocks */
102 memcpy(nistvector+len,vector+len+16,tail);
103 memcpy(nistvector+len+tail,vector+len,16);
104 len += 16 + tail;
105 tail = 16;
106
107 /* test block-based encryption */
108 memcpy(iv,test_iv,sizeof(test_iv));
109 CRYPTO_nistcts128_encrypt_block(test_input,ciphertext,len,&encks,iv,(block128_f)AES_encrypt);
110 if (memcmp(ciphertext,nistvector,len))
111 fprintf(stderr,"output_%d mismatch\n",len), exit(1);
112 if (memcmp(iv,nistvector+len-tail,sizeof(iv)))
113 fprintf(stderr,"iv_%d mismatch\n",len), exit(1);
114
115 /* test block-based decryption */
116 memcpy(iv,test_iv,sizeof(test_iv));
117 CRYPTO_nistcts128_decrypt_block(ciphertext,cleartext,len,&decks,iv,(block128_f)AES_decrypt);
118 if (memcmp(cleartext,test_input,len))
119 fprintf(stderr,"input_%d mismatch\n",len), exit(2);
120 if (memcmp(iv,nistvector+len-tail,sizeof(iv)))
121 fprintf(stderr,"iv_%d mismatch\n",len), exit(2);
122
123 /* test streamed encryption */
124 memcpy(iv,test_iv,sizeof(test_iv));
125 CRYPTO_nistcts128_encrypt(test_input,ciphertext,len,&encks,iv,(cbc128_f)AES_cbc_encrypt);
126 if (memcmp(ciphertext,nistvector,len))
127 fprintf(stderr,"output_%d mismatch\n",len), exit(3);
128 if (memcmp(iv,nistvector+len-tail,sizeof(iv)))
129 fprintf(stderr,"iv_%d mismatch\n",len), exit(3);
130
131 /* test streamed decryption */
132 memcpy(iv,test_iv,sizeof(test_iv));
133 CRYPTO_nistcts128_decrypt(ciphertext,cleartext,len,&decks,iv,(cbc128_f)AES_cbc_encrypt);
134 if (memcmp(cleartext,test_input,len))
135 fprintf(stderr,"input_%d mismatch\n",len), exit(4);
136 if (memcmp(iv,nistvector+len-tail,sizeof(iv)))
137 fprintf(stderr,"iv_%d mismatch\n",len), exit(4);
138}
139
140int main()
141{
142 AES_set_encrypt_key(test_key,128,&encks);
143 AES_set_decrypt_key(test_key,128,&decks);
144
145 test_vector(vector_17,sizeof(vector_17));
146 test_vector(vector_31,sizeof(vector_31));
147 test_vector(vector_32,sizeof(vector_32));
148 test_vector(vector_47,sizeof(vector_47));
149 test_vector(vector_48,sizeof(vector_48));
150 test_vector(vector_64,sizeof(vector_64));
151
152 test_nistvector(vector_17,sizeof(vector_17));
153 test_nistvector(vector_31,sizeof(vector_31));
154 test_nistvector(vector_32,sizeof(vector_32));
155 test_nistvector(vector_47,sizeof(vector_47));
156 test_nistvector(vector_48,sizeof(vector_48));
157 test_nistvector(vector_64,sizeof(vector_64));
158
159 return 0;
160}