Radcli library 2.0.0
A simple radius library -- new API reference
Loading...
Searching...
No Matches
rc-crypto.c
1/*
2 * Copyright (C) 2026 Nikos Mavrogiannopoulos
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 */
25
29
30#include <string.h> /* memset() */
31#include "config.h" /* HAVE_DIGEST_LENGTH_ARG */
32#include "rc-crypto.h"
33
34void rc_md5_calc(unsigned char *output, unsigned char const *input,
35 size_t inlen)
36{
37 struct md5_ctx context;
38
39 md5_init(&context);
40 md5_update(&context, inlen, input);
41#ifdef HAVE_DIGEST_LENGTH_ARG
42 md5_digest(&context, MD5_DIGEST_SIZE, output);
43#else
44 md5_digest(&context, output);
45#endif
46}
47
48void rc_hmac_md5(uint8_t *data, size_t data_len,
49 uint8_t *key, size_t key_len,
50 uint8_t digest[MD5_DIGEST_SIZE])
51{
52 struct hmac_md5_ctx md5;
53
54 memset(digest, 0, MD5_DIGEST_SIZE);
55 hmac_md5_set_key(&md5, key_len, key);
56 hmac_md5_update(&md5, data_len, data);
57#ifdef HAVE_DIGEST_LENGTH_ARG
58 hmac_md5_digest(&md5, MD5_DIGEST_SIZE, digest);
59#else
60 hmac_md5_digest(&md5, digest);
61#endif
62}
63
64void rc_sha256_calc(uint8_t output[RC_SHA256_DIGEST_SIZE],
65 unsigned char const *input, size_t inlen)
66{
67 struct sha256_ctx context;
68
69 sha256_init(&context);
70 sha256_update(&context, inlen, input);
71#ifdef HAVE_DIGEST_LENGTH_ARG
72 sha256_digest(&context, RC_SHA256_DIGEST_SIZE, output);
73#else
74 sha256_digest(&context, output);
75#endif
76}