본문 바로가기
내가만드는것_만든것/오픈소스

JCP (JsCryptoProvider) C++에서 Java의 SecurityProvider같은거

by Joseph.Lee 2019. 7. 22.

https://github.com/jc-lab/jcp

지금은 mbedtls만 지원. HAS_MBEDTLS 혹은 HAS_MBEDCRYPTO 전처리기 필요

예제 :

#include <stdio.h>
#include <iostream>

#ifdef _DEBUG
#pragma comment(lib, "mbedcryptod.lib")
#pragma comment(lib, "mbedx509d.lib")
#else
#pragma comment(lib, "mbedcrypto.lib")
#pragma comment(lib, "mbedx509.lib")
#endif

#include <jcp/provider.hpp>
#include <jcp/asym_key.hpp>

#include <jcp/cipher.hpp>
#include <jcp/gcm_param_spec.hpp>
#include <jcp/message_digest.hpp>
#include <jcp/mac.hpp>
#include <jcp/key_agreement.hpp>
#include <jcp/signature.hpp>

using namespace jcp;

void printHexBytes(const void *ptr, int len) {
    const unsigned char* cptr = (const unsigned char*)ptr;
    int i;
    for (i = 0; i < len; i++) {
        printf("%02x ", cptr[i]);
    }
    printf("\n");
}

