Diff

Differences From Artifact [d9395b5407]:

To Artifact [f5fc2eab10]:


1
2
3
4
5
6
7
8
9
10
11
12
13





















14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41













+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







/* XXX:TODO: OpenMP support is currently incomplete */
#undef NANO_TCL_HAVE_OPENMP

#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <tcl.h>
#ifdef NANO_TCL_HAVE_OPENMP
#  include <omp.h>
#endif

/*
 * We need both getaddrinfo and inet_ntop for DNS resolution
 */
#if defined(HAVE_GETNAMEINFO) && defined(HAVE_GETADDRINFO)
#  define NANO_TCL_CAN_RESOLVE_NAMES 1
#else
#  undef NANO_TCL_CAN_RESOLVE_NAMES
#endif

#ifdef NANO_TCL_CAN_RESOLVE_NAMES
#  ifdef HAVE_SYS_SOCKET_H
#    include <sys/socket.h>
#  endif
#  ifdef HAVE_SYS_TYPES_H
#    include <sys/types.h>
#  endif
#  ifdef HAVE_NETDB_H
#    include <netdb.h>
# endif
#endif

#include "randombytes.h"
#include "monocypher.h"
#include "argon2.h"
#include "aes.h"

#define NANO_SECRET_KEY_LENGTH 32
#define NANO_PUBLIC_KEY_LENGTH 32
563
564
565
566
567
568
569

































































































570
571
572
573
574
575
576
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(buffer, number_of_bytes));

	return(TCL_OK);

	/* NOTREACH */
	clientData = clientData;
}

#ifdef NANO_TCL_CAN_RESOLVE_NAMES
static int nano_tcl_resolve_name(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
	Tcl_Encoding ascii_encoding;
	Tcl_Obj *list_of_hostnames, *hostname_obj;
	struct addrinfo *gai_data, *addr_current;
	char *hostname_utf8, hostname[256];
	int hostname_utf8_length, hostname_utf8_length_processed, hostname_length;
	int tute_ret, gai_ret, gni_ret;

	if (objc != 2) {
		Tcl_WrongNumArgs(interp, 1, objv, "hostname");

		return(TCL_ERROR);
	}

	hostname_utf8 = Tcl_GetStringFromObj(objv[1], &hostname_utf8_length);
	if (!hostname_utf8) {
		return(TCL_ERROR);
	}

	/*
	 * getaddrinfo() accepts ASCII input, so convert to that encoding
	 */
	ascii_encoding = Tcl_GetEncoding(interp, "ascii");
	if (ascii_encoding == NULL) {
		return(TCL_ERROR);
	}

	tute_ret = Tcl_UtfToExternal(interp, ascii_encoding, hostname_utf8,
	               hostname_utf8_length, TCL_ENCODING_STOPONERROR, NULL,
	               hostname, sizeof(hostname),
	               &hostname_utf8_length_processed,
	               &hostname_length, NULL
	           );

	Tcl_FreeEncoding(ascii_encoding);

	if (tute_ret != TCL_OK) {
		Tcl_SetResult(interp, "Failed to convert to ASCII", NULL);

		return(TCL_ERROR);
	}

	if (hostname_utf8_length_processed != hostname_utf8_length) {
		Tcl_SetResult(interp, "Failed to convert entire buffer", NULL);

		return(TCL_ERROR);
	}

	gai_ret = getaddrinfo(hostname, NULL, NULL, &gai_data);
	if (gai_ret == EAI_NODATA || gai_ret == EAI_NONAME) {
		Tcl_SetResult(interp, "", NULL);

		return(TCL_OK);
	}
	if (gai_ret != 0) {
		Tcl_SetResult(interp, (char *) gai_strerror(gai_ret), NULL);

		return(TCL_ERROR);
	}

	list_of_hostnames = Tcl_NewObj();
	for (addr_current = gai_data; addr_current; addr_current = addr_current->ai_next) {
		if (addr_current->ai_family != AF_INET && addr_current->ai_family != AF_INET6) {
			continue;
		}

		gni_ret = getnameinfo(addr_current->ai_addr, addr_current->ai_addrlen,
		                      hostname, sizeof(hostname),
		                      NULL, 0, NI_NUMERICHOST
		          );
		if (gni_ret != 0) {
			continue;
		}

		hostname_obj = Tcl_NewStringObj(hostname, -1);

		Tcl_ListObjAppendElement(NULL, list_of_hostnames, hostname_obj);
	}

	freeaddrinfo(gai_data);

	Tcl_SetObjResult(interp, list_of_hostnames);

	return(TCL_OK);

	/* NOTREACH */
	clientData = clientData;
}
#else
static int nano_tcl_resolve_name(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
	Tcl_SetResult(interp, "Not supported on this platform", NULL);
	
	return(TCL_ERROR);
}
#endif

static int nano_tcl_self_test(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
	if (objc != 1) {
		Tcl_WrongNumArgs(interp, 1, objv, "");

		return(TCL_ERROR);
	}
599
600
601
602
603
604
605

606
607
608
609
610
611
612
613
614
615
616





617
618
619
620
621
622
623
624
625
626
627
628
629

630
631
632
633
634
635
636
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761







+











+
+
+
+
+













+







#endif

	if (!interp) {
		return(TCL_OK);
	}

	TclNano_CreateNamespace(interp, "::nano");
	TclNano_CreateNamespace(interp, "::nano::internal");
	TclNano_CreateNamespace(interp, "::nano::block");
	TclNano_CreateNamespace(interp, "::nano::key");
	TclNano_CreateNamespace(interp, "::nano::work");

	TclNano_SetIntVar(interp, "::nano::block::hashLength", NANO_BLOCK_HASH_LENGTH);
	TclNano_SetIntVar(interp, "::nano::block::signatureLength", NANO_BLOCK_SIGNATURE_LENGTH);
	TclNano_SetIntVar(interp, "::nano::key::publicKeyLength", NANO_PUBLIC_KEY_LENGTH);
	TclNano_SetIntVar(interp, "::nano::key::privateKeyLength", NANO_SECRET_KEY_LENGTH);
	TclNano_SetIntVar(interp, "::nano::key::seedLength", NANO_SECRET_KEY_LENGTH);
	TclNano_SetIntVar(interp, "::nano::work::workValueLength", NANO_WORK_VALUE_LENGTH);
	TclNano_SetIntVar(interp, "::nano::work::workHashLength", NANO_WORK_HASH_LENGTH);
#ifdef NANO_TCL_CAN_RESOLVE_NAMES
	TclNano_SetIntVar(interp, "::nano::internal::haveResolveName", 1);
#else
	TclNano_SetIntVar(interp, "::nano::internal::haveResolveName", 0);
#endif

	TclNano_CreateObjCommand(interp, "::nano::internal::selfTest", nano_tcl_self_test);
	TclNano_CreateObjCommand(interp, "::nano::internal::generateKey", nano_tcl_generate_keypair);
	TclNano_CreateObjCommand(interp, "::nano::internal::generateSeed", nano_tcl_generate_seed);
	TclNano_CreateObjCommand(interp, "::nano::internal::publicKey", nano_tcl_secret_key_to_public_key);
	TclNano_CreateObjCommand(interp, "::nano::internal::signDetached", nano_tcl_sign_detached);
	TclNano_CreateObjCommand(interp, "::nano::internal::verifyDetached", nano_tcl_verify_detached);
	TclNano_CreateObjCommand(interp, "::nano::internal::hashData", nano_tcl_hash_data);
	TclNano_CreateObjCommand(interp, "::nano::internal::deriveKeyFromPassword", nano_tcl_derive_key_from_password);
	TclNano_CreateObjCommand(interp, "::nano::internal::AES256-CTR", nano_tcl_aes256_ctr);
	TclNano_CreateObjCommand(interp, "::nano::internal::validateWork", nano_tcl_validate_work);
	TclNano_CreateObjCommand(interp, "::nano::internal::generateWork", nano_tcl_generate_work);
	TclNano_CreateObjCommand(interp, "::nano::internal::randomBytes", nano_tcl_random_bytes);
	TclNano_CreateObjCommand(interp, "::nano::internal::resolveName", nano_tcl_resolve_name);

	TclNano_Eval(interp, nanoInitScript);

	TclNano_PkgProvide(interp, "nano", PACKAGE_VERSION);

	return(TCL_OK);
}