sctp_sla1.c 8.33 KB
Newer Older
Jon Grimm's avatar
Jon Grimm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/* SCTP kernel reference Implementation
 * Copyright (c) 1999-2000 Cisco, Inc.
 * Copyright (c) 1999-2001 Motorola, Inc.
 * 
 * This file is part of the SCTP kernel reference Implementation
 * 
 * $Header: /cvsroot/lksctp/lksctp/sctp_cvs/net/sctp/sctp_sla1.c,v 1.4 2002/07/19 22:00:33 jgrimm Exp $
 * 
 * (It's really SHA-1 but Hey I was tired when I created this 
 * file, and on a plane to France :-)
 * 
 * The SCTP reference implementation is free software; 
 * you can redistribute it and/or modify it under the terms of 
 * the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 * 
 * The SCTP reference implementation is distributed in the hope that it 
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 *                 ************************
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GNU CC; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.  
 * 
 * Please send any bug reports or fixes you make to the
 * email address(es):
 *    lksctp developers <lksctp-developers@lists.sourceforge.net>
 * 
 * Or submit a bug report through the following website:
 *    http://www.sf.net/projects/lksctp
 *
 * Written or modified by: 
 *    Randall Stewart <rstewar1@email.mot.com>
 *    kmorneau@cisco.com
 *    qxie1@email.mot.com
 *
 * Based on:
 *    Randy Stewart, et al. SCTP Reference Implementation which is licenced
 *    under the GPL. 
 * 
 * Any bugs reported given to us we will try to fix... any fixes shared will
 * be incorporated into the next SCTP release.
 */
static char *cvs_id __attribute__ ((unused)) = "$Id: sctp_sla1.c,v 1.4 2002/07/19 22:00:33 jgrimm Exp $";

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/fcntl.h>
#include <asm/string.h>		/* for memcpy */
#include <linux/sched.h>	/* dead chicken for in.h */
#include <linux/in.h>		/* for htonl and ntohl */

#include <net/sctp/sctp_sla1.h>

59
void SLA1_Init(struct SLA_1_Context *ctx)
Jon Grimm's avatar
Jon Grimm committed
60
{
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	/* Init the SLA-1 context structure.  */
	ctx->A = 0;
	ctx->B = 0;
	ctx->C = 0;
	ctx->D = 0;
	ctx->E = 0;
	ctx->H0 = H0INIT;
	ctx->H1 = H1INIT;
	ctx->H2 = H2INIT;
	ctx->H3 = H3INIT;
	ctx->H4 = H4INIT;
	ctx->TEMP = 0;
	memset(ctx->words, 0, sizeof(ctx->words));
	ctx->howManyInBlock = 0;
	ctx->runningTotal = 0;
Jon Grimm's avatar
Jon Grimm committed
76 77
}

