· 7 years ago · Oct 02, 2018, 03:20 PM
1import sha512
2import strutils
3const sourcePath = currentSourcePath().split({'\\', '/'})[0..^2].join("/")
4{.passC: "-I\"" & sourcePath & "/src\"".}
5{.passC: "-I\"" & sourcePath & "/src/optional\"".}
6const headermonocypher = sourcePath & "/src/monocypher.h"
7{.passC: "-DED25519_SHA512".}
8{.compile: "nimmonocypher/src/monocypher.c".}
9type
10 crypto_chacha_ctx* {.importc: "crypto_chacha_ctx", header: headermonocypher, bycopy.} = object
11 input* {.importc: "input".}: array[16, uint32]
12 pool* {.importc: "pool".}: array[16, uint32]
13 pool_idx* {.importc: "pool_idx".}: csize
14
15 crypto_poly1305_ctx* {.importc: "crypto_poly1305_ctx", header: headermonocypher,
16 bycopy.} = object
17 r* {.importc: "r".}: array[4, uint32]
18 h* {.importc: "h".}: array[5, uint32]
19 c* {.importc: "c".}: array[5, uint32]
20 pad* {.importc: "pad".}: array[4, uint32]
21 c_idx* {.importc: "c_idx".}: csize
22
23 crypto_lock_ctx* {.importc: "crypto_lock_ctx", header: headermonocypher, bycopy.} = object
24 chacha* {.importc: "chacha".}: crypto_chacha_ctx
25 poly* {.importc: "poly".}: crypto_poly1305_ctx
26 ad_size* {.importc: "ad_size".}: uint64
27 message_size* {.importc: "message_size".}: uint64
28 ad_phase* {.importc: "ad_phase".}: cint
29
30 crypto_blake2b_ctx* {.importc: "crypto_blake2b_ctx", header: headermonocypher,
31 bycopy.} = object
32 hash* {.importc: "hash".}: array[8, uint64]
33 input_offset* {.importc: "input_offset".}: array[2, uint64]
34 input* {.importc: "input".}: array[16, uint64]
35 input_idx* {.importc: "input_idx".}: csize
36 hash_size* {.importc: "hash_size".}: csize
37
38 crypto_hash_ctx* = crypto_sha512_ctx
39 crypto_sign_ctx* {.importc: "crypto_sign_ctx", header: headermonocypher, bycopy.} = object
40 hash* {.importc: "hash".}: crypto_hash_ctx
41 buf* {.importc: "buf".}: array[96, uint8]
42 pk* {.importc: "pk".}: array[32, uint8]
43
44 crypto_check_ctx* {.importc: "crypto_check_ctx", header: headermonocypher, bycopy.} = object
45 hash* {.importc: "hash".}: crypto_hash_ctx
46 sig* {.importc: "sig".}: array[64, uint8]
47 pk* {.importc: "pk".}: array[32, uint8]
48
49
50proc crypto_verify16*(a: array[16, uint8]; b: array[16, uint8]): cint {.stdcall,
51 importc: "crypto_verify16", header: headermonocypher.}
52proc crypto_verify32*(a: array[32, uint8]; b: array[32, uint8]): cint {.stdcall,
53 importc: "crypto_verify32", header: headermonocypher.}
54proc crypto_verify64*(a: array[64, uint8]; b: array[64, uint8]): cint {.stdcall,
55 importc: "crypto_verify64", header: headermonocypher.}
56proc crypto_wipe*(secret: pointer; size: csize) {.stdcall, importc: "crypto_wipe",
57 header: headermonocypher.}
58proc crypto_lock*(mac: array[16, uint8]; cipher_text: ptr uint8;
59 key: array[32, uint8]; nonce: array[24, uint8];
60 plain_text: ptr uint8; text_size: csize) {.stdcall,
61 importc: "crypto_lock", header: headermonocypher.}
62proc crypto_unlock*(plain_text: ptr uint8; key: array[32, uint8];
63 nonce: array[24, uint8]; mac: array[16, uint8];
64 cipher_text: ptr uint8; text_size: csize): cint {.stdcall,
65 importc: "crypto_unlock", header: headermonocypher.}
66proc crypto_lock_aead*(mac: array[16, uint8]; cipher_text: ptr uint8;
67 key: array[32, uint8]; nonce: array[24, uint8];
68 ad: ptr uint8; ad_size: csize; plain_text: ptr uint8;
69 text_size: csize) {.stdcall, importc: "crypto_lock_aead",
70 header: headermonocypher.}
71proc crypto_unlock_aead*(plain_text: ptr uint8; key: array[32, uint8];
72 nonce: array[24, uint8]; mac: array[16, uint8];
73 ad: ptr uint8; ad_size: csize; cipher_text: ptr uint8;
74 text_size: csize): cint {.stdcall,
75 importc: "crypto_unlock_aead", header: headermonocypher.}
76proc crypto_lock_init*(ctx: ptr crypto_lock_ctx; key: array[32, uint8];
77 nonce: array[24, uint8]) {.stdcall,
78 importc: "crypto_lock_init", header: headermonocypher.}
79proc crypto_lock_auth_ad*(ctx: ptr crypto_lock_ctx; message: ptr uint8;
80 message_size: csize) {.stdcall,
81 importc: "crypto_lock_auth_ad", header: headermonocypher.}
82proc crypto_lock_auth_message*(ctx: ptr crypto_lock_ctx; cipher_text: ptr uint8;
83 text_size: csize) {.stdcall,
84 importc: "crypto_lock_auth_message", header: headermonocypher.}
85proc crypto_lock_update*(ctx: ptr crypto_lock_ctx; cipher_text: ptr uint8;
86 plain_text: ptr uint8; text_size: csize) {.stdcall,
87 importc: "crypto_lock_update", header: headermonocypher.}
88proc crypto_lock_final*(ctx: ptr crypto_lock_ctx; mac: array[16, uint8]) {.stdcall,
89 importc: "crypto_lock_final", header: headermonocypher.}
90proc crypto_unlock_update*(ctx: ptr crypto_lock_ctx; plain_text: ptr uint8;
91 cipher_text: ptr uint8; text_size: csize) {.stdcall,
92 importc: "crypto_unlock_update", header: headermonocypher.}
93proc crypto_unlock_final*(ctx: ptr crypto_lock_ctx; mac: array[16, uint8]): cint {.
94 stdcall, importc: "crypto_unlock_final", header: headermonocypher.}
95proc crypto_blake2b*(hash: array[64, uint8]; message: ptr uint8;
96 message_size: csize) {.stdcall, importc: "crypto_blake2b",
97 header: headermonocypher.}
98proc crypto_blake2b_general*(hash: ptr uint8; hash_size: csize; key: ptr uint8;
99 key_size: csize; message: ptr uint8;
100 message_size: csize) {.stdcall,
101 importc: "crypto_blake2b_general", header: headermonocypher.}
102proc crypto_blake2b_init*(ctx: ptr crypto_blake2b_ctx) {.stdcall,
103 importc: "crypto_blake2b_init", header: headermonocypher.}
104proc crypto_blake2b_update*(ctx: ptr crypto_blake2b_ctx; message: ptr uint8;
105 message_size: csize) {.stdcall,
106 importc: "crypto_blake2b_update", header: headermonocypher.}
107proc crypto_blake2b_final*(ctx: ptr crypto_blake2b_ctx; hash: ptr uint8) {.stdcall,
108 importc: "crypto_blake2b_final", header: headermonocypher.}
109proc crypto_blake2b_general_init*(ctx: ptr crypto_blake2b_ctx; hash_size: csize;
110 key: ptr uint8; key_size: csize) {.stdcall,
111 importc: "crypto_blake2b_general_init", header: headermonocypher.}
112proc crypto_argon2i*(hash: ptr uint8; hash_size: uint32; work_area: pointer;
113 nb_blocks: uint32; nb_iterations: uint32;
114 password: ptr uint8; password_size: uint32;
115 salt: ptr uint8; salt_size: uint32) {.stdcall,
116 importc: "crypto_argon2i", header: headermonocypher.}
117proc crypto_argon2i_general*(hash: ptr uint8; hash_size: uint32;
118 work_area: pointer; nb_blocks: uint32;
119 nb_iterations: uint32; password: ptr uint8;
120 password_size: uint32; salt: ptr uint8;
121 salt_size: uint32; key: ptr uint8;
122 key_size: uint32; ad: ptr uint8; ad_size: uint32) {.
123 stdcall, importc: "crypto_argon2i_general", header: headermonocypher.}
124proc crypto_key_exchange*(shared_key: array[32, uint8];
125 your_secret_key: array[32, uint8];
126 their_public_key: array[32, uint8]): cint {.stdcall,
127 importc: "crypto_key_exchange", header: headermonocypher.}
128proc crypto_sign_public_key*(public_key: array[32, uint8];
129 secret_key: array[32, uint8]) {.stdcall,
130 importc: "crypto_sign_public_key", header: headermonocypher.}
131proc crypto_sign*(signature: array[64, uint8]; secret_key: array[32, uint8];
132 public_key: array[32, uint8]; message: ptr uint8;
133 message_size: csize) {.stdcall, importc: "crypto_sign",
134 header: headermonocypher.}
135proc crypto_check*(signature: array[64, uint8]; public_key: array[32, uint8];
136 message: ptr uint8; message_size: csize): cint {.stdcall,
137 importc: "crypto_check", header: headermonocypher.}
138proc crypto_sign_init_first_pass*(ctx: ptr crypto_sign_ctx;
139 secret_key: array[32, uint8];
140 public_key: array[32, uint8]) {.stdcall,
141 importc: "crypto_sign_init_first_pass", header: headermonocypher.}
142proc crypto_sign_update*(ctx: ptr crypto_sign_ctx; message: ptr uint8;
143 message_size: csize) {.stdcall,
144 importc: "crypto_sign_update", header: headermonocypher.}
145proc crypto_sign_init_second_pass*(ctx: ptr crypto_sign_ctx) {.stdcall,
146 importc: "crypto_sign_init_second_pass", header: headermonocypher.}
147proc crypto_sign_final*(ctx: ptr crypto_sign_ctx; signature: array[64, uint8]) {.
148 stdcall, importc: "crypto_sign_final", header: headermonocypher.}
149proc crypto_check_init*(ctx: ptr crypto_check_ctx; signature: array[64, uint8];
150 public_key: array[32, uint8]) {.stdcall,
151 importc: "crypto_check_init", header: headermonocypher.}
152proc crypto_check_update*(ctx: ptr crypto_check_ctx; message: ptr uint8;
153 message_size: csize) {.stdcall,
154 importc: "crypto_check_update", header: headermonocypher.}
155proc crypto_check_final*(ctx: ptr crypto_check_ctx): cint {.stdcall,
156 importc: "crypto_check_final", header: headermonocypher.}
157proc crypto_chacha20_H*(`out`: array[32, uint8]; key: array[32, uint8];
158 `in`: array[16, uint8]) {.stdcall,
159 importc: "crypto_chacha20_H", header: headermonocypher.}
160proc crypto_chacha20_init*(ctx: ptr crypto_chacha_ctx; key: array[32, uint8];
161 nonce: array[8, uint8]) {.stdcall,
162 importc: "crypto_chacha20_init", header: headermonocypher.}
163proc crypto_chacha20_x_init*(ctx: ptr crypto_chacha_ctx; key: array[32, uint8];
164 nonce: array[24, uint8]) {.stdcall,
165 importc: "crypto_chacha20_x_init", header: headermonocypher.}
166proc crypto_chacha20_set_ctr*(ctx: ptr crypto_chacha_ctx; ctr: uint64) {.stdcall,
167 importc: "crypto_chacha20_set_ctr", header: headermonocypher.}
168proc crypto_chacha20_encrypt*(ctx: ptr crypto_chacha_ctx; cipher_text: ptr uint8;
169 plain_text: ptr uint8; text_size: csize) {.stdcall,
170 importc: "crypto_chacha20_encrypt", header: headermonocypher.}
171proc crypto_chacha20_stream*(ctx: ptr crypto_chacha_ctx; stream: ptr uint8;
172 size: csize) {.stdcall,
173 importc: "crypto_chacha20_stream", header: headermonocypher.}
174proc crypto_poly1305*(mac: array[16, uint8]; message: ptr uint8;
175 message_size: csize; key: array[32, uint8]) {.stdcall,
176 importc: "crypto_poly1305", header: headermonocypher.}
177proc crypto_poly1305_init*(ctx: ptr crypto_poly1305_ctx; key: array[32, uint8]) {.
178 stdcall, importc: "crypto_poly1305_init", header: headermonocypher.}
179proc crypto_poly1305_update*(ctx: ptr crypto_poly1305_ctx; message: ptr uint8;
180 message_size: csize) {.stdcall,
181 importc: "crypto_poly1305_update", header: headermonocypher.}
182proc crypto_poly1305_final*(ctx: ptr crypto_poly1305_ctx; mac: array[16, uint8]) {.
183 stdcall, importc: "crypto_poly1305_final", header: headermonocypher.}
184proc crypto_x25519_public_key*(public_key: array[32, uint8];
185 secret_key: array[32, uint8]) {.stdcall,
186 importc: "crypto_x25519_public_key", header: headermonocypher.}
187proc crypto_x25519*(raw_shared_secret: array[32, uint8];
188 your_secret_key: array[32, uint8];
189 their_public_key: array[32, uint8]): cint {.stdcall,
190 importc: "crypto_x25519", header: headermonocypher.}