int main() {
    // Hmac Test
    if (0)
    {
        std::unique_ptr<Mac> md(Mac::getInstance("HmacSHA256"));
        SecretKey macKey((const unsigned char*)"abcd", 4);
        md->init(&macKey);
        md->update("abcdefg", 7);
        md->update("1234", 4);
        std::unique_ptr<Result<Buffer>> digest = md->digest();
        printf("doFinal : "); printHexBytes(&digest->result().data()[0], digest->result().size());

        // 39646d8e7cc8f48310089caa891aaede962a054aac9b38feeafac6d00ea1c85c
        // TEST OK
    }

    // Message Digest SHA test
    if (0)
    {
        std::unique_ptr<MessageDigest> md(MessageDigest::getInstance("SHA-256"));
        md->update("abcdefg", 7);
        md->update("1234", 4);
        std::unique_ptr<Result<Buffer>> digest = md->digest();
        printf("doFinal : "); printHexBytes(&digest->result().data()[0], digest->result().size());

        // a5fece454cbf03ada21e1de86dbc705af674ae029dac95a027b5ce5f9bdfd10f
        // TEST OK
    }

    // AES/GCM Test
    if (1)
    {
        std::vector<unsigned char> ciphertext;

        {
            std::unique_ptr<Cipher> cipher(Cipher::getInstance("AES/GCM/NoPadding"));
            SecretKey encKey((const unsigned char*)"0123456789abcdef", 16);
            std::unique_ptr<Result<Buffer>> result_with_buffer;

            cipher->init(Cipher::ENCRYPT_MODE, &encKey, GCMParameterSpec::create(128, (uint8_t*)"0000000000000000", 16).get());
            cipher->updateAAD("auth_1234", 9);
            result_with_buffer = cipher->update("abcd", 4);
            std::cout << "ENCRYPTING : " << std::endl;
            if (result_with_buffer->result().size() > 0) {
                printHexBytes(&result_with_buffer->result().data()[0], result_with_buffer->result().size());
                ciphertext.insert(ciphertext.end(), result_with_buffer->result().data(), result_with_buffer->result().data() + result_with_buffer->result().size());
            }
            result_with_buffer = cipher->doFinal();
            std::cout << "ENCRYPTING(FINAL) : " << std::endl;
            if (result_with_buffer->result().size() > 0) {
                printHexBytes(&result_with_buffer->result().data()[0], result_with_buffer->result().size());
                ciphertext.insert(ciphertext.end(), result_with_buffer->result().data(), result_with_buffer->result().data() + result_with_buffer->result().size());
            }
        }

        std::cout << "CIPHER TEXT : " << std::endl;
        printHexBytes(ciphertext.data(), ciphertext.size());
        // ad0f07ed e997dd8608e0205be4f5f8621fcf652e
        // ENCRYPTION TEST OK


        {
            std::unique_ptr<Cipher> cipher(Cipher::getInstance("AES/GCM/NoPadding"));
            SecretKey encKey((const unsigned char*)"0123456789abcdef", 16);
            std::unique_ptr<Result<Buffer>> result_with_buffer;

            cipher->init(Cipher::DECRYPT_MODE, &encKey, GCMParameterSpec::create(128, (uint8_t*)"0000000000000000", 16).get());
            cipher->updateAAD("auth_1234", 9);
            result_with_buffer = cipher->update(ciphertext.data(), ciphertext.size());
            std::cout << "DECRYPTING : " << std::endl;
            if (result_with_buffer->result().size() > 0) {
                printHexBytes(&result_with_buffer->result().data()[0], result_with_buffer->result().size());
                ciphertext.insert(ciphertext.end(), result_with_buffer->result().data(), result_with_buffer->result().data() + result_with_buffer->result().size());
            }
            result_with_buffer = cipher->doFinal();
            std::cout << "DECRYPTING(FINAL) : excep(" << (result_with_buffer->exception() ? "true" : "false") << ") : " << std::endl;
            printHexBytes(&result_with_buffer->result().data()[0], result_with_buffer->result().size());
        }

        // DECRYPTION TEST OK
    }

    // RSA Encryption Test
    if (0)
    {
        std::unique_ptr<Cipher> cipher(Cipher::getInstance("RSA/ECB/OEAPPadding"));

        jcp::AsymKeyImpl prikey;
        jcp::AsymKeyImpl pubkey;
        uint8_t bin_pubkey[] = { 0x30,0x82,0x01,0x22,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01,0x05,0x00,0x03,0x82,0x01,0x0f,0x00,0x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xc7,0x85,0xb3,0xae,0x8b,0x52,0x80,0xe3,0xb5,0x2d,0x55,0x01,0x2d,0xa8,0x58,0x1c,0x10,0xf8,0x3a,0xe2,0xc9,0xa7,0xbd,0x76,0x4a,0x8d,0xe1,0x3d,0xae,0x46,0xe1,0x7b,0xee,0x6b,0x1d,0x89,0xc8,0xc3,0x43,0x28,0x0c,0xcc,0xb0,0x2b,0x7b,0x25,0x51,0x00,0x54,0x30,0xda,0x72,0x1b,0x72,0x46,0x54,0xc8,0x26,0xef,0x1c,0x8a,0x13,0xb4,0xcc,0xaf,0xd2,0x06,0x1e,0xfa,0x24,0xe3,0xb8,0x5b,0xd4,0x00,0x47,0xdd,0x1e,0x8a,0xc2,0xcb,0xf4,0x31,0x1e,0x8c,0xf5,0x9b,0x90,0x64,0xc5,0x02,0x02,0xb3,0x54,0x9d,0x38,0x44,0x78,0x45,0x42,0xa2,0x1f,0x22,0x06,0x87,0xba,0xef,0x44,0x7c,0x50,0xca,0xd5,0x08,0x8d,0xa0,0xf6,0x31,0xda,0x12,0xd5,0x0f,0xe3,0xd3,0x38,0x34,0x99,0x3a,0x46,0xe5,0xe8,0xa5,0x80,0x1c,0x0e,0xd6,0xd3,0x63,0x2b,0x62,0xf7,0x99,0x57,0xba,0xa8,0x2f,0x6f,0x84,0x96,0xbd,0xfe,0x3b,0x47,0x68,0x87,0x33,0x57,0x41,0x98,0xfd,0xde,0xe5,0x72,0xa0,0x2c,0xc2,0x00,0x87,0x5f,0x97,0xd9,0xe6,0xdf,0xe4,0x33,0x33,0xe0,0x4f,0x51,0x16,0xd1,0xd1,0x55,0xe2,0xa5,0x97,0x8a,0x5e,0x60,0x13,0x73,0xae,0x73,0x05,0x7b,0x7c,0x95,0x0c,0xff,0x27,0x6d,0x43,0xb1,0x3a,0x5c,0xa6,0x0c,0x01,0x72,0x7f,0x81,0xb0,0xa1,0x99,0x9c,0x9c,0xeb,0x7e,0x48,0xe1,0x09,0x18,0x61,0x45,0xfc,0xb1,0xb2,0x33,0x82,0x26,0xcc,0x42,0xfe,0x8a,0x54,0x35,0xf8,0x4c,0x9a,0x68,0x8d,0x67,0xc5,0xb3,0x36,0x83,0x9d,0x5a,0x11,0x11,0x81,0xcc,0xe5,0xb8,0xa5,0xb4,0xef,0x02,0x03,0x01,0x00,0x01 };
        uint8_t bin_prikey[] = { 0x30,0x82,0x04,0xbf,0x02,0x01,0x00,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01,0x05,0x00,0x04,0x82,0x04,0xa9,0x30,0x82,0x04,0xa5,0x02,0x01,0x00,0x02,0x82,0x01,0x01,0x00,0xc7,0x85,0xb3,0xae,0x8b,0x52,0x80,0xe3,0xb5,0x2d,0x55,0x01,0x2d,0xa8,0x58,0x1c,0x10,0xf8,0x3a,0xe2,0xc9,0xa7,0xbd,0x76,0x4a,0x8d,0xe1,0x3d,0xae,0x46,0xe1,0x7b,0xee,0x6b,0x1d,0x89,0xc8,0xc3,0x43,0x28,0x0c,0xcc,0xb0,0x2b,0x7b,0x25,0x51,0x00,0x54,0x30,0xda,0x72,0x1b,0x72,0x46,0x54,0xc8,0x26,0xef,0x1c,0x8a,0x13,0xb4,0xcc,0xaf,0xd2,0x06,0x1e,0xfa,0x24,0xe3,0xb8,0x5b,0xd4,0x00,0x47,0xdd,0x1e,0x8a,0xc2,0xcb,0xf4,0x31,0x1e,0x8c,0xf5,0x9b,0x90,0x64,0xc5,0x02,0x02,0xb3,0x54,0x9d,0x38,0x44,0x78,0x45,0x42,0xa2,0x1f,0x22,0x06,0x87,0xba,0xef,0x44,0x7c,0x50,0xca,0xd5,0x08,0x8d,0xa0,0xf6,0x31,0xda,0x12,0xd5,0x0f,0xe3,0xd3,0x38,0x34,0x99,0x3a,0x46,0xe5,0xe8,0xa5,0x80,0x1c,0x0e,0xd6,0xd3,0x63,0x2b,0x62,0xf7,0x99,0x57,0xba,0xa8,0x2f,0x6f,0x84,0x96,0xbd,0xfe,0x3b,0x47,0x68,0x87,0x33,0x57,0x41,0x98,0xfd,0xde,0xe5,0x72,0xa0,0x2c,0xc2,0x00,0x87,0x5f,0x97,0xd9,0xe6,0xdf,0xe4,0x33,0x33,0xe0,0x4f,0x51,0x16,0xd1,0xd1,0x55,0xe2,0xa5,0x97,0x8a,0x5e,0x60,0x13,0x73,0xae,0x73,0x05,0x7b,0x7c,0x95,0x0c,0xff,0x27,0x6d,0x43,0xb1,0x3a,0x5c,0xa6,0x0c,0x01,0x72,0x7f,0x81,0xb0,0xa1,0x99,0x9c,0x9c,0xeb,0x7e,0x48,0xe1,0x09,0x18,0x61,0x45,0xfc,0xb1,0xb2,0x33,0x82,0x26,0xcc,0x42,0xfe,0x8a,0x54,0x35,0xf8,0x4c,0x9a,0x68,0x8d,0x67,0xc5,0xb3,0x36,0x83,0x9d,0x5a,0x11,0x11,0x81,0xcc,0xe5,0xb8,0xa5,0xb4,0xef,0x02,0x03,0x01,0x00,0x01,0x02,0x82,0x01,0x01,0x00,0xbd,0x97,0x63,0xf5,0x50,0xf8,0x75,0xf7,0x6a,0x9b,0x07,0xf2,0x55,0xb3,0x68,0xfe,0x4a,0x75,0xc6,0x66,0xe8,0x5e,0x17,0x5f,0xc7,0x5a,0x68,0x95,0x16,0xfc,0xcd,0x98,0xd9,0x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,0x81,0x81,0x00,0xfc,0x7f,0x13,0x79,0xef,0x87,0x60,0xd8,0x17,0xbc,0x15,0x04,0x75,0xac,0xdd,0xf2,0xa0,0xcb,0x90,0x83,0x7a,0x4d,0x65,0x39,0x28,0x9a,0xb7,0x52,0x6b,0x11,0x7f,0xcc,0xc7,0x28,0x0c,0xc2,0xc6,0xce,0x58,0x02,0xe4,0x1b,0x13,0x97,0xe7,0xdc,0x7b,0x33,0xe7,0x20,0x9c,0x36,0x53,0xfa,0x79,0x20,0xfe,0x70,0x29,0x3e,0x71,0xc0,0xf0,0x2a,0xe5,0x7c,0xcb,0xac,0x98,0x32,0xb4,0xcf,0x53,0x94,0xbd,0xc9,0x73,0xb6,0x9a,0x76,0xb1,0x1f,0x2b,0x27,0x0e,0xef,0xbc,0x3c,0xc9,0x6c,0x63,0x2b,0x5c,0x31,0xbf,0x25,0x17,0xaa,0x0c,0x25,0xad,0xdb,0xf3,0xfd,0x65,0x14,0x0c,0xd8,0xc2,0x69,0xd3,0x9f,0xa7,0x63,0x0e,0x26,0x58,0x63,0xb1,0xe8,0x7b,0x80,0x27,0x45,0xbd,0xc6,0xd2,0x25,0x02,0x81,0x81,0x00,0xca,0x4a,0x73,0x28,0x2a,0x0a,0xec,0xc4,0xbd,0x9d,0x4e,0x08,0x63,0x41,0xa5,0x7b,0x53,0x8b,0x99,0xf5,0x57,0x45,0x50,0xad,0xf9,0xc7,0x5f,0xd5,0xf1,0x24,0x89,0x22,0x92,0x5e,0xa3,0xf0,0x27,0x35,0x69,0x5c,0x90,0x69,0x17,0x44,0x0a,0x84,0xe7,0x28,0xee,0x84,0x02,0x4c,0x7e,0x2e,0xb0,0x95,0xc9,0xae,0x25,0xa8,0x2d,0x8a,0x37,0xca,0xf7,0xd4,0x0f,0x91,0xd5,0x39,0xcd,0xe9,0x25,0xe8,0xcc,0x57,0x87,0xcb,0xab,0x00,0x4d,0xe6,0xe0,0x72,0x18,0x1b,0x15,0x73,0xdf,0x41,0xc2,0x25,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,0xdc,0xef,0xff,0x89,0x50,0x8e,0x65,0xc8,0xc1,0xaa,0x96,0xda,0x4b,0xa2,0x79,0x0b,0xba,0x11,0x54,0x1f,0x96,0x96,0xd0,0xed,0xee,0xef,0xf9,0xf8,0x40,0x6e,0x42,0x21,0x26,0xc1,0x9e,0x09,0xef,0xf4,0xf9,0xb3,0xdd,0x48,0xf2,0x64,0x2e,0x3c,0x33,0xed,0xc3,0x09,0xc6,0xd5,0x2a,0x98,0x47,0xca,0x9d,0x70,0x96,0xa6,0x03,0x61,0xf9,0xc6,0x34,0x7e,0x47,0xbe,0x7b,0x7d,0xcc,0x20,0x91,0x65,0xb7,0x57,0x02,0x81,0x80,0x45,0x80,0x1b,0xa3,0x69,0x88,0x96,0x4e,0xf3,0x98,0x2e,0xb5,0x81,0x9d,0x5d,0x56,0xb8,0x47,0xe3,0xc6,0x63,0xb8,0x0b,0xb3,0x1b,0xd2,0x96,0xde,0x28,0x95,0xf5,0x85,0x11,0x01,0x39,0x64,0x73,0xfa,0x1f,0x1e,0x39,0x00,0x0c,0x11,0x0d,0x34,0x3a,0xe6,0xaa,0xf6,0x05,0xbf,0xe5,0x6a,0x17,0x2d,0xd3,0xfa,0x46,0xb1,0xe5,0x96,0x22,0x8c,0x63,0x35,0x1b,0xd5,0x7a,0xbf,0x21,0xff,0x7d,0xa0,0x6f,0x93,0xb2,0x0f,0x47,0xc8,0x4f,0x10,0x7f,0x66,0xb8,0xc9,0xe4,0xc9,0x60,0x81,0xce,0x26,0xeb,0x40,0xe3,0x1e,0x96,0xbd,0xdb,0x97,0x15,0xeb,0xa2,0x3e,0xef,0x8b,0xd5,0x85,0x89,0xb8,0x2b,0x62,0xc2,0x9b,0x80,0x58,0x66,0xbe,0xdb,0x54,0x3b,0xb4,0xf7,0xbf,0x3f,0x8e,0xc9,0x8a };
        prikey.setPrivateKey(bin_prikey, sizeof(bin_prikey));
        pubkey.setPublicKey(bin_pubkey, sizeof(bin_pubkey));

        std::unique_ptr<Result<Buffer>> result_with_buffer;
        std::unique_ptr<Result<void>> result;

        result = cipher->init(Cipher::ENCRYPT_MODE, &pubkey);
        result_with_buffer = cipher->update("abcd", 4);
        printHexBytes(&result_with_buffer->result().data()[0], result_with_buffer->result().size());
        result_with_buffer = cipher->doFinal();
        printHexBytes(&result_with_buffer->result().data()[0], result_with_buffer->result().size());
    }

    // ECDH Test
    if (0)
    {
        uint8_t a_pubkey[] = { 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x59, 0x8d, 0x70, 0xf6, 0x7f, 0x93, 0x87, 0xbb, 0x13, 0x08, 0xc7, 0xa3, 0xc4, 0xf8, 0x87, 0x57, 0xdb, 0xe0, 0x0e, 0xc1, 0x64, 0x8b, 0xfc, 0x1f, 0x5f, 0x5f, 0x05, 0xba, 0xce, 0xfa, 0xde, 0xf8, 0xb4, 0x42, 0xb8, 0xf5, 0x49, 0xde, 0xb5, 0x10, 0x14, 0x63, 0x40, 0xec, 0xd6, 0x6c, 0xf0, 0x5d, 0x11, 0x8d, 0x21, 0xe5, 0x99, 0x6a, 0x48, 0xff, 0x3c, 0x91, 0xc4, 0x27, 0xd2, 0x33, 0x08, 0x15 };
        uint8_t a_prikey[] = { 0x30, 0x41, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04, 0x27, 0x30, 0x25, 0x02, 0x01, 0x01, 0x04, 0x20, 0x5d, 0x58, 0x4e, 0xfc, 0x9e, 0x75, 0x7f, 0x2b, 0x69, 0xf7, 0x09, 0x19, 0x68, 0x26, 0x00, 0x77, 0xa7, 0x35, 0x6e, 0x0c, 0x74, 0x00, 0xbe, 0x89, 0xd4, 0xf9, 0x53, 0x31, 0x55, 0x9d, 0x94, 0xbf };
        uint8_t b_pubkey[] = { 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x12, 0x22, 0x31, 0x9c, 0xb1, 0x5e, 0xbd, 0xb7, 0xdd, 0x7e, 0xad, 0x20, 0x71, 0x8f, 0xd4, 0xf3, 0x57, 0x02, 0x95, 0xf3, 0xb4, 0xc4, 0xbd, 0xf8, 0x04, 0x42, 0xed, 0x4b, 0xc0, 0x4f, 0x8d, 0x36, 0x15, 0xc4, 0x1c, 0x03, 0xd1, 0xf0, 0xcf, 0x4f, 0xf0, 0xc1, 0x60, 0xaa, 0x4c, 0x23, 0x03, 0x71, 0x40, 0xaa, 0xb5, 0xcb, 0x28, 0x6b, 0xf9, 0x56, 0x57, 0xfd, 0xb4, 0xa7, 0x3b, 0x78, 0x57, 0xeb };
        uint8_t b_prikey[] = { 0x30, 0x41, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04, 0x27, 0x30, 0x25, 0x02, 0x01, 0x01, 0x04, 0x20, 0xb3, 0x5b, 0x9e, 0xfb, 0x80, 0x04, 0x53, 0x47, 0x3f, 0x5e, 0xbb, 0x88, 0x06, 0x89, 0x38, 0x18, 0x07, 0x9a, 0x57, 0x01, 0x89, 0x9b, 0x8d, 0xde, 0xb7, 0xd6, 0x81, 0x2a, 0x93, 0x10, 0x7f, 0x37 };
        uint8_t secret_answer[] = { 0xcb, 0x3b, 0x7e, 0x9d, 0x27, 0xf5, 0xd4, 0x77, 0xa8, 0x9a, 0xe8, 0xb4, 0xfa, 0x19, 0x59, 0x00, 0x71, 0x33, 0x20, 0x5a, 0x15, 0xe5, 0xfc, 0x5a, 0x2a, 0x5b, 0x78, 0xf1, 0xa4, 0x4e, 0x6b, 0x1e };

        jcp::AsymKeyImpl apri; apri.setPrivateKey(a_prikey, sizeof(a_prikey));
        jcp::AsymKeyImpl bpub; bpub.setPublicKey(b_pubkey, sizeof(b_pubkey));

        std::unique_ptr<KeyAgreement> ka(KeyAgreement::getInstance("ECDH"));
        ka->init(&apri);
        ka->doPhase(&bpub);
        std::unique_ptr<Result<Buffer>> secret = ka->generateSecret();
        printHexBytes(&secret->result().data()[0], secret->result().size());

        // TEST OK
    }

    // ECDSA Test
    if (0)
    {
        uint8_t a_pubkey[] = { 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x59, 0x8d, 0x70, 0xf6, 0x7f, 0x93, 0x87, 0xbb, 0x13, 0x08, 0xc7, 0xa3, 0xc4, 0xf8, 0x87, 0x57, 0xdb, 0xe0, 0x0e, 0xc1, 0x64, 0x8b, 0xfc, 0x1f, 0x5f, 0x5f, 0x05, 0xba, 0xce, 0xfa, 0xde, 0xf8, 0xb4, 0x42, 0xb8, 0xf5, 0x49, 0xde, 0xb5, 0x10, 0x14, 0x63, 0x40, 0xec, 0xd6, 0x6c, 0xf0, 0x5d, 0x11, 0x8d, 0x21, 0xe5, 0x99, 0x6a, 0x48, 0xff, 0x3c, 0x91, 0xc4, 0x27, 0xd2, 0x33, 0x08, 0x15 };
        uint8_t a_prikey[] = { 0x30, 0x41, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04, 0x27, 0x30, 0x25, 0x02, 0x01, 0x01, 0x04, 0x20, 0x5d, 0x58, 0x4e, 0xfc, 0x9e, 0x75, 0x7f, 0x2b, 0x69, 0xf7, 0x09, 0x19, 0x68, 0x26, 0x00, 0x77, 0xa7, 0x35, 0x6e, 0x0c, 0x74, 0x00, 0xbe, 0x89, 0xd4, 0xf9, 0x53, 0x31, 0x55, 0x9d, 0x94, 0xbf };
        uint8_t secret_answer[] = { 0xcb, 0x3b, 0x7e, 0x9d, 0x27, 0xf5, 0xd4, 0x77, 0xa8, 0x9a, 0xe8, 0xb4, 0xfa, 0x19, 0x59, 0x00, 0x71, 0x33, 0x20, 0x5a, 0x15, 0xe5, 0xfc, 0x5a, 0x2a, 0x5b, 0x78, 0xf1, 0xa4, 0x4e, 0x6b, 0x1e };

        jcp::AsymKeyImpl apri; apri.setPrivateKey(a_prikey, sizeof(a_prikey));
        jcp::AsymKeyImpl apub; apub.setPublicKey(a_pubkey, sizeof(a_pubkey));

        std::unique_ptr<Signature> sign(Signature::getInstance("SHA256withECDSA"));
        sign->initSign(&apri);
        sign->update("12345678", 8);
        std::unique_ptr<Result<Buffer>> signature_result = sign->sign();
        printHexBytes(&signature_result->result().data()[0], signature_result->result().size());

        std::unique_ptr<Signature> verify(Signature::getInstance("SHA256withECDSA"));
        verify->initVerify(&apub);
        verify->update("12345678", 8);
        std::unique_ptr<Result<bool>> verify_result = verify->verify(signature_result->result().data(), signature_result->result().size());
        printf("\nverify = %d\n", verify_result->result());

        // TEST OK
    }

    // PBKDF2 Test
    if (0) {
        unsigned char salt[] = "SALT1234";

        const jcp::SecretKeyFactory* factory = jcp::SecretKeyFactory::getInstance("PBKDF2WithHmacSHA1");
        jcp::PBEKeySpec keySpec("PASSWORD", 8, salt, 8, 1000, 128);
        std::unique_ptr<Result<SecretKey>> result = factory->generateSecret(&keySpec);
        std::cout << "ENCODED : " << std::endl;
        printHexBytes(result->result().getEncoded().data(), result->result().getEncoded().size());
        // 77 27 25 72 63 fe 4f a3 77 38 be 1b 3b 04 42 ab
        // TEST OK
    }

    return 0;
}
반응형

댓글