From 59e2334029d23bbe08dcbfd18bb7772ed5916d48 Mon Sep 17 00:00:00 2001 From: "Pavel I. Kryukov" Date: Sun, 11 Feb 2018 13:40:24 +0300 Subject: [PATCH] Add pointers to SHA1 and SHA256 implementation functions These pointers will be required in next commits where subroutines with new instructions are introduced. Depending on CPUID dynamic check, pointers will refer to old SW-only implementations or to new instructions subroutines --- ssh.h | 6 ++++-- sshsh256.c | 11 +++++++++-- sshsha.c | 12 ++++++++++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/ssh.h b/ssh.h index 4039b00b..2f946e83 100644 --- a/ssh.h +++ b/ssh.h @@ -258,11 +258,12 @@ void hmacmd5_key(void *handle, void const *key, int len); void hmacmd5_do_hmac(void *handle, unsigned char const *blk, int len, unsigned char *hmac); -typedef struct { +typedef struct SHA_State { uint32 h[5]; unsigned char block[64]; int blkused; uint32 lenhi, lenlo; + void (*sha1)(struct SHA_State * s, const unsigned char *p, int len); } SHA_State; void SHA_Init(SHA_State * s); void SHA_Bytes(SHA_State * s, const void *p, int len); @@ -271,11 +272,12 @@ void SHA_Simple(const void *p, int len, unsigned char *output); void hmac_sha1_simple(void *key, int keylen, void *data, int datalen, unsigned char *output); -typedef struct { +typedef struct SHA256_State { uint32 h[8]; unsigned char block[64]; int blkused; uint32 lenhi, lenlo; + void (*sha256)(struct SHA256_State * s, const unsigned char *p, int len); } SHA256_State; void SHA256_Init(SHA256_State * s); void SHA256_Bytes(SHA256_State * s, const void *p, int len); diff --git a/sshsh256.c b/sshsh256.c index 4186f3e8..e83266b5 100644 --- a/sshsh256.c +++ b/sshsh256.c @@ -19,6 +19,8 @@ #define smallsigma0(x) ( ror((x),7) ^ ror((x),18) ^ shr((x),3) ) #define smallsigma1(x) ( ror((x),17) ^ ror((x),19) ^ shr((x),10) ) +static void SHA256_sw(SHA256_State *s, const unsigned char *q, int len); + void SHA256_Core_Init(SHA256_State *s) { s->h[0] = 0x6a09e667; s->h[1] = 0xbb67ae85; @@ -97,19 +99,24 @@ void SHA256_Init(SHA256_State *s) { SHA256_Core_Init(s); s->blkused = 0; s->lenhi = s->lenlo = 0; + s->sha256 = &SHA256_sw; } void SHA256_Bytes(SHA256_State *s, const void *p, int len) { unsigned char *q = (unsigned char *)p; - uint32 wordblock[16]; uint32 lenw = len; - int i; /* * Update the length field. */ s->lenlo += lenw; s->lenhi += (s->lenlo < lenw); + (*(s->sha256))(s, q, len); +} + +static void SHA256_sw(SHA256_State *s, const unsigned char *q, int len) { + uint32 wordblock[16]; + int i; if (s->blkused && s->blkused+len < BLKSIZE) { /* diff --git a/sshsha.c b/sshsha.c index c10a8217..711b08a6 100644 --- a/sshsha.c +++ b/sshsha.c @@ -13,6 +13,8 @@ #define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) ) +static void sha1_sw(SHA_State * s, const unsigned char *q, int len); + static void SHA_Core_Init(uint32 h[5]) { h[0] = 0x67452301; @@ -124,20 +126,26 @@ void SHA_Init(SHA_State * s) SHA_Core_Init(s->h); s->blkused = 0; s->lenhi = s->lenlo = 0; + s->sha1 = &sha1_sw; } void SHA_Bytes(SHA_State * s, const void *p, int len) { const unsigned char *q = (const unsigned char *) p; - uint32 wordblock[16]; uint32 lenw = len; - int i; /* * Update the length field. */ s->lenlo += lenw; s->lenhi += (s->lenlo < lenw); + (*(s->sha1))(s, q, len); +} + +static void sha1_sw(SHA_State * s, const unsigned char *q, int len) +{ + uint32 wordblock[16]; + int i; if (s->blkused && s->blkused + len < 64) { /*