コード例 #1
0
ファイル: scan_hex.c プロジェクト: dpejesh/libsv-common
int main() {
  char hex[1024];
  size_t hex_len;
  char src[1024];
  size_t src_len;
  char dst[1024];
  
  src_len = strlen(TEST_STR);
  memcpy(src, TEST_STR, src_len);
  src[src_len++] = 0;
  
  hex[fmt_hex(hex, src, src_len)] = 0;
  
  scan_hex(dst, hex);
  dst[src_len] = 0;
  
  if (strcmp(src, dst) != 0)
    return 1;
    
  return 0;
}
コード例 #2
0
static int tf_self_test_integrity(const char *alg_name, struct module *mod)
{
	unsigned char expected[32];
	unsigned char actual[32];
	struct scatterlist *sg = NULL;
	struct hash_desc desc = {NULL, 0};
	size_t digest_length;
	unsigned char *const key = tf_integrity_hmac_sha256_key;
	size_t const key_length = sizeof(tf_integrity_hmac_sha256_key);
	int error;

	if (mod->raw_binary_ptr == NULL)
		return -ENXIO;
	if (tf_integrity_hmac_sha256_expected_value == NULL)
		return -ENOENT;
	INFO("expected=%s", tf_integrity_hmac_sha256_expected_value);
	error = scan_hex(expected, sizeof(expected),
			 tf_integrity_hmac_sha256_expected_value);
	if (error < 0) {
		pr_err("tf_driver: Badly formatted hmac_sha256 parameter "
		       "(should be a hex string)\n");
		return -EIO;
	};

	desc.tfm = crypto_alloc_hash(alg_name, 0, 0);
	if (IS_ERR_OR_NULL(desc.tfm)) {
		ERROR("crypto_alloc_hash(%s) failed", alg_name);
		error = (desc.tfm == NULL ? -ENOMEM : (int)desc.tfm);
		goto abort;
	}
	digest_length = crypto_hash_digestsize(desc.tfm);
	INFO("alg_name=%s driver_name=%s digest_length=%u",
	     alg_name,
	     crypto_tfm_alg_driver_name(crypto_hash_tfm(desc.tfm)),
	     digest_length);

	error = crypto_hash_setkey(desc.tfm, key, key_length);
	if (error) {
		ERROR("crypto_hash_setkey(%s) failed: %d",
		      alg_name, error);
		goto abort;
	}

	sg = vmalloc_to_sg(mod->raw_binary_ptr, mod->raw_binary_size);
	if (IS_ERR_OR_NULL(sg)) {
		ERROR("vmalloc_to_sg(%lu) failed: %d",
		      mod->raw_binary_size, (int)sg);
		error = (sg == NULL ? -ENOMEM : (int)sg);
		goto abort;
	}

	error = crypto_hash_digest(&desc, sg, mod->raw_binary_size, actual);
	if (error) {
		ERROR("crypto_hash_digest(%s) failed: %d",
		      alg_name, error);
		goto abort;
	}

	kfree(sg);
	crypto_free_hash(desc.tfm);

#ifdef CONFIG_TF_DRIVER_FAULT_INJECTION
	if (tf_fault_injection_mask & TF_CRYPTO_ALG_INTEGRITY) {
		pr_warning("TF: injecting fault in integrity check!\n");
		actual[0] = 0xff;
		actual[1] ^= 0xff;
	}
#endif
	TF_TRACE_ARRAY(expected, digest_length);
	TF_TRACE_ARRAY(actual, digest_length);
	if (memcmp(expected, actual, digest_length)) {
		ERROR("wrong %s digest value", alg_name);
		error = -EINVAL;
	} else {
		INFO("%s: digest successful", alg_name);
		error = 0;
	}

	return error;

abort:
	if (!IS_ERR_OR_NULL(sg))
		kfree(sg);
	if (!IS_ERR_OR_NULL(desc.tfm))
		crypto_free_hash(desc.tfm);
	return error == -ENOMEM ? error : -EIO;
}
コード例 #3
0
ファイル: rewrite-spot-name.c プロジェクト: ncbi/ncbi-vdb
static
rc_t CC illumina_rewrite_spot_name ( void *data, const VXformInfo *info, int64_t row_id,
                                     VRowResult *rslt, uint32_t argc, const VRowData argv [] )
{
    rc_t rc;
    char buffer [ 64];
    uint32_t coord_len;
    uint32_t prefix_len;
    unsigned int a, b, c, d;
    KDataBuffer *dst = rslt -> data;

    const char *prefix;
    const char *skey = argv [ 0 ] . u . data . base;
    uint64_t i, j, count = argv [ 0 ] . u . data . elem_count;

    skey += argv [ 0 ] . u . data . first_elem;

    /* find last hex portion */
    for ( i = count; i > 0; )
    {
        if ( ! isxdigit ( skey [ -- i ] ) )
            break;
    }

    if ( count - i < ( SLX_COORD_LEN - 1 ) )
    {
        const char *end = skey + count;

        /* new format */
        for ( d = 0, j = 0, i = count; i > 0; )
        {
            if ( ! isdigit ( skey [ -- i ] ) )
            {
                j = i + 1;
                break;
            }
        }
        d = non_braindead_atoi ( & skey [ j ], end );
        for ( c = 0, j = 0; i > 0; )
        {
            if ( ! isdigit ( skey [ -- i ] ) )
            {
                j = i + 1;
                break;
            }
        }
        c = atoi ( & skey [ j ] );
        for ( b = 0, j = 0; i > 0; )
        {
            if ( ! isdigit ( skey [ -- i ] ) )
            {
                j = i + 1;
                break;
            }
        }
        b = atoi ( & skey [ j ] );
        for ( a = 0, j = 0; i > 0; )
        {
            if ( ! isdigit ( skey [ -- i ] ) )
            {
                j = i + 1;
                break;
            }
        }
        a = atoi ( & skey [ j ] );
        if ( j > 0 )
        {
            if ( i > 0 )
                -- i;
            while ( isalpha ( skey [ i ] ) )
                ++ i;
        }
    }
    else
    {
        a = scan_hex ( skey, 1 );
        b = scan_hex ( & skey [ 1 ], 3 );
        c = scan_hex ( & skey [ 4 ], 3 );
        d = scan_hex ( & skey [ 7 ], 3 );
        if ( count > SLX_COORD_LEN )
        {
            i = count - SLX_COORD_LEN;
        }
    }

    /* generate coordinates */
    coord_len = sprintf ( buffer, ":%d:%d:%d:%d", a, b, c, d );

    /* get size of prefix */
    if ( argc == 1 )
    {
        prefix = "";
        prefix_len = 0;
    }
    else
    {
        prefix = argv [ 1 ] . u . data . base;
        assert(argv [ 1 ] . u . data . elem_count >> 32 == 0);
        prefix_len = (uint32_t)argv [ 1 ] . u . data . elem_count;
        prefix += argv [ 1 ] . u . data . first_elem;
    }

    /* resize output buffer for prefix, name stuff, coordinates */
    if ( dst -> elem_bits != 8 )
    {
        rc = KDataBufferCast ( dst, dst, 8, true );
        if ( rc != 0 )
            return rc;
    }
    rc = KDataBufferResize ( dst, prefix_len + i + coord_len + 1 );
    if ( rc != 0 )
        return rc;

    /* copy in prefix, name prefix, coordinates */
    rslt -> elem_count = sprintf ( dst -> base, "%.*s%.*s%s"
                                   , ( int ) prefix_len, prefix
                                   , ( int ) i, skey
                                   , buffer );

    return 0;
}