Curve25519 (Elliptic Curve Cryptography)

Share your advanced PureBasic knowledge/code with the community.
javabean
User
User
Posts: 60
Joined: Sat Nov 08, 2003 10:29 am
Location: Austria

Curve25519 (Elliptic Curve Cryptography)

Post by javabean »

Hi there!
I found a Curve25519 implementation on the net and created a ".lib" (x86, '_stdcall') out of it.
There is also a x64 implementation in the original sources, see (http://github.com/agl/curve25519-donna) for that.

For those who don't know what 'Curve25519' is - here's a link to the authors page.

I have coded a little example using the .lib:

Code: Select all

;+++++++++++++++++++++++++++++++++++++++++++++++
;| Demonstration of the 'curve25519' algorithm |
;+++++++++++++++++++++++++++++++++++++++++++++++
;
; Demo:
; PureBasic demonstration code by javabean (2010)
; PureBasic 4.40 (x86) for Windows
;---
; Library source:
; Source code for 'curve25519_donna.lib' taken from the
; C language implementation of 'Donna' (curve25519_donna.c)
; (http://github.com/agl/curve25519-donna)
;---
; Library build:
; 'curve25519_donna.lib' built with 'PellesC for Windows 4.50.113'
; Additional objectfiles (exploded from 'crt.lib' using POLIB):
;_llmul.obj, _lldiv.obj, _llshr.obj, _llshl.obj
;---
; Original 'Curve25519' algorithm by D. J. Bernstein (Public domain).
; (http://cr.yp.to/ecdh.html)
;++++++++++++++++++++++++++++++++++++++++++++++++



Import "curve25519_donna.lib"; '_stdcall'
  curve(*output, *secret, *basepoint) As "_curve25519_donna@12"
EndImport


*mysecret = AllocateMemory(32)
  For i=0 To 31 : PokeA(*mysecret+i,42) : Next
  PokeA(*mysecret+0, PeekA(*mysecret+0) & 248)
  PokeA(*mysecret+31, PeekA(*mysecret+31) & 127)
  PokeA(*mysecret+31, PeekA(*mysecret+31) | 64)
*mypublic = AllocateMemory(32)
*basepoint = AllocateMemory(32)
  PokeA(*basepoint+0, 9)

;-----
*hissecret = AllocateMemory(32)
  For i=0 To 31 : PokeA(*hissecret+i,11) : Next
  PokeA(*hissecret+0, PeekA(*hissecret+0) & 248)
  PokeA(*hissecret+31, PeekA(*hissecret+31) & 127)
  PokeA(*hissecret+31, PeekA(*hissecret+31) | 64)
*hispublic = AllocateMemory(32)
;---
*myshared = AllocateMemory(32)
*hisshared = AllocateMemory(32)


OpenConsole()
ConsoleTitle("Curve25519 Test")

str$="": For i=0 To 31: str$+RSet(Hex(PeekA(*mysecret+i),#PB_Byte),2,"0"): Next :   PrintN("mysecret : "+str$)
str$="": For i=0 To 31: str$+RSet(Hex(PeekA(*basepoint+i),#PB_Byte),2,"0"): Next :  PrintN("basepoint: "+str$)
curve(*mypublic, *mysecret, *basepoint)
str$="": For i=0 To 31: str$+RSet(Hex(PeekA(*mypublic+i),#PB_Byte),2,"0"): Next :   PrintN("mypublic : "+str$)
PrintN("")
str$="": For i=0 To 31: str$+RSet(Hex(PeekA(*hissecret+i),#PB_Byte),2,"0"): Next :  PrintN("hissecret: "+str$)
str$="": For i=0 To 31: str$+RSet(Hex(PeekA(*basepoint+i),#PB_Byte),2,"0"): Next :  PrintN("basepoint: "+str$)
curve(*hispublic, *hissecret, *basepoint)
str$="": For i=0 To 31: str$+RSet(Hex(PeekA(*hispublic+i),#PB_Byte),2,"0"): Next :  PrintN("hispublic: "+str$)
PrintN("")
curve(*myshared, *mysecret, *hispublic)
curve(*hisshared, *hissecret, *mypublic)
str_myshared$="": For i=0 To 31: str_myshared$+RSet(Hex(PeekA(*myshared+i),#PB_Byte),2,"0"): Next :   PrintN("myshared : "+str_myshared$)
str_hisshared$="": For i=0 To 31: str_hisshared$+RSet(Hex(PeekA(*hisshared+i),#PB_Byte),2,"0"): Next :  PrintN("hisshared: "+str_hisshared$)
PrintN("")
If str_myshared$ = str_hisshared$
  PrintN("myshared = hisshared")
Else
  PrintN("myshared <> hisshared")
EndIf

PrintN("")
PrintN("")
PrintN("PRESS 'RETURN' TO QUIT.")
Input()
CloseConsole()
All files I used for the .lib, the static '.lib'-file itself, the original sources as well as the example can be downloaded HERE (left mouse click to download!) as a 7z archive (~44 kB).

Cheers,
javabean

Edit: [21.02.2010]: Changed "ImportC" to "Import" (_stdcall)
Last edited by javabean on Tue Mar 02, 2010 8:04 pm, edited 6 times in total.
User avatar
idle
Always Here
Always Here
Posts: 5839
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Curve25519 (Elliptic Curve Cryptography)

Post by idle »

thanks looks interesting
User avatar
idle
Always Here
Always Here
Posts: 5839
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Curve25519 (Elliptic Curve Cryptography)

Post by idle »

There seems to be a problem with the lib in procedures not sure what the issue is though
javabean
User
User
Posts: 60
Joined: Sat Nov 08, 2003 10:29 am
Location: Austria

Re: Curve25519 (Elliptic Curve Cryptography)

Post by javabean »

My fault! :oops:
-The lib uses "_stdcall" as calling convention, so you have to use "Import" instead of ""ImportC".

Code: Select all

Import "curve25519_donna.lib"
  curve(*output, *secret, *basepoint) As "_curve25519_donna@12"
EndImport
Fixed it in the downloads and the demo above.
Hope it will work now...
User avatar
idle
Always Here
Always Here
Posts: 5839
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Curve25519 (Elliptic Curve Cryptography)

Post by idle »

thanks, that would explain it :lol:
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: Curve25519 (Elliptic Curve Cryptography)

Post by AndyMK »

Can anyone give me some pointers on how this works between a client and the server on a network? How would i implement it?
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: Curve25519 (Elliptic Curve Cryptography)

Post by gnasen »

Wow, thanks. When I heared lectures about all this algebraic number theory stuff, I never figured out how to use it in the "real world". I often heared, that it is used very much in encrypting/decrypting, but (besides RSA) this is the first example I have seen!
pb 5.11
Post Reply