1 /* 2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the License); you may 5 * not use this file except in compliance with the License. 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 */ 9 10 11 12 #ifndef GMSSL_DIGEST_H 13 #define GMSSL_DIGEST_H 14 15 16 #include <stdint.h> 17 #include <stdlib.h> 18 #include <gmssl/sm3.h> 19 #include <gmssl/md5.h> 20 #include <gmssl/sha1.h> 21 #include <gmssl/sha2.h> 22 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 29 typedef struct DIGEST DIGEST; 30 typedef struct DIGEST_CTX DIGEST_CTX; 31 32 33 #define DIGEST_MAX_SIZE 64 34 #define DIGEST_MAX_BLOCK_SIZE (1024/8) 35 36 37 struct DIGEST_CTX { 38 union { 39 SM3_CTX sm3_ctx; 40 // MD5_CTX md5_ctx; 41 SHA1_CTX sha1_ctx; 42 SHA224_CTX sha224_ctx; 43 SHA256_CTX sha256_ctx; 44 SHA384_CTX sha384_ctx; 45 SHA512_CTX sha512_ctx; 46 } u; 47 const DIGEST *digest; 48 }; 49 50 struct DIGEST { 51 int oid; 52 size_t digest_size; 53 size_t block_size; 54 size_t ctx_size; 55 int (*init)(DIGEST_CTX *ctx); 56 int (*update)(DIGEST_CTX *ctx, const uint8_t *data, size_t datalen); 57 int (*finish)(DIGEST_CTX *ctx, uint8_t *dgst); 58 }; 59 60 const DIGEST *DIGEST_sm3(void); 61 //const DIGEST *DIGEST_md5(void); 62 const DIGEST *DIGEST_sha1(void); 63 const DIGEST *DIGEST_sha224(void); 64 const DIGEST *DIGEST_sha256(void); 65 const DIGEST *DIGEST_sha384(void); 66 const DIGEST *DIGEST_sha512(void); 67 const DIGEST *DIGEST_sha512_224(void); 68 const DIGEST *DIGEST_sha512_256(void); 69 70 const DIGEST *digest_from_name(const char *name); 71 const char *digest_name(const DIGEST *digest); 72 int digest_init(DIGEST_CTX *ctx, const DIGEST *algor); 73 int digest_update(DIGEST_CTX *ctx, const uint8_t *data, size_t datalen); 74 int digest_finish(DIGEST_CTX *ctx, uint8_t *dgst, size_t *dgstlen); 75 int digest(const DIGEST *digest, const uint8_t *data, size_t datalen, uint8_t *dgst, size_t *dgstlen); 76 77 78 #ifdef __cplusplus 79 } 80 #endif 81 #endif 82