/* Generate HMAC-SHA1 intermediate Hash */ static void sa_hmac_sha1_get_pad(const u8 *key, u16 key_sz, u32 *ipad, u32 *opad) { u32 ws[SHA_WORKSPACE_WORDS]; u8 k_ipad[SHA_MESSAGE_BYTES]; u8 k_opad[SHA_MESSAGE_BYTES]; int i; for (i = 0; i < key_sz; i++) { k_ipad[i] = key[i] ^ 0x36; k_opad[i] = key[i] ^ 0x5c; } /* Instead of XOR with 0 */ for (; i < SHA_MESSAGE_BYTES; i++) { k_ipad[i] = 0x36; k_opad[i] = 0x5c; } /* SHA-1 on k_ipad */ sha_init(ipad); sha_transform(ipad, k_ipad, ws); for (i = 0; i < SHA_DIGEST_WORDS; i++) ipad[i] = cpu_to_be32(ipad[i]); /* SHA-1 on k_opad */ sha_init(opad); sha_transform(opad, k_opad, ws); for (i = 0; i < SHA_DIGEST_WORDS; i++) opad[i] = cpu_to_be32(opad[i]); }
void ssh_sha_final(void *c, unsigned char *digest) { SshSHAContext *context = c; int padding; unsigned char temp = 0x80; unsigned int in_buffer; SshUInt32 total_low, total_high; total_low = context->total_length[0]; total_high = context->total_length[1]; ssh_sha_update(context, &temp, 1); in_buffer = context->total_length[0] % 64; padding = (64 - (in_buffer + 9) % 64) % 64; if (in_buffer > 56) { memset(&context->in[in_buffer], 0, 64 - in_buffer); padding -= (64 - in_buffer); sha_transform(context, context->in); in_buffer = 0; } /* change the byte count to bits count */ total_high <<= 3; total_high += (total_low >> 29); total_low <<= 3; SSH_PUT_32BIT(context->in + 56, total_high); SSH_PUT_32BIT(context->in + 60, total_low); if ((64 - in_buffer - 8) > 0) { memset(&context->in[in_buffer], 0, 64 - in_buffer - 8); } sha_transform(context, context->in); SSH_PUT_32BIT(digest, context->A); SSH_PUT_32BIT(digest + 4, context->B); SSH_PUT_32BIT(digest + 8, context->C); SSH_PUT_32BIT(digest + 12, context->D); SSH_PUT_32BIT(digest + 16, context->E); memset(context, 0, sizeof(SshSHAContext)); }
int NET_CookieHash(netadr_t *from){ uint32_t digest[5]; uint32_t workspace[80]; char data[64]; int i; if(from->type == NA_IP){ for(i = 0; i < 4; i++) data[i] = from->ip[i]; *((unsigned short*)&data[4]) = from->port; Com_Memcpy(&data[6], net_cookieSecret, sizeof(net_cookieSecret)); }else if(from->type == NA_IP6){ for(i = 0; i < 16; i++) data[i] = from->ip[i]; *((unsigned short*)&data[16]) = from->port; Com_Memcpy(&data[18], net_cookieSecret, sizeof(net_cookieSecret) - 46); }else return 0; sha_init(digest); sha_transform(digest, data, workspace); return digest[0]; }
static void sha1_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len) { struct sha1_ctx *sctx = crypto_tfm_ctx(tfm); unsigned int partial, done; const u8 *src; partial = sctx->count & 0x3f; sctx->count += len; done = 0; src = data; if ((partial + len) > 63) { u32 temp[SHA_WORKSPACE_WORDS]; if (partial) { done = -partial; memcpy(sctx->buffer + partial, data, done + 64); src = sctx->buffer; } do { sha_transform(sctx->state, src, temp); done += 64; src = data + done; } while (done + 63 < len); memset(temp, 0, sizeof(temp)); partial = 0; } memcpy(sctx->buffer + partial, src, len - done); }
void ssh_sha_update(void *c, const unsigned char *buf, size_t len) { SshSHAContext *context = c; unsigned int to_copy = 0; unsigned int in_buffer; SshUInt32 old_length = context->total_length[0]; in_buffer = old_length % 64; context->total_length[0] += len; context->total_length[0] &= 0xFFFFFFFFL; if (context->total_length[0] < old_length) /* carry */ context->total_length[1]++; while (len > 0) { if (in_buffer == 0 && len >= 64) { sha_transform(context, buf); buf += 64; len -= 64; continue; } /* do copy? */ to_copy = 64 - in_buffer; if (to_copy > 0) { if (to_copy > len) to_copy = len; memcpy(&context->in[in_buffer], buf, to_copy); buf += to_copy; len -= to_copy; in_buffer += to_copy; if (in_buffer == 64) { sha_transform(context, context->in); in_buffer = 0; } } } }
static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport, u32 count, int c) { __u32 *tmp = __get_cpu_var(ipv4_cookie_scratch); memcpy(tmp + 4, syncookie_secret[c], sizeof(syncookie_secret[c])); tmp[0] = (__force u32)saddr; tmp[1] = (__force u32)daddr; tmp[2] = ((__force u32)sport << 16) + (__force u32)dport; tmp[3] = count; sha_transform(tmp + 16, (__u8 *)tmp, tmp + 16 + 5); return tmp[17]; }
static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport, u32 count, int c) { __u32 *tmp = __get_cpu_var(ipv4_cookie_scratch); static unsigned char inited = 0; if (! inited) { get_random_bytes(syncookie_secret, sizeof(syncookie_secret)); inited = 1; } memcpy(tmp + 4, syncookie_secret[c], sizeof(syncookie_secret[c])); tmp[0] = (__force u32)saddr; tmp[1] = (__force u32)daddr; tmp[2] = ((__force u32)sport << 16) + (__force u32)dport; tmp[3] = count; sha_transform(tmp + 16, (__u8 *)tmp, tmp + 16 + 5); return tmp[17]; }