| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> |
| 6 #include <stdint.h> |
| 7 #include <stdio.h> |
| 5 #include <string.h> | 8 #include <string.h> |
| 6 #include <stdio.h> | |
| 7 | 9 |
| 8 #include "crypto/p224.h" | 10 #include "crypto/p224.h" |
| 9 | 11 |
| 12 #include "base/macros.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 14 |
| 12 namespace crypto { | 15 namespace crypto { |
| 13 | 16 |
| 14 using p224::Point; | 17 using p224::Point; |
| 15 | 18 |
| 16 // kBasePointExternal is the P224 base point in external representation. | 19 // kBasePointExternal is the P224 base point in external representation. |
| 17 static const uint8 kBasePointExternal[56] = { | 20 static const uint8_t kBasePointExternal[56] = { |
| 18 0xb7, 0x0e, 0x0c, 0xbd, 0x6b, 0xb4, 0xbf, 0x7f, | 21 0xb7, 0x0e, 0x0c, 0xbd, 0x6b, 0xb4, 0xbf, 0x7f, 0x32, 0x13, 0x90, 0xb9, |
| 19 0x32, 0x13, 0x90, 0xb9, 0x4a, 0x03, 0xc1, 0xd3, | 22 0x4a, 0x03, 0xc1, 0xd3, 0x56, 0xc2, 0x11, 0x22, 0x34, 0x32, 0x80, 0xd6, |
| 20 0x56, 0xc2, 0x11, 0x22, 0x34, 0x32, 0x80, 0xd6, | 23 0x11, 0x5c, 0x1d, 0x21, 0xbd, 0x37, 0x63, 0x88, 0xb5, 0xf7, 0x23, 0xfb, |
| 21 0x11, 0x5c, 0x1d, 0x21, 0xbd, 0x37, 0x63, 0x88, | 24 0x4c, 0x22, 0xdf, 0xe6, 0xcd, 0x43, 0x75, 0xa0, 0x5a, 0x07, 0x47, 0x64, |
| 22 0xb5, 0xf7, 0x23, 0xfb, 0x4c, 0x22, 0xdf, 0xe6, | 25 0x44, 0xd5, 0x81, 0x99, 0x85, 0x00, 0x7e, 0x34, |
| 23 0xcd, 0x43, 0x75, 0xa0, 0x5a, 0x07, 0x47, 0x64, | |
| 24 0x44, 0xd5, 0x81, 0x99, 0x85, 0x00, 0x7e, 0x34, | |
| 25 }; | 26 }; |
| 26 | 27 |
| 27 // TestVector represents a test of scalar multiplication of the base point. | 28 // TestVector represents a test of scalar multiplication of the base point. |
| 28 // |scalar| is a big-endian scalar and |affine| is the external representation | 29 // |scalar| is a big-endian scalar and |affine| is the external representation |
| 29 // of g*scalar. | 30 // of g*scalar. |
| 30 struct TestVector { | 31 struct TestVector { |
| 31 uint8 scalar[28]; | 32 uint8_t scalar[28]; |
| 32 uint8 affine[28*2]; | 33 uint8_t affine[28 * 2]; |
| 33 }; | 34 }; |
| 34 | 35 |
| 35 static const int kNumNISTTestVectors = 52; | 36 static const int kNumNISTTestVectors = 52; |
| 36 | 37 |
| 37 // kNISTTestVectors are the NIST test vectors for P224. | 38 // kNISTTestVectors are the NIST test vectors for P224. |
| 38 static const TestVector kNISTTestVectors[kNumNISTTestVectors] = { | 39 static const TestVector kNISTTestVectors[kNumNISTTestVectors] = { |
| 39 { | 40 { |
| 40 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 41 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| (...skipping 764 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 807 p224::Add(minus_b, sum, &a_again); | 808 p224::Add(minus_b, sum, &a_again); |
| 808 EXPECT_TRUE(a_again.ToString() == a.ToString()); | 809 EXPECT_TRUE(a_again.ToString() == a.ToString()); |
| 809 } | 810 } |
| 810 | 811 |
| 811 TEST(P224, Infinity) { | 812 TEST(P224, Infinity) { |
| 812 char zeros[56]; | 813 char zeros[56]; |
| 813 memset(zeros, 0, sizeof(zeros)); | 814 memset(zeros, 0, sizeof(zeros)); |
| 814 | 815 |
| 815 // Test that x^0 = ∞. | 816 // Test that x^0 = ∞. |
| 816 Point a; | 817 Point a; |
| 817 p224::ScalarBaseMult(reinterpret_cast<const uint8*>(zeros), &a); | 818 p224::ScalarBaseMult(reinterpret_cast<const uint8_t*>(zeros), &a); |
| 818 EXPECT_TRUE(memcmp(zeros, a.ToString().data(), sizeof(zeros)) == 0); | 819 EXPECT_TRUE(memcmp(zeros, a.ToString().data(), sizeof(zeros)) == 0); |
| 819 | 820 |
| 820 // We shouldn't allow ∞ to be imported. | 821 // We shouldn't allow ∞ to be imported. |
| 821 EXPECT_FALSE(a.SetFromString(std::string(zeros, sizeof(zeros)))); | 822 EXPECT_FALSE(a.SetFromString(std::string(zeros, sizeof(zeros)))); |
| 822 } | 823 } |
| 823 | 824 |
| 824 } // namespace crypto | 825 } // namespace crypto |
| OLD | NEW |