OLD | NEW |
1 /* crypto/sha/sha256.c */ | 1 /* crypto/sha/sha256.c */ |
2 /* ==================================================================== | 2 /* ==================================================================== |
3 * Copyright (c) 2004 The OpenSSL Project. All rights reserved | 3 * Copyright (c) 2004 The OpenSSL Project. All rights reserved |
4 * according to the OpenSSL license [found in ../../LICENSE]. | 4 * according to the OpenSSL license [found in ../../LICENSE]. |
5 * ==================================================================== | 5 * ==================================================================== |
6 */ | 6 */ |
7 #include <openssl/opensslconf.h> | 7 #include <openssl/opensslconf.h> |
8 #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA256) | 8 #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA256) |
9 | 9 |
10 #include <stdlib.h> | 10 #include <stdlib.h> |
11 #include <string.h> | 11 #include <string.h> |
12 | 12 |
13 #include <openssl/crypto.h> | 13 #include <openssl/crypto.h> |
14 #include <openssl/sha.h> | 14 #include <openssl/sha.h> |
15 #ifdef OPENSSL_FIPS | |
16 #include <openssl/fips.h> | |
17 #endif | |
18 | |
19 #include <openssl/opensslv.h> | 15 #include <openssl/opensslv.h> |
20 | 16 |
21 const char SHA256_version[]="SHA-256" OPENSSL_VERSION_PTEXT; | 17 const char SHA256_version[]="SHA-256" OPENSSL_VERSION_PTEXT; |
22 | 18 |
23 int SHA224_Init (SHA256_CTX *c) | 19 int SHA224_Init (SHA256_CTX *c) |
24 { | 20 { |
25 #ifdef OPENSSL_FIPS | 21 » memset (c,0,sizeof(*c)); |
26 » FIPS_selftest_check(); | |
27 #endif | |
28 c->h[0]=0xc1059ed8UL; c->h[1]=0x367cd507UL; | 22 c->h[0]=0xc1059ed8UL; c->h[1]=0x367cd507UL; |
29 c->h[2]=0x3070dd17UL; c->h[3]=0xf70e5939UL; | 23 c->h[2]=0x3070dd17UL; c->h[3]=0xf70e5939UL; |
30 c->h[4]=0xffc00b31UL; c->h[5]=0x68581511UL; | 24 c->h[4]=0xffc00b31UL; c->h[5]=0x68581511UL; |
31 c->h[6]=0x64f98fa7UL; c->h[7]=0xbefa4fa4UL; | 25 c->h[6]=0x64f98fa7UL; c->h[7]=0xbefa4fa4UL; |
32 » c->Nl=0;» c->Nh=0; | 26 » c->md_len=SHA224_DIGEST_LENGTH; |
33 » c->num=0;» c->md_len=SHA224_DIGEST_LENGTH; | |
34 return 1; | 27 return 1; |
35 } | 28 } |
36 | 29 |
37 int SHA256_Init (SHA256_CTX *c) | 30 int SHA256_Init (SHA256_CTX *c) |
38 { | 31 { |
39 #ifdef OPENSSL_FIPS | 32 » memset (c,0,sizeof(*c)); |
40 » FIPS_selftest_check(); | |
41 #endif | |
42 c->h[0]=0x6a09e667UL; c->h[1]=0xbb67ae85UL; | 33 c->h[0]=0x6a09e667UL; c->h[1]=0xbb67ae85UL; |
43 c->h[2]=0x3c6ef372UL; c->h[3]=0xa54ff53aUL; | 34 c->h[2]=0x3c6ef372UL; c->h[3]=0xa54ff53aUL; |
44 c->h[4]=0x510e527fUL; c->h[5]=0x9b05688cUL; | 35 c->h[4]=0x510e527fUL; c->h[5]=0x9b05688cUL; |
45 c->h[6]=0x1f83d9abUL; c->h[7]=0x5be0cd19UL; | 36 c->h[6]=0x1f83d9abUL; c->h[7]=0x5be0cd19UL; |
46 » c->Nl=0;» c->Nh=0; | 37 » c->md_len=SHA256_DIGEST_LENGTH; |
47 » c->num=0;» c->md_len=SHA256_DIGEST_LENGTH; | |
48 return 1; | 38 return 1; |
49 } | 39 } |
50 | 40 |
51 unsigned char *SHA224(const unsigned char *d, size_t n, unsigned char *md) | 41 unsigned char *SHA224(const unsigned char *d, size_t n, unsigned char *md) |
52 { | 42 { |
53 SHA256_CTX c; | 43 SHA256_CTX c; |
54 static unsigned char m[SHA224_DIGEST_LENGTH]; | 44 static unsigned char m[SHA224_DIGEST_LENGTH]; |
55 | 45 |
56 if (md == NULL) md=m; | 46 if (md == NULL) md=m; |
57 SHA224_Init(&c); | 47 SHA224_Init(&c); |
(...skipping 29 matching lines...) Expand all Loading... |
87 /* | 77 /* |
88 * Note that FIPS180-2 discusses "Truncation of the Hash Function Output." | 78 * Note that FIPS180-2 discusses "Truncation of the Hash Function Output." |
89 * default: case below covers for it. It's not clear however if it's | 79 * default: case below covers for it. It's not clear however if it's |
90 * permitted to truncate to amount of bytes not divisible by 4. I bet not, | 80 * permitted to truncate to amount of bytes not divisible by 4. I bet not, |
91 * but if it is, then default: case shall be extended. For reference. | 81 * but if it is, then default: case shall be extended. For reference. |
92 * Idea behind separate cases for pre-defined lenghts is to let the | 82 * Idea behind separate cases for pre-defined lenghts is to let the |
93 * compiler decide if it's appropriate to unroll small loops. | 83 * compiler decide if it's appropriate to unroll small loops. |
94 */ | 84 */ |
95 #define HASH_MAKE_STRING(c,s) do { \ | 85 #define HASH_MAKE_STRING(c,s) do { \ |
96 unsigned long ll; \ | 86 unsigned long ll; \ |
97 » unsigned int xn;» » \ | 87 » unsigned int nn;» » \ |
98 switch ((c)->md_len) \ | 88 switch ((c)->md_len) \ |
99 { case SHA224_DIGEST_LENGTH: \ | 89 { case SHA224_DIGEST_LENGTH: \ |
100 » » for (xn=0;xn<SHA224_DIGEST_LENGTH/4;xn++)» \ | 90 » » for (nn=0;nn<SHA224_DIGEST_LENGTH/4;nn++)» \ |
101 » » { ll=(c)->h[xn]; HOST_l2c(ll,(s)); }» \ | 91 » » { ll=(c)->h[nn]; HOST_l2c(ll,(s)); }» \ |
102 break; \ | 92 break; \ |
103 case SHA256_DIGEST_LENGTH: \ | 93 case SHA256_DIGEST_LENGTH: \ |
104 » » for (xn=0;xn<SHA256_DIGEST_LENGTH/4;xn++)» \ | 94 » » for (nn=0;nn<SHA256_DIGEST_LENGTH/4;nn++)» \ |
105 » » { ll=(c)->h[xn]; HOST_l2c(ll,(s)); }» \ | 95 » » { ll=(c)->h[nn]; HOST_l2c(ll,(s)); }» \ |
106 break; \ | 96 break; \ |
107 default: \ | 97 default: \ |
108 if ((c)->md_len > SHA256_DIGEST_LENGTH) \ | 98 if ((c)->md_len > SHA256_DIGEST_LENGTH) \ |
109 return 0; \ | 99 return 0; \ |
110 » » for (xn=0;xn<(c)->md_len/4;xn++)» » \ | 100 » » for (nn=0;nn<(c)->md_len/4;nn++)» » \ |
111 » » { ll=(c)->h[xn]; HOST_l2c(ll,(s)); }» \ | 101 » » { ll=(c)->h[nn]; HOST_l2c(ll,(s)); }» \ |
112 break; \ | 102 break; \ |
113 } \ | 103 } \ |
114 } while (0) | 104 } while (0) |
115 | 105 |
116 #define HASH_UPDATE SHA256_Update | 106 #define HASH_UPDATE SHA256_Update |
117 #define HASH_TRANSFORM SHA256_Transform | 107 #define HASH_TRANSFORM SHA256_Transform |
118 #define HASH_FINAL SHA256_Final | 108 #define HASH_FINAL SHA256_Final |
119 #define HASH_BLOCK_DATA_ORDER sha256_block_data_order | 109 #define HASH_BLOCK_DATA_ORDER sha256_block_data_order |
120 #ifndef SHA256_ASM | 110 #ifndef SHA256_ASM |
121 static | 111 static |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 ctx->h[0] += a; ctx->h[1] += b; ctx->h[2] += c; ctx->h[3] += d; | 273 ctx->h[0] += a; ctx->h[1] += b; ctx->h[2] += c; ctx->h[3] += d; |
284 ctx->h[4] += e; ctx->h[5] += f; ctx->h[6] += g; ctx->h[7] += h; | 274 ctx->h[4] += e; ctx->h[5] += f; ctx->h[6] += g; ctx->h[7] += h; |
285 | 275 |
286 } | 276 } |
287 } | 277 } |
288 | 278 |
289 #endif | 279 #endif |
290 #endif /* SHA256_ASM */ | 280 #endif /* SHA256_ASM */ |
291 | 281 |
292 #endif /* OPENSSL_NO_SHA256 */ | 282 #endif /* OPENSSL_NO_SHA256 */ |
OLD | NEW |