aboutsummaryrefslogblamecommitdiffstats
path: root/libhashsum_init_hasher_from_string.c
blob: a1ee82759bec09764356920e877fdf880519e6af (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14



                                                         









                                                                                                                  
                                   
                            
            


























                                                              













                                                     
                                                                                      


                                                               
                                          

                                   


                                    
                                                  
         


                                             
                                                       

                                                    
                                                       

                                                    



                                                       













                                            
                                          



                       


 











                                                                                  
                                   
                            

                                   
                            

                                   
                            

                                   
                            

                      
                            
            
















                                                        

                                                                         
                                                 
                          












                                    


                                                                                         
                                       
                                                                      
































                                                                                                          
                                                                                                        






                                                                                                               
 
/* See LICENSE file for copyright and license details. */
#include "common.h"


static int
with_n(int (*initfunc)(struct libhashsum_hasher *, size_t), struct libhashsum_hasher *this, const char *algorithm)
{
	const char *p;
	size_t n = 0, digit;

	p = strchr(algorithm, '[');
	if (!p || *++p == ']')
		return initfunc(this, 0);

	if (*p != 'n' && *p != 'N')
		goto einval;
	p++;
	if (*p++ != '=' || !('1' <= *p && *p <= '9'))
		goto einval;

	while (isdigit(*p)) {
		digit = (size_t)(*p++ & 15);
		if (n > (SIZE_MAX - digit) / 10U)
			goto einval;
		n = n * 10U + digit;
	}

	if (*p++ != ']' || *p)
		goto einval;

	return initfunc(this, n);

einval:
	errno = EINVAL;
	return -1;
}


static int
parse_value(size_t *valp, const char *p, const char **end_out)
{
	size_t digit;
	if (*valp)
		return -1;
	if (*p++ != '=' || !('1' <= *p && *p <= '9'))
		return -1;
	while (isdigit(*p)) {
		digit = (size_t)(*p++ & 15);
		if (*valp > (SIZE_MAX - digit) / 10U)
			return -1;
		*valp = *valp * 10U + digit;
	}
	*end_out = p;
	return 0;
}


static int
with_rcnz(int (*initfunc)(struct libhashsum_hasher *, size_t, size_t, size_t, size_t),
         struct libhashsum_hasher *this, const char *algorithm)
{
	const char *p;
	size_t r = 0, c = 0, n = 0, z = 0;

	p = strchr(algorithm, '[');
	if (!p || *++p == ']') {
		if (p && *p)
			goto einval;
		return initfunc(this, 0, 0, 0, 0);
	}

	for (;;) {
		if (*p == 'r' || *p == 'R') {
			if (parse_value(&r, &p[1], &p))
				goto einval;
		} else if (*p == 'c' || *p == 'C') {
			if (parse_value(&c, &p[1], &p))
				goto einval;
		} else if (*p == 'n' || *p == 'N') {
			if (parse_value(&n, &p[1], &p))
				goto einval;
		} else if (*p == 'z' || *p == 'Z') {
			if (parse_value(&z, &p[1], &p))
				goto einval;
		} else if (*p == ']') {
			break;
		} else {
			goto einval;
		}
		if (*p == ']')
			break;
		if (*p++ != ',')
			goto einval;
	}
	if (p[1])
		goto einval;

	return initfunc(this, r, c, n, z);

einval:
	errno = EINVAL;
	return -1;
}


static int
with_salt(int (*initfunc)(struct libhashsum_hasher *, const void *),
          struct libhashsum_hasher *this, const char *algorithm, size_t saltbytes)
{
	const char *p;
	uint8_t a, b, salt[32];
	size_t salti = 0;

	p = strchr(algorithm, '[');
	if (!p || *++p == ']')
		return initfunc(this, NULL);

	if (*p != 's' && *p != 'S')
		goto einval;
	p++;
	if (*p != 'a' && *p != 'A')
		goto einval;
	p++;
	if (*p != 'l' && *p != 'L')
		goto einval;
	p++;
	if (*p != 't' && *p != 'T')
		goto einval;
	p++;
	if (*p != '=')
		goto einval;
	p++;

	if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
		p = &p[2];

	for (;;) {
		if (isxdigit(p[0]) && isxdigit(p[1])) {
			if (!saltbytes)
				goto einval;
			saltbytes--;
		} else if (isxdigit(p[0])) {
			goto einval;
		} else if (saltbytes) {
			goto einval;
		} else {
			break;
		}

		a = (uint8_t)(((p[0] & 15) + (p[0] > '9' ? 9 : 0)) << 4);
		b = (uint8_t)(((p[1] & 15) + (p[1] > '9' ? 9 : 0)) << 0);
		salt[salti++] = (uint8_t)(a | b);
		p = &p[2];
	}

	if (*p++ != ']' || *p)
		goto einval;

	return initfunc(this, salt);

einval:
	errno = EINVAL;
	return -1;
}


int
libhashsum_init_hasher_from_string(struct libhashsum_hasher *this, const char *algorithm)
{
	enum libhashsum_algorithm algo;
	if (!libhashsum_get_algorithm_from_string(&algo, algorithm)) {
		errno = EINVAL;
		return -1;
	}
	switch (algo) {
	case LIBHASHSUM_MD2:          return libhashsum_init_md2_hasher(this);
	case LIBHASHSUM_MD4:          return libhashsum_init_md4_hasher(this);
	case LIBHASHSUM_MD5:          return libhashsum_init_md5_hasher(this);
	case LIBHASHSUM_RIPEMD_128:   return libhashsum_init_ripemd_128_hasher(this);
	case LIBHASHSUM_RIPEMD_160:   return libhashsum_init_ripemd_160_hasher(this);
	case LIBHASHSUM_RIPEMD_256:   return libhashsum_init_ripemd_256_hasher(this);
	case LIBHASHSUM_RIPEMD_320:   return libhashsum_init_ripemd_320_hasher(this);
	case LIBHASHSUM_SHA0:         return libhashsum_init_sha0_hasher(this);
	case LIBHASHSUM_SHA1:         return libhashsum_init_sha1_hasher(this);
	case LIBHASHSUM_SHA_224:      return libhashsum_init_sha_224_hasher(this);
	case LIBHASHSUM_SHA_256:      return libhashsum_init_sha_256_hasher(this);
	case LIBHASHSUM_SHA_384:      return libhashsum_init_sha_384_hasher(this);
	case LIBHASHSUM_SHA_512:      return libhashsum_init_sha_512_hasher(this);
	case LIBHASHSUM_SHA_512_224:  return libhashsum_init_sha_512_224_hasher(this);
	case LIBHASHSUM_SHA_512_256:  return libhashsum_init_sha_512_256_hasher(this);
	case LIBHASHSUM_KECCAK_224:   return libhashsum_init_keccak_224_hasher(this);
	case LIBHASHSUM_KECCAK_256:   return libhashsum_init_keccak_256_hasher(this);
	case LIBHASHSUM_KECCAK_384:   return libhashsum_init_keccak_384_hasher(this);
	case LIBHASHSUM_KECCAK_512:   return libhashsum_init_keccak_512_hasher(this);
	case LIBHASHSUM_SHA3_224:     return libhashsum_init_sha3_224_hasher(this);
	case LIBHASHSUM_SHA3_256:     return libhashsum_init_sha3_256_hasher(this);
	case LIBHASHSUM_SHA3_384:     return libhashsum_init_sha3_384_hasher(this);
	case LIBHASHSUM_SHA3_512:     return libhashsum_init_sha3_512_hasher(this);
	case LIBHASHSUM_SHAKE128:     return with_n(&libhashsum_init_shake128_hasher, this, algorithm);
	case LIBHASHSUM_SHAKE256:     return with_n(&libhashsum_init_shake256_hasher, this, algorithm);
	case LIBHASHSUM_SHAKE512:     return with_n(&libhashsum_init_shake512_hasher, this, algorithm);
	case LIBHASHSUM_RAWSHAKE128:  return with_n(&libhashsum_init_rawshake128_hasher, this, algorithm);
	case LIBHASHSUM_RAWSHAKE256:  return with_n(&libhashsum_init_rawshake256_hasher, this, algorithm);
	case LIBHASHSUM_RAWSHAKE512:  return with_n(&libhashsum_init_rawshake512_hasher, this, algorithm);
	case LIBHASHSUM_KECCAK:       return with_rcnz(&libhashsum_init_keccak_hasher, this, algorithm);
	case LIBHASHSUM_BLAKE224:     return with_salt(&libhashsum_init_blake224_hasher, this, algorithm, 16U);
	case LIBHASHSUM_BLAKE256:     return with_salt(&libhashsum_init_blake256_hasher, this, algorithm, 16U);
	case LIBHASHSUM_BLAKE384:     return with_salt(&libhashsum_init_blake384_hasher, this, algorithm, 32U);
	case LIBHASHSUM_BLAKE512:     return with_salt(&libhashsum_init_blake512_hasher, this, algorithm, 32U);
	default:
		abort();
	}
}