xref: /netbsd-src/lib/libcrypt/util.c (revision f7145efd49910ac14efea48a054bbf12de6802e2)
1*f7145efdSnia /*	$NetBSD: util.c,v 1.3 2021/10/12 15:25:39 nia Exp $	*/
280833458Snia /*
380833458Snia  * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
480833458Snia  * All rights reserved.
580833458Snia  *
680833458Snia  * Redistribution and use in source and binary forms, with or without
780833458Snia  * modification, are permitted provided that the following conditions
880833458Snia  * are met:
980833458Snia  * 1. Redistributions of source code must retain the above copyright
1080833458Snia  *    notice, this list of conditions and the following disclaimer.
1180833458Snia  * 2. Redistributions in binary form must reproduce the above copyright
1280833458Snia  *    notice, this list of conditions and the following disclaimer in the
1380833458Snia  *    documentation and/or other materials provided with the distribution.
1480833458Snia  * 3. All advertising materials mentioning features or use of this software
1580833458Snia  *    must display the following acknowledgement:
1680833458Snia  *      This product includes software developed by Niels Provos.
1780833458Snia  * 4. The name of the author may not be used to endorse or promote products
1880833458Snia  *    derived from this software without specific prior written permission.
1980833458Snia  *
2080833458Snia  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2180833458Snia  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2280833458Snia  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2380833458Snia  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2480833458Snia  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2580833458Snia  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2680833458Snia  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2780833458Snia  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2880833458Snia  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2980833458Snia  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3080833458Snia  */
313a0c68edSsjg #include <sys/cdefs.h>
323a0c68edSsjg #if !defined(lint)
33*f7145efdSnia __RCSID("$NetBSD: util.c,v 1.3 2021/10/12 15:25:39 nia Exp $");
343a0c68edSsjg #endif /* not lint */
353a0c68edSsjg 
363a0c68edSsjg #include <sys/types.h>
3780833458Snia #include <errno.h>
3880833458Snia #include <limits.h>
3980833458Snia #include <stddef.h>
4080833458Snia #include <stdlib.h>
413a0c68edSsjg 
423a0c68edSsjg #include "crypt.h"
433a0c68edSsjg 
44*f7145efdSnia /* traditional unix "B64" encoding */
453a0c68edSsjg static const unsigned char itoa64[] =		/* 0 ... 63 => ascii - 64 */
463a0c68edSsjg 	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
473a0c68edSsjg 
48*f7145efdSnia /* standard base64 encoding, used by Argon2 */
49*f7145efdSnia static const unsigned char itoabase64[] =
50*f7145efdSnia 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
51*f7145efdSnia 
5280833458Snia crypt_private int
getnum(const char * str,size_t * num)5380833458Snia getnum(const char *str, size_t *num)
5480833458Snia {
5580833458Snia 	char *ep;
5680833458Snia 	unsigned long rv;
5780833458Snia 
5880833458Snia 	if (str == NULL) {
5980833458Snia 		*num = 0;
6080833458Snia 		return 0;
6180833458Snia 	}
6280833458Snia 
6380833458Snia 	rv = strtoul(str, &ep, 0);
6480833458Snia 
6580833458Snia 	if (str == ep || *ep) {
6680833458Snia 		errno = EINVAL;
6780833458Snia 		return -1;
6880833458Snia 	}
6980833458Snia 
7080833458Snia 	if (errno == ERANGE && rv == ULONG_MAX)
7180833458Snia 		return -1;
7280833458Snia 	*num = (size_t)rv;
7380833458Snia 	return 0;
7480833458Snia }
7580833458Snia 
76*f7145efdSnia crypt_private void
__crypt_to64(char * s,uint32_t v,int n)7780833458Snia __crypt_to64(char *s, uint32_t v, int n)
783a0c68edSsjg {
793a0c68edSsjg 
803a0c68edSsjg 	while (--n >= 0) {
813a0c68edSsjg 		*s++ = itoa64[v & 0x3f];
823a0c68edSsjg 		v >>= 6;
833a0c68edSsjg 	}
843a0c68edSsjg }
85*f7145efdSnia 
86*f7145efdSnia crypt_private void
__crypt_tobase64(char * s,uint32_t v,int n)87*f7145efdSnia __crypt_tobase64(char *s, uint32_t v, int n)
88*f7145efdSnia {
89*f7145efdSnia 
90*f7145efdSnia 	while (--n >= 0) {
91*f7145efdSnia 		*s++ = itoabase64[v & 0x3f];
92*f7145efdSnia 		v >>= 6;
93*f7145efdSnia 	}
94*f7145efdSnia }
95