1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /*
8  * Definition of CCM:
9  * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
10  * RFC 3610 "Counter with CBC-MAC (CCM)"
11  *
12  * Related:
13  * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
14  */
15 #include "ccm_alt.h"
16 
17 #if defined(MBEDTLS_CCM_C) && defined (MBEDTLS_CCM_ALT)
18 
19 #include "cc_pal_types.h"
20 #include "mbedtls_common.h"
21 
22 #include "mbedtls_ccm_internal.h"
23 #include "mbedtls_ccm_common.h"
24 
25 #include "mbedtls/error.h"
26 
27 /************************ Public Functions **********************/
28 /*
29  * Initialize context
30  */
mbedtls_ccm_init(mbedtls_ccm_context * ctx)31 void mbedtls_ccm_init( mbedtls_ccm_context *ctx )
32 {
33     mbedtls_ccm_init_int(ctx);
34 }
35 
mbedtls_ccm_setkey(mbedtls_ccm_context * ctx,mbedtls_cipher_id_t cipher,const unsigned char * key,unsigned int keybits)36 int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
37                         mbedtls_cipher_id_t cipher,
38                         const unsigned char *key,
39                         unsigned int keybits)
40 {
41     return mbedtls_ccm_setkey_int(ctx, cipher, key, keybits);
42 }
43 
44 /*
45  * Free context
46  */
mbedtls_ccm_free(mbedtls_ccm_context * ctx)47 void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
48 {
49     mbedtls_ccm_free_int(ctx);
50 }
51 
52 /*
53  * Authenticated encryption
54  */
mbedtls_ccm_encrypt_and_tag(mbedtls_ccm_context * ctx,size_t length,const unsigned char * iv,size_t iv_len,const unsigned char * add,size_t add_len,const unsigned char * input,unsigned char * output,unsigned char * tag,size_t tag_len)55 int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx,
56                                  size_t length,
57                                  const unsigned char *iv,
58                                  size_t iv_len,
59                                  const unsigned char *add,
60                                  size_t add_len,
61                                  const unsigned char *input,
62                                  unsigned char *output,
63                                  unsigned char *tag,
64                                  size_t tag_len )
65 {
66     return mbedtls_ccm_encrypt_and_tag_int(ctx, length, iv, iv_len, add, add_len, input, output, tag, tag_len, MBEDTLS_AESCCM_MODE_CCM);
67 }
68 
69 /*
70  * Authenticated decryption
71  */
mbedtls_ccm_auth_decrypt(mbedtls_ccm_context * ctx,size_t length,const unsigned char * iv,size_t iv_len,const unsigned char * add,size_t add_len,const unsigned char * input,unsigned char * output,const unsigned char * tag,size_t tag_len)72 int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx,
73                               size_t length,
74                               const unsigned char *iv,
75                               size_t iv_len,
76                               const unsigned char *add,
77                               size_t add_len,
78                               const unsigned char *input,
79                               unsigned char *output,
80                               const unsigned char *tag,
81                               size_t tag_len )
82 {
83 
84 
85     return mbedtls_ccm_auth_decrypt_int( ctx, length, iv, iv_len, add, add_len, input, output, tag, tag_len, MBEDTLS_AESCCM_MODE_CCM);
86 }
87 
mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context * ctx,size_t length,const unsigned char * iv,size_t iv_len,const unsigned char * add,size_t add_len,const unsigned char * input,unsigned char * output,unsigned char * tag,size_t tag_len)88 int mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context *ctx,
89                                      size_t length,
90                                      const unsigned char *iv,
91                                      size_t iv_len,
92                                      const unsigned char *add,
93                                      size_t add_len,
94                                      const unsigned char *input,
95                                      unsigned char *output,
96                                      unsigned char *tag,
97                                      size_t tag_len)
98 {
99     return mbedtls_ccm_encrypt_and_tag_int(ctx, length, iv, iv_len, add, add_len, input, output, tag, tag_len, MBEDTLS_AESCCM_MODE_STAR);
100 }
101 
mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context * ctx,size_t length,const unsigned char * iv,size_t iv_len,const unsigned char * add,size_t add_len,const unsigned char * input,unsigned char * output,const unsigned char * tag,size_t tag_len)102 int mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context *ctx,
103                                   size_t length,
104                                   const unsigned char *iv,
105                                   size_t iv_len,
106                                   const unsigned char *add,
107                                   size_t add_len,
108                                   const unsigned char *input,
109                                   unsigned char *output,
110                                   const unsigned char *tag,
111                                   size_t tag_len)
112 {
113     return mbedtls_ccm_auth_decrypt_int(ctx, length, iv, iv_len, add, add_len, input, output, tag, tag_len, MBEDTLS_AESCCM_MODE_STAR);
114 
115 }
116 
mbedtls_ccm_starts(mbedtls_ccm_context * ctx,int mode,const unsigned char * iv,size_t iv_len)117 int mbedtls_ccm_starts( mbedtls_ccm_context *ctx,
118                         int mode,
119                         const unsigned char *iv,
120                         size_t iv_len )
121 {
122     CC_UNUSED_PARAM(ctx);
123     CC_UNUSED_PARAM(mode);
124     CC_UNUSED_PARAM(iv);
125     CC_UNUSED_PARAM(iv_len);
126 
127     return (MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED);
128 }
129 
mbedtls_ccm_set_lengths(mbedtls_ccm_context * ctx,size_t total_ad_len,size_t plaintext_len,size_t tag_len)130 int mbedtls_ccm_set_lengths( mbedtls_ccm_context *ctx,
131                              size_t total_ad_len,
132                              size_t plaintext_len,
133                              size_t tag_len )
134 {
135     CC_UNUSED_PARAM(ctx);
136     CC_UNUSED_PARAM(total_ad_len);
137     CC_UNUSED_PARAM(plaintext_len);
138     CC_UNUSED_PARAM(tag_len);
139 
140     return (MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED);
141 }
142 
mbedtls_ccm_update_ad(mbedtls_ccm_context * ctx,const unsigned char * ad,size_t ad_len)143 int mbedtls_ccm_update_ad( mbedtls_ccm_context *ctx,
144                            const unsigned char *ad,
145                            size_t ad_len )
146 {
147     CC_UNUSED_PARAM(ctx);
148     CC_UNUSED_PARAM(ad);
149     CC_UNUSED_PARAM(ad_len);
150 
151     return (MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED);
152 }
153 
mbedtls_ccm_update(mbedtls_ccm_context * ctx,const unsigned char * input,size_t input_len,unsigned char * output,size_t output_size,size_t * output_len)154 int mbedtls_ccm_update( mbedtls_ccm_context *ctx,
155                         const unsigned char *input, size_t input_len,
156                         unsigned char *output, size_t output_size,
157                         size_t *output_len )
158 {
159     CC_UNUSED_PARAM(ctx);
160     CC_UNUSED_PARAM(input);
161     CC_UNUSED_PARAM(input_len);
162     CC_UNUSED_PARAM(output);
163     CC_UNUSED_PARAM(output_size);
164     CC_UNUSED_PARAM(output_len);
165 
166     return (MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED);
167 }
168 
mbedtls_ccm_finish(mbedtls_ccm_context * ctx,unsigned char * tag,size_t tag_len)169 int mbedtls_ccm_finish( mbedtls_ccm_context *ctx,
170                         unsigned char *tag, size_t tag_len )
171 {
172     CC_UNUSED_PARAM(ctx);
173     CC_UNUSED_PARAM(tag);
174     CC_UNUSED_PARAM(tag_len);
175 
176     return (MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED);
177 }
178 
179 #endif /* defined(MBEDTLS_CCM_C) && defined (MBEDTLS_CCM_ALT) */
180