Mercurial > hg > ucis.core
comparison NaCl/crypto_box/curve25519xsalsa20poly1305.cs @ 20:c873e3dd73fe
Added NaCl cryptography code
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Mon, 15 Apr 2013 00:43:48 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
19:b9ef273964fd | 20:c873e3dd73fe |
---|---|
1 using System; | |
2 using UCIS.NaCl; | |
3 using System.Runtime.InteropServices; | |
4 | |
5 namespace UCIS.NaCl.crypto_box { | |
6 public static class curve25519xsalsa20poly1305 { | |
7 /* constants */ | |
8 public const int PUBLICKEYBYTES = 32; | |
9 public const int SECRETKEYBYTES = 32; | |
10 public const int BEFORENMBYTES = 32; | |
11 public const int NONCEBYTES = 24; | |
12 public const int ZEROBYTES = 32; | |
13 public const int BOXZEROBYTES = 16; | |
14 | |
15 //Never written to | |
16 static Byte[] sigma = new Byte[16] {(Byte)'e', (Byte)'x', (Byte)'p', (Byte)'a', //[16] = "expand 32-byte k"; | |
17 (Byte)'n', (Byte)'d', (Byte)' ', (Byte)'3', | |
18 (Byte)'2', (Byte)'-', (Byte)'b', (Byte)'y', | |
19 (Byte)'t', (Byte)'e', (Byte)' ', (Byte)'k', }; | |
20 | |
21 /* static pointer based methods */ | |
22 static unsafe public void crypto_box_getpublickey(Byte* pk, Byte* sk) { | |
23 crypto_scalarmult.curve25519.crypto_scalarmult_base(pk, sk); | |
24 } | |
25 static unsafe public int crypto_box_afternm(Byte* c, Byte* m, UInt64 mlen, Byte* n, Byte* k) { | |
26 return crypto_secretbox.xsalsa20poly1305.crypto_secretbox(c, m, mlen, n, k); | |
27 } | |
28 static unsafe public int crypto_box_open_afternm(Byte* m, Byte* c, UInt64 clen, Byte* n, Byte* k) { | |
29 return crypto_secretbox.xsalsa20poly1305.crypto_secretbox_open(m, c, clen, n, k); | |
30 } | |
31 static unsafe public void crypto_box_beforenm(Byte* k, Byte* pk, Byte* sk) { | |
32 Byte[] s = new Byte[32]; | |
33 fixed (Byte* sp = s, sigmap = sigma) { //, np = n | |
34 crypto_scalarmult.curve25519.crypto_scalarmult(sp, sk, pk); | |
35 crypto_core.hsalsa20.crypto_core(k, null, sp, sigmap); //k, np, sp, sigmap | |
36 } | |
37 } | |
38 static unsafe public int crypto_box(Byte* c, Byte* m, UInt64 mlen, Byte* n, Byte* pk, Byte* sk) { | |
39 Byte[] k = new Byte[BEFORENMBYTES]; | |
40 fixed (Byte* kp = k) { | |
41 crypto_box_beforenm(kp, pk, sk); | |
42 return crypto_box_afternm(c, m, mlen, n, kp); | |
43 } | |
44 } | |
45 static unsafe public int crypto_box_open(Byte* m, Byte* c, UInt64 clen, Byte* n, Byte* pk, Byte* sk) { | |
46 Byte[] k = new Byte[BEFORENMBYTES]; | |
47 fixed (Byte* kp = k) { | |
48 crypto_box_beforenm(kp, pk, sk); | |
49 return crypto_box_open_afternm(m, c, clen, n, kp); | |
50 } | |
51 } | |
52 | |
53 /* static array based methods */ | |
54 static unsafe public void crypto_box_keypair(out Byte[] pk, out Byte[] sk) { | |
55 sk = new Byte[32]; | |
56 pk = new Byte[32]; | |
57 randombytes.generate(sk); //randombytes(sk, 32); | |
58 fixed (Byte* skp = sk, pkp = pk) crypto_scalarmult.curve25519.crypto_scalarmult_base(pkp, skp); | |
59 } | |
60 static unsafe public Byte[] crypto_box_getpublickey(Byte[] sk) { | |
61 Byte[] pk; | |
62 crypto_box_getpublickey(out pk, sk); | |
63 return pk; | |
64 } | |
65 static unsafe public void crypto_box_getpublickey(out Byte[] pk, Byte[] sk) { | |
66 if (sk.Length != SECRETKEYBYTES) throw new ArgumentOutOfRangeException("sk"); | |
67 pk = new Byte[32]; | |
68 fixed (Byte* skp = sk, pkp = pk) crypto_box_getpublickey(pkp, skp); | |
69 } | |
70 static unsafe public void crypto_box_beforenm(Byte[] k, Byte[] pk, Byte[] sk) { | |
71 fixed (Byte* kp = k, pkp = pk, skp = sk) crypto_box_beforenm(kp, pkp, skp); | |
72 } | |
73 static unsafe public Byte[] crypto_box_beforenm(Byte[] pk, Byte[] sk) { | |
74 if (pk.Length != PUBLICKEYBYTES) throw new ArgumentOutOfRangeException("pk"); | |
75 if (sk.Length != SECRETKEYBYTES) throw new ArgumentOutOfRangeException("sk"); | |
76 Byte[] k = new Byte[BEFORENMBYTES]; | |
77 fixed (Byte* kp = k, pkp = pk, skp = sk) crypto_box_beforenm(kp, pkp, skp); | |
78 return k; | |
79 } | |
80 static unsafe public int crypto_box_afternm(Byte[] c, Byte[] m, Byte[] n, Byte[] k) { | |
81 fixed (Byte* cp = c, mp = m, np = n, kp = k) return crypto_box_afternm(cp, mp, (ulong)m.Length, np, kp); | |
82 } | |
83 static unsafe public int crypto_box_open_afternm(Byte[] m, Byte[] c, Byte[] n, Byte[] k) { | |
84 fixed (Byte* cp = c, mp = m, np = n, kp = k) return crypto_box_open_afternm(mp, cp, (ulong)c.Length, np, kp); | |
85 } | |
86 static unsafe public int crypto_box(Byte[] c, Byte[] m, Byte[] n, Byte[] pk, Byte[] sk) { | |
87 fixed (Byte* cp = c, mp = m, np = n, pkp = pk, skp = sk) return crypto_box(cp, mp, (ulong)m.Length, np, pkp, skp); | |
88 } | |
89 static unsafe public int crypto_box_open(Byte[] m, Byte[] c, Byte[] n, Byte[] pk, Byte[] sk) { | |
90 fixed (Byte* cp = c, mp = m, np = n, pkp = pk, skp = sk) return crypto_box_open(mp, cp, (ulong)c.Length, np, pkp, skp); | |
91 } | |
92 | |
93 static unsafe public int crypto_box_afternm(Byte[] c, int coffset, Byte[] m, int moffset, int mlen, Byte[] n, Byte[] k) { | |
94 fixed (Byte* cp = c, mp = m, np = n, kp = k) return crypto_box_afternm(cp + coffset, mp + moffset, (ulong)mlen, np, kp); | |
95 } | |
96 static unsafe public int crypto_box_open_afternm(Byte[] m, int moffset, Byte[] c, int coffset, int clen, Byte[] n, Byte[] k) { | |
97 fixed (Byte* cp = c, mp = m, np = n, kp = k) return crypto_box_open_afternm(mp + moffset, cp + coffset, (ulong)clen, np, kp); | |
98 } | |
99 static unsafe public int crypto_box(Byte[] c, int coffset, Byte[] m, int moffset, int mlen, Byte[] n, Byte[] pk, Byte[] sk) { | |
100 fixed (Byte* cp = c, mp = m, np = n, pkp = pk, skp = sk) return crypto_box(cp + coffset, mp + moffset, (ulong)mlen, np, pkp, skp); | |
101 } | |
102 static unsafe public int crypto_box_open(Byte[] m, int moffset, Byte[] c, int coffset, int clen, Byte[] n, Byte[] pk, Byte[] sk) { | |
103 fixed (Byte* cp = c, mp = m, np = n, pkp = pk, skp = sk) return crypto_box_open(mp + moffset, cp + coffset, (ulong)clen, np, pkp, skp); | |
104 } | |
105 } | |
106 } |