View | Details | Raw Unified | Return to bug 200142 | Differences between
and this patch

Collapse All | Expand All

(-)a/contrib/xz/src/liblzma/check/sha256.c (-196 lines)
Removed Link Here
1
///////////////////////////////////////////////////////////////////////////////
2
//
3
/// \file       sha256.c
4
/// \brief      SHA-256
5
///
6
/// \todo       Crypto++ has x86 ASM optimizations. They use SSE so if they
7
///             are imported to liblzma, SSE instructions need to be used
8
///             conditionally to keep the code working on older boxes.
9
//
10
//  This code is based on the code found from 7-Zip, which has a modified
11
//  version of the SHA-256 found from Crypto++ <http://www.cryptopp.com/>.
12
//  The code was modified a little to fit into liblzma.
13
//
14
//  Authors:    Kevin Springle
15
//              Wei Dai
16
//              Igor Pavlov
17
//              Lasse Collin
18
//
19
//  This file has been put into the public domain.
20
//  You can do whatever you want with this file.
21
//
22
///////////////////////////////////////////////////////////////////////////////
23
24
#include "check.h"
25
26
// Rotate a uint32_t. GCC can optimize this to a rotate instruction
27
// at least on x86.
28
static inline uint32_t
29
rotr_32(uint32_t num, unsigned amount)
30
{
31
        return (num >> amount) | (num << (32 - amount));
32
}
33
34
#define blk0(i) (W[i] = conv32be(data[i]))
35
#define blk2(i) (W[i & 15] += s1(W[(i - 2) & 15]) + W[(i - 7) & 15] \
36
		+ s0(W[(i - 15) & 15]))
37
38
#define Ch(x, y, z) (z ^ (x & (y ^ z)))
39
#define Maj(x, y, z) ((x & (y ^ z)) + (y & z))
40
41
#define a(i) T[(0 - i) & 7]
42
#define b(i) T[(1 - i) & 7]
43
#define c(i) T[(2 - i) & 7]
44
#define d(i) T[(3 - i) & 7]
45
#define e(i) T[(4 - i) & 7]
46
#define f(i) T[(5 - i) & 7]
47
#define g(i) T[(6 - i) & 7]
48
#define h(i) T[(7 - i) & 7]
49
50
#define R(i, j, blk) \
51
	h(i) += S1(e(i)) + Ch(e(i), f(i), g(i)) + SHA256_K[i + j] + blk; \
52
	d(i) += h(i); \
53
	h(i) += S0(a(i)) + Maj(a(i), b(i), c(i))
