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"
37#include "pgpsum.h"
38
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
45don_armor(digest_t *hash, uint8_t *in, size_t insize, int doarmor)
46{
47 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) {
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));
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
110calcsum(uint8_t *out, size_t size, 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
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
130 digest_init(&hash, (const unsigned)hashalg);
131 if (strchr("tw", doarmor) != NULL && !already_armored(mem, cc)) {
132 /* this took me ages to find - something causes gpg to truncate its input */
133 don_armor(&hash, mem, cc - 1, doarmor);
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 }
174 ret = calcsum(data, size, 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 }
191 return calcsum(data, size, mem, cc, hashed, hashsize, doarmor);
192}
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"
37#include "pgpsum.h"
38
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
45don_armor(digest_t *hash, uint8_t *in, size_t insize, int doarmor)
46{
47 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) {
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));
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
110calcsum(uint8_t *out, size_t size, 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
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
130 digest_init(&hash, (const unsigned)hashalg);
131 if (strchr("tw", doarmor) != NULL && !already_armored(mem, cc)) {
132 /* this took me ages to find - something causes gpg to truncate its input */
133 don_armor(&hash, mem, cc - 1, doarmor);
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 }
174 ret = calcsum(data, size, 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 }
191 return calcsum(data, size, mem, cc, hashed, hashsize, doarmor);
192}