custom-crypto.h (7190B)
1/** 2 * WinPR: Windows Portable Runtime 3 * Cryptography API (CryptoAPI) 4 * 5 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20#ifndef WINPR_CUSTOM_CRYPTO_H 21#define WINPR_CUSTOM_CRYPTO_H 22 23#include <winpr/winpr.h> 24#include <winpr/wtypes.h> 25 26#include <winpr/error.h> 27 28/** 29 * Custom Crypto API Abstraction Layer 30 */ 31 32#define WINPR_MD4_DIGEST_LENGTH 16 33#define WINPR_MD5_DIGEST_LENGTH 16 34#define WINPR_SHA1_DIGEST_LENGTH 20 35#define WINPR_SHA224_DIGEST_LENGTH 28 36#define WINPR_SHA256_DIGEST_LENGTH 32 37#define WINPR_SHA384_DIGEST_LENGTH 48 38#define WINPR_SHA512_DIGEST_LENGTH 64 39#define WINPR_RIPEMD160_DIGEST_LENGTH 20 40#define WINPR_SHA3_224_DIGEST_LENGTH 28 41#define WINPR_SHA3_256_DIGEST_LENGTH 32 42#define WINPR_SHA3_384_DIGEST_LENGTH 48 43#define WINPR_SHA3_512_DIGEST_LENGTH 64 44#define WINPR_SHAKE128_DIGEST_LENGTH 16 45#define WINPR_SHAKE256_DIGEST_LENGTH 32 46 47/** 48 * HMAC 49 */ 50typedef enum 51{ 52 WINPR_MD_NONE = 0, 53 WINPR_MD_MD2 = 1, 54 WINPR_MD_MD4 = 2, 55 WINPR_MD_MD5 = 3, 56 WINPR_MD_SHA1 = 4, 57 WINPR_MD_SHA224 = 5, 58 WINPR_MD_SHA256 = 6, 59 WINPR_MD_SHA384 = 7, 60 WINPR_MD_SHA512 = 8, 61 WINPR_MD_RIPEMD160 = 9, 62 WINPR_MD_SHA3_224 = 10, 63 WINPR_MD_SHA3_256 = 11, 64 WINPR_MD_SHA3_384 = 12, 65 WINPR_MD_SHA3_512 = 13, 66 WINPR_MD_SHAKE128 = 14, 67 WINPR_MD_SHAKE256 = 15 68} WINPR_MD_TYPE; 69 70typedef struct _winpr_hmac_ctx_private_st WINPR_HMAC_CTX; 71 72#ifdef __cplusplus 73extern "C" 74{ 75#endif 76 77 WINPR_API WINPR_MD_TYPE winpr_md_type_from_string(const char* name); 78 WINPR_API const char* winpr_md_type_to_string(WINPR_MD_TYPE md); 79 80 WINPR_API WINPR_HMAC_CTX* winpr_HMAC_New(void); 81 WINPR_API BOOL winpr_HMAC_Init(WINPR_HMAC_CTX* ctx, WINPR_MD_TYPE md, const BYTE* key, 82 size_t keylen); 83 WINPR_API BOOL winpr_HMAC_Update(WINPR_HMAC_CTX* ctx, const BYTE* input, size_t ilen); 84 WINPR_API BOOL winpr_HMAC_Final(WINPR_HMAC_CTX* ctx, BYTE* output, size_t ilen); 85 WINPR_API void winpr_HMAC_Free(WINPR_HMAC_CTX* ctx); 86 WINPR_API BOOL winpr_HMAC(WINPR_MD_TYPE md, const BYTE* key, size_t keylen, const BYTE* input, 87 size_t ilen, BYTE* output, size_t olen); 88 89#ifdef __cplusplus 90} 91#endif 92 93/** 94 * Generic Digest API 95 */ 96 97typedef struct _winpr_digest_ctx_private_st WINPR_DIGEST_CTX; 98 99#ifdef __cplusplus 100extern "C" 101{ 102#endif 103 104 WINPR_API WINPR_DIGEST_CTX* winpr_Digest_New(void); 105 WINPR_API BOOL winpr_Digest_Init_Allow_FIPS(WINPR_DIGEST_CTX* ctx, WINPR_MD_TYPE md); 106 WINPR_API BOOL winpr_Digest_Init(WINPR_DIGEST_CTX* ctx, WINPR_MD_TYPE md); 107 WINPR_API BOOL winpr_Digest_Update(WINPR_DIGEST_CTX* ctx, const BYTE* input, size_t ilen); 108 WINPR_API BOOL winpr_Digest_Final(WINPR_DIGEST_CTX* ctx, BYTE* output, size_t ilen); 109 WINPR_API void winpr_Digest_Free(WINPR_DIGEST_CTX* ctx); 110 WINPR_API BOOL winpr_Digest_Allow_FIPS(WINPR_MD_TYPE md, const BYTE* input, size_t ilen, 111 BYTE* output, size_t olen); 112 WINPR_API BOOL winpr_Digest(WINPR_MD_TYPE md, const BYTE* input, size_t ilen, BYTE* output, 113 size_t olen); 114 115#ifdef __cplusplus 116} 117#endif 118 119/** 120 * Random Number Generation 121 */ 122 123#ifdef __cplusplus 124extern "C" 125{ 126#endif 127 128 WINPR_API int winpr_RAND(BYTE* output, size_t len); 129 WINPR_API int winpr_RAND_pseudo(BYTE* output, size_t len); 130 131#ifdef __cplusplus 132} 133#endif 134 135/** 136 * RC4 137 */ 138 139typedef struct _winpr_rc4_ctx_private_st WINPR_RC4_CTX; 140 141#ifdef __cplusplus 142extern "C" 143{ 144#endif 145 146 WINPR_API WINPR_RC4_CTX* winpr_RC4_New_Allow_FIPS(const BYTE* key, size_t keylen); 147 WINPR_API WINPR_RC4_CTX* winpr_RC4_New(const BYTE* key, size_t keylen); 148 WINPR_API BOOL winpr_RC4_Update(WINPR_RC4_CTX* ctx, size_t length, const BYTE* input, 149 BYTE* output); 150 WINPR_API void winpr_RC4_Free(WINPR_RC4_CTX* ctx); 151 152#ifdef __cplusplus 153} 154#endif 155 156/** 157 * Generic Cipher API 158 */ 159 160#define WINPR_AES_BLOCK_SIZE 16 161 162/* cipher operation types */ 163#define WINPR_ENCRYPT 0 164#define WINPR_DECRYPT 1 165 166/* cipher types */ 167#define WINPR_CIPHER_NONE 0 168#define WINPR_CIPHER_NULL 1 169#define WINPR_CIPHER_AES_128_ECB 2 170#define WINPR_CIPHER_AES_192_ECB 3 171#define WINPR_CIPHER_AES_256_ECB 4 172#define WINPR_CIPHER_AES_128_CBC 5 173#define WINPR_CIPHER_AES_192_CBC 6 174#define WINPR_CIPHER_AES_256_CBC 7 175#define WINPR_CIPHER_AES_128_CFB128 8 176#define WINPR_CIPHER_AES_192_CFB128 9 177#define WINPR_CIPHER_AES_256_CFB128 10 178#define WINPR_CIPHER_AES_128_CTR 11 179#define WINPR_CIPHER_AES_192_CTR 12 180#define WINPR_CIPHER_AES_256_CTR 13 181#define WINPR_CIPHER_AES_128_GCM 14 182#define WINPR_CIPHER_AES_192_GCM 15 183#define WINPR_CIPHER_AES_256_GCM 16 184#define WINPR_CIPHER_CAMELLIA_128_ECB 17 185#define WINPR_CIPHER_CAMELLIA_192_ECB 18 186#define WINPR_CIPHER_CAMELLIA_256_ECB 19 187#define WINPR_CIPHER_CAMELLIA_128_CBC 20 188#define WINPR_CIPHER_CAMELLIA_192_CBC 21 189#define WINPR_CIPHER_CAMELLIA_256_CBC 22 190#define WINPR_CIPHER_CAMELLIA_128_CFB128 23 191#define WINPR_CIPHER_CAMELLIA_192_CFB128 24 192#define WINPR_CIPHER_CAMELLIA_256_CFB128 25 193#define WINPR_CIPHER_CAMELLIA_128_CTR 26 194#define WINPR_CIPHER_CAMELLIA_192_CTR 27 195#define WINPR_CIPHER_CAMELLIA_256_CTR 28 196#define WINPR_CIPHER_CAMELLIA_128_GCM 29 197#define WINPR_CIPHER_CAMELLIA_192_GCM 30 198#define WINPR_CIPHER_CAMELLIA_256_GCM 31 199#define WINPR_CIPHER_DES_ECB 32 200#define WINPR_CIPHER_DES_CBC 33 201#define WINPR_CIPHER_DES_EDE_ECB 34 202#define WINPR_CIPHER_DES_EDE_CBC 35 203#define WINPR_CIPHER_DES_EDE3_ECB 36 204#define WINPR_CIPHER_DES_EDE3_CBC 37 205#define WINPR_CIPHER_BLOWFISH_ECB 38 206#define WINPR_CIPHER_BLOWFISH_CBC 39 207#define WINPR_CIPHER_BLOWFISH_CFB64 40 208#define WINPR_CIPHER_BLOWFISH_CTR 41 209#define WINPR_CIPHER_ARC4_128 42 210#define WINPR_CIPHER_AES_128_CCM 43 211#define WINPR_CIPHER_AES_192_CCM 44 212#define WINPR_CIPHER_AES_256_CCM 45 213#define WINPR_CIPHER_CAMELLIA_128_CCM 46 214#define WINPR_CIPHER_CAMELLIA_192_CCM 47 215#define WINPR_CIPHER_CAMELLIA_256_CCM 48 216 217typedef struct _winpr_cipher_ctx_private_st WINPR_CIPHER_CTX; 218 219#ifdef __cplusplus 220extern "C" 221{ 222#endif 223 224 WINPR_API WINPR_CIPHER_CTX* winpr_Cipher_New(int cipher, int op, const BYTE* key, 225 const BYTE* iv); 226 WINPR_API BOOL winpr_Cipher_Update(WINPR_CIPHER_CTX* ctx, const BYTE* input, size_t ilen, 227 BYTE* output, size_t* olen); 228 WINPR_API BOOL winpr_Cipher_Final(WINPR_CIPHER_CTX* ctx, BYTE* output, size_t* olen); 229 WINPR_API void winpr_Cipher_Free(WINPR_CIPHER_CTX* ctx); 230 231#ifdef __cplusplus 232} 233#endif 234 235/** 236 * Key Generation 237 */ 238 239#ifdef __cplusplus 240extern "C" 241{ 242#endif 243 244 WINPR_API int winpr_Cipher_BytesToKey(int cipher, int md, const BYTE* salt, const BYTE* data, 245 int datal, int count, BYTE* key, BYTE* iv); 246 247#ifdef __cplusplus 248} 249#endif 250 251#endif /* WINPR_CUSTOM_CRYPTO_H */