1 /* ppp-crypto.h - Generic API for access to crypto/digest functions. 2 * 3 * Copyright (c) 2022 Eivind Nss. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * 3. The name(s) of the authors of this software must not be used to 18 * endorse or promote products derived from this software without 19 * prior written permission. 20 * 21 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO 22 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 23 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY 24 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 25 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 26 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 27 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 28 */ 29 30 #ifndef PPP_CRYPTO_H 31 #define PPP_CRYPTO_H 32 33 #ifdef __cplusplus 34 extern "C" { 35 #endif 36 37 #ifndef MD5_DIGEST_LENGTH 38 #define MD5_DIGEST_LENGTH 16 39 #endif 40 41 #ifndef MD4_DIGEST_LENGTH 42 #define MD4_DIGEST_LENGTH 16 43 #endif 44 45 #ifndef SHA_DIGEST_LENGTH 46 #define SHA_DIGEST_LENGTH 20 47 #endif 48 49 struct _PPP_MD_CTX; 50 struct _PPP_MD; 51 52 typedef struct _PPP_MD_CTX PPP_MD_CTX; 53 typedef struct _PPP_MD PPP_MD; 54 55 /* 56 * Create a new Message Digest context object 57 */ 58 PPP_MD_CTX *PPP_MD_CTX_new(void); 59 60 /* 61 * Free the Message Digest context 62 */ 63 void PPP_MD_CTX_free(PPP_MD_CTX*); 64 65 /* 66 * Fetch the MD4 algorithm 67 */ 68 const PPP_MD *PPP_md4(void); 69 70 /* 71 * Fetch the MD5 algorithm 72 */ 73 const PPP_MD *PPP_md5(void); 74 75 /* 76 * Fetch the SHA1 algorithm 77 */ 78 const PPP_MD *PPP_sha1(void); 79 80 /* 81 * Initializes a context object 82 */ 83 int PPP_DigestInit(PPP_MD_CTX *ctx, 84 const PPP_MD *type); 85 86 /* 87 * For each iteration update the context with more input 88 */ 89 int PPP_DigestUpdate(PPP_MD_CTX *ctx, 90 const void *data, size_t cnt); 91 92 /* 93 * Perform the final operation, and output the digest 94 */ 95 int PPP_DigestFinal(PPP_MD_CTX *ctx, 96 unsigned char *out, unsigned int *outlen); 97 98 99 struct _PPP_CIPHER_CTX; 100 struct _PPP_CIPHER; 101 102 typedef struct _PPP_CIPHER_CTX PPP_CIPHER_CTX; 103 typedef struct _PPP_CIPHER PPP_CIPHER; 104 105 106 /* 107 * Create a new Cipher Context 108 */ 109 PPP_CIPHER_CTX *PPP_CIPHER_CTX_new(void); 110 111 /* 112 * Release the Cipher Context 113 */ 114 void PPP_CIPHER_CTX_free(PPP_CIPHER_CTX *ctx); 115 116 /* 117 * Fetch the DES in ECB mode cipher algorithm 118 */ 119 const PPP_CIPHER *PPP_des_ecb(void); 120 121 /* 122 * Set the particular data directly 123 */ 124 void PPP_CIPHER_CTX_set_cipher_data(PPP_CIPHER_CTX *ctx, 125 const unsigned char *key); 126 127 /* 128 * Initialize the crypto operation 129 */ 130 int PPP_CipherInit(PPP_CIPHER_CTX *ctx, 131 const PPP_CIPHER *cipher, 132 const unsigned char *key, 133 const unsigned char *iv, 134 int encr); 135 136 /* 137 * Encrypt input data, and store it in the output buffer 138 */ 139 int PPP_CipherUpdate(PPP_CIPHER_CTX *ctx, 140 unsigned char *out, int *outl, 141 const unsigned char *in, int inl); 142 143 /* 144 * Finish the crypto operation, and fetch any outstanding bytes 145 */ 146 int PPP_CipherFinal(PPP_CIPHER_CTX *ctx, 147 unsigned char *out, int *outl); 148 149 /* 150 * Log an error message to the log and append the crypto error 151 */ 152 void PPP_crypto_error(char *fmt, ...); 153 154 /* 155 * Global initialization, must be called once per process 156 */ 157 int PPP_crypto_init(void); 158 159 /* 160 * Global deinitialization 161 */ 162 int PPP_crypto_deinit(void); 163 164 #ifdef __cplusplus 165 } 166 #endif 167 168 #endif // PPP_CRYPTO_H 169