libosmogsm  1.9.0.24-e0d0.202311132026
Osmocom GSM library
crypto.h
Go to the documentation of this file.
1 /*
2  * WPA Supplicant / wrapper functions for crypto libraries
3  * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  *
14  * This file defines the cryptographic functions that need to be implemented
15  * for wpa_supplicant and hostapd. When TLS is not used, internal
16  * implementation of MD5, SHA1, and AES is used and no external libraries are
17  * required. When TLS is enabled (e.g., by enabling EAP-TLS or EAP-PEAP), the
18  * crypto library used by the TLS implementation is expected to be used for
19  * non-TLS needs, too, in order to save space by not implementing these
20  * functions twice.
21  *
22  * Wrapper code for using each crypto library is in its own file (crypto*.c)
23  * and one of these files is build and linked in to provide the functions
24  * defined here.
25  */
26 
27 #ifndef CRYPTO_H
28 #define CRYPTO_H
29 
38 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
39 
48 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
49 
50 #ifdef CONFIG_FIPS
51 
59 int md5_vector_non_fips_allow(size_t num_elem, const u8 *addr[],
60  const size_t *len, u8 *mac);
61 #else /* CONFIG_FIPS */
62 #define md5_vector_non_fips_allow md5_vector
63 #endif /* CONFIG_FIPS */
64 
65 
74 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len,
75  u8 *mac);
76 
89 int __must_check fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x,
90  size_t xlen);
91 
100 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
101  u8 *mac);
102 
109 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher);
110 
117 void * aes_encrypt_init(const u8 *key, size_t len);
118 
125 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
126 
131 void aes_encrypt_deinit(void *ctx);
132 
139 void * aes_decrypt_init(const u8 *key, size_t len);
140 
147 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
148 
153 void aes_decrypt_deinit(void *ctx);
154 
155 
160 };
161 
162 struct crypto_hash;
163 
176 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
177  size_t key_len);
178 
189 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len);
190 
208 int crypto_hash_finish(struct crypto_hash *ctx, u8 *hash, size_t *len);
209 
210 
214 };
215 
216 struct crypto_cipher;
217 
231 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
232  const u8 *iv, const u8 *key,
233  size_t key_len);
234 
247 int __must_check crypto_cipher_encrypt(struct crypto_cipher *ctx,
248  const u8 *plain, u8 *crypt, size_t len);
249 
262 int __must_check crypto_cipher_decrypt(struct crypto_cipher *ctx,
263  const u8 *crypt, u8 *plain, size_t len);
264 
273 void crypto_cipher_deinit(struct crypto_cipher *ctx);
274 
275 
276 struct crypto_public_key;
277 struct crypto_private_key;
278 
293 struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len);
294 
306 struct crypto_private_key * crypto_private_key_import(const u8 *key,
307  size_t len,
308  const char *passwd);
309 
324 struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
325  size_t len);
326 
341  struct crypto_public_key *key, const u8 *in, size_t inlen,
342  u8 *out, size_t *outlen);
343 
358  struct crypto_private_key *key, const u8 *in, size_t inlen,
359  u8 *out, size_t *outlen);
360 
374 int __must_check crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
375  const u8 *in, size_t inlen,
376  u8 *out, size_t *outlen);
377 
386 void crypto_public_key_free(struct crypto_public_key *key);
387 
396 void crypto_private_key_free(struct crypto_private_key *key);
397 
408  struct crypto_public_key *key, const u8 *crypt, size_t crypt_len,
409  u8 *plain, size_t *plain_len);
410 
419 
427 void crypto_global_deinit(void);
428 
449 int __must_check crypto_mod_exp(const u8 *base, size_t base_len,
450  const u8 *power, size_t power_len,
451  const u8 *modulus, size_t modulus_len,
452  u8 *result, size_t *result_len);
453 
467 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
468  u8 *data, size_t data_len);
469 
470 #endif /* CRYPTO_H */
void crypto_public_key_free(struct crypto_public_key *key)
crypto_public_key_free - Free public key : Public key
Definition: crypto.h:213
crypto_cipher_alg
Definition: crypto.h:211
int __must_check crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key, const u8 *in, size_t inlen, u8 *out, size_t *outlen)
crypto_public_key_encrypt_pkcs1_v15 - Public key encryption (PKCS #1 v1.5) : Public key : Plaintext b...
int __must_check crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain, u8 *crypt, size_t len)
crypto_cipher_encrypt - Cipher encrypt : Context pointer from crypto_cipher_init() : Plaintext to cip...
int crypto_hash_finish(struct crypto_hash *ctx, u8 *hash, size_t *len)
crypto_hash_finish - Complete hash calculation : Context pointer from crypto_hash_init() : Buffer for...
Definition: crypto.h:157
void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
crypto_hash_update - Add data to hash calculation : Context pointer from crypto_hash_init() : Data bu...
int __must_check crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key, const u8 *in, size_t inlen, u8 *out, size_t *outlen)
crypto_private_key_decrypt_pkcs1_v15 - Private key decryption (PKCS #1 v1.5) : Private key : Encrypte...
int __must_check fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
fips186_2-prf - NIST FIPS Publication 186-2 change notice 1 PRF : Seed/key for the PRF : Seed length ...
int __must_check crypto_mod_exp(const u8 *base, size_t base_len, const u8 *power, size_t power_len, const u8 *modulus, size_t modulus_len, u8 *result, size_t *result_len)
crypto_mod_exp - Modular exponentiation of large integers : Base integer (big endian byte array) : Le...
Definition: crypto.h:159
#define md5_vector_non_fips_allow
Definition: crypto.h:62
Definition: crypto.h:159
void crypto_cipher_deinit(struct crypto_cipher *ctx)
crypto_cipher_decrypt - Free cipher context : Context pointer from crypto_cipher_init() ...
void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
aes_decrypt - Decrypt one AES block : Context pointer from aes_encrypt_init() : Encrypted data (16 by...
uint8_t u8
Definition: common.h:27
void * aes_decrypt_init(const u8 *key, size_t len)
aes_decrypt_init - Initialize AES for decryption : Decryption key : Key length in bytes (usually 16...
Definition: crypto.h:158
uint8_t data[0]
Definition: crypto.h:158
crypto_hash_alg
Definition: crypto.h:156
int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
md5_vector - MD5 hash for data vector : Number of elements in the data vector : Pointers to the data ...
struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf, size_t len)
crypto_public_key_from_cert - Import an RSA public key from a certificate : DER encoded X...
void * aes_encrypt_init(const u8 *key, size_t len)
aes_encrypt_init - Initialize AES for encryption : Encryption key : Key length in bytes (usually 16...
Definition: aes-internal-enc.c:101
void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
aes_encrypt - Encrypt one AES block : Context pointer from aes_encrypt_init() : Plaintext data to be ...
Definition: aes-internal-enc.c:114
int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
md4_vector - MD4 hash for data vector : Number of elements in the data vector : Pointers to the data ...
int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
sha1_vector - SHA-1 hash for data vector : Number of elements in the data vector : Pointers to the da...
Definition: sha1-internal.c:35
int __must_check crypto_private_key_sign_pkcs1(struct crypto_private_key *key, const u8 *in, size_t inlen, u8 *out, size_t *outlen)
crypto_private_key_sign_pkcs1 - Sign with private key (PKCS #1) : Private key from crypto_private_key...
struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
crypto_public_key_import - Import an RSA public key : Key buffer (DER encoded RSA public key) : Key b...
uint8_t len
Definition: gsm_04_11.h:480
int __must_check crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key, const u8 *crypt, size_t crypt_len, u8 *plain, size_t *plain_len)
crypto_public_key_decrypt_pkcs1 - Decrypt PKCS #1 signature : Public key : Encrypted signature data (...
int rc4_skip(const u8 *key, size_t keylen, size_t skip, u8 *data, size_t data_len)
rc4_skip - XOR RC4 stream to given data with skip-stream-start : RC4 key : RC4 key length number of ...
Definition: crypto.h:157
struct crypto_private_key * crypto_private_key_import(const u8 *key, size_t len, const char *passwd)
crypto_private_key_import - Import an RSA private key : Key buffer (DER encoded RSA private key) : Ke...
void aes_decrypt_deinit(void *ctx)
aes_decrypt_deinit - Deinitialize AES decryption : Context pointer from aes_encrypt_init() ...
void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
des_encrypt - Encrypt one block with DES : 8 octets (in) : 7 octets (in) (no parity bits included) : ...
Definition: crypto.h:212
void crypto_private_key_free(struct crypto_private_key *key)
crypto_private_key_free - Free private key : Private key from crypto_private_key_import() ...
int __must_check crypto_global_init(void)
crypto_global_init - Initialize crypto wrapper
int __must_check crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt, u8 *plain, size_t len)
crypto_cipher_decrypt - Cipher decrypt : Context pointer from crypto_cipher_init() : Ciphertext to de...
struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key, size_t key_len)
crypto_hash_init - Initialize hash/HMAC function : Hash algorithm : Key for keyed hash (e...
Definition: crypto.h:213
Definition: crypto.h:212
struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg, const u8 *iv, const u8 *key, size_t key_len)
crypto_cipher_init - Initialize block/stream cipher function : Cipher algorithm : Initialization vect...
int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
sha256_vector - SHA256 hash for data vector : Number of elements in the data vector : Pointers to the...
Definition: sha256-internal.c:30
Definition: crypto.h:212
void aes_encrypt_deinit(void *ctx)
aes_encrypt_deinit - Deinitialize AES encryption : Context pointer from aes_encrypt_init() ...
Definition: aes-internal-enc.c:120
Definition: crypto.h:213
void crypto_global_deinit(void)
crypto_global_deinit - Deinitialize crypto wrapper
#define __must_check
Definition: common.h:101