]> pd.if.org Git - zpackage/blob - lib/sha256.h
check for non-regular file in zpm test
[zpackage] / lib / sha256.h
1 #ifndef TOMCRYPT_H_
2 #define TOMCRYPT_H_
3 #include <assert.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <time.h>
8 #include <ctype.h>
9 #include <limits.h>
10
11 #include <stdint.h>
12
13 /* max size of either a cipher/hash block or symmetric key [largest of the two] */
14 #define MAXBLOCKSIZE  128
15
16 /* descriptor table size */
17 #define TAB_SIZE      32
18
19 /* error codes [will be expanded in future releases] */
20 enum {
21    CRYPT_OK=0,             /* Result OK */
22    CRYPT_ERROR,            /* Generic Error */
23    CRYPT_NOP,              /* Not a failure but no operation was performed */
24
25    CRYPT_INVALID_KEYSIZE,  /* Invalid key size given */
26    CRYPT_INVALID_ROUNDS,   /* Invalid number of rounds */
27    CRYPT_FAIL_TESTVECTOR,  /* Algorithm failed test vectors */
28
29    CRYPT_BUFFER_OVERFLOW,  /* Not enough space for output */
30    CRYPT_INVALID_PACKET,   /* Invalid input packet given */
31
32    CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */
33    CRYPT_ERROR_READPRNG,   /* Could not read enough from PRNG */
34
35    CRYPT_INVALID_CIPHER,   /* Invalid cipher specified */
36    CRYPT_INVALID_HASH,     /* Invalid hash specified */
37    CRYPT_INVALID_PRNG,     /* Invalid PRNG specified */
38
39    CRYPT_MEM,              /* Out of memory */
40
41    CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */
42    CRYPT_PK_NOT_PRIVATE,   /* Requires a private PK key */
43
44    CRYPT_INVALID_ARG,      /* Generic invalid argument */
45    CRYPT_FILE_NOTFOUND,    /* File Not Found */
46
47    CRYPT_PK_INVALID_TYPE,  /* Invalid type of PK key */
48    CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */
49    CRYPT_PK_DUP,           /* Duplicate key already in key ring */
50    CRYPT_PK_NOT_FOUND,     /* Key not found in keyring */
51    CRYPT_PK_INVALID_SIZE,  /* Invalid size input for PK parameters */
52
53    CRYPT_INVALID_PRIME_SIZE,/* Invalid size of prime requested */
54    CRYPT_PK_INVALID_PADDING /* Invalid padding on input */
55 };
56
57 #define LTC_MUTEX_GLOBAL(x)
58 #define LTC_MUTEX_PROTO(x)
59 #define LTC_MUTEX_TYPE(x)
60 #define LTC_MUTEX_INIT(x)
61 #define LTC_MUTEX_LOCK(x)
62 #define LTC_MUTEX_UNLOCK(x)
63
64 #define STORE32H(x, y)                                                                     \
65      { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255);   \
66        (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
67
68 #define LOAD32H(x, y)                            \
69      { x = ((unsigned long)((y)[0] & 255)<<24) | \
70            ((unsigned long)((y)[1] & 255)<<16) | \
71            ((unsigned long)((y)[2] & 255)<<8)  | \
72            ((unsigned long)((y)[3] & 255)); }
73
74 #define STORE64H(x, y)                                                                     \
75    { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255);     \
76      (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255);     \
77      (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255);     \
78      (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
79
80 /* rotates the hard way */
81 #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
82 #define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
83 #define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
84 #define RORc(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
85
86
87 #ifndef MIN
88    #define MIN(x, y) ( ((x)<(y))?(x):(y) )
89 #endif
90
91 struct sha256_state {
92     uint64_t length;
93     uint32_t state[8], curlen;
94     unsigned char buf[64];
95 };
96
97 typedef union Hash_state {
98     char dummy[1];
99     struct sha256_state sha256;
100     void *data;
101 } hash_state;
102
103 /** hash descriptor */
104 extern  struct ltc_hash_descriptor {
105     /** name of hash */
106     char *name;
107     /** internal ID */
108     unsigned char ID;
109     /** Size of digest in octets */
110     unsigned long hashsize;
111     /** Input block size in octets */
112     unsigned long blocksize;
113     /** ASN.1 OID */
114     unsigned long OID[16];
115     /** Length of DER encoding */
116     unsigned long OIDlen;
117
118     /** Init a hash state
119       @param hash   The hash to initialize
120       @return CRYPT_OK if successful
121     */
122     int (*init)(hash_state *hash);
123     /** Process a block of data 
124       @param hash   The hash state
125       @param in     The data to hash
126       @param inlen  The length of the data (octets)
127       @return CRYPT_OK if successful
128     */
129     int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen);
130     /** Produce the digest and store it
131       @param hash   The hash state
132       @param out    [out] The destination of the digest
133       @return CRYPT_OK if successful
134     */
135     int (*done)(hash_state *hash, unsigned char *out);
136     /** Self-test
137       @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
138     */
139     int (*test)(void);
140
141     /* accelerated hmac callback: if you need to-do multiple packets just use the generic hmac_memory and provide a hash callback */
142     int  (*hmac_block)(const unsigned char *key, unsigned long  keylen,
143                        const unsigned char *in,  unsigned long  inlen, 
144                              unsigned char *out, unsigned long *outlen);
145
146 } hash_descriptor[];
147
148 int sha256_init(hash_state * md);
149 int sha256_process(hash_state * md, const unsigned char *in, unsigned long inlen);
150 int sha256_done(hash_state * md, unsigned char *hash);
151 int sha256_test(void);
152 extern const struct ltc_hash_descriptor sha256_desc;
153
154 int find_hash(const char *name);
155 int find_hash_id(unsigned char ID);
156 int find_hash_oid(const unsigned long *ID, unsigned long IDlen);
157 int find_hash_any(const char *name, int digestlen);
158 int register_hash(const struct ltc_hash_descriptor *hash);
159 int unregister_hash(const struct ltc_hash_descriptor *hash);
160 int hash_is_valid(int idx);
161
162 LTC_MUTEX_PROTO(ltc_hash_mutex)
163
164 int hash_memory(int hash, 
165                 const unsigned char *in,  unsigned long inlen, 
166                       unsigned char *out, unsigned long *outlen);
167 int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen,
168                       const unsigned char *in, unsigned long inlen, ...);
169
170 #ifndef LTC_NO_FILE
171 int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen);
172 int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen);
173 #endif
174
175 /* a simple macro for making hash "process" functions */
176 #define HASH_PROCESS(func_name, compress_name, state_var, block_size)                       \
177 int func_name (hash_state * md, const unsigned char *in, unsigned long inlen)               \
178 {                                                                                           \
179     unsigned long n;                                                                        \
180     int           err;                                                                      \
181     LTC_ARGCHK(md != NULL);                                                                 \
182     LTC_ARGCHK(in != NULL);                                                                 \
183     if (md-> state_var .curlen > sizeof(md-> state_var .buf)) {                             \
184        return CRYPT_INVALID_ARG;                                                            \
185     }                                                                                       \
186     while (inlen > 0) {                                                                     \
187         if (md-> state_var .curlen == 0 && inlen >= block_size) {                           \
188            if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) {               \
189               return err;                                                                   \
190            }                                                                                \
191            md-> state_var .length += block_size * 8;                                        \
192            in             += block_size;                                                    \
193            inlen          -= block_size;                                                    \
194         } else {                                                                            \
195            n = MIN(inlen, (block_size - md-> state_var .curlen));                           \
196            memcpy(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n);              \
197            md-> state_var .curlen += n;                                                     \
198            in             += n;                                                             \
199            inlen          -= n;                                                             \
200            if (md-> state_var .curlen == block_size) {                                      \
201               if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) {            \
202                  return err;                                                                \
203               }                                                                             \
204               md-> state_var .length += 8*block_size;                                       \
205               md-> state_var .curlen = 0;                                                   \
206            }                                                                                \
207        }                                                                                    \
208     }                                                                                       \
209     return CRYPT_OK;                                                                        \
210 }
211
212 #endif
213 /* TOMCRYPT_H_ */