xref: /php-src/ext/hash/php_hash_sha.h (revision 1d369271)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | SHA1 Author: Stefan Esser <sesser@php.net>                           |
14    | SHA256 Author: Sara Golemon <pollita@php.net>                        |
15    +----------------------------------------------------------------------+
16 */
17 
18 #ifndef PHP_HASH_SHA_H
19 #define PHP_HASH_SHA_H
20 
21 #include "ext/standard/sha1.h"
22 
23 /* SHA224 context. */
24 typedef struct {
25 	uint32_t state[8];		/* state */
26 	uint32_t count[2];		/* number of bits, modulo 2^64 */
27 	unsigned char buffer[64];	/* input buffer */
28 } PHP_SHA224_CTX;
29 #define PHP_SHA224_SPEC "l8l2b64."
30 
31 #define PHP_SHA224Init(ctx) PHP_SHA224InitArgs(ctx, NULL)
32 PHP_HASH_API void PHP_SHA224InitArgs(PHP_SHA224_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *);
33 PHP_HASH_API void PHP_SHA224Update(PHP_SHA224_CTX *, const unsigned char *, size_t);
34 PHP_HASH_API void PHP_SHA224Final(unsigned char[28], PHP_SHA224_CTX *);
35 
36 /* SHA256 context. */
37 typedef struct {
38 	uint32_t state[8];		/* state */
39 	uint32_t count[2];		/* number of bits, modulo 2^64 */
40 	unsigned char buffer[64];	/* input buffer */
41 } PHP_SHA256_CTX;
42 #define PHP_SHA256_SPEC "l8l2b64."
43 
44 #define PHP_SHA256Init(ctx) PHP_SHA256InitArgs(ctx, NULL)
45 PHP_HASH_API void PHP_SHA256InitArgs(PHP_SHA256_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *);
46 PHP_HASH_API void PHP_SHA256Update(PHP_SHA256_CTX *, const unsigned char *, size_t);
47 
48 #if defined(__cplusplus) || defined(_MSC_VER)
49 # define PHP_STATIC_RESTRICT
50 #else
51 # define PHP_STATIC_RESTRICT static restrict
52 #endif
53 
54 #if defined(__SSE2__)
55 void SHA256_Transform_sse2(uint32_t state[PHP_STATIC_RESTRICT 8], const uint8_t block[PHP_STATIC_RESTRICT 64], uint32_t W[PHP_STATIC_RESTRICT 64], uint32_t S[PHP_STATIC_RESTRICT 8]);
56 #endif
57 
58 #if ((defined(__i386__) || defined(__x86_64__)) && defined(HAVE_IMMINTRIN_H)) || defined(_M_X64) || defined(_M_IX86)
59 # if defined(__SSSE3__) && defined(__SHA__)
60 #  define PHP_HASH_INTRIN_SHA_NATIVE 1
61 # elif defined(HAVE_FUNC_ATTRIBUTE_TARGET) || defined(_M_X64) || defined(_M_IX86)
62 #  define PHP_HASH_INTRIN_SHA_RESOLVER 1
63 # endif
64 #endif
65 
66 #if defined(PHP_HASH_INTRIN_SHA_NATIVE) || defined(PHP_HASH_INTRIN_SHA_RESOLVER)
67 void SHA256_Transform_shani(uint32_t state[PHP_STATIC_RESTRICT 8], const uint8_t block[PHP_STATIC_RESTRICT 64]);
68 #endif
69 
70 PHP_HASH_API void PHP_SHA256Final(unsigned char[32], PHP_SHA256_CTX *);
71 
72 /* SHA384 context */
73 typedef struct {
74 	uint64_t state[8];	/* state */
75 	uint64_t count[2];	/* number of bits, modulo 2^128 */
76 	unsigned char buffer[128];	/* input buffer */
77 } PHP_SHA384_CTX;
78 #define PHP_SHA384_SPEC "q8q2b128."
79 
80 #define PHP_SHA384Init(ctx) PHP_SHA384InitArgs(ctx, NULL)
81 PHP_HASH_API void PHP_SHA384InitArgs(PHP_SHA384_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *);
82 PHP_HASH_API void PHP_SHA384Update(PHP_SHA384_CTX *, const unsigned char *, size_t);
83 PHP_HASH_API void PHP_SHA384Final(unsigned char[48], PHP_SHA384_CTX *);
84 
85 /* SHA512 context */
86 typedef struct {
87 	uint64_t state[8];	/* state */
88 	uint64_t count[2];	/* number of bits, modulo 2^128 */
89 	unsigned char buffer[128];	/* input buffer */
90 } PHP_SHA512_CTX;
91 #define PHP_SHA512_SPEC "q8q2b128."
92 
93 #define PHP_SHA512Init(ctx) PHP_SHA512InitArgs(ctx, NULL)
94 PHP_HASH_API void PHP_SHA512InitArgs(PHP_SHA512_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *);
95 PHP_HASH_API void PHP_SHA512Update(PHP_SHA512_CTX *, const unsigned char *, size_t);
96 PHP_HASH_API void PHP_SHA512Final(unsigned char[64], PHP_SHA512_CTX *);
97 
98 #define PHP_SHA512_256Init(ctx) PHP_SHA512_256InitArgs(ctx, NULL)
99 PHP_HASH_API void PHP_SHA512_256InitArgs(PHP_SHA512_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *);
100 #define PHP_SHA512_256Update PHP_SHA512Update
101 PHP_HASH_API void PHP_SHA512_256Final(unsigned char[32], PHP_SHA512_CTX *);
102 
103 #define PHP_SHA512_224Init(ctx) PHP_SHA512_224InitArgs(ctx, NULL)
104 PHP_HASH_API void PHP_SHA512_224InitArgs(PHP_SHA512_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *);
105 #define PHP_SHA512_224Update PHP_SHA512Update
106 PHP_HASH_API void PHP_SHA512_224Final(unsigned char[28], PHP_SHA512_CTX *);
107 
108 #endif /* PHP_HASH_SHA_H */
109