]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/9/port/random.c
kernel: massive pci code rewrite
[plan9front.git] / sys / src / 9 / port / random.c
index 253c89b031cda51eace685e5dcb31341e46dc1d8..b27e5720dbd9c427f17ec62075c9ad8e7e9f4416 100644 (file)
@@ -89,7 +89,6 @@ ulong
 randomread(void *p, ulong n)
 {
        Chachastate c;
-       ulong b;
 
        if(n == 0)
                return 0;
@@ -97,12 +96,13 @@ randomread(void *p, ulong n)
        if(hwrandbuf != nil)
                (*hwrandbuf)(p, n);
 
-       /* copy chacha state and advance block counter */
+       /* copy chacha state, rekey and increment iv */
        qlock(rs);
        c = *rs;
-       b = rs->input[12];
-       rs->input[12] += (n + ChachaBsize-1)/ChachaBsize;
-       if(rs->input[12] < b) rs->input[13]++;
+       chacha_encrypt((uchar*)&rs->input[4], 32, &c);
+       if(++rs->input[13] == 0)
+               if(++rs->input[14] == 0)
+                       ++rs->input[15];
        qunlock(rs);
 
        /* encrypt the buffer, can fault */