blob: 453edfb13de7bdb27d7cbed725fb464f2da80d15 [file] [log] [blame]
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02001/*
2 * NIST SP800-38C compliant CCM implementation
3 *
4 * Copyright (C) 2014, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * Definition of CCM:
28 * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
29 * RFC 3610 "Counter with CBC-MAC (CCM)"
30 *
31 * Related:
32 * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
33 */
34
35#if !defined(POLARSSL_CONFIG_FILE)
36#include "polarssl/config.h"
37#else
38#include POLARSSL_CONFIG_FILE
39#endif
40
41#if defined(POLARSSL_CCM_C)
42
43#include "polarssl/ccm.h"
44
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020045/*
46 * Initialize context
47 */
48int ccm_init( ccm_context *ctx, cipher_id_t cipher,
49 const unsigned char *key, unsigned int keysize )
50{
51 int ret;
52 const cipher_info_t *cipher_info;
53
54 memset( ctx, 0, sizeof( ccm_context ) );
55
56 cipher_info = cipher_info_from_values( cipher, keysize, POLARSSL_MODE_ECB );
57 if( cipher_info == NULL )
58 return( POLARSSL_ERR_CCM_BAD_INPUT );
59
60 if( cipher_info->block_size != 16 )
61 return( POLARSSL_ERR_CCM_BAD_INPUT );
62
63 if( ( ret = cipher_init_ctx( &ctx->cipher_ctx, cipher_info ) ) != 0 )
64 return( ret );
65
66 if( ( ret = cipher_setkey( &ctx->cipher_ctx, key, keysize,
67 POLARSSL_ENCRYPT ) ) != 0 )
68 {
69 return( ret );
70 }
71
72 return( 0 );
73}
74
75/*
76 * Free context
77 */
78void ccm_free( ccm_context *ctx )
79{
80 (void) cipher_free_ctx( &ctx->cipher_ctx );
81 memset( ctx, 0, sizeof( ccm_context ) );
82}
83
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020084
85#if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C)
86
87#if defined(POLARSSL_PLATFORM_C)
88#include "polarssl/platform.h"
89#else
90#define polarssl_printf printf
91#endif
92
93int ccm_self_test( int verbose )
94{
95 if( verbose != 0 )
96 polarssl_printf( " CCM: skip\n" );
97
98 if( verbose != 0 )
99 polarssl_printf( "\n" );
100
101 return( 0 );
102}
103
104#endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */
105
106#endif /* POLARSSL_CCM_C */