[N32G45x] 【N32G45XVL-STB_V1.1】开发板评测2: 硬件密码单元测试

[复制链接]
 楼主| zhanzr21 发表于 2022-2-7 20:45 | 显示全部楼层 |阅读模式
国民技术的主要产品是专用加密芯片, 如U盾, 各种充值卡, 银**, 会员卡上的芯片. 这些芯片上都会涉及到加解密. 所以通用芯片上也有硬件加密单元也就不为奇.

根据用户手册, N32G457上的支持的硬件加密算法有:

算法支持 DES/3DES、 AES、 SHA1/SHA224/SHA256、 SM1、 SM3、 SM4F、 SM7、 MD5、 CRC16/CRC32、 TRNG
给对密码算法不熟的坛友简介一下子:

DES/3DES
这是对称加解密算法, 属于比较老的的算法种类, 目前已经在淘汰的边缘, 但是很多老产品还在使用. 之所以要淘汰, 是因为目前的硬件发展使得暴力**DES成本很低了.所谓对称算法是相对于非对称算法来说的, 对称算法中, 加解密过程是对称的, 密钥是通用的. 非对称算法中有私钥公钥之分. 非对称算法比对称算法要复杂, 所以手上拿的这个芯片没有相应的硬件加速单元, 成本和功能的平衡.
3DES是DES算法接连做三次, 增加暴力**成本, 其实也只是加个补丁. 目前有更好的算法供选择, 新产品中不建议使用DES/3DES算法.

AES
刚刚说到DES/3DES目前属于被淘汰的算法, AES就是主要替代算法. 理解为DES/3DES的升级版本即可.

SHA1/SHA224/SHA256/MD5

这都是哈希算法, 也叫散列算法, SHA1和MD5属于要淘汰的算法, 新产品不建议使用, 原因也是目前硬件暴力**的成本下降, 不安全.

SM1/SM4F/SM7

国家密码局颁布的商用密码, 都是对称算法, 用于商业金融领域, 简单理解为DES/3DES/AES的国内替代品即可.

SM3
SHA256的国内替代算法.


目前密码学界的共识是: SM系列算法安全度高于AES/SHA256系列. 但是AES/SH256是国际通用的, 遇到的可能性更大.

另外, S * M 是 商用密码的商密二字的汉语拼音缩写. 不知道论坛发贴让不让发, 有点歧义.

