| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | |
| | | 6 | | namespace Azure.Security.KeyVault.Keys.Cryptography |
| | | 7 | | { |
| | | 8 | | internal static class ByteExtensions |
| | | 9 | | { |
| | | 10 | | internal static bool SequenceEqualConstantTime(this byte[] self, byte[] other) |
| | | 11 | | { |
| | 10 | 12 | | if (self == null) |
| | 0 | 13 | | throw new ArgumentNullException(nameof(self)); |
| | | 14 | | |
| | 10 | 15 | | if (other == null) |
| | 0 | 16 | | throw new ArgumentNullException(nameof(other)); |
| | | 17 | | |
| | | 18 | | // Constant time comparison of two byte arrays |
| | 10 | 19 | | uint difference = (uint)self.Length ^ (uint)other.Length; |
| | | 20 | | |
| | 180 | 21 | | for (var i = 0; i < self.Length && i < other.Length; i++) |
| | | 22 | | { |
| | 80 | 23 | | difference |= (uint)(self[i] ^ other[i]); |
| | | 24 | | } |
| | | 25 | | |
| | 10 | 26 | | return difference == 0; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | internal static byte[] Or(this byte[] self, byte[] other) |
| | | 30 | | { |
| | 0 | 31 | | return Or(self, other, 0); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | internal static byte[] Or(this byte[] self, byte[] other, int offset) |
| | | 35 | | { |
| | 0 | 36 | | if (self == null) |
| | 0 | 37 | | throw new ArgumentNullException(nameof(self)); |
| | | 38 | | |
| | 0 | 39 | | if (other == null) |
| | 0 | 40 | | throw new ArgumentNullException(nameof(other)); |
| | | 41 | | |
| | 0 | 42 | | if (self.Length > other.Length - offset) |
| | 0 | 43 | | throw new ArgumentException("self and other lengths do not match"); |
| | | 44 | | |
| | 0 | 45 | | var result = new byte[self.Length]; |
| | | 46 | | |
| | 0 | 47 | | for (var i = 0; i < self.Length; i++) |
| | | 48 | | { |
| | 0 | 49 | | result[i] = (byte)(self[i] | other[offset + i]); |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | return result; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | internal static byte[] Xor(this byte[] self, byte[] other, bool inPlace = false) |
| | | 56 | | { |
| | 336 | 57 | | return Xor(self, other, 0, inPlace); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | internal static byte[] Xor(this byte[] self, byte[] other, int offset, bool inPlace = false) |
| | | 61 | | { |
| | 336 | 62 | | if (self == null) |
| | 0 | 63 | | throw new ArgumentNullException(nameof(self)); |
| | | 64 | | |
| | 336 | 65 | | if (other == null) |
| | 0 | 66 | | throw new ArgumentNullException(nameof(other)); |
| | | 67 | | |
| | 336 | 68 | | if (self.Length > other.Length - offset) |
| | 0 | 69 | | throw new ArgumentException("self and other lengths do not match"); |
| | | 70 | | |
| | 336 | 71 | | byte[] result = (inPlace) ? self : new byte[self.Length]; |
| | | 72 | | |
| | 6048 | 73 | | for (var i = 0; i < self.Length; i++) |
| | | 74 | | { |
| | 2688 | 75 | | result[i] = (byte)(self[i] ^ other[offset + i]); |
| | | 76 | | } |
| | | 77 | | |
| | 336 | 78 | | return result; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | internal static byte[] Take(this byte[] self, int count) |
| | | 82 | | { |
| | 0 | 83 | | return Take(self, 0, count); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | internal static byte[] Take(this byte[] self, int offset, int count) |
| | | 87 | | { |
| | 0 | 88 | | if (self == null) |
| | 0 | 89 | | throw new ArgumentNullException(nameof(self)); |
| | | 90 | | |
| | 0 | 91 | | if (offset < 0) |
| | 0 | 92 | | throw new ArgumentException($"{nameof(offset)} cannot be < 0", nameof(offset)); |
| | | 93 | | |
| | 0 | 94 | | if (count <= 0) |
| | 0 | 95 | | throw new ArgumentException($"{nameof(count)} cannot be <= 0", nameof(count)); |
| | | 96 | | |
| | 0 | 97 | | if (offset + count > self.Length) |
| | 0 | 98 | | throw new ArgumentException("offset + count cannot be > self.Length", nameof(count)); |
| | | 99 | | |
| | 0 | 100 | | var result = new byte[count]; |
| | | 101 | | |
| | 0 | 102 | | Array.Copy(self, offset, result, 0, count); |
| | | 103 | | |
| | 0 | 104 | | return result; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | internal static void Zero(this byte[] self) |
| | | 108 | | { |
| | 20 | 109 | | if (self == null) |
| | 0 | 110 | | throw new ArgumentNullException(nameof(self)); |
| | | 111 | | |
| | 20 | 112 | | Array.Clear(self, 0, self.Length); |
| | 20 | 113 | | } |
| | | 114 | | } |
| | | 115 | | } |