从openssl中剥离bn库编译成静态库

修改 include/openssl/err.h
# define BNerr(f,r)   ERR_PUT_error(ERR_LIB_BN,(f),(r),OPENSSL_FILE,OPENSSL_LINE)

修改 crypto/bn/bn_rand.c
这个函数调用了 SHA512 库,但这个函数并不常用,可以去掉
int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,
                          const BIGNUM *priv, const unsigned char *message,
                          size_t message_len, BN_CTX *ctx)
{
   return 0;
}


# 编译 openssl
cd ~/openssl-OpenSSL_1_1_1m/
./Configure linux-armv4 --prefix=/home/meng/openssl
make install -j8

# 复制编译好的 bignum 目标文件
cd ~/openssl-OpenSSL_1_1_1m/crypto
cp -R bn ~/

# 不要 bn_blind bn_err bn_print 这些函数库暂时用不上
rm -rf ~/bn/bn_blind.*
rm -rf ~/bn/bn_err.*
rm -rf ~/bn/bn_print.*

# 复制静态初始化函数 OPENSSL_cpuid_setup
cp armv4cpuid.* ~/bn/
cp armcap.* ~/bn/

# 封装一些 bn 所需的函数
echo '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>

# ifdef PTHREAD_RWLOCK_INITIALIZER
#  define USE_RWLOCK
# endif

typedef void CRYPTO_RWLOCK;

void *CRYPTO_malloc(size_t num, const char *file, int line)
{
    return malloc(num);
}

void *CRYPTO_zalloc(size_t num, const char *file, int line)
{
    void *ret = CRYPTO_malloc(num, file, line);

    if (ret != NULL)
        memset(ret, 0, num);
    return ret;
}

void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
{
    return realloc(str, num);
}

void CRYPTO_free(void *str, const char *file, int line)
{
    free(str);
}

//void OPENSSL_cleanse(void *ptr, size_t len)
//{
//    memset(ptr, 0, len);
//}

void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
{
    if (str == NULL)
        return;
    if (num)
        OPENSSL_cleanse(str, num);
    CRYPTO_free(str, file, line);
}

void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
{
    return CRYPTO_malloc(num, file, line);
}

void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
{
    return CRYPTO_zalloc(num, file, line);
}

void CRYPTO_secure_free(void *ptr, const char *file, int line)
{
    CRYPTO_free(ptr, file, line);
}

void CRYPTO_secure_clear_free(void *ptr, size_t num,
                              const char *file, int line)
{
    if (ptr == NULL)
        return;
    OPENSSL_cleanse(ptr, num);
    CRYPTO_free(ptr, file, line);
}

int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
{
# ifdef USE_RWLOCK
    if (pthread_rwlock_rdlock(lock) != 0)
        return 0;
# else
    if (pthread_mutex_lock(lock) != 0)
        return 0;
# endif

    return 1;
}

int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
{
# ifdef USE_RWLOCK
    if (pthread_rwlock_wrlock(lock) != 0)
        return 0;
# else
    if (pthread_mutex_lock(lock) != 0)
        return 0;
# endif

    return 1;
}

int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
{
# ifdef USE_RWLOCK
    if (pthread_rwlock_unlock(lock) != 0)
        return 0;
# else
    if (pthread_mutex_unlock(lock) != 0)
        return 0;
# endif

    return 1;
}

int RAND_bytes(void *buf, int size)
{
    int fd;

    //伪随机数 /dev/urandom
    //真随机数 /dev/random 慢且可能阻塞较长时间
    fd = open("/dev/urandom", O_RDONLY);
    if(fd > 0)
    {
        read(fd, buf, size);
        close(fd);
        return 1;
    }
    return 0;
}

int RAND_priv_bytes(unsigned char *buf, int num)
{
    RAND_bytes(buf, num);
}
' > ~/bn/cryptolib.c

# 开始编译成库
cd ~/bn
arm-linux-gnueabihf-gcc -c -o cryptolib.o cryptolib.c
ar r libcrypto.a *.o

# 编译测试程序
echo '
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>      //open
#include <time.h>       //clock_gettime
#include <sys/time.h>   //gettimeofday
#include <sys/types.h>

#include <openssl/opensslv.h>
#include <openssl/bn.h>

