1/*-
2 * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
--- 19 unchanged lines hidden (view full) ---
28
29#include <err.h>
30#include <inttypes.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35
36#include <netpgp/digest.h>
37
38#include "pgpsum.h"
39
40/* add the ascii armor line endings (except for last line) */
41static size_t
42don_armor(digest_t *hash, uint8_t *in, size_t insize)
43{
44 uint8_t *from;
45 uint8_t *p;
46 uint8_t dos_line_end[2];
47
48 dos_line_end[0] = '\r';
49 dos_line_end[1] = '\n';
50 for (from = in ; (p = memchr(from, '\n', insize - (size_t)(from - in))) != NULL ; from = p + 1) {
51 digest_update(hash, from, (size_t)(p - from));
52 digest_update(hash, dos_line_end, sizeof(dos_line_end));
53 }
54 digest_update(hash, from, insize - (size_t)(from - in));
55 return 1;
56}
57
58#ifdef NETPGPV_DEBUG
59/* just for giggles, write what we're about to checksum */
--- 33 unchanged lines hidden (view full) ---
93 return 0;
94 }
95 }
96 return 1;
97}
98
99/* calculate the checksum for the data we have */
100static int
101calcsum(uint8_t *out, size_t size, const char *name, uint8_t *mem, size_t cc, const uint8_t *hashed, size_t hashsize, int doarmor)
102{
103 digest_t hash;
104 uint32_t len32;
105 uint16_t len16;
106 uint8_t hashalg;
107 uint8_t trailer[6];
108
109 /* hashed data is non-null (previously checked) */
110 hashalg = hashed[3];
111 memcpy(&len16, &hashed[4], sizeof(len16));
112 len32 = ntohs(len16) + 6;
113 len32 = htonl(len32);
114 trailer[0] = 0x04;
115 trailer[1] = 0xff;
116 memcpy(&trailer[2], &len32, sizeof(len32));
117#ifdef NETPGPV_DEBUG
118 writefile(mem, cc);
119#endif
120 digest_init(&hash, hashalg);
121 if (doarmor && !already_armored(mem, cc)) {
122 /* this took me ages to find - something causes gpg to truncate its input */
123 don_armor(&hash, mem, cc - 1);
124 } else {
125 digest_update(&hash, mem, cc);
126 }
127 if (hashed) {
128 digest_update(&hash, hashed, hashsize);
129 }
130 digest_update(&hash, trailer, sizeof(trailer));
131 return digest_final(out, &hash);
--- 24 unchanged lines hidden (view full) ---
156 warn("%s - can't stat", name);
157 goto done;
158 }
159 cc = (size_t)(st.st_size);
160 if ((mem = mmap(NULL, cc, PROT_READ, MAP_SHARED, fileno(fp), 0)) == MAP_FAILED) {
161 warn("%s - can't mmap", name);
162 goto done;
163 }
164 ret = calcsum(data, size, name, mem, cc, hashed, hashsize, doarmor);
165done:
166 if (data) {
167 munmap(mem, cc);
168 }
169 fclose(fp);
170 return ret;
171}
172
173/* calculate the digest over memory too */
174int
175pgpv_digest_memory(uint8_t *data, size_t size, void *mem, size_t cc, const uint8_t *hashed, size_t hashsize, int doarmor)
176{
177 if (hashed == NULL || data == NULL || mem == NULL) {
178 fprintf(stderr, "no hashed data provided\n");
179 return 0;
180 }
181 return calcsum(data, size, "[memory]", mem, cc, hashed, hashsize, doarmor);
182}
2 * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
--- 19 unchanged lines hidden (view full) ---
28
29#include <err.h>
30#include <inttypes.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35
36#include <netpgp/digest.h>
37
38#include "pgpsum.h"
39
40/* add the ascii armor line endings (except for last line) */
41static size_t
42don_armor(digest_t *hash, uint8_t *in, size_t insize)
43{
44 uint8_t *from;
45 uint8_t *p;
46 uint8_t dos_line_end[2];
47
48 dos_line_end[0] = '\r';
49 dos_line_end[1] = '\n';
50 for (from = in ; (p = memchr(from, '\n', insize - (size_t)(from - in))) != NULL ; from = p + 1) {
51 digest_update(hash, from, (size_t)(p - from));
52 digest_update(hash, dos_line_end, sizeof(dos_line_end));
53 }
54 digest_update(hash, from, insize - (size_t)(from - in));
55 return 1;
56}
57
58#ifdef NETPGPV_DEBUG
59/* just for giggles, write what we're about to checksum */
--- 33 unchanged lines hidden (view full) ---
93 return 0;
94 }
95 }
96 return 1;
97}
98
99/* calculate the checksum for the data we have */
100static int
101calcsum(uint8_t *out, size_t size, const char *name, uint8_t *mem, size_t cc, const uint8_t *hashed, size_t hashsize, int doarmor)
102{
103 digest_t hash;
104 uint32_t len32;
105 uint16_t len16;
106 uint8_t hashalg;
107 uint8_t trailer[6];
108
109 /* hashed data is non-null (previously checked) */
110 hashalg = hashed[3];
111 memcpy(&len16, &hashed[4], sizeof(len16));
112 len32 = ntohs(len16) + 6;
113 len32 = htonl(len32);
114 trailer[0] = 0x04;
115 trailer[1] = 0xff;
116 memcpy(&trailer[2], &len32, sizeof(len32));
117#ifdef NETPGPV_DEBUG
118 writefile(mem, cc);
119#endif
120 digest_init(&hash, hashalg);
121 if (doarmor && !already_armored(mem, cc)) {
122 /* this took me ages to find - something causes gpg to truncate its input */
123 don_armor(&hash, mem, cc - 1);
124 } else {
125 digest_update(&hash, mem, cc);
126 }
127 if (hashed) {
128 digest_update(&hash, hashed, hashsize);
129 }
130 digest_update(&hash, trailer, sizeof(trailer));
131 return digest_final(out, &hash);
--- 24 unchanged lines hidden (view full) ---
156 warn("%s - can't stat", name);
157 goto done;
158 }
159 cc = (size_t)(st.st_size);
160 if ((mem = mmap(NULL, cc, PROT_READ, MAP_SHARED, fileno(fp), 0)) == MAP_FAILED) {
161 warn("%s - can't mmap", name);
162 goto done;
163 }
164 ret = calcsum(data, size, name, mem, cc, hashed, hashsize, doarmor);
165done:
166 if (data) {
167 munmap(mem, cc);
168 }
169 fclose(fp);
170 return ret;
171}
172
173/* calculate the digest over memory too */
174int
175pgpv_digest_memory(uint8_t *data, size_t size, void *mem, size_t cc, const uint8_t *hashed, size_t hashsize, int doarmor)
176{
177 if (hashed == NULL || data == NULL || mem == NULL) {
178 fprintf(stderr, "no hashed data provided\n");
179 return 0;
180 }
181 return calcsum(data, size, "[memory]", mem, cc, hashed, hashsize, doarmor);
182}