Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /*- |
michael@0 | 2 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. |
michael@0 | 3 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. |
michael@0 | 4 | * Copyright (c) 2008-2013, by Michael Tuexen. All rights reserved. |
michael@0 | 5 | * Copyright (c) 2013, by Lally Singh. All rights reserved. |
michael@0 | 6 | * |
michael@0 | 7 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 8 | * modification, are permitted provided that the following conditions are met: |
michael@0 | 9 | * |
michael@0 | 10 | * a) Redistributions of source code must retain the above copyright notice, |
michael@0 | 11 | * this list of conditions and the following disclaimer. |
michael@0 | 12 | * |
michael@0 | 13 | * b) Redistributions in binary form must reproduce the above copyright |
michael@0 | 14 | * notice, this list of conditions and the following disclaimer in |
michael@0 | 15 | * the documentation and/or other materials provided with the distribution. |
michael@0 | 16 | * |
michael@0 | 17 | * c) Neither the name of Cisco Systems, Inc. nor the names of its |
michael@0 | 18 | * contributors may be used to endorse or promote products derived |
michael@0 | 19 | * from this software without specific prior written permission. |
michael@0 | 20 | * |
michael@0 | 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 23 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
michael@0 | 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
michael@0 | 25 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
michael@0 | 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
michael@0 | 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
michael@0 | 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
michael@0 | 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
michael@0 | 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
michael@0 | 31 | * THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 32 | */ |
michael@0 | 33 | |
michael@0 | 34 | #include <netinet/sctp_sha1.h> |
michael@0 | 35 | |
michael@0 | 36 | #if defined(SCTP_USE_NSS_SHA1) |
michael@0 | 37 | /* A SHA-1 Digest is 160 bits, or 20 bytes */ |
michael@0 | 38 | #define SHA_DIGEST_LENGTH (20) |
michael@0 | 39 | |
michael@0 | 40 | void |
michael@0 | 41 | sctp_sha1_init(struct sctp_sha1_context *ctx) |
michael@0 | 42 | { |
michael@0 | 43 | ctx->pk11_ctx = PK11_CreateDigestContext(SEC_OID_SHA1); |
michael@0 | 44 | PK11_DigestBegin(ctx->pk11_ctx); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | void |
michael@0 | 48 | sctp_sha1_update(struct sctp_sha1_context *ctx, const unsigned char *ptr, unsigned int siz) |
michael@0 | 49 | { |
michael@0 | 50 | PK11_DigestOp(ctx->pk11_ctx, ptr, siz); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | void |
michael@0 | 54 | sctp_sha1_final(unsigned char *digest, struct sctp_sha1_context *ctx) |
michael@0 | 55 | { |
michael@0 | 56 | unsigned int output_len = 0; |
michael@0 | 57 | |
michael@0 | 58 | PK11_DigestFinal(ctx->pk11_ctx, digest, &output_len, SHA_DIGEST_LENGTH); |
michael@0 | 59 | PK11_DestroyContext(ctx->pk11_ctx, PR_TRUE); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | #elif defined(SCTP_USE_OPENSSL_SHA1) |
michael@0 | 63 | |
michael@0 | 64 | void |
michael@0 | 65 | sctp_sha1_init(struct sctp_sha1_context *ctx) |
michael@0 | 66 | { |
michael@0 | 67 | SHA1_Init(&ctx->sha_ctx); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | void |
michael@0 | 71 | sctp_sha1_update(struct sctp_sha1_context *ctx, const unsigned char *ptr, unsigned int siz) |
michael@0 | 72 | { |
michael@0 | 73 | SHA1_Update(&ctx->sha_ctx, ptr, (unsigned long)siz); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | void |
michael@0 | 77 | sctp_sha1_final(unsigned char *digest, struct sctp_sha1_context *ctx) |
michael@0 | 78 | { |
michael@0 | 79 | SHA1_Final(digest, &ctx->sha_ctx); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | #else |
michael@0 | 83 | |
michael@0 | 84 | #include <string.h> |
michael@0 | 85 | #if defined(__Userspace_os_Windows) |
michael@0 | 86 | #include <winsock2.h> |
michael@0 | 87 | #elif !defined(__Windows__) |
michael@0 | 88 | #include <arpa/inet.h> |
michael@0 | 89 | #endif |
michael@0 | 90 | |
michael@0 | 91 | #define F1(B,C,D) (((B & C) | ((~B) & D))) /* 0 <= t <= 19 */ |
michael@0 | 92 | #define F2(B,C,D) (B ^ C ^ D) /* 20 <= t <= 39 */ |
michael@0 | 93 | #define F3(B,C,D) ((B & C) | (B & D) | (C & D)) /* 40 <= t <= 59 */ |
michael@0 | 94 | #define F4(B,C,D) (B ^ C ^ D) /* 600 <= t <= 79 */ |
michael@0 | 95 | |
michael@0 | 96 | /* circular shift */ |
michael@0 | 97 | #define CSHIFT(A,B) ((B << A) | (B >> (32-A))) |
michael@0 | 98 | |
michael@0 | 99 | #define K1 0x5a827999 /* 0 <= t <= 19 */ |
michael@0 | 100 | #define K2 0x6ed9eba1 /* 20 <= t <= 39 */ |
michael@0 | 101 | #define K3 0x8f1bbcdc /* 40 <= t <= 59 */ |
michael@0 | 102 | #define K4 0xca62c1d6 /* 60 <= t <= 79 */ |
michael@0 | 103 | |
michael@0 | 104 | #define H0INIT 0x67452301 |
michael@0 | 105 | #define H1INIT 0xefcdab89 |
michael@0 | 106 | #define H2INIT 0x98badcfe |
michael@0 | 107 | #define H3INIT 0x10325476 |
michael@0 | 108 | #define H4INIT 0xc3d2e1f0 |
michael@0 | 109 | |
michael@0 | 110 | void |
michael@0 | 111 | sctp_sha1_init(struct sctp_sha1_context *ctx) |
michael@0 | 112 | { |
michael@0 | 113 | /* Init the SHA-1 context structure */ |
michael@0 | 114 | ctx->A = 0; |
michael@0 | 115 | ctx->B = 0; |
michael@0 | 116 | ctx->C = 0; |
michael@0 | 117 | ctx->D = 0; |
michael@0 | 118 | ctx->E = 0; |
michael@0 | 119 | ctx->H0 = H0INIT; |
michael@0 | 120 | ctx->H1 = H1INIT; |
michael@0 | 121 | ctx->H2 = H2INIT; |
michael@0 | 122 | ctx->H3 = H3INIT; |
michael@0 | 123 | ctx->H4 = H4INIT; |
michael@0 | 124 | ctx->TEMP = 0; |
michael@0 | 125 | memset(ctx->words, 0, sizeof(ctx->words)); |
michael@0 | 126 | ctx->how_many_in_block = 0; |
michael@0 | 127 | ctx->running_total = 0; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | static void |
michael@0 | 131 | sctp_sha1_process_a_block(struct sctp_sha1_context *ctx, unsigned int *block) |
michael@0 | 132 | { |
michael@0 | 133 | int i; |
michael@0 | 134 | |
michael@0 | 135 | /* init the W0-W15 to the block of words being hashed. */ |
michael@0 | 136 | /* step a) */ |
michael@0 | 137 | for (i = 0; i < 16; i++) { |
michael@0 | 138 | ctx->words[i] = ntohl(block[i]); |
michael@0 | 139 | } |
michael@0 | 140 | /* now init the rest based on the SHA-1 formula, step b) */ |
michael@0 | 141 | for (i = 16; i < 80; i++) { |
michael@0 | 142 | ctx->words[i] = CSHIFT(1, ((ctx->words[(i - 3)]) ^ |
michael@0 | 143 | (ctx->words[(i - 8)]) ^ |
michael@0 | 144 | (ctx->words[(i - 14)]) ^ |
michael@0 | 145 | (ctx->words[(i - 16)]))); |
michael@0 | 146 | } |
michael@0 | 147 | /* step c) */ |
michael@0 | 148 | ctx->A = ctx->H0; |
michael@0 | 149 | ctx->B = ctx->H1; |
michael@0 | 150 | ctx->C = ctx->H2; |
michael@0 | 151 | ctx->D = ctx->H3; |
michael@0 | 152 | ctx->E = ctx->H4; |
michael@0 | 153 | |
michael@0 | 154 | /* step d) */ |
michael@0 | 155 | for (i = 0; i < 80; i++) { |
michael@0 | 156 | if (i < 20) { |
michael@0 | 157 | ctx->TEMP = ((CSHIFT(5, ctx->A)) + |
michael@0 | 158 | (F1(ctx->B, ctx->C, ctx->D)) + |
michael@0 | 159 | (ctx->E) + |
michael@0 | 160 | ctx->words[i] + |
michael@0 | 161 | K1); |
michael@0 | 162 | } else if (i < 40) { |
michael@0 | 163 | ctx->TEMP = ((CSHIFT(5, ctx->A)) + |
michael@0 | 164 | (F2(ctx->B, ctx->C, ctx->D)) + |
michael@0 | 165 | (ctx->E) + |
michael@0 | 166 | (ctx->words[i]) + |
michael@0 | 167 | K2); |
michael@0 | 168 | } else if (i < 60) { |
michael@0 | 169 | ctx->TEMP = ((CSHIFT(5, ctx->A)) + |
michael@0 | 170 | (F3(ctx->B, ctx->C, ctx->D)) + |
michael@0 | 171 | (ctx->E) + |
michael@0 | 172 | (ctx->words[i]) + |
michael@0 | 173 | K3); |
michael@0 | 174 | } else { |
michael@0 | 175 | ctx->TEMP = ((CSHIFT(5, ctx->A)) + |
michael@0 | 176 | (F4(ctx->B, ctx->C, ctx->D)) + |
michael@0 | 177 | (ctx->E) + |
michael@0 | 178 | (ctx->words[i]) + |
michael@0 | 179 | K4); |
michael@0 | 180 | } |
michael@0 | 181 | ctx->E = ctx->D; |
michael@0 | 182 | ctx->D = ctx->C; |
michael@0 | 183 | ctx->C = CSHIFT(30, ctx->B); |
michael@0 | 184 | ctx->B = ctx->A; |
michael@0 | 185 | ctx->A = ctx->TEMP; |
michael@0 | 186 | } |
michael@0 | 187 | /* step e) */ |
michael@0 | 188 | ctx->H0 = (ctx->H0) + (ctx->A); |
michael@0 | 189 | ctx->H1 = (ctx->H1) + (ctx->B); |
michael@0 | 190 | ctx->H2 = (ctx->H2) + (ctx->C); |
michael@0 | 191 | ctx->H3 = (ctx->H3) + (ctx->D); |
michael@0 | 192 | ctx->H4 = (ctx->H4) + (ctx->E); |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | void |
michael@0 | 196 | sctp_sha1_update(struct sctp_sha1_context *ctx, const unsigned char *ptr, unsigned int siz) |
michael@0 | 197 | { |
michael@0 | 198 | unsigned int number_left, left_to_fill; |
michael@0 | 199 | |
michael@0 | 200 | number_left = siz; |
michael@0 | 201 | while (number_left > 0) { |
michael@0 | 202 | left_to_fill = sizeof(ctx->sha_block) - ctx->how_many_in_block; |
michael@0 | 203 | if (left_to_fill > number_left) { |
michael@0 | 204 | /* can only partially fill up this one */ |
michael@0 | 205 | memcpy(&ctx->sha_block[ctx->how_many_in_block], |
michael@0 | 206 | ptr, number_left); |
michael@0 | 207 | ctx->how_many_in_block += number_left; |
michael@0 | 208 | ctx->running_total += number_left; |
michael@0 | 209 | number_left = 0; |
michael@0 | 210 | break; |
michael@0 | 211 | } else { |
michael@0 | 212 | /* block is now full, process it */ |
michael@0 | 213 | memcpy(&ctx->sha_block[ctx->how_many_in_block], |
michael@0 | 214 | ptr, left_to_fill); |
michael@0 | 215 | sctp_sha1_process_a_block(ctx, |
michael@0 | 216 | (unsigned int *)ctx->sha_block); |
michael@0 | 217 | number_left -= left_to_fill; |
michael@0 | 218 | ctx->running_total += left_to_fill; |
michael@0 | 219 | ctx->how_many_in_block = 0; |
michael@0 | 220 | ptr = (const unsigned char *)(ptr + left_to_fill); |
michael@0 | 221 | } |
michael@0 | 222 | } |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | void |
michael@0 | 226 | sctp_sha1_final(unsigned char *digest, struct sctp_sha1_context *ctx) |
michael@0 | 227 | { |
michael@0 | 228 | /* |
michael@0 | 229 | * if any left in block fill with padding and process. Then transfer |
michael@0 | 230 | * the digest to the pointer. At the last block some special rules |
michael@0 | 231 | * need to apply. We must add a 1 bit following the message, then we |
michael@0 | 232 | * pad with 0's. The total size is encoded as a 64 bit number at the |
michael@0 | 233 | * end. Now if the last buffer has more than 55 octets in it we |
michael@0 | 234 | * cannot fit the 64 bit number + 10000000 pad on the end and must |
michael@0 | 235 | * add the 10000000 pad, pad the rest of the message with 0's and |
michael@0 | 236 | * then create an all 0 message with just the 64 bit size at the end |
michael@0 | 237 | * and run this block through by itself. Also the 64 bit int must |
michael@0 | 238 | * be in network byte order. |
michael@0 | 239 | */ |
michael@0 | 240 | int left_to_fill; |
michael@0 | 241 | unsigned int i, *ptr; |
michael@0 | 242 | |
michael@0 | 243 | if (ctx->how_many_in_block > 55) { |
michael@0 | 244 | /* |
michael@0 | 245 | * special case, we need to process two blocks here. One for |
michael@0 | 246 | * the current stuff plus possibly the pad. The other for |
michael@0 | 247 | * the size. |
michael@0 | 248 | */ |
michael@0 | 249 | left_to_fill = sizeof(ctx->sha_block) - ctx->how_many_in_block; |
michael@0 | 250 | if (left_to_fill == 0) { |
michael@0 | 251 | /* Should not really happen but I am paranoid */ |
michael@0 | 252 | sctp_sha1_process_a_block(ctx, |
michael@0 | 253 | (unsigned int *)ctx->sha_block); |
michael@0 | 254 | /* init last block, a bit different than the rest */ |
michael@0 | 255 | ctx->sha_block[0] = '\x80'; |
michael@0 | 256 | for (i = 1; i < sizeof(ctx->sha_block); i++) { |
michael@0 | 257 | ctx->sha_block[i] = 0x0; |
michael@0 | 258 | } |
michael@0 | 259 | } else if (left_to_fill == 1) { |
michael@0 | 260 | ctx->sha_block[ctx->how_many_in_block] = '\x80'; |
michael@0 | 261 | sctp_sha1_process_a_block(ctx, |
michael@0 | 262 | (unsigned int *)ctx->sha_block); |
michael@0 | 263 | /* init last block */ |
michael@0 | 264 | memset(ctx->sha_block, 0, sizeof(ctx->sha_block)); |
michael@0 | 265 | } else { |
michael@0 | 266 | ctx->sha_block[ctx->how_many_in_block] = '\x80'; |
michael@0 | 267 | for (i = (ctx->how_many_in_block + 1); |
michael@0 | 268 | i < sizeof(ctx->sha_block); |
michael@0 | 269 | i++) { |
michael@0 | 270 | ctx->sha_block[i] = 0x0; |
michael@0 | 271 | } |
michael@0 | 272 | sctp_sha1_process_a_block(ctx, |
michael@0 | 273 | (unsigned int *)ctx->sha_block); |
michael@0 | 274 | /* init last block */ |
michael@0 | 275 | memset(ctx->sha_block, 0, sizeof(ctx->sha_block)); |
michael@0 | 276 | } |
michael@0 | 277 | /* This is in bits so multiply by 8 */ |
michael@0 | 278 | ctx->running_total *= 8; |
michael@0 | 279 | ptr = (unsigned int *)&ctx->sha_block[60]; |
michael@0 | 280 | *ptr = htonl(ctx->running_total); |
michael@0 | 281 | sctp_sha1_process_a_block(ctx, (unsigned int *)ctx->sha_block); |
michael@0 | 282 | } else { |
michael@0 | 283 | /* |
michael@0 | 284 | * easy case, we just pad this message to size - end with 0 |
michael@0 | 285 | * add the magic 0x80 to the next word and then put the |
michael@0 | 286 | * network byte order size in the last spot and process the |
michael@0 | 287 | * block. |
michael@0 | 288 | */ |
michael@0 | 289 | ctx->sha_block[ctx->how_many_in_block] = '\x80'; |
michael@0 | 290 | for (i = (ctx->how_many_in_block + 1); |
michael@0 | 291 | i < sizeof(ctx->sha_block); |
michael@0 | 292 | i++) { |
michael@0 | 293 | ctx->sha_block[i] = 0x0; |
michael@0 | 294 | } |
michael@0 | 295 | /* get last int spot */ |
michael@0 | 296 | ctx->running_total *= 8; |
michael@0 | 297 | ptr = (unsigned int *)&ctx->sha_block[60]; |
michael@0 | 298 | *ptr = htonl(ctx->running_total); |
michael@0 | 299 | sctp_sha1_process_a_block(ctx, (unsigned int *)ctx->sha_block); |
michael@0 | 300 | } |
michael@0 | 301 | /* transfer the digest back to the user */ |
michael@0 | 302 | digest[3] = (ctx->H0 & 0xff); |
michael@0 | 303 | digest[2] = ((ctx->H0 >> 8) & 0xff); |
michael@0 | 304 | digest[1] = ((ctx->H0 >> 16) & 0xff); |
michael@0 | 305 | digest[0] = ((ctx->H0 >> 24) & 0xff); |
michael@0 | 306 | |
michael@0 | 307 | digest[7] = (ctx->H1 & 0xff); |
michael@0 | 308 | digest[6] = ((ctx->H1 >> 8) & 0xff); |
michael@0 | 309 | digest[5] = ((ctx->H1 >> 16) & 0xff); |
michael@0 | 310 | digest[4] = ((ctx->H1 >> 24) & 0xff); |
michael@0 | 311 | |
michael@0 | 312 | digest[11] = (ctx->H2 & 0xff); |
michael@0 | 313 | digest[10] = ((ctx->H2 >> 8) & 0xff); |
michael@0 | 314 | digest[9] = ((ctx->H2 >> 16) & 0xff); |
michael@0 | 315 | digest[8] = ((ctx->H2 >> 24) & 0xff); |
michael@0 | 316 | |
michael@0 | 317 | digest[15] = (ctx->H3 & 0xff); |
michael@0 | 318 | digest[14] = ((ctx->H3 >> 8) & 0xff); |
michael@0 | 319 | digest[13] = ((ctx->H3 >> 16) & 0xff); |
michael@0 | 320 | digest[12] = ((ctx->H3 >> 24) & 0xff); |
michael@0 | 321 | |
michael@0 | 322 | digest[19] = (ctx->H4 & 0xff); |
michael@0 | 323 | digest[18] = ((ctx->H4 >> 8) & 0xff); |
michael@0 | 324 | digest[17] = ((ctx->H4 >> 16) & 0xff); |
michael@0 | 325 | digest[16] = ((ctx->H4 >> 24) & 0xff); |
michael@0 | 326 | } |
michael@0 | 327 | |
michael@0 | 328 | #endif |