|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic |
| 5 | + */ |
| 6 | + |
| 7 | +/**@file |
| 8 | + * @defgroup nrf_oberon_sha_1 SHA-1 APIs |
| 9 | + * @ingroup nrf_oberon |
| 10 | + * @{ |
| 11 | + * @brief Type declarations and APIs for the SHA-1 algorithm. |
| 12 | + * |
| 13 | + * A fixed-sized message digest is computed from variable length input data. |
| 14 | + * The function is practically impossible to revert, and small changes in the |
| 15 | + * input message lead to major changes in the message digest. |
| 16 | + * |
| 17 | + * SHA-1 is no longer considered secure against well-funded opponents; |
| 18 | + * replacement by SHA-2 or SHA-3 is recommended. |
| 19 | + */ |
| 20 | + |
| 21 | +#ifndef OCRYPTO_SHA1_H |
| 22 | +#define OCRYPTO_SHA1_H |
| 23 | + |
| 24 | +#ifdef __cplusplus |
| 25 | +extern "C" { |
| 26 | +#endif |
| 27 | + |
| 28 | +#include <stddef.h> |
| 29 | +#include <stdint.h> |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * Length of SHA-1 hash. |
| 34 | + */ |
| 35 | +#define ocrypto_sha1_BYTES (20) |
| 36 | + |
| 37 | + |
| 38 | +/**@cond */ |
| 39 | +typedef struct { |
| 40 | + uint32_t h[5]; |
| 41 | + uint8_t padded[64]; |
| 42 | + uint32_t length; |
| 43 | + size_t bytes; |
| 44 | +} ocrypto_sha1_ctx; |
| 45 | +/**@endcond */ |
| 46 | + |
| 47 | + |
| 48 | +/**@name Incremental SHA-1 generator. |
| 49 | + * |
| 50 | + * This group of functions can be used to incrementally compute the SHA-1 |
| 51 | + * hash for a given message. |
| 52 | + */ |
| 53 | +/**@{*/ |
| 54 | +/** |
| 55 | + * SHA-1 initialization. |
| 56 | + * |
| 57 | + * The generator state @p ctx is initialized by this function. |
| 58 | + * |
| 59 | + * @param[out] ctx Generator state. |
| 60 | + */ |
| 61 | +void ocrypto_sha1_init( |
| 62 | + ocrypto_sha1_ctx *ctx); |
| 63 | + |
| 64 | +/** |
| 65 | + * SHA-1 incremental data input. |
| 66 | + * |
| 67 | + * The generator state @p ctx is updated to hash a message chunk @p in. |
| 68 | + * |
| 69 | + * This function can be called repeatedly until the whole message is processed. |
| 70 | + * |
| 71 | + * @param ctx Generator state. |
| 72 | + * @param in Input data. |
| 73 | + * @param in_len Length of @p in. |
| 74 | + * |
| 75 | + * @remark Initialization of the generator state @p ctx through |
| 76 | + * @c ocrypto_sha1_init is required before this function can be called. |
| 77 | + */ |
| 78 | +void ocrypto_sha1_update( |
| 79 | + ocrypto_sha1_ctx *ctx, |
| 80 | + const uint8_t *in, size_t in_len); |
| 81 | + |
| 82 | +/** |
| 83 | + * SHA-1 output. |
| 84 | + * |
| 85 | + * The generator state @p ctx is updated to finalize the hash for the previously |
| 86 | + * processed message chunks. The hash is put into @p r. |
| 87 | + * |
| 88 | + * @param ctx Generator state. |
| 89 | + * @param[out] r Generated hash value. |
| 90 | + * |
| 91 | + * @remark Initialization of the generator state @p ctx through |
| 92 | + * @c ocrypto_sha1_init is required before this function can be called. |
| 93 | + * |
| 94 | + * @remark After return, the generator state @p ctx must no longer be used with |
| 95 | + * @c ocrypto_sha1_update and @c ocrypto_sha1_final unless it is |
| 96 | + * reinitialized using @c ocrypto_sha1_init. |
| 97 | + */ |
| 98 | +void ocrypto_sha1_final( |
| 99 | + ocrypto_sha1_ctx *ctx, |
| 100 | + uint8_t r[ocrypto_sha1_BYTES]); |
| 101 | +/**@}*/ |
| 102 | + |
| 103 | +/** |
| 104 | + * SHA-1 hash. |
| 105 | + * |
| 106 | + * The SHA-1 hash of a given input message @p in is computed and put into @p r. |
| 107 | + * |
| 108 | + * **Example** |
| 109 | + * @include ocrypto_sha1.c |
| 110 | + * |
| 111 | + * @param[out] r Generated hash. |
| 112 | + * @param in Input data. |
| 113 | + * @param in_len Length of @p in. |
| 114 | + */ |
| 115 | +void ocrypto_sha1( |
| 116 | + uint8_t r[ocrypto_sha1_BYTES], |
| 117 | + const uint8_t *in, size_t in_len); |
| 118 | + |
| 119 | +#ifdef __cplusplus |
| 120 | +} |
| 121 | +#endif |
| 122 | + |
| 123 | +#endif |
| 124 | + |
| 125 | +/** @} */ |
0 commit comments