file_path
stringlengths
19
75
code
stringlengths
279
1.37M
./openssl/include/crypto/dsaerr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_DSAERR_H # define OSSL_CRYPTO_DSAERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif # ifndef OPENSSL_NO_DSA int ossl_err_load_DSA_strings(void); # endif # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/asn1_dsa.h
/* * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_ASN1_DSA_H # define OSSL_CRYPTO_ASN1_DSA_H # pragma once #include "internal/packet.h" int ossl_encode_der_length(WPACKET *pkt, size_t cont_len); int ossl_encode_der_integer(WPACKET *pkt, const BIGNUM *n); int ossl_encode_der_dsa_sig(WPACKET *pkt, const BIGNUM *r, const BIGNUM *s); int ossl_decode_der_length(PACKET *pkt, PACKET *subpkt); int ossl_decode_der_integer(PACKET *pkt, BIGNUM *n); size_t ossl_decode_der_dsa_sig(BIGNUM *r, BIGNUM *s, const unsigned char **ppin, size_t len); #endif
./openssl/include/crypto/uierr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_UIERR_H # define OSSL_CRYPTO_UIERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_UI_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/rand_pool.h
/* * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_PROVIDER_RAND_POOL_H # define OSSL_PROVIDER_RAND_POOL_H # pragma once # include <stdio.h> # include <openssl/rand.h> /* * Maximum allocation size for RANDOM_POOL buffers * * The max_len value for the buffer provided to the rand_drbg_get_entropy() * callback is currently 2^31 bytes (2 gigabytes), if a derivation function * is used. Since this is much too large to be allocated, the ossl_rand_pool_new() * function chooses more modest values as default pool length, bounded * by RAND_POOL_MIN_LENGTH and RAND_POOL_MAX_LENGTH * * The choice of the RAND_POOL_FACTOR is large enough such that the * RAND_POOL can store a random input which has a lousy entropy rate of * 8/256 (= 0.03125) bits per byte. This input will be sent through the * derivation function which 'compresses' the low quality input into a * high quality output. * * The factor 1.5 below is the pessimistic estimate for the extra amount * of entropy required when no get_nonce() callback is defined. */ # define RAND_POOL_FACTOR 256 # define RAND_POOL_MAX_LENGTH (RAND_POOL_FACTOR * \ 3 * (RAND_DRBG_STRENGTH / 16)) /* * = (RAND_POOL_FACTOR * \ * 1.5 * (RAND_DRBG_STRENGTH / 8)) */ /* * Initial allocation minimum. * * There is a distinction between the secure and normal allocation minimums. * Ideally, the secure allocation size should be a power of two. The normal * allocation size doesn't have any such restriction. * * The secure value is based on 128 bits of secure material, which is 16 bytes. * Typically, the DRBGs will set a minimum larger than this so optimal * allocation ought to take place (for full quality seed material). * * The normal value has been chosen by noticing that the rand_drbg_get_nonce * function is usually the largest of the built in allocation (twenty four * bytes and then appending another sixteen bytes). This means the buffer ends * with 40 bytes. The value of forty eight is comfortably above this which * allows some slack in the platform specific values used. */ # define RAND_POOL_MIN_ALLOCATION(secure) ((secure) ? 16 : 48) /* * The 'random pool' acts as a dumb container for collecting random * input from various entropy sources. It is the callers duty to 1) initialize * the random pool, 2) pass it to the polling callbacks, 3) seed the RNG, and * 4) cleanup the random pool again. * * The random pool contains no locking mechanism because its scope and * lifetime is intended to be restricted to a single stack frame. */ typedef struct rand_pool_st { unsigned char *buffer; /* points to the beginning of the random pool */ size_t len; /* current number of random bytes contained in the pool */ int attached; /* true pool was attached to existing buffer */ int secure; /* 1: allocated on the secure heap, 0: otherwise */ size_t min_len; /* minimum number of random bytes requested */ size_t max_len; /* maximum number of random bytes (allocated buffer size) */ size_t alloc_len; /* current number of bytes allocated */ size_t entropy; /* current entropy count in bits */ size_t entropy_requested; /* requested entropy count in bits */ } RAND_POOL; RAND_POOL *ossl_rand_pool_new(int entropy_requested, int secure, size_t min_len, size_t max_len); RAND_POOL *ossl_rand_pool_attach(const unsigned char *buffer, size_t len, size_t entropy); void ossl_rand_pool_free(RAND_POOL *pool); const unsigned char *ossl_rand_pool_buffer(RAND_POOL *pool); unsigned char *ossl_rand_pool_detach(RAND_POOL *pool); void ossl_rand_pool_reattach(RAND_POOL *pool, unsigned char *buffer); size_t ossl_rand_pool_entropy(RAND_POOL *pool); size_t ossl_rand_pool_length(RAND_POOL *pool); size_t ossl_rand_pool_entropy_available(RAND_POOL *pool); size_t ossl_rand_pool_entropy_needed(RAND_POOL *pool); /* |entropy_factor| expresses how many bits of data contain 1 bit of entropy */ size_t ossl_rand_pool_bytes_needed(RAND_POOL *pool, unsigned int entropy_factor); size_t ossl_rand_pool_bytes_remaining(RAND_POOL *pool); int ossl_rand_pool_add(RAND_POOL *pool, const unsigned char *buffer, size_t len, size_t entropy); unsigned char *ossl_rand_pool_add_begin(RAND_POOL *pool, size_t len); int ossl_rand_pool_add_end(RAND_POOL *pool, size_t len, size_t entropy); #endif /* OSSL_PROVIDER_RAND_POOL_H */
./openssl/include/crypto/err.h
/* * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_ERR_H # define OSSL_CRYPTO_ERR_H # pragma once int ossl_err_load_ERR_strings(void); int ossl_err_load_crypto_strings(void); void err_cleanup(void); int err_shelve_state(void **); void err_unshelve_state(void *); #endif
./openssl/include/crypto/pkcs12err.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_PKCS12ERR_H # define OSSL_CRYPTO_PKCS12ERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_PKCS12_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/bn_dh.h
/* * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #define declare_dh_bn(x) \ extern const BIGNUM ossl_bignum_dh##x##_p; \ extern const BIGNUM ossl_bignum_dh##x##_q; \ extern const BIGNUM ossl_bignum_dh##x##_g; \ declare_dh_bn(1024_160) declare_dh_bn(2048_224) declare_dh_bn(2048_256) extern const BIGNUM ossl_bignum_const_2; extern const BIGNUM ossl_bignum_ffdhe2048_p; extern const BIGNUM ossl_bignum_ffdhe3072_p; extern const BIGNUM ossl_bignum_ffdhe4096_p; extern const BIGNUM ossl_bignum_ffdhe6144_p; extern const BIGNUM ossl_bignum_ffdhe8192_p; extern const BIGNUM ossl_bignum_ffdhe2048_q; extern const BIGNUM ossl_bignum_ffdhe3072_q; extern const BIGNUM ossl_bignum_ffdhe4096_q; extern const BIGNUM ossl_bignum_ffdhe6144_q; extern const BIGNUM ossl_bignum_ffdhe8192_q; extern const BIGNUM ossl_bignum_modp_1536_p; extern const BIGNUM ossl_bignum_modp_2048_p; extern const BIGNUM ossl_bignum_modp_3072_p; extern const BIGNUM ossl_bignum_modp_4096_p; extern const BIGNUM ossl_bignum_modp_6144_p; extern const BIGNUM ossl_bignum_modp_8192_p; extern const BIGNUM ossl_bignum_modp_1536_q; extern const BIGNUM ossl_bignum_modp_2048_q; extern const BIGNUM ossl_bignum_modp_3072_q; extern const BIGNUM ossl_bignum_modp_4096_q; extern const BIGNUM ossl_bignum_modp_6144_q; extern const BIGNUM ossl_bignum_modp_8192_q;
./openssl/include/crypto/x509err.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_X509ERR_H # define OSSL_CRYPTO_X509ERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_X509_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/dherr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_DHERR_H # define OSSL_CRYPTO_DHERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif # ifndef OPENSSL_NO_DH int ossl_err_load_DH_strings(void); # endif # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/punycode.h
/* * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_PUNYCODE_H # define OSSL_CRYPTO_PUNYCODE_H # pragma once # include <stddef.h> /* for size_t */ int ossl_punycode_decode ( const char *pEncoded, const size_t enc_len, unsigned int *pDecoded, unsigned int *pout_length ); int ossl_a2ulabel(const char *in, char *out, size_t outlen); #endif
./openssl/include/crypto/aes_platform.h
/* * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_AES_PLATFORM_H # define OSSL_AES_PLATFORM_H # pragma once # include <openssl/aes.h> # ifdef VPAES_ASM int vpaes_set_encrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); int vpaes_set_decrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); void vpaes_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); void vpaes_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); void vpaes_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, int enc); # endif /* VPAES_ASM */ # ifdef BSAES_ASM void ossl_bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char ivec[16], int enc); void ossl_bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, size_t len, const AES_KEY *key, const unsigned char ivec[16]); void ossl_bsaes_xts_encrypt(const unsigned char *inp, unsigned char *out, size_t len, const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); void ossl_bsaes_xts_decrypt(const unsigned char *inp, unsigned char *out, size_t len, const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); # endif /* BSAES_ASM */ # ifdef AES_CTR_ASM void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out, size_t blocks, const AES_KEY *key, const unsigned char ivec[AES_BLOCK_SIZE]); # endif /* AES_CTR_ASM */ # ifdef AES_XTS_ASM void AES_xts_encrypt(const unsigned char *inp, unsigned char *out, size_t len, const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); void AES_xts_decrypt(const unsigned char *inp, unsigned char *out, size_t len, const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); # endif /* AES_XTS_ASM */ # if defined(OPENSSL_CPUID_OBJ) # if (defined(__powerpc__) || defined(__POWERPC__) || defined(_ARCH_PPC)) # include "crypto/ppc_arch.h" # ifdef VPAES_ASM # define VPAES_CAPABLE (OPENSSL_ppccap_P & PPC_ALTIVEC) # endif # if !defined(OPENSSL_SYS_AIX) && !defined(OPENSSL_SYS_MACOSX) # define HWAES_CAPABLE (OPENSSL_ppccap_P & PPC_CRYPTO207) # define HWAES_set_encrypt_key aes_p8_set_encrypt_key # define HWAES_set_decrypt_key aes_p8_set_decrypt_key # define HWAES_encrypt aes_p8_encrypt # define HWAES_decrypt aes_p8_decrypt # define HWAES_cbc_encrypt aes_p8_cbc_encrypt # define HWAES_ctr32_encrypt_blocks aes_p8_ctr32_encrypt_blocks # define HWAES_xts_encrypt aes_p8_xts_encrypt # define HWAES_xts_decrypt aes_p8_xts_decrypt # define PPC_AES_GCM_CAPABLE (OPENSSL_ppccap_P & PPC_MADD300) # define AES_GCM_ENC_BYTES 128 # define AES_GCM_DEC_BYTES 128 size_t ppc_aes_gcm_encrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], u64 *Xi); size_t ppc_aes_gcm_decrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], u64 *Xi); # define AES_GCM_ASM_PPC(gctx) ((gctx)->ctr==aes_p8_ctr32_encrypt_blocks && \ (gctx)->gcm.funcs.ghash==gcm_ghash_p8) void gcm_ghash_p8(u64 Xi[2],const u128 Htable[16],const u8 *inp, size_t len); # endif /* OPENSSL_SYS_AIX || OPENSSL_SYS_MACOSX */ # endif /* PPC */ # if (defined(__arm__) || defined(__arm) || defined(__aarch64__) || defined(_M_ARM64)) # include "arm_arch.h" # if __ARM_MAX_ARCH__>=7 # if defined(BSAES_ASM) # define BSAES_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON) # endif # if defined(VPAES_ASM) # define VPAES_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON) # endif # define HWAES_CAPABLE (OPENSSL_armcap_P & ARMV8_AES) # define HWAES_set_encrypt_key aes_v8_set_encrypt_key # define HWAES_set_decrypt_key aes_v8_set_decrypt_key # define HWAES_encrypt aes_v8_encrypt # define HWAES_decrypt aes_v8_decrypt # define HWAES_cbc_encrypt aes_v8_cbc_encrypt # define HWAES_ecb_encrypt aes_v8_ecb_encrypt # if __ARM_MAX_ARCH__>=8 && (defined(__aarch64__) || defined(_M_ARM64)) # define ARMv8_HWAES_CAPABLE (OPENSSL_armcap_P & ARMV8_AES) # define HWAES_xts_encrypt aes_v8_xts_encrypt # define HWAES_xts_decrypt aes_v8_xts_decrypt # endif # define HWAES_ctr32_encrypt_blocks aes_v8_ctr32_encrypt_blocks # define HWAES_ctr32_encrypt_blocks_unroll12_eor3 aes_v8_ctr32_encrypt_blocks_unroll12_eor3 # define AES_PMULL_CAPABLE ((OPENSSL_armcap_P & ARMV8_PMULL) && (OPENSSL_armcap_P & ARMV8_AES)) # define AES_UNROLL12_EOR3_CAPABLE (OPENSSL_armcap_P & ARMV8_UNROLL12_EOR3) # define AES_GCM_ENC_BYTES 512 # define AES_GCM_DEC_BYTES 512 # if __ARM_MAX_ARCH__>=8 && (defined(__aarch64__) || defined(_M_ARM64)) # define AES_gcm_encrypt armv8_aes_gcm_encrypt # define AES_gcm_decrypt armv8_aes_gcm_decrypt # define AES_GCM_ASM(gctx) (((gctx)->ctr==aes_v8_ctr32_encrypt_blocks_unroll12_eor3 || \ (gctx)->ctr==aes_v8_ctr32_encrypt_blocks) && \ (gctx)->gcm.funcs.ghash==gcm_ghash_v8) /* The [unroll8_eor3_]aes_gcm_(enc|dec)_(128|192|256)_kernel() functions * take input length in BITS and return number of BYTES processed */ size_t aes_gcm_enc_128_kernel(const uint8_t *plaintext, uint64_t plaintext_length, uint8_t *ciphertext, uint64_t *Xi, unsigned char ivec[16], const void *key); size_t aes_gcm_enc_192_kernel(const uint8_t *plaintext, uint64_t plaintext_length, uint8_t *ciphertext, uint64_t *Xi, unsigned char ivec[16], const void *key); size_t aes_gcm_enc_256_kernel(const uint8_t *plaintext, uint64_t plaintext_length, uint8_t *ciphertext, uint64_t *Xi, unsigned char ivec[16], const void *key); size_t aes_gcm_dec_128_kernel(const uint8_t *ciphertext, uint64_t plaintext_length, uint8_t *plaintext, uint64_t *Xi, unsigned char ivec[16], const void *key); size_t aes_gcm_dec_192_kernel(const uint8_t *ciphertext, uint64_t plaintext_length, uint8_t *plaintext, uint64_t *Xi, unsigned char ivec[16], const void *key); size_t aes_gcm_dec_256_kernel(const uint8_t *ciphertext, uint64_t plaintext_length, uint8_t *plaintext, uint64_t *Xi, unsigned char ivec[16], const void *key); size_t unroll8_eor3_aes_gcm_enc_128_kernel(const uint8_t *plaintext, uint64_t plaintext_length, uint8_t *ciphertext, uint64_t *Xi, unsigned char ivec[16], const void *key); size_t unroll8_eor3_aes_gcm_enc_192_kernel(const uint8_t *plaintext, uint64_t plaintext_length, uint8_t *ciphertext, uint64_t *Xi, unsigned char ivec[16], const void *key); size_t unroll8_eor3_aes_gcm_enc_256_kernel(const uint8_t *plaintext, uint64_t plaintext_length, uint8_t *ciphertext, uint64_t *Xi, unsigned char ivec[16], const void *key); size_t unroll8_eor3_aes_gcm_dec_128_kernel(const uint8_t *ciphertext, uint64_t plaintext_length, uint8_t *plaintext, uint64_t *Xi, unsigned char ivec[16], const void *key); size_t unroll8_eor3_aes_gcm_dec_192_kernel(const uint8_t *ciphertext, uint64_t plaintext_length, uint8_t *plaintext, uint64_t *Xi, unsigned char ivec[16], const void *key); size_t unroll8_eor3_aes_gcm_dec_256_kernel(const uint8_t *ciphertext, uint64_t plaintext_length, uint8_t *plaintext, uint64_t *Xi, unsigned char ivec[16], const void *key); size_t armv8_aes_gcm_encrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], u64 *Xi); size_t armv8_aes_gcm_decrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], u64 *Xi); void gcm_ghash_v8(u64 Xi[2],const u128 Htable[16],const u8 *inp, size_t len); # endif # endif # endif # endif /* OPENSSL_CPUID_OBJ */ # if defined(AES_ASM) && ( \ defined(__x86_64) || defined(__x86_64__) || \ defined(_M_AMD64) || defined(_M_X64) ) # define AES_CBC_HMAC_SHA_CAPABLE 1 # define AESNI_CBC_HMAC_SHA_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32))) # endif # if defined(__loongarch__) || defined(__loongarch64) # include "loongarch_arch.h" # if defined(VPAES_ASM) # define VPAES_CAPABLE (OPENSSL_loongarch_hwcap_P & LOONGARCH_HWCAP_LSX) # endif # endif # if defined(AES_ASM) && !defined(I386_ONLY) && ( \ ((defined(__i386) || defined(__i386__) || \ defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \ defined(__x86_64) || defined(__x86_64__) || \ defined(_M_AMD64) || defined(_M_X64) ) /* AES-NI section */ # define AESNI_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32))) # ifdef VPAES_ASM # define VPAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32))) # endif # ifdef BSAES_ASM # define BSAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32))) # endif # define AES_GCM_ENC_BYTES 32 # define AES_GCM_DEC_BYTES 16 int aesni_set_encrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); int aesni_set_decrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); void aesni_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); void aesni_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); void aesni_ecb_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, int enc); void aesni_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, int enc); # ifndef OPENSSL_NO_OCB void aesni_ocb_encrypt(const unsigned char *in, unsigned char *out, size_t blocks, const void *key, size_t start_block_num, unsigned char offset_i[16], const unsigned char L_[][16], unsigned char checksum[16]); void aesni_ocb_decrypt(const unsigned char *in, unsigned char *out, size_t blocks, const void *key, size_t start_block_num, unsigned char offset_i[16], const unsigned char L_[][16], unsigned char checksum[16]); # endif /* OPENSSL_NO_OCB */ void aesni_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, size_t blocks, const void *key, const unsigned char *ivec); void aesni_xts_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); void aesni_xts_decrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); void aesni_ccm64_encrypt_blocks(const unsigned char *in, unsigned char *out, size_t blocks, const void *key, const unsigned char ivec[16], unsigned char cmac[16]); void aesni_ccm64_decrypt_blocks(const unsigned char *in, unsigned char *out, size_t blocks, const void *key, const unsigned char ivec[16], unsigned char cmac[16]); # if defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64) size_t aesni_gcm_encrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], u64 *Xi); size_t aesni_gcm_decrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], u64 *Xi); void gcm_ghash_avx(u64 Xi[2], const u128 Htable[16], const u8 *in, size_t len); # define AES_gcm_encrypt aesni_gcm_encrypt # define AES_gcm_decrypt aesni_gcm_decrypt # define AES_GCM_ASM(ctx) (ctx->ctr == aesni_ctr32_encrypt_blocks && \ ctx->gcm.funcs.ghash == gcm_ghash_avx) # endif # elif defined(AES_ASM) && (defined(__sparc) || defined(__sparc__)) /* Fujitsu SPARC64 X support */ # include "crypto/sparc_arch.h" # define SPARC_AES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_AES) # define HWAES_CAPABLE (OPENSSL_sparcv9cap_P[0] & SPARCV9_FJAESX) # define HWAES_set_encrypt_key aes_fx_set_encrypt_key # define HWAES_set_decrypt_key aes_fx_set_decrypt_key # define HWAES_encrypt aes_fx_encrypt # define HWAES_decrypt aes_fx_decrypt # define HWAES_cbc_encrypt aes_fx_cbc_encrypt # define HWAES_ctr32_encrypt_blocks aes_fx_ctr32_encrypt_blocks void aes_t4_set_encrypt_key(const unsigned char *key, int bits, AES_KEY *ks); void aes_t4_set_decrypt_key(const unsigned char *key, int bits, AES_KEY *ks); void aes_t4_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); void aes_t4_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); /* * Key-length specific subroutines were chosen for following reason. * Each SPARC T4 core can execute up to 8 threads which share core's * resources. Loading as much key material to registers allows to * minimize references to shared memory interface, as well as amount * of instructions in inner loops [much needed on T4]. But then having * non-key-length specific routines would require conditional branches * either in inner loops or on subroutines' entries. Former is hardly * acceptable, while latter means code size increase to size occupied * by multiple key-length specific subroutines, so why fight? */ void aes128_t4_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t len, const AES_KEY *key, unsigned char *ivec, int /*unused*/); void aes128_t4_cbc_decrypt(const unsigned char *in, unsigned char *out, size_t len, const AES_KEY *key, unsigned char *ivec, int /*unused*/); void aes192_t4_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t len, const AES_KEY *key, unsigned char *ivec, int /*unused*/); void aes192_t4_cbc_decrypt(const unsigned char *in, unsigned char *out, size_t len, const AES_KEY *key, unsigned char *ivec, int /*unused*/); void aes256_t4_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t len, const AES_KEY *key, unsigned char *ivec, int /*unused*/); void aes256_t4_cbc_decrypt(const unsigned char *in, unsigned char *out, size_t len, const AES_KEY *key, unsigned char *ivec, int /*unused*/); void aes128_t4_ctr32_encrypt(const unsigned char *in, unsigned char *out, size_t blocks, const AES_KEY *key, unsigned char *ivec); void aes192_t4_ctr32_encrypt(const unsigned char *in, unsigned char *out, size_t blocks, const AES_KEY *key, unsigned char *ivec); void aes256_t4_ctr32_encrypt(const unsigned char *in, unsigned char *out, size_t blocks, const AES_KEY *key, unsigned char *ivec); void aes128_t4_xts_encrypt(const unsigned char *in, unsigned char *out, size_t blocks, const AES_KEY *key1, const AES_KEY *key2, const unsigned char *ivec); void aes128_t4_xts_decrypt(const unsigned char *in, unsigned char *out, size_t blocks, const AES_KEY *key1, const AES_KEY *key2, const unsigned char *ivec); void aes256_t4_xts_encrypt(const unsigned char *in, unsigned char *out, size_t blocks, const AES_KEY *key1, const AES_KEY *key2, const unsigned char *ivec); void aes256_t4_xts_decrypt(const unsigned char *in, unsigned char *out, size_t blocks, const AES_KEY *key1, const AES_KEY *key2, const unsigned char *ivec); # elif defined(OPENSSL_CPUID_OBJ) && defined(__s390__) /* IBM S390X support */ # include "s390x_arch.h" /* Convert key size to function code: [16,24,32] -> [18,19,20]. */ # define S390X_AES_FC(keylen) (S390X_AES_128 + ((((keylen) << 3) - 128) >> 6)) /* Most modes of operation need km for partial block processing. */ # define S390X_aes_128_CAPABLE (OPENSSL_s390xcap_P.km[0] & \ S390X_CAPBIT(S390X_AES_128)) # define S390X_aes_192_CAPABLE (OPENSSL_s390xcap_P.km[0] & \ S390X_CAPBIT(S390X_AES_192)) # define S390X_aes_256_CAPABLE (OPENSSL_s390xcap_P.km[0] & \ S390X_CAPBIT(S390X_AES_256)) # define S390X_aes_128_cbc_CAPABLE 1 /* checked by callee */ # define S390X_aes_192_cbc_CAPABLE 1 # define S390X_aes_256_cbc_CAPABLE 1 # define S390X_aes_128_ecb_CAPABLE S390X_aes_128_CAPABLE # define S390X_aes_192_ecb_CAPABLE S390X_aes_192_CAPABLE # define S390X_aes_256_ecb_CAPABLE S390X_aes_256_CAPABLE # define S390X_aes_128_ofb_CAPABLE (S390X_aes_128_CAPABLE && \ (OPENSSL_s390xcap_P.kmo[0] & \ S390X_CAPBIT(S390X_AES_128))) # define S390X_aes_192_ofb_CAPABLE (S390X_aes_192_CAPABLE && \ (OPENSSL_s390xcap_P.kmo[0] & \ S390X_CAPBIT(S390X_AES_192))) # define S390X_aes_256_ofb_CAPABLE (S390X_aes_256_CAPABLE && \ (OPENSSL_s390xcap_P.kmo[0] & \ S390X_CAPBIT(S390X_AES_256))) # define S390X_aes_128_cfb_CAPABLE (S390X_aes_128_CAPABLE && \ (OPENSSL_s390xcap_P.kmf[0] & \ S390X_CAPBIT(S390X_AES_128))) # define S390X_aes_192_cfb_CAPABLE (S390X_aes_192_CAPABLE && \ (OPENSSL_s390xcap_P.kmf[0] & \ S390X_CAPBIT(S390X_AES_192))) # define S390X_aes_256_cfb_CAPABLE (S390X_aes_256_CAPABLE && \ (OPENSSL_s390xcap_P.kmf[0] & \ S390X_CAPBIT(S390X_AES_256))) # define S390X_aes_128_cfb8_CAPABLE (OPENSSL_s390xcap_P.kmf[0] & \ S390X_CAPBIT(S390X_AES_128)) # define S390X_aes_192_cfb8_CAPABLE (OPENSSL_s390xcap_P.kmf[0] & \ S390X_CAPBIT(S390X_AES_192)) # define S390X_aes_256_cfb8_CAPABLE (OPENSSL_s390xcap_P.kmf[0] & \ S390X_CAPBIT(S390X_AES_256)) # define S390X_aes_128_cfb1_CAPABLE 0 # define S390X_aes_192_cfb1_CAPABLE 0 # define S390X_aes_256_cfb1_CAPABLE 0 # define S390X_aes_128_ctr_CAPABLE 1 /* checked by callee */ # define S390X_aes_192_ctr_CAPABLE 1 # define S390X_aes_256_ctr_CAPABLE 1 # define S390X_aes_128_xts_CAPABLE 1 /* checked by callee */ # define S390X_aes_256_xts_CAPABLE 1 # define S390X_aes_128_gcm_CAPABLE (S390X_aes_128_CAPABLE && \ (OPENSSL_s390xcap_P.kma[0] & \ S390X_CAPBIT(S390X_AES_128))) # define S390X_aes_192_gcm_CAPABLE (S390X_aes_192_CAPABLE && \ (OPENSSL_s390xcap_P.kma[0] & \ S390X_CAPBIT(S390X_AES_192))) # define S390X_aes_256_gcm_CAPABLE (S390X_aes_256_CAPABLE && \ (OPENSSL_s390xcap_P.kma[0] & \ S390X_CAPBIT(S390X_AES_256))) # define S390X_aes_128_ccm_CAPABLE (S390X_aes_128_CAPABLE && \ (OPENSSL_s390xcap_P.kmac[0] & \ S390X_CAPBIT(S390X_AES_128))) # define S390X_aes_192_ccm_CAPABLE (S390X_aes_192_CAPABLE && \ (OPENSSL_s390xcap_P.kmac[0] & \ S390X_CAPBIT(S390X_AES_192))) # define S390X_aes_256_ccm_CAPABLE (S390X_aes_256_CAPABLE && \ (OPENSSL_s390xcap_P.kmac[0] & \ S390X_CAPBIT(S390X_AES_256))) # define S390X_CCM_AAD_FLAG 0x40 # ifndef OPENSSL_NO_OCB # define S390X_aes_128_ocb_CAPABLE 0 # define S390X_aes_192_ocb_CAPABLE 0 # define S390X_aes_256_ocb_CAPABLE 0 # endif /* OPENSSL_NO_OCB */ # ifndef OPENSSL_NO_SIV # define S390X_aes_128_siv_CAPABLE 0 # define S390X_aes_192_siv_CAPABLE 0 # define S390X_aes_256_siv_CAPABLE 0 # endif /* OPENSSL_NO_SIV */ /* Convert key size to function code: [16,24,32] -> [18,19,20]. */ # define S390X_AES_FC(keylen) (S390X_AES_128 + ((((keylen) << 3) - 128) >> 6)) # elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64 /* RISC-V 64 support */ # include "riscv_arch.h" /* Zkne and Zknd extensions (scalar crypto AES). */ int rv64i_zkne_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); int rv64i_zknd_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); void rv64i_zkne_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); void rv64i_zknd_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); /* Zvkned extension (vector crypto AES). */ int rv64i_zvkned_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); int rv64i_zvkned_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); void rv64i_zvkned_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); void rv64i_zvkned_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); void rv64i_zvkned_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, const int enc); void rv64i_zvkned_cbc_decrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, const int enc); void rv64i_zvkned_ecb_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, const int enc); void rv64i_zvkned_ecb_decrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, const int enc); void rv64i_zvkb_zvkned_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, size_t blocks, const void *key, const unsigned char ivec[16]); size_t rv64i_zvkb_zvkg_zvkned_aes_gcm_encrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], u64 *Xi); size_t rv64i_zvkb_zvkg_zvkned_aes_gcm_decrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], u64 *Xi); void rv64i_zvbb_zvkg_zvkned_aes_xts_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); void rv64i_zvbb_zvkg_zvkned_aes_xts_decrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); void gcm_ghash_rv64i_zvkg(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); #define AES_GCM_ENC_BYTES 64 #define AES_GCM_DEC_BYTES 64 #define AES_gcm_encrypt rv64i_zvkb_zvkg_zvkned_aes_gcm_encrypt #define AES_gcm_decrypt rv64i_zvkb_zvkg_zvkned_aes_gcm_decrypt #define AES_GCM_ASM(ctx) \ (ctx->ctr == rv64i_zvkb_zvkned_ctr32_encrypt_blocks && \ ctx->gcm.funcs.ghash == gcm_ghash_rv64i_zvkg) # elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 32 /* RISC-V 32 support */ # include "riscv_arch.h" int rv32i_zkne_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); /* set_decrypt_key needs both zknd and zkne */ int rv32i_zknd_zkne_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); int rv32i_zbkb_zkne_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); int rv32i_zbkb_zknd_zkne_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); void rv32i_zkne_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); void rv32i_zknd_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); # endif # if defined(HWAES_CAPABLE) int HWAES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); int HWAES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); void HWAES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); void HWAES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); void HWAES_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, const int enc); void HWAES_ecb_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, const int enc); void HWAES_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, size_t len, const void *key, const unsigned char ivec[16]); # if defined(AES_UNROLL12_EOR3_CAPABLE) void HWAES_ctr32_encrypt_blocks_unroll12_eor3(const unsigned char *in, unsigned char *out, size_t len, const void *key, const unsigned char ivec[16]); # endif void HWAES_xts_encrypt(const unsigned char *inp, unsigned char *out, size_t len, const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); void HWAES_xts_decrypt(const unsigned char *inp, unsigned char *out, size_t len, const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); # ifndef OPENSSL_NO_OCB # ifdef HWAES_ocb_encrypt void HWAES_ocb_encrypt(const unsigned char *in, unsigned char *out, size_t blocks, const void *key, size_t start_block_num, unsigned char offset_i[16], const unsigned char L_[][16], unsigned char checksum[16]); # else # define HWAES_ocb_encrypt ((ocb128_f)NULL) # endif # ifdef HWAES_ocb_decrypt void HWAES_ocb_decrypt(const unsigned char *in, unsigned char *out, size_t blocks, const void *key, size_t start_block_num, unsigned char offset_i[16], const unsigned char L_[][16], unsigned char checksum[16]); # else # define HWAES_ocb_decrypt ((ocb128_f)NULL) # endif # endif /* OPENSSL_NO_OCB */ # endif /* HWAES_CAPABLE */ #endif /* OSSL_AES_PLATFORM_H */
./openssl/include/crypto/encodererr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_ENCODERERR_H # define OSSL_CRYPTO_ENCODERERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_OSSL_ENCODER_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/rand.h
/* * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * Licensed under the Apache License 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * https://www.openssl.org/source/license.html * or in the file LICENSE in the source distribution. */ #ifndef OSSL_CRYPTO_RAND_H # define OSSL_CRYPTO_RAND_H # pragma once # include <openssl/rand.h> # include "crypto/rand_pool.h" # if defined(__APPLE__) && !defined(OPENSSL_NO_APPLE_CRYPTO_RANDOM) # include <Availability.h> # if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200) || \ (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) # define OPENSSL_APPLE_CRYPTO_RANDOM 1 # include <CommonCrypto/CommonCryptoError.h> # include <CommonCrypto/CommonRandom.h> # endif # endif /* * Defines related to seed sources */ #ifndef DEVRANDOM /* * set this to a comma-separated list of 'random' device files to try out. By * default, we will try to read at least one of these files */ # define DEVRANDOM "/dev/urandom", "/dev/random", "/dev/hwrng", "/dev/srandom" # if defined(__linux) && !defined(__ANDROID__) # ifndef DEVRANDOM_WAIT # define DEVRANDOM_WAIT "/dev/random" # endif /* * Linux kernels 4.8 and later changes how their random device works and there * is no reliable way to tell that /dev/urandom has been seeded -- getentropy(2) * should be used instead. */ # ifndef DEVRANDOM_SAFE_KERNEL # define DEVRANDOM_SAFE_KERNEL 4, 8 # endif /* * Some operating systems do not permit select(2) on their random devices, * defining this to zero will force the use of read(2) to extract one byte * from /dev/random. */ # ifndef DEVRANDM_WAIT_USE_SELECT # define DEVRANDM_WAIT_USE_SELECT 1 # endif /* * Define the shared memory identifier used to indicate if the operating * system has properly seeded the DEVRANDOM source. */ # ifndef OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID # define OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID 114 # endif # endif #endif #if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD) /* * set this to a comma-separated list of 'egd' sockets to try out. These * sockets will be tried in the order listed in case accessing the device * files listed in DEVRANDOM did not return enough randomness. */ # define DEVRANDOM_EGD "/var/run/egd-pool", "/dev/egd-pool", "/etc/egd-pool", "/etc/entropy" #endif void ossl_rand_cleanup_int(void); /* * Initialise the random pool reseeding sources. * * Returns 1 on success and 0 on failure. */ int ossl_rand_pool_init(void); /* * Finalise the random pool reseeding sources. */ void ossl_rand_pool_cleanup(void); /* * Control the random pool use of open file descriptors. */ void ossl_rand_pool_keep_random_devices_open(int keep); /* * Configuration */ void ossl_random_add_conf_module(void); /* * Get and cleanup random seed material. */ size_t ossl_rand_get_entropy(OSSL_LIB_CTX *ctx, unsigned char **pout, int entropy, size_t min_len, size_t max_len); size_t ossl_rand_get_user_entropy(OSSL_LIB_CTX *ctx, unsigned char **pout, int entropy, size_t min_len, size_t max_len); void ossl_rand_cleanup_entropy(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t len); void ossl_rand_cleanup_user_entropy(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t len); size_t ossl_rand_get_nonce(OSSL_LIB_CTX *ctx, unsigned char **pout, size_t min_len, size_t max_len, const void *salt, size_t salt_len); size_t ossl_rand_get_user_nonce(OSSL_LIB_CTX *ctx, unsigned char **pout, size_t min_len, size_t max_len, const void *salt, size_t salt_len); void ossl_rand_cleanup_nonce(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t len); void ossl_rand_cleanup_user_nonce(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t len); /* * Get seeding material from the operating system sources. */ size_t ossl_pool_acquire_entropy(RAND_POOL *pool); int ossl_pool_add_nonce_data(RAND_POOL *pool); # ifdef FIPS_MODULE EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx); # else EVP_RAND_CTX *ossl_rand_get0_seed_noncreating(OSSL_LIB_CTX *ctx); # endif /* Generate a uniformly distributed random integer in the interval [0, upper) */ uint32_t ossl_rand_uniform_uint32(OSSL_LIB_CTX *ctx, uint32_t upper, int *err); /* * Generate a uniformly distributed random integer in the interval * [lower, upper). */ uint32_t ossl_rand_range_uint32(OSSL_LIB_CTX *ctx, uint32_t lower, uint32_t upper, int *err); #endif
./openssl/include/crypto/x509.h
/* * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_X509_H # define OSSL_CRYPTO_X509_H # pragma once # include "internal/refcount.h" # include <openssl/asn1.h> # include <openssl/x509.h> # include <openssl/conf.h> # include "crypto/types.h" /* Internal X509 structures and functions: not for application use */ /* Note: unless otherwise stated a field pointer is mandatory and should * never be set to NULL: the ASN.1 code and accessors rely on mandatory * fields never being NULL. */ /* * name entry structure, equivalent to AttributeTypeAndValue defined * in RFC5280 et al. */ struct X509_name_entry_st { ASN1_OBJECT *object; /* AttributeType */ ASN1_STRING *value; /* AttributeValue */ int set; /* index of RDNSequence for this entry */ int size; /* temp variable */ }; /* Name from RFC 5280. */ struct X509_name_st { STACK_OF(X509_NAME_ENTRY) *entries; /* DN components */ int modified; /* true if 'bytes' needs to be built */ BUF_MEM *bytes; /* cached encoding: cannot be NULL */ /* canonical encoding used for rapid Name comparison */ unsigned char *canon_enc; int canon_enclen; } /* X509_NAME */ ; /* Signature info structure */ struct x509_sig_info_st { /* NID of message digest */ int mdnid; /* NID of public key algorithm */ int pknid; /* Security bits */ int secbits; /* Various flags */ uint32_t flags; }; /* PKCS#10 certificate request */ struct X509_req_info_st { ASN1_ENCODING enc; /* cached encoding of signed part */ ASN1_INTEGER *version; /* version, defaults to v1(0) so can be NULL */ X509_NAME *subject; /* certificate request DN */ X509_PUBKEY *pubkey; /* public key of request */ /* * Zero or more attributes. * NB: although attributes is a mandatory field some broken * encodings omit it so this may be NULL in that case. */ STACK_OF(X509_ATTRIBUTE) *attributes; }; struct X509_req_st { X509_REQ_INFO req_info; /* signed certificate request data */ X509_ALGOR sig_alg; /* signature algorithm */ ASN1_BIT_STRING *signature; /* signature */ CRYPTO_REF_COUNT references; CRYPTO_RWLOCK *lock; /* Set on live certificates for authentication purposes */ ASN1_OCTET_STRING *distinguishing_id; OSSL_LIB_CTX *libctx; char *propq; }; struct X509_crl_info_st { ASN1_INTEGER *version; /* version: defaults to v1(0) so may be NULL */ X509_ALGOR sig_alg; /* signature algorithm */ X509_NAME *issuer; /* CRL issuer name */ ASN1_TIME *lastUpdate; /* lastUpdate field */ ASN1_TIME *nextUpdate; /* nextUpdate field: optional */ STACK_OF(X509_REVOKED) *revoked; /* revoked entries: optional */ STACK_OF(X509_EXTENSION) *extensions; /* extensions: optional */ ASN1_ENCODING enc; /* encoding of signed portion of CRL */ }; struct X509_crl_st { X509_CRL_INFO crl; /* signed CRL data */ X509_ALGOR sig_alg; /* CRL signature algorithm */ ASN1_BIT_STRING signature; /* CRL signature */ CRYPTO_REF_COUNT references; int flags; /* * Cached copies of decoded extension values, since extensions * are optional any of these can be NULL. */ AUTHORITY_KEYID *akid; ISSUING_DIST_POINT *idp; /* Convenient breakdown of IDP */ int idp_flags; int idp_reasons; /* CRL and base CRL numbers for delta processing */ ASN1_INTEGER *crl_number; ASN1_INTEGER *base_crl_number; STACK_OF(GENERAL_NAMES) *issuers; /* hash of CRL */ unsigned char sha1_hash[SHA_DIGEST_LENGTH]; /* alternative method to handle this CRL */ const X509_CRL_METHOD *meth; void *meth_data; CRYPTO_RWLOCK *lock; OSSL_LIB_CTX *libctx; char *propq; }; struct x509_revoked_st { ASN1_INTEGER serialNumber; /* revoked entry serial number */ ASN1_TIME *revocationDate; /* revocation date */ STACK_OF(X509_EXTENSION) *extensions; /* CRL entry extensions: optional */ /* decoded value of CRLissuer extension: set if indirect CRL */ STACK_OF(GENERAL_NAME) *issuer; /* revocation reason: set to CRL_REASON_NONE if reason extension absent */ int reason; /* * CRL entries are reordered for faster lookup of serial numbers. This * field contains the original load sequence for this entry. */ int sequence; }; /* * This stuff is certificate "auxiliary info": it contains details which are * useful in certificate stores and databases. When used this is tagged onto * the end of the certificate itself. OpenSSL specific structure not defined * in any RFC. */ struct x509_cert_aux_st { STACK_OF(ASN1_OBJECT) *trust; /* trusted uses */ STACK_OF(ASN1_OBJECT) *reject; /* rejected uses */ ASN1_UTF8STRING *alias; /* "friendly name" */ ASN1_OCTET_STRING *keyid; /* key id of private key */ STACK_OF(X509_ALGOR) *other; /* other unspecified info */ }; struct x509_cinf_st { ASN1_INTEGER *version; /* [ 0 ] default of v1 */ ASN1_INTEGER serialNumber; X509_ALGOR signature; X509_NAME *issuer; X509_VAL validity; X509_NAME *subject; X509_PUBKEY *key; ASN1_BIT_STRING *issuerUID; /* [ 1 ] optional in v2 */ ASN1_BIT_STRING *subjectUID; /* [ 2 ] optional in v2 */ STACK_OF(X509_EXTENSION) *extensions; /* [ 3 ] optional in v3 */ ASN1_ENCODING enc; }; struct x509_st { X509_CINF cert_info; X509_ALGOR sig_alg; ASN1_BIT_STRING signature; X509_SIG_INFO siginf; CRYPTO_REF_COUNT references; CRYPTO_EX_DATA ex_data; /* These contain copies of various extension values */ long ex_pathlen; long ex_pcpathlen; uint32_t ex_flags; uint32_t ex_kusage; uint32_t ex_xkusage; uint32_t ex_nscert; ASN1_OCTET_STRING *skid; AUTHORITY_KEYID *akid; X509_POLICY_CACHE *policy_cache; STACK_OF(DIST_POINT) *crldp; STACK_OF(GENERAL_NAME) *altname; NAME_CONSTRAINTS *nc; # ifndef OPENSSL_NO_RFC3779 STACK_OF(IPAddressFamily) *rfc3779_addr; struct ASIdentifiers_st *rfc3779_asid; # endif unsigned char sha1_hash[SHA_DIGEST_LENGTH]; X509_CERT_AUX *aux; CRYPTO_RWLOCK *lock; volatile int ex_cached; /* Set on live certificates for authentication purposes */ ASN1_OCTET_STRING *distinguishing_id; OSSL_LIB_CTX *libctx; char *propq; } /* X509 */ ; /* * This is a used when verifying cert chains. Since the gathering of the * cert chain can take some time (and have to be 'retried', this needs to be * kept and passed around. */ struct x509_store_ctx_st { /* X509_STORE_CTX */ X509_STORE *store; /* The following are set by the caller */ /* The cert to check */ X509 *cert; /* chain of X509s - untrusted - passed in */ STACK_OF(X509) *untrusted; /* set of CRLs passed in */ STACK_OF(X509_CRL) *crls; X509_VERIFY_PARAM *param; /* Other info for use with get_issuer() */ void *other_ctx; /* Callbacks for various operations */ /* called to verify a certificate */ int (*verify) (X509_STORE_CTX *ctx); /* error callback */ int (*verify_cb) (int ok, X509_STORE_CTX *ctx); /* get issuers cert from ctx */ int (*get_issuer) (X509 **issuer, X509_STORE_CTX *ctx, X509 *x); /* check issued */ int (*check_issued) (X509_STORE_CTX *ctx, X509 *x, X509 *issuer); /* Check revocation status of chain */ int (*check_revocation) (X509_STORE_CTX *ctx); /* retrieve CRL */ int (*get_crl) (X509_STORE_CTX *ctx, X509_CRL **crl, X509 *x); /* Check CRL validity */ int (*check_crl) (X509_STORE_CTX *ctx, X509_CRL *crl); /* Check certificate against CRL */ int (*cert_crl) (X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x); /* Check policy status of the chain */ int (*check_policy) (X509_STORE_CTX *ctx); STACK_OF(X509) *(*lookup_certs) (X509_STORE_CTX *ctx, const X509_NAME *nm); /* cannot constify 'ctx' param due to lookup_certs_sk() in x509_vfy.c */ STACK_OF(X509_CRL) *(*lookup_crls) (const X509_STORE_CTX *ctx, const X509_NAME *nm); int (*cleanup) (X509_STORE_CTX *ctx); /* The following is built up */ /* if 0, rebuild chain */ int valid; /* number of untrusted certs */ int num_untrusted; /* chain of X509s - built up and trusted */ STACK_OF(X509) *chain; /* Valid policy tree */ X509_POLICY_TREE *tree; /* Require explicit policy value */ int explicit_policy; /* When something goes wrong, this is why */ int error_depth; int error; X509 *current_cert; /* cert currently being tested as valid issuer */ X509 *current_issuer; /* current CRL */ X509_CRL *current_crl; /* score of current CRL */ int current_crl_score; /* Reason mask */ unsigned int current_reasons; /* For CRL path validation: parent context */ X509_STORE_CTX *parent; CRYPTO_EX_DATA ex_data; SSL_DANE *dane; /* signed via bare TA public key, rather than CA certificate */ int bare_ta_signed; /* Raw Public Key */ EVP_PKEY *rpk; OSSL_LIB_CTX *libctx; char *propq; }; /* PKCS#8 private key info structure */ struct pkcs8_priv_key_info_st { ASN1_INTEGER *version; X509_ALGOR *pkeyalg; ASN1_OCTET_STRING *pkey; STACK_OF(X509_ATTRIBUTE) *attributes; }; struct X509_sig_st { X509_ALGOR *algor; ASN1_OCTET_STRING *digest; }; struct x509_object_st { /* one of the above types */ X509_LOOKUP_TYPE type; union { char *ptr; X509 *x509; X509_CRL *crl; EVP_PKEY *pkey; } data; }; int ossl_a2i_ipadd(unsigned char *ipout, const char *ipasc); int ossl_x509_set1_time(int *modified, ASN1_TIME **ptm, const ASN1_TIME *tm); int ossl_x509_print_ex_brief(BIO *bio, X509 *cert, unsigned long neg_cflags); int ossl_x509v3_cache_extensions(X509 *x); int ossl_x509_init_sig_info(X509 *x); int ossl_x509_set0_libctx(X509 *x, OSSL_LIB_CTX *libctx, const char *propq); int ossl_x509_crl_set0_libctx(X509_CRL *x, OSSL_LIB_CTX *libctx, const char *propq); int ossl_x509_req_set0_libctx(X509_REQ *x, OSSL_LIB_CTX *libctx, const char *propq); int ossl_asn1_item_digest_ex(const ASN1_ITEM *it, const EVP_MD *type, void *data, unsigned char *md, unsigned int *len, OSSL_LIB_CTX *libctx, const char *propq); int ossl_x509_add_cert_new(STACK_OF(X509) **sk, X509 *cert, int flags); int ossl_x509_add_certs_new(STACK_OF(X509) **p_sk, STACK_OF(X509) *certs, int flags); STACK_OF(X509_ATTRIBUTE) *ossl_x509at_dup(const STACK_OF(X509_ATTRIBUTE) *x); int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq, const X509_PUBKEY *key); /* Calculate default key identifier according to RFC 5280 section 4.2.1.2 (1) */ ASN1_OCTET_STRING *ossl_x509_pubkey_hash(X509_PUBKEY *pubkey); X509_PUBKEY *ossl_d2i_X509_PUBKEY_INTERNAL(const unsigned char **pp, long len, OSSL_LIB_CTX *libctx, const char *propq); void ossl_X509_PUBKEY_INTERNAL_free(X509_PUBKEY *xpub); RSA *ossl_d2i_RSA_PSS_PUBKEY(RSA **a, const unsigned char **pp, long length); int ossl_i2d_RSA_PSS_PUBKEY(const RSA *a, unsigned char **pp); # ifndef OPENSSL_NO_DSA DSA *ossl_d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length); # endif /* OPENSSL_NO_DSA */ # ifndef OPENSSL_NO_DH DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length); int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp); DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length); int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp); # endif /* OPENSSL_NO_DH */ # ifndef OPENSSL_NO_EC ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a, const unsigned char **pp, long length); int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp); ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a, const unsigned char **pp, long length); int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp); ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a, const unsigned char **pp, long length); int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp); ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a, const unsigned char **pp, long length); int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp); # endif /* OPENSSL_NO_EC */ EVP_PKEY *ossl_d2i_PUBKEY_legacy(EVP_PKEY **a, const unsigned char **pp, long length); int ossl_x509_check_private_key(const EVP_PKEY *k, const EVP_PKEY *pkey); int x509v3_add_len_value_uchar(const char *name, const unsigned char *value, size_t vallen, STACK_OF(CONF_VALUE) **extlist); /* Attribute addition functions not checking for duplicate attributes */ STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x, X509_ATTRIBUTE *attr); STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE) **x, const ASN1_OBJECT *obj, int type, const unsigned char *bytes, int len); STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE) **x, int nid, int type, const unsigned char *bytes, int len); STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE) **x, const char *attrname, int type, const unsigned char *bytes, int len); #endif /* OSSL_CRYPTO_X509_H */
./openssl/include/crypto/tserr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_TSERR_H # define OSSL_CRYPTO_TSERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif # ifndef OPENSSL_NO_TS int ossl_err_load_TS_strings(void); # endif # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/lhash.h
/* * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_LHASH_H # define OSSL_CRYPTO_LHASH_H # pragma once unsigned long ossl_lh_strcasehash(const char *); #endif /* OSSL_CRYPTO_LHASH_H */
./openssl/include/crypto/evperr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_EVPERR_H # define OSSL_CRYPTO_EVPERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_EVP_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/pem.h
/* * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_INTERNAL_PEM_H # define OSSL_INTERNAL_PEM_H # pragma once # include <openssl/pem.h> # include "crypto/types.h" /* Found in crypto/pem/pvkfmt.c */ /* Maximum length of a blob after header */ # define BLOB_MAX_LENGTH 102400 int ossl_do_blob_header(const unsigned char **in, unsigned int length, unsigned int *pmagic, unsigned int *pbitlen, int *pisdss, int *pispub); unsigned int ossl_blob_length(unsigned bitlen, int isdss, int ispub); int ossl_do_PVK_header(const unsigned char **in, unsigned int length, int skip_magic, unsigned int *psaltlen, unsigned int *pkeylen); # ifndef OPENSSL_NO_DEPRECATED_3_0 # ifndef OPENSSL_NO_DSA DSA *ossl_b2i_DSA_after_header(const unsigned char **in, unsigned int bitlen, int ispub); # endif RSA *ossl_b2i_RSA_after_header(const unsigned char **in, unsigned int bitlen, int ispub); # endif EVP_PKEY *ossl_b2i(const unsigned char **in, unsigned int length, int *ispub); EVP_PKEY *ossl_b2i_bio(BIO *in, int *ispub); # ifndef OPENSSL_NO_DEPRECATED_3_0 # ifndef OPENSSL_NO_DSA DSA *b2i_DSA_PVK_bio(BIO *in, pem_password_cb *cb, void *u); DSA *b2i_DSA_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx, const char *propq); # endif RSA *b2i_RSA_PVK_bio(BIO *in, pem_password_cb *cb, void *u); RSA *b2i_RSA_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx, const char *propq); # endif #endif
./openssl/include/crypto/async.h
/* * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_ASYNC_H # define OSSL_CRYPTO_ASYNC_H # pragma once # include <openssl/async.h> int async_init(void); void async_deinit(void); #endif
./openssl/include/crypto/buffererr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_BUFFERERR_H # define OSSL_CRYPTO_BUFFERERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_BUF_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/pkcs7err.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_PKCS7ERR_H # define OSSL_CRYPTO_PKCS7ERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_PKCS7_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/dh.h
/* * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_DH_H # define OSSL_CRYPTO_DH_H # pragma once # include <openssl/core.h> # include <openssl/params.h> # include <openssl/dh.h> # include "internal/ffc.h" DH *ossl_dh_new_by_nid_ex(OSSL_LIB_CTX *libctx, int nid); DH *ossl_dh_new_ex(OSSL_LIB_CTX *libctx); void ossl_dh_set0_libctx(DH *d, OSSL_LIB_CTX *libctx); int ossl_dh_generate_ffc_parameters(DH *dh, int type, int pbits, int qbits, BN_GENCB *cb); int ossl_dh_generate_public_key(BN_CTX *ctx, const DH *dh, const BIGNUM *priv_key, BIGNUM *pub_key); int ossl_dh_get_named_group_uid_from_size(int pbits); const char *ossl_dh_gen_type_id2name(int id); int ossl_dh_gen_type_name2id(const char *name, int type); void ossl_dh_cache_named_group(DH *dh); int ossl_dh_is_named_safe_prime_group(const DH *dh); FFC_PARAMS *ossl_dh_get0_params(DH *dh); int ossl_dh_get0_nid(const DH *dh); int ossl_dh_params_fromdata(DH *dh, const OSSL_PARAM params[]); int ossl_dh_key_fromdata(DH *dh, const OSSL_PARAM params[], int include_private); int ossl_dh_params_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[]); int ossl_dh_key_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[], int include_private); DH *ossl_dh_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf, OSSL_LIB_CTX *libctx, const char *propq); int ossl_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); int ossl_dh_check_pub_key_partial(const DH *dh, const BIGNUM *pub_key, int *ret); int ossl_dh_check_priv_key(const DH *dh, const BIGNUM *priv_key, int *ret); int ossl_dh_check_pairwise(const DH *dh); const DH_METHOD *ossl_dh_get_method(const DH *dh); int ossl_dh_buf2key(DH *key, const unsigned char *buf, size_t len); size_t ossl_dh_key2buf(const DH *dh, unsigned char **pbuf, size_t size, int alloc); int ossl_dh_kdf_X9_42_asn1(unsigned char *out, size_t outlen, const unsigned char *Z, size_t Zlen, const char *cek_alg, const unsigned char *ukm, size_t ukmlen, const EVP_MD *md, OSSL_LIB_CTX *libctx, const char *propq); int ossl_dh_is_foreign(const DH *dh); DH *ossl_dh_dup(const DH *dh, int selection); #endif /* OSSL_CRYPTO_DH_H */
./openssl/include/crypto/sm2err.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_SM2ERR_H # define OSSL_CRYPTO_SM2ERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif # ifndef OPENSSL_NO_SM2 int ossl_err_load_SM2_strings(void); /* * SM2 reason codes. */ # define SM2_R_ASN1_ERROR 100 # define SM2_R_BAD_SIGNATURE 101 # define SM2_R_BUFFER_TOO_SMALL 107 # define SM2_R_DIST_ID_TOO_LARGE 110 # define SM2_R_ID_NOT_SET 112 # define SM2_R_ID_TOO_LARGE 111 # define SM2_R_INVALID_CURVE 108 # define SM2_R_INVALID_DIGEST 102 # define SM2_R_INVALID_DIGEST_TYPE 103 # define SM2_R_INVALID_ENCODING 104 # define SM2_R_INVALID_FIELD 105 # define SM2_R_INVALID_PRIVATE_KEY 113 # define SM2_R_NO_PARAMETERS_SET 109 # define SM2_R_USER_ID_TOO_LARGE 106 # endif # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/rsa.h
/* * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_INTERNAL_RSA_H # define OSSL_INTERNAL_RSA_H # pragma once # include <openssl/core.h> # include <openssl/rsa.h> # include "crypto/types.h" #define RSA_MIN_MODULUS_BITS 512 typedef struct rsa_pss_params_30_st { int hash_algorithm_nid; struct { int algorithm_nid; /* Currently always NID_mgf1 */ int hash_algorithm_nid; } mask_gen; int salt_len; int trailer_field; } RSA_PSS_PARAMS_30; RSA_PSS_PARAMS_30 *ossl_rsa_get0_pss_params_30(RSA *r); int ossl_rsa_pss_params_30_set_defaults(RSA_PSS_PARAMS_30 *rsa_pss_params); int ossl_rsa_pss_params_30_copy(RSA_PSS_PARAMS_30 *to, const RSA_PSS_PARAMS_30 *from); int ossl_rsa_pss_params_30_is_unrestricted(const RSA_PSS_PARAMS_30 *rsa_pss_params); int ossl_rsa_pss_params_30_set_hashalg(RSA_PSS_PARAMS_30 *rsa_pss_params, int hashalg_nid); int ossl_rsa_pss_params_30_set_maskgenhashalg(RSA_PSS_PARAMS_30 *rsa_pss_params, int maskgenhashalg_nid); int ossl_rsa_pss_params_30_set_saltlen(RSA_PSS_PARAMS_30 *rsa_pss_params, int saltlen); int ossl_rsa_pss_params_30_set_trailerfield(RSA_PSS_PARAMS_30 *rsa_pss_params, int trailerfield); int ossl_rsa_pss_params_30_hashalg(const RSA_PSS_PARAMS_30 *rsa_pss_params); int ossl_rsa_pss_params_30_maskgenalg(const RSA_PSS_PARAMS_30 *rsa_pss_params); int ossl_rsa_pss_params_30_maskgenhashalg(const RSA_PSS_PARAMS_30 *rsa_pss_params); int ossl_rsa_pss_params_30_saltlen(const RSA_PSS_PARAMS_30 *rsa_pss_params); int ossl_rsa_pss_params_30_trailerfield(const RSA_PSS_PARAMS_30 *rsa_pss_params); const char *ossl_rsa_mgf_nid2name(int mgf); int ossl_rsa_oaeppss_md2nid(const EVP_MD *md); const char *ossl_rsa_oaeppss_nid2name(int md); RSA *ossl_rsa_new_with_ctx(OSSL_LIB_CTX *libctx); OSSL_LIB_CTX *ossl_rsa_get0_libctx(RSA *r); void ossl_rsa_set0_libctx(RSA *r, OSSL_LIB_CTX *libctx); int ossl_rsa_set0_all_params(RSA *r, STACK_OF(BIGNUM) *primes, STACK_OF(BIGNUM) *exps, STACK_OF(BIGNUM) *coeffs); int ossl_rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes, STACK_OF(BIGNUM_const) *exps, STACK_OF(BIGNUM_const) *coeffs); int ossl_rsa_is_foreign(const RSA *rsa); RSA *ossl_rsa_dup(const RSA *rsa, int selection); int ossl_rsa_todata(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[], int include_private); int ossl_rsa_fromdata(RSA *rsa, const OSSL_PARAM params[], int include_private); int ossl_rsa_pss_params_30_todata(const RSA_PSS_PARAMS_30 *pss, OSSL_PARAM_BLD *bld, OSSL_PARAM params[]); int ossl_rsa_pss_params_30_fromdata(RSA_PSS_PARAMS_30 *pss_params, int *defaults_set, const OSSL_PARAM params[], OSSL_LIB_CTX *libctx); int ossl_rsa_set0_pss_params(RSA *r, RSA_PSS_PARAMS *pss); int ossl_rsa_pss_get_param_unverified(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd, const EVP_MD **pmgf1md, int *psaltlen, int *ptrailerField); RSA_PSS_PARAMS *ossl_rsa_pss_decode(const X509_ALGOR *alg); int ossl_rsa_param_decode(RSA *rsa, const X509_ALGOR *alg); RSA *ossl_rsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf, OSSL_LIB_CTX *libctx, const char *propq); int ossl_rsa_padding_check_PKCS1_type_2(OSSL_LIB_CTX *ctx, unsigned char *to, int tlen, const unsigned char *from, int flen, int num, unsigned char *kdk); int ossl_rsa_padding_check_PKCS1_type_2_TLS(OSSL_LIB_CTX *ctx, unsigned char *to, size_t tlen, const unsigned char *from, size_t flen, int client_version, int alt_version); int ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(OSSL_LIB_CTX *libctx, unsigned char *to, int tlen, const unsigned char *from, int flen, const unsigned char *param, int plen, const EVP_MD *md, const EVP_MD *mgf1md); int ossl_rsa_validate_public(const RSA *key); int ossl_rsa_validate_private(const RSA *key); int ossl_rsa_validate_pairwise(const RSA *key); int ossl_rsa_verify(int dtype, const unsigned char *m, unsigned int m_len, unsigned char *rm, size_t *prm_len, const unsigned char *sigbuf, size_t siglen, RSA *rsa); const unsigned char *ossl_rsa_digestinfo_encoding(int md_nid, size_t *len); extern const char *ossl_rsa_mp_factor_names[]; extern const char *ossl_rsa_mp_exp_names[]; extern const char *ossl_rsa_mp_coeff_names[]; ASN1_STRING *ossl_rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx); int ossl_rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx, const X509_ALGOR *sigalg, EVP_PKEY *pkey); # if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS) int ossl_rsa_acvp_test_gen_params_new(OSSL_PARAM **dst, const OSSL_PARAM src[]); void ossl_rsa_acvp_test_gen_params_free(OSSL_PARAM *dst); int ossl_rsa_acvp_test_set_params(RSA *r, const OSSL_PARAM params[]); int ossl_rsa_acvp_test_get_params(RSA *r, OSSL_PARAM params[]); typedef struct rsa_acvp_test_st RSA_ACVP_TEST; void ossl_rsa_acvp_test_free(RSA_ACVP_TEST *t); # else # define RSA_ACVP_TEST void # endif RSA *evp_pkey_get1_RSA_PSS(EVP_PKEY *pkey); #endif
./openssl/include/crypto/siv.h
/* * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OPENSSL_NO_SIV typedef struct siv128_context SIV128_CONTEXT; SIV128_CONTEXT *ossl_siv128_new(const unsigned char *key, int klen, EVP_CIPHER *cbc, EVP_CIPHER *ctr, OSSL_LIB_CTX *libctx, const char *propq); int ossl_siv128_init(SIV128_CONTEXT *ctx, const unsigned char *key, int klen, const EVP_CIPHER *cbc, const EVP_CIPHER *ctr, OSSL_LIB_CTX *libctx, const char *propq); int ossl_siv128_copy_ctx(SIV128_CONTEXT *dest, SIV128_CONTEXT *src); int ossl_siv128_aad(SIV128_CONTEXT *ctx, const unsigned char *aad, size_t len); int ossl_siv128_encrypt(SIV128_CONTEXT *ctx, const unsigned char *in, unsigned char *out, size_t len); int ossl_siv128_decrypt(SIV128_CONTEXT *ctx, const unsigned char *in, unsigned char *out, size_t len); int ossl_siv128_finish(SIV128_CONTEXT *ctx); int ossl_siv128_set_tag(SIV128_CONTEXT *ctx, const unsigned char *tag, size_t len); int ossl_siv128_get_tag(SIV128_CONTEXT *ctx, unsigned char *tag, size_t len); int ossl_siv128_cleanup(SIV128_CONTEXT *ctx); int ossl_siv128_speed(SIV128_CONTEXT *ctx, int arg); #endif /* OPENSSL_NO_SIV */
./openssl/include/crypto/sm4.h
/* * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved. * Copyright 2017 Ribose Inc. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_SM4_H # define OSSL_CRYPTO_SM4_H # pragma once # include <openssl/opensslconf.h> # include <openssl/e_os2.h> # ifdef OPENSSL_NO_SM4 # error SM4 is disabled. # endif # define SM4_ENCRYPT 1 # define SM4_DECRYPT 0 # define SM4_BLOCK_SIZE 16 # define SM4_KEY_SCHEDULE 32 typedef struct SM4_KEY_st { uint32_t rk[SM4_KEY_SCHEDULE]; } SM4_KEY; int ossl_sm4_set_key(const uint8_t *key, SM4_KEY *ks); void ossl_sm4_encrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks); void ossl_sm4_decrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks); #endif
./openssl/include/crypto/ecerr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_ECERR_H # define OSSL_CRYPTO_ECERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif # ifndef OPENSSL_NO_EC int ossl_err_load_EC_strings(void); # endif # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/asyncerr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_ASYNCERR_H # define OSSL_CRYPTO_ASYNCERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_ASYNC_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/esserr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_ESSERR_H # define OSSL_CRYPTO_ESSERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_ESS_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/httperr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_HTTPERR_H # define OSSL_CRYPTO_HTTPERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_HTTP_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/evp.h
/* * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_EVP_H # define OSSL_CRYPTO_EVP_H # pragma once # include <openssl/evp.h> # include <openssl/core_dispatch.h> # include "internal/refcount.h" # include "crypto/ecx.h" /* * Default PKCS5 PBE KDF salt lengths * In RFC 8018, PBE1 uses 8 bytes (64 bits) for its salt length. * It also specifies to use at least 8 bytes for PBES2. * The NIST requirement for PBKDF2 is 128 bits so we use this as the * default for PBE2 (scrypt and HKDF2) */ # define PKCS5_DEFAULT_PBE1_SALT_LEN PKCS5_SALT_LEN # define PKCS5_DEFAULT_PBE2_SALT_LEN 16 /* * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag * values in evp.h */ #define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400 #define EVP_MD_CTX_FLAG_FINALISED 0x0800 #define evp_pkey_ctx_is_legacy(ctx) \ ((ctx)->keymgmt == NULL) #define evp_pkey_ctx_is_provided(ctx) \ (!evp_pkey_ctx_is_legacy(ctx)) struct evp_pkey_ctx_st { /* Actual operation */ int operation; /* * Library context, property query, keytype and keymgmt associated with * this context */ OSSL_LIB_CTX *libctx; char *propquery; const char *keytype; /* If |pkey| below is set, this field is always a reference to its keymgmt */ EVP_KEYMGMT *keymgmt; union { struct { void *genctx; } keymgmt; struct { EVP_KEYEXCH *exchange; /* * Opaque ctx returned from a providers exchange algorithm * implementation OSSL_FUNC_keyexch_newctx() */ void *algctx; } kex; struct { EVP_SIGNATURE *signature; /* * Opaque ctx returned from a providers signature algorithm * implementation OSSL_FUNC_signature_newctx() */ void *algctx; } sig; struct { EVP_ASYM_CIPHER *cipher; /* * Opaque ctx returned from a providers asymmetric cipher algorithm * implementation OSSL_FUNC_asym_cipher_newctx() */ void *algctx; } ciph; struct { EVP_KEM *kem; /* * Opaque ctx returned from a providers KEM algorithm * implementation OSSL_FUNC_kem_newctx() */ void *algctx; } encap; } op; /* * Cached parameters. Inits of operations that depend on these should * call evp_pkey_ctx_use_delayed_data() when the operation has been set * up properly. */ struct { /* Distinguishing Identifier, ISO/IEC 15946-3, FIPS 196 */ char *dist_id_name; /* The name used with EVP_PKEY_CTX_ctrl_str() */ void *dist_id; /* The distinguishing ID itself */ size_t dist_id_len; /* The length of the distinguishing ID */ /* Indicators of what has been set. Keep them together! */ unsigned int dist_id_set : 1; } cached_parameters; /* Application specific data, usually used by the callback */ void *app_data; /* Keygen callback */ EVP_PKEY_gen_cb *pkey_gencb; /* implementation specific keygen data */ int *keygen_info; int keygen_info_count; /* Legacy fields below */ /* EVP_PKEY identity */ int legacy_keytype; /* Method associated with this operation */ const EVP_PKEY_METHOD *pmeth; /* Engine that implements this method or NULL if builtin */ ENGINE *engine; /* Key: may be NULL */ EVP_PKEY *pkey; /* Peer key for key agreement, may be NULL */ EVP_PKEY *peerkey; /* Algorithm specific data */ void *data; /* Indicator if digest_custom needs to be called */ unsigned int flag_call_digest_custom:1; /* * Used to support taking custody of memory in the case of a provider being * used with the deprecated EVP_PKEY_CTX_set_rsa_keygen_pubexp() API. This * member should NOT be used for any other purpose and should be removed * when said deprecated API is excised completely. */ BIGNUM *rsa_pubexp; } /* EVP_PKEY_CTX */ ; #define EVP_PKEY_FLAG_DYNAMIC 1 struct evp_pkey_method_st { int pkey_id; int flags; int (*init) (EVP_PKEY_CTX *ctx); int (*copy) (EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src); void (*cleanup) (EVP_PKEY_CTX *ctx); int (*paramgen_init) (EVP_PKEY_CTX *ctx); int (*paramgen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); int (*keygen_init) (EVP_PKEY_CTX *ctx); int (*keygen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); int (*sign_init) (EVP_PKEY_CTX *ctx); int (*sign) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen); int (*verify_init) (EVP_PKEY_CTX *ctx); int (*verify) (EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen, const unsigned char *tbs, size_t tbslen); int (*verify_recover_init) (EVP_PKEY_CTX *ctx); int (*verify_recover) (EVP_PKEY_CTX *ctx, unsigned char *rout, size_t *routlen, const unsigned char *sig, size_t siglen); int (*signctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); int (*signctx) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, EVP_MD_CTX *mctx); int (*verifyctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); int (*verifyctx) (EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen, EVP_MD_CTX *mctx); int (*encrypt_init) (EVP_PKEY_CTX *ctx); int (*encrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen); int (*decrypt_init) (EVP_PKEY_CTX *ctx); int (*decrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen); int (*derive_init) (EVP_PKEY_CTX *ctx); int (*derive) (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen); int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, void *p2); int (*ctrl_str) (EVP_PKEY_CTX *ctx, const char *type, const char *value); int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen); int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig, size_t siglen, const unsigned char *tbs, size_t tbslen); int (*check) (EVP_PKEY *pkey); int (*public_check) (EVP_PKEY *pkey); int (*param_check) (EVP_PKEY *pkey); int (*digest_custom) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); } /* EVP_PKEY_METHOD */ ; DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD) void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx); const EVP_PKEY_METHOD *ossl_dh_pkey_method(void); const EVP_PKEY_METHOD *ossl_dhx_pkey_method(void); const EVP_PKEY_METHOD *ossl_dsa_pkey_method(void); const EVP_PKEY_METHOD *ossl_ec_pkey_method(void); const EVP_PKEY_METHOD *ossl_ecx25519_pkey_method(void); const EVP_PKEY_METHOD *ossl_ecx448_pkey_method(void); const EVP_PKEY_METHOD *ossl_ed25519_pkey_method(void); const EVP_PKEY_METHOD *ossl_ed448_pkey_method(void); const EVP_PKEY_METHOD *ossl_rsa_pkey_method(void); const EVP_PKEY_METHOD *ossl_rsa_pss_pkey_method(void); struct evp_mac_st { OSSL_PROVIDER *prov; int name_id; char *type_name; const char *description; CRYPTO_REF_COUNT refcnt; OSSL_FUNC_mac_newctx_fn *newctx; OSSL_FUNC_mac_dupctx_fn *dupctx; OSSL_FUNC_mac_freectx_fn *freectx; OSSL_FUNC_mac_init_fn *init; OSSL_FUNC_mac_update_fn *update; OSSL_FUNC_mac_final_fn *final; OSSL_FUNC_mac_gettable_params_fn *gettable_params; OSSL_FUNC_mac_gettable_ctx_params_fn *gettable_ctx_params; OSSL_FUNC_mac_settable_ctx_params_fn *settable_ctx_params; OSSL_FUNC_mac_get_params_fn *get_params; OSSL_FUNC_mac_get_ctx_params_fn *get_ctx_params; OSSL_FUNC_mac_set_ctx_params_fn *set_ctx_params; }; struct evp_kdf_st { OSSL_PROVIDER *prov; int name_id; char *type_name; const char *description; CRYPTO_REF_COUNT refcnt; OSSL_FUNC_kdf_newctx_fn *newctx; OSSL_FUNC_kdf_dupctx_fn *dupctx; OSSL_FUNC_kdf_freectx_fn *freectx; OSSL_FUNC_kdf_reset_fn *reset; OSSL_FUNC_kdf_derive_fn *derive; OSSL_FUNC_kdf_gettable_params_fn *gettable_params; OSSL_FUNC_kdf_gettable_ctx_params_fn *gettable_ctx_params; OSSL_FUNC_kdf_settable_ctx_params_fn *settable_ctx_params; OSSL_FUNC_kdf_get_params_fn *get_params; OSSL_FUNC_kdf_get_ctx_params_fn *get_ctx_params; OSSL_FUNC_kdf_set_ctx_params_fn *set_ctx_params; }; #define EVP_ORIG_DYNAMIC 0 #define EVP_ORIG_GLOBAL 1 #define EVP_ORIG_METH 2 struct evp_md_st { /* nid */ int type; /* Legacy structure members */ int pkey_type; int md_size; unsigned long flags; int origin; int (*init) (EVP_MD_CTX *ctx); int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count); int (*final) (EVP_MD_CTX *ctx, unsigned char *md); int (*copy) (EVP_MD_CTX *to, const EVP_MD_CTX *from); int (*cleanup) (EVP_MD_CTX *ctx); int block_size; int ctx_size; /* how big does the ctx->md_data need to be */ /* control function */ int (*md_ctrl) (EVP_MD_CTX *ctx, int cmd, int p1, void *p2); /* New structure members */ /* Above comment to be removed when legacy has gone */ int name_id; char *type_name; const char *description; OSSL_PROVIDER *prov; CRYPTO_REF_COUNT refcnt; OSSL_FUNC_digest_newctx_fn *newctx; OSSL_FUNC_digest_init_fn *dinit; OSSL_FUNC_digest_update_fn *dupdate; OSSL_FUNC_digest_final_fn *dfinal; OSSL_FUNC_digest_squeeze_fn *dsqueeze; OSSL_FUNC_digest_digest_fn *digest; OSSL_FUNC_digest_freectx_fn *freectx; OSSL_FUNC_digest_dupctx_fn *dupctx; OSSL_FUNC_digest_get_params_fn *get_params; OSSL_FUNC_digest_set_ctx_params_fn *set_ctx_params; OSSL_FUNC_digest_get_ctx_params_fn *get_ctx_params; OSSL_FUNC_digest_gettable_params_fn *gettable_params; OSSL_FUNC_digest_settable_ctx_params_fn *settable_ctx_params; OSSL_FUNC_digest_gettable_ctx_params_fn *gettable_ctx_params; } /* EVP_MD */ ; struct evp_cipher_st { int nid; int block_size; /* Default value for variable length ciphers */ int key_len; int iv_len; /* Legacy structure members */ /* Various flags */ unsigned long flags; /* How the EVP_CIPHER was created. */ int origin; /* init key */ int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc); /* encrypt/decrypt data */ int (*do_cipher) (EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl); /* cleanup ctx */ int (*cleanup) (EVP_CIPHER_CTX *); /* how big ctx->cipher_data needs to be */ int ctx_size; /* Populate a ASN1_TYPE with parameters */ int (*set_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *); /* Get parameters from a ASN1_TYPE */ int (*get_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *); /* Miscellaneous operations */ int (*ctrl) (EVP_CIPHER_CTX *, int type, int arg, void *ptr); /* Application data */ void *app_data; /* New structure members */ /* Above comment to be removed when legacy has gone */ int name_id; char *type_name; const char *description; OSSL_PROVIDER *prov; CRYPTO_REF_COUNT refcnt; OSSL_FUNC_cipher_newctx_fn *newctx; OSSL_FUNC_cipher_encrypt_init_fn *einit; OSSL_FUNC_cipher_decrypt_init_fn *dinit; OSSL_FUNC_cipher_update_fn *cupdate; OSSL_FUNC_cipher_final_fn *cfinal; OSSL_FUNC_cipher_cipher_fn *ccipher; OSSL_FUNC_cipher_freectx_fn *freectx; OSSL_FUNC_cipher_dupctx_fn *dupctx; OSSL_FUNC_cipher_get_params_fn *get_params; OSSL_FUNC_cipher_get_ctx_params_fn *get_ctx_params; OSSL_FUNC_cipher_set_ctx_params_fn *set_ctx_params; OSSL_FUNC_cipher_gettable_params_fn *gettable_params; OSSL_FUNC_cipher_gettable_ctx_params_fn *gettable_ctx_params; OSSL_FUNC_cipher_settable_ctx_params_fn *settable_ctx_params; } /* EVP_CIPHER */ ; /* Macros to code block cipher wrappers */ /* Wrapper functions for each cipher mode */ #define EVP_C_DATA(kstruct, ctx) \ ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx)) #define BLOCK_CIPHER_ecb_loop() \ size_t i, bl; \ bl = EVP_CIPHER_CTX_get0_cipher(ctx)->block_size; \ if (inl < bl) return 1;\ inl -= bl; \ for (i=0; i <= inl; i+=bl) #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \ static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ {\ BLOCK_CIPHER_ecb_loop() \ cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_is_encrypting(ctx)); \ return 1;\ } #define EVP_MAXCHUNK ((size_t)1 << 30) #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \ static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ {\ while(inl>=EVP_MAXCHUNK) {\ int num = EVP_CIPHER_CTX_get_num(ctx);\ cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \ EVP_CIPHER_CTX_set_num(ctx, num);\ inl-=EVP_MAXCHUNK;\ in +=EVP_MAXCHUNK;\ out+=EVP_MAXCHUNK;\ }\ if (inl) {\ int num = EVP_CIPHER_CTX_get_num(ctx);\ cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \ EVP_CIPHER_CTX_set_num(ctx, num);\ }\ return 1;\ } #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \ static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ {\ while(inl>=EVP_MAXCHUNK) \ {\ cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));\ inl-=EVP_MAXCHUNK;\ in +=EVP_MAXCHUNK;\ out+=EVP_MAXCHUNK;\ }\ if (inl)\ cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));\ return 1;\ } #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \ static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ {\ size_t chunk = EVP_MAXCHUNK;\ if (cbits == 1) chunk >>= 3;\ if (inl < chunk) chunk = inl;\ while (inl && inl >= chunk)\ {\ int num = EVP_CIPHER_CTX_get_num(ctx);\ cprefix##_cfb##cbits##_encrypt(in, out, (long) \ ((cbits == 1) \ && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \ ? chunk*8 : chunk), \ &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv,\ &num, EVP_CIPHER_CTX_is_encrypting(ctx));\ EVP_CIPHER_CTX_set_num(ctx, num);\ inl -= chunk;\ in += chunk;\ out += chunk;\ if (inl < chunk) chunk = inl;\ }\ return 1;\ } #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \ BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \ BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \ BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \ BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \ key_len, iv_len, flags, init_key, cleanup, \ set_asn1, get_asn1, ctrl) \ static const EVP_CIPHER cname##_##mode = { \ nid##_##nmode, block_size, key_len, iv_len, \ flags | EVP_CIPH_##MODE##_MODE, \ EVP_ORIG_GLOBAL, \ init_key, \ cname##_##mode##_cipher, \ cleanup, \ sizeof(kstruct), \ set_asn1, get_asn1,\ ctrl, \ NULL \ }; \ const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; } #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \ iv_len, flags, init_key, cleanup, set_asn1, \ get_asn1, ctrl) \ BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \ iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl) #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \ iv_len, cbits, flags, init_key, cleanup, \ set_asn1, get_asn1, ctrl) \ BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \ key_len, iv_len, flags, init_key, cleanup, set_asn1, \ get_asn1, ctrl) #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \ iv_len, cbits, flags, init_key, cleanup, \ set_asn1, get_asn1, ctrl) \ BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \ key_len, iv_len, flags, init_key, cleanup, set_asn1, \ get_asn1, ctrl) #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \ flags, init_key, cleanup, set_asn1, \ get_asn1, ctrl) \ BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \ 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl) #define BLOCK_CIPHER_defs(cname, kstruct, \ nid, block_size, key_len, iv_len, cbits, flags, \ init_key, cleanup, set_asn1, get_asn1, ctrl) \ BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \ init_key, cleanup, set_asn1, get_asn1, ctrl) \ BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \ flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \ flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \ init_key, cleanup, set_asn1, get_asn1, ctrl) /*- #define BLOCK_CIPHER_defs(cname, kstruct, \ nid, block_size, key_len, iv_len, flags,\ init_key, cleanup, set_asn1, get_asn1, ctrl)\ static const EVP_CIPHER cname##_cbc = {\ nid##_cbc, block_size, key_len, iv_len, \ flags | EVP_CIPH_CBC_MODE,\ EVP_ORIG_GLOBAL,\ init_key,\ cname##_cbc_cipher,\ cleanup,\ sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ set_asn1, get_asn1,\ ctrl, \ NULL \ };\ const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\ static const EVP_CIPHER cname##_cfb = {\ nid##_cfb64, 1, key_len, iv_len, \ flags | EVP_CIPH_CFB_MODE,\ EVP_ORIG_GLOBAL,\ init_key,\ cname##_cfb_cipher,\ cleanup,\ sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ set_asn1, get_asn1,\ ctrl,\ NULL \ };\ const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\ static const EVP_CIPHER cname##_ofb = {\ nid##_ofb64, 1, key_len, iv_len, \ flags | EVP_CIPH_OFB_MODE,\ EVP_ORIG_GLOBAL,\ init_key,\ cname##_ofb_cipher,\ cleanup,\ sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ set_asn1, get_asn1,\ ctrl,\ NULL \ };\ const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\ static const EVP_CIPHER cname##_ecb = {\ nid##_ecb, block_size, key_len, iv_len, \ flags | EVP_CIPH_ECB_MODE,\ EVP_ORIG_GLOBAL,\ init_key,\ cname##_ecb_cipher,\ cleanup,\ sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ set_asn1, get_asn1,\ ctrl,\ NULL \ };\ const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; } */ #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \ block_size, key_len, iv_len, cbits, \ flags, init_key, \ cleanup, set_asn1, get_asn1, ctrl) \ BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \ BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \ cbits, flags, init_key, cleanup, set_asn1, \ get_asn1, ctrl) #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \ BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \ BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \ NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \ (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \ cipher##_init_key, NULL, NULL, NULL, NULL) typedef struct { unsigned char iv[EVP_MAX_IV_LENGTH]; unsigned int iv_len; unsigned int tag_len; } evp_cipher_aead_asn1_params; int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type, evp_cipher_aead_asn1_params *params); int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type, evp_cipher_aead_asn1_params *params); /* * To support transparent execution of operation in backends other * than the "origin" key, we support transparent export/import to * those providers, and maintain a cache of the imported keydata, * so we don't need to redo the export/import every time we perform * the same operation in that same provider. * This requires that the "origin" backend (whether it's a legacy or a * provider "origin") implements exports, and that the target provider * has an EVP_KEYMGMT that implements import. */ typedef struct { EVP_KEYMGMT *keymgmt; void *keydata; int selection; } OP_CACHE_ELEM; DEFINE_STACK_OF(OP_CACHE_ELEM) /* * An EVP_PKEY can have the following states: * * untyped & empty: * * type == EVP_PKEY_NONE && keymgmt == NULL * * typed & empty: * * (type != EVP_PKEY_NONE && pkey.ptr == NULL) ## legacy (libcrypto only) * || (keymgmt != NULL && keydata == NULL) ## provider side * * fully assigned: * * (type != EVP_PKEY_NONE && pkey.ptr != NULL) ## legacy (libcrypto only) * || (keymgmt != NULL && keydata != NULL) ## provider side * * The easiest way to detect a legacy key is: * * keymgmt == NULL && type != EVP_PKEY_NONE * * The easiest way to detect a provider side key is: * * keymgmt != NULL */ #define evp_pkey_is_blank(pk) \ ((pk)->type == EVP_PKEY_NONE && (pk)->keymgmt == NULL) #define evp_pkey_is_typed(pk) \ ((pk)->type != EVP_PKEY_NONE || (pk)->keymgmt != NULL) #ifndef FIPS_MODULE # define evp_pkey_is_assigned(pk) \ ((pk)->pkey.ptr != NULL || (pk)->keydata != NULL) #else # define evp_pkey_is_assigned(pk) \ ((pk)->keydata != NULL) #endif #define evp_pkey_is_legacy(pk) \ ((pk)->type != EVP_PKEY_NONE && (pk)->keymgmt == NULL) #define evp_pkey_is_provided(pk) \ ((pk)->keymgmt != NULL) union legacy_pkey_st { void *ptr; struct rsa_st *rsa; /* RSA */ # ifndef OPENSSL_NO_DSA struct dsa_st *dsa; /* DSA */ # endif # ifndef OPENSSL_NO_DH struct dh_st *dh; /* DH */ # endif # ifndef OPENSSL_NO_EC struct ec_key_st *ec; /* ECC */ # ifndef OPENSSL_NO_ECX ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */ # endif # endif }; struct evp_pkey_st { /* == Legacy attributes == */ int type; int save_type; # ifndef FIPS_MODULE /* * Legacy key "origin" is composed of a pointer to an EVP_PKEY_ASN1_METHOD, * a pointer to a low level key and possibly a pointer to an engine. */ const EVP_PKEY_ASN1_METHOD *ameth; ENGINE *engine; ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */ /* Union to store the reference to an origin legacy key */ union legacy_pkey_st pkey; /* Union to store the reference to a non-origin legacy key */ union legacy_pkey_st legacy_cache_pkey; # endif /* == Common attributes == */ CRYPTO_REF_COUNT references; CRYPTO_RWLOCK *lock; #ifndef FIPS_MODULE STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */ int save_parameters; unsigned int foreign:1; /* the low-level key is using an engine or an app-method */ CRYPTO_EX_DATA ex_data; #endif /* == Provider attributes == */ /* * Provider keydata "origin" is composed of a pointer to an EVP_KEYMGMT * and a pointer to the provider side key data. This is never used at * the same time as the legacy key data above. */ EVP_KEYMGMT *keymgmt; void *keydata; /* * If any libcrypto code does anything that may modify the keydata * contents, this dirty counter must be incremented. */ size_t dirty_cnt; /* * To support transparent execution of operation in backends other * than the "origin" key, we support transparent export/import to * those providers, and maintain a cache of the imported keydata, * so we don't need to redo the export/import every time we perform * the same operation in that same provider. */ STACK_OF(OP_CACHE_ELEM) *operation_cache; /* * We keep a copy of that "origin"'s dirty count, so we know if the * operation cache needs flushing. */ size_t dirty_cnt_copy; /* Cache of key object information */ struct { int bits; int security_bits; int size; } cache; } /* EVP_PKEY */ ; #define EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) \ ((ctx)->operation == EVP_PKEY_OP_SIGN \ || (ctx)->operation == EVP_PKEY_OP_SIGNCTX \ || (ctx)->operation == EVP_PKEY_OP_VERIFY \ || (ctx)->operation == EVP_PKEY_OP_VERIFYCTX \ || (ctx)->operation == EVP_PKEY_OP_VERIFYRECOVER) #define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \ ((ctx)->operation == EVP_PKEY_OP_DERIVE) #define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) \ ((ctx)->operation == EVP_PKEY_OP_ENCRYPT \ || (ctx)->operation == EVP_PKEY_OP_DECRYPT) #define EVP_PKEY_CTX_IS_GEN_OP(ctx) \ ((ctx)->operation == EVP_PKEY_OP_PARAMGEN \ || (ctx)->operation == EVP_PKEY_OP_KEYGEN) #define EVP_PKEY_CTX_IS_FROMDATA_OP(ctx) \ ((ctx)->operation == EVP_PKEY_OP_FROMDATA) #define EVP_PKEY_CTX_IS_KEM_OP(ctx) \ ((ctx)->operation == EVP_PKEY_OP_ENCAPSULATE \ || (ctx)->operation == EVP_PKEY_OP_DECAPSULATE) void openssl_add_all_ciphers_int(void); void openssl_add_all_digests_int(void); void evp_cleanup_int(void); void evp_app_cleanup_int(void); void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx, EVP_KEYMGMT **keymgmt, const char *propquery); #ifndef FIPS_MODULE int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src); void *evp_pkey_get_legacy(EVP_PKEY *pk); void evp_pkey_free_legacy(EVP_PKEY *x); EVP_PKEY *evp_pkcs82pkey_legacy(const PKCS8_PRIV_KEY_INFO *p8inf, OSSL_LIB_CTX *libctx, const char *propq); #endif /* * KEYMGMT utility functions */ /* * Key import structure and helper function, to be used as an export callback */ struct evp_keymgmt_util_try_import_data_st { EVP_KEYMGMT *keymgmt; void *keydata; int selection; }; int evp_keymgmt_util_try_import(const OSSL_PARAM params[], void *arg); int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt, void *keydata); EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata); int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection, OSSL_CALLBACK *export_cb, void *export_cbarg); void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt, int selection); OP_CACHE_ELEM *evp_keymgmt_util_find_operation_cache(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt, int selection); int evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk); int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt, void *keydata, int selection); void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk); void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt, int selection, const OSSL_PARAM params[]); int evp_keymgmt_util_has(EVP_PKEY *pk, int selection); int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection); int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection); void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt, void *genctx, OSSL_CALLBACK *cb, void *cbarg); int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT *keymgmt, void *keydata, char *mdname, size_t mdname_sz); const char *evp_keymgmt_util_query_operation_name(EVP_KEYMGMT *keymgmt, int op_id); /* * KEYMGMT provider interface functions */ void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt); void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keyddata); int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt, void *keydata, OSSL_PARAM params[]); int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt, void *keydata, const OSSL_PARAM params[]); void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection, const OSSL_PARAM params[]); int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx, void *templ); int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx, const OSSL_PARAM params[]); void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx, OSSL_CALLBACK *cb, void *cbarg); void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx); int evp_keymgmt_has_load(const EVP_KEYMGMT *keymgmt); void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt, const void *objref, size_t objref_sz); int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keyddata, int selection); int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata, int selection, int checktype); int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt, const void *keydata1, const void *keydata2, int selection); int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata, int selection, const OSSL_PARAM params[]); const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt, int selection); int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata, int selection, OSSL_CALLBACK *param_cb, void *cbarg); const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt, int selection); void *evp_keymgmt_dup(const EVP_KEYMGMT *keymgmt, const void *keydata_from, int selection); EVP_KEYMGMT *evp_keymgmt_fetch_from_prov(OSSL_PROVIDER *prov, const char *name, const char *properties); /* Pulling defines out of C source files */ # define EVP_RC4_KEY_SIZE 16 # ifndef TLS1_1_VERSION # define TLS1_1_VERSION 0x0302 # endif void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags); /* EVP_ENCODE_CTX flags */ /* Don't generate new lines when encoding */ #define EVP_ENCODE_CTX_NO_NEWLINES 1 /* Use the SRP base64 alphabet instead of the standard one */ #define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2 const EVP_CIPHER *evp_get_cipherbyname_ex(OSSL_LIB_CTX *libctx, const char *name); const EVP_MD *evp_get_digestbyname_ex(OSSL_LIB_CTX *libctx, const char *name); int ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen, const unsigned char *salt, int saltlen, int iter, const EVP_MD *digest, int keylen, unsigned char *out, OSSL_LIB_CTX *libctx, const char *propq); # ifndef FIPS_MODULE /* * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params(). * * Return 1 on success, 0 or negative for errors. * * In particular they return -2 if any of the params is not supported. * * They are not available in FIPS_MODULE as they depend on * - EVP_PKEY_CTX_{get,set}_params() * - EVP_PKEY_CTX_{gettable,settable}_params() * */ int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params); int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params); EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id, OSSL_LIB_CTX *libctx, const char *propq); int evp_pkey_name2type(const char *name); const char *evp_pkey_type2name(int type); int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx); # endif /* !defined(FIPS_MODULE) */ int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx); int evp_method_store_remove_all_provided(const OSSL_PROVIDER *prov); int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable, int loadconfig); int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq, int loadconfig, int mirrored); char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig); void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_digest); /* just free the algctx if set, returns 0 on inconsistent state of ctx */ int evp_md_ctx_free_algctx(EVP_MD_CTX *ctx); /* Three possible states: */ # define EVP_PKEY_STATE_UNKNOWN 0 # define EVP_PKEY_STATE_LEGACY 1 # define EVP_PKEY_STATE_PROVIDER 2 int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx); /* These two must ONLY be called for provider side operations */ int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd, int p1, void *p2); int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name, const char *value); /* These two must ONLY be called for legacy operations */ int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params); int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params); /* This must ONLY be called for legacy EVP_PKEYs */ int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params); /* Same as the public get0 functions but are not const */ # ifndef OPENSSL_NO_DEPRECATED_3_0 DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey); EC_KEY *evp_pkey_get0_EC_KEY_int(const EVP_PKEY *pkey); RSA *evp_pkey_get0_RSA_int(const EVP_PKEY *pkey); # endif /* Get internal identification number routines */ int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher); int evp_cipher_get_number(const EVP_CIPHER *cipher); int evp_kdf_get_number(const EVP_KDF *kdf); int evp_kem_get_number(const EVP_KEM *wrap); int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch); int evp_keymgmt_get_number(const EVP_KEYMGMT *keymgmt); int evp_keymgmt_get_legacy_alg(const EVP_KEYMGMT *keymgmt); int evp_mac_get_number(const EVP_MAC *mac); int evp_md_get_number(const EVP_MD *md); int evp_rand_get_number(const EVP_RAND *rand); int evp_rand_can_seed(EVP_RAND_CTX *ctx); size_t evp_rand_get_seed(EVP_RAND_CTX *ctx, unsigned char **buffer, int entropy, size_t min_len, size_t max_len, int prediction_resistance, const unsigned char *adin, size_t adin_len); void evp_rand_clear_seed(EVP_RAND_CTX *ctx, unsigned char *buffer, size_t b_len); int evp_signature_get_number(const EVP_SIGNATURE *signature); int evp_pkey_decrypt_alloc(EVP_PKEY_CTX *ctx, unsigned char **outp, size_t *outlenp, size_t expected_outlen, const unsigned char *in, size_t inlen); #endif /* OSSL_CRYPTO_EVP_H */
./openssl/include/crypto/asn1err.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_ASN1ERR_H # define OSSL_CRYPTO_ASN1ERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_ASN1_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/sha.h
/* * Copyright 2018-2023 The OpenSSL Project Authors. All Rights Reserved. * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_SHA_H # define OSSL_CRYPTO_SHA_H # pragma once # include <openssl/sha.h> int ossl_sha256_192_init(SHA256_CTX *c); int sha512_224_init(SHA512_CTX *); int sha512_256_init(SHA512_CTX *); int ossl_sha1_ctrl(SHA_CTX *ctx, int cmd, int mslen, void *ms); unsigned char *ossl_sha1(const unsigned char *d, size_t n, unsigned char *md); #endif
./openssl/include/crypto/cryptlib.h
/* * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_CRYPTLIB_H # define OSSL_CRYPTO_CRYPTLIB_H # pragma once # include <openssl/core.h> # include "internal/cryptlib.h" /* This file is not scanned by mkdef.pl, whereas cryptlib.h is */ int ossl_init_thread_start(const void *index, void *arg, OSSL_thread_stop_handler_fn handfn); int ossl_init_thread_deregister(void *index); int ossl_init_thread(void); void ossl_cleanup_thread(void); void ossl_ctx_thread_stop(OSSL_LIB_CTX *ctx); /* * OPENSSL_INIT flags. The primary list of these is in crypto.h. Flags below * are those omitted from crypto.h because they are "reserved for internal * use". */ # define OPENSSL_INIT_BASE_ONLY 0x00040000L void ossl_trace_cleanup(void); void ossl_malloc_setup_failures(void); int ossl_crypto_alloc_ex_data_intern(int class_index, void *obj, CRYPTO_EX_DATA *ad, int idx); #endif /* OSSL_CRYPTO_CRYPTLIB_H */
./openssl/include/crypto/comperr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_COMPERR_H # define OSSL_CRYPTO_COMPERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif # ifndef OPENSSL_NO_COMP int ossl_err_load_COMP_strings(void); # endif # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/bnerr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_BNERR_H # define OSSL_CRYPTO_BNERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_BN_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/pkcs7.h
/* * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_PKCS7_H # define OSSL_CRYPTO_PKCS7_H # pragma once void ossl_pkcs7_resolve_libctx(PKCS7 *p7); void ossl_pkcs7_set0_libctx(PKCS7 *p7, OSSL_LIB_CTX *ctx); int ossl_pkcs7_set1_propq(PKCS7 *p7, const char *propq); #endif
./openssl/include/crypto/conferr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_CONFERR_H # define OSSL_CRYPTO_CONFERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_CONF_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/decodererr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_DECODERERR_H # define OSSL_CRYPTO_DECODERERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_OSSL_DECODER_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/ctype.h
/* * Copyright 2017-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * This version of ctype.h provides a standardised and platform * independent implementation that supports seven bit ASCII characters. * The specific intent is to not pass extended ASCII characters (> 127) * even if the host operating system would. * * There is EBCDIC support included for machines which use this. However, * there are a number of concerns about how well EBCDIC is supported * throughout the rest of the source code. Refer to issue #4154 for * details. */ #ifndef OSSL_CRYPTO_CTYPE_H # define OSSL_CRYPTO_CTYPE_H # pragma once # include <openssl/e_os2.h> # define CTYPE_MASK_lower 0x1 # define CTYPE_MASK_upper 0x2 # define CTYPE_MASK_digit 0x4 # define CTYPE_MASK_space 0x8 # define CTYPE_MASK_xdigit 0x10 # define CTYPE_MASK_blank 0x20 # define CTYPE_MASK_cntrl 0x40 # define CTYPE_MASK_graph 0x80 # define CTYPE_MASK_print 0x100 # define CTYPE_MASK_punct 0x200 # define CTYPE_MASK_base64 0x400 # define CTYPE_MASK_asn1print 0x800 # define CTYPE_MASK_alpha (CTYPE_MASK_lower | CTYPE_MASK_upper) # define CTYPE_MASK_alnum (CTYPE_MASK_alpha | CTYPE_MASK_digit) /* * The ascii mask assumes that any other classification implies that * the character is ASCII and that there are no ASCII characters * that aren't in any of the classifications. * * This assumption holds at the moment, but it might not in the future. */ # define CTYPE_MASK_ascii (~0) # ifdef CHARSET_EBCDIC int ossl_toascii(int c); int ossl_fromascii(int c); # else # define ossl_toascii(c) (c) # define ossl_fromascii(c) (c) # endif int ossl_ctype_check(int c, unsigned int mask); int ossl_tolower(int c); int ossl_toupper(int c); int ossl_isdigit(int c); int ossl_islower(int c); int ossl_isupper(int c); int ossl_ascii_isdigit(int c); # define ossl_isalnum(c) (ossl_ctype_check((c), CTYPE_MASK_alnum)) # define ossl_isalpha(c) (ossl_ctype_check((c), CTYPE_MASK_alpha)) # ifdef CHARSET_EBCDIC # define ossl_isascii(c) (ossl_ctype_check((c), CTYPE_MASK_ascii)) # else # define ossl_isascii(c) (((c) & ~127) == 0) # endif # define ossl_isblank(c) (ossl_ctype_check((c), CTYPE_MASK_blank)) # define ossl_iscntrl(c) (ossl_ctype_check((c), CTYPE_MASK_cntrl)) # define ossl_isgraph(c) (ossl_ctype_check((c), CTYPE_MASK_graph)) # define ossl_isprint(c) (ossl_ctype_check((c), CTYPE_MASK_print)) # define ossl_ispunct(c) (ossl_ctype_check((c), CTYPE_MASK_punct)) # define ossl_isspace(c) (ossl_ctype_check((c), CTYPE_MASK_space)) # define ossl_isxdigit(c) (ossl_ctype_check((c), CTYPE_MASK_xdigit)) # define ossl_isbase64(c) (ossl_ctype_check((c), CTYPE_MASK_base64)) # define ossl_isasn1print(c) (ossl_ctype_check((c), CTYPE_MASK_asn1print)) #endif
./openssl/include/crypto/x509v3err.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_X509V3ERR_H # define OSSL_CRYPTO_X509V3ERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_X509V3_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/chacha.h
/* * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_CHACHA_H #define OSSL_CRYPTO_CHACHA_H # pragma once #include <stddef.h> /* * ChaCha20_ctr32 encrypts |len| bytes from |inp| with the given key and * nonce and writes the result to |out|, which may be equal to |inp|. * The |key| is not 32 bytes of verbatim key material though, but the * said material collected into 8 32-bit elements array in host byte * order. Same approach applies to nonce: the |counter| argument is * pointer to concatenated nonce and counter values collected into 4 * 32-bit elements. This, passing crypto material collected into 32-bit * elements as opposite to passing verbatim byte vectors, is chosen for * efficiency in multi-call scenarios. */ void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp, size_t len, const unsigned int key[8], const unsigned int counter[4]); #ifdef INCLUDE_C_CHACHA20 /* The fallback implementation for `ChaCha20_ctr32`. */ void ChaCha20_ctr32_c(unsigned char *out, const unsigned char *inp, size_t len, const unsigned int key[8], const unsigned int counter[4]); #endif /* * You can notice that there is no key setup procedure. Because it's * as trivial as collecting bytes into 32-bit elements, it's reckoned * that below macro is sufficient. */ #define CHACHA_U8TOU32(p) ( \ ((unsigned int)(p)[0]) | ((unsigned int)(p)[1]<<8) | \ ((unsigned int)(p)[2]<<16) | ((unsigned int)(p)[3]<<24) ) #define CHACHA_KEY_SIZE 32 #define CHACHA_CTR_SIZE 16 #define CHACHA_BLK_SIZE 64 #endif
./openssl/include/crypto/siphash.h
/* * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_SIPHASH_H # define OSSL_CRYPTO_SIPHASH_H # pragma once # include <stddef.h> # define SIPHASH_BLOCK_SIZE 8 # define SIPHASH_KEY_SIZE 16 # define SIPHASH_MIN_DIGEST_SIZE 8 # define SIPHASH_MAX_DIGEST_SIZE 16 typedef struct siphash_st SIPHASH; size_t SipHash_ctx_size(void); size_t SipHash_hash_size(SIPHASH *ctx); int SipHash_set_hash_size(SIPHASH *ctx, size_t hash_size); int SipHash_Init(SIPHASH *ctx, const unsigned char *k, int crounds, int drounds); void SipHash_Update(SIPHASH *ctx, const unsigned char *in, size_t inlen); int SipHash_Final(SIPHASH *ctx, unsigned char *out, size_t outlen); /* Based on https://131002.net/siphash C reference implementation */ struct siphash_st { uint64_t total_inlen; uint64_t v0; uint64_t v1; uint64_t v2; uint64_t v3; unsigned int len; unsigned int hash_size; unsigned int crounds; unsigned int drounds; unsigned char leavings[SIPHASH_BLOCK_SIZE]; }; /* default: SipHash-2-4 */ # define SIPHASH_C_ROUNDS 2 # define SIPHASH_D_ROUNDS 4 #endif
./openssl/include/crypto/modes.h
/* * Copyright 2010-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* This header can move into provider when legacy support is removed */ #include <openssl/modes.h> #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__) typedef __int64 i64; typedef unsigned __int64 u64; # define U64(C) C##UI64 #elif defined(__arch64__) typedef long i64; typedef unsigned long u64; # define U64(C) C##UL #else typedef long long i64; typedef unsigned long long u64; # define U64(C) C##ULL #endif typedef unsigned int u32; typedef unsigned char u8; #define STRICT_ALIGNMENT 1 #ifndef PEDANTIC # if defined(__i386) || defined(__i386__) || \ defined(__x86_64) || defined(__x86_64__) || \ defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \ defined(__aarch64__) || \ defined(__s390__) || defined(__s390x__) # undef STRICT_ALIGNMENT # endif #endif #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) # if defined(__GNUC__) && __GNUC__>=2 # if defined(__x86_64) || defined(__x86_64__) # define BSWAP8(x) ({ u64 ret_=(x); \ asm ("bswapq %0" \ : "+r"(ret_)); ret_; }) # define BSWAP4(x) ({ u32 ret_=(x); \ asm ("bswapl %0" \ : "+r"(ret_)); ret_; }) # elif (defined(__i386) || defined(__i386__)) && !defined(I386_ONLY) # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \ asm ("bswapl %0; bswapl %1" \ : "+r"(hi_),"+r"(lo_)); \ (u64)hi_<<32|lo_; }) # define BSWAP4(x) ({ u32 ret_=(x); \ asm ("bswapl %0" \ : "+r"(ret_)); ret_; }) # elif defined(__aarch64__) # if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \ __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__ # define BSWAP8(x) ({ u64 ret_; \ asm ("rev %0,%1" \ : "=r"(ret_) : "r"(x)); ret_; }) # define BSWAP4(x) ({ u32 ret_; \ asm ("rev %w0,%w1" \ : "=r"(ret_) : "r"(x)); ret_; }) # endif # elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT) # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \ asm ("rev %0,%0; rev %1,%1" \ : "+r"(hi_),"+r"(lo_)); \ (u64)hi_<<32|lo_; }) # define BSWAP4(x) ({ u32 ret_; \ asm ("rev %0,%1" \ : "=r"(ret_) : "r"((u32)(x))); \ ret_; }) # elif (defined(__riscv_zbb) || defined(__riscv_zbkb)) && __riscv_xlen == 64 # define BSWAP8(x) ({ u64 ret_=(x); \ asm ("rev8 %0,%0" \ : "+r"(ret_)); ret_; }) # define BSWAP4(x) ({ u32 ret_=(x); \ asm ("rev8 %0,%0; srli %0,%0,32"\ : "+&r"(ret_)); ret_; }) # endif # elif defined(_MSC_VER) # if _MSC_VER>=1300 # include <stdlib.h> # pragma intrinsic(_byteswap_uint64,_byteswap_ulong) # define BSWAP8(x) _byteswap_uint64((u64)(x)) # define BSWAP4(x) _byteswap_ulong((u32)(x)) # elif defined(_M_IX86) __inline u32 _bswap4(u32 val) { _asm mov eax, val _asm bswap eax} # define BSWAP4(x) _bswap4(x) # endif # endif #endif #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT) # define GETU32(p) BSWAP4(*(const u32 *)(p)) # define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v) #else # define GETU32(p) ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3]) # define PUTU32(p,v) ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v)) #endif /*- GCM definitions */ typedef struct { u64 hi, lo; } u128; typedef void (*gcm_init_fn)(u128 Htable[16], const u64 H[2]); typedef void (*gcm_ghash_fn)(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); typedef void (*gcm_gmult_fn)(u64 Xi[2], const u128 Htable[16]); struct gcm_funcs_st { gcm_init_fn ginit; gcm_ghash_fn ghash; gcm_gmult_fn gmult; }; struct gcm128_context { /* Following 6 names follow names in GCM specification */ union { u64 u[2]; u32 d[4]; u8 c[16]; size_t t[16 / sizeof(size_t)]; } Yi, EKi, EK0, len, Xi, H; /* * Relative position of Yi, EKi, EK0, len, Xi, H and pre-computed Htable is * used in some assembler modules, i.e. don't change the order! */ u128 Htable[16]; struct gcm_funcs_st funcs; unsigned int mres, ares; block128_f block; void *key; #if !defined(OPENSSL_SMALL_FOOTPRINT) unsigned char Xn[48]; #endif }; /* GHASH functions */ void ossl_gcm_init_4bit(u128 Htable[16], const u64 H[2]); void ossl_gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); void ossl_gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]); /* * The maximum permitted number of cipher blocks per data unit in XTS mode. * Reference IEEE Std 1619-2018. */ #define XTS_MAX_BLOCKS_PER_DATA_UNIT (1<<20) struct xts128_context { void *key1, *key2; block128_f block1, block2; }; /* XTS mode for SM4 algorithm specified by GB/T 17964-2021 */ int ossl_crypto_xts128gb_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16], const unsigned char *inp, unsigned char *out, size_t len, int enc); struct ccm128_context { union { u64 u[2]; u8 c[16]; } nonce, cmac; u64 blocks; block128_f block; void *key; }; #ifndef OPENSSL_NO_OCB typedef union { u64 a[2]; unsigned char c[16]; } OCB_BLOCK; # define ocb_block16_xor(in1,in2,out) \ ( (out)->a[0]=(in1)->a[0]^(in2)->a[0], \ (out)->a[1]=(in1)->a[1]^(in2)->a[1] ) # if STRICT_ALIGNMENT # define ocb_block16_xor_misaligned(in1,in2,out) \ ocb_block_xor((in1)->c,(in2)->c,16,(out)->c) # else # define ocb_block16_xor_misaligned ocb_block16_xor # endif struct ocb128_context { /* Need both encrypt and decrypt key schedules for decryption */ block128_f encrypt; block128_f decrypt; void *keyenc; void *keydec; ocb128_f stream; /* direction dependent */ /* Key dependent variables. Can be reused if key remains the same */ size_t l_index; size_t max_l_index; OCB_BLOCK l_star; OCB_BLOCK l_dollar; OCB_BLOCK *l; /* Must be reset for each session */ struct { u64 blocks_hashed; u64 blocks_processed; OCB_BLOCK offset_aad; OCB_BLOCK sum; OCB_BLOCK offset; OCB_BLOCK checksum; } sess; }; #endif /* OPENSSL_NO_OCB */ #ifndef OPENSSL_NO_SIV #define SIV_LEN 16 typedef union siv_block_u { uint64_t word[SIV_LEN/sizeof(uint64_t)]; unsigned char byte[SIV_LEN]; } SIV_BLOCK; struct siv128_context { /* d stores intermediate results of S2V; it corresponds to D from the pseudocode in section 2.4 of RFC 5297. */ SIV_BLOCK d; SIV_BLOCK tag; EVP_CIPHER_CTX *cipher_ctx; EVP_MAC *mac; EVP_MAC_CTX *mac_ctx_init; int final_ret; int crypto_ok; }; #endif /* OPENSSL_NO_SIV */
./openssl/include/crypto/des_platform.h
/* * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_DES_PLATFORM_H # define OSSL_DES_PLATFORM_H # pragma once # if defined(DES_ASM) && (defined(__sparc) || defined(__sparc__)) /* Fujitsu SPARC64 X support */ # include "crypto/sparc_arch.h" # ifndef OPENSSL_NO_DES # define SPARC_DES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_DES) # include <openssl/des.h> void des_t4_key_expand(const void *key, DES_key_schedule *ks); void des_t4_ede3_cbc_encrypt(const void *inp, void *out, size_t len, const DES_key_schedule ks[3], unsigned char iv[8]); void des_t4_ede3_cbc_decrypt(const void *inp, void *out, size_t len, const DES_key_schedule ks[3], unsigned char iv[8]); void des_t4_cbc_encrypt(const void *inp, void *out, size_t len, const DES_key_schedule *ks, unsigned char iv[8]); void des_t4_cbc_decrypt(const void *inp, void *out, size_t len, const DES_key_schedule *ks, unsigned char iv[8]); # endif /* OPENSSL_NO_DES */ # endif /* DES_ASM && sparc */ #endif /* OSSL_CRYPTO_CIPHERMODE_PLATFORM_H */
./openssl/include/crypto/ocsperr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_OCSPERR_H # define OSSL_CRYPTO_OCSPERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif # ifndef OPENSSL_NO_OCSP int ossl_err_load_OCSP_strings(void); # endif # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/bioerr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_BIOERR_H # define OSSL_CRYPTO_BIOERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_BIO_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/sm4_platform.h
/* * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_SM4_PLATFORM_H # define OSSL_SM4_PLATFORM_H # pragma once # if defined(OPENSSL_CPUID_OBJ) # if defined(__aarch64__) || defined (_M_ARM64) # include "arm_arch.h" extern unsigned int OPENSSL_arm_midr; static inline int vpsm4_capable(void) { return (OPENSSL_armcap_P & ARMV8_CPUID) && (MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_V1) || MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_N1)); } static inline int vpsm4_ex_capable(void) { return (OPENSSL_armcap_P & ARMV8_CPUID) && (MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, HISI_CPU_IMP, HISI_CPU_PART_KP920)); } # if defined(VPSM4_ASM) # define VPSM4_CAPABLE vpsm4_capable() # define VPSM4_EX_CAPABLE vpsm4_ex_capable() # endif # define HWSM4_CAPABLE (OPENSSL_armcap_P & ARMV8_SM4) # define HWSM4_set_encrypt_key sm4_v8_set_encrypt_key # define HWSM4_set_decrypt_key sm4_v8_set_decrypt_key # define HWSM4_encrypt sm4_v8_encrypt # define HWSM4_decrypt sm4_v8_decrypt # define HWSM4_cbc_encrypt sm4_v8_cbc_encrypt # define HWSM4_ecb_encrypt sm4_v8_ecb_encrypt # define HWSM4_ctr32_encrypt_blocks sm4_v8_ctr32_encrypt_blocks # elif defined(__riscv) && __riscv_xlen == 64 /* RV64 support */ # include "riscv_arch.h" /* Zvksed extension (vector crypto SM4). */ int rv64i_zvksed_sm4_set_encrypt_key(const unsigned char *userKey, SM4_KEY *key); int rv64i_zvksed_sm4_set_decrypt_key(const unsigned char *userKey, SM4_KEY *key); void rv64i_zvksed_sm4_encrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *key); void rv64i_zvksed_sm4_decrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *key); # endif /* RV64 */ # endif /* OPENSSL_CPUID_OBJ */ # if defined(HWSM4_CAPABLE) int HWSM4_set_encrypt_key(const unsigned char *userKey, SM4_KEY *key); int HWSM4_set_decrypt_key(const unsigned char *userKey, SM4_KEY *key); void HWSM4_encrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *key); void HWSM4_decrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *key); void HWSM4_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t length, const SM4_KEY *key, unsigned char *ivec, const int enc); void HWSM4_ecb_encrypt(const unsigned char *in, unsigned char *out, size_t length, const SM4_KEY *key, const int enc); void HWSM4_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, size_t len, const void *key, const unsigned char ivec[16]); # endif /* HWSM4_CAPABLE */ # ifdef VPSM4_CAPABLE int vpsm4_set_encrypt_key(const unsigned char *userKey, SM4_KEY *key); int vpsm4_set_decrypt_key(const unsigned char *userKey, SM4_KEY *key); void vpsm4_encrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *key); void vpsm4_decrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *key); void vpsm4_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t length, const SM4_KEY *key, unsigned char *ivec, const int enc); void vpsm4_ecb_encrypt(const unsigned char *in, unsigned char *out, size_t length, const SM4_KEY *key, const int enc); void vpsm4_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, size_t len, const void *key, const unsigned char ivec[16]); void vpsm4_xts_encrypt(const unsigned char *in, unsigned char *out, size_t len, const SM4_KEY *key1, const SM4_KEY *key2, const unsigned char ivec[16], const int enc); void vpsm4_xts_encrypt_gb(const unsigned char *in, unsigned char *out, size_t len, const SM4_KEY *key1, const SM4_KEY *key2, const unsigned char ivec[16], const int enc); # endif /* VPSM4_CAPABLE */ # ifdef VPSM4_EX_CAPABLE int vpsm4_ex_set_encrypt_key(const unsigned char *userKey, SM4_KEY *key); int vpsm4_ex_set_decrypt_key(const unsigned char *userKey, SM4_KEY *key); void vpsm4_ex_encrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *key); void vpsm4_ex_decrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *key); void vpsm4_ex_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t length, const SM4_KEY *key, unsigned char *ivec, const int enc); void vpsm4_ex_ecb_encrypt(const unsigned char *in, unsigned char *out, size_t length, const SM4_KEY *key, const int enc); void vpsm4_ex_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, size_t len, const void *key, const unsigned char ivec[16]); void vpsm4_ex_xts_encrypt(const unsigned char *in, unsigned char *out, size_t len, const SM4_KEY *key1, const SM4_KEY *key2, const unsigned char ivec[16], const int enc); void vpsm4_ex_xts_encrypt_gb(const unsigned char *in, unsigned char *out, size_t len, const SM4_KEY *key1, const SM4_KEY *key2, const unsigned char ivec[16], const int enc); # endif /* VPSM4_EX_CAPABLE */ #endif /* OSSL_SM4_PLATFORM_H */
./openssl/include/crypto/bn_srp.h
/* * Copyright 2014-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OPENSSL_NO_SRP extern const BIGNUM ossl_bn_group_1024; extern const BIGNUM ossl_bn_group_1536; extern const BIGNUM ossl_bn_group_2048; extern const BIGNUM ossl_bn_group_3072; extern const BIGNUM ossl_bn_group_4096; extern const BIGNUM ossl_bn_group_6144; extern const BIGNUM ossl_bn_group_8192; extern const BIGNUM ossl_bn_generator_19; extern const BIGNUM ossl_bn_generator_5; extern const BIGNUM ossl_bn_generator_2; #endif
./openssl/include/crypto/asn1.h
/* * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_ASN1_H # define OSSL_CRYPTO_ASN1_H # pragma once # include <openssl/asn1.h> # include <openssl/core_dispatch.h> /* OSSL_FUNC_keymgmt_import() */ /* Internal ASN1 structures and functions: not for application use */ /* ASN1 public key method structure */ #include <openssl/core.h> struct evp_pkey_asn1_method_st { int pkey_id; int pkey_base_id; unsigned long pkey_flags; char *pem_str; char *info; int (*pub_decode) (EVP_PKEY *pk, const X509_PUBKEY *pub); int (*pub_encode) (X509_PUBKEY *pub, const EVP_PKEY *pk); int (*pub_cmp) (const EVP_PKEY *a, const EVP_PKEY *b); int (*pub_print) (BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx); int (*priv_decode) (EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf); int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk); int (*priv_print) (BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx); int (*pkey_size) (const EVP_PKEY *pk); int (*pkey_bits) (const EVP_PKEY *pk); int (*pkey_security_bits) (const EVP_PKEY *pk); int (*param_decode) (EVP_PKEY *pkey, const unsigned char **pder, int derlen); int (*param_encode) (const EVP_PKEY *pkey, unsigned char **pder); int (*param_missing) (const EVP_PKEY *pk); int (*param_copy) (EVP_PKEY *to, const EVP_PKEY *from); int (*param_cmp) (const EVP_PKEY *a, const EVP_PKEY *b); int (*param_print) (BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx); int (*sig_print) (BIO *out, const X509_ALGOR *sigalg, const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx); void (*pkey_free) (EVP_PKEY *pkey); int (*pkey_ctrl) (EVP_PKEY *pkey, int op, long arg1, void *arg2); /* Legacy functions for old PEM */ int (*old_priv_decode) (EVP_PKEY *pkey, const unsigned char **pder, int derlen); int (*old_priv_encode) (const EVP_PKEY *pkey, unsigned char **pder); /* Custom ASN1 signature verification */ int (*item_verify) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *data, const X509_ALGOR *a, const ASN1_BIT_STRING *sig, EVP_PKEY *pkey); int (*item_sign) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *data, X509_ALGOR *alg1, X509_ALGOR *alg2, ASN1_BIT_STRING *sig); int (*siginf_set) (X509_SIG_INFO *siginf, const X509_ALGOR *alg, const ASN1_STRING *sig); /* Check */ int (*pkey_check) (const EVP_PKEY *pk); int (*pkey_public_check) (const EVP_PKEY *pk); int (*pkey_param_check) (const EVP_PKEY *pk); /* Get/set raw private/public key data */ int (*set_priv_key) (EVP_PKEY *pk, const unsigned char *priv, size_t len); int (*set_pub_key) (EVP_PKEY *pk, const unsigned char *pub, size_t len); int (*get_priv_key) (const EVP_PKEY *pk, unsigned char *priv, size_t *len); int (*get_pub_key) (const EVP_PKEY *pk, unsigned char *pub, size_t *len); /* Exports and imports to / from providers */ size_t (*dirty_cnt) (const EVP_PKEY *pk); int (*export_to) (const EVP_PKEY *pk, void *to_keydata, OSSL_FUNC_keymgmt_import_fn *importer, OSSL_LIB_CTX *libctx, const char *propq); OSSL_CALLBACK *import_from; int (*copy) (EVP_PKEY *to, EVP_PKEY *from); int (*priv_decode_ex) (EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf, OSSL_LIB_CTX *libctx, const char *propq); } /* EVP_PKEY_ASN1_METHOD */ ; DEFINE_STACK_OF_CONST(EVP_PKEY_ASN1_METHOD) extern const EVP_PKEY_ASN1_METHOD ossl_dh_asn1_meth; extern const EVP_PKEY_ASN1_METHOD ossl_dhx_asn1_meth; extern const EVP_PKEY_ASN1_METHOD ossl_dsa_asn1_meths[5]; extern const EVP_PKEY_ASN1_METHOD ossl_eckey_asn1_meth; extern const EVP_PKEY_ASN1_METHOD ossl_ecx25519_asn1_meth; extern const EVP_PKEY_ASN1_METHOD ossl_ecx448_asn1_meth; extern const EVP_PKEY_ASN1_METHOD ossl_ed25519_asn1_meth; extern const EVP_PKEY_ASN1_METHOD ossl_ed448_asn1_meth; extern const EVP_PKEY_ASN1_METHOD ossl_sm2_asn1_meth; extern const EVP_PKEY_ASN1_METHOD ossl_rsa_asn1_meths[2]; extern const EVP_PKEY_ASN1_METHOD ossl_rsa_pss_asn1_meth; /* * These are used internally in the ASN1_OBJECT to keep track of whether the * names and data need to be free()ed */ # define ASN1_OBJECT_FLAG_DYNAMIC 0x01/* internal use */ # define ASN1_OBJECT_FLAG_CRITICAL 0x02/* critical x509v3 object id */ # define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04/* internal use */ # define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08/* internal use */ struct asn1_object_st { const char *sn, *ln; int nid; int length; const unsigned char *data; /* data remains const after init */ int flags; /* Should we free this one */ }; /* ASN1 print context structure */ struct asn1_pctx_st { unsigned long flags; unsigned long nm_flags; unsigned long cert_flags; unsigned long oid_flags; unsigned long str_flags; } /* ASN1_PCTX */ ; /* ASN1 type functions */ int ossl_asn1_type_set_octetstring_int(ASN1_TYPE *a, long num, unsigned char *data, int len); int ossl_asn1_type_get_octetstring_int(const ASN1_TYPE *a, long *num, unsigned char *data, int max_len); int ossl_x509_algor_new_from_md(X509_ALGOR **palg, const EVP_MD *md); const EVP_MD *ossl_x509_algor_get_md(X509_ALGOR *alg); X509_ALGOR *ossl_x509_algor_mgf1_decode(X509_ALGOR *alg); int ossl_x509_algor_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md); int ossl_asn1_time_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags); EVP_PKEY *ossl_d2i_PrivateKey_legacy(int keytype, EVP_PKEY **a, const unsigned char **pp, long length, OSSL_LIB_CTX *libctx, const char *propq); X509_ALGOR *ossl_X509_ALGOR_from_nid(int nid, int ptype, void *pval); time_t ossl_asn1_string_to_time_t(const char *asn1_string); void ossl_asn1_string_set_bits_left(ASN1_STRING *str, unsigned int num); #endif /* ndef OSSL_CRYPTO_ASN1_H */
./openssl/include/crypto/encoder.h
/* * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_ENCODER_H # define OSSL_CRYPTO_ENCODER_H # pragma once # include <openssl/types.h> int ossl_encoder_get_number(const OSSL_ENCODER *encoder); int ossl_encoder_store_cache_flush(OSSL_LIB_CTX *libctx); int ossl_encoder_store_remove_all_provided(const OSSL_PROVIDER *prov); #endif
./openssl/include/crypto/cterr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_CTERR_H # define OSSL_CRYPTO_CTERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif # ifndef OPENSSL_NO_CT int ossl_err_load_CT_strings(void); # endif # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/ppc_arch.h
/* * Copyright 2014-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_PPC_ARCH_H # define OSSL_CRYPTO_PPC_ARCH_H extern unsigned int OPENSSL_ppccap_P; /* * Flags' usage can appear ambiguous, because they are set rather * to reflect OpenSSL performance preferences than actual processor * capabilities. */ # define PPC_FPU64 (1<<0) # define PPC_ALTIVEC (1<<1) # define PPC_CRYPTO207 (1<<2) # define PPC_FPU (1<<3) # define PPC_MADD300 (1<<4) # define PPC_MFTB (1<<5) # define PPC_MFSPR268 (1<<6) # define PPC_BRD31 (1<<7) #endif
./openssl/include/crypto/pemerr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_PEMERR_H # define OSSL_CRYPTO_PEMERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_PEM_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/ecx.h
/* * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Internal EC functions for other submodules: not for application use */ #ifndef OSSL_CRYPTO_ECX_H # define OSSL_CRYPTO_ECX_H # pragma once # include <openssl/opensslconf.h> # ifndef OPENSSL_NO_ECX # include <openssl/core.h> # include <openssl/e_os2.h> # include <openssl/crypto.h> # include "internal/refcount.h" # include "crypto/types.h" # define X25519_KEYLEN 32 # define X448_KEYLEN 56 # define ED25519_KEYLEN 32 # define ED448_KEYLEN 57 # define MAX_KEYLEN ED448_KEYLEN # define X25519_BITS 253 # define X25519_SECURITY_BITS 128 # define X448_BITS 448 # define X448_SECURITY_BITS 224 # define ED25519_BITS 256 /* RFC8032 Section 8.5 */ # define ED25519_SECURITY_BITS 128 # define ED25519_SIGSIZE 64 # define ED448_BITS 456 /* RFC8032 Section 8.5 */ # define ED448_SECURITY_BITS 224 # define ED448_SIGSIZE 114 typedef enum { ECX_KEY_TYPE_X25519, ECX_KEY_TYPE_X448, ECX_KEY_TYPE_ED25519, ECX_KEY_TYPE_ED448 } ECX_KEY_TYPE; #define KEYTYPE2NID(type) \ ((type) == ECX_KEY_TYPE_X25519 \ ? EVP_PKEY_X25519 \ : ((type) == ECX_KEY_TYPE_X448 \ ? EVP_PKEY_X448 \ : ((type) == ECX_KEY_TYPE_ED25519 \ ? EVP_PKEY_ED25519 \ : EVP_PKEY_ED448))) struct ecx_key_st { OSSL_LIB_CTX *libctx; char *propq; unsigned int haspubkey:1; unsigned char pubkey[MAX_KEYLEN]; unsigned char *privkey; size_t keylen; ECX_KEY_TYPE type; CRYPTO_REF_COUNT references; }; size_t ossl_ecx_key_length(ECX_KEY_TYPE type); ECX_KEY *ossl_ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type, int haspubkey, const char *propq); void ossl_ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx); unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key); void ossl_ecx_key_free(ECX_KEY *key); int ossl_ecx_key_up_ref(ECX_KEY *key); ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection); int ossl_ecx_compute_key(ECX_KEY *peer, ECX_KEY *priv, size_t keylen, unsigned char *secret, size_t *secretlen, size_t outlen); int ossl_x25519(uint8_t out_shared_key[32], const uint8_t private_key[32], const uint8_t peer_public_value[32]); void ossl_x25519_public_from_private(uint8_t out_public_value[32], const uint8_t private_key[32]); int ossl_ed25519_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[32], const uint8_t private_key[32], const char *propq); int ossl_ed25519_sign(uint8_t *out_sig, const uint8_t *tbs, size_t tbs_len, const uint8_t public_key[32], const uint8_t private_key[32], const uint8_t dom2flag, const uint8_t phflag, const uint8_t csflag, const uint8_t *context, size_t context_len, OSSL_LIB_CTX *libctx, const char *propq); int ossl_ed25519_verify(const uint8_t *tbs, size_t tbs_len, const uint8_t signature[64], const uint8_t public_key[32], const uint8_t dom2flag, const uint8_t phflag, const uint8_t csflag, const uint8_t *context, size_t context_len, OSSL_LIB_CTX *libctx, const char *propq); int ossl_ed448_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[57], const uint8_t private_key[57], const char *propq); int ossl_ed448_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig, const uint8_t *message, size_t message_len, const uint8_t public_key[57], const uint8_t private_key[57], const uint8_t *context, size_t context_len, const uint8_t phflag, const char *propq); int ossl_ed448_verify(OSSL_LIB_CTX *ctx, const uint8_t *message, size_t message_len, const uint8_t signature[114], const uint8_t public_key[57], const uint8_t *context, size_t context_len, const uint8_t phflag, const char *propq); int ossl_x448(uint8_t out_shared_key[56], const uint8_t private_key[56], const uint8_t peer_public_value[56]); void ossl_x448_public_from_private(uint8_t out_public_value[56], const uint8_t private_key[56]); /* Backend support */ typedef enum { KEY_OP_PUBLIC, KEY_OP_PRIVATE, KEY_OP_KEYGEN } ecx_key_op_t; ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg, const unsigned char *p, int plen, int pkey_id, ecx_key_op_t op, OSSL_LIB_CTX *libctx, const char *propq); int ossl_ecx_public_from_private(ECX_KEY *key); int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[], int include_private); ECX_KEY *ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf, OSSL_LIB_CTX *libctx, const char *propq); ECX_KEY *ossl_evp_pkey_get1_X25519(EVP_PKEY *pkey); ECX_KEY *ossl_evp_pkey_get1_X448(EVP_PKEY *pkey); ECX_KEY *ossl_evp_pkey_get1_ED25519(EVP_PKEY *pkey); ECX_KEY *ossl_evp_pkey_get1_ED448(EVP_PKEY *pkey); # endif /* OPENSSL_NO_ECX */ #endif
./openssl/include/crypto/bn.h
/* * Copyright 2014-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_BN_H # define OSSL_CRYPTO_BN_H # pragma once # include <openssl/bn.h> # include <limits.h> BIGNUM *bn_wexpand(BIGNUM *a, int words); BIGNUM *bn_expand2(BIGNUM *a, int words); void bn_correct_top(BIGNUM *a); /* * Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'. * This is an array r[] of values that are either zero or odd with an * absolute value less than 2^w satisfying scalar = \sum_j r[j]*2^j where at * most one of any w+1 consecutive digits is non-zero with the exception that * the most significant digit may be only w-1 zeros away from that next * non-zero digit. */ signed char *bn_compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len); int bn_get_top(const BIGNUM *a); int bn_get_dmax(const BIGNUM *a); /* Set all words to zero */ void bn_set_all_zero(BIGNUM *a); /* * Copy the internal BIGNUM words into out which holds size elements (and size * must be bigger than top) */ int bn_copy_words(BN_ULONG *out, const BIGNUM *in, int size); BN_ULONG *bn_get_words(const BIGNUM *a); /* * Set the internal data words in a to point to words which contains size * elements. The BN_FLG_STATIC_DATA flag is set */ void bn_set_static_words(BIGNUM *a, const BN_ULONG *words, int size); /* * Copy words into the BIGNUM |a|, reallocating space as necessary. * The negative flag of |a| is not modified. * Returns 1 on success and 0 on failure. */ /* * |num_words| is int because bn_expand2 takes an int. This is an internal * function so we simply trust callers not to pass negative values. */ int bn_set_words(BIGNUM *a, const BN_ULONG *words, int num_words); /* * Some BIGNUM functions assume most significant limb to be non-zero, which * is customarily arranged by bn_correct_top. Output from below functions * is not processed with bn_correct_top, and for this reason it may not be * returned out of public API. It may only be passed internally into other * functions known to support non-minimal or zero-padded BIGNUMs. Even * though the goal is to facilitate constant-time-ness, not each subroutine * is constant-time by itself. They all have pre-conditions, consult source * code... */ int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_MONT_CTX *mont, BN_CTX *ctx); int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont, BN_CTX *ctx); int bn_from_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont, BN_CTX *ctx); int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m); int bn_mod_sub_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m); int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx); int bn_lshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n); int bn_rshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n); int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx); #define BN_PRIMETEST_COMPOSITE 0 #define BN_PRIMETEST_COMPOSITE_WITH_FACTOR 1 #define BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME 2 #define BN_PRIMETEST_PROBABLY_PRIME 3 int ossl_bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx, BN_GENCB *cb, int enhanced, int *status); int ossl_bn_check_generated_prime(const BIGNUM *w, int checks, BN_CTX *ctx, BN_GENCB *cb); const BIGNUM *ossl_bn_get0_small_factors(void); int ossl_bn_rsa_fips186_4_gen_prob_primes(BIGNUM *p, BIGNUM *Xpout, BIGNUM *p1, BIGNUM *p2, const BIGNUM *Xp, const BIGNUM *Xp1, const BIGNUM *Xp2, int nlen, const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb); int ossl_bn_rsa_fips186_4_derive_prime(BIGNUM *Y, BIGNUM *X, const BIGNUM *Xin, const BIGNUM *r1, const BIGNUM *r2, int nlen, const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb); OSSL_LIB_CTX *ossl_bn_get_libctx(BN_CTX *ctx); extern const BIGNUM ossl_bn_inv_sqrt_2; #if defined(OPENSSL_SYS_LINUX) && !defined(FIPS_MODULE) && defined (__s390x__) # define S390X_MOD_EXP #endif int s390x_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); int s390x_crt(BIGNUM *r, const BIGNUM *i, const BIGNUM *p, const BIGNUM *q, const BIGNUM *dmp, const BIGNUM *dmq, const BIGNUM *iqmp); #endif
./openssl/include/crypto/dsa.h
/* * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_DSA_H # define OSSL_CRYPTO_DSA_H # pragma once # include <openssl/core.h> # include <openssl/dsa.h> # include "internal/ffc.h" /* * DSA Paramgen types * Note, adding to this list requires adjustments to various checks * in dsa_gen range validation checks */ #define DSA_PARAMGEN_TYPE_FIPS_186_4 0 /* Use FIPS186-4 standard */ #define DSA_PARAMGEN_TYPE_FIPS_186_2 1 /* Use legacy FIPS186-2 standard */ #define DSA_PARAMGEN_TYPE_FIPS_DEFAULT 2 DSA *ossl_dsa_new(OSSL_LIB_CTX *libctx); void ossl_dsa_set0_libctx(DSA *d, OSSL_LIB_CTX *libctx); int ossl_dsa_generate_ffc_parameters(DSA *dsa, int type, int pbits, int qbits, BN_GENCB *cb); int ossl_dsa_sign_int(int type, const unsigned char *dgst, int dlen, unsigned char *sig, unsigned int *siglen, DSA *dsa, unsigned int nonce_type, const char *digestname, OSSL_LIB_CTX *libctx, const char *propq); FFC_PARAMS *ossl_dsa_get0_params(DSA *dsa); int ossl_dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[]); int ossl_dsa_key_fromdata(DSA *dsa, const OSSL_PARAM params[], int include_private); DSA *ossl_dsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf, OSSL_LIB_CTX *libctx, const char *propq); int ossl_dsa_generate_public_key(BN_CTX *ctx, const DSA *dsa, const BIGNUM *priv_key, BIGNUM *pub_key); int ossl_dsa_check_params(const DSA *dsa, int checktype, int *ret); int ossl_dsa_check_pub_key(const DSA *dsa, const BIGNUM *pub_key, int *ret); int ossl_dsa_check_pub_key_partial(const DSA *dsa, const BIGNUM *pub_key, int *ret); int ossl_dsa_check_priv_key(const DSA *dsa, const BIGNUM *priv_key, int *ret); int ossl_dsa_check_pairwise(const DSA *dsa); int ossl_dsa_is_foreign(const DSA *dsa); DSA *ossl_dsa_dup(const DSA *dsa, int selection); #endif
./openssl/include/crypto/md32_common.h
/* * Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /*- * This is a generic 32 bit "collector" for message digest algorithms. * Whenever needed it collects input character stream into chunks of * 32 bit values and invokes a block function that performs actual hash * calculations. * * Porting guide. * * Obligatory macros: * * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN * this macro defines byte order of input stream. * HASH_CBLOCK * size of a unit chunk HASH_BLOCK operates on. * HASH_LONG * has to be at least 32 bit wide. * HASH_CTX * context structure that at least contains following * members: * typedef struct { * ... * HASH_LONG Nl,Nh; * either { * HASH_LONG data[HASH_LBLOCK]; * unsigned char data[HASH_CBLOCK]; * }; * unsigned int num; * ... * } HASH_CTX; * data[] vector is expected to be zeroed upon first call to * HASH_UPDATE. * HASH_UPDATE * name of "Update" function, implemented here. * HASH_TRANSFORM * name of "Transform" function, implemented here. * HASH_FINAL * name of "Final" function, implemented here. * HASH_BLOCK_DATA_ORDER * name of "block" function capable of treating *unaligned* input * message in original (data) byte order, implemented externally. * HASH_MAKE_STRING * macro converting context variables to an ASCII hash string. * * MD5 example: * * #define DATA_ORDER_IS_LITTLE_ENDIAN * * #define HASH_LONG MD5_LONG * #define HASH_CTX MD5_CTX * #define HASH_CBLOCK MD5_CBLOCK * #define HASH_UPDATE MD5_Update * #define HASH_TRANSFORM MD5_Transform * #define HASH_FINAL MD5_Final * #define HASH_BLOCK_DATA_ORDER md5_block_data_order */ #ifndef OSSL_CRYPTO_MD32_COMMON_H # define OSSL_CRYPTO_MD32_COMMON_H # pragma once # include <openssl/crypto.h> # if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN) # error "DATA_ORDER must be defined!" # endif # ifndef HASH_CBLOCK # error "HASH_CBLOCK must be defined!" # endif # ifndef HASH_LONG # error "HASH_LONG must be defined!" # endif # ifndef HASH_CTX # error "HASH_CTX must be defined!" # endif # ifndef HASH_UPDATE # error "HASH_UPDATE must be defined!" # endif # ifndef HASH_TRANSFORM # error "HASH_TRANSFORM must be defined!" # endif # ifndef HASH_FINAL # error "HASH_FINAL must be defined!" # endif # ifndef HASH_BLOCK_DATA_ORDER # error "HASH_BLOCK_DATA_ORDER must be defined!" # endif # define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n)))) #ifndef PEDANTIC # if defined(__GNUC__) && __GNUC__>=2 && \ !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) # if defined(__riscv_zbb) || defined(__riscv_zbkb) # if __riscv_xlen == 64 # undef ROTATE # define ROTATE(x, n) ({ MD32_REG_T ret; \ asm ("roriw %0, %1, %2" \ : "=r"(ret) \ : "r"(x), "i"(32 - (n))); ret;}) # endif # if __riscv_xlen == 32 # undef ROTATE # define ROTATE(x, n) ({ MD32_REG_T ret; \ asm ("rori %0, %1, %2" \ : "=r"(ret) \ : "r"(x), "i"(32 - (n))); ret;}) # endif # endif # endif #endif # if defined(DATA_ORDER_IS_BIG_ENDIAN) # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \ l|=(((unsigned long)(*((c)++)))<<16), \ l|=(((unsigned long)(*((c)++)))<< 8), \ l|=(((unsigned long)(*((c)++))) ) ) # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ *((c)++)=(unsigned char)(((l)>>16)&0xff), \ *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ *((c)++)=(unsigned char)(((l) )&0xff), \ l) # elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \ l|=(((unsigned long)(*((c)++)))<< 8), \ l|=(((unsigned long)(*((c)++)))<<16), \ l|=(((unsigned long)(*((c)++)))<<24) ) # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ *((c)++)=(unsigned char)(((l)>>16)&0xff), \ *((c)++)=(unsigned char)(((l)>>24)&0xff), \ l) # endif /* * Time for some action :-) */ int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) { const unsigned char *data = data_; unsigned char *p; HASH_LONG l; size_t n; if (len == 0) return 1; l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL; if (l < c->Nl) /* overflow */ c->Nh++; c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on * 16-bit */ c->Nl = l; n = c->num; if (n != 0) { p = (unsigned char *)c->data; if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) { memcpy(p + n, data, HASH_CBLOCK - n); HASH_BLOCK_DATA_ORDER(c, p, 1); n = HASH_CBLOCK - n; data += n; len -= n; c->num = 0; /* * We use memset rather than OPENSSL_cleanse() here deliberately. * Using OPENSSL_cleanse() here could be a performance issue. It * will get properly cleansed on finalisation so this isn't a * security problem. */ memset(p, 0, HASH_CBLOCK); /* keep it zeroed */ } else { memcpy(p + n, data, len); c->num += (unsigned int)len; return 1; } } n = len / HASH_CBLOCK; if (n > 0) { HASH_BLOCK_DATA_ORDER(c, data, n); n *= HASH_CBLOCK; data += n; len -= n; } if (len != 0) { p = (unsigned char *)c->data; c->num = (unsigned int)len; memcpy(p, data, len); } return 1; } void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data) { HASH_BLOCK_DATA_ORDER(c, data, 1); } int HASH_FINAL(unsigned char *md, HASH_CTX *c) { unsigned char *p = (unsigned char *)c->data; size_t n = c->num; p[n] = 0x80; /* there is always room for one */ n++; if (n > (HASH_CBLOCK - 8)) { memset(p + n, 0, HASH_CBLOCK - n); n = 0; HASH_BLOCK_DATA_ORDER(c, p, 1); } memset(p + n, 0, HASH_CBLOCK - 8 - n); p += HASH_CBLOCK - 8; # if defined(DATA_ORDER_IS_BIG_ENDIAN) (void)HOST_l2c(c->Nh, p); (void)HOST_l2c(c->Nl, p); # elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) (void)HOST_l2c(c->Nl, p); (void)HOST_l2c(c->Nh, p); # endif p -= HASH_CBLOCK; HASH_BLOCK_DATA_ORDER(c, p, 1); c->num = 0; OPENSSL_cleanse(p, HASH_CBLOCK); # ifndef HASH_MAKE_STRING # error "HASH_MAKE_STRING must be defined!" # else HASH_MAKE_STRING(c, md); # endif return 1; } # ifndef MD32_REG_T # if defined(__alpha) || defined(__sparcv9) || defined(__mips) # define MD32_REG_T long /* * This comment was originally written for MD5, which is why it * discusses A-D. But it basically applies to all 32-bit digests, * which is why it was moved to common header file. * * In case you wonder why A-D are declared as long and not * as MD5_LONG. Doing so results in slight performance * boost on LP64 architectures. The catch is we don't * really care if 32 MSBs of a 64-bit register get polluted * with eventual overflows as we *save* only 32 LSBs in * *either* case. Now declaring 'em long excuses the compiler * from keeping 32 MSBs zeroed resulting in 13% performance * improvement under SPARC Solaris7/64 and 5% under AlphaLinux. * Well, to be honest it should say that this *prevents* * performance degradation. */ # else /* * Above is not absolute and there are LP64 compilers that * generate better code if MD32_REG_T is defined int. The above * pre-processor condition reflects the circumstances under which * the conclusion was made and is subject to further extension. */ # define MD32_REG_T int # endif # endif #endif
./openssl/include/crypto/aria.h
/* * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved. * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Copyright (c) 2017 National Security Research Institute. All rights reserved. */ #ifndef OSSL_CRYPTO_ARIA_H # define OSSL_CRYPTO_ARIA_H # pragma once # include <openssl/opensslconf.h> # ifdef OPENSSL_NO_ARIA # error ARIA is disabled. # endif # define ARIA_ENCRYPT 1 # define ARIA_DECRYPT 0 # define ARIA_BLOCK_SIZE 16 /* Size of each encryption/decryption block */ # define ARIA_MAX_KEYS 17 /* Number of keys needed in the worst case */ typedef union { unsigned char c[ARIA_BLOCK_SIZE]; unsigned int u[ARIA_BLOCK_SIZE / sizeof(unsigned int)]; } ARIA_u128; typedef unsigned char ARIA_c128[ARIA_BLOCK_SIZE]; struct aria_key_st { ARIA_u128 rd_key[ARIA_MAX_KEYS]; unsigned int rounds; }; typedef struct aria_key_st ARIA_KEY; int ossl_aria_set_encrypt_key(const unsigned char *userKey, const int bits, ARIA_KEY *key); int ossl_aria_set_decrypt_key(const unsigned char *userKey, const int bits, ARIA_KEY *key); void ossl_aria_encrypt(const unsigned char *in, unsigned char *out, const ARIA_KEY *key); #endif
./openssl/include/crypto/store.h
/* * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_STORE_H # define OSSL_CRYPTO_STORE_H # pragma once # include <openssl/bio.h> # include <openssl/store.h> # include <openssl/ui.h> void ossl_store_cleanup_int(void); int ossl_store_loader_get_number(const OSSL_STORE_LOADER *loader); int ossl_store_loader_store_cache_flush(OSSL_LIB_CTX *libctx); int ossl_store_loader_store_remove_all_provided(const OSSL_PROVIDER *prov); #endif
./openssl/include/crypto/cryptoerr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_CRYPTOERR_H # define OSSL_CRYPTO_CRYPTOERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_CRYPTO_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/engine.h
/* * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/engine.h> void engine_load_openssl_int(void); void engine_load_devcrypto_int(void); void engine_load_rdrand_int(void); void engine_load_dynamic_int(void); void engine_load_padlock_int(void); void engine_load_capi_int(void); void engine_load_dasync_int(void); void engine_load_afalg_int(void); void engine_cleanup_int(void);
./openssl/include/crypto/rsaerr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_RSAERR_H # define OSSL_CRYPTO_RSAERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif int ossl_err_load_RSA_strings(void); # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/objects.h
/* * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/objects.h> void ossl_obj_cleanup_int(void);
./openssl/include/crypto/engineerr.h
/* * Generated by util/mkerr.pl DO NOT EDIT * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_ENGINEERR_H # define OSSL_CRYPTO_ENGINEERR_H # pragma once # include <openssl/opensslconf.h> # include <openssl/symhacks.h> # ifdef __cplusplus extern "C" { # endif # ifndef OPENSSL_NO_ENGINE int ossl_err_load_ENGINE_strings(void); # endif # ifdef __cplusplus } # endif #endif
./openssl/include/crypto/sm2.h
/* * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved. * Copyright 2017 Ribose Inc. All Rights Reserved. * Ported from Ribose contributions from Botan. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_CRYPTO_SM2_H # define OSSL_CRYPTO_SM2_H # pragma once # include <openssl/opensslconf.h> # if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODULE) # include <openssl/ec.h> # include "crypto/types.h" int ossl_sm2_key_private_check(const EC_KEY *eckey); /* The default user id as specified in GM/T 0009-2012 */ # define SM2_DEFAULT_USERID "1234567812345678" int ossl_sm2_compute_z_digest(uint8_t *out, const EVP_MD *digest, const uint8_t *id, const size_t id_len, const EC_KEY *key); /* * SM2 signature operation. Computes Z and then signs H(Z || msg) using SM2 */ ECDSA_SIG *ossl_sm2_do_sign(const EC_KEY *key, const EVP_MD *digest, const uint8_t *id, const size_t id_len, const uint8_t *msg, size_t msg_len); int ossl_sm2_do_verify(const EC_KEY *key, const EVP_MD *digest, const ECDSA_SIG *signature, const uint8_t *id, const size_t id_len, const uint8_t *msg, size_t msg_len); /* * SM2 signature generation. */ int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen, unsigned char *sig, unsigned int *siglen, EC_KEY *eckey); /* * SM2 signature verification. */ int ossl_sm2_internal_verify(const unsigned char *dgst, int dgstlen, const unsigned char *sig, int siglen, EC_KEY *eckey); /* * SM2 encryption */ int ossl_sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len, size_t *ct_size); int ossl_sm2_plaintext_size(const unsigned char *ct, size_t ct_size, size_t *pt_size); int ossl_sm2_encrypt(const EC_KEY *key, const EVP_MD *digest, const uint8_t *msg, size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len); int ossl_sm2_decrypt(const EC_KEY *key, const EVP_MD *digest, const uint8_t *ciphertext, size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len); const unsigned char *ossl_sm2_algorithmidentifier_encoding(int md_nid, size_t *len); # endif /* OPENSSL_NO_SM2 */ #endif
./openssl/demos/bio/server-conf.c
/* * Copyright 2013-2017 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * A minimal program to serve an SSL connection. It uses blocking. It uses * the SSL_CONF API with a configuration file. cc -I../../include saccept.c * -L../.. -lssl -lcrypto -ldl */ #include <stdio.h> #include <string.h> #include <signal.h> #include <stdlib.h> #include <openssl/err.h> #include <openssl/ssl.h> #include <openssl/conf.h> int main(int argc, char *argv[]) { char *port = "*:4433"; BIO *in = NULL; BIO *ssl_bio, *tmp; SSL_CTX *ctx; SSL_CONF_CTX *cctx = NULL; CONF *conf = NULL; STACK_OF(CONF_VALUE) *sect = NULL; CONF_VALUE *cnf; long errline = -1; char buf[512]; int ret = EXIT_FAILURE, i; ctx = SSL_CTX_new(TLS_server_method()); conf = NCONF_new(NULL); if (NCONF_load(conf, "accept.cnf", &errline) <= 0) { if (errline <= 0) fprintf(stderr, "Error processing config file\n"); else fprintf(stderr, "Error on line %ld\n", errline); goto err; } sect = NCONF_get_section(conf, "default"); if (sect == NULL) { fprintf(stderr, "Error retrieving default section\n"); goto err; } cctx = SSL_CONF_CTX_new(); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE); SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); for (i = 0; i < sk_CONF_VALUE_num(sect); i++) { int rv; cnf = sk_CONF_VALUE_value(sect, i); rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value); if (rv > 0) continue; if (rv != -2) { fprintf(stderr, "Error processing %s = %s\n", cnf->name, cnf->value); ERR_print_errors_fp(stderr); goto err; } if (strcmp(cnf->name, "Port") == 0) { port = cnf->value; } else { fprintf(stderr, "Unknown configuration option %s\n", cnf->name); goto err; } } if (!SSL_CONF_CTX_finish(cctx)) { fprintf(stderr, "Finish error\n"); ERR_print_errors_fp(stderr); goto err; } /* Setup server side SSL bio */ ssl_bio = BIO_new_ssl(ctx, 0); if ((in = BIO_new_accept(port)) == NULL) goto err; /* * This means that when a new connection is accepted on 'in', The ssl_bio * will be 'duplicated' and have the new socket BIO push into it. * Basically it means the SSL BIO will be automatically setup */ BIO_set_accept_bios(in, ssl_bio); again: /* * The first call will setup the accept socket, and the second will get a * socket. In this loop, the first actual accept will occur in the * BIO_read() function. */ if (BIO_do_accept(in) <= 0) goto err; for (;;) { i = BIO_read(in, buf, 512); if (i == 0) { /* * If we have finished, remove the underlying BIO stack so the * next time we call any function for this BIO, it will attempt * to do an accept */ printf("Done\n"); tmp = BIO_pop(in); BIO_free_all(tmp); goto again; } if (i < 0) { if (BIO_should_retry(in)) continue; goto err; } fwrite(buf, 1, i, stdout); fflush(stdout); } ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) ERR_print_errors_fp(stderr); BIO_free(in); return ret; }
./openssl/demos/bio/sconnect.c
/* * Copyright 1998-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /*- * A minimal program to do SSL to a passed host and port. * It is actually using non-blocking IO but in a very simple manner * sconnect host:port - it does a 'GET / HTTP/1.0' * * cc -I../../include sconnect.c -L../.. -lssl -lcrypto */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <openssl/err.h> #include <openssl/ssl.h> #define HOSTPORT "localhost:4433" #define CAFILE "root.pem" int main(int argc, char *argv[]) { const char *hostport = HOSTPORT; const char *CAfile = CAFILE; const char *hostname; BIO *out = NULL; char buf[1024 * 10], *p; SSL_CTX *ssl_ctx = NULL; SSL *ssl; BIO *ssl_bio; int i, len, off, ret = EXIT_FAILURE; if (argc > 1) hostport = argv[1]; if (argc > 2) CAfile = argv[2]; #ifdef WATT32 dbug_init(); sock_init(); #endif ssl_ctx = SSL_CTX_new(TLS_client_method()); /* Enable trust chain verification */ SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL); SSL_CTX_load_verify_locations(ssl_ctx, CAfile, NULL); /* Lets make a SSL structure */ ssl = SSL_new(ssl_ctx); SSL_set_connect_state(ssl); /* Use it inside an SSL BIO */ ssl_bio = BIO_new(BIO_f_ssl()); BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE); /* Lets use a connect BIO under the SSL BIO */ out = BIO_new(BIO_s_connect()); BIO_set_conn_hostname(out, hostport); /* The BIO has parsed the host:port and even IPv6 literals in [] */ hostname = BIO_get_conn_hostname(out); if (!hostname || SSL_set1_host(ssl, hostname) <= 0) goto err; BIO_set_nbio(out, 1); out = BIO_push(ssl_bio, out); p = "GET / HTTP/1.0\r\n\r\n"; len = strlen(p); off = 0; for (;;) { i = BIO_write(out, &(p[off]), len); if (i <= 0) { if (BIO_should_retry(out)) { fprintf(stderr, "write DELAY\n"); sleep(1); continue; } else { goto err; } } off += i; len -= i; if (len <= 0) break; } for (;;) { i = BIO_read(out, buf, sizeof(buf)); if (i == 0) break; if (i < 0) { if (BIO_should_retry(out)) { fprintf(stderr, "read DELAY\n"); sleep(1); continue; } goto err; } fwrite(buf, 1, i, stdout); } ret = EXIT_SUCCESS; goto done; err: if (ERR_peek_error() == 0) { /* system call error */ fprintf(stderr, "errno=%d ", errno); perror("error"); } else { ERR_print_errors_fp(stderr); } done: BIO_free_all(out); SSL_CTX_free(ssl_ctx); return ret; }
./openssl/demos/bio/client-arg.c
/* * Copyright 2013-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/err.h> #include <openssl/ssl.h> int main(int argc, char **argv) { BIO *sbio = NULL, *out = NULL; int len; char tmpbuf[1024]; SSL_CTX *ctx; SSL_CONF_CTX *cctx; SSL *ssl; char **args = argv + 1; const char *connect_str = "localhost:4433"; int nargs = argc - 1; int ret = EXIT_FAILURE; ctx = SSL_CTX_new(TLS_client_method()); cctx = SSL_CONF_CTX_new(); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT); SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); while (*args && **args == '-') { int rv; /* Parse standard arguments */ rv = SSL_CONF_cmd_argv(cctx, &nargs, &args); if (rv == -3) { fprintf(stderr, "Missing argument for %s\n", *args); goto end; } if (rv < 0) { fprintf(stderr, "Error in command %s\n", *args); ERR_print_errors_fp(stderr); goto end; } /* If rv > 0 we processed something so proceed to next arg */ if (rv > 0) continue; /* Otherwise application specific argument processing */ if (strcmp(*args, "-connect") == 0) { connect_str = args[1]; if (connect_str == NULL) { fprintf(stderr, "Missing -connect argument\n"); goto end; } args += 2; nargs -= 2; continue; } else { fprintf(stderr, "Unknown argument %s\n", *args); goto end; } } if (!SSL_CONF_CTX_finish(cctx)) { fprintf(stderr, "Finish error\n"); ERR_print_errors_fp(stderr); goto end; } /* * We'd normally set some stuff like the verify paths and * mode here * because as things stand this will connect to * any server whose * certificate is signed by any CA. */ sbio = BIO_new_ssl_connect(ctx); BIO_get_ssl(sbio, &ssl); if (!ssl) { fprintf(stderr, "Can't locate SSL pointer\n"); goto end; } /* We might want to do other things with ssl here */ BIO_set_conn_hostname(sbio, connect_str); out = BIO_new_fp(stdout, BIO_NOCLOSE); if (BIO_do_connect(sbio) <= 0) { fprintf(stderr, "Error connecting to server\n"); ERR_print_errors_fp(stderr); goto end; } /* Could examine ssl here to get connection info */ BIO_puts(sbio, "GET / HTTP/1.0\n\n"); for (;;) { len = BIO_read(sbio, tmpbuf, 1024); if (len <= 0) break; BIO_write(out, tmpbuf, len); } ret = EXIT_SUCCESS; end: SSL_CONF_CTX_free(cctx); BIO_free_all(sbio); BIO_free(out); return ret; }
./openssl/demos/bio/saccept.c
/* * Copyright 1998-2017 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /*- * A minimal program to serve an SSL connection. * It uses blocking. * saccept host:port * host is the interface IP to use. If any interface, use *:port * The default it *:4433 * * cc -I../../include saccept.c -L../.. -lssl -lcrypto -ldl */ #include <stdio.h> #include <signal.h> #include <stdlib.h> #include <openssl/err.h> #include <openssl/ssl.h> #define CERT_FILE "server.pem" static volatile int done = 0; void interrupt(int sig) { done = 1; } void sigsetup(void) { struct sigaction sa; /* * Catch at most once, and don't restart the accept system call. */ sa.sa_flags = SA_RESETHAND; sa.sa_handler = interrupt; sigemptyset(&sa.sa_mask); sigaction(SIGINT, &sa, NULL); } int main(int argc, char *argv[]) { char *port = NULL; BIO *in = NULL; BIO *ssl_bio, *tmp; SSL_CTX *ctx; char buf[512]; int ret = EXIT_FAILURE, i; if (argc <= 1) port = "*:4433"; else port = argv[1]; ctx = SSL_CTX_new(TLS_server_method()); if (!SSL_CTX_use_certificate_chain_file(ctx, CERT_FILE)) goto err; if (!SSL_CTX_use_PrivateKey_file(ctx, CERT_FILE, SSL_FILETYPE_PEM)) goto err; if (!SSL_CTX_check_private_key(ctx)) goto err; /* Setup server side SSL bio */ ssl_bio = BIO_new_ssl(ctx, 0); if ((in = BIO_new_accept(port)) == NULL) goto err; /* * This means that when a new connection is accepted on 'in', The ssl_bio * will be 'duplicated' and have the new socket BIO push into it. * Basically it means the SSL BIO will be automatically setup */ BIO_set_accept_bios(in, ssl_bio); /* Arrange to leave server loop on interrupt */ sigsetup(); again: /* * The first call will setup the accept socket, and the second will get a * socket. In this loop, the first actual accept will occur in the * BIO_read() function. */ if (BIO_do_accept(in) <= 0) goto err; while (!done) { i = BIO_read(in, buf, 512); if (i == 0) { /* * If we have finished, remove the underlying BIO stack so the * next time we call any function for this BIO, it will attempt * to do an accept */ printf("Done\n"); tmp = BIO_pop(in); BIO_free_all(tmp); goto again; } if (i < 0) goto err; fwrite(buf, 1, i, stdout); fflush(stdout); } ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) ERR_print_errors_fp(stderr); BIO_free(in); return ret; }
./openssl/demos/bio/client-conf.c
/* * Copyright 2013-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/err.h> #include <openssl/ssl.h> #include <openssl/conf.h> int main(int argc, char **argv) { BIO *sbio = NULL, *out = NULL; int i, len, rv; char tmpbuf[1024]; SSL_CTX *ctx = NULL; SSL_CONF_CTX *cctx = NULL; SSL *ssl = NULL; CONF *conf = NULL; STACK_OF(CONF_VALUE) *sect = NULL; CONF_VALUE *cnf; const char *connect_str = "localhost:4433"; long errline = -1; int ret = EXIT_FAILURE; conf = NCONF_new(NULL); if (NCONF_load(conf, "connect.cnf", &errline) <= 0) { if (errline <= 0) fprintf(stderr, "Error processing config file\n"); else fprintf(stderr, "Error on line %ld\n", errline); goto end; } sect = NCONF_get_section(conf, "default"); if (sect == NULL) { fprintf(stderr, "Error retrieving default section\n"); goto end; } ctx = SSL_CTX_new(TLS_client_method()); cctx = SSL_CONF_CTX_new(); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE); SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); for (i = 0; i < sk_CONF_VALUE_num(sect); i++) { cnf = sk_CONF_VALUE_value(sect, i); rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value); if (rv > 0) continue; if (rv != -2) { fprintf(stderr, "Error processing %s = %s\n", cnf->name, cnf->value); ERR_print_errors_fp(stderr); goto end; } if (strcmp(cnf->name, "Connect") == 0) { connect_str = cnf->value; } else { fprintf(stderr, "Unknown configuration option %s\n", cnf->name); goto end; } } if (!SSL_CONF_CTX_finish(cctx)) { fprintf(stderr, "Finish error\n"); ERR_print_errors_fp(stderr); goto end; } /* * We'd normally set some stuff like the verify paths and * mode here * because as things stand this will connect to * any server whose * certificate is signed by any CA. */ sbio = BIO_new_ssl_connect(ctx); BIO_get_ssl(sbio, &ssl); if (!ssl) { fprintf(stderr, "Can't locate SSL pointer\n"); goto end; } /* We might want to do other things with ssl here */ BIO_set_conn_hostname(sbio, connect_str); out = BIO_new_fp(stdout, BIO_NOCLOSE); if (BIO_do_connect(sbio) <= 0) { fprintf(stderr, "Error connecting to server\n"); ERR_print_errors_fp(stderr); goto end; } /* Could examine ssl here to get connection info */ BIO_puts(sbio, "GET / HTTP/1.0\n\n"); for (;;) { len = BIO_read(sbio, tmpbuf, 1024); if (len <= 0) break; BIO_write(out, tmpbuf, len); } ret = EXIT_SUCCESS; end: SSL_CONF_CTX_free(cctx); BIO_free_all(sbio); BIO_free(out); NCONF_free(conf); return ret; }
./openssl/demos/bio/server-cmod.c
/* * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * A minimal TLS server it ses SSL_CTX_config and a configuration file to * set most server parameters. */ #include <stdio.h> #include <signal.h> #include <stdlib.h> #include <openssl/err.h> #include <openssl/ssl.h> #include <openssl/conf.h> int main(int argc, char *argv[]) { unsigned char buf[512]; char *port = "*:4433"; BIO *in = NULL; BIO *ssl_bio, *tmp; SSL_CTX *ctx; int ret = EXIT_FAILURE, i; ctx = SSL_CTX_new(TLS_server_method()); if (CONF_modules_load_file("cmod.cnf", "testapp", 0) <= 0) { fprintf(stderr, "Error processing config file\n"); goto err; } if (SSL_CTX_config(ctx, "server") == 0) { fprintf(stderr, "Error configuring server.\n"); goto err; } /* Setup server side SSL bio */ ssl_bio = BIO_new_ssl(ctx, 0); if ((in = BIO_new_accept(port)) == NULL) goto err; /* * This means that when a new connection is accepted on 'in', The ssl_bio * will be 'duplicated' and have the new socket BIO push into it. * Basically it means the SSL BIO will be automatically setup */ BIO_set_accept_bios(in, ssl_bio); again: /* * The first call will setup the accept socket, and the second will get a * socket. In this loop, the first actual accept will occur in the * BIO_read() function. */ if (BIO_do_accept(in) <= 0) goto err; for (;;) { i = BIO_read(in, buf, sizeof(buf)); if (i == 0) { /* * If we have finished, remove the underlying BIO stack so the * next time we call any function for this BIO, it will attempt * to do an accept */ printf("Done\n"); tmp = BIO_pop(in); BIO_free_all(tmp); goto again; } if (i < 0) { if (BIO_should_retry(in)) continue; goto err; } fwrite(buf, 1, i, stdout); fflush(stdout); } ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) ERR_print_errors_fp(stderr); BIO_free(in); return ret; }
./openssl/demos/bio/server-arg.c
/* * Copyright 2013-2017 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * A minimal program to serve an SSL connection. It uses blocking. It use the * SSL_CONF API with the command line. cc -I../../include server-arg.c * -L../.. -lssl -lcrypto -ldl */ #include <stdio.h> #include <string.h> #include <signal.h> #include <stdlib.h> #include <openssl/err.h> #include <openssl/ssl.h> int main(int argc, char *argv[]) { char *port = "*:4433"; BIO *ssl_bio, *tmp; SSL_CTX *ctx; SSL_CONF_CTX *cctx; char buf[512]; BIO *in = NULL; int ret = EXIT_FAILURE, i; char **args = argv + 1; int nargs = argc - 1; ctx = SSL_CTX_new(TLS_server_method()); cctx = SSL_CONF_CTX_new(); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE); SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); while (*args && **args == '-') { int rv; /* Parse standard arguments */ rv = SSL_CONF_cmd_argv(cctx, &nargs, &args); if (rv == -3) { fprintf(stderr, "Missing argument for %s\n", *args); goto err; } if (rv < 0) { fprintf(stderr, "Error in command %s\n", *args); ERR_print_errors_fp(stderr); goto err; } /* If rv > 0 we processed something so proceed to next arg */ if (rv > 0) continue; /* Otherwise application specific argument processing */ if (strcmp(*args, "-port") == 0) { port = args[1]; if (port == NULL) { fprintf(stderr, "Missing -port argument\n"); goto err; } args += 2; nargs -= 2; continue; } else { fprintf(stderr, "Unknown argument %s\n", *args); goto err; } } if (!SSL_CONF_CTX_finish(cctx)) { fprintf(stderr, "Finish error\n"); ERR_print_errors_fp(stderr); goto err; } #ifdef ITERATE_CERTS /* * Demo of how to iterate over all certificates in an SSL_CTX structure. */ { X509 *x; int rv; rv = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST); while (rv) { X509 *x = SSL_CTX_get0_certificate(ctx); X509_NAME_print_ex_fp(stdout, X509_get_subject_name(x), 0, XN_FLAG_ONELINE); printf("\n"); rv = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT); } fflush(stdout); } #endif /* Setup server side SSL bio */ ssl_bio = BIO_new_ssl(ctx, 0); if ((in = BIO_new_accept(port)) == NULL) goto err; /* * This means that when a new connection is accepted on 'in', The ssl_bio * will be 'duplicated' and have the new socket BIO push into it. * Basically it means the SSL BIO will be automatically setup */ BIO_set_accept_bios(in, ssl_bio); again: /* * The first call will setup the accept socket, and the second will get a * socket. In this loop, the first actual accept will occur in the * BIO_read() function. */ if (BIO_do_accept(in) <= 0) goto err; for (;;) { i = BIO_read(in, buf, 512); if (i == 0) { /* * If we have finished, remove the underlying BIO stack so the * next time we call any function for this BIO, it will attempt * to do an accept */ printf("Done\n"); tmp = BIO_pop(in); BIO_free_all(tmp); goto again; } if (i < 0) goto err; fwrite(buf, 1, i, stdout); fflush(stdout); } ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) ERR_print_errors_fp(stderr); BIO_free(in); return ret; }
./openssl/demos/sslecho/main.c
/* * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <openssl/ssl.h> #include <openssl/err.h> #include <signal.h> static const int server_port = 4433; typedef unsigned char bool; #define true 1 #define false 0 /* * This flag won't be useful until both accept/read (TCP & SSL) methods * can be called with a timeout. TBD. */ static volatile bool server_running = true; int create_socket(bool isServer) { int s; int optval = 1; struct sockaddr_in addr; s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) { perror("Unable to create socket"); exit(EXIT_FAILURE); } if (isServer) { addr.sin_family = AF_INET; addr.sin_port = htons(server_port); addr.sin_addr.s_addr = INADDR_ANY; /* Reuse the address; good for quick restarts */ if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) { perror("setsockopt(SO_REUSEADDR) failed"); exit(EXIT_FAILURE); } if (bind(s, (struct sockaddr*) &addr, sizeof(addr)) < 0) { perror("Unable to bind"); exit(EXIT_FAILURE); } if (listen(s, 1) < 0) { perror("Unable to listen"); exit(EXIT_FAILURE); } } return s; } SSL_CTX* create_context(bool isServer) { const SSL_METHOD *method; SSL_CTX *ctx; if (isServer) method = TLS_server_method(); else method = TLS_client_method(); ctx = SSL_CTX_new(method); if (ctx == NULL) { perror("Unable to create SSL context"); ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } return ctx; } void configure_server_context(SSL_CTX *ctx) { /* Set the key and cert */ if (SSL_CTX_use_certificate_chain_file(ctx, "cert.pem") <= 0) { ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } if (SSL_CTX_use_PrivateKey_file(ctx, "key.pem", SSL_FILETYPE_PEM) <= 0) { ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } } void configure_client_context(SSL_CTX *ctx) { /* * Configure the client to abort the handshake if certificate verification * fails */ SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); /* * In a real application you would probably just use the default system certificate trust store and call: * SSL_CTX_set_default_verify_paths(ctx); * In this demo though we are using a self-signed certificate, so the client must trust it directly. */ if (!SSL_CTX_load_verify_locations(ctx, "cert.pem", NULL)) { ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } } void usage(void) { printf("Usage: sslecho s\n"); printf(" --or--\n"); printf(" sslecho c ip\n"); printf(" c=client, s=server, ip=dotted ip of server\n"); exit(EXIT_FAILURE); } int main(int argc, char **argv) { bool isServer; int result; SSL_CTX *ssl_ctx = NULL; SSL *ssl = NULL; int server_skt = -1; int client_skt = -1; /* used by getline relying on realloc, can't be statically allocated */ char *txbuf = NULL; size_t txcap = 0; int txlen; char rxbuf[128]; size_t rxcap = sizeof(rxbuf); int rxlen; char *rem_server_ip = NULL; struct sockaddr_in addr; unsigned int addr_len = sizeof(addr); /* ignore SIGPIPE so that server can continue running when client pipe closes abruptly */ signal(SIGPIPE, SIG_IGN); /* Splash */ printf("\nsslecho : Simple Echo Client/Server : %s : %s\n\n", __DATE__, __TIME__); /* Need to know if client or server */ if (argc < 2) { usage(); /* NOTREACHED */ } isServer = (argv[1][0] == 's') ? true : false; /* If client get remote server address (could be 127.0.0.1) */ if (!isServer) { if (argc != 3) { usage(); /* NOTREACHED */ } rem_server_ip = argv[2]; } /* Create context used by both client and server */ ssl_ctx = create_context(isServer); /* If server */ if (isServer) { printf("We are the server on port: %d\n\n", server_port); /* Configure server context with appropriate key files */ configure_server_context(ssl_ctx); /* Create server socket; will bind with server port and listen */ server_skt = create_socket(true); /* * Loop to accept clients. * Need to implement timeouts on TCP & SSL connect/read functions * before we can catch a CTRL-C and kill the server. */ while (server_running) { /* Wait for TCP connection from client */ client_skt = accept(server_skt, (struct sockaddr*) &addr, &addr_len); if (client_skt < 0) { perror("Unable to accept"); exit(EXIT_FAILURE); } printf("Client TCP connection accepted\n"); /* Create server SSL structure using newly accepted client socket */ ssl = SSL_new(ssl_ctx); SSL_set_fd(ssl, client_skt); /* Wait for SSL connection from the client */ if (SSL_accept(ssl) <= 0) { ERR_print_errors_fp(stderr); server_running = false; } else { printf("Client SSL connection accepted\n\n"); /* Echo loop */ while (true) { /* Get message from client; will fail if client closes connection */ if ((rxlen = SSL_read(ssl, rxbuf, rxcap)) <= 0) { if (rxlen == 0) { printf("Client closed connection\n"); } else { printf("SSL_read returned %d\n", rxlen); } ERR_print_errors_fp(stderr); break; } /* Insure null terminated input */ rxbuf[rxlen] = 0; /* Look for kill switch */ if (strcmp(rxbuf, "kill\n") == 0) { /* Terminate...with extreme prejudice */ printf("Server received 'kill' command\n"); server_running = false; break; } /* Show received message */ printf("Received: %s", rxbuf); /* Echo it back */ if (SSL_write(ssl, rxbuf, rxlen) <= 0) { ERR_print_errors_fp(stderr); } } } if (server_running) { /* Cleanup for next client */ SSL_shutdown(ssl); SSL_free(ssl); close(client_skt); } } printf("Server exiting...\n"); } /* Else client */ else { printf("We are the client\n\n"); /* Configure client context so we verify the server correctly */ configure_client_context(ssl_ctx); /* Create "bare" socket */ client_skt = create_socket(false); /* Set up connect address */ addr.sin_family = AF_INET; inet_pton(AF_INET, rem_server_ip, &addr.sin_addr.s_addr); addr.sin_port = htons(server_port); /* Do TCP connect with server */ if (connect(client_skt, (struct sockaddr*) &addr, sizeof(addr)) != 0) { perror("Unable to TCP connect to server"); goto exit; } else { printf("TCP connection to server successful\n"); } /* Create client SSL structure using dedicated client socket */ ssl = SSL_new(ssl_ctx); SSL_set_fd(ssl, client_skt); /* Set hostname for SNI */ SSL_set_tlsext_host_name(ssl, rem_server_ip); /* Configure server hostname check */ SSL_set1_host(ssl, rem_server_ip); /* Now do SSL connect with server */ if (SSL_connect(ssl) == 1) { printf("SSL connection to server successful\n\n"); /* Loop to send input from keyboard */ while (true) { /* Get a line of input */ txlen = getline(&txbuf, &txcap, stdin); /* Exit loop on error */ if (txlen < 0 || txbuf == NULL) { break; } /* Exit loop if just a carriage return */ if (txbuf[0] == '\n') { break; } /* Send it to the server */ if ((result = SSL_write(ssl, txbuf, txlen)) <= 0) { printf("Server closed connection\n"); ERR_print_errors_fp(stderr); break; } /* Wait for the echo */ rxlen = SSL_read(ssl, rxbuf, rxcap); if (rxlen <= 0) { printf("Server closed connection\n"); ERR_print_errors_fp(stderr); break; } else { /* Show it */ rxbuf[rxlen] = 0; printf("Received: %s", rxbuf); } } printf("Client exiting...\n"); } else { printf("SSL connection to server failed\n\n"); ERR_print_errors_fp(stderr); } } exit: /* Close up */ if (ssl != NULL) { SSL_shutdown(ssl); SSL_free(ssl); } SSL_CTX_free(ssl_ctx); if (client_skt != -1) close(client_skt); if (server_skt != -1) close(server_skt); if (txbuf != NULL && txcap > 0) free(txbuf); printf("sslecho exiting\n"); return EXIT_SUCCESS; }
./openssl/demos/mac/gmac.c
/* * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <stdlib.h> #include <openssl/core_names.h> #include <openssl/evp.h> #include <openssl/params.h> #include <openssl/err.h> /* * Taken from NIST's GCM Test Vectors * http://csrc.nist.gov/groups/STM/cavp/ */ /* * Hard coding the key into an application is very bad. * It is done here solely for educational purposes. */ static unsigned char key[] = { 0x77, 0xbe, 0x63, 0x70, 0x89, 0x71, 0xc4, 0xe2, 0x40, 0xd1, 0xcb, 0x79, 0xe8, 0xd7, 0x7f, 0xeb }; /* * The initialisation vector (IV) is better not being hard coded too. * Repeating password/IV pairs compromises the integrity of GMAC. * The IV is not considered secret information and is safe to store with * an encrypted password. */ static unsigned char iv[] = { 0xe0, 0xe0, 0x0f, 0x19, 0xfe, 0xd7, 0xba, 0x01, 0x36, 0xa7, 0x97, 0xf3 }; static unsigned char data[] = { 0x7a, 0x43, 0xec, 0x1d, 0x9c, 0x0a, 0x5a, 0x78, 0xa0, 0xb1, 0x65, 0x33, 0xa6, 0x21, 0x3c, 0xab }; static const unsigned char expected_output[] = { 0x20, 0x9f, 0xcc, 0x8d, 0x36, 0x75, 0xed, 0x93, 0x8e, 0x9c, 0x71, 0x66, 0x70, 0x9d, 0xd9, 0x46 }; /* * A property query used for selecting the GMAC implementation and the * underlying GCM mode cipher. */ static char *propq = NULL; int main(int argc, char **argv) { int ret = EXIT_FAILURE; EVP_MAC *mac = NULL; EVP_MAC_CTX *mctx = NULL; unsigned char out[16]; OSSL_PARAM params[4], *p = params; OSSL_LIB_CTX *library_context = NULL; size_t out_len = 0; library_context = OSSL_LIB_CTX_new(); if (library_context == NULL) { fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n"); goto end; } /* Fetch the GMAC implementation */ mac = EVP_MAC_fetch(library_context, "GMAC", propq); if (mac == NULL) { fprintf(stderr, "EVP_MAC_fetch() returned NULL\n"); goto end; } /* Create a context for the GMAC operation */ mctx = EVP_MAC_CTX_new(mac); if (mctx == NULL) { fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n"); goto end; } /* GMAC requires a GCM mode cipher to be specified */ *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, "AES-128-GCM", 0); /* * If a non-default property query is required when fetching the GCM mode * cipher, it needs to be specified too. */ if (propq != NULL) *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES, propq, 0); /* Set the initialisation vector (IV) */ *p++ = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, iv, sizeof(iv)); *p = OSSL_PARAM_construct_end(); /* Initialise the GMAC operation */ if (!EVP_MAC_init(mctx, key, sizeof(key), params)) { fprintf(stderr, "EVP_MAC_init() failed\n"); goto end; } /* Make one or more calls to process the data to be authenticated */ if (!EVP_MAC_update(mctx, data, sizeof(data))) { fprintf(stderr, "EVP_MAC_update() failed\n"); goto end; } /* Make one call to the final to get the MAC */ if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) { fprintf(stderr, "EVP_MAC_final() failed\n"); goto end; } printf("Generated MAC:\n"); BIO_dump_indent_fp(stdout, out, out_len, 2); putchar('\n'); if (out_len != sizeof(expected_output)) { fprintf(stderr, "Generated MAC has an unexpected length\n"); goto end; } if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) { fprintf(stderr, "Generated MAC does not match expected value\n"); goto end; } ret = EXIT_SUCCESS; end: EVP_MAC_CTX_free(mctx); EVP_MAC_free(mac); OSSL_LIB_CTX_free(library_context); if (ret != EXIT_SUCCESS) ERR_print_errors_fp(stderr); return ret; }
./openssl/demos/mac/cmac-aes256.c
/*- * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * Example of using EVP_MAC_ methods to calculate * a CMAC of static buffers */ #include <string.h> #include <stdio.h> #include <openssl/crypto.h> #include <openssl/core_names.h> #include <openssl/err.h> #include <openssl/evp.h> #include <openssl/cmac.h> #include <openssl/params.h> /* * Hard coding the key into an application is very bad. * It is done here solely for educational purposes. */ static unsigned char key[] = { 0x6c, 0xde, 0x14, 0xf5, 0xd5, 0x2a, 0x4a, 0xdf, 0x12, 0x39, 0x1e, 0xbf, 0x36, 0xf9, 0x6a, 0x46, 0x48, 0xd0, 0xb6, 0x51, 0x89, 0xfc, 0x24, 0x85, 0xa8, 0x8d, 0xdf, 0x7e, 0x80, 0x14, 0xc8, 0xce, }; static const unsigned char data[] = "To be, or not to be, that is the question,\n" "Whether tis nobler in the minde to suffer\n" "The ſlings and arrowes of outragious fortune,\n" "Or to take Armes again in a sea of troubles,\n" "And by opposing, end them, to die to sleep;\n" "No more, and by a sleep, to say we end\n" "The heart-ache, and the thousand natural shocks\n" "That flesh is heir to? tis a consumation\n" "Devoutly to be wished. To die to sleep,\n" "To sleepe, perchance to dreame, Aye, there's the rub,\n" "For in that sleep of death what dreams may come\n" "When we haue shuffled off this mortal coil\n" "Must give us pause. There's the respect\n" "That makes calamity of so long life:\n" "For who would bear the Ships and Scorns of time,\n" "The oppressor's wrong, the proud man's Contumely,\n" "The pangs of dispised love, the Law's delay,\n" ; /* The known value of the CMAC/AES256 MAC of the above soliloqy */ static const unsigned char expected_output[] = { 0x67, 0x92, 0x32, 0x23, 0x50, 0x3d, 0xc5, 0xba, 0x78, 0xd4, 0x6d, 0x63, 0xf2, 0x2b, 0xe9, 0x56, }; /* * A property query used for selecting the MAC implementation. */ static const char *propq = NULL; int main(void) { int ret = EXIT_FAILURE; OSSL_LIB_CTX *library_context = NULL; EVP_MAC *mac = NULL; EVP_MAC_CTX *mctx = NULL; unsigned char *out = NULL; size_t out_len = 0; OSSL_PARAM params[4], *p = params; char cipher_name[] = "AES-256-CBC"; library_context = OSSL_LIB_CTX_new(); if (library_context == NULL) { fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n"); goto end; } /* Fetch the CMAC implementation */ mac = EVP_MAC_fetch(library_context, "CMAC", propq); if (mac == NULL) { fprintf(stderr, "EVP_MAC_fetch() returned NULL\n"); goto end; } /* Create a context for the CMAC operation */ mctx = EVP_MAC_CTX_new(mac); if (mctx == NULL) { fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n"); goto end; } /* The underlying cipher to be used */ *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, cipher_name, sizeof(cipher_name)); *p = OSSL_PARAM_construct_end(); /* Initialise the CMAC operation */ if (!EVP_MAC_init(mctx, key, sizeof(key), params)) { fprintf(stderr, "EVP_MAC_init() failed\n"); goto end; } /* Make one or more calls to process the data to be authenticated */ if (!EVP_MAC_update(mctx, data, sizeof(data))) { fprintf(stderr, "EVP_MAC_update() failed\n"); goto end; } /* Make a call to the final with a NULL buffer to get the length of the MAC */ if (!EVP_MAC_final(mctx, NULL, &out_len, 0)) { fprintf(stderr, "EVP_MAC_final() failed\n"); goto end; } out = OPENSSL_malloc(out_len); if (out == NULL) { fprintf(stderr, "malloc failed\n"); goto end; } /* Make one call to the final to get the MAC */ if (!EVP_MAC_final(mctx, out, &out_len, out_len)) { fprintf(stderr, "EVP_MAC_final() failed\n"); goto end; } printf("Generated MAC:\n"); BIO_dump_indent_fp(stdout, out, out_len, 2); putchar('\n'); if (out_len != sizeof(expected_output)) { fprintf(stderr, "Generated MAC has an unexpected length\n"); goto end; } if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) { fprintf(stderr, "Generated MAC does not match expected value\n"); goto end; } ret = EXIT_SUCCESS; end: if (ret != EXIT_SUCCESS) ERR_print_errors_fp(stderr); /* OpenSSL free functions will ignore NULL arguments */ OPENSSL_free(out); EVP_MAC_CTX_free(mctx); EVP_MAC_free(mac); OSSL_LIB_CTX_free(library_context); return ret; }
./openssl/demos/mac/siphash.c
/* * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <stdlib.h> #include <openssl/core_names.h> #include <openssl/evp.h> #include <openssl/params.h> #include <openssl/err.h> /* * Taken from the test vector from the paper "SipHash: a fast short-input PRF". * https://www.aumasson.jp/siphash/siphash.pdf */ /* * Hard coding the key into an application is very bad. * It is done here solely for educational purposes. */ static unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; static unsigned char data[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e }; static const unsigned char expected_output[] = { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1 }; /* * A property query used for selecting the SIPHASH implementation. */ static char *propq = NULL; int main(int argc, char **argv) { int ret = EXIT_FAILURE; EVP_MAC *mac = NULL; EVP_MAC_CTX *mctx = NULL; unsigned char out[8]; OSSL_PARAM params[4], *p = params; OSSL_LIB_CTX *library_context = NULL; unsigned int digest_len = 8, c_rounds = 2, d_rounds = 4; size_t out_len = 0; library_context = OSSL_LIB_CTX_new(); if (library_context == NULL) { fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n"); goto end; } /* Fetch the SipHash implementation */ mac = EVP_MAC_fetch(library_context, "SIPHASH", propq); if (mac == NULL) { fprintf(stderr, "EVP_MAC_fetch() returned NULL\n"); goto end; } /* Create a context for the SipHash operation */ mctx = EVP_MAC_CTX_new(mac); if (mctx == NULL) { fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n"); goto end; } /* SipHash can support either 8 or 16-byte digests. */ *p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_SIZE, &digest_len); /* * The number of C-rounds and D-rounds is configurable. Standard SipHash * uses values of 2 and 4 respectively. The following lines are unnecessary * as they set the default, but demonstrate how to change these values. */ *p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_C_ROUNDS, &c_rounds); *p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_D_ROUNDS, &d_rounds); *p = OSSL_PARAM_construct_end(); /* Initialise the SIPHASH operation */ if (!EVP_MAC_init(mctx, key, sizeof(key), params)) { fprintf(stderr, "EVP_MAC_init() failed\n"); goto end; } /* Make one or more calls to process the data to be authenticated */ if (!EVP_MAC_update(mctx, data, sizeof(data))) { fprintf(stderr, "EVP_MAC_update() failed\n"); goto end; } /* Make one call to the final to get the MAC */ if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) { fprintf(stderr, "EVP_MAC_final() failed\n"); goto end; } printf("Generated MAC:\n"); BIO_dump_indent_fp(stdout, out, out_len, 2); putchar('\n'); if (out_len != sizeof(expected_output)) { fprintf(stderr, "Generated MAC has an unexpected length\n"); goto end; } if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) { fprintf(stderr, "Generated MAC does not match expected value\n"); goto end; } ret = EXIT_SUCCESS; end: EVP_MAC_CTX_free(mctx); EVP_MAC_free(mac); OSSL_LIB_CTX_free(library_context); if (ret != EXIT_SUCCESS) ERR_print_errors_fp(stderr); return ret; }
./openssl/demos/mac/hmac-sha512.c
/*- * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * Example of using EVP_MAC_ methods to calculate * a HMAC of static buffers */ #include <string.h> #include <stdio.h> #include <openssl/crypto.h> #include <openssl/core_names.h> #include <openssl/err.h> #include <openssl/evp.h> #include <openssl/hmac.h> #include <openssl/params.h> /* * Hard coding the key into an application is very bad. * It is done here solely for educational purposes. */ static unsigned char key[] = { 0x25, 0xfd, 0x12, 0x99, 0xdf, 0xad, 0x1a, 0x03, 0x0a, 0x81, 0x3c, 0x2d, 0xcc, 0x05, 0xd1, 0x5c, 0x17, 0x7a, 0x36, 0x73, 0x17, 0xef, 0x41, 0x75, 0x71, 0x18, 0xe0, 0x1a, 0xda, 0x99, 0xc3, 0x61, 0x38, 0xb5, 0xb1, 0xe0, 0x82, 0x2c, 0x70, 0xa4, 0xc0, 0x8e, 0x5e, 0xf9, 0x93, 0x9f, 0xcf, 0xf7, 0x32, 0x4d, 0x0c, 0xbd, 0x31, 0x12, 0x0f, 0x9a, 0x15, 0xee, 0x82, 0xdb, 0x8d, 0x29, 0x54, 0x14, }; static const unsigned char data[] = "To be, or not to be, that is the question,\n" "Whether tis nobler in the minde to suffer\n" "The ſlings and arrowes of outragious fortune,\n" "Or to take Armes again in a sea of troubles,\n" "And by opposing, end them, to die to sleep;\n" "No more, and by a sleep, to say we end\n" "The heart-ache, and the thousand natural shocks\n" "That flesh is heir to? tis a consumation\n" "Devoutly to be wished. To die to sleep,\n" "To sleepe, perchance to dreame, Aye, there's the rub,\n" "For in that sleep of death what dreams may come\n" "When we haue shuffled off this mortal coil\n" "Must give us pause. There's the respect\n" "That makes calamity of so long life:\n" "For who would bear the Ships and Scorns of time,\n" "The oppressor's wrong, the proud man's Contumely,\n" "The pangs of dispised love, the Law's delay,\n" ; /* The known value of the HMAC/SHA3-512 MAC of the above soliloqy */ static const unsigned char expected_output[] = { 0x3b, 0x77, 0x5f, 0xf1, 0x4f, 0x9e, 0xb9, 0x23, 0x8f, 0xdc, 0xa0, 0x68, 0x15, 0x7b, 0x8a, 0xf1, 0x96, 0x23, 0xaa, 0x3c, 0x1f, 0xe9, 0xdc, 0x89, 0x11, 0x7d, 0x58, 0x07, 0xe7, 0x96, 0x17, 0xe3, 0x44, 0x8b, 0x03, 0x37, 0x91, 0xc0, 0x6e, 0x06, 0x7c, 0x54, 0xe4, 0xa4, 0xcc, 0xd5, 0x16, 0xbb, 0x5e, 0x4d, 0x64, 0x7d, 0x88, 0x23, 0xc9, 0xb7, 0x25, 0xda, 0xbe, 0x4b, 0xe4, 0xd5, 0x34, 0x30, }; /* * A property query used for selecting the MAC implementation. */ static const char *propq = NULL; int main(void) { int ret = EXIT_FAILURE; OSSL_LIB_CTX *library_context = NULL; EVP_MAC *mac = NULL; EVP_MAC_CTX *mctx = NULL; EVP_MD_CTX *digest_context = NULL; unsigned char *out = NULL; size_t out_len = 0; OSSL_PARAM params[4], *p = params; char digest_name[] = "SHA3-512"; library_context = OSSL_LIB_CTX_new(); if (library_context == NULL) { fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n"); goto end; } /* Fetch the HMAC implementation */ mac = EVP_MAC_fetch(library_context, "HMAC", propq); if (mac == NULL) { fprintf(stderr, "EVP_MAC_fetch() returned NULL\n"); goto end; } /* Create a context for the HMAC operation */ mctx = EVP_MAC_CTX_new(mac); if (mctx == NULL) { fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n"); goto end; } /* The underlying digest to be used */ *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest_name, sizeof(digest_name)); *p = OSSL_PARAM_construct_end(); /* Initialise the HMAC operation */ if (!EVP_MAC_init(mctx, key, sizeof(key), params)) { fprintf(stderr, "EVP_MAC_init() failed\n"); goto end; } /* Make one or more calls to process the data to be authenticated */ if (!EVP_MAC_update(mctx, data, sizeof(data))) { fprintf(stderr, "EVP_MAC_update() failed\n"); goto end; } /* Make a call to the final with a NULL buffer to get the length of the MAC */ if (!EVP_MAC_final(mctx, NULL, &out_len, 0)) { fprintf(stderr, "EVP_MAC_final() failed\n"); goto end; } out = OPENSSL_malloc(out_len); if (out == NULL) { fprintf(stderr, "malloc failed\n"); goto end; } /* Make one call to the final to get the MAC */ if (!EVP_MAC_final(mctx, out, &out_len, out_len)) { fprintf(stderr, "EVP_MAC_final() failed\n"); goto end; } printf("Generated MAC:\n"); BIO_dump_indent_fp(stdout, out, out_len, 2); putchar('\n'); if (out_len != sizeof(expected_output)) { fprintf(stderr, "Generated MAC has an unexpected length\n"); goto end; } if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) { fprintf(stderr, "Generated MAC does not match expected value\n"); goto end; } ret = EXIT_SUCCESS; end: if (ret != EXIT_SUCCESS) ERR_print_errors_fp(stderr); /* OpenSSL free functions will ignore NULL arguments */ OPENSSL_free(out); EVP_MD_CTX_free(digest_context); EVP_MAC_CTX_free(mctx); EVP_MAC_free(mac); OSSL_LIB_CTX_free(library_context); return ret; }
./openssl/demos/mac/poly1305.c
/* * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/core_names.h> #include <openssl/evp.h> #include <openssl/params.h> #include <openssl/err.h> /* * This is a demonstration of how to compute Poly1305-AES using the OpenSSL * Poly1305 and AES providers and the EVP API. * * Please note that: * * - Poly1305 must never be used alone and must be used in conjunction with * another primitive which processes the input nonce to be secure; * * - you must never pass a nonce to the Poly1305 primitive directly; * * - Poly1305 exhibits catastrophic failure (that is, can be broken) if a * nonce is ever reused for a given key. * * If you are looking for a general purpose MAC, you should consider using a * different MAC and looking at one of the other examples, unless you have a * good familiarity with the details and caveats of Poly1305. * * This example uses AES, as described in the original paper, "The Poly1305-AES * message authentication code": * https://cr.yp.to/mac/poly1305-20050329.pdf * * The test vectors below are from that paper. */ /* * Hard coding the key into an application is very bad. * It is done here solely for educational purposes. * These are the "r" and "k" inputs to Poly1305-AES. */ static const unsigned char test_r[] = { 0x85, 0x1f, 0xc4, 0x0c, 0x34, 0x67, 0xac, 0x0b, 0xe0, 0x5c, 0xc2, 0x04, 0x04, 0xf3, 0xf7, 0x00 }; static const unsigned char test_k[] = { 0xec, 0x07, 0x4c, 0x83, 0x55, 0x80, 0x74, 0x17, 0x01, 0x42, 0x5b, 0x62, 0x32, 0x35, 0xad, 0xd6 }; /* * Hard coding a nonce must not be done under any circumstances and is done here * purely for demonstration purposes. Please note that Poly1305 exhibits * catastrophic failure (that is, can be broken) if a nonce is ever reused for a * given key. */ static const unsigned char test_n[] = { 0xfb, 0x44, 0x73, 0x50, 0xc4, 0xe8, 0x68, 0xc5, 0x2a, 0xc3, 0x27, 0x5c, 0xf9, 0xd4, 0x32, 0x7e }; /* Input message. */ static const unsigned char test_m[] = { 0xf3, 0xf6 }; static const unsigned char expected_output[] = { 0xf4, 0xc6, 0x33, 0xc3, 0x04, 0x4f, 0xc1, 0x45, 0xf8, 0x4f, 0x33, 0x5c, 0xb8, 0x19, 0x53, 0xde }; /* * A property query used for selecting the POLY1305 implementation. */ static char *propq = NULL; int main(int argc, char **argv) { int ret = EXIT_FAILURE; EVP_CIPHER *aes = NULL; EVP_CIPHER_CTX *aesctx = NULL; EVP_MAC *mac = NULL; EVP_MAC_CTX *mctx = NULL; unsigned char composite_key[32]; unsigned char out[16]; OSSL_LIB_CTX *library_context = NULL; size_t out_len = 0; int aes_len = 0; library_context = OSSL_LIB_CTX_new(); if (library_context == NULL) { fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n"); goto end; } /* Fetch the Poly1305 implementation */ mac = EVP_MAC_fetch(library_context, "POLY1305", propq); if (mac == NULL) { fprintf(stderr, "EVP_MAC_fetch() returned NULL\n"); goto end; } /* Create a context for the Poly1305 operation */ mctx = EVP_MAC_CTX_new(mac); if (mctx == NULL) { fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n"); goto end; } /* Fetch the AES implementation */ aes = EVP_CIPHER_fetch(library_context, "AES-128-ECB", propq); if (aes == NULL) { fprintf(stderr, "EVP_CIPHER_fetch() returned NULL\n"); goto end; } /* Create a context for AES */ aesctx = EVP_CIPHER_CTX_new(); if (aesctx == NULL) { fprintf(stderr, "EVP_CIPHER_CTX_new() returned NULL\n"); goto end; } /* Initialize the AES cipher with the 128-bit key k */ if (!EVP_EncryptInit_ex(aesctx, aes, NULL, test_k, NULL)) { fprintf(stderr, "EVP_EncryptInit_ex() failed\n"); goto end; } /* * Disable padding for the AES cipher. We do not strictly need to do this as * we are encrypting a single block and thus there are no alignment or * padding concerns, but this ensures that the operation below fails if * padding would be required for some reason, which in this circumstance * would indicate an implementation bug. */ if (!EVP_CIPHER_CTX_set_padding(aesctx, 0)) { fprintf(stderr, "EVP_CIPHER_CTX_set_padding() failed\n"); goto end; } /* * Computes the value AES_k(n) which we need for our Poly1305-AES * computation below. */ if (!EVP_EncryptUpdate(aesctx, composite_key + 16, &aes_len, test_n, sizeof(test_n))) { fprintf(stderr, "EVP_EncryptUpdate() failed\n"); goto end; } /* * The Poly1305 provider expects the key r to be passed as the first 16 * bytes of the "key" and the processed nonce (that is, AES_k(n)) to be * passed as the second 16 bytes of the "key". We already put the processed * nonce in the correct place above, so copy r into place. */ memcpy(composite_key, test_r, 16); /* Initialise the Poly1305 operation */ if (!EVP_MAC_init(mctx, composite_key, sizeof(composite_key), NULL)) { fprintf(stderr, "EVP_MAC_init() failed\n"); goto end; } /* Make one or more calls to process the data to be authenticated */ if (!EVP_MAC_update(mctx, test_m, sizeof(test_m))) { fprintf(stderr, "EVP_MAC_update() failed\n"); goto end; } /* Make one call to the final to get the MAC */ if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) { fprintf(stderr, "EVP_MAC_final() failed\n"); goto end; } printf("Generated MAC:\n"); BIO_dump_indent_fp(stdout, out, out_len, 2); putchar('\n'); if (out_len != sizeof(expected_output)) { fprintf(stderr, "Generated MAC has an unexpected length\n"); goto end; } if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) { fprintf(stderr, "Generated MAC does not match expected value\n"); goto end; } ret = EXIT_SUCCESS; end: EVP_CIPHER_CTX_free(aesctx); EVP_CIPHER_free(aes); EVP_MAC_CTX_free(mctx); EVP_MAC_free(mac); OSSL_LIB_CTX_free(library_context); if (ret != EXIT_SUCCESS) ERR_print_errors_fp(stderr); return ret; }
./openssl/demos/cms/cms_dec.c
/* * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Simple S/MIME decryption example */ #include <openssl/pem.h> #include <openssl/cms.h> #include <openssl/err.h> int main(int argc, char **argv) { BIO *in = NULL, *out = NULL, *tbio = NULL; X509 *rcert = NULL; EVP_PKEY *rkey = NULL; CMS_ContentInfo *cms = NULL; int ret = EXIT_FAILURE; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); /* Read in recipient certificate and private key */ tbio = BIO_new_file("signer.pem", "r"); if (!tbio) goto err; rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (BIO_reset(tbio) < 0) goto err; rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL); if (!rcert || !rkey) goto err; /* Open S/MIME message to decrypt */ in = BIO_new_file("smencr.txt", "r"); if (!in) goto err; /* Parse message */ cms = SMIME_read_CMS(in, NULL); if (!cms) goto err; out = BIO_new_file("decout.txt", "w"); if (!out) goto err; /* Decrypt S/MIME message */ if (!CMS_decrypt(cms, rkey, rcert, NULL, out, 0)) goto err; printf("Decryption Successful\n"); ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Decrypting Data\n"); ERR_print_errors_fp(stderr); } CMS_ContentInfo_free(cms); X509_free(rcert); EVP_PKEY_free(rkey); BIO_free(in); BIO_free(out); BIO_free(tbio); return ret; }
./openssl/demos/cms/cms_enc.c
/* * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Simple S/MIME encrypt example */ #include <openssl/pem.h> #include <openssl/cms.h> #include <openssl/err.h> int main(int argc, char **argv) { BIO *in = NULL, *out = NULL, *tbio = NULL; X509 *rcert = NULL; STACK_OF(X509) *recips = NULL; CMS_ContentInfo *cms = NULL; int ret = EXIT_FAILURE; /* * On OpenSSL 1.0.0 and later only: * for streaming set CMS_STREAM */ int flags = CMS_STREAM; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); /* Read in recipient certificate */ tbio = BIO_new_file("signer.pem", "r"); if (!tbio) goto err; rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (!rcert) goto err; /* Create recipient STACK and add recipient cert to it */ recips = sk_X509_new_null(); if (!recips || !sk_X509_push(recips, rcert)) goto err; /* * OSSL_STACK_OF_X509_free() will free up recipient STACK and its contents * so set rcert to NULL so it isn't freed up twice. */ rcert = NULL; /* Open content being encrypted */ in = BIO_new_file("encr.txt", "r"); if (!in) goto err; /* encrypt content */ cms = CMS_encrypt(recips, in, EVP_des_ede3_cbc(), flags); if (!cms) goto err; out = BIO_new_file("smencr.txt", "w"); if (!out) goto err; /* Write out S/MIME message */ if (!SMIME_write_CMS(out, cms, in, flags)) goto err; printf("Encryption Successful\n"); ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Encrypting Data\n"); ERR_print_errors_fp(stderr); } CMS_ContentInfo_free(cms); X509_free(rcert); OSSL_STACK_OF_X509_free(recips); BIO_free(in); BIO_free(out); BIO_free(tbio); return ret; }
./openssl/demos/cms/cms_sign.c
/* * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Simple S/MIME signing example */ #include <openssl/pem.h> #include <openssl/cms.h> #include <openssl/err.h> int main(int argc, char **argv) { BIO *in = NULL, *out = NULL, *tbio = NULL; X509 *scert = NULL; EVP_PKEY *skey = NULL; CMS_ContentInfo *cms = NULL; int ret = EXIT_FAILURE; /* * For simple S/MIME signing use CMS_DETACHED. On OpenSSL 1.0.0 only: for * streaming detached set CMS_DETACHED|CMS_STREAM for streaming * non-detached set CMS_STREAM */ int flags = CMS_DETACHED | CMS_STREAM; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); /* Read in signer certificate and private key */ tbio = BIO_new_file("signer.pem", "r"); if (!tbio) goto err; scert = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (BIO_reset(tbio) < 0) goto err; skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL); if (!scert || !skey) goto err; /* Open content being signed */ in = BIO_new_file("sign.txt", "r"); if (!in) goto err; /* Sign content */ cms = CMS_sign(scert, skey, NULL, in, flags); if (!cms) goto err; out = BIO_new_file("smout.txt", "w"); if (!out) goto err; if (!(flags & CMS_STREAM)) { if (BIO_reset(in) < 0) goto err; } /* Write out S/MIME message */ if (!SMIME_write_CMS(out, cms, in, flags)) goto err; ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Signing Data\n"); ERR_print_errors_fp(stderr); } CMS_ContentInfo_free(cms); X509_free(scert); EVP_PKEY_free(skey); BIO_free(in); BIO_free(out); BIO_free(tbio); return ret; }
./openssl/demos/cms/cms_denc.c
/* * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * S/MIME detached data encrypt example: rarely done but should the need * arise this is an example.... */ #include <openssl/pem.h> #include <openssl/cms.h> #include <openssl/err.h> int main(int argc, char **argv) { BIO *in = NULL, *out = NULL, *tbio = NULL, *dout = NULL; X509 *rcert = NULL; STACK_OF(X509) *recips = NULL; CMS_ContentInfo *cms = NULL; int ret = EXIT_FAILURE; int flags = CMS_STREAM | CMS_DETACHED; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); /* Read in recipient certificate */ tbio = BIO_new_file("signer.pem", "r"); if (!tbio) goto err; rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (!rcert) goto err; /* Create recipient STACK and add recipient cert to it */ recips = sk_X509_new_null(); if (!recips || !sk_X509_push(recips, rcert)) goto err; /* * OSSL_STACK_OF_X509_free() free up recipient STACK and its contents * so set rcert to NULL so it isn't freed up twice. */ rcert = NULL; /* Open content being encrypted */ in = BIO_new_file("encr.txt", "r"); dout = BIO_new_file("smencr.out", "wb"); if (!in) goto err; /* encrypt content */ cms = CMS_encrypt(recips, in, EVP_des_ede3_cbc(), flags); if (!cms) goto err; out = BIO_new_file("smencr.pem", "w"); if (!out) goto err; if (!CMS_final(cms, in, dout, flags)) goto err; /* Write out CMS structure without content */ if (!PEM_write_bio_CMS(out, cms)) goto err; ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Encrypting Data\n"); ERR_print_errors_fp(stderr); } CMS_ContentInfo_free(cms); X509_free(rcert); OSSL_STACK_OF_X509_free(recips); BIO_free(in); BIO_free(out); BIO_free(dout); BIO_free(tbio); return ret; }
./openssl/demos/cms/cms_comp.c
/* * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Simple S/MIME compress example */ #include <openssl/pem.h> #include <openssl/cms.h> #include <openssl/err.h> int main(int argc, char **argv) { BIO *in = NULL, *out = NULL; CMS_ContentInfo *cms = NULL; int ret = EXIT_FAILURE; /* * On OpenSSL 1.0.0+ only: * for streaming set CMS_STREAM */ int flags = CMS_STREAM; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); /* Open content being compressed */ in = BIO_new_file("comp.txt", "r"); if (!in) goto err; /* compress content */ cms = CMS_compress(in, NID_zlib_compression, flags); if (!cms) goto err; out = BIO_new_file("smcomp.txt", "w"); if (!out) goto err; /* Write out S/MIME message */ if (!SMIME_write_CMS(out, cms, in, flags)) goto err; ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Compressing Data\n"); ERR_print_errors_fp(stderr); } CMS_ContentInfo_free(cms); BIO_free(in); BIO_free(out); return ret; }
./openssl/demos/cms/cms_ver.c
/* * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Simple S/MIME verification example */ #include <openssl/pem.h> #include <openssl/cms.h> #include <openssl/err.h> /* * print any signingTime attributes. * signingTime is when each party purportedly signed the message. */ static void print_signingTime(CMS_ContentInfo *cms) { STACK_OF(CMS_SignerInfo) *sis; CMS_SignerInfo *si; X509_ATTRIBUTE *attr; ASN1_TYPE *t; ASN1_UTCTIME *utctime; ASN1_GENERALIZEDTIME *gtime; BIO *b; int i, loc; b = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT); sis = CMS_get0_SignerInfos(cms); for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) { si = sk_CMS_SignerInfo_value(sis, i); loc = CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1); attr = CMS_signed_get_attr(si, loc); t = X509_ATTRIBUTE_get0_type(attr, 0); if (t == NULL) continue; switch (t->type) { case V_ASN1_UTCTIME: utctime = t->value.utctime; ASN1_UTCTIME_print(b, utctime); break; case V_ASN1_GENERALIZEDTIME: gtime = t->value.generalizedtime; ASN1_GENERALIZEDTIME_print(b, gtime); break; default: fprintf(stderr, "unrecognized signingTime type\n"); break; } BIO_printf(b, ": signingTime from SignerInfo %i\n", i); } BIO_free(b); return; } int main(int argc, char **argv) { BIO *in = NULL, *out = NULL, *tbio = NULL, *cont = NULL; X509_STORE *st = NULL; X509 *cacert = NULL; CMS_ContentInfo *cms = NULL; int ret = EXIT_FAILURE; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); /* Set up trusted CA certificate store */ st = X509_STORE_new(); if (st == NULL) goto err; /* Read in CA certificate */ tbio = BIO_new_file("cacert.pem", "r"); if (tbio == NULL) goto err; cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (cacert == NULL) goto err; if (!X509_STORE_add_cert(st, cacert)) goto err; /* Open message being verified */ in = BIO_new_file("smout.txt", "r"); if (in == NULL) goto err; /* parse message */ cms = SMIME_read_CMS(in, &cont); if (cms == NULL) goto err; print_signingTime(cms); /* File to output verified content to */ out = BIO_new_file("smver.txt", "w"); if (out == NULL) goto err; if (!CMS_verify(cms, NULL, st, cont, out, 0)) { fprintf(stderr, "Verification Failure\n"); goto err; } printf("Verification Successful\n"); ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Verifying Data\n"); ERR_print_errors_fp(stderr); } X509_STORE_free(st); CMS_ContentInfo_free(cms); X509_free(cacert); BIO_free(in); BIO_free(out); BIO_free(tbio); return ret; }
./openssl/demos/cms/cms_uncomp.c
/* * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Simple S/MIME uncompression example */ #include <openssl/pem.h> #include <openssl/cms.h> #include <openssl/err.h> int main(int argc, char **argv) { BIO *in = NULL, *out = NULL; CMS_ContentInfo *cms = NULL; int ret = EXIT_FAILURE; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); /* Open compressed content */ in = BIO_new_file("smcomp.txt", "r"); if (!in) goto err; /* Sign content */ cms = SMIME_read_CMS(in, NULL); if (!cms) goto err; out = BIO_new_file("smuncomp.txt", "w"); if (!out) goto err; /* Uncompress S/MIME message */ if (!CMS_uncompress(cms, out, NULL, 0)) goto err; ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Uncompressing Data\n"); ERR_print_errors_fp(stderr); } CMS_ContentInfo_free(cms); BIO_free(in); BIO_free(out); return ret; }
./openssl/demos/cms/cms_ddec.c
/* * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * S/MIME detached data decrypt example: rarely done but should the need * arise this is an example.... */ #include <openssl/pem.h> #include <openssl/cms.h> #include <openssl/err.h> int main(int argc, char **argv) { BIO *in = NULL, *out = NULL, *tbio = NULL, *dcont = NULL; X509 *rcert = NULL; EVP_PKEY *rkey = NULL; CMS_ContentInfo *cms = NULL; int ret = EXIT_FAILURE; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); /* Read in recipient certificate and private key */ tbio = BIO_new_file("signer.pem", "r"); if (!tbio) goto err; rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (BIO_reset(tbio) < 0) goto err; rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL); if (!rcert || !rkey) goto err; /* Open PEM file containing enveloped data */ in = BIO_new_file("smencr.pem", "r"); if (!in) goto err; /* Parse PEM content */ cms = PEM_read_bio_CMS(in, NULL, 0, NULL); if (!cms) goto err; /* Open file containing detached content */ dcont = BIO_new_file("smencr.out", "rb"); if (!in) goto err; out = BIO_new_file("encrout.txt", "w"); if (!out) goto err; /* Decrypt S/MIME message */ if (!CMS_decrypt(cms, rkey, rcert, dcont, out, 0)) goto err; ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Decrypting Data\n"); ERR_print_errors_fp(stderr); } CMS_ContentInfo_free(cms); X509_free(rcert); EVP_PKEY_free(rkey); BIO_free(in); BIO_free(out); BIO_free(tbio); BIO_free(dcont); return ret; }
./openssl/demos/cms/cms_sign2.c
/* * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* S/MIME signing example: 2 signers */ #include <openssl/pem.h> #include <openssl/cms.h> #include <openssl/err.h> int main(int argc, char **argv) { BIO *in = NULL, *out = NULL, *tbio = NULL; X509 *scert = NULL, *scert2 = NULL; EVP_PKEY *skey = NULL, *skey2 = NULL; CMS_ContentInfo *cms = NULL; int ret = EXIT_FAILURE; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); tbio = BIO_new_file("signer.pem", "r"); if (!tbio) goto err; scert = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (BIO_reset(tbio) < 0) goto err; skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL); BIO_free(tbio); tbio = BIO_new_file("signer2.pem", "r"); if (!tbio) goto err; scert2 = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (BIO_reset(tbio) < 0) goto err; skey2 = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL); if (!scert2 || !skey2) goto err; in = BIO_new_file("sign.txt", "r"); if (!in) goto err; cms = CMS_sign(NULL, NULL, NULL, in, CMS_STREAM | CMS_PARTIAL); if (!cms) goto err; /* Add each signer in turn */ if (!CMS_add1_signer(cms, scert, skey, NULL, 0)) goto err; if (!CMS_add1_signer(cms, scert2, skey2, NULL, 0)) goto err; out = BIO_new_file("smout.txt", "w"); if (!out) goto err; /* NB: content included and finalized by SMIME_write_CMS */ if (!SMIME_write_CMS(out, cms, in, CMS_STREAM)) goto err; printf("Signing Successful\n"); ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Signing Data\n"); ERR_print_errors_fp(stderr); } CMS_ContentInfo_free(cms); X509_free(scert); EVP_PKEY_free(skey); X509_free(scert2); EVP_PKEY_free(skey2); BIO_free(in); BIO_free(out); BIO_free(tbio); return ret; }
./openssl/demos/smime/smdec.c
/* * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Simple S/MIME signing example */ #include <openssl/pem.h> #include <openssl/pkcs7.h> #include <openssl/err.h> int main(int argc, char **argv) { BIO *in = NULL, *out = NULL, *tbio = NULL; X509 *rcert = NULL; EVP_PKEY *rkey = NULL; PKCS7 *p7 = NULL; int ret = EXIT_FAILURE; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); /* Read in recipient certificate and private key */ tbio = BIO_new_file("signer.pem", "r"); if (!tbio) goto err; rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (BIO_reset(tbio) < 0) goto err; rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL); if (!rcert || !rkey) goto err; /* Open content being signed */ in = BIO_new_file("smencr.txt", "r"); if (!in) goto err; /* Sign content */ p7 = SMIME_read_PKCS7(in, NULL); if (!p7) goto err; out = BIO_new_file("encrout.txt", "w"); if (!out) goto err; /* Decrypt S/MIME message */ if (!PKCS7_decrypt(p7, rkey, rcert, out, 0)) goto err; printf("Success\n"); ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Signing Data\n"); ERR_print_errors_fp(stderr); } PKCS7_free(p7); X509_free(rcert); EVP_PKEY_free(rkey); BIO_free(in); BIO_free(out); BIO_free(tbio); return ret; }
./openssl/demos/smime/smenc.c
/* * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Simple S/MIME encrypt example */ #include <openssl/pem.h> #include <openssl/pkcs7.h> #include <openssl/err.h> int main(int argc, char **argv) { BIO *in = NULL, *out = NULL, *tbio = NULL; X509 *rcert = NULL; STACK_OF(X509) *recips = NULL; PKCS7 *p7 = NULL; int ret = EXIT_FAILURE; /* * for streaming set PKCS7_STREAM */ int flags = PKCS7_STREAM; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); /* Read in recipient certificate */ tbio = BIO_new_file("signer.pem", "r"); if (!tbio) goto err; rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (!rcert) goto err; /* Create recipient STACK and add recipient cert to it */ recips = sk_X509_new_null(); if (!recips || !sk_X509_push(recips, rcert)) goto err; /* * OSSL_STACK_OF_X509_free() will free up recipient STACK and its contents * so set rcert to NULL so it isn't freed up twice. */ rcert = NULL; /* Open content being encrypted */ in = BIO_new_file("encr.txt", "r"); if (!in) goto err; /* encrypt content */ p7 = PKCS7_encrypt(recips, in, EVP_des_ede3_cbc(), flags); if (!p7) goto err; out = BIO_new_file("smencr.txt", "w"); if (!out) goto err; /* Write out S/MIME message */ if (!SMIME_write_PKCS7(out, p7, in, flags)) goto err; printf("Success\n"); ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Encrypting Data\n"); ERR_print_errors_fp(stderr); } PKCS7_free(p7); X509_free(rcert); OSSL_STACK_OF_X509_free(recips); BIO_free(in); BIO_free(out); BIO_free(tbio); return ret; }
./openssl/demos/smime/smsign.c
/* * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Simple S/MIME signing example */ #include <openssl/pem.h> #include <openssl/pkcs7.h> #include <openssl/err.h> int main(int argc, char **argv) { BIO *in = NULL, *out = NULL, *tbio = NULL; X509 *scert = NULL; EVP_PKEY *skey = NULL; PKCS7 *p7 = NULL; int ret = EXIT_FAILURE; /* * For simple S/MIME signing use PKCS7_DETACHED. * for streaming detached set PKCS7_DETACHED|PKCS7_STREAM for streaming * non-detached set PKCS7_STREAM */ int flags = PKCS7_DETACHED | PKCS7_STREAM; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); /* Read in signer certificate and private key */ tbio = BIO_new_file("signer.pem", "r"); if (!tbio) goto err; scert = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (BIO_reset(tbio) < 0) goto err; skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL); if (!scert || !skey) goto err; /* Open content being signed */ in = BIO_new_file("sign.txt", "r"); if (!in) goto err; /* Sign content */ p7 = PKCS7_sign(scert, skey, NULL, in, flags); if (!p7) goto err; out = BIO_new_file("smout.txt", "w"); if (!out) goto err; if (!(flags & PKCS7_STREAM)) { if (BIO_reset(in) < 0) goto err; } /* Write out S/MIME message */ if (!SMIME_write_PKCS7(out, p7, in, flags)) goto err; printf("Success\n"); ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Signing Data\n"); ERR_print_errors_fp(stderr); } PKCS7_free(p7); X509_free(scert); EVP_PKEY_free(skey); BIO_free(in); BIO_free(out); BIO_free(tbio); return ret; }
./openssl/demos/smime/smver.c
/* * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Simple S/MIME verification example */ #include <openssl/pem.h> #include <openssl/pkcs7.h> #include <openssl/err.h> int main(int argc, char **argv) { BIO *in = NULL, *out = NULL, *tbio = NULL, *cont = NULL; X509_STORE *st = NULL; X509 *cacert = NULL; PKCS7 *p7 = NULL; int ret = EXIT_FAILURE; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); /* Set up trusted CA certificate store */ st = X509_STORE_new(); if (st == NULL) goto err; /* Read in signer certificate and private key */ tbio = BIO_new_file("cacert.pem", "r"); if (tbio == NULL) goto err; cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (cacert == NULL) goto err; if (!X509_STORE_add_cert(st, cacert)) goto err; /* Open content being signed */ in = BIO_new_file("smout.txt", "r"); if (in == NULL) goto err; /* Sign content */ p7 = SMIME_read_PKCS7(in, &cont); if (p7 == NULL) goto err; /* File to output verified content to */ out = BIO_new_file("smver.txt", "w"); if (out == NULL) goto err; if (!PKCS7_verify(p7, NULL, st, cont, out, 0)) { fprintf(stderr, "Verification Failure\n"); goto err; } printf("Verification Successful\n"); ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Verifying Data\n"); ERR_print_errors_fp(stderr); } X509_STORE_free(st); PKCS7_free(p7); X509_free(cacert); BIO_free(in); BIO_free(out); BIO_free(tbio); return ret; }
./openssl/demos/smime/smsign2.c
/* * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* S/MIME signing example: 2 signers */ #include <openssl/pem.h> #include <openssl/pkcs7.h> #include <openssl/err.h> int main(int argc, char **argv) { BIO *in = NULL, *out = NULL, *tbio = NULL; X509 *scert = NULL, *scert2 = NULL; EVP_PKEY *skey = NULL, *skey2 = NULL; PKCS7 *p7 = NULL; int ret = EXIT_FAILURE; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); tbio = BIO_new_file("signer.pem", "r"); if (!tbio) goto err; scert = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (BIO_reset(tbio) < 0) goto err; skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL); BIO_free(tbio); tbio = BIO_new_file("signer2.pem", "r"); if (!tbio) goto err; scert2 = PEM_read_bio_X509(tbio, NULL, 0, NULL); if (BIO_reset(tbio) < 0) goto err; skey2 = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL); if (!scert2 || !skey2) goto err; in = BIO_new_file("sign.txt", "r"); if (!in) goto err; p7 = PKCS7_sign(NULL, NULL, NULL, in, PKCS7_STREAM | PKCS7_PARTIAL); if (!p7) goto err; /* Add each signer in turn */ if (!PKCS7_sign_add_signer(p7, scert, skey, NULL, 0)) goto err; if (!PKCS7_sign_add_signer(p7, scert2, skey2, NULL, 0)) goto err; out = BIO_new_file("smout.txt", "w"); if (!out) goto err; /* NB: content included and finalized by SMIME_write_PKCS7 */ if (!SMIME_write_PKCS7(out, p7, in, PKCS7_STREAM)) goto err; printf("Success\n"); ret = EXIT_SUCCESS; err: if (ret != EXIT_SUCCESS) { fprintf(stderr, "Error Signing Data\n"); ERR_print_errors_fp(stderr); } PKCS7_free(p7); X509_free(scert); EVP_PKEY_free(skey); X509_free(scert2); EVP_PKEY_free(skey2); BIO_free(in); BIO_free(out); BIO_free(tbio); return ret; }
./openssl/demos/cipher/aesgcm.c
/* * Copyright 2012-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * Simple AES GCM authenticated encryption with additional data (AEAD) * demonstration program. */ #include <stdio.h> #include <openssl/err.h> #include <openssl/bio.h> #include <openssl/evp.h> #include <openssl/core_names.h> /* AES-GCM test data obtained from NIST public test vectors */ /* AES key */ static const unsigned char gcm_key[] = { 0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66, 0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69, 0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f }; /* Unique initialisation vector */ static const unsigned char gcm_iv[] = { 0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84 }; /* Example plaintext to encrypt */ static const unsigned char gcm_pt[] = { 0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea, 0xcc, 0x2b, 0xf2, 0xa5 }; /* * Example of Additional Authenticated Data (AAD), i.e. unencrypted data * which can be authenticated using the generated Tag value. */ static const unsigned char gcm_aad[] = { 0x4d, 0x23, 0xc3, 0xce, 0xc3, 0x34, 0xb4, 0x9b, 0xdb, 0x37, 0x0c, 0x43, 0x7f, 0xec, 0x78, 0xde }; /* Expected ciphertext value */ static const unsigned char gcm_ct[] = { 0xf7, 0x26, 0x44, 0x13, 0xa8, 0x4c, 0x0e, 0x7c, 0xd5, 0x36, 0x86, 0x7e, 0xb9, 0xf2, 0x17, 0x36 }; /* Expected AEAD Tag value */ static const unsigned char gcm_tag[] = { 0x67, 0xba, 0x05, 0x10, 0x26, 0x2a, 0xe4, 0x87, 0xd7, 0x37, 0xee, 0x62, 0x98, 0xf7, 0x7e, 0x0c }; /* * A library context and property query can be used to select & filter * algorithm implementations. If they are NULL then the default library * context and properties are used. */ OSSL_LIB_CTX *libctx = NULL; const char *propq = NULL; int aes_gcm_encrypt(void) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int outlen, tmplen; size_t gcm_ivlen = sizeof(gcm_iv); unsigned char outbuf[1024]; unsigned char outtag[16]; OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; printf("AES GCM Encrypt:\n"); printf("Plaintext:\n"); BIO_dump_fp(stdout, gcm_pt, sizeof(gcm_pt)); /* Create a context for the encrypt operation */ if ((ctx = EVP_CIPHER_CTX_new()) == NULL) goto err; /* Fetch the cipher implementation */ if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-GCM", propq)) == NULL) goto err; /* Set IV length if default 96 bits is not appropriate */ params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, &gcm_ivlen); /* * Initialise an encrypt operation with the cipher/mode, key, IV and * IV length parameter. * For demonstration purposes the IV is being set here. In a compliant * application the IV would be generated internally so the iv passed in * would be NULL. */ if (!EVP_EncryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params)) goto err; /* Zero or more calls to specify any AAD */ if (!EVP_EncryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad))) goto err; /* Encrypt plaintext */ if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, gcm_pt, sizeof(gcm_pt))) goto err; /* Output encrypted block */ printf("Ciphertext:\n"); BIO_dump_fp(stdout, outbuf, outlen); /* Finalise: note get no output for GCM */ if (!EVP_EncryptFinal_ex(ctx, outbuf, &tmplen)) goto err; /* Get tag */ params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, outtag, 16); if (!EVP_CIPHER_CTX_get_params(ctx, params)) goto err; /* Output tag */ printf("Tag:\n"); BIO_dump_fp(stdout, outtag, 16); ret = 1; err: if (!ret) ERR_print_errors_fp(stderr); EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } int aes_gcm_decrypt(void) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int outlen, rv; size_t gcm_ivlen = sizeof(gcm_iv); unsigned char outbuf[1024]; OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; printf("AES GCM Decrypt:\n"); printf("Ciphertext:\n"); BIO_dump_fp(stdout, gcm_ct, sizeof(gcm_ct)); if ((ctx = EVP_CIPHER_CTX_new()) == NULL) goto err; /* Fetch the cipher implementation */ if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-GCM", propq)) == NULL) goto err; /* Set IV length if default 96 bits is not appropriate */ params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, &gcm_ivlen); /* * Initialise an encrypt operation with the cipher/mode, key, IV and * IV length parameter. */ if (!EVP_DecryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params)) goto err; /* Zero or more calls to specify any AAD */ if (!EVP_DecryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad))) goto err; /* Decrypt plaintext */ if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, gcm_ct, sizeof(gcm_ct))) goto err; /* Output decrypted block */ printf("Plaintext:\n"); BIO_dump_fp(stdout, outbuf, outlen); /* Set expected tag value. */ params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, (void*)gcm_tag, sizeof(gcm_tag)); if (!EVP_CIPHER_CTX_set_params(ctx, params)) goto err; /* Finalise: note get no output for GCM */ rv = EVP_DecryptFinal_ex(ctx, outbuf, &outlen); /* * Print out return value. If this is not successful authentication * failed and plaintext is not trustworthy. */ printf("Tag Verify %s\n", rv > 0 ? "Successful!" : "Failed!"); ret = 1; err: if (!ret) ERR_print_errors_fp(stderr); EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } int main(int argc, char **argv) { if (!aes_gcm_encrypt()) return EXIT_FAILURE; if (!aes_gcm_decrypt()) return EXIT_FAILURE; return EXIT_SUCCESS; }
./openssl/demos/cipher/aeskeywrap.c
/* * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * Simple aes wrap encryption demonstration program. */ #include <stdio.h> #include <openssl/err.h> #include <openssl/bio.h> #include <openssl/evp.h> #include <openssl/crypto.h> #include <openssl/core_names.h> /* aes key */ static const unsigned char wrap_key[] = { 0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66, 0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69, 0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f }; /* Unique initialisation vector */ static const unsigned char wrap_iv[] = { 0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84, 0x99, 0xaa, 0x3e, 0x68, }; /* Example plaintext to encrypt */ static const unsigned char wrap_pt[] = { 0xad, 0x4f, 0xc9, 0xfc, 0x77, 0x69, 0xc9, 0xea, 0xfc, 0xdf, 0x00, 0xac, 0x34, 0xec, 0x40, 0xbc, 0x28, 0x3f, 0xa4, 0x5e, 0xd8, 0x99, 0xe4, 0x5d, 0x5e, 0x7a, 0xc4, 0xe6, 0xca, 0x7b, 0xa5, 0xb7, }; /* Expected ciphertext value */ static const unsigned char wrap_ct[] = { 0x97, 0x99, 0x55, 0xca, 0xf6, 0x3e, 0x95, 0x54, 0x39, 0xd6, 0xaf, 0x63, 0xff, 0x2c, 0xe3, 0x96, 0xf7, 0x0d, 0x2c, 0x9c, 0xc7, 0x43, 0xc0, 0xb6, 0x31, 0x43, 0xb9, 0x20, 0xac, 0x6b, 0xd3, 0x67, 0xad, 0x01, 0xaf, 0xa7, 0x32, 0x74, 0x26, 0x92, }; /* * A library context and property query can be used to select & filter * algorithm implementations. If they are NULL then the default library * context and properties are used. */ OSSL_LIB_CTX *libctx = NULL; const char *propq = NULL; int aes_wrap_encrypt(void) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int outlen, tmplen; unsigned char outbuf[1024]; printf("aes wrap Encrypt:\n"); printf("Plaintext:\n"); BIO_dump_fp(stdout, wrap_pt, sizeof(wrap_pt)); /* Create a context for the encrypt operation */ if ((ctx = EVP_CIPHER_CTX_new()) == NULL) goto err; EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); /* Fetch the cipher implementation */ if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-WRAP", propq)) == NULL) goto err; /* * Initialise an encrypt operation with the cipher/mode, key and IV. * We are not setting any custom params so let params be just NULL. */ if (!EVP_EncryptInit_ex2(ctx, cipher, wrap_key, wrap_iv, /* params */ NULL)) goto err; /* Encrypt plaintext */ if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, wrap_pt, sizeof(wrap_pt))) goto err; /* Finalise: there can be some additional output from padding */ if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) goto err; outlen += tmplen; /* Output encrypted block */ printf("Ciphertext (outlen:%d):\n", outlen); BIO_dump_fp(stdout, outbuf, outlen); if (sizeof(wrap_ct) == outlen && !CRYPTO_memcmp(outbuf, wrap_ct, outlen)) printf("Final ciphertext matches expected ciphertext\n"); else printf("Final ciphertext differs from expected ciphertext\n"); ret = 1; err: if (!ret) ERR_print_errors_fp(stderr); EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } int aes_wrap_decrypt(void) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int outlen, tmplen; unsigned char outbuf[1024]; printf("aes wrap Decrypt:\n"); printf("Ciphertext:\n"); BIO_dump_fp(stdout, wrap_ct, sizeof(wrap_ct)); if ((ctx = EVP_CIPHER_CTX_new()) == NULL) goto err; EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); /* Fetch the cipher implementation */ if ((cipher = EVP_CIPHER_fetch(libctx, "aes-256-wrap", propq)) == NULL) goto err; /* * Initialise an encrypt operation with the cipher/mode, key and IV. * We are not setting any custom params so let params be just NULL. */ if (!EVP_DecryptInit_ex2(ctx, cipher, wrap_key, wrap_iv, /* params */ NULL)) goto err; /* Decrypt plaintext */ if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, wrap_ct, sizeof(wrap_ct))) goto err; /* Finalise: there can be some additional output from padding */ if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen)) goto err; outlen += tmplen; /* Output decrypted block */ printf("Plaintext (outlen:%d):\n", outlen); BIO_dump_fp(stdout, outbuf, outlen); if (sizeof(wrap_pt) == outlen && !CRYPTO_memcmp(outbuf, wrap_pt, outlen)) printf("Final plaintext matches original plaintext\n"); else printf("Final plaintext differs from original plaintext\n"); ret = 1; err: if (!ret) ERR_print_errors_fp(stderr); EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } int main(int argc, char **argv) { if (!aes_wrap_encrypt()) return EXIT_FAILURE; if (!aes_wrap_decrypt()) return EXIT_FAILURE; return EXIT_SUCCESS; }
./openssl/demos/cipher/aesccm.c
/* * Copyright 2013-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * Simple AES CCM authenticated encryption with additional data (AEAD) * demonstration program. */ #include <stdio.h> #include <openssl/err.h> #include <openssl/bio.h> #include <openssl/evp.h> #include <openssl/core_names.h> /* AES-CCM test data obtained from NIST public test vectors */ /* AES key */ static const unsigned char ccm_key[] = { 0xce, 0xb0, 0x09, 0xae, 0xa4, 0x45, 0x44, 0x51, 0xfe, 0xad, 0xf0, 0xe6, 0xb3, 0x6f, 0x45, 0x55, 0x5d, 0xd0, 0x47, 0x23, 0xba, 0xa4, 0x48, 0xe8 }; /* Unique nonce to be used for this message */ static const unsigned char ccm_nonce[] = { 0x76, 0x40, 0x43, 0xc4, 0x94, 0x60, 0xb7 }; /* * Example of Additional Authenticated Data (AAD), i.e. unencrypted data * which can be authenticated using the generated Tag value. */ static const unsigned char ccm_adata[] = { 0x6e, 0x80, 0xdd, 0x7f, 0x1b, 0xad, 0xf3, 0xa1, 0xc9, 0xab, 0x25, 0xc7, 0x5f, 0x10, 0xbd, 0xe7, 0x8c, 0x23, 0xfa, 0x0e, 0xb8, 0xf9, 0xaa, 0xa5, 0x3a, 0xde, 0xfb, 0xf4, 0xcb, 0xf7, 0x8f, 0xe4 }; /* Example plaintext to encrypt */ static const unsigned char ccm_pt[] = { 0xc8, 0xd2, 0x75, 0xf9, 0x19, 0xe1, 0x7d, 0x7f, 0xe6, 0x9c, 0x2a, 0x1f, 0x58, 0x93, 0x9d, 0xfe, 0x4d, 0x40, 0x37, 0x91, 0xb5, 0xdf, 0x13, 0x10 }; /* Expected ciphertext value */ static const unsigned char ccm_ct[] = { 0x8a, 0x0f, 0x3d, 0x82, 0x29, 0xe4, 0x8e, 0x74, 0x87, 0xfd, 0x95, 0xa2, 0x8a, 0xd3, 0x92, 0xc8, 0x0b, 0x36, 0x81, 0xd4, 0xfb, 0xc7, 0xbb, 0xfd }; /* Expected AEAD Tag value */ static const unsigned char ccm_tag[] = { 0x2d, 0xd6, 0xef, 0x1c, 0x45, 0xd4, 0xcc, 0xb7, 0x23, 0xdc, 0x07, 0x44, 0x14, 0xdb, 0x50, 0x6d }; /* * A library context and property query can be used to select & filter * algorithm implementations. If they are NULL then the default library * context and properties are used. */ OSSL_LIB_CTX *libctx = NULL; const char *propq = NULL; int aes_ccm_encrypt(void) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int outlen, tmplen; size_t ccm_nonce_len = sizeof(ccm_nonce); size_t ccm_tag_len = sizeof(ccm_tag); unsigned char outbuf[1024]; unsigned char outtag[16]; OSSL_PARAM params[3] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END }; printf("AES CCM Encrypt:\n"); printf("Plaintext:\n"); BIO_dump_fp(stdout, ccm_pt, sizeof(ccm_pt)); /* Create a context for the encrypt operation */ if ((ctx = EVP_CIPHER_CTX_new()) == NULL) goto err; /* Fetch the cipher implementation */ if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL) goto err; /* Set nonce length if default 96 bits is not appropriate */ params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, &ccm_nonce_len); /* Set tag length */ params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, ccm_tag_len); /* * Initialise encrypt operation with the cipher & mode, * nonce length and tag length parameters. */ if (!EVP_EncryptInit_ex2(ctx, cipher, NULL, NULL, params)) goto err; /* Initialise key and nonce */ if (!EVP_EncryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce)) goto err; /* Set plaintext length: only needed if AAD is used */ if (!EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_pt))) goto err; /* Zero or one call to specify any AAD */ if (!EVP_EncryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata))) goto err; /* Encrypt plaintext: can only be called once */ if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, ccm_pt, sizeof(ccm_pt))) goto err; /* Output encrypted block */ printf("Ciphertext:\n"); BIO_dump_fp(stdout, outbuf, outlen); /* Finalise: note get no output for CCM */ if (!EVP_EncryptFinal_ex(ctx, NULL, &tmplen)) goto err; /* Get tag */ params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, outtag, ccm_tag_len); params[1] = OSSL_PARAM_construct_end(); if (!EVP_CIPHER_CTX_get_params(ctx, params)) goto err; /* Output tag */ printf("Tag:\n"); BIO_dump_fp(stdout, outtag, ccm_tag_len); ret = 1; err: if (!ret) ERR_print_errors_fp(stderr); EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } int aes_ccm_decrypt(void) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int outlen, rv; unsigned char outbuf[1024]; size_t ccm_nonce_len = sizeof(ccm_nonce); OSSL_PARAM params[3] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END }; printf("AES CCM Decrypt:\n"); printf("Ciphertext:\n"); BIO_dump_fp(stdout, ccm_ct, sizeof(ccm_ct)); if ((ctx = EVP_CIPHER_CTX_new()) == NULL) goto err; /* Fetch the cipher implementation */ if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL) goto err; /* Set nonce length if default 96 bits is not appropriate */ params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, &ccm_nonce_len); /* Set tag length */ params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, (unsigned char *)ccm_tag, sizeof(ccm_tag)); /* * Initialise decrypt operation with the cipher & mode, * nonce length and expected tag parameters. */ if (!EVP_DecryptInit_ex2(ctx, cipher, NULL, NULL, params)) goto err; /* Specify key and IV */ if (!EVP_DecryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce)) goto err; /* Set ciphertext length: only needed if we have AAD */ if (!EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_ct))) goto err; /* Zero or one call to specify any AAD */ if (!EVP_DecryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata))) goto err; /* Decrypt plaintext, verify tag: can only be called once */ rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, ccm_ct, sizeof(ccm_ct)); /* Output decrypted block: if tag verify failed we get nothing */ if (rv > 0) { printf("Tag verify successful!\nPlaintext:\n"); BIO_dump_fp(stdout, outbuf, outlen); } else { printf("Tag verify failed!\nPlaintext not available\n"); goto err; } ret = 1; err: if (!ret) ERR_print_errors_fp(stderr); EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } int main(int argc, char **argv) { if (!aes_ccm_encrypt()) return EXIT_FAILURE; if (!aes_ccm_decrypt()) return EXIT_FAILURE; return EXIT_SUCCESS; }
./openssl/demos/cipher/ariacbc.c
/* * Copyright 2012-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * Simple ARIA CBC encryption demonstration program. */ #include <stdio.h> #include <openssl/err.h> #include <openssl/bio.h> #include <openssl/evp.h> #include <openssl/crypto.h> #include <openssl/core_names.h> /* ARIA key */ static const unsigned char cbc_key[] = { 0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66, 0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69, 0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f }; /* Unique initialisation vector */ static const unsigned char cbc_iv[] = { 0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84, 0x99, 0xaa, 0x3e, 0x68, }; /* Example plaintext to encrypt */ static const unsigned char cbc_pt[] = { 0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea, 0xcc, 0x2b, 0xf2, 0xa5 }; /* Expected ciphertext value */ static const unsigned char cbc_ct[] = { 0x9a, 0x44, 0xe6, 0x85, 0x94, 0x26, 0xff, 0x30, 0x03, 0xd3, 0x7e, 0xc6, 0xb5, 0x4a, 0x09, 0x66, 0x39, 0x28, 0xf3, 0x67, 0x14, 0xbc, 0xe8, 0xe2, 0xcf, 0x31, 0xb8, 0x60, 0x42, 0x72, 0x6d, 0xc8 }; /* * A library context and property query can be used to select & filter * algorithm implementations. If they are NULL then the default library * context and properties are used. */ OSSL_LIB_CTX *libctx = NULL; const char *propq = NULL; int aria_cbc_encrypt(void) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int outlen, tmplen; unsigned char outbuf[1024]; printf("ARIA CBC Encrypt:\n"); printf("Plaintext:\n"); BIO_dump_fp(stdout, cbc_pt, sizeof(cbc_pt)); /* Create a context for the encrypt operation */ if ((ctx = EVP_CIPHER_CTX_new()) == NULL) goto err; /* Fetch the cipher implementation */ if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL) goto err; /* * Initialise an encrypt operation with the cipher/mode, key and IV. * We are not setting any custom params so let params be just NULL. */ if (!EVP_EncryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL)) goto err; /* Encrypt plaintext */ if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, cbc_pt, sizeof(cbc_pt))) goto err; /* Finalise: there can be some additional output from padding */ if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) goto err; outlen += tmplen; /* Output encrypted block */ printf("Ciphertext (outlen:%d):\n", outlen); BIO_dump_fp(stdout, outbuf, outlen); if (sizeof(cbc_ct) == outlen && !CRYPTO_memcmp(outbuf, cbc_ct, outlen)) printf("Final ciphertext matches expected ciphertext\n"); else printf("Final ciphertext differs from expected ciphertext\n"); ret = 1; err: if (!ret) ERR_print_errors_fp(stderr); EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } int aria_cbc_decrypt(void) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int outlen, tmplen; unsigned char outbuf[1024]; printf("ARIA CBC Decrypt:\n"); printf("Ciphertext:\n"); BIO_dump_fp(stdout, cbc_ct, sizeof(cbc_ct)); if ((ctx = EVP_CIPHER_CTX_new()) == NULL) goto err; /* Fetch the cipher implementation */ if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL) goto err; /* * Initialise an encrypt operation with the cipher/mode, key and IV. * We are not setting any custom params so let params be just NULL. */ if (!EVP_DecryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL)) goto err; /* Decrypt plaintext */ if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, cbc_ct, sizeof(cbc_ct))) goto err; /* Finalise: there can be some additional output from padding */ if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen)) goto err; outlen += tmplen; /* Output decrypted block */ printf("Plaintext (outlen:%d):\n", outlen); BIO_dump_fp(stdout, outbuf, outlen); if (sizeof(cbc_pt) == outlen && !CRYPTO_memcmp(outbuf, cbc_pt, outlen)) printf("Final plaintext matches original plaintext\n"); else printf("Final plaintext differs from original plaintext\n"); ret = 1; err: if (!ret) ERR_print_errors_fp(stderr); EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } int main(int argc, char **argv) { if (!aria_cbc_encrypt()) return EXIT_FAILURE; if (!aria_cbc_decrypt()) return EXIT_FAILURE; return EXIT_SUCCESS; }
./openssl/demos/pkcs12/pkwrite.c
/* * Copyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <stdlib.h> #include <openssl/pem.h> #include <openssl/err.h> #include <openssl/pkcs12.h> /* Simple PKCS#12 file creator */ int main(int argc, char **argv) { FILE *fp; EVP_PKEY *pkey; X509 *cert; PKCS12 *p12; if (argc != 5) { fprintf(stderr, "Usage: pkwrite infile password name p12file\n"); exit(EXIT_FAILURE); } OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); if ((fp = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "Error opening file %s\n", argv[1]); exit(EXIT_FAILURE); } cert = PEM_read_X509(fp, NULL, NULL, NULL); rewind(fp); pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL); fclose(fp); p12 = PKCS12_create(argv[2], argv[3], pkey, cert, NULL, 0, 0, 0, 0, 0); if (!p12) { fprintf(stderr, "Error creating PKCS#12 structure\n"); ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } if ((fp = fopen(argv[4], "wb")) == NULL) { fprintf(stderr, "Error opening file %s\n", argv[4]); ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } i2d_PKCS12_fp(fp, p12); PKCS12_free(p12); fclose(fp); return EXIT_SUCCESS; }
./openssl/demos/pkcs12/pkread.c
/* * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <stdlib.h> #include <openssl/pem.h> #include <openssl/err.h> #include <openssl/pkcs12.h> /* Simple PKCS#12 file reader */ static char *find_friendly_name(PKCS12 *p12) { STACK_OF(PKCS7) *safes; int n, m; char *name = NULL; PKCS7 *safe; STACK_OF(PKCS12_SAFEBAG) *bags; PKCS12_SAFEBAG *bag; if ((safes = PKCS12_unpack_authsafes(p12)) == NULL) return NULL; for (n = 0; n < sk_PKCS7_num(safes) && name == NULL; n++) { safe = sk_PKCS7_value(safes, n); if (OBJ_obj2nid(safe->type) != NID_pkcs7_data || (bags = PKCS12_unpack_p7data(safe)) == NULL) continue; for (m = 0; m < sk_PKCS12_SAFEBAG_num(bags) && name == NULL; m++) { bag = sk_PKCS12_SAFEBAG_value(bags, m); name = PKCS12_get_friendlyname(bag); } sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); } sk_PKCS7_pop_free(safes, PKCS7_free); return name; } int main(int argc, char **argv) { FILE *fp; EVP_PKEY *pkey = NULL; X509 *cert = NULL; STACK_OF(X509) *ca = NULL; PKCS12 *p12 = NULL; char *name = NULL; int i, ret = EXIT_FAILURE; if (argc != 4) { fprintf(stderr, "Usage: pkread p12file password opfile\n"); exit(EXIT_FAILURE); } if ((fp = fopen(argv[1], "rb")) == NULL) { fprintf(stderr, "Error opening file %s\n", argv[1]); exit(EXIT_FAILURE); } p12 = d2i_PKCS12_fp(fp, NULL); fclose(fp); if (p12 == NULL) { fprintf(stderr, "Error reading PKCS#12 file\n"); ERR_print_errors_fp(stderr); goto err; } if (!PKCS12_parse(p12, argv[2], &pkey, &cert, &ca)) { fprintf(stderr, "Error parsing PKCS#12 file\n"); ERR_print_errors_fp(stderr); goto err; } name = find_friendly_name(p12); PKCS12_free(p12); if ((fp = fopen(argv[3], "w")) == NULL) { fprintf(stderr, "Error opening file %s\n", argv[3]); goto err; } if (name != NULL) fprintf(fp, "***Friendly Name***\n%s\n", name); if (pkey != NULL) { fprintf(fp, "***Private Key***\n"); PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL); } if (cert != NULL) { fprintf(fp, "***User Certificate***\n"); PEM_write_X509_AUX(fp, cert); } if (ca != NULL && sk_X509_num(ca) > 0) { fprintf(fp, "***Other Certificates***\n"); for (i = 0; i < sk_X509_num(ca); i++) PEM_write_X509_AUX(fp, sk_X509_value(ca, i)); } fclose(fp); ret = EXIT_SUCCESS; err: OPENSSL_free(name); X509_free(cert); EVP_PKEY_free(pkey); OSSL_STACK_OF_X509_free(ca); return ret; }
./openssl/demos/kdf/scrypt.c
/* * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <openssl/core_names.h> #include <openssl/crypto.h> #include <openssl/kdf.h> #include <openssl/obj_mac.h> #include <openssl/params.h> /* * test vector from * https://datatracker.ietf.org/doc/html/rfc7914 */ /* * Hard coding a password into an application is very bad. * It is done here solely for educational purposes. */ static unsigned char password[] = { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' }; /* * The salt is better not being hard coded too. Each password should have a * different salt if possible. The salt is not considered secret information * and is safe to store with an encrypted password. */ static unsigned char scrypt_salt[] = { 'N', 'a', 'C', 'l' }; /* * The SCRYPT parameters can be variable or hard coded. The disadvantage with * hard coding them is that they cannot easily be adjusted for future * technological improvements appear. */ static unsigned int scrypt_n = 1024; static unsigned int scrypt_r = 8; static unsigned int scrypt_p = 16; static const unsigned char expected_output[] = { 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00, 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe, 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30, 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62, 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88, 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda, 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d, 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40 }; int main(int argc, char **argv) { int ret = EXIT_FAILURE; EVP_KDF *kdf = NULL; EVP_KDF_CTX *kctx = NULL; unsigned char out[64]; OSSL_PARAM params[6], *p = params; OSSL_LIB_CTX *library_context = NULL; library_context = OSSL_LIB_CTX_new(); if (library_context == NULL) { fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n"); goto end; } /* Fetch the key derivation function implementation */ kdf = EVP_KDF_fetch(library_context, "SCRYPT", NULL); if (kdf == NULL) { fprintf(stderr, "EVP_KDF_fetch() returned NULL\n"); goto end; } /* Create a context for the key derivation operation */ kctx = EVP_KDF_CTX_new(kdf); if (kctx == NULL) { fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n"); goto end; } /* Set password */ *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, password, sizeof(password)); /* Set salt */ *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, scrypt_salt, sizeof(scrypt_salt)); /* Set N (default 1048576) */ *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_N, &scrypt_n); /* Set R (default 8) */ *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_R, &scrypt_r); /* Set P (default 1) */ *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_P, &scrypt_p); *p = OSSL_PARAM_construct_end(); /* Derive the key */ if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) { fprintf(stderr, "EVP_KDF_derive() failed\n"); goto end; } if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) { fprintf(stderr, "Generated key does not match expected value\n"); goto end; } printf("Success\n"); ret = EXIT_SUCCESS; end: EVP_KDF_CTX_free(kctx); EVP_KDF_free(kdf); OSSL_LIB_CTX_free(library_context); return ret; }