54
#define R0(i) R(i, 0, blk0(i))
55
#define R2(i) R(i, j, blk2(i))
56
57
#define S0(x) rotr_32(x ^ rotr_32(x ^ rotr_32(x, 9), 11), 2)
58
#define S1(x) rotr_32(x ^ rotr_32(x ^ rotr_32(x, 14), 5), 6)
59
#define s0(x) (rotr_32(x ^ rotr_32(x, 11), 7) ^ (x >> 3))
60
#define s1(x) (rotr_32(x ^ rotr_32(x, 2), 17) ^ (x >> 10))
61
62
63
static const uint32_t SHA256_K[64] = {
64
	0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
65
	0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
66
	0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
67
	0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
68
	0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
69
	0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
70
	0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
71
	0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
72
	0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
73
	0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
74
	0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
75
	0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
76
	0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
77
	0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
78
	0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
79
	0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
80
};
81
82
83
static void
84
transform(uint32_t state[8], const uint32_t data[16])
85
{
86
	uint32_t W[16];
87
	uint32_t T[8];
88
89
	// Copy state[] to working vars.
90
	memcpy(T, state, sizeof(T));
91
92
	// The first 16 operations unrolled
93
	R0( 0); R0( 1); R0( 2); R0( 3);
94
	R0( 4); R0( 5); R0( 6); R0( 7);
95
	R0( 8); R0( 9); R0(10); R0(11);
96
	R0(12); R0(13); R0(14); R0(15);
97
98
	// The remaining 48 operations partially unrolled
99
	for (unsigned int j = 16; j < 64; j += 16) {
100
		R2( 0); R2( 1); R2( 2); R2( 3);
101
		R2( 4); R2( 5); R2( 6); R2( 7);
102
		R2( 8); R2( 9); R2(10); R2(11);
103
		R2(12); R2(13); R2(14); R2(15);
104
	}
105
106
	// Add the working vars back into state[].
107
	state[0] += a(0);
108
	state[1] += b(0);
109
	state[2] += c(0);
110
	state[3] += d(0);
111
	state[4] += e(0);
112
	state[5] += f(0);
113
	state[6] += g(0);
114
	state[7] += h(0);
115
}
116
117
118
static void
119
process(lzma_check_state *check)
120
{
121
	transform(check->state.sha256.state, check->buffer.u32);
122
	return;
123
}
124
125
126
extern void
127
lzma_sha256_init(lzma_check_state *check)
128
{
129
	static const uint32_t s[8] = {
130
		0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
131
		0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
132
	};
133
134
	memcpy(check->state.sha256.state, s, sizeof(s));
135
	check->state.sha256.size = 0;
136
137
	return;
138
}
139
140
141
extern void
142
lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check_state *check)
143
{
144
	// Copy the input data into a properly aligned temporary buffer.
145
	// This way we can be called with arbitrarily sized buffers
146
	// (no need to be multiple of 64 bytes), and the code works also
147
	// on architectures that don't allow unaligned memory access.
148
	while (size > 0) {
149
		const size_t copy_start = check->state.sha256.size & 0x3F;
150
		size_t copy_size = 64 - copy_start;
151
		if (copy_size > size)
152
			copy_size = size;
153
154
		memcpy(check->buffer.u8 + copy_start, buf, copy_size);
155
156
		buf += copy_size;
157
		size -= copy_size;
158
		check->state.sha256.size += copy_size;
159
160
		if ((check->state.sha256.size & 0x3F) == 0)
161
			process(check);
162
	}
163
164
	return;
165
}
166
167
168
extern void
169
lzma_sha256_finish(lzma_check_state *check)
170
{
171
	// Add padding as described in RFC 3174 (it describes SHA-1 but
172
	// the same padding style is used for SHA-256 too).
173
	size_t pos = check->state.sha256.size & 0x3F;
174
	check->buffer.u8[pos++] = 0x80;
175
176
	while (pos != 64 - 8) {
177
		if (pos == 64) {
178
			process(check);
179
			pos = 0;
180
		}
181
182
		check->buffer.u8[pos++] = 0x00;
183
	}
184
185
	// Convert the message size from bytes to bits.
186
	check->state.sha256.size *= 8;
187
188
	check->buffer.u64[(64 - 8) / 8] = conv64be(check->state.sha256.size);
189
190
	process(check);
191
192
	for (size_t i = 0; i < 8; ++i)
193
		check->buffer.u32[i] = conv32be(check->state.sha256.state[i]);
194
195
	return;
196
}
(-)b/lib/liblzma/Makefile (-8 / +8 lines)
Lines 78-85 SRCS+= common.c \ Link Here
78
.PATH: ${LZMADIR}/check
78
.PATH: ${LZMADIR}/check
79
SRCS+=	check.c \
79
SRCS+=	check.c \
80
	crc32_table.c \
80
	crc32_table.c \
81
	crc64_table.c \
81
	crc64_table.c
82
	sha256.c
83
.if defined(MACHINE_ARCH) && ${MACHINE_ARCH} == "i386"
82
.if defined(MACHINE_ARCH) && ${MACHINE_ARCH} == "i386"
84
SRCS+=	crc32_x86.S \
83
SRCS+=	crc32_x86.S \
85
	crc64_x86.S
84
	crc64_x86.S
Lines 125-135 SRCS+= simple_coder.c \ Link Here
125
124
126
.PATH: ${LZMADIR}
125
.PATH: ${LZMADIR}
127
126
128
VERSION_MAJOR!=	awk '$$1 == "\#define" && $$2 == "LZMA_VERSION_MAJOR" {print $$3 } ' \
127
VERSION_MAJOR!=	sed -n '/define.*LZMA_VERSION_MAJOR/{s,[^0-9.],,gp;q;}' \
129
			${LZMADIR}/api/lzma/version.h
128
			${LZMADIR}/api/lzma/version.h
130
VERSION_MINOR!=	awk '$$1 == "\#define" && $$2 == "LZMA_VERSION_MINOR" {print $$3 } ' \
129
VERSION_MINOR!=	sed -n '/define.*LZMA_VERSION_MINOR/{s,[^0-9.],,gp;q;}' \
131
			${LZMADIR}/api/lzma/version.h
130
			${LZMADIR}/api/lzma/version.h
132
VERSION_PATCH!=	awk '$$1 == "\#define" && $$2 == "LZMA_VERSION_PATCH" {print $$3 } ' \
131
VERSION_PATCH!=	sed -n '/define.*LZMA_VERSION_PATCH/{s,[^0-9.],,gp;q;}' \
133
			${LZMADIR}/api/lzma/version.h
132
			${LZMADIR}/api/lzma/version.h
134
133
135
WARNS?=	3
134
WARNS?=	3
Lines 147-153 CFLAGS+= -DHAVE_CONFIG_H \ Link Here
147
		-I${LZMADIR}/simple \
146
		-I${LZMADIR}/simple \
148
		-I${LZMADIR:H}/common
147
		-I${LZMADIR:H}/common
149
148
150
LIBADD+=	pthread
149
LIBADD+=	md pthread
151
150
152
VERSION_DEF=	${.CURDIR}/Versions.def
151
VERSION_DEF=	${.CURDIR}/Versions.def
153
SYMBOL_MAPS=	${.CURDIR}/Symbol.map
152
SYMBOL_MAPS=	${.CURDIR}/Symbol.map
Lines 160-169 FILESDIR= ${LIBDATADIR}/pkgconfig Link Here
160
159
161
liblzma.pc: liblzma.pc.in
160
liblzma.pc: liblzma.pc.in
162
	sed -e 's,@prefix@,/usr,g ; \
161
	sed -e 's,@prefix@,/usr,g ; \
163
		s,@exec_prefix@,/usr,g  ; \
162
		s,@exec_prefix@,/usr,g ; \
164
		s,@libdir@,/usr/lib,g ; \
163
		s,@libdir@,/usr/lib,g ; \
165
		s,@includedir@,/usr/include,g ; \
164
		s,@includedir@,/usr/include,g ; \
166
		s,@PACKAGE_URL@,http://tukaani.org/xz/,g ; \
165
		s,@LIBS@,-pthread -lmd,g ; \
166
		s,@PACKAGE_URL@,https://tukaani.org/xz/,g ; \
167
		s,@PACKAGE_VERSION@,${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH},g ; \
167
		s,@PACKAGE_VERSION@,${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH},g ; \
168
		s,@PTHREAD_CFLAGS@,,g ; \
168
		s,@PTHREAD_CFLAGS@,,g ; \
169
		s,@PTHREAD_LIBS@,,g' ${.ALLSRC} > ${.TARGET}
169
		s,@PTHREAD_LIBS@,,g' ${.ALLSRC} > ${.TARGET}
(-)b/lib/liblzma/Symbol.map (-3 lines)
Lines 180-188 XZprivate_1.0 { Link Here
180
	lzma_raw_coder_memusage;
180
	lzma_raw_coder_memusage;
181
	lzma_raw_decoder_init;
181
	lzma_raw_decoder_init;
182
	lzma_raw_encoder_init;
182
	lzma_raw_encoder_init;
183
	lzma_sha256_finish;
184
	lzma_sha256_init;
185
	lzma_sha256_update;
186
	lzma_simple_arm_decoder_init;
183
	lzma_simple_arm_decoder_init;
187
	lzma_simple_arm_encoder_init;
184
	lzma_simple_arm_encoder_init;
188
	lzma_simple_armthumb_decoder_init;
185
	lzma_simple_armthumb_decoder_init;
(-)b/lib/liblzma/config.h (-6 / +3 lines)
Lines 211-226 Link Here
211
/* #undef HAVE_SHA256INIT */
211
/* #undef HAVE_SHA256INIT */
212
212
213
/* Define to 1 if the system has the type `SHA256_CTX'. */
213
/* Define to 1 if the system has the type `SHA256_CTX'. */
214
/* FreeBSD - disabled libmd SHA256 for now */
214
#define HAVE_SHA256_CTX 1
215
/* #undef HAVE_SHA256_CTX */
216
215
217
/* Define to 1 if you have the <sha256.h> header file. */
216
/* Define to 1 if you have the <sha256.h> header file. */
218
/* FreeBSD - disabled libmd SHA256 for now */
217
#define HAVE_SHA256_H 1
219
/* #undef HAVE_SHA256_H */
220
218
221
/* Define to 1 if you have the `SHA256_Init' function. */
219
/* Define to 1 if you have the `SHA256_Init' function. */
222
/* FreeBSD - disabled libmd SHA256 for now */
220
#define HAVE_SHA256_INIT 1
223
/* #undef HAVE_SHA256_INIT */
224
221
225
/* Define to 1 if the system has the type `SHA2_CTX'. */
222
/* Define to 1 if the system has the type `SHA2_CTX'. */
226
/* #undef HAVE_SHA2_CTX */
223
/* #undef HAVE_SHA2_CTX */
(-)b/share/mk/src.libnames.mk (-2 / +1 lines)
Lines 350-356 _DP_heimipcs= heimbase roken pthread Link Here
350
_DP_kafs5=	asn1 krb5 roken
350
_DP_kafs5=	asn1 krb5 roken
351
_DP_krb5+=	asn1 com_err crypt crypto hx509 roken wind heimbase heimipcc
351
_DP_krb5+=	asn1 com_err crypt crypto hx509 roken wind heimbase heimipcc
352
_DP_gssapi_krb5+=	gssapi krb5 crypto roken asn1 com_err
352
_DP_gssapi_krb5+=	gssapi krb5 crypto roken asn1 com_err
353
_DP_lzma=	pthread
353
_DP_lzma=	md pthread
354
_DP_ucl=	m
354
_DP_ucl=	m
355
_DP_vmmapi=	util
355
_DP_vmmapi=	util
356
_DP_opencsd=	cxxrt
356
_DP_opencsd=	cxxrt
357
- 

Return to bug 200142