libosmogsm  1.11.0.26-c59d.202505112026
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
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 
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 */
uint8_t data[0]
uint8_t len
Definition: gsm_04_11.h:0
uint8_t u8
Definition: common.h:27
#define __must_check
Definition: common.h:101
int crypto_hash_finish(struct crypto_hash *ctx, u8 *hash, size_t *len)
crypto_hash_finish - Complete hash calculation @ctx: Context pointer from crypto_hash_init() @hash: B...
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: Base integer (big endian byte array)...
void crypto_global_deinit(void)
crypto_global_deinit - Deinitialize crypto wrapper
void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
crypto_hash_update - Add data to hash calculation @ctx: Context pointer from crypto_hash_init() @data...
int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
sha256_vector - SHA256 hash for data vector @num_elem: Number of elements in the data vector @addr: P...
Definition: sha256-internal.c:30
void crypto_cipher_deinit(struct crypto_cipher *ctx)
crypto_cipher_decrypt - Free cipher context @ctx: Context pointer from crypto_cipher_init()
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) @key: Private key @in: E...
int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
md4_vector - MD4 hash for data vector @num_elem: Number of elements in the data vector @addr: Pointer...
void aes_encrypt_deinit(void *ctx)
aes_encrypt_deinit - Deinitialize AES encryption @ctx: Context pointer from aes_encrypt_init()
Definition: aes-internal-enc.c:120
crypto_hash_alg
Definition: crypto.h:156
@ CRYPTO_HASH_ALG_HMAC_SHA256
Definition: crypto.h:159
@ CRYPTO_HASH_ALG_SHA1
Definition: crypto.h:157
@ CRYPTO_HASH_ALG_SHA256
Definition: crypto.h:159
@ CRYPTO_HASH_ALG_HMAC_MD5
Definition: crypto.h:158
@ CRYPTO_HASH_ALG_MD5
Definition: crypto.h:157
@ CRYPTO_HASH_ALG_HMAC_SHA1
Definition: crypto.h:158
int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
md5_vector - MD5 hash for data vector @num_elem: Number of elements in the data vector @addr: Pointer...
void * aes_encrypt_init(const u8 *key, size_t len)
aes_encrypt_init - Initialize AES for encryption @key: Encryption key @len: Key length in bytes (usua...
Definition: aes-internal-enc.c:101
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: Key buffer (DER encoded RSA private key) ...
int __must_check crypto_global_init(void)
crypto_global_init - Initialize crypto wrapper
crypto_cipher_alg
Definition: crypto.h:211
@ CRYPTO_CIPHER_ALG_RC2
Definition: crypto.h:213
@ CRYPTO_CIPHER_ALG_DES
Definition: crypto.h:213
@ CRYPTO_CIPHER_ALG_AES
Definition: crypto.h:212
@ CRYPTO_CIPHER_ALG_3DES
Definition: crypto.h:212
@ CRYPTO_CIPHER_ALG_RC4
Definition: crypto.h:213
@ CRYPTO_CIPHER_NULL
Definition: crypto.h:212
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 @key: RC4 key @keylen: RC4 key length ...
void * aes_decrypt_init(const u8 *key, size_t len)
aes_decrypt_init - Initialize AES for decryption @key: Decryption key @len: Key length in bytes (usua...
#define md5_vector_non_fips_allow
Definition: crypto.h:62
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 @alg: Hash algorithm @key: Key for keyed hash (e....
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 @alg: Cipher algorithm @iv: Initializati...
struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
crypto_public_key_import - Import an RSA public key @key: Key buffer (DER encoded RSA public key) @le...
void crypto_public_key_free(struct crypto_public_key *key)
crypto_public_key_free - Free public key @key: Public key
int __must_check crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt, u8 *plain, size_t len)
crypto_cipher_decrypt - Cipher decrypt @ctx: Context pointer from crypto_cipher_init() @crypt: Cipher...
void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
des_encrypt - Encrypt one block with DES @clear: 8 octets (in) @key: 7 octets (in) (no parity bits in...
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) @key: Private key from crypto_private...
void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
aes_encrypt - Encrypt one AES block @ctx: Context pointer from aes_encrypt_init() @plain: Plaintext d...
Definition: aes-internal-enc.c:114
void crypto_private_key_free(struct crypto_private_key *key)
crypto_private_key_free - Free private key @key: Private key from crypto_private_key_import()
int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
sha1_vector - SHA-1 hash for data vector @num_elem: Number of elements in the data vector @addr: Poin...
Definition: sha1-internal.c:35
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 @key: Public key @crypt: Encrypted signat...
void aes_decrypt_deinit(void *ctx)
aes_decrypt_deinit - Deinitialize AES decryption @ctx: Context pointer from aes_encrypt_init()
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 @buf: DER encoded X....
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) @key: Public key @in: Plai...
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: Seed/key for the PRF @seed_len...
void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
aes_decrypt - Decrypt one AES block @ctx: Context pointer from aes_encrypt_init() @crypt: Encrypted d...
int __must_check crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain, u8 *crypt, size_t len)
crypto_cipher_encrypt - Cipher encrypt @ctx: Context pointer from crypto_cipher_init() @plain: Plaint...