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