2017-11-30 9 views
9

https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Helpers/Crypto.cs#L159이 루프가 의도적으로 최적화되지 않은 이유는 무엇입니까?

// Compares two byte arrays for equality. The method is specifically written so that the loop is not optimized. 
[MethodImpl(MethodImplOptions.NoOptimization)] 
private static bool ByteArraysEqual(byte[] a, byte[] b) 
{ 
    if (ReferenceEquals(a, b)) 
    { 
     return true; 
    } 

    if (a == null || b == null || a.Length != b.Length) 
    { 
     return false; 
    } 

    bool areSame = true; 
    for (int i = 0; i < a.Length; i++) 
    { 
     areSame &= (a[i] == b[i]); 
    } 
    return areSame; 
} 
+5

https://security.stackexchange.com/questions/160808/why-should-memcmp-not-be-used-to-compare-security-critical-data – Ryan

답변

7

그것은 timing attacks의 가능성을 배제하기 위해 그런 식으로 작성합니다.

코드에 초기 최적화가 명백한 경우이를 실행하는 데 걸리는 시간을 기준으로 비교 결과에 대한 정보가 누설 될 수 있습니다. 동일한 배열은 비교하는 데 시간이 오래 걸립니다.

암호 관련 코드 구현의 일부로 사용되는 경우 누출 된 정보가 공격자가 해킹하려고 시도하는 데 도움이 될 수 있습니다.

언뜻보기에는보기 힘든 방법 인 것처럼 보이지만 실제적인 위협입니다. 예를 들어 this paper을 참조하십시오.