OpenVMS Source Code Demos

DIFFIE_HELLMAN_DEMO_IN_BASIC

1000	%title "diffie_hellman_demo_in_BASIC"					!
	declare string constant k_version = "101.1"			,	!						&
				k_program = "diffie_hellman_demo_in_BASIC"	!
	!========================================================================================================================
	! title     : diffie_hellman_demo_in_BASIC_xxx.bas
	! author    : Neil Rieck ( https://neilrieck.net/ )
	! platform  : HP-BASIC on OpenVMS-8.4 (Alpha)
	! purpose   : demonstrates Diffie-Hellman key exchange employing the usual actors: Bob and Alice (who want to communicate
	!             privately) and Eve (who wishes to Evesdrop)
	! Caveats   : 1) This program demonstrates key-exchange concepts but is limited to 64-bit math (63-bits if you realize
	!                that most BASIC dialects, including HP-BASIC for OpenVMS, have no unsigned integers). If you choose
	!                values which cause g^a1 (or g^b1) to require more than 19 decimal digits, then this program will throw
	!                an "integer overflow" error.
	!             2) The diffie-hellman algorithm is only secure when P is a prime number over 1000 bits in size
	!             3) all the other input numbers must be smaller than P
	! references: https://en.wikipedia.org/wiki/Diffie-Hellman_key_exchange
	!           : http://asecuritysite.com/encryption/diffie (uses JavaScript with BigInt)
	! history   :
	! 100 NSR 120722 1. original effort (fails with HP-BASIC-1.7-000 due to a compiler bug affecting MOD
	!     NSR 120922 2. continued work using experimental HP-BASIC-1.7-001 (HP fixed my compiler)
	!     NSR 140418 3. changed the default values; now request P first; renamed Y to G (generator)
	! 101 NSR 140419 1. ripped out my experimental code
	!========================================================================================================================
	option type = explicit							! cuz tricks are for kids
	option size = (real xfloat, integer quad)				! not sure this is required here
	set no prompt								!
	!
	!	Alice's data
	!
	declare quad	a1		,&
			a2		,&
			a3
	!
	!	Bob's data
	!
	declare quad	b1		,&
			b2		,&
			b3
	!
	!	Common data
	!
	declare	quad	g		,! generator	&
			p		,! prime	&
			junk%
	!
	declare string	junk$							!
	!=======================================================================
	!	main
	!=======================================================================
2000	main:
	on error goto trap							! 'old school' error trapping
	print k_program +"_"+ k_version						!
	print string$(len(k_program +"_"+ k_version), asc("="))			!
	!
	print "Caveats:"
	print "1) This demo program employs 64-bit signed integers. If you choose values"
	print "   which cause g^a1 (or g^b1) to require more than 19 decimal digits, then"
	print "   this program will throw an 'integer overflow' error."		!
	print "2) The actual algorithm is not secure unless p is a prime number larger"
	print "   than 1000 bits (~ 300 decimal digits)"			!
	print									!
	!
	p = 29
	print "enter public prime 'p'     (default="+str$(p)+") ";		!
	input junk$								!
	when error in								!
	    junk% = integer(junk$)						!
	use									!
	    junk% = 0								!
	end when								!
	p = junk%	if junk% > 0						!
	!
	g = 3
	print "note: all future inputs must be less than prime"
	print
	print "enter public generator 'g' (default="+str$(g)+" ) ";		!
	input junk$								!
	when error in								!
	    junk% = integer(junk$)						!
	use									!
	    junk% = 0								!
	end when								!
	g = junk%	if junk% > 0						!
	!
	!=======================================================================
	!	in a production program, these numbers would be picked randomly
	!	at the beginning of each communication session
	!=======================================================================
	a1 = 4									! Alice's secret number (random)
	print "enter Alice's guess        (default="+str$(a1)+" ) ";		!
	input junk$								!
	when error in								!
	    junk% = integer(junk$)						!
	use									!
	    junk% = 0								!
	end when								!
	a1 = junk%	if junk% > 0						!
	!
	b1 = 5									! Bob's secret number (random)
	print "enter Bob's   guess        (default="+str$(b1)+" ) ";		!
	input junk$								!
	when error in								!
	    junk% = integer(junk$)						!
	use									!
	    junk% = 0								!
	end when								!
	b1 = junk%	if junk% > 0						!
	!
	!	Alice's calc #1 (private guess >> public)
	!
3001	a2 = (g ^ a1)								!
3002	a2 = mod(a2, p)								! a2 will be sent to BOB
	!
	!	Bob's calc #1 (private guess >> public)
	!
3003	b2 = (g ^ b1)								!
3004	b2 = mod(b2, p)								! b2 will be sent to ALICE
	!
	!	Alice's calc #2
	!
3005	a3 = (b2^ a1)								! Alice's computed key
3006	a3 = mod(a3, p)								!
	!
	!	Bob's calc #2
	!
3007	b3 = (a2^ b1)								! Bob's computed key
3008	b3 = mod(b3, p)								!
	!
9999	print "================================================================="
	print "Data sent across the channel (Eve can see it):"			!
	print "  public parameter P     p : "+ str$(p)				!
	print "  public parameter g     g : "+ str$(g)				!
	print "Randomly generated private data (never sent):"			!
	print "  Alice's private number a1: "+ str$(a1)				!
	print "  Bob's   private key    b1: "+ str$(b1)				!
	print "Computed data sent across the channel (Eve can see it):"		!
	print "  Alice's public number  a2: "+ str$(a2)				!
	print "  Bob's   public number  b2: "+ str$(b2)				!
	print "Computed private data (never sent):"				!
	print "  Alice's computed key   a3: "+ str$(a3)				!
	print "  Bob's   computed key   b3: "+ str$(b3)				!
	print "Notes:"
	print "  1) Alice and Bob can now communicate privately using"		!
	print "      computed symmetric key '"+str$(a3)+"' to encrypt/decrypt"	!
	print " 2. If this exchange was encrypted it would be even more secure"	!
	goto fini								!
	!=======================================================================
	!	old school error trapping
	!=======================================================================
	trap:									!
	print "error: ";str$(err)
	print "line : ";str$(erl)
	print "text : ";ert$(err)
	resume fini								! fix the stack
	!=======================================================================
	!	that's all folks
	!=======================================================================
32000	fini:									!
	end program 1								! vms success