78
void SLA1processABlock(struct SLA_1_Context *ctx,unsigned int *block)
Jon Grimm's avatar
Jon Grimm committed
79
{
80 81 82 83 84
	int i;
	/* init the W0-W15 to the block of words being
	 * hashed.
	 */
	/* step a) */
Jon Grimm's avatar
Jon Grimm committed
85

86 87
	for (i = 0; i < 16; i++)
		ctx->words[i] = ntohl(block[i]);
Jon Grimm's avatar
Jon Grimm committed
88

89 90 91 92 93 94 95
	/* now init the rest based on the SLA-1 formula, step b) */
	for (i = 16; i < 80; i++)
		ctx->words[i] =
			CSHIFT(1, ((ctx->words[(i-3)]) ^
				   (ctx->words[(i-8)]) ^
				   (ctx->words[(i-14)]) ^
				   (ctx->words[(i-16)])));
Jon Grimm's avatar
Jon Grimm committed
96

97 98 99 100 101 102
	/* step c) */
	ctx->A = ctx->H0;
	ctx->B = ctx->H1;
	ctx->C = ctx->H2;
	ctx->D = ctx->H3;
	ctx->E = ctx->H4;
Jon Grimm's avatar
Jon Grimm committed
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	/* step d) */
	for (i = 0; i < 80; i++) {
		if (i < 20) {
			ctx->TEMP = ((CSHIFT(5, ctx->A)) +
				     (F1(ctx->B, ctx->C, ctx->D)) +
				     (ctx->E) +
				     ctx->words[i] +
				     K1
				);
		} else if (i < 40) {
			ctx->TEMP = ((CSHIFT(5, ctx->A)) +
				     (F2(ctx->B, ctx->C, ctx->D)) +
				     (ctx->E) +
				     (ctx->words[i]) +
				     K2
				);
		} else if (i < 60) {
			ctx->TEMP = ((CSHIFT(5, ctx->A)) +
				     (F3(ctx->B, ctx->C, ctx->D)) +
				     (ctx->E) +
				     (ctx->words[i]) +
				     K3
				);
		} else {
			ctx->TEMP = ((CSHIFT(5, ctx->A)) +
				     (F4(ctx->B, ctx->C, ctx->D)) +
				     (ctx->E) +
				     (ctx->words[i]) +
				     K4
				);
		}
		ctx->E = ctx->D;
		ctx->D = ctx->C;
		ctx->C = CSHIFT(30, ctx->B);
		ctx->B = ctx->A;
		ctx->A = ctx->TEMP;
	}

	/* step e) */
	ctx->H0 = (ctx->H0) + (ctx->A);
	ctx->H1 = (ctx->H1) + (ctx->B);
	ctx->H2 = (ctx->H2) + (ctx->C);
	ctx->H3 = (ctx->H3) + (ctx->D);
	ctx->H4 = (ctx->H4) + (ctx->E);
Jon Grimm's avatar
Jon Grimm committed
148 149
}

