nagendra modadugu | 4fae542 | 2016-05-10 16:11:54 -0700 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #include "cryptoc/p256_prng.h" |
| 18 | |
| 19 | static const uint8_t expected[P256_PRNG_SIZE] = { |
| 20 | 0x3c,0x0a,0x3f,0xdc,0x53,0x13,0xde,0x17, |
| 21 | 0x5f,0xad,0xc5,0x53,0xd7,0x6b,0xc6,0x28, |
| 22 | 0x40,0x37,0x23,0x3c,0x26,0x80,0xe0,0xcd, |
| 23 | 0x4c,0xf6,0xfd,0x11,0x28,0x18,0xfe,0x31 |
| 24 | }; |
| 25 | |
| 26 | int main(int argc, char* argv[]) { |
| 27 | P256_PRNG_CTX ctx; |
| 28 | uint8_t rnd[P256_PRNG_SIZE]; |
| 29 | int i; |
| 30 | |
| 31 | p256_prng_init(&ctx, "p256_prng_test", 14, 0); |
| 32 | |
| 33 | if (ctx.instance_count != 0) return 1; |
| 34 | if (ctx.call_count != 0) return 1; |
| 35 | |
| 36 | p256_prng_draw(&ctx, rnd); |
| 37 | |
| 38 | if (ctx.instance_count != 0) return 1; |
| 39 | if (ctx.call_count != 1) return 1; |
| 40 | |
| 41 | p256_prng_draw(&ctx, rnd); |
| 42 | |
| 43 | if (ctx.instance_count != 0) return 1; |
| 44 | if (ctx.call_count != 2) return 1; |
| 45 | |
| 46 | // Compare prng output against known good output. |
| 47 | // This catches endianess issues and algorigthm changes. |
| 48 | if (memcmp(rnd, expected, P256_PRNG_SIZE)) { |
| 49 | // Print failed output in case it is to be the new known good output. |
| 50 | printf("{"); |
| 51 | for (i = 0; i < P256_PRNG_SIZE; ++i) { |
| 52 | if (i) printf(","); |
| 53 | printf("0x%02x", rnd[i]); |
| 54 | } |
| 55 | printf("}\n"); |
| 56 | |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |