summaryrefslogtreecommitdiff
path: root/src/apk_csum.h
blob: 564339be16a43d712d0ab3dd621ba55d0a189df8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* apk_csum.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation. See http://www.gnu.org/ for details.
 */

#ifndef APK_CSUM_H
#define APK_CSUM_H

#include <openssl/evp.h>

#define MAX_CSUM_SIZE	16 /* MD5 */

typedef unsigned char *csum_p;
typedef unsigned char csum_t[MAX_CSUM_SIZE];
typedef EVP_MD_CTX csum_ctx_t;

extern csum_t bad_checksum;

static inline void csum_init(csum_ctx_t *ctx)
{
	EVP_DigestInit(ctx, EVP_md5());
}

static inline void csum_process(csum_ctx_t *ctx, unsigned char *buf, size_t len)
{
	EVP_DigestUpdate(ctx, buf, len);
}

static inline void csum_finish(csum_ctx_t *ctx, csum_p csum)
{
	EVP_DigestFinal(ctx, csum, NULL);
}

static inline int csum_valid(csum_p csum)
{
	return memcmp(csum, bad_checksum, MAX_CSUM_SIZE);
}

static inline void csum_blob(apk_blob_t blob, csum_p csum)
{
	csum_ctx_t ctx;

	csum_init(&ctx);
	csum_process(&ctx, (csum_p) blob.ptr, blob.len);
	csum_finish(&ctx, csum);
}

#endif