typo
[openssl.git] / demos / evp / aesccm.c
1 /* Simple AES CCM test program, uses the same NIST data used for the FIPS
2  * self test but uses the application level EVP APIs.
3  */
4 #include <stdio.h>
5 #include <openssl/bio.h>
6 #include <openssl/evp.h>
7
8 /* AES-CCM test data from NIST public test vectors */
9
10 static const unsigned char ccm_key[] = {
11         0xce,0xb0,0x09,0xae,0xa4,0x45,0x44,0x51,0xfe,0xad,0xf0,0xe6,
12         0xb3,0x6f,0x45,0x55,0x5d,0xd0,0x47,0x23,0xba,0xa4,0x48,0xe8
13 };
14
15 static const unsigned char ccm_nonce[] = {
16         0x76,0x40,0x43,0xc4,0x94,0x60,0xb7
17 };
18
19 static const unsigned char ccm_adata[] = {
20         0x6e,0x80,0xdd,0x7f,0x1b,0xad,0xf3,0xa1,0xc9,0xab,0x25,0xc7,
21         0x5f,0x10,0xbd,0xe7,0x8c,0x23,0xfa,0x0e,0xb8,0xf9,0xaa,0xa5,
22         0x3a,0xde,0xfb,0xf4,0xcb,0xf7,0x8f,0xe4
23 };
24
25 static const unsigned char ccm_pt[] = {
26         0xc8,0xd2,0x75,0xf9,0x19,0xe1,0x7d,0x7f,0xe6,0x9c,0x2a,0x1f,
27         0x58,0x93,0x9d,0xfe,0x4d,0x40,0x37,0x91,0xb5,0xdf,0x13,0x10
28 };
29
30 static const unsigned char ccm_ct[] = {
31         0x8a,0x0f,0x3d,0x82,0x29,0xe4,0x8e,0x74,0x87,0xfd,0x95,0xa2,
32         0x8a,0xd3,0x92,0xc8,0x0b,0x36,0x81,0xd4,0xfb,0xc7,0xbb,0xfd
33 };
34
35 static const unsigned char ccm_tag[] = {
36         0x2d,0xd6,0xef,0x1c,0x45,0xd4,0xcc,0xb7,0x23,0xdc,0x07,0x44,
37         0x14,0xdb,0x50,0x6d
38 };
39
40 void aes_ccm_encrypt(void)
41         {
42         EVP_CIPHER_CTX *ctx;
43         int outlen, tmplen;
44         unsigned char outbuf[1024];
45         printf("AES CCM Encrypt:\n");
46         printf("Plaintext:\n");
47         BIO_dump_fp(stdout, ccm_pt, sizeof(ccm_pt));
48         ctx = EVP_CIPHER_CTX_new();
49         /* Set cipher type and mode */
50         EVP_EncryptInit_ex(ctx, EVP_aes_192_ccm(), NULL, NULL, NULL);
51         /* Set nonce length if default 96 bits is not appropriate */
52         EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, sizeof(ccm_nonce), NULL);
53         /* Set tag length */
54         EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, sizeof(ccm_tag), NULL);
55         /* Initialise key and IV */
56         EVP_EncryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce);
57         /* Set plaintext length: only needed if AAD is used */
58         EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_pt));
59         /* Zero or one call to specify any AAD */
60         EVP_EncryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata));
61         /* Encrypt plaintext: can only be called once */
62         EVP_EncryptUpdate(ctx, outbuf, &outlen, ccm_pt, sizeof(ccm_pt));
63         /* Output encrypted block */
64         printf("Ciphertext:\n");
65         BIO_dump_fp(stdout, outbuf, outlen);
66         /* Finalise: note get no output for CCM */
67         EVP_EncryptFinal_ex(ctx, outbuf, &outlen);
68         /* Get tag */
69         EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_GET_TAG, 16, outbuf);
70         /* Output tag */
71         printf("Tag:\n");
72         BIO_dump_fp(stdout, outbuf, 16);
73         EVP_CIPHER_CTX_free(ctx);
74         }
75
76 void aes_ccm_decrypt(void)
77         {
78         EVP_CIPHER_CTX *ctx;
79         int outlen, tmplen, rv;
80         unsigned char outbuf[1024];
81         printf("AES CCM Derypt:\n");
82         printf("Ciphertext:\n");
83         BIO_dump_fp(stdout, ccm_ct, sizeof(ccm_ct));
84         ctx = EVP_CIPHER_CTX_new();
85         /* Select cipher */
86         EVP_DecryptInit_ex(ctx, EVP_aes_192_ccm(), NULL, NULL, NULL);
87         /* Set nonce length, omit for 96 bits */
88         EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, sizeof(ccm_nonce), NULL);
89         /* Set expected tag value */
90         EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG,
91                                         sizeof(ccm_tag), (void *)ccm_tag);
92         /* Specify key and IV */
93         EVP_DecryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce);
94         /* Set ciphertext length: only needed if we have AAD */
95         EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_ct));
96         /* Zero or one call to specify any AAD */
97         EVP_DecryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata));
98         /* Decrypt plaintext, verify tag: can only be called once */
99         rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, ccm_ct, sizeof(ccm_ct));
100         /* Output decrypted block: if tag verify failed we get nothing */
101         if (rv > 0)
102                 {
103                 printf("Plaintext:\n");
104                 BIO_dump_fp(stdout, outbuf, outlen);
105                 }
106         else
107                 printf("Plaintext not available: tag verify failed.\n");
108         EVP_CIPHER_CTX_free(ctx);
109         }
110
111 int main(int argc, char **argv)
112         {
113         aes_ccm_encrypt();
114         aes_ccm_decrypt();
115         }