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

这篇具有很好参考价值的文章主要介绍了openssl3.2 - 官方demo学习 - kdf - argon2.c。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

概述

设置KDF算法的参数, 并获得key文章来源地址https://www.toymoban.com/news/detail-798572.html

笔记

/*!
\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的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • openssl3.2 - 官方demo学习 - certs

    打开官方demos的certs目录, 没看到.c. 茫然了一下. 官方在这个目录中要展示啥呢? 看了readme, 懂了. 原来官方在这个目录中, 要展示如何使用openssl.exe的命令行来操作证书(建立证书, 证书入库, 吊销证书, 查询证书). 官方通过3个.sh来展示证书操作. mkcerts.sh - 一组操作, 用来建立证书

    2024年02月01日
    浏览(34)
  • openssl3.2 - 官方demo学习 - sconnect.c

    TLS客户端 - 使用根证书, 非阻塞, 向服务器要东西. 开始一个新demo学习时, 要从头配置包含路径, 麻烦. 直接拷贝上一个实现工程, 换掉实现.c方便一些. 换的新demo实现, 要加入库包含和头包含, 麻烦, 做一个公用头文件, 直接include方便一些.

    2024年02月01日
    浏览(35)
  • openssl3.2 - 官方demo学习 - test - certs

    官方demos目录有证书操作的例子 已经做了笔记 openssl3.2 - 官方demo学习 - certs 但是这个demos/certs目录的脚本, 并没有演示如何操作PKCS12证书. 在官方给的程序例子中, 有操作PKCS12证书的工程, 但是却没有配套的PKCS12证书. 这咋弄? 翻了一下openssl源码工程, 发现测试目录中有2个脚本

    2024年01月18日
    浏览(37)
  • openssl3.2 - 官方demo学习 - saccept.c

    建立TLSServer(使用了证书, 和证书中的私钥), 接收客户端的连接, 并将客户端发来的信息打印到屏幕 笔记

    2024年01月20日
    浏览(36)
  • openssl3.2 - 官方demo学习 - smime - smdec.c

    从pem证书中得到x509*和私钥, 用私钥和证书解密MIME格式的PKCS7密文, 并保存解密后的明文 MIME的数据操作, 都是PKCS7相关的

    2024年01月18日
    浏览(31)
  • openssl3.2 - 官方demo学习 - smime - smenc.c

    读取X509证书, 用PKCS7加密明文(证书 + 明文 + 3DES_CBC), 保存为MIME格式的密文 openssl API的命名含义 BIO_new_file “new” a “file”, return a “BIO” object PEM_read_bio_X509() Read a certificate in PEM format from a BIO data format is “PEM”, “read” from “bio”, return a object type is “X509”

    2024年01月20日
    浏览(38)
  • openssl3.2 - 官方demo学习 - mac - siphash.c

    MAC算法为 SIPHASH, 设置参数(C-rounds, D-rounds, 也可以不设置, 有默认值) 用key初始化MAC算法, 算明文的MAC值

    2024年01月19日
    浏览(41)
  • openssl3.2 - 官方demo学习 - smime - smver.c

    对于签名文件(不管是单独签名, 还是联合签名), 都要用顶层证书进行验签(靠近根CA的证书) 读证书文件, 得到x509*, 添加到证书容器 读取签名密文, 得到pkcs7*和密文的bio 进行pkcs7验签, 并将验签得到的签名的明文写到文件.

    2024年01月19日
    浏览(34)
  • openssl3.2 - 官方demo学习 - cipher - aesgcm.c

    AES-256-GCM 在这个实验中验证了EVP_CIPHER_fetch()中算法名称字符串的来源定位. 在工程中配置环境变量PATH, 且合并环境. 这样就不用将openSSL的DLL和配置文件拷贝到工程里面了. 提交代码时, 就能节省很多空间. 配置工程调试时, 也顺畅多了.

    2024年01月22日
    浏览(38)
  • openssl3.2 - 官方demo学习 - smime - smsign.c

    从证书中得到X509*和私钥指针 用证书和私钥对铭文进行签名, 得到签名后的pkcs7指针 将pkcs7指向的bio_in, 写为MIME格式的签名密文 BIO_reset() 可以将一个bio恢复到刚打开的状态(应该就是将文件指针重新指向文件头部), 一般用于只读打开的场景 经常用于多个对象要操作同一个bio的场

    2024年01月19日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包