Implement FIPS CMAC.
[oweals/openssl.git] / fips / cmac / fips_cmac_selftest.c
1 /* ====================================================================
2  * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer. 
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the OpenSSL Project
19  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20  *
21  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For written permission, please contact
24  *    openssl-core@openssl.org.
25  *
26  * 5. Products derived from this software may not be called "OpenSSL"
27  *    nor may "OpenSSL" appear in their names without prior written
28  *    permission of the OpenSSL Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  *
48  */
49
50 #define OPENSSL_FIPSAPI
51
52 #include <string.h>
53 #include <openssl/err.h>
54 #include <openssl/fips.h>
55 #include <openssl/cmac.h>
56
57 #ifdef OPENSSL_FIPS
58 typedef struct {
59         const EVP_CIPHER *(*alg)(void);
60         const unsigned char key[EVP_MAX_KEY_LENGTH]; int keysize;
61         const unsigned char msg[64]; int msgsize;
62         const unsigned char mac[32]; int macsize;
63 } CMAC_KAT;
64
65 /* from http://csrc.nist.gov/publications/nistpubs/800-38B/SP_800-38B.pdf */
66 static const CMAC_KAT vector[] = {
67     {   EVP_aes_128_cbc,        /* Example 3: Mlen = 320 */
68         { 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
69           0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c }, 128,
70         { 0x6b,0xc1,0xbe,0xe2, 0x2e,0x40,0x9f,0x96,
71           0xe9,0x3d,0x7e,0x11, 0x73,0x93,0x17,0x2a,
72           0xae,0x2d,0x8a,0x57, 0x1e,0x03,0xac,0x9c,
73           0x9e,0xb7,0x6f,0xac, 0x45,0xaf,0x8e,0x51,
74           0x30,0xc8,0x1c,0x46, 0xa3,0x5c,0xe4,0x11 }, 320,
75         { 0xdf,0xa6,0x67,0x47, 0xde,0x9a,0xe6,0x30,
76           0x30,0xca,0x32,0x61, 0x14,0x97,0xc8,0x27 }, 128
77     },
78     {   EVP_aes_192_cbc,        /* Example 5: Mlen = 0 */
79         { 0x8e,0x73,0xb0,0xf7, 0xda,0x0e,0x64,0x52,
80           0xc8,0x10,0xf3,0x2b, 0x80,0x90,0x79,0xe5,
81           0x62,0xf8,0xea,0xd2, 0x52,0x2c,0x6b,0x7b, }, 192,
82         { 0x0 }, 0,
83         { 0xd1,0x7d,0xdf,0x46, 0xad,0xaa,0xcd,0xe5,
84           0x31,0xca,0xc4,0x83, 0xde,0x7a,0x93,0x67, }, 128
85     },
86     {   EVP_aes_256_cbc,        /* Example 12: Mlen = 512 */
87         { 0x60,0x3d,0xeb,0x10, 0x15,0xca,0x71,0xbe,
88           0x2b,0x73,0xae,0xf0, 0x85,0x7d,0x77,0x81,
89           0x1f,0x35,0x2c,0x07, 0x3b,0x61,0x08,0xd7,
90           0x2d,0x98,0x10,0xa3, 0x09,0x14,0xdf,0xf4, }, 256,
91         { 0x6b,0xc1,0xbe,0xe2, 0x2e,0x40,0x9f,0x96,
92           0xe9,0x3d,0x7e,0x11, 0x73,0x93,0x17,0x2a,
93           0xae,0x2d,0x8a,0x57, 0x1e,0x03,0xac,0x9c,
94           0x9e,0xb7,0x6f,0xac, 0x45,0xaf,0x8e,0x51,
95           0x30,0xc8,0x1c,0x46, 0xa3,0x5c,0xe4,0x11,
96           0xe5,0xfb,0xc1,0x19, 0x1a,0x0a,0x52,0xef,
97           0xf6,0x9f,0x24,0x45, 0xdf,0x4f,0x9b,0x17,
98           0xad,0x2b,0x41,0x7b, 0xe6,0x6c,0x37,0x10, }, 512,
99         { 0xe1,0x99,0x21,0x90, 0x54,0x9f,0x6e,0xd5,
100           0x69,0x6a,0x2c,0x05, 0x6c,0x31,0x54,0x10, }, 128,
101     },
102 # if 0
103     /* Removed because the actual result was:
104           0x74,0x3d,0xdb,0xe0,  0xce,0x2d,0xc2,0xed
105        I suspect an error on my part -- Richard Levitte
106      */
107     {   EVP_des_ede3_cbc,       /* Example 15: Mlen = 160 */
108         { 0x8a,0xa8,0x3b,0xf8, 0xcb,0xda,0x10,0x62,
109           0x0b,0xc1,0xbf,0x19, 0xfb,0xb6,0xcd,0x58,
110           0xbc,0x31,0x3d,0x4a, 0x37,0x1c,0xa8,0xb5, }, 192,
111         { 0x6b,0xc1,0xbe,0xe2, 0x2e,0x40,0x9f,0x96,
112           0xe9,0x3d,0x7e,0x11, 0x73,0x93,0x17,0x2a,
113           0xae,0x2d,0x8a,0x57, }, 160,
114         { 0xd3,0x2b,0xce,0xbe, 0x43,0xd2,0x3d,0x80, }, 64,
115     },
116 # endif
117 };
118
119 int FIPS_selftest_cmac()
120     {
121     size_t n;
122     unsigned int     outlen;
123     unsigned char    out[32];
124     const EVP_CIPHER *cipher;
125     CMAC_CTX *ctx = CMAC_CTX_new();
126     const CMAC_KAT *t;
127
128     for(n=0,t=vector; n<sizeof(vector)/sizeof(vector[0]); n++,t++)
129         {
130         cipher = (*t->alg)();
131         CMAC_Init(ctx, t->key, t->keysize/8, cipher, 0);
132         CMAC_Update(ctx, t->msg, t->msgsize/8);
133         CMAC_Final(ctx, out, &outlen);
134         CMAC_CTX_cleanup(ctx);
135
136         if(outlen != t->macsize/8 || memcmp(out,t->mac,outlen))
137             {
138             FIPSerr(FIPS_F_FIPS_SELFTEST_CMAC,FIPS_R_SELFTEST_FAILED);
139             return 0;
140             }
141         }
142
143     CMAC_CTX_free(ctx);
144     return 1;
145     }
146 #endif