|
| 1 | +/**************************************************************************** |
| 2 | + * apps/crypto/mbedtls/source/poly1305_alt.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + ****************************************************************************/ |
| 19 | + |
| 20 | +/**************************************************************************** |
| 21 | + * Included Files |
| 22 | + ****************************************************************************/ |
| 23 | + |
| 24 | +#include "mbedtls/error.h" |
| 25 | +#include "mbedtls/poly1305.h" |
| 26 | + |
| 27 | +/**************************************************************************** |
| 28 | + * Public Functions |
| 29 | + ****************************************************************************/ |
| 30 | + |
| 31 | +void mbedtls_poly1305_init(FAR mbedtls_poly1305_context *ctx) |
| 32 | +{ |
| 33 | + cryptodev_init(ctx); |
| 34 | +} |
| 35 | + |
| 36 | +void mbedtls_poly1305_free(FAR mbedtls_poly1305_context *ctx) |
| 37 | +{ |
| 38 | + cryptodev_free(ctx); |
| 39 | +} |
| 40 | + |
| 41 | +int mbedtls_poly1305_starts(mbedtls_poly1305_context *ctx, |
| 42 | + const unsigned char key[32]) |
| 43 | +{ |
| 44 | + ctx->session.mac = CRYPTO_POLY1305; |
| 45 | + ctx->session.mackey = (caddr_t)key; |
| 46 | + ctx->session.mackeylen = 32; |
| 47 | + return cryptodev_get_session(ctx); |
| 48 | +} |
| 49 | + |
| 50 | +int mbedtls_poly1305_update(FAR mbedtls_poly1305_context *ctx, |
| 51 | + FAR const unsigned char *input, |
| 52 | + size_t ilen) |
| 53 | +{ |
| 54 | + ctx->crypt.op = COP_ENCRYPT; |
| 55 | + ctx->crypt.flags |= COP_FLAG_UPDATE; |
| 56 | + ctx->crypt.src = (caddr_t)input; |
| 57 | + ctx->crypt.len = ilen; |
| 58 | + return cryptodev_crypt(ctx); |
| 59 | +} |
| 60 | + |
| 61 | +int mbedtls_poly1305_finish(FAR mbedtls_poly1305_context *ctx, |
| 62 | + unsigned char mac[16]) |
| 63 | +{ |
| 64 | + int ret; |
| 65 | + |
| 66 | + ctx->crypt.op = COP_ENCRYPT; |
| 67 | + ctx->crypt.flags = 0; |
| 68 | + ctx->crypt.mac = (caddr_t)mac; |
| 69 | + ret = cryptodev_crypt(ctx); |
| 70 | + cryptodev_free_session(ctx); |
| 71 | + return ret; |
| 72 | +} |
| 73 | + |
| 74 | +int mbedtls_poly1305_mac(const unsigned char key[32], |
| 75 | + FAR const unsigned char *input, |
| 76 | + size_t ilen, |
| 77 | + unsigned char mac[16]) |
| 78 | +{ |
| 79 | + mbedtls_poly1305_context ctx; |
| 80 | + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 81 | + |
| 82 | + mbedtls_poly1305_init(&ctx); |
| 83 | + |
| 84 | + ret = mbedtls_poly1305_starts(&ctx, key); |
| 85 | + if (ret != 0) |
| 86 | + { |
| 87 | + goto cleanup; |
| 88 | + } |
| 89 | + |
| 90 | + ret = mbedtls_poly1305_update(&ctx, input, ilen); |
| 91 | + if (ret != 0) |
| 92 | + { |
| 93 | + goto cleanup; |
| 94 | + } |
| 95 | + |
| 96 | + ret = mbedtls_poly1305_finish(&ctx, mac); |
| 97 | + |
| 98 | +cleanup: |
| 99 | + mbedtls_poly1305_free(&ctx); |
| 100 | + return ret; |
| 101 | +} |
0 commit comments