1 /*
2  * Copyright (c) 2001-2022, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "mbedtls/build_info.h"
8 
9 #if defined(MBEDTLS_POLY1305_C)
10 #include "mbedtls/poly1305.h"
11 #include "mbedtls/error.h"
12 #include "poly.h"
13 #include "mbedtls/platform_util.h"
14 #include "chacha_driver.h"
15 #include "cc_pal_abort.h"
16 #include "cc_pal_mem.h"
17 #include "cc_pal_types.h"
18 
19 #if defined(MBEDTLS_SELF_TEST)
20 #if defined(MBEDTLS_PLATFORM_C)
21 #include "mbedtls/platform.h"
22 #else
23 #define mbedtls_printf printf
24 #endif /* MBEDTLS_PLATFORM_C */
25 #endif /* MBEDTLS_SELF_TEST */
26 
27 #if defined(MBEDTLS_POLY1305_ALT)
28 
29 
mbedtls_poly1305_init(mbedtls_poly1305_context * ctx)30 void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx )
31 {
32     CC_UNUSED_PARAM(ctx);
33     return;
34 }
35 
mbedtls_poly1305_free(mbedtls_poly1305_context * ctx)36 void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx )
37 {
38     CC_UNUSED_PARAM(ctx);
39     return;
40 }
41 
42 /* Cryptocell only supports integrated poly1305 operations  */
mbedtls_poly1305_starts(mbedtls_poly1305_context * ctx,const unsigned char key[32])43 int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx,
44                              const unsigned char key[32] )
45 {
46     CC_UNUSED_PARAM(ctx);
47     CC_UNUSED_PARAM(key);
48     return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
49 }
50 
51 /* Cryptocell only supports integrated poly1305 operations  */
mbedtls_poly1305_update(mbedtls_poly1305_context * ctx,const unsigned char * input,size_t ilen)52 int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx,
53                              const unsigned char *input,
54                              size_t ilen )
55 {
56     CC_UNUSED_PARAM(ctx);
57     CC_UNUSED_PARAM(input);
58     CC_UNUSED_PARAM(ilen);
59     return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
60 }
61 
62 /* Cryptocell only supports integrated poly1305 operations  */
mbedtls_poly1305_finish(mbedtls_poly1305_context * ctx,unsigned char mac[16])63 int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx,
64                              unsigned char mac[16] )
65 {
66     CC_UNUSED_PARAM(ctx);
67     CC_UNUSED_PARAM(mac);
68     return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
69 }
70 
mbedtls_poly1305_mac(const unsigned char key[32],const unsigned char * input,size_t ilen,unsigned char mac[16])71 int mbedtls_poly1305_mac( const unsigned char key[32],
72                           const unsigned char *input,
73                           size_t ilen,
74                           unsigned char mac[16] )
75 {
76     int rc;
77     mbedtls_poly_key pKey;
78     mbedtls_poly_mac macRes;
79 
80     // Verify inputs
81     if (key == NULL) {
82         return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA;
83     }
84     if ((mac == NULL) || ((input == NULL) ^ (ilen == 0)) || (ilen > CC_MAX_UINT32_VAL)) {
85         return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA;
86     }
87 
88     CC_PalMemCopy((unsigned char *)pKey, key, sizeof(mbedtls_poly_key));
89 
90     rc = PolyMacCalc(pKey, NULL, 0, input, ilen, macRes, false);
91     if (rc != 0) {
92         return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
93     }
94 
95     CC_PalMemCopy(mac, (unsigned char *)macRes, sizeof(mbedtls_poly_mac));
96 
97     return ( 0 );
98 }
99 
100 
101 #endif /* !MBEDTLS_POLY1305_ALT */
102 
103 #endif /* !MBEDTLS_POLY1305_C */
104