-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libsec: add chacha20 poly1305 aead, allow 64 bit iv's for chacha,
add tsmemcmp() http://9legacy.org/9legacy/patch/libsec-chacha-iv.diff
- Loading branch information
1 parent
a6a8bfb
commit 18b5cf8
Showing
6 changed files
with
192 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,8 @@ OFILES=\ | |
pbkdf2.$O\ | ||
hkdf.$O\ | ||
poly1305.$O\ | ||
ccpoly.$O\ | ||
tsmemcmp.$O\ | ||
|
||
default: $(LIB) | ||
$(LIB): $(OFILES) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <u.h> | ||
#include <libc.h> | ||
#include <libsec.h> | ||
|
||
static void | ||
ccpolyotk(Chachastate *cs, DigestState *ds) | ||
{ | ||
uchar otk[ChachaBsize]; | ||
|
||
memset(ds, 0, sizeof(*ds)); | ||
memset(otk, 0, 32); | ||
chacha_setblock(cs, 0); | ||
chacha_encrypt(otk, ChachaBsize, cs); | ||
poly1305(nil, 0, otk, 32, nil, ds); | ||
} | ||
|
||
static void | ||
ccpolymac(uchar *buf, ulong nbuf, DigestState *ds) | ||
{ | ||
static uchar zeros[16] = {0}; | ||
ulong npad; | ||
|
||
if(nbuf == 0) | ||
return; | ||
poly1305(buf, nbuf, nil, 0, nil, ds); | ||
npad = nbuf % 16; | ||
if(npad == 0) | ||
return; | ||
poly1305(zeros, 16 - npad, nil, 0, nil, ds); | ||
} | ||
|
||
static void | ||
ccpolytag(ulong ndat, ulong naad, uchar tag[16], DigestState *ds) | ||
{ | ||
uchar info[16]; | ||
|
||
info[0] = naad; | ||
info[1] = naad>>8; | ||
info[2] = naad>>16; | ||
info[3] = naad>>24; | ||
info[4] = 0; | ||
info[5] = 0; | ||
info[6] = 0; | ||
info[7] = 0; | ||
|
||
info[8] = ndat; | ||
info[9] = ndat>>8; | ||
info[10] = ndat>>16; | ||
info[11] = ndat>>24; | ||
info[12] = 0; | ||
info[13] = 0; | ||
info[14] = 0; | ||
info[15] = 0; | ||
|
||
poly1305(info, 16, nil, 0, tag, ds); | ||
} | ||
|
||
void | ||
ccpoly_encrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], Chachastate *cs) | ||
{ | ||
DigestState ds; | ||
|
||
ccpolyotk(cs, &ds); | ||
ccpolymac(aad, naad, &ds); | ||
chacha_encrypt(dat, ndat, cs); | ||
ccpolymac(dat, ndat, &ds); | ||
ccpolytag(ndat, naad, tag, &ds); | ||
} | ||
|
||
int | ||
ccpoly_decrypt(uchar *dat, ulong ndat, uchar *aad, ulong naad, uchar tag[16], Chachastate *cs) | ||
{ | ||
DigestState ds; | ||
uchar tmp[16]; | ||
|
||
ccpolyotk(cs, &ds); | ||
ccpolymac(aad, naad, &ds); | ||
ccpolymac(dat, ndat, &ds); | ||
ccpolytag(ndat, naad, tmp, &ds); | ||
if(tsmemcmp(tag, tmp, 16) != 0) | ||
return -1; | ||
chacha_encrypt(dat, ndat, cs); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <u.h> | ||
#include <libc.h> | ||
#include <libsec.h> | ||
|
||
/* | ||
* timing safe memcmp() | ||
*/ | ||
int | ||
tsmemcmp(void *a1, void *a2, ulong n) | ||
{ | ||
int lt, gt, c1, c2, r, m; | ||
uchar *s1, *s2; | ||
|
||
r = m = 0; | ||
s1 = a1; | ||
s2 = a2; | ||
while(n--){ | ||
c1 = *s1++; | ||
c2 = *s2++; | ||
lt = (c1 - c2) >> 8; | ||
gt = (c2 - c1) >> 8; | ||
r |= (lt - gt) & ~m; | ||
m |= lt | gt; | ||
} | ||
return r; | ||
} |