13 /* max size of either a cipher/hash block or symmetric key [largest of the two] */
14 #define MAXBLOCKSIZE 128
16 /* descriptor table size */
19 /* error codes [will be expanded in future releases] */
21 CRYPT_OK=0, /* Result OK */
22 CRYPT_ERROR, /* Generic Error */
23 CRYPT_NOP, /* Not a failure but no operation was performed */
25 CRYPT_INVALID_KEYSIZE, /* Invalid key size given */
26 CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */
27 CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */
29 CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */
30 CRYPT_INVALID_PACKET, /* Invalid input packet given */
32 CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */
33 CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */
35 CRYPT_INVALID_CIPHER, /* Invalid cipher specified */
36 CRYPT_INVALID_HASH, /* Invalid hash specified */
37 CRYPT_INVALID_PRNG, /* Invalid PRNG specified */
39 CRYPT_MEM, /* Out of memory */
41 CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */
42 CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */
44 CRYPT_INVALID_ARG, /* Generic invalid argument */
45 CRYPT_FILE_NOTFOUND, /* File Not Found */
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 */
53 CRYPT_INVALID_PRIME_SIZE,/* Invalid size of prime requested */
54 CRYPT_PK_INVALID_PADDING /* Invalid padding on input */
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)
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); }
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)); }
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); }
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)
88 #define MIN(x, y) ( ((x)<(y))?(x):(y) )
93 uint32_t state[8], curlen;
94 unsigned char buf[64];
97 typedef union Hash_state {
99 struct sha256_state sha256;
103 /** hash descriptor */
104 extern struct ltc_hash_descriptor {
109 /** Size of digest in octets */
110 unsigned long hashsize;
111 /** Input block size in octets */
112 unsigned long blocksize;
114 unsigned long OID[16];
115 /** Length of DER encoding */
116 unsigned long OIDlen;
118 /** Init a hash state
119 @param hash The hash to initialize
120 @return CRYPT_OK if successful
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
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
135 int (*done)(hash_state *hash, unsigned char *out);
137 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
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);
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;
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);
162 LTC_MUTEX_PROTO(ltc_hash_mutex)
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, ...);
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);
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) \
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; \
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) { \
191 md-> state_var .length += block_size * 8; \
193 inlen -= block_size; \
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; \
200 if (md-> state_var .curlen == block_size) { \
201 if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \
204 md-> state_var .length += 8*block_size; \
205 md-> state_var .curlen = 0; \