这些算法都可以用软件来实现, 但是用硬件实现后性能要高一点. 比如DES-EBC简单数百行即可实现. (EBC是对称加密算法的模式代号).
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. typedef unsigned char ubyte;

  5. #define KEY_LEN 8
  6. typedef ubyte simple_des_key_t[KEY_LEN];

  7. const static ubyte PC1[] = {
  8.     57, 49, 41, 33, 25, 17,  9,
  9.      1, 58, 50, 42, 34, 26, 18,
  10.     10,  2, 59, 51, 43, 35, 27,
  11.     19, 11,  3, 60, 52, 44, 36,
  12.     63, 55, 47, 39, 31, 23, 15,
  13.      7, 62, 54, 46, 38, 30, 22,
  14.     14,  6, 61, 53, 45, 37, 29,
  15.     21, 13,  5, 28, 20, 12,  4
  16. };

  17. const static ubyte PC2[] = {
  18.     14, 17, 11, 24,  1,  5,
  19.      3, 28, 15,  6, 21, 10,
  20.     23, 19, 12,  4, 26,  8,
  21.     16,  7, 27, 20, 13,  2,
  22.     41, 52, 31, 37, 47, 55,
  23.     30, 40, 51, 45, 33, 48,
  24.     44, 49, 39, 56, 34, 53,
  25.     46, 42, 50, 36, 29, 32
  26. };

  27. const static ubyte IP[] = {
  28.     58, 50, 42, 34, 26, 18, 10,  2,
  29.     60, 52, 44, 36, 28, 20, 12,  4,
  30.     62, 54, 46, 38, 30, 22, 14,  6,
  31.     64, 56, 48, 40, 32, 24, 16,  8,
  32.     57, 49, 41, 33, 25, 17,  9,  1,
  33.     59, 51, 43, 35, 27, 19, 11,  3,
  34.     61, 53, 45, 37, 29, 21, 13,  5,
  35.     63, 55, 47, 39, 31, 23, 15,  7
  36. };

  37. const static ubyte E[] = {
  38.     32,  1,  2,  3,  4,  5,
  39.      4,  5,  6,  7,  8,  9,
  40.      8,  9, 10, 11, 12, 13,
  41.     12, 13, 14, 15, 16, 17,
  42.     16, 17, 18, 19, 20, 21,
  43.     20, 21, 22, 23, 24, 25,
  44.     24, 25, 26, 27, 28, 29,
  45.     28, 29, 30, 31, 32,  1
  46. };

  47. const static ubyte S[][64] = {
  48.     {
  49.         14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
  50.          0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
  51.          4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
  52.         15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13
  53.     },
  54.     {
  55.         15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
  56.          3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
  57.          0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
  58.         13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9
  59.     },
  60.     {
  61.         10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
  62.         13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
  63.         13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
  64.          1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12
  65.     },
  66.     {
  67.          7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
  68.         13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
  69.         10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
  70.          3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14
  71.     },
  72.     {
  73.          2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
  74.         14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
  75.          4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
  76.         11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3
  77.     },
  78.     {
  79.         12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
  80.         10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
  81.          9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
  82.          4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13
  83.     },
  84.     {
  85.          4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
  86.         13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
  87.          1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
  88.          6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12
  89.     },
  90.     {
  91.         13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
  92.          1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
  93.          7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
  94.          2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11
  95.     }
  96. };

  97. const static ubyte P[] = {
  98.     16,  7, 20, 21,
  99.     29, 12, 28, 17,
  100.      1, 15, 23, 26,
  101.      5, 18, 31, 10,
  102.      2,  8, 24, 14,
  103.     32, 27,  3,  9,
  104.     19, 13, 30,  6,
  105.     22, 11,  4, 25
  106. };

  107. const static ubyte IP2[] = {
  108.     40,  8, 48, 16, 56, 24, 64, 32,
  109.     39,  7, 47, 15, 55, 23, 63, 31,
  110.     38,  6, 46, 14, 54, 22, 62, 30,
  111.     37,  5, 45, 13, 53, 21, 61, 29,
  112.     36,  4, 44, 12, 52, 20, 60, 28,
  113.     35,  3, 43, 11, 51, 19, 59, 27,
  114.     34,  2, 42, 10, 50, 18, 58, 26,
  115.     33,  1, 41,  9, 49, 17, 57, 25
  116. };

  117. const static ubyte SHIFTS[] = {
  118.     1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  119. };

  120. typedef struct {
  121.     ubyte *data;
  122.     int len;
  123. } String;

  124. /*
  125. * Transform a single nibble into a hex character
  126. *
  127. * in: a value < 0x10
  128. *
  129. * returns: the character that represents the nibble
  130. */
  131. static char toHex(ubyte in) {
  132.     if (0x00 <= in && in < 0x0A) {
  133.         return '0' + in;
  134.     }
  135.     if (0x0A <= in && in <= 0x0F) {
  136.         return 'A' + in - 0x0A;
  137.     }
  138.     return 0;
  139. }

  140. /*
  141. * Convert an array of bytes into a string
  142. *
  143. * ptr: the array of bytes
  144. * len: the number of bytes
  145. * out: a buffer allocated by the caller with enough space for 2*len+1 characters
  146. */
  147. static void printBytes(const ubyte *ptr, int len, char *out) {
  148.     while (len-- > 0) {
  149.         *out++ = toHex(*ptr >> 4);
  150.         *out++ = toHex(*ptr & 0x0F);

  151.         ptr++;
  152.     }
  153.     *out = 0;
  154. }

  155. /*
  156. * Gets the value of a bit in an array of bytes
  157. *
  158. * src: the array of bytes to index
  159. * index: the desired bit to test the value of
  160. *
  161. * returns: the bit at the specified position in the array
  162. */
  163. static int peekBit(const ubyte *src, int index) {
  164.     int cell = index / 8;
  165.     int bit = 7 - index % 8;
  166.     return (src[cell] & (1 << bit)) != 0;
  167. }

  168. /*
  169. * Sets the value of a bit in an array of bytes
  170. *
  171. * dst: the array of bits to set a bit in
  172. * index: the position of the bit to set
  173. * value: the value for the bit to set
  174. */
  175. static void pokeBit(ubyte *dst, int index, int value) {
  176.     int cell = index / 8;
  177.     int bit = 7 - index % 8;
  178.     if (value == 0) {
  179.         dst[cell] &= ~(1 << bit);
  180.     } else {
  181.         dst[cell] |= (1 << bit);
  182.     }
  183. }

  184. /*
  185. * Transforms one array of bytes by shifting the bits the specified number of positions
  186. *
  187. * src: the array to shift bits from
  188. * len: the length of the src array
  189. * times: the number of positions that the bits should be shifted
  190. * dst: a bytes array allocated by the caller to store the shifted values
  191. */
  192. static void shiftLeft(const ubyte *src, int len, int times, ubyte *dst) {
  193.     int i, t;
  194.     for (i = 0; i <= len; ++i) {
  195.         pokeBit(dst, i, peekBit(src, i));
  196.     }
  197.     for (t = 1; t <= times; ++t) {
  198.         int temp = peekBit(dst, 0);
  199.         for (i = 1; i <= len; ++i) {
  200.             pokeBit(dst, i - 1, peekBit(dst, i));
  201.         }
  202.         pokeBit(dst, len - 1, temp);
  203.     }
  204. }

  205. /*
  206. * Calculates the sub keys to be used in processing the messages
  207. *
  208. * key: the array of bytes representing the key
  209. * ks: the subkeys that have been allocated by the caller
  210. */
  211. typedef ubyte subkey_t[17][6]; /* 17 sets of 48 bits */
  212. static void getSubKeys(const simple_des_key_t key, subkey_t ks) {
  213.     ubyte c[17][7];  /* 56 bits */
  214.     ubyte d[17][4];  /* 28 bits */
  215.     ubyte kp[7];
  216.     int i, j;

  217.     /* intialize */
  218.     memset(c, 0, sizeof(c));
  219.     memset(d, 0, sizeof(d));
  220.     memset(ks, 0, sizeof(subkey_t));

  221.     /* permute 'key' using table PC1 */
  222.     for (i = 0; i < 56; ++i) {
  223.         pokeBit(kp, i, peekBit(key, PC1[i] - 1));
  224.     }

  225.     /* split 'kp' in half and process the resulting series of 'c' and 'd' */
  226.     for (i = 0; i < 28; ++i) {
  227.         pokeBit(c[0], i, peekBit(kp, i));
  228.         pokeBit(d[0], i, peekBit(kp, i + 28));
  229.     }

  230.     /* shift the components of c and d */
  231.     for (i = 1; i < 17; ++i) {
  232.         shiftLeft(c[i - 1], 28, SHIFTS[i - 1], c[i]);
  233.         shiftLeft(d[i - 1], 28, SHIFTS[i - 1], d[i]);
  234.     }

  235.     /* merge 'd' into 'c' */
  236.     for (i = 1; i < 17; ++i) {
  237.         for (j = 28; j < 56; ++j) {
  238.             pokeBit(c[i], j, peekBit(d[i], j - 28));
  239.         }
  240.     }

  241.     /* form the sub-keys and store them in 'ks'
  242.      * permute 'c' using table PC2 */
  243.     for (i = 1; i < 17; ++i) {
  244.         for (j = 0; j < 48; ++j) {
  245.             pokeBit(ks[i], j, peekBit(c[i], PC2[j] - 1));
  246.         }
  247.     }
  248. }

  249. /*
  250. * Function used in processing the messages
  251. *
  252. * r: an array of bytes to be processed
  253. * ks: one of the subkeys to be used for processing
  254. * sp: output from the processing
  255. */
  256. static void f(ubyte *r, ubyte *ks, ubyte *sp) {
  257.     ubyte er[6]; /* 48 bits */
  258.     ubyte sr[4]; /* 32 bits */
  259.     int i;

  260.     /* initialize */
  261.     memset(er, 0, sizeof(er));
  262.     memset(sr, 0, sizeof(sr));

  263.     /* permute 'r' using table E */
  264.     for (i = 0; i < 48; ++i) {
  265.         pokeBit(er, i, peekBit(r, E[i] - 1));
  266.     }

  267.     /* xor 'er' with 'ks' and store back into 'er' */
  268.     for (i = 0; i < 6; ++i) {
  269.         er[i] ^= ks[i];
  270.     }

  271.     /* process 'er' six bits at a time and store resulting four bits in 'sr' */
  272.     for (i = 0; i < 8; ++i) {
  273.         int j = i * 6;
  274.         int b[6];
  275.         int k, row, col, m, n;

  276.         for (k = 0; k < 6; ++k) {
  277.             b[k] = peekBit(er, j + k) != 0 ? 1 : 0;
  278.         }

  279.         row = 2 * b[0] + b[5];
  280.         col = 8 * b[1] + 4 * b[2] + 2 * b[3] + b[4];
  281.         m = S[i][row * 16 + col]; /* apply table s */
  282.         n = 1;

  283.         while (m > 0) {
  284.             int p = m % 2;
  285.             pokeBit(sr, (i + 1) * 4 - n, p == 1);
  286.             m /= 2;
  287.             n++;
  288.         }
  289.     }

  290.     /* permute sr using table P */
  291.     for (i = 0; i < 32; ++i) {
  292.         pokeBit(sp, i, peekBit(sr, P[i] - 1));
  293.     }
  294. }

  295. /*
  296. * Processing of block of the message
  297. *
  298. * message: an 8 byte block from the message
  299. * ks: the subkeys to use in processing
  300. * ep: space for an encoded 8 byte block allocated by the caller
  301. */
  302. static void processMessage(const ubyte *message, subkey_t ks, ubyte *ep) {
  303.     ubyte left[17][4];  /* 32 bits */
  304.     ubyte right[17][4]; /* 32 bits */
  305.     ubyte mp[8];        /* 64 bits */
  306.     ubyte e[8];         /* 64 bits */
  307.     int i, j;

  308.     /* permute 'message' using table IP */
  309.     for (i = 0; i < 64; ++i) {
  310.         pokeBit(mp, i, peekBit(message, IP[i] - 1));
  311.     }

  312.     /* split 'mp' in half and process the resulting series of 'l' and 'r */
  313.     for (i = 0; i < 32; ++i) {
  314.         pokeBit(left[0], i, peekBit(mp, i));
  315.         pokeBit(right[0], i, peekBit(mp, i + 32));
  316.     }
  317.     for (i = 1; i < 17; ++i) {
  318.         ubyte fs[4]; /* 32 bits */

  319.         memcpy(left[i], right[i - 1], 4);
  320.         f(right[i - 1], ks[i], fs);
  321.         for (j = 0; j < 4; ++j) {
  322.             left[i - 1][j] ^= fs[j];
  323.         }
  324.         memcpy(right[i], left[i - 1], 4);
  325.     }

  326.     /* amalgamate r[16] and l[16] (in that order) into 'e' */
  327.     for (i = 0; i < 32; ++i) {
  328.         pokeBit(e, i, peekBit(right[16], i));
  329.     }
  330.     for (i = 32; i < 64; ++i) {
  331.         pokeBit(e, i, peekBit(left[16], i - 32));
  332.     }

  333.     /* permute 'e' using table IP2 ad return result as a hex string */
  334.     for (i = 0; i < 64; ++i) {
  335.         pokeBit(ep, i, peekBit(e, IP2[i] - 1));
  336.     }
  337. }

  338. /*
  339. * Encrypts a message using DES
  340. *
  341. * key: the key to use to encrypt the message
  342. * message: the message to be encrypted
  343. * len: the length of the message
  344. *
  345. * returns: a paring of dynamically allocated memory for the encoded message,
  346. *          and the length of the encoded message.
  347. *          the caller will need to free the memory after use.
  348. */
  349. String encrypt(const simple_des_key_t key, const ubyte *message, int len) {
  350.     String result = { 0, 0 };
  351.     subkey_t ks;
  352.     ubyte padByte;
  353.     int i;

  354.     getSubKeys(key, ks);

  355.     padByte = 8 - len % 8;
  356.     result.len = len + padByte;
  357.     result.data = (ubyte*)malloc(result.len);
  358.     memcpy(result.data, message, len);
  359.     memset(&result.data[len], padByte, padByte);

  360.     for (i = 0; i < result.len; i += 8) {
  361.         processMessage(&result.data[i], ks, &result.data[i]);
  362.     }

  363.     return result;
  364. }

  365. /*
  366. * Decrypts a message using DES
  367. *
  368. * key: the key to use to decrypt the message
  369. * message: the message to be decrypted
  370. * len: the length of the message
  371. *
  372. * returns: a paring of dynamically allocated memory for the decoded message,
  373. *          and the length of the decoded message.
  374. *          the caller will need to free the memory after use.
  375. */
  376. String decrypt(const simple_des_key_t key, const ubyte *message, int len) {
  377.     String result = { 0, 0 };
  378.     subkey_t ks;
  379.     int i, j;
  380.     ubyte padByte;

  381.     getSubKeys(key, ks);
  382.     /* reverse the subkeys */
  383.     for (i = 1; i < 9; ++i) {
  384.         for (j = 0; j < 6; ++j) {
  385.             ubyte temp = ks[i][j];
  386.             ks[i][j] = ks[17 - i][j];
  387.             ks[17 - i][j] = temp;
  388.         }
  389.     }

  390.     result.data = (ubyte*)malloc(len);
  391.     memcpy(result.data, message, len);
  392.     result.len = len;
  393.     for (i = 0; i < result.len; i += 8) {
  394.         processMessage(&result.data[i], ks, &result.data[i]);
  395.     }

  396.     padByte = result.data[len - 1];
  397.     result.len -= padByte;
  398.     return result;
  399. }

  400. /*
  401. * Convienience method for showing the round trip processing of a message
  402. */
  403. void driver(const simple_des_key_t key, const ubyte *message, int len) {
  404.     String encoded, decoded;
  405.     char buffer[128];

  406.     printBytes(key, KEY_LEN, buffer);
  407.     printf("Key     : %s\n", buffer);

  408.     printBytes(message, len, buffer);
  409.     printf("Message : %s\n", buffer);

  410.     encoded = encrypt(key, message, len);
  411.     printBytes(encoded.data, encoded.len, buffer);
  412.     printf("Encoded : %s\n", buffer);

  413.     decoded = decrypt(key, encoded.data, encoded.len);
  414.     printBytes(decoded.data, decoded.len, buffer);
  415.     printf("Decoded : %s\n\n", buffer);

  416.     /* release allocated memory */
  417.     if (encoded.len > 0) {
  418.         free(encoded.data);
  419.         encoded.data = 0;
  420.     }
  421.     if (decoded.len > 0) {
  422.         free(decoded.data);
  423.         decoded.data = 0;
  424.     }
  425. }

  426. int test_soft_des_ebc(void) {
  427.     const simple_des_key_t keys[] = {
  428.         {0x13, 0x34, 0x57, 0x79, 0x9B, 0xBC, 0xDF, 0xF1},
  429.         {0x0E, 0x32, 0x92, 0x32, 0xEA, 0x6D, 0x0D, 0x73},
  430.         {0x0E, 0x32, 0x92, 0x32, 0xEA, 0x6D, 0x0D, 0x73}
  431.     };
  432.     const ubyte message1[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
  433.     const ubyte message2[] = { 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87 };
  434.     const ubyte message3[] = { 0x59, 0x6F, 0x75, 0x72, 0x20, 0x6C, 0x69, 0x70, 0x73, 0x20, 0x61, 0x72, 0x65, 0x20, 0x73, 0x6D, 0x6F, 0x6F, 0x74, 0x68, 0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6E, 0x20, 0x76, 0x61, 0x73, 0x65, 0x6C, 0x69, 0x6E, 0x65, 0x0D, 0x0A };
  435.     int len;

  436.     len = sizeof(message1) / sizeof(ubyte);
  437.     driver(keys[0], message1, len);

  438.     len = sizeof(message2) / sizeof(ubyte);
  439.     driver(keys[1], message2, len);

  440.     len = sizeof(message3) / sizeof(ubyte);
  441.     driver(keys[2], message3, len);
  442.     return 0;
  443. }
硬件实现的上述算法, 主要是从性能和编程的易用性考虑. 官方提供的Demo中没有商密系列的例子, 如果要使用这些算法的硬件加速单元, 可以和销售/技术支持联系.

还有上面的列表中说明支持TRNG
这个是真随机数发生器, 也是密码学算法中要使用的, 密码学中使用随机数, 既需要真随机也需要伪随机. 两者区别在于真随机数使用热噪音作为种子, 伪随机数使用特定的输入作为种子. 数学理论上没有真随机数, 类似于无法实现数学中的1/3分压,分频一样. 之所以称作TRNG, 是通过了一定的密码学测试标准, 国内/国际都有相应的测试标准.
  1. uint32_t GetPseudoRand_U32(uint32_t *rand, uint32_t wordLen,uint32_t seed[2]);
  2. uint32_t GetTrueRand_U32(uint32_t *rand, uint32_t wordLen);
同学们可能会说C语言的库中也有随机数函数:
  1. void srand(unsigned int _Seed);
  2. int rand(void);
为何不直接使用? 原因还是密码学标准, 不管国际还是国内的密码学标准, 都不会依赖某个libc的随机数实现. libC中的实现只能用于最简单的场景, 如模拟输入测试等等.


文末例子工程中hsm_test分支是硬件加速单元和几个软件算法实现的例子. 可供参考.

以后有闲暇会做软件/硬件 加密算法的benchmark. 本贴先到此为止.

例子代码下载地址点此






6552918 发表于 2022-2-7 22:25 | 显示全部楼层
这块是国民的优势,应该好好发挥一下
麻花油条 发表于 2022-2-8 11:04 来自手机 | 显示全部楼层
优势就应该好好发挥
单片小菜 发表于 2022-2-8 16:28 | 显示全部楼层
加密算法都是通用的吧?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:每天都進步

91

主题

1017

帖子

34

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:每天都進步

91

主题

1017

帖子

34

粉丝
快速回复 在线客服 返回列表 返回顶部