​OpenSSL项目​​最近6个月添加了许多新特性, 包括对中国SM2/SM3/SM4算法的支持:

参考: 中国国家密码管理局制定的商业密码算法标准

  • 《GM/T 0006-2012 密码应用标识规范》定义国密算法OID标识
  • 《GB/T 32907-2016 SM4分组密码算法》(原GM/T 0002-2012)
  • 《GB/T 329??-2016 SM2椭圆曲线公钥密码算法》(原GM/T 0003-2012)
  • 《GB/T 32905-2016 SM3密码杂凑算法》(原GM/T 0004-2012)
下载源码, 编译, 以及验证步骤

下载源码

解压缩

  1.  

    tar xzvf openssl-1.1.1-pre4.tar.gz
  2.  

    tar xzvf openssl-1.1.1-pre5.tar.gz

编译步骤

  1.  

    cd openssl-1.1.1-pre5
  2.  

    ./config
  3.  

    make

本地安装(可选步骤)

sudo make install


配置LD_LIBRARY_PATH并检查openssl可执行程序版本号

  1.  

    $ export LD_LIBRARY_PATH=`pwd`
  2.  
     
  3.  

    $ ./apps/openssl version
  4.  

    OpenSSL 1.1.1-pre5 (beta) 17 Apr 2018

检查 SM3 哈希校验和

  1.  

    $ echo -n "abc" | ./apps/openssl dgst -SM3
  2.  

    (stdin)= 66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0

检查椭圆曲线是否包含SM2

  1.  

    $ ./apps/openssl ecparam -list_curves | grep SM2
  2.  

    SM2 : SM2 curve over a 256 bit prime field

检查对称算法

  1.  

    ./apps/openssl enc -ciphers
  2.  

    -sm4
  3.  

    -sm4-cbc
  4.  

    -sm4-cfb
  5.  

    -sm4-ctr
  6.  

    -sm4-ecb
  7.  

    -sm4-ofb
查找SM4对称加密API接口文档

???