150
void SLA1_Process(struct SLA_1_Context *ctx, const unsigned char *ptr, int siz)
Jon Grimm's avatar
Jon Grimm committed
151
{
152
	int numberLeft, leftToFill;
Jon Grimm's avatar
Jon Grimm committed
153

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	numberLeft = siz;
	while (numberLeft > 0) {
		leftToFill = sizeof(ctx->SLAblock) - ctx->howManyInBlock;
		if (leftToFill > numberLeft) {
			/* can only partially fill up this one */
			memcpy(&ctx->SLAblock[ctx->howManyInBlock],
			       ptr, numberLeft);
			ctx->howManyInBlock += siz;
			ctx->runningTotal += siz;
			break;
		} else {
			/* block is now full, process it */
			memcpy(&ctx->SLAblock[ctx->howManyInBlock],
			       ptr, leftToFill);
			SLA1processABlock(ctx, (unsigned int *) ctx->SLAblock);
			numberLeft -= leftToFill;
			ctx->runningTotal += leftToFill;
			ctx->howManyInBlock = 0;
		}
	}
Jon Grimm's avatar
Jon Grimm committed
174 175
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
void SLA1_Final(struct SLA_1_Context *ctx, unsigned char *digestBuf)
{
	/* if any left in block fill with padding
	 * and process. Then transfer the digest to
	 * the pointer. At the last block some special
	 * rules need to apply. We must add a 1 bit
	 * following the message, then we pad with
	 * 0's. The total size is encoded as a 64 bit
	 * number at the end. Now if the last buffer has
	 * more than 55 octets in it we cannot fit
	 * the 64 bit number + 10000000 pad on the end
	 * and must add the 10000000 pad, pad the rest
	 * of the message with 0's and then create a
	 * all 0 message with just the 64 bit size
	 * at the end and run this block through by itself.
	 * Also the 64 bit int must be in network byte
	 * order.
	 */
	int i, leftToFill;
	unsigned int *ptr;
Jon Grimm's avatar
Jon Grimm committed
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	if (ctx->howManyInBlock > 55) {
		/* special case, we need to process two
		 * blocks here. One for the current stuff
		 * plus possibly the pad. The other for
		 * the size.
		 */
		leftToFill = sizeof(ctx->SLAblock) - ctx->howManyInBlock;
		if (leftToFill == 0) {
			/* Should not really happen but I am paranoid */
			/* Not paranoid enough!  It is possible for leftToFill
			 * to become negative!  AAA!!!!  This is another reason
			 * to pick MD5 :-)...
			 */
			SLA1processABlock(ctx, (unsigned int *) ctx->SLAblock);
			/* init last block, a bit different then the rest :-) */
			ctx->SLAblock[0] = 0x80;
			for (i = 1; i < sizeof(ctx->SLAblock); i++) {
				ctx->SLAblock[i] = 0x0;
			}
		} else if (leftToFill == 1) {
			ctx->SLAblock[ctx->howManyInBlock] = 0x80;
			SLA1processABlock(ctx, (unsigned int *) ctx->SLAblock);
			/* init last block */
			memset(ctx->SLAblock, 0, sizeof(ctx->SLAblock));
		} else {
			ctx->SLAblock[ctx->howManyInBlock] = 0x80;
			for (i = (ctx->howManyInBlock + 1);
			     i < sizeof(ctx->SLAblock);
			     i++) {
				ctx->SLAblock[i] = 0x0;
			}
			SLA1processABlock(ctx, (unsigned int *) ctx->SLAblock);
			/* init last block */
			memset(ctx->SLAblock, 0, sizeof(ctx->SLAblock));
		}
		/* This is in bits so multiply by 8 */
		ctx->runningTotal *= 8;
		ptr = (unsigned int *) &ctx->SLAblock[60];
		*ptr = htonl(ctx->runningTotal);
		SLA1processABlock(ctx, (unsigned int *) ctx->SLAblock);
	} else {
		/* easy case, we just pad this
		 * message to size - end with 0
		 * add the magic 0x80 to the next
		 * word and then put the network byte
		 * order size in the last spot and
		 * process the block.
		 */
		ctx->SLAblock[ctx->howManyInBlock] = 0x80;
		for (i = (ctx->howManyInBlock + 1);
		     i < sizeof(ctx->SLAblock);
		     i++) {
			ctx->SLAblock[i] = 0x0;
		}
		/* get last int spot */
		ctx->runningTotal *= 8;
		ptr = (unsigned int *) &ctx->SLAblock[60];
		*ptr = htonl(ctx->runningTotal);
		SLA1processABlock(ctx, (unsigned int *) ctx->SLAblock);
	}
	/* Now at this point all we need do is transfer the
	 * digest back to the user
	 */
	digestBuf[3] = (ctx->H0 & 0xff);
	digestBuf[2] = ((ctx->H0 >> 8) & 0xff);
	digestBuf[1] = ((ctx->H0 >> 16) & 0xff);
	digestBuf[0] = ((ctx->H0 >> 24) & 0xff);
Jon Grimm's avatar
Jon Grimm committed
264

265 266 267 268
	digestBuf[7] = (ctx->H1 & 0xff);
	digestBuf[6] = ((ctx->H1 >> 8) & 0xff);
	digestBuf[5] = ((ctx->H1 >> 16) & 0xff);
	digestBuf[4] = ((ctx->H1 >> 24) & 0xff);
Jon Grimm's avatar
Jon Grimm committed
269

270 271 272 273
	digestBuf[11] = (ctx->H2 & 0xff);
	digestBuf[10] = ((ctx->H2 >> 8) & 0xff);
	digestBuf[9] = ((ctx->H2 >> 16) & 0xff);
	digestBuf[8] = ((ctx->H2 >> 24) & 0xff);
Jon Grimm's avatar
Jon Grimm committed
274

275 276 277 278
	digestBuf[15] = (ctx->H3 & 0xff);
	digestBuf[14] = ((ctx->H3 >> 8) & 0xff);
	digestBuf[13] = ((ctx->H3 >> 16) & 0xff);
	digestBuf[12] = ((ctx->H3 >> 24) & 0xff);
Jon Grimm's avatar
Jon Grimm committed
279

280 281 282 283 284
	digestBuf[19] = (ctx->H4 & 0xff);
	digestBuf[18] = ((ctx->H4 >> 8) & 0xff);
	digestBuf[17] = ((ctx->H4 >> 16) & 0xff);
	digestBuf[16] = ((ctx->H4 >> 24) & 0xff);
}