Deleted Added
full compact
pgpsum.c (1.3.6.2) pgpsum.c (1.1.2.1)
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
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 "digest.h"
36#include <netpgp/digest.h>
37
37#include "pgpsum.h"
38
38#include "pgpsum.h"
39
39#ifndef USE_ARG
40#define USE_ARG(x) /*LINTED*/(void)&(x)
41#endif
42
43/* add the ascii armor line endings (except for last line) */
44static size_t
40/* add the ascii armor line endings (except for last line) */
41static size_t
45don_armor(digest_t *hash, uint8_t *in, size_t insize, int doarmor)
42don_armor(digest_t *hash, uint8_t *in, size_t insize)
46{
47 uint8_t *from;
43{
44 uint8_t *from;
48 uint8_t *newp;
49 uint8_t *p;
50 uint8_t dos_line_end[2];
51
52 dos_line_end[0] = '\r';
53 dos_line_end[1] = '\n';
54 for (from = in ; (p = memchr(from, '\n', insize - (size_t)(from - in))) != NULL ; from = p + 1) {
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) {
55 for (newp = p ; doarmor == 'w' && newp > from ; --newp) {
56 if (*(newp - 1) != ' ' && *(newp - 1) != '\t') {
57 break;
58 }
59 }
60 digest_update(hash, from, (size_t)(newp - from));
51 digest_update(hash, from, (size_t)(p - from));
61 digest_update(hash, dos_line_end, sizeof(dos_line_end));
62 }
63 digest_update(hash, from, insize - (size_t)(from - in));
64 return 1;
65}
66
67#ifdef NETPGPV_DEBUG
68/* just for giggles, write what we're about to checksum */

--- 33 unchanged lines hidden (view full) ---

102 return 0;
103 }
104 }
105 return 1;
106}
107
108/* calculate the checksum for the data we have */
109static int
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
110calcsum(uint8_t *out, size_t size, uint8_t *mem, size_t cc, const uint8_t *hashed, size_t hashsize, int doarmor)
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)
111{
112 digest_t hash;
113 uint32_t len32;
114 uint16_t len16;
115 uint8_t hashalg;
116 uint8_t trailer[6];
117
102{
103 digest_t hash;
104 uint32_t len32;
105 uint16_t len16;
106 uint8_t hashalg;
107 uint8_t trailer[6];
108
118 USE_ARG(size);
119 /* hashed data is non-null (previously checked) */
120 hashalg = hashed[3];
121 memcpy(&len16, &hashed[4], sizeof(len16));
122 len32 = ntohs(len16) + 6;
123 len32 = htonl(len32);
124 trailer[0] = 0x04;
125 trailer[1] = 0xff;
126 memcpy(&trailer[2], &len32, sizeof(len32));
127#ifdef NETPGPV_DEBUG
128 writefile(mem, cc);
129#endif
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
130 digest_init(&hash, (const unsigned)hashalg);
131 if (strchr("tw", doarmor) != NULL && !already_armored(mem, cc)) {
120 digest_init(&hash, hashalg);
121 if (doarmor && !already_armored(mem, cc)) {
132 /* this took me ages to find - something causes gpg to truncate its input */
122 /* this took me ages to find - something causes gpg to truncate its input */
133 don_armor(&hash, mem, cc - 1, doarmor);
123 don_armor(&hash, mem, cc - 1);
134 } else {
135 digest_update(&hash, mem, cc);
136 }
137 if (hashed) {
138 digest_update(&hash, hashed, hashsize);
139 }
140 digest_update(&hash, trailer, sizeof(trailer));
141 return digest_final(out, &hash);

--- 24 unchanged lines hidden (view full) ---

166 warn("%s - can't stat", name);
167 goto done;
168 }
169 cc = (size_t)(st.st_size);
170 if ((mem = mmap(NULL, cc, PROT_READ, MAP_SHARED, fileno(fp), 0)) == MAP_FAILED) {
171 warn("%s - can't mmap", name);
172 goto done;
173 }
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 }
174 ret = calcsum(data, size, mem, cc, hashed, hashsize, doarmor);
164 ret = calcsum(data, size, name, mem, cc, hashed, hashsize, doarmor);
175done:
176 if (data) {
177 munmap(mem, cc);
178 }
179 fclose(fp);
180 return ret;
181}
182
183/* calculate the digest over memory too */
184int
185pgpv_digest_memory(uint8_t *data, size_t size, void *mem, size_t cc, const uint8_t *hashed, size_t hashsize, int doarmor)
186{
187 if (hashed == NULL || data == NULL || mem == NULL) {
188 fprintf(stderr, "no hashed data provided\n");
189 return 0;
190 }
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 }
191 return calcsum(data, size, mem, cc, hashed, hashsize, doarmor);
181 return calcsum(data, size, "[memory]", mem, cc, hashed, hashsize, doarmor);
192}
182}