uint32_t GetMonotonicTimestamp(void)
{
    struct timespec ts;
    struct timeval tv;

    gettimeofday(&tv, NULL);

    #if defined(CLOCK_MONOTONIC_HR)
    clock_gettime(CLOCK_MONOTONIC_HR, &ts);
    #elif defined(CLOCK_MONOTONIC_FAST)
    clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
    #elif defined(CLOCK_MONOTONIC_COARSE)
    clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
    #elif defined(CLOCK_MONOTONIC)
    clock_gettime(CLOCK_MONOTONIC, &ts);
    #else
    ts.tv_sec = tv.tv_sec;
    ts.tv_nsec = (long int)tv.tv_usec * 1000;
    #endif

    return (uint32_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}

static const uint8_t kSRPGenerator5[] = {5};
static const uint8_t kSRPModulus3072[] =
{
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34,
    0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74,
    0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
    0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x37,
    0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45, 0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6,
    0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x37, 0xED, 0x6B, 0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED,
    0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5, 0xAE, 0x9F, 0x24, 0x11, 0x7C, 0x4B, 0x1F, 0xE6,
    0x49, 0x28, 0x66, 0x51, 0xEC, 0xE4, 0x5B, 0x3D, 0xC2, 0x00, 0x7C, 0xB8, 0xA1, 0x63, 0xBF, 0x05,
    0x98, 0xDA, 0x48, 0x36, 0x1C, 0x55, 0xD3, 0x9A, 0x69, 0x16, 0x3F, 0xA8, 0xFD, 0x24, 0xCF, 0x5F,
    0x83, 0x65, 0x5D, 0x23, 0xDC, 0xA3, 0xAD, 0x96, 0x1C, 0x62, 0xF3, 0x56, 0x20, 0x85, 0x52, 0xBB,
    0x9E, 0xD5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6D, 0x67, 0x0C, 0x35, 0x4E, 0x4A, 0xBC, 0x98, 0x04,
    0xF1, 0x74, 0x6C, 0x08, 0xCA, 0x18, 0x21, 0x7C, 0x32, 0x90, 0x5E, 0x46, 0x2E, 0x36, 0xCE, 0x3B,
    0xE3, 0x9E, 0x77, 0x2C, 0x18, 0x0E, 0x86, 0x03, 0x9B, 0x27, 0x83, 0xA2, 0xEC, 0x07, 0xA2, 0x8F,
    0xB5, 0xC5, 0x5D, 0xF0, 0x6F, 0x4C, 0x52, 0xC9, 0xDE, 0x2B, 0xCB, 0xF6, 0x95, 0x58, 0x17, 0x18,
    0x39, 0x95, 0x49, 0x7C, 0xEA, 0x95, 0x6A, 0xE5, 0x15, 0xD2, 0x26, 0x18, 0x98, 0xFA, 0x05, 0x10,
    0x15, 0x72, 0x8E, 0x5A, 0x8A, 0xAA, 0xC4, 0x2D, 0xAD, 0x33, 0x17, 0x0D, 0x04, 0x50, 0x7A, 0x33,
    0xA8, 0x55, 0x21, 0xAB, 0xDF, 0x1C, 0xBA, 0x64, 0xEC, 0xFB, 0x85, 0x04, 0x58, 0xDB, 0xEF, 0x0A,
    0x8A, 0xEA, 0x71, 0x57, 0x5D, 0x06, 0x0C, 0x7D, 0xB3, 0x97, 0x0F, 0x85, 0xA6, 0xE1, 0xE4, 0xC7,
    0xAB, 0xF5, 0xAE, 0x8C, 0xDB, 0x09, 0x33, 0xD7, 0x1E, 0x8C, 0x94, 0xE0, 0x4A, 0x25, 0x61, 0x9D,
    0xCE, 0xE3, 0xD2, 0x26, 0x1A, 0xD2, 0xEE, 0x6B, 0xF1, 0x2F, 0xFA, 0x06, 0xD9, 0x8A, 0x08, 0x64,
    0xD8, 0x76, 0x02, 0x73, 0x3E, 0xC8, 0x6A, 0x64, 0x52, 0x1F, 0x2B, 0x18, 0x17, 0x7B, 0x20, 0x0C,
    0xBB, 0xE1, 0x17, 0x57, 0x7A, 0x61, 0x5D, 0x6C, 0x77, 0x09, 0x88, 0xC0, 0xBA, 0xD9, 0x46, 0xE2,
    0x08, 0xE2, 0x4F, 0xA0, 0x74, 0xE5, 0xAB, 0x31, 0x43, 0xDB, 0x5B, 0xFC, 0xE0, 0xFD, 0x10, 0x8E,
    0x4B, 0x82, 0xD1, 0x20, 0xA9, 0x3A, 0xD2, 0xCA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

static const uint8_t secret[256] = {
    0x35, 0x8A, 0x94, 0xD6, 0xDA, 0x5E, 0xFB, 0x3F, 0x04, 0x95, 0xD3, 0x3A, 0x51, 0x1A, 0x92, 0x4E,
    0x3B, 0x56, 0x3A, 0xE7, 0x9B, 0xF3, 0xFC, 0x45, 0xBC, 0x6C, 0x13, 0x54, 0x0E, 0xB5, 0xED, 0x51,
    0xBD, 0x72, 0x2A, 0x4E, 0xC7, 0x70, 0x01, 0xFB, 0x47, 0x73, 0x0E, 0x45, 0x16, 0x75, 0xA7, 0xB5,
    0x03, 0xF1, 0x25, 0x9D, 0x2F, 0xE0, 0xD9, 0x7A, 0x5A, 0x73, 0x87, 0x1D, 0x32, 0xB6, 0xA5, 0x7F,
    0xE1, 0xAB, 0xB7, 0x5E, 0x7F, 0x8C, 0x89, 0xF0, 0x16, 0x2C, 0x20, 0x5B, 0x5D, 0xC8, 0x9B, 0xC2,
    0x6C, 0xA9, 0x69, 0x03, 0xED, 0x69, 0x81, 0x8E, 0xAC, 0xCB, 0x92, 0xE1, 0x73, 0x64, 0x3E, 0x89,
    0xA1, 0x94, 0xEC, 0xD5, 0xE8, 0x8D, 0xC6, 0x7A, 0x15, 0x55, 0xD5, 0xDD, 0xE2, 0x17, 0x6E, 0xD0,
    0x1F, 0x2C, 0x4F, 0xE7, 0xC8, 0x9B, 0x23, 0xBD, 0xC0, 0x1B, 0x59, 0xC0, 0x57, 0xB7, 0x70, 0x6B,
    0xD0, 0xAE, 0x2B, 0x04, 0x7C, 0x36, 0x5D, 0x36, 0x3E, 0x28, 0x2E, 0x2C, 0x73, 0xCE, 0x13, 0xFF,
    0x9B, 0x4C, 0xD5, 0x9F, 0x41, 0x6B, 0x5D, 0x88, 0xF8, 0xB0, 0x38, 0xE0, 0x77, 0x0C, 0xE9, 0xE8,
    0x15, 0x9C, 0x8C, 0xC2, 0x49, 0x2A, 0x68, 0x08, 0xDC, 0x82, 0x5F, 0xAF, 0xF4, 0xB9, 0x72, 0x32,
    0x30, 0x02, 0xAC, 0x01, 0x71, 0xB0, 0x45, 0xB1, 0x0C, 0x78, 0xBD, 0x6A, 0x7E, 0x23, 0x4B, 0x83,
    0xEC, 0x27, 0xDC, 0x68, 0xF0, 0xF5, 0x75, 0x14, 0x90, 0xE6, 0xD1, 0xD2, 0x5B, 0x0D, 0x64, 0x10,
    0x05, 0x67, 0x3B, 0x6B, 0x05, 0x24, 0x61, 0x44, 0x05, 0x09, 0xAB, 0x8A, 0x2F, 0x20, 0x2A, 0x88,
    0xA4, 0x3D, 0x98, 0xD5, 0xA8, 0x02, 0x87, 0xC7, 0x4D, 0x79, 0x20, 0x04, 0xB2, 0x5E, 0xB9, 0x06,
    0x11, 0xB9, 0x9B, 0xBB, 0x3B, 0x67, 0xAE, 0x8A, 0x41, 0x98, 0xF8, 0x72, 0x5D, 0x8C, 0x0E, 0x03
};

#define TIMER_START()       t0 = GetMonotonicTimestamp();
#define TIMER_END(_label)   \
    t1 = GetMonotonicTimestamp();\
    printf("%s cost %d ms\n", _label, t1 - t0);

int main(int argc, char *argv[])
{
    BIGNUM *a, *b, *c, *x;
    BN_CTX *ctx;
    uint32_t t0, t1;
    int ret;
    int len;
    char buf[512];

    a = BN_new();
    b = BN_new();
    c = BN_new();
    x = BN_new();
    ctx = BN_CTX_new();

    BN_bin2bn(kSRPGenerator5, sizeof(kSRPGenerator5), a);
    BN_bin2bn(secret, sizeof(secret), b);
    BN_bin2bn(kSRPModulus3072, sizeof(kSRPModulus3072), c);

    TIMER_START();
    ret = BN_mod_exp(x, a, b, c, ctx);
    TIMER_END("BN_mod_exp");
    printf("BN_mod_exp ret %d\n", ret);
}
'> ~/bn/bntest.c

cp -R ~/openssl/include ~/bn/
arm-linux-gnueabihf-gcc -o bntest -Iinclude bntest.c libcrypto.a -lpthread


评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值