Guest User

Untitled

a guest
Nov 29th, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.07 KB | None | 0 0
  1. 1,3,5,7 - это сообщения Enrolle
  2. #Building Message M1
  3. static struct wpabuf * wps_build_m1(struct wps_data *wps)
  4. {
  5.     struct wpabuf *msg;
  6.     u16 methods;
  7.  
  8.     if (os_get_random(wps->nonce_e, WPS_NONCE_LEN) < 0)
  9.         return NULL;
  10.     wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
  11.             wps->nonce_e, WPS_NONCE_LEN);
  12.  
  13.     wpa_printf(MSG_DEBUG, "WPS: Building Message M1");
  14.     msg = wpabuf_alloc(1000);
  15.     if (msg == NULL)
  16.         return NULL;
  17.  
  18.     methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
  19.     if (wps->pbc)
  20.         methods |= WPS_CONFIG_PUSHBUTTON;
  21.  
  22.     if (wps_build_version(msg) ||
  23.         wps_build_msg_type(msg, WPS_M1) ||
  24.         wps_build_uuid_e(msg, wps->uuid_e) ||
  25.         wps_build_mac_addr(wps, msg) ||
  26.         wps_build_enrollee_nonce(wps, msg) ||
  27.         wps_build_public_key(wps, msg) ||
  28.         wps_build_auth_type_flags(wps, msg) ||
  29.         wps_build_encr_type_flags(wps, msg) ||
  30.         wps_build_conn_type_flags(wps, msg) ||
  31.         wps_build_config_methods(msg, methods) ||
  32.         wps_build_wps_state(wps, msg) ||
  33.         wps_build_device_attrs(&wps->wps->dev, msg) ||
  34.         wps_build_rf_bands(&wps->wps->dev, msg) ||
  35.         wps_build_assoc_state(wps, msg) ||
  36.         wps_build_dev_password_id(msg, wps->dev_pw_id) ||
  37.         wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
  38.         wps_build_os_version(&wps->wps->dev, msg)) {
  39.         wpabuf_free(msg);
  40.         return NULL;
  41.     }
  42.  
  43.     wps->state = RECV_M2;
  44.     return msg;
  45. }
  46.  
  47. #Building Message M3
  48. static struct wpabuf * wps_build_m3(struct wps_data *wps)
  49. {
  50.     struct wpabuf *msg;
  51.  
  52.     wpa_printf(MSG_DEBUG, "WPS: Building Message M3");
  53.  
  54.     if (wps->dev_password == NULL) {
  55.         wpa_printf(MSG_DEBUG, "WPS: No Device Password available");
  56.         return NULL;
  57.     }
  58.     wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
  59.  
  60.     msg = wpabuf_alloc(1000);
  61.     if (msg == NULL)
  62.         return NULL;
  63.  
  64.     if (wps_build_version(msg) ||
  65.         wps_build_msg_type(msg, WPS_M3) ||
  66.         wps_build_registrar_nonce(wps, msg) ||
  67.         wps_build_e_hash(wps, msg) ||
  68.         wps_build_authenticator(wps, msg)) {
  69.         wpabuf_free(msg);
  70.         return NULL;
  71.     }
  72.  
  73.     wps->state = RECV_M4;
  74.     return msg;
  75. }
  76.  
  77. #Building Message M5
  78. static struct wpabuf * wps_build_m5(struct wps_data *wps)
  79. {
  80.     struct wpabuf *msg, *plain;
  81.  
  82.     wpa_printf(MSG_DEBUG, "WPS: Building Message M5");
  83.  
  84.     plain = wpabuf_alloc(200);
  85.     if (plain == NULL)
  86.         return NULL;
  87.  
  88.     msg = wpabuf_alloc(1000);
  89.     if (msg == NULL) {
  90.         wpabuf_free(plain);
  91.         return NULL;
  92.     }
  93.  
  94.     if (wps_build_version(msg) ||
  95.         wps_build_msg_type(msg, WPS_M5) ||
  96.         wps_build_registrar_nonce(wps, msg) ||
  97.         wps_build_e_snonce1(wps, plain) ||
  98.         wps_build_key_wrap_auth(wps, plain) ||
  99.         wps_build_encr_settings(wps, msg, plain) ||
  100.         wps_build_authenticator(wps, msg)) {
  101.         wpabuf_free(plain);
  102.         wpabuf_free(msg);
  103.         return NULL;
  104.     }
  105.     wpabuf_free(plain);
  106.  
  107.     wps->state = RECV_M6;
  108.     return msg;
  109. }
  110.  
  111.  
  112. #Building Message M7
  113. static struct wpabuf * wps_build_m7(struct wps_data *wps)
  114. {
  115.     struct wpabuf *msg, *plain;
  116.  
  117.     wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
  118.  
  119.     plain = wpabuf_alloc(500 + wps->wps->ap_settings_len);
  120.     if (plain == NULL)
  121.         return NULL;
  122.  
  123.     msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len);
  124.     if (msg == NULL) {
  125.         wpabuf_free(plain);
  126.         return NULL;
  127.     }
  128.  
  129.     if (wps_build_version(msg) ||
  130.         wps_build_msg_type(msg, WPS_M7) ||
  131.         wps_build_registrar_nonce(wps, msg) ||
  132.         wps_build_e_snonce2(wps, plain) ||
  133.         (wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
  134.         wps_build_key_wrap_auth(wps, plain) ||
  135.         wps_build_encr_settings(wps, msg, plain) ||
  136.         wps_build_authenticator(wps, msg)) {
  137.         wpabuf_free(plain);
  138.         wpabuf_free(msg);
  139.         return NULL;
  140.     }
  141.     wpabuf_free(plain);
  142.  
  143.     wps->state = RECV_M8;
  144.     return msg;
  145. }
  146.  
  147.  
  148.  
  149. --------------------------------------------------------------------------=============---------------------------------------------
  150. 2,4,6,8 - сообщения Registrar
  151. #Building Message M2
  152. static struct wpabuf * wps_build_m2(struct wps_data *wps)
  153. {
  154.     struct wpabuf *msg;
  155.  
  156.     if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
  157.         return NULL;
  158.     wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
  159.             wps->nonce_r, WPS_NONCE_LEN);
  160.     wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
  161.  
  162.     wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
  163.     msg = wpabuf_alloc(1000);
  164.     if (msg == NULL)
  165.         return NULL;
  166.  
  167.     if (wps_build_version(msg) ||
  168.         wps_build_msg_type(msg, WPS_M2) ||
  169.         wps_build_enrollee_nonce(wps, msg) ||
  170.         wps_build_registrar_nonce(wps, msg) ||
  171.         wps_build_uuid_r(wps, msg) ||
  172.         wps_build_public_key(wps, msg) ||
  173.         wps_derive_keys(wps) ||
  174.         wps_build_auth_type_flags(wps, msg) ||
  175.         wps_build_encr_type_flags(wps, msg) ||
  176.         wps_build_conn_type_flags(wps, msg) ||
  177.         wps_build_config_methods_r(wps->wps->registrar, msg) ||
  178.         wps_build_device_attrs(&wps->wps->dev, msg) ||
  179.         wps_build_rf_bands(&wps->wps->dev, msg) ||
  180.         wps_build_assoc_state(wps, msg) ||
  181.         wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
  182.         wps_build_dev_password_id(msg, wps->dev_pw_id) ||
  183.         wps_build_os_version(&wps->wps->dev, msg) ||
  184.         wps_build_authenticator(wps, msg)) {
  185.         wpabuf_free(msg);
  186.         return NULL;
  187.     }
  188.  
  189.     wps->state = RECV_M3;
  190.     return msg;
  191. }
  192.  
  193. #Building Message M4
  194. static struct wpabuf * wps_build_m4(struct wps_data *wps)
  195. {
  196.     struct wpabuf *msg, *plain;
  197.  
  198.     wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
  199.  
  200.     wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
  201.  
  202.     plain = wpabuf_alloc(200);
  203.     if (plain == NULL)
  204.         return NULL;
  205.  
  206.     msg = wpabuf_alloc(1000);
  207.     if (msg == NULL) {
  208.         wpabuf_free(plain);
  209.         return NULL;
  210.     }
  211.  
  212.     if (wps_build_version(msg) ||
  213.         wps_build_msg_type(msg, WPS_M4) ||
  214.         wps_build_enrollee_nonce(wps, msg) ||
  215.         wps_build_r_hash(wps, msg) ||
  216.         wps_build_r_snonce1(wps, plain) ||
  217.         wps_build_key_wrap_auth(wps, plain) ||
  218.         wps_build_encr_settings(wps, msg, plain) ||
  219.         wps_build_authenticator(wps, msg)) {
  220.         wpabuf_free(plain);
  221.         wpabuf_free(msg);
  222.         return NULL;
  223.     }
  224.     wpabuf_free(plain);
  225.  
  226.     wps->state = RECV_M5;
  227.     return msg;
  228. }
  229.  
  230. #Building Message M6
  231. static struct wpabuf * wps_build_m6(struct wps_data *wps)
  232. {
  233.     struct wpabuf *msg, *plain;
  234.  
  235.     wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
  236.  
  237.     plain = wpabuf_alloc(200);
  238.     if (plain == NULL)
  239.         return NULL;
  240.  
  241.     msg = wpabuf_alloc(1000);
  242.     if (msg == NULL) {
  243.         wpabuf_free(plain);
  244.         return NULL;
  245.     }
  246.  
  247.     if (wps_build_version(msg) ||
  248.         wps_build_msg_type(msg, WPS_M6) ||
  249.         wps_build_enrollee_nonce(wps, msg) ||
  250.         wps_build_r_snonce2(wps, plain) ||
  251.         wps_build_key_wrap_auth(wps, plain) ||
  252.         wps_build_encr_settings(wps, msg, plain) ||
  253.         wps_build_authenticator(wps, msg)) {
  254.         wpabuf_free(plain);
  255.         wpabuf_free(msg);
  256.         return NULL;
  257.     }
  258.     wpabuf_free(plain);
  259.  
  260.     wps->wps_pin_revealed = 1;
  261.     wps->state = RECV_M7;
  262.     return msg;
  263. }
  264.  
  265. #Building Message M8
  266. static struct wpabuf * wps_build_m8(struct wps_data *wps)
  267. {
  268.     struct wpabuf *msg, *plain;
  269.  
  270.     wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
  271.  
  272.     plain = wpabuf_alloc(500);
  273.     if (plain == NULL)
  274.         return NULL;
  275.  
  276.     msg = wpabuf_alloc(1000);
  277.     if (msg == NULL) {
  278.         wpabuf_free(plain);
  279.         return NULL;
  280.     }
  281.  
  282.     if (wps_build_version(msg) ||
  283.         wps_build_msg_type(msg, WPS_M8) ||
  284.         wps_build_enrollee_nonce(wps, msg) ||
  285.         (wps->wps->ap && wps_build_cred(wps, plain)) ||
  286.         (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
  287.         wps_build_key_wrap_auth(wps, plain) ||
  288.         wps_build_encr_settings(wps, msg, plain) ||
  289.         wps_build_authenticator(wps, msg)) {
  290.         wpabuf_free(plain);
  291.         wpabuf_free(msg);
  292.         return NULL;
  293.     }
  294.     wpabuf_free(plain);
  295.  
  296.     wps->state = RECV_DONE;
  297.     return msg;
  298. }
  299.  
  300.  
  301.  
  302. Enrolle и Registrar используют похожие функции построения nonce
  303. int wps_build_enrollee_nonce(struct wps_data *wps, struct wpabuf *msg)
  304. {
  305.     wpa_printf(MSG_DEBUG, "WPS:  * Enrollee Nonce");
  306.     wpabuf_put_be16(msg, ATTR_ENROLLEE_NONCE);
  307.     wpabuf_put_be16(msg, WPS_NONCE_LEN);
  308.     wpabuf_put_data(msg, wps->nonce_e, WPS_NONCE_LEN);
  309.     return 0;
  310. }
  311.  
  312. и Enrolle и Registrar испольуют для генерации nonce E и nonce R следующие функции
  313.  
  314. static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
  315. {
  316.     u8 hash[SHA256_MAC_LEN];
  317.     const u8 *addr[4];
  318.     size_t len[4];
  319.  
  320.     if (e_snonce1 == NULL) {
  321.         wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
  322.         return -1;
  323.     }
  324.  
  325.     wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
  326.             WPS_SECRET_NONCE_LEN);
  327.  
  328.     /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
  329.     addr[0] = e_snonce1;
  330.     len[0] = WPS_SECRET_NONCE_LEN;
  331.     addr[1] = wps->psk1;
  332.     len[1] = WPS_PSK_LEN;
  333.     addr[2] = wpabuf_head(wps->dh_pubkey_e);
  334.     len[2] = wpabuf_len(wps->dh_pubkey_e);
  335.     addr[3] = wpabuf_head(wps->dh_pubkey_r);
  336.     len[3] = wpabuf_len(wps->dh_pubkey_r);
  337.     hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  338.  
  339.     if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
  340.         wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
  341.                "not match with the pre-committed value");
  342.         wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
  343.         wps_pwd_auth_fail_event(wps->wps, 0, 1);
  344.         return -1;
  345.     }
  346.  
  347.     wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
  348.            "half of the device password");
  349.  
  350.     return 0;
  351. }
  352.  
  353. static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
  354. {
  355.     u8 hash[SHA256_MAC_LEN];
  356.     const u8 *addr[4];
  357.     size_t len[4];
  358.  
  359.     if (e_snonce2 == NULL) {
  360.         wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
  361.         return -1;
  362.     }
  363.  
  364.     wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
  365.             WPS_SECRET_NONCE_LEN);
  366.  
  367.     /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
  368.     addr[0] = e_snonce2;
  369.     len[0] = WPS_SECRET_NONCE_LEN;
  370.     addr[1] = wps->psk2;
  371.     len[1] = WPS_PSK_LEN;
  372.     addr[2] = wpabuf_head(wps->dh_pubkey_e);
  373.     len[2] = wpabuf_len(wps->dh_pubkey_e);
  374.     addr[3] = wpabuf_head(wps->dh_pubkey_r);
  375.     len[3] = wpabuf_len(wps->dh_pubkey_r);
  376.     hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  377.  
  378.     if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
  379.         wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
  380.                "not match with the pre-committed value");
  381.         wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
  382.         wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
  383.         wps_pwd_auth_fail_event(wps->wps, 0, 2);
  384.         return -1;
  385.     }
  386.  
  387.     wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
  388.            "half of the device password");
  389.     wps->wps_pin_revealed = 0;
  390.     wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
  391.  
  392.     return 0;
  393. }
  394.  
  395.  
  396. # и генерят через функции
  397. static int wps_build_e_snonce1(struct wps_data *wps, struct wpabuf *msg)
  398. {
  399.     wpa_printf(MSG_DEBUG, "WPS:  * E-SNonce1");
  400.     wpabuf_put_be16(msg, ATTR_E_SNONCE1);
  401.     wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
  402.     wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
  403.     return 0;
  404. }
  405.  
  406.  
  407. static int wps_build_e_snonce2(struct wps_data *wps, struct wpabuf *msg)
  408. {
  409.     wpa_printf(MSG_DEBUG, "WPS:  * E-SNonce2");
  410.     wpabuf_put_be16(msg, ATTR_E_SNONCE2);
  411.     wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
  412.     wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
  413.             WPS_SECRET_NONCE_LEN);
  414.     return 0;
  415. }
  416.  
  417. wpabuf_put кладет значение nonce в буфер
  418. еперед этим сгенерировав обычной функцией
  419. /**
  420.      * os_random - Get pseudo random value (not necessarily very strong)
  421.      * Returns: Pseudo random value
  422.      */
  423.     unsigned long os_random(void)
  424.         {
  425.             return random();
  426.         }
  427.  
  428.  
  429. --------------------------==================----------------------------
  430. Supported devices:
  431.  
  432. RT-N66U
  433. RT-AC66U
  434. RT-AC66U_B1 (use the RT-AC68U firmware)
  435. RT-AC56U
  436. RT-AC68U, RT-AC68P, RT-AC68UF (including HW revision C1 and E1)
  437. RT-AC87U
  438. RT-AC3200
  439. RT-AC88U
  440. RT-AC3100
  441. RT-AC5300
  442. RT-AC1900 & RT-AC1900P (use the RT-AC68U firmware)
  443. Supported devices:
  444.  
  445. RT-AC88U
  446. RT-AC3100
  447. RT-AC86U
  448. No longer supported:
  449. RT-N16
  450.  
  451. void linux_random(uint8 *rand, int len)
  452. {
  453.     static int dev_random_fd = -1;
  454.     int status;
  455.     int i;
  456.  
  457.     if (dev_random_fd == -1)
  458.         dev_random_fd = open("/dev/urandom", O_RDONLY|O_NONBLOCK);
  459.  
  460.     assert(dev_random_fd != -1);
  461.  
  462.     for (i = 0; i < RANDOM_READ_TRY_MAX; i++) {
  463.         status = read(dev_random_fd, rand, len);
  464.         if (status == -1) {
  465.             if (errno == EINTR)
  466.                 continue;
  467.  
  468.             assert(status != -1);
  469.         }
  470.  
  471.         return;
  472.     }
  473.  
  474.     assert(i != RANDOM_READ_TRY_MAX);
  475. }
  476. #elif __ECOS
  477. void RAND_ecos_init()
  478. {
  479.     BN_register_RAND(generic_random);
  480. }
  481.  
  482. #elif WIN32
  483. void RAND_windows_init()
  484. {
  485.     BN_register_RAND(windows_random);
  486. }
  487.  
  488. void windows_random(uint8 *rand, int len)
  489. {
  490.     /* Declare and initialize variables */
  491.  
  492.     HCRYPTPROV hCryptProv = NULL;
  493.     LPCSTR UserName = "{56E9D11F-76B8-42fa-8645-76980E4E8648}";
  494.  
  495.     /*
  496.     Attempt to acquire a context and a key
  497.     container. The context will use the default CSP
  498.     for the RSA_FULL provider type. DwFlags is set to 0
  499.     to attempt to open an existing key container.
  500.     */
  501.     if (CryptAcquireContext(&hCryptProv,
  502.         UserName,
  503.         NULL,
  504.         PROV_RSA_FULL,
  505.         0))
  506.     {
  507.         /* do nothing */
  508.     }
  509.     else
  510.     {
  511.         /*
  512.         An error occurred in acquiring the context. This could mean
  513.         that the key container requested does not exist. In this case,
  514.         the function can be called again to attempt to create a new key
  515.         container. Error codes are defined in winerror.h.
  516.         */
  517.         if (GetLastError() == NTE_BAD_KEYSET)
  518.         {
  519.             if (!CryptAcquireContext(&hCryptProv,
  520.                 UserName,
  521.                 NULL,
  522.                 PROV_RSA_FULL,
  523.                 CRYPT_NEWKEYSET))
  524.             {
  525.                 printf("Could not create a new key container.\n");
  526.             }
  527.         }
  528.         else
  529.         {
  530.             printf("A cryptographic service handle could not be acquired.\n");
  531.         }
  532.     }
  533.  
  534.     if (hCryptProv)
  535.     {
  536.         /* Generate a random initialization vector. */
  537.         if (!CryptGenRandom(hCryptProv, len, rand))
  538.         {
  539.             printf("Error during CryptGenRandom.\n");
  540.         }
  541.         if (!CryptReleaseContext(hCryptProv, 0))
  542.             printf("Failed CryptReleaseContext\n");
  543.     }
  544.     return;
  545. }
  546.  
  547. #как я понял для realtek 2860 и 2870
  548. void GenRandom(struct rt_rtmp_adapter *pAd, u8 * macAddr, u8 * random)
  549. {
  550.     int i, curr;
  551.     u8 local[80], KeyCounter[32];
  552.     u8 result[80];
  553.     unsigned long CurrentTime;
  554.     u8 prefix[] =
  555.         { 'I', 'n', 'i', 't', ' ', 'C', 'o', 'u', 'n', 't', 'e', 'r' };
  556.  
  557.     /* Zero the related information */
  558.     NdisZeroMemory(result, 80);
  559.     NdisZeroMemory(local, 80);
  560.     NdisZeroMemory(KeyCounter, 32);
  561.  
  562.     for (i = 0; i < 32; i++) {
  563.         /* copy the local MAC address */
  564.         COPY_MAC_ADDR(local, macAddr);
  565.         curr = MAC_ADDR_LEN;
  566.  
  567.         /* concatenate the current time */
  568.         NdisGetSystemUpTime(&CurrentTime);
  569.         NdisMoveMemory(&local[curr], &CurrentTime, sizeof(CurrentTime));
  570.         curr += sizeof(CurrentTime);
  571.  
  572.         /* concatenate the last result */
  573.         NdisMoveMemory(&local[curr], result, 32);
  574.         curr += 32;
  575.  
  576.         /* concatenate a variable */
  577.         NdisMoveMemory(&local[curr], &i, 2);
  578.         curr += 2;
  579.  
  580.         /* calculate the result */
  581.         PRF(KeyCounter, 32, prefix, 12, local, curr, result, 32);
  582.     }
  583.  
  584.     NdisMoveMemory(random, result, 32);
  585. }
  586.  
  587. # nonce генерится както так
  588. `GCRY_WEAK_RANDOM'
  589.     For all functions, except for `gcry_mpi_randomize', this level maps
  590.      to GCRY_STRONG_RANDOM.  If you do not want this, consider using
  591.      `gcry_create_nonce'.
  592.  
  593. /* Create an unpredicable nonce of LENGTH bytes in BUFFER. */
  594. void
  595. gcry_create_nonce (void *buffer, size_t length)
  596. {
  597.  if (fips_mode ())
  598.    _gcry_rngfips_create_nonce (buffer, length);
  599.  else
  600.    _gcry_rngcsprng_create_nonce (buffer, length);
  601. }
  602.  
  603. ------------------------------------------==================
  604. # ВОТ ТАК ВОТ ГЕНЕРИТСЯ ВСЕ
  605. /* Create an unpredicable nonce of LENGTH bytes in BUFFER. */
  606. void
  607. _gcry_rngcsprng_create_nonce (void *buffer, size_t length)
  608. {
  609.  static unsigned char nonce_buffer[20+8];
  610.  static int nonce_buffer_initialized = 0;
  611.  static volatile pid_t my_pid; /* The volatile is there to make sure the
  612.                                   compiler does not optimize the code away
  613.                                   in case the getpid function is badly
  614.                                   attributed. */
  615.  volatile pid_t apid;
  616.  unsigned char *p;
  617.  size_t n;
  618.  int err;
  619.  
  620.  /* Make sure we are initialized. */
  621.  initialize ();
  622.  
  623. #ifdef USE_RANDOM_DAEMON
  624.  if (allow_daemon
  625.      && !_gcry_daemon_create_nonce (daemon_socket_name, buffer, length))
  626.    return; /* The daemon succeeded. */
  627.  allow_daemon = 0; /* Daemon failed - switch off. */
  628. #endif /*USE_RANDOM_DAEMON*/
  629.  
  630.  /* Acquire the nonce buffer lock. */
  631.  err = ath_mutex_lock (&nonce_buffer_lock);
  632.  if (err)
  633.    log_fatal ("failed to acquire the nonce buffer lock: %s\n",
  634.               strerror (err));
  635.  
  636.  apid = getpid ();
  637.  /* The first time initialize our buffer. */
  638.  if (!nonce_buffer_initialized)
  639.    {
  640.      time_t atime = time (NULL);
  641.      pid_t xpid = apid;
  642.  
  643.      my_pid = apid;
  644.  
  645.      if ((sizeof apid + sizeof atime) > sizeof nonce_buffer)
  646.        BUG ();
  647.  
  648.      /* Initialize the first 20 bytes with a reasonable value so that
  649.         a failure of gcry_randomize won't affect us too much.  Don't
  650.         care about the uninitialized remaining bytes. */
  651.      p = nonce_buffer;
  652.      memcpy (p, &xpid, sizeof xpid);
  653.      p += sizeof xpid;
  654.      memcpy (p, &atime, sizeof atime);
  655.  
  656.      /* Initialize the never changing private part of 64 bits. */
  657.      gcry_randomize (nonce_buffer+20, 8, GCRY_WEAK_RANDOM);
  658.  
  659.      nonce_buffer_initialized = 1;
  660.    }
  661.  else if ( my_pid != apid )
  662.    {
  663.      /* We forked. Need to reseed the buffer - doing this for the
  664.         private part should be sufficient. */
  665.      gcry_randomize (nonce_buffer+20, 8, GCRY_WEAK_RANDOM);
  666.      /* Update the pid so that we won't run into here again and
  667.          again. */
  668.       my_pid = apid;
  669.     }
  670.  
  671.   /* Create the nonce by hashing the entire buffer, returning the hash
  672.      and updating the first 20 bytes of the buffer with this hash. */
  673.   for (p = buffer; length > 0; length -= n, p += n)
  674.     {
  675.       _gcry_sha1_hash_buffer (nonce_buffer,
  676.                               nonce_buffer, sizeof nonce_buffer);
  677.       n = length > 20? 20 : length;
  678.       memcpy (p, nonce_buffer, n);
  679.     }
  680.  
  681.  
  682.   /* Release the nonce buffer lock. */
  683.   err = ath_mutex_unlock (&nonce_buffer_lock);
  684.   if (err)
  685.     log_fatal ("failed to release the nonce buffer lock: %s\n",
  686.                strerror (err));
  687.  
  688. }
  689. -----------------------------------
  690.  
  691. # проверяется nonce вот так
  692. static void
  693. check_nonce (void)
  694. {
  695.   char a[32], b[32];
  696.   int i,j;
  697.   int oops=0;
  698.  
  699.   if (verbose)
  700.     fprintf (stderr, "checking gcry_create_nonce\n");
  701.  
  702.   gcry_create_nonce (a, sizeof a);
  703.   for (i=0; i < 10; i++)
  704.     {
  705.       gcry_create_nonce (b, sizeof b);
  706.       if (!memcmp (a, b, sizeof a))
  707.         die ("identical nounce found\n");
  708.     }
  709.   for (i=0; i < 10; i++)
  710.     {
  711.       gcry_create_nonce (a, sizeof a);
  712.       if (!memcmp (a, b, sizeof a))
  713.         die ("identical nounce found\n");
  714.     }
  715.  
  716.  again:
  717.   for (i=1,j=0; i < sizeof a; i++)
  718.     if (a[0] == a[i])
  719.       j++;
  720.   if (j+1 == sizeof (a))
  721.     {
  722.       if (oops)
  723.         die ("impossible nonce found\n");
  724.       oops++;
  725.       gcry_create_nonce (a, sizeof a);
  726.       goto again;
  727.     }
  728. }
Add Comment
Please, Sign In to add comment