SM4-自测试数据
  1. 测试SM4-ECB电子密码本模式, 选取AES-128-ECB作为参考
    ​https:///liuqun/openssl-sm4-demo/​
  2.  

    /** 文件名: https:///liuqun/openssl-sm4-demo/blob/cmake/src/main.c */
  3.  

    #include <stddef.h>
  4.  

    #include <stdio.h>
  5.  

    #include <stdlib.h>
  6.  

    #include <string.h>
  7.  

    #include "openssl/err.h"
  8.  

    #include "openssl/evp.h"
  9.  
     
  10.  

    /* Before OpenSSL 1.1.1-pre1, we did not have EVP_sm4_ecb() */
  11.  

    #if defined(OPENSSL_VERSION_NUMBER) \
  12.  

    && OPENSSL_VERSION_NUMBER < 0x10101001L
  13.  

    static const EVP_CIPHER *(*EVP_sm4_ecb)()=EVP_aes_128_ecb;
  14.  

    #endif
  15.  
     
  16.  

    typedef struct {
  17.  

    const unsigned char *in_data;
  18.  

    size_t in_data_len;
  19.  

    int in_data_is_already_padded;
  20.  

    const unsigned char *in_ivec;
  21.  

    const unsigned char *in_key;
  22.  

    size_t in_key_len;
  23.  

    } test_case_t;
  24.  
     
  25.  
     
  26.  

    void test_encrypt_with_cipher(const test_case_t *in, const EVP_CIPHER *cipher)
  27.  

    {
  28.  

    unsigned char *out_buf = NULL;
  29.  

    int out_len;
  30.  

    int out_padding_len;
  31.  

    EVP_CIPHER_CTX *ctx;
  32.  
     
  33.  

    ctx = EVP_CIPHER_CTX_new();
  34.  

    EVP_EncryptInit_ex(ctx, cipher, NULL, in->in_key, in->in_ivec);
  35.  
     
  36.  

    if (in->in_data_is_already_padded)
  37.  

    {
  38.  

    /* Check whether the input data is already padded.
  39.  

    And its length must be an integral multiple of the cipher's block size. */
  40.  

    const size_t bs = EVP_CIPHER_block_size(cipher);
  41.  

    if (in->in_data_len % bs != 0)
  42.  

    {
  43.  

    printf("ERROR-1: data length=%d which is not added yet; block size=%d\n", (int) in->in_data_len, (int) bs);
  44.  

    /* Warning: Remember to do some clean-ups */
  45.  

    EVP_CIPHER_CTX_free(ctx);
  46.  

    return;
  47.  

    }
  48.  

    /* Disable the implicit PKCS#7 padding defined in EVP_CIPHER */
  49.  

    EVP_CIPHER_CTX_set_padding(ctx, 0);
  50.  

    }
  51.  
     
  52.  

    out_buf = (unsigned char *) malloc(((in->in_data_len>>4)+1) << 4);
  53.  

    out_len = 0;
  54.  

    EVP_EncryptUpdate(ctx, out_buf, &out_len, in->in_data, in->in_data_len);
  55.  

    if (1)
  56.  

    {
  57.  

    printf("Debug: out_len=%d\n", out_len);
  58.  

    }
  59.  
     
  60.  

    out_padding_len = 0;
  61.  

    EVP_EncryptFinal_ex(ctx, out_buf+out_len, &out_padding_len);
  62.  

    if (1)
  63.  

    {
  64.  

    printf("Debug: out_padding_len=%d\n", out_padding_len);
  65.  

    }
  66.  
     
  67.  

    EVP_CIPHER_CTX_free(ctx);
  68.  

    if (1)
  69.  

    {
  70.  

    int i;
  71.  

    int len;
  72.  

    len = out_len + out_padding_len;
  73.  

    for (i=0; i<len; i++)
  74.  

    {
  75.  

    printf("%02x ", out_buf[i]);
  76.  

    }
  77.  

    printf("\n");
  78.  

    }
  79.  
     
  80.  

    if (out_buf)
  81.  

    {
  82.  

    free(out_buf);
  83.  

    out_buf = NULL;
  84.  

    }
  85.  

    }
  86.  
     
  87.  

    void main()
  88.  

    {
  89.  

    int have_sm4 = (OPENSSL_VERSION_NUMBER >= 0x10101001L);
  90.  

    int have_aes = 1;
  91.  

    const unsigned char data[]=
  92.  

    {
  93.  

    0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  94.  

    0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  95.  

    };
  96.  

    unsigned char ivec[EVP_MAX_IV_LENGTH]; ///< IV 向量
  97.  

    const unsigned char key1[16] = ///< key_data, 密钥内容, 至少16字节
  98.  

    {
  99.  

    0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  100.  

    0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  101.  

    };
  102.  

    test_case_t tc;
  103.  
     
  104.  

    tc.in_data = data;
  105.  

    tc.in_data_len = sizeof(data);
  106.  

    tc.in_data_is_already_padded = (tc.in_data_len % 16)==0; // Hard coded 16 as the cipher's block size
  107.  

    tc.in_key = key1;
  108.  

    tc.in_key_len = sizeof(key1);
  109.  

    memset(ivec, 0x00, EVP_MAX_IV_LENGTH);
  110.  

    tc.in_ivec = ivec;
  111.  
     
  112.  

    #if defined(OPENSSL_NO_SM4)
  113.  

    have_sm4 = 0;
  114.  

    #endif
  115.  

    if (have_sm4)
  116.  

    {
  117.  

    printf("[1]\n");
  118.  

    printf("Debug: EVP_sm4_ecb() test\n");
  119.  

    test_encrypt_with_cipher(&tc, EVP_sm4_ecb());
  120.  

    }
  121.  

    #if defined(OPENSSL_NO_AES)
  122.  

    have_aes = 0;
  123.  

    #endif
  124.  

    if (have_aes)
  125.  

    {
  126.  

    printf("[2]\n");
  127.  

    printf("Debug: EVP_aes_128_ecb() test\n");
  128.  

    test_encrypt_with_cipher(&tc, EVP_aes_128_ecb());
  129.  

    }
  130.  

    }
  131.  

    假定当前是把main.c放在 openssl-1.1.1-pre5/文件夹内
  132.  

    gcc -Iinclude -c main.c
  133.  

    gcc main.o libcrypto.so -o a.out
  134.  
     
  135.  

    export LD_LIBRARY_PATH=`pwd`
  136.  

    ldd a.out
  137.  
     
  138.  

    ./a.out


9.1. GM/T OIDs
9.1.1. SCA OID Prefix
All SM4 GM/T OIDs belong under the "1.2.156.10197" OID prefix,
registered by the Chinese Cryptography Standardization Technology
Committee ("CCSTC"), a committee under the SCA. Its components are
described below in ASN.1 notation.