Fast, safe array access - stackalloc equivalent?
-
Hey guys. I'm writing a key schedule method for a cryptographic primitive - the signature of this method is
void SetKey(byte[] key)
. The problem I'm encountering is that I don't know the length of thekey
array before the method is called, but I need to expand it to a fixed length (say, 256 bits) because all manipulation needs to be done on the fixed-length array. I won't need this array any more after theSetKey
method has finished, and since the array contains sensitive material I'd like it to be wiped from memory as quickly as possible. I can find four ways to do what I need here: 1. Use something likebyte* expanded = stackalloc byte[256 / 8]
. The key is declared on the stack, so it will be overwritten on future function calls. Array lookups are also extremely fast, but this code cannot be verified typesafe. The lack of type safety makes it a poor candidate. 2. Create a new array on every execution of the method:byte[] expanded = new byte[256 / 8]
. This is typesafe, and I can callArray.Clear
before the method returns, but this is dreadfully slow if theSetKey
method is called in a loop. 3. Define an arraybyte[] m_expanded = new byte[256 / 8]
in the class containingSetKey
. This is typesafe and prevents overworking the garbage collector ifSetKey
is called in a loop, and I can simply clear the array when the method exits. But putting this temporary array in the class itself seems like bad coding style. 4. For certain primitives, the key needs to be cast to a series of integers, and the key scheduling algorithms operate directly on the integers. I have a structure declared in the class containingSetKey
:private struct Block { uint a; uint b; . . . }
. Then I can declareBlock b;
as a local (stack) variable withinSetKey
and passref b
as an argument to helper methods (which must operate directly onb
). This is typesafe and declared on the stack, so it will be overwritten on future function calls. This only works in cases where the expanded key array is updated in order (index 0, 1, 2, . . ., corresponding to membersb.a
,b.b
,b.c
, ...) and where I can unroll the key expanding loop. So this is not a general solution. Currently I'm using (3) for most of my primitives and (4) for a select few. What I'd really -
Hey guys. I'm writing a key schedule method for a cryptographic primitive - the signature of this method is
void SetKey(byte[] key)
. The problem I'm encountering is that I don't know the length of thekey
array before the method is called, but I need to expand it to a fixed length (say, 256 bits) because all manipulation needs to be done on the fixed-length array. I won't need this array any more after theSetKey
method has finished, and since the array contains sensitive material I'd like it to be wiped from memory as quickly as possible. I can find four ways to do what I need here: 1. Use something likebyte* expanded = stackalloc byte[256 / 8]
. The key is declared on the stack, so it will be overwritten on future function calls. Array lookups are also extremely fast, but this code cannot be verified typesafe. The lack of type safety makes it a poor candidate. 2. Create a new array on every execution of the method:byte[] expanded = new byte[256 / 8]
. This is typesafe, and I can callArray.Clear
before the method returns, but this is dreadfully slow if theSetKey
method is called in a loop. 3. Define an arraybyte[] m_expanded = new byte[256 / 8]
in the class containingSetKey
. This is typesafe and prevents overworking the garbage collector ifSetKey
is called in a loop, and I can simply clear the array when the method exits. But putting this temporary array in the class itself seems like bad coding style. 4. For certain primitives, the key needs to be cast to a series of integers, and the key scheduling algorithms operate directly on the integers. I have a structure declared in the class containingSetKey
:private struct Block { uint a; uint b; . . . }
. Then I can declareBlock b;
as a local (stack) variable withinSetKey
and passref b
as an argument to helper methods (which must operate directly onb
). This is typesafe and declared on the stack, so it will be overwritten on future function calls. This only works in cases where the expanded key array is updated in order (index 0, 1, 2, . . ., corresponding to membersb.a
,b.b
,b.c
, ...) and where I can unroll the key expanding loop. So this is not a general solution. Currently I'm using (3) for most of my primitives and (4) for a select few. What I'd reallyVega02 wrote:
1. Use something like byte* expanded = stackalloc byte[256 / 8]. The key is declared on the stack, so it will be overwritten on future function calls.
That statement isn't 100% true. It will only be overwritten when enough nested function calls are made such that the stack height overwrites the array. It's possible that the call to SetKey is the deepest call ever made and thus the data is never overwritten. Jared Parsons jaredp@beanseed.org http://jaredparsons.blogspot.com/[^]
-
Vega02 wrote:
1. Use something like byte* expanded = stackalloc byte[256 / 8]. The key is declared on the stack, so it will be overwritten on future function calls.
That statement isn't 100% true. It will only be overwritten when enough nested function calls are made such that the stack height overwrites the array. It's possible that the call to SetKey is the deepest call ever made and thus the data is never overwritten. Jared Parsons jaredp@beanseed.org http://jaredparsons.blogspot.com/[^]
-
Hey guys. I'm writing a key schedule method for a cryptographic primitive - the signature of this method is
void SetKey(byte[] key)
. The problem I'm encountering is that I don't know the length of thekey
array before the method is called, but I need to expand it to a fixed length (say, 256 bits) because all manipulation needs to be done on the fixed-length array. I won't need this array any more after theSetKey
method has finished, and since the array contains sensitive material I'd like it to be wiped from memory as quickly as possible. I can find four ways to do what I need here: 1. Use something likebyte* expanded = stackalloc byte[256 / 8]
. The key is declared on the stack, so it will be overwritten on future function calls. Array lookups are also extremely fast, but this code cannot be verified typesafe. The lack of type safety makes it a poor candidate. 2. Create a new array on every execution of the method:byte[] expanded = new byte[256 / 8]
. This is typesafe, and I can callArray.Clear
before the method returns, but this is dreadfully slow if theSetKey
method is called in a loop. 3. Define an arraybyte[] m_expanded = new byte[256 / 8]
in the class containingSetKey
. This is typesafe and prevents overworking the garbage collector ifSetKey
is called in a loop, and I can simply clear the array when the method exits. But putting this temporary array in the class itself seems like bad coding style. 4. For certain primitives, the key needs to be cast to a series of integers, and the key scheduling algorithms operate directly on the integers. I have a structure declared in the class containingSetKey
:private struct Block { uint a; uint b; . . . }
. Then I can declareBlock b;
as a local (stack) variable withinSetKey
and passref b
as an argument to helper methods (which must operate directly onb
). This is typesafe and declared on the stack, so it will be overwritten on future function calls. This only works in cases where the expanded key array is updated in order (index 0, 1, 2, . . ., corresponding to membersb.a
,b.b
,b.c
, ...) and where I can unroll the key expanding loop. So this is not a general solution. Currently I'm using (3) for most of my primitives and (4) for a select few. What I'd reallyI think using pinned pointers is better in this case. eg
byte[] buffer = new byte[256];
fixed (byte* pbuf = buffer)
{
// no pbuf will be fixed. You can merribly call subfunctions till the end of the block.
}xacc.ide-0.1.1.4 - now with LSharp integration and scripting :)