1 /*
2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License); you may
5 * not use this file except in compliance with the License.
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 */
9
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdint.h>
15 #include <gmssl/hex.h>
16 #include <gmssl/digest.h>
17 #include <gmssl/hash_drbg.h>
18 #include <gmssl/error.h>
19
20
21 #define EntropyInput "212956390783381dbfc6362dd0da9a09"
22 #define Nonce "5280987fc5e27a49"
23 #define PersonalizationString ""
24 #define AdditionalInput ""
25 #define V0 "02b84eba8121ca090b6b66d3371609eaf76405a5c2807d80035c1a13dfed5aa18e536af599a7b3c68b2c56240ed11997f4048910d84604"
26 #define C0 "a677e4921587563eebe55d1b25e59c3f3d200bc61aaee665e7a6858c2857c45dba4bce8182252962ae86de491046a5e3450eec44938a0a"
27
28 #define AdditionalInput1 ""
29 #define EntropyInputPR1 "2edb396eeb8960f77943c2a59075a786"
30 #define V1 "f9afadfbbf2c3d1004f9baca38be247342e5fbb83281915d5de18beb963712a344e89bb0e6b925a7bbc32eadb8b441efc1fa0c649df42a"
31 #define C1 "1d41cbbd634909e4761c232fcfd6a6c2edf0a7f4d3d3c164f74a88955f355efce2d86c1e9fa897b7005ef9d4d3a51bf4fc0b805ab896c9"
32
33 #define PR1 "2edb396eeb8960f77943c2a59075a786"
34 #define PR2 "30b565b63a5012676940d3ef17d9e996"
35
36
main(void)37 int main(void)
38 {
39 HASH_DRBG drbg;
40
41 uint8_t entropy[sizeof(EntropyInput)/2];
42 uint8_t nonce[sizeof(Nonce)/2];
43 uint8_t personalstr[sizeof(PersonalizationString)/2];
44 uint8_t v[sizeof(V0)/2];
45 uint8_t c[sizeof(C0)/2];
46 uint8_t entropy_pr1[sizeof(EntropyInputPR1)/2];
47 uint8_t pr1[sizeof(PR1)/2];
48 uint8_t pr2[sizeof(PR2)/2];
49 size_t entropy_len, nonce_len, personalstr_len, vlen, clen;
50 size_t entropy_pr1len;
51 size_t pr1_len, pr2_len;
52 unsigned char out[640/8];
53 int i;
54
55 hex_to_bytes(EntropyInput, strlen(EntropyInput), entropy, &entropy_len);
56 hex_to_bytes(Nonce, strlen(Nonce), nonce, &nonce_len);
57 hex_to_bytes(PersonalizationString, strlen(PersonalizationString), personalstr, &personalstr_len);
58 hex_to_bytes(V0, strlen(V0), v, &vlen);
59 hex_to_bytes(C0, strlen(C0), c, &clen);
60 hex_to_bytes(EntropyInputPR1, strlen(EntropyInputPR1), entropy_pr1, &entropy_pr1len);
61 hex_to_bytes(PR1, strlen(PR1), pr1, &pr1_len);
62 hex_to_bytes(PR2, strlen(PR2), pr2, &pr2_len);
63
64 hash_drbg_init(&drbg, DIGEST_sha1(),
65 entropy, entropy_len,
66 nonce, nonce_len,
67 personalstr, personalstr_len);
68
69 printf("sha1_drbg test 1 ");
70 if (drbg.seedlen != vlen
71 || memcmp(drbg.V, v, vlen) != 0
72 || memcmp(drbg.C, c, clen) != 0
73 || drbg.reseed_counter != 1) {
74 printf("failed\n");
75 return 1;
76 } else {
77 printf("ok\n");
78 }
79
80 hash_drbg_reseed(&drbg, pr1, pr1_len, NULL, 0);
81 hash_drbg_generate(&drbg, NULL, 0, 640/8, out);
82
83 hash_drbg_reseed(&drbg, pr2, pr2_len, NULL, 0);
84 hash_drbg_generate(&drbg, NULL, 0, 640/8, out);
85
86 for (i = 0; i < sizeof(out); i++) {
87 printf("%02x", out[i]);
88 }
89 printf("\n");
90
91 return 0;
92 }
93