Mercurial > hg > quicktun
comparison src/proto.nacltai.c @ 37:bb4bbf380938
Added option PRIVATE_KEY_FILE to read private key from file
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Mon, 06 May 2013 21:28:45 +0200 |
parents | 51c6d2fc712f |
children | d9f5caa13898 |
comparison
equal
deleted
inserted
replaced
36:1fe62a94c28a | 37:bb4bbf380938 |
---|---|
162 printf("Initializing cryptography...\n"); | 162 printf("Initializing cryptography...\n"); |
163 unsigned char cownpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES], cpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES], csecretkey[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES]; | 163 unsigned char cownpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES], cpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES], csecretkey[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES]; |
164 if (!(envval = getconf("PUBLIC_KEY"))) return errorexit("Missing PUBLIC_KEY"); | 164 if (!(envval = getconf("PUBLIC_KEY"))) return errorexit("Missing PUBLIC_KEY"); |
165 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PUBLIC_KEY length"); | 165 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PUBLIC_KEY length"); |
166 hex2bin(cpublickey, envval, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES); | 166 hex2bin(cpublickey, envval, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES); |
167 if (!(envval = getconf("PRIVATE_KEY"))) return errorexit("Missing PRIVATE_KEY"); | 167 if (envval = getconf("PRIVATE_KEY")) { |
168 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PRIVATE_KEY length"); | 168 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PRIVATE_KEY length"); |
169 hex2bin(csecretkey, envval, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES); | 169 hex2bin(csecretkey, envval, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES); |
170 } else if (envval = getconf("PRIVATE_KEY_FILE")) { | |
171 FILE* pkfile = fopen(envval, "rb"); | |
172 if (!pkfile) return errorexitp("Could not open PRIVATE_KEY_FILE"); | |
173 char pktextbuf[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES * 2]; | |
174 size_t pktextsize = fread(pktextbuf, 1, sizeof(pktextbuf), pkfile); | |
175 if (pktextsize == crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) { | |
176 memcpy(csecretkey, pktextbuf, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES); | |
177 } else if (pktextsize = 2 * crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) { | |
178 hex2bin(csecretkey, pktextbuf, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES); | |
179 } else { | |
180 return errorexit("PRIVATE_KEY length"); | |
181 } | |
182 fclose(pkfile); | |
183 } else { | |
184 return errorexit("Missing PRIVATE_KEY"); | |
185 } | |
170 crypto_box_curve25519xsalsa20poly1305_beforenm(d->cbefore, cpublickey, csecretkey); | 186 crypto_box_curve25519xsalsa20poly1305_beforenm(d->cbefore, cpublickey, csecretkey); |
171 | 187 |
172 memset(d->cenonce, 0, crypto_box_curve25519xsalsa20poly1305_NONCEBYTES); | 188 memset(d->cenonce, 0, crypto_box_curve25519xsalsa20poly1305_NONCEBYTES); |
173 memset(d->cdnonce, 0, crypto_box_curve25519xsalsa20poly1305_NONCEBYTES); | 189 memset(d->cdnonce, 0, crypto_box_curve25519xsalsa20poly1305_NONCEBYTES); |
174 | 190 |