openssl3.2 - 官方demo学习 - kdf - argon2.c

2024-01-16 12:28

本文主要是介绍openssl3.2 - 官方demo学习 - kdf - argon2.c,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • openssl3.2 - 官方demo学习 - kdf - argon2.c
    • 概述
    • 笔记
    • END

openssl3.2 - 官方demo学习 - kdf - argon2.c

概述

设置KDF算法的参数, 并获得key

笔记

/*!
\file argon2.c
\note openssl3.2 - 官方demo学习 - kdf - argon2.c
设置KDF算法的参数, 并获得key
*//** Copyright 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/params.h>
#include <openssl/thread.h>#include "my_openSSL_lib.h"/** Example showing how to use Argon2 KDF.* See man EVP_KDF-ARGON2 for more information.** test vector from* https://datatracker.ietf.org/doc/html/rfc9106*//** Hard coding a password into an application is very bad.* It is done here solely for educational purposes.*/
static unsigned char password[] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
};/** 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 salt[] = {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02
};/** Optional secret for KDF*/
static unsigned char secret[] = {0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
};/** Optional additional data*/
static unsigned char ad[] = {0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,0x04, 0x04, 0x04, 0x04
};/** Argon2 cost parameters*/
static uint32_t memory_cost    = 32;
static uint32_t iteration_cost =  3;
static uint32_t parallel_cost  =  4;static const unsigned char expected_output[] = {0x0d, 0x64, 0x0d, 0xf5, 0x8d, 0x78, 0x76, 0x6c,0x08, 0xc0, 0x37, 0xa3, 0x4a, 0x8b, 0x53, 0xc9,0xd0, 0x1e, 0xf0, 0x45, 0x2d, 0x75, 0xb6, 0x5e,0xb5, 0x25, 0x20, 0xe9, 0x6b, 0x01, 0xe6, 0x59
};int main(int argc, char **argv)
{int rv = EXIT_FAILURE;EVP_KDF *_evp_kdf = NULL;EVP_KDF_CTX *_evp_kdf_ctx = NULL;unsigned char out[32];OSSL_PARAM _ossl_param_ary[9], *p_ossl_param = _ossl_param_ary;OSSL_LIB_CTX *_ossl_lib_ctx = NULL;unsigned int threads;_ossl_lib_ctx = OSSL_LIB_CTX_new();if (_ossl_lib_ctx == NULL) {fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");goto end;}/* Fetch the key derivation function implementation */_evp_kdf = EVP_KDF_fetch(_ossl_lib_ctx, "argon2id", NULL);if (_evp_kdf == NULL) {fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");goto end;}/* Create a context for the key derivation operation */_evp_kdf_ctx = EVP_KDF_CTX_new(_evp_kdf);if (_evp_kdf_ctx == NULL) {fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");goto end;}/** Thread support can be turned off; use serialization if we cannot* set requested number of threads.*/threads = parallel_cost;if (OSSL_set_max_threads(_ossl_lib_ctx, parallel_cost) != 1) {uint64_t max_threads = OSSL_get_max_threads(_ossl_lib_ctx);if (max_threads == 0)threads = 1;else if (max_threads < parallel_cost)threads = (unsigned int)max_threads;}/* Set password */*p_ossl_param++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, password, sizeof(password));/* Set salt */*p_ossl_param++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, sizeof(salt));/* Set optional additional data */*p_ossl_param++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_ARGON2_AD, ad, sizeof(ad));/* Set optional secret */*p_ossl_param++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET, secret, sizeof(secret));/* Set iteration count */*p_ossl_param++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ITER, &iteration_cost);/* Set threads performing derivation (can be decreased) */*p_ossl_param++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_THREADS, &threads);/* Set parallel cost */*p_ossl_param++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_LANES, &parallel_cost);/* Set memory requirement */*p_ossl_param++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_MEMCOST, &memory_cost);*p_ossl_param = OSSL_PARAM_construct_end();/* Derive the key */if (EVP_KDF_derive(_evp_kdf_ctx, out, sizeof(out), _ossl_param_ary) != 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;}rv = EXIT_SUCCESS;
end:EVP_KDF_CTX_free(_evp_kdf_ctx);EVP_KDF_free(_evp_kdf);OSSL_LIB_CTX_free(_ossl_lib_ctx);return rv;
}

END

这篇关于openssl3.2 - 官方demo学习 - kdf - argon2.c的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/612602

相关文章

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

ubuntu系统使用官方操作命令升级Dify指南

《ubuntu系统使用官方操作命令升级Dify指南》Dify支持自动化执行、日志记录和结果管理,适用于数据处理、模型训练和部署等场景,今天我们就来看看ubuntu系统中使用官方操作命令升级Dify的方... Dify 是一个基于 docker 的工作流管理工具,旨在简化机器学习和数据科学领域的多步骤工作流。

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

Mybatis官方生成器的使用方式

《Mybatis官方生成器的使用方式》本文详细介绍了MyBatisGenerator(MBG)的使用方法,通过实际代码示例展示了如何配置Maven插件来自动化生成MyBatis项目所需的实体类、Map... 目录1. MyBATis Generator 简介2. MyBatis Generator 的功能3

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06