public static Keys128128 getKeySchedules_128_128(QWords128 key) { var r = Keys128128.alloc(); var keys = r.keys; keys[0] = key.v0; for (uint64 i = 0; i < ROUNDS128128 - 1; i++) { speck_round_64(ref key.v1, ref key.v0, i); keys[1 + i] = key.v0; } return(r); }
public static void encrypt_128_128_4blocks(Keys128128 keySchedules, QWords128 *plaintext) { var keys = keySchedules.keys; uint64 v01 = plaintext[0].v1, v00 = plaintext[0].v0; uint64 v11 = plaintext[1].v1, v10 = plaintext[1].v0; uint64 v21 = plaintext[2].v1, v20 = plaintext[2].v0; uint64 v31 = plaintext[3].v1, v30 = plaintext[3].v0; foreach (var key in keys) { const int WORDSIZE = 64; v01 = (v01 >> 8) | (v01 << (WORDSIZE - 8)); // x = ROTR(x, 8) v01 += v00; v01 ^= key; v00 = (v00 << 3) | (v00 >> (WORDSIZE - 3)); // y = ROTL(y, 3) v00 ^= v01; v11 = (v11 >> 8) | (v11 << (WORDSIZE - 8)); v11 += v10; v11 ^= key; v10 = (v10 << 3) | (v10 >> (WORDSIZE - 3)); v10 ^= v11; v21 = (v21 >> 8) | (v21 << (WORDSIZE - 8)); v21 += v20; v21 ^= key; v20 = (v20 << 3) | (v20 >> (WORDSIZE - 3)); v20 ^= v21; v31 = (v31 >> 8) | (v31 << (WORDSIZE - 8)); v31 += v30; v31 ^= key; v30 = (v30 << 3) | (v30 >> (WORDSIZE - 3)); v30 ^= v31; } plaintext[0].v1 = v01; plaintext[0].v0 = v00; plaintext[1].v1 = v11; plaintext[1].v0 = v10; plaintext[2].v1 = v21; plaintext[2].v0 = v20; plaintext[3].v1 = v31; plaintext[3].v0 = v30; }
static void Update(byte *bytes, uint len, QWords128 *keyStreamBuf, ref uint keystreamBufferPos, ref QWords128 counter, Keys128128 keys) { while (len > 0) { var remainningKeystream = KeystreamBufferSize - keystreamBufferPos; if (remainningKeystream == 0) { keystreamBufferPos = 0; remainningKeystream = KeystreamBufferSize; var ksb = keyStreamBuf; for (uint i = 0; i < KsBlockCount; i += 4) { for (uint j = 0; j < 4; j++) { ksb[i + j] = counter; if (++counter.v1 == 0) { ++counter.v0; } } Cipher.encrypt_128_128_4blocks(keys, &ksb[i]); } } var count = len < remainningKeystream ? len : remainningKeystream; NaiveUtils.XorBytesUnsafe((byte *)keyStreamBuf + keystreamBufferPos, bytes, count); bytes += count; len -= count; keystreamBufferPos += count; } }