< Summary

Information
Class: Renci.SshNet.Security.Chaos.NaCl.Internal.Salsa.SalsaCore
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Chaos.NaCl\Internal\Salsa\SalsaCore.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 126
Coverable lines: 126
Total lines: 263
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
HSalsa(...)0%20%
Salsa(...)100%10%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Chaos.NaCl\Internal\Salsa\SalsaCore.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3
 4namespace Renci.SshNet.Security.Chaos.NaCl.Internal.Salsa
 5{
 6    internal static class SalsaCore
 7    {
 8        internal static void HSalsa(out Array16<UInt32> output, ref Array16<UInt32> input, int rounds)
 09        {
 010            InternalAssert.Assert(rounds % 2 == 0, "Number of salsa rounds must be even");
 11
 012            int doubleRounds = rounds / 2;
 13
 014            UInt32 x0 = input.x0;
 015            UInt32 x1 = input.x1;
 016            UInt32 x2 = input.x2;
 017            UInt32 x3 = input.x3;
 018            UInt32 x4 = input.x4;
 019            UInt32 x5 = input.x5;
 020            UInt32 x6 = input.x6;
 021            UInt32 x7 = input.x7;
 022            UInt32 x8 = input.x8;
 023            UInt32 x9 = input.x9;
 024            UInt32 x10 = input.x10;
 025            UInt32 x11 = input.x11;
 026            UInt32 x12 = input.x12;
 027            UInt32 x13 = input.x13;
 028            UInt32 x14 = input.x14;
 029            UInt32 x15 = input.x15;
 30
 31            unchecked
 032            {
 033                for (int i = 0; i < doubleRounds; i++)
 034                {
 35                    UInt32 y;
 36
 37                    // row 0
 038                    y = x0 + x12;
 039                    x4 ^= (y << 7) | (y >> (32 - 7));
 040                    y = x4 + x0;
 041                    x8 ^= (y << 9) | (y >> (32 - 9));
 042                    y = x8 + x4;
 043                    x12 ^= (y << 13) | (y >> (32 - 13));
 044                    y = x12 + x8;
 045                    x0 ^= (y << 18) | (y >> (32 - 18));
 46
 47                    // row 1
 048                    y = x5 + x1;
 049                    x9 ^= (y << 7) | (y >> (32 - 7));
 050                    y = x9 + x5;
 051                    x13 ^= (y << 9) | (y >> (32 - 9));
 052                    y = x13 + x9;
 053                    x1 ^= (y << 13) | (y >> (32 - 13));
 054                    y = x1 + x13;
 055                    x5 ^= (y << 18) | (y >> (32 - 18));
 56
 57                    // row 2
 058                    y = x10 + x6;
 059                    x14 ^= (y << 7) | (y >> (32 - 7));
 060                    y = x14 + x10;
 061                    x2 ^= (y << 9) | (y >> (32 - 9));
 062                    y = x2 + x14;
 063                    x6 ^= (y << 13) | (y >> (32 - 13));
 064                    y = x6 + x2;
 065                    x10 ^= (y << 18) | (y >> (32 - 18));
 66
 67                    // row 3
 068                    y = x15 + x11;
 069                    x3 ^= (y << 7) | (y >> (32 - 7));
 070                    y = x3 + x15;
 071                    x7 ^= (y << 9) | (y >> (32 - 9));
 072                    y = x7 + x3;
 073                    x11 ^= (y << 13) | (y >> (32 - 13));
 074                    y = x11 + x7;
 075                    x15 ^= (y << 18) | (y >> (32 - 18));
 76
 77                    // column 0
 078                    y = x0 + x3;
 079                    x1 ^= (y << 7) | (y >> (32 - 7));
 080                    y = x1 + x0;
 081                    x2 ^= (y << 9) | (y >> (32 - 9));
 082                    y = x2 + x1;
 083                    x3 ^= (y << 13) | (y >> (32 - 13));
 084                    y = x3 + x2;
 085                    x0 ^= (y << 18) | (y >> (32 - 18));
 86
 87                    // column 1
 088                    y = x5 + x4;
 089                    x6 ^= (y << 7) | (y >> (32 - 7));
 090                    y = x6 + x5;
 091                    x7 ^= (y << 9) | (y >> (32 - 9));
 092                    y = x7 + x6;
 093                    x4 ^= (y << 13) | (y >> (32 - 13));
 094                    y = x4 + x7;
 095                    x5 ^= (y << 18) | (y >> (32 - 18));
 96
 97                    // column 2
 098                    y = x10 + x9;
 099                    x11 ^= (y << 7) | (y >> (32 - 7));
 0100                    y = x11 + x10;
 0101                    x8 ^= (y << 9) | (y >> (32 - 9));
 0102                    y = x8 + x11;
 0103                    x9 ^= (y << 13) | (y >> (32 - 13));
 0104                    y = x9 + x8;
 0105                    x10 ^= (y << 18) | (y >> (32 - 18));
 106
 107                    // column 3
 0108                    y = x15 + x14;
 0109                    x12 ^= (y << 7) | (y >> (32 - 7));
 0110                    y = x12 + x15;
 0111                    x13 ^= (y << 9) | (y >> (32 - 9));
 0112                    y = x13 + x12;
 0113                    x14 ^= (y << 13) | (y >> (32 - 13));
 0114                    y = x14 + x13;
 0115                    x15 ^= (y << 18) | (y >> (32 - 18));
 0116                }
 0117            }
 118
 0119            output.x0 = x0;
 0120            output.x1 = x1;
 0121            output.x2 = x2;
 0122            output.x3 = x3;
 0123            output.x4 = x4;
 0124            output.x5 = x5;
 0125            output.x6 = x6;
 0126            output.x7 = x7;
 0127            output.x8 = x8;
 0128            output.x9 = x9;
 0129            output.x10 = x10;
 0130            output.x11 = x11;
 0131            output.x12 = x12;
 0132            output.x13 = x13;
 0133            output.x14 = x14;
 0134            output.x15 = x15;
 0135        }
 136
 137        internal static void Salsa(out Array16<UInt32> output, ref Array16<UInt32> input, int rounds)
 0138        {
 139            Array16<UInt32> temp;
 0140            HSalsa(out temp, ref input, rounds);
 141            unchecked
 0142            {
 0143                output.x0 = temp.x0 + input.x0;
 0144                output.x1 = temp.x1 + input.x1;
 0145                output.x2 = temp.x2 + input.x2;
 0146                output.x3 = temp.x3 + input.x3;
 0147                output.x4 = temp.x4 + input.x4;
 0148                output.x5 = temp.x5 + input.x5;
 0149                output.x6 = temp.x6 + input.x6;
 0150                output.x7 = temp.x7 + input.x7;
 0151                output.x8 = temp.x8 + input.x8;
 0152                output.x9 = temp.x9 + input.x9;
 0153                output.x10 = temp.x10 + input.x10;
 0154                output.x11 = temp.x11 + input.x11;
 0155                output.x12 = temp.x12 + input.x12;
 0156                output.x13 = temp.x13 + input.x13;
 0157                output.x14 = temp.x14 + input.x14;
 0158                output.x15 = temp.x15 + input.x15;
 0159            }
 0160        }
 161
 162        /*internal static void SalsaCore(int[] output, int outputOffset, int[] input, int inputOffset, int rounds)
 163        {
 164            if (rounds % 2 != 0)
 165                throw new ArgumentException("rounds must be even");
 166        }
 167
 168
 169static void store_littleendian(unsigned char *x,uint32 u)
 170{
 171  x[0] = u; u >>= 8;
 172  x[1] = u; u >>= 8;
 173  x[2] = u; u >>= 8;
 174  x[3] = u;
 175}
 176
 177        internal static void HSalsaCore(int[] output, int outputOffset, int[] input, int inputOffset, int rounds)
 178        {
 179            if (rounds % 2 != 0)
 180                throw new ArgumentException("rounds must be even");
 181            static uint32 rotate(uint32 u,int c)
 182{
 183  return (u << c) | (u >> (32 - c));
 184}
 185
 186
 187
 188int crypto_core(
 189        unsigned char *out,
 190  const unsigned char *in,
 191  const unsigned char *k,
 192  const unsigned char *c
 193)
 194{
 195  uint32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
 196  int i;
 197
 198  x0 = load_littleendian(c + 0);
 199  x1 = load_littleendian(k + 0);
 200  x2 = load_littleendian(k + 4);
 201  x3 = load_littleendian(k + 8);
 202  x4 = load_littleendian(k + 12);
 203  x5 = load_littleendian(c + 4);
 204  x6 = load_littleendian(in + 0);
 205  x7 = load_littleendian(in + 4);
 206  x8 = load_littleendian(in + 8);
 207  x9 = load_littleendian(in + 12);
 208  x10 = load_littleendian(c + 8);
 209  x11 = load_littleendian(k + 16);
 210  x12 = load_littleendian(k + 20);
 211  x13 = load_littleendian(k + 24);
 212  x14 = load_littleendian(k + 28);
 213  x15 = load_littleendian(c + 12);
 214
 215  for (i = ROUNDS;i > 0;i -= 2) {
 216     x4 ^= rotate( x0+x12, 7);
 217     x8 ^= rotate( x4+ x0, 9);
 218    x12 ^= rotate( x8+ x4,13);
 219     x0 ^= rotate(x12+ x8,18);
 220     x9 ^= rotate( x5+ x1, 7);
 221    x13 ^= rotate( x9+ x5, 9);
 222     x1 ^= rotate(x13+ x9,13);
 223     x5 ^= rotate( x1+x13,18);
 224    x14 ^= rotate(x10+ x6, 7);
 225     x2 ^= rotate(x14+x10, 9);
 226     x6 ^= rotate( x2+x14,13);
 227    x10 ^= rotate( x6+ x2,18);
 228     x3 ^= rotate(x15+x11, 7);
 229     x7 ^= rotate( x3+x15, 9);
 230    x11 ^= rotate( x7+ x3,13);
 231    x15 ^= rotate(x11+ x7,18);
 232     x1 ^= rotate( x0+ x3, 7);
 233     x2 ^= rotate( x1+ x0, 9);
 234     x3 ^= rotate( x2+ x1,13);
 235     x0 ^= rotate( x3+ x2,18);
 236     x6 ^= rotate( x5+ x4, 7);
 237     x7 ^= rotate( x6+ x5, 9);
 238     x4 ^= rotate( x7+ x6,13);
 239     x5 ^= rotate( x4+ x7,18);
 240    x11 ^= rotate(x10+ x9, 7);
 241     x8 ^= rotate(x11+x10, 9);
 242     x9 ^= rotate( x8+x11,13);
 243    x10 ^= rotate( x9+ x8,18);
 244    x12 ^= rotate(x15+x14, 7);
 245    x13 ^= rotate(x12+x15, 9);
 246    x14 ^= rotate(x13+x12,13);
 247    x15 ^= rotate(x14+x13,18);
 248  }
 249
 250  store_littleendian(out + 0,x0);
 251  store_littleendian(out + 4,x5);
 252  store_littleendian(out + 8,x10);
 253  store_littleendian(out + 12,x15);
 254  store_littleendian(out + 16,x6);
 255  store_littleendian(out + 20,x7);
 256  store_littleendian(out + 24,x8);
 257  store_littleendian(out + 28,x9);
 258
 259  return 0;
 260}*/
 261
 262    }
 263}