util.c revision 1.3 1 1.3 nia /* $NetBSD: util.c,v 1.3 2021/10/12 15:25:39 nia Exp $ */
2 1.2 nia /*
3 1.2 nia * Copyright 1997 Niels Provos <provos (at) physnet.uni-hamburg.de>
4 1.2 nia * All rights reserved.
5 1.2 nia *
6 1.2 nia * Redistribution and use in source and binary forms, with or without
7 1.2 nia * modification, are permitted provided that the following conditions
8 1.2 nia * are met:
9 1.2 nia * 1. Redistributions of source code must retain the above copyright
10 1.2 nia * notice, this list of conditions and the following disclaimer.
11 1.2 nia * 2. Redistributions in binary form must reproduce the above copyright
12 1.2 nia * notice, this list of conditions and the following disclaimer in the
13 1.2 nia * documentation and/or other materials provided with the distribution.
14 1.2 nia * 3. All advertising materials mentioning features or use of this software
15 1.2 nia * must display the following acknowledgement:
16 1.2 nia * This product includes software developed by Niels Provos.
17 1.2 nia * 4. The name of the author may not be used to endorse or promote products
18 1.2 nia * derived from this software without specific prior written permission.
19 1.2 nia *
20 1.2 nia * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.2 nia * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.2 nia * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.2 nia * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.2 nia * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.2 nia * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.2 nia * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.2 nia * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.2 nia * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.2 nia * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.2 nia */
31 1.1 sjg #include <sys/cdefs.h>
32 1.1 sjg #if !defined(lint)
33 1.3 nia __RCSID("$NetBSD: util.c,v 1.3 2021/10/12 15:25:39 nia Exp $");
34 1.1 sjg #endif /* not lint */
35 1.1 sjg
36 1.1 sjg #include <sys/types.h>
37 1.2 nia #include <errno.h>
38 1.2 nia #include <limits.h>
39 1.2 nia #include <stddef.h>
40 1.2 nia #include <stdlib.h>
41 1.1 sjg
42 1.1 sjg #include "crypt.h"
43 1.1 sjg
44 1.3 nia /* traditional unix "B64" encoding */
45 1.1 sjg static const unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
46 1.1 sjg "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
47 1.1 sjg
48 1.3 nia /* standard base64 encoding, used by Argon2 */
49 1.3 nia static const unsigned char itoabase64[] =
50 1.3 nia "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
51 1.3 nia
52 1.2 nia crypt_private int
53 1.2 nia getnum(const char *str, size_t *num)
54 1.2 nia {
55 1.2 nia char *ep;
56 1.2 nia unsigned long rv;
57 1.2 nia
58 1.2 nia if (str == NULL) {
59 1.2 nia *num = 0;
60 1.2 nia return 0;
61 1.2 nia }
62 1.2 nia
63 1.2 nia rv = strtoul(str, &ep, 0);
64 1.2 nia
65 1.2 nia if (str == ep || *ep) {
66 1.2 nia errno = EINVAL;
67 1.2 nia return -1;
68 1.2 nia }
69 1.2 nia
70 1.2 nia if (errno == ERANGE && rv == ULONG_MAX)
71 1.2 nia return -1;
72 1.2 nia *num = (size_t)rv;
73 1.2 nia return 0;
74 1.2 nia }
75 1.2 nia
76 1.3 nia crypt_private void
77 1.2 nia __crypt_to64(char *s, uint32_t v, int n)
78 1.1 sjg {
79 1.1 sjg
80 1.1 sjg while (--n >= 0) {
81 1.1 sjg *s++ = itoa64[v & 0x3f];
82 1.1 sjg v >>= 6;
83 1.1 sjg }
84 1.1 sjg }
85 1.3 nia
86 1.3 nia crypt_private void
87 1.3 nia __crypt_tobase64(char *s, uint32_t v, int n)
88 1.3 nia {
89 1.3 nia
90 1.3 nia while (--n >= 0) {
91 1.3 nia *s++ = itoabase64[v & 0x3f];
92 1.3 nia v >>= 6;
93 1.3 nia }
94 1.3 nia }
95