]>
Commit | Line | Data |
---|---|---|
c38bb727 BL |
1 | /* |
2 | * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the OpenSSL licenses, (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * https://www.openssl.org/source/license.html | |
8 | * or in the file LICENSE in the source distribution. | |
9 | */ | |
10 | ||
11 | /* | |
12 | * Confirm that a^b mod c agrees when calculated cleverly vs naively, for | |
13 | * random a, b and c. | |
14 | */ | |
15 | ||
16 | #include <stdio.h> | |
17 | #include <openssl/bn.h> | |
18 | #include "fuzzer.h" | |
19 | ||
7d22ccee KR |
20 | static BN_CTX *ctx; |
21 | static BIGNUM *b1; | |
22 | static BIGNUM *b2; | |
23 | static BIGNUM *b3; | |
24 | static BIGNUM *b4; | |
25 | static BIGNUM *b5; | |
26 | ||
f3e911d5 KR |
27 | int FuzzerInitialize(int *argc, char ***argv) |
28 | { | |
7d22ccee KR |
29 | b1 = BN_new(); |
30 | b2 = BN_new(); | |
31 | b3 = BN_new(); | |
32 | b4 = BN_new(); | |
33 | b5 = BN_new(); | |
34 | ctx = BN_CTX_new(); | |
35 | ||
90d28f05 BL |
36 | return 1; |
37 | } | |
38 | ||
f3e911d5 KR |
39 | int FuzzerTestOneInput(const uint8_t *buf, size_t len) |
40 | { | |
90d28f05 BL |
41 | int success = 0; |
42 | size_t l1 = 0, l2 = 0, l3 = 0; | |
43 | int s1 = 0, s2 = 0, s3 = 0; | |
c38bb727 | 44 | |
90d28f05 BL |
45 | /* Divide the input into three parts, using the values of the first two |
46 | * bytes to choose lengths, which generate b1, b2 and b3. Use three bits | |
47 | * of the third byte to choose signs for the three numbers. | |
48 | */ | |
c38bb727 BL |
49 | if (len > 2) { |
50 | len -= 3; | |
51 | l1 = (buf[0] * len) / 255; | |
52 | ++buf; | |
53 | l2 = (buf[0] * (len - l1)) / 255; | |
54 | ++buf; | |
55 | l3 = len - l1 - l2; | |
56 | ||
57 | s1 = buf[0] & 1; | |
58 | s2 = buf[0] & 2; | |
59 | s3 = buf[0] & 4; | |
60 | ++buf; | |
61 | } | |
62 | OPENSSL_assert(BN_bin2bn(buf, l1, b1) == b1); | |
63 | BN_set_negative(b1, s1); | |
64 | OPENSSL_assert(BN_bin2bn(buf + l1, l2, b2) == b2); | |
65 | BN_set_negative(b2, s2); | |
66 | OPENSSL_assert(BN_bin2bn(buf + l1 + l2, l3, b3) == b3); | |
67 | BN_set_negative(b3, s3); | |
68 | ||
90d28f05 | 69 | /* mod 0 is undefined */ |
c38bb727 BL |
70 | if (BN_is_zero(b3)) { |
71 | success = 1; | |
72 | goto done; | |
73 | } | |
74 | ||
75 | OPENSSL_assert(BN_mod_exp(b4, b1, b2, b3, ctx)); | |
76 | OPENSSL_assert(BN_mod_exp_simple(b5, b1, b2, b3, ctx)); | |
77 | ||
78 | success = BN_cmp(b4, b5) == 0; | |
79 | if (!success) { | |
80 | BN_print_fp(stdout, b1); | |
81 | putchar('\n'); | |
82 | BN_print_fp(stdout, b2); | |
83 | putchar('\n'); | |
84 | BN_print_fp(stdout, b3); | |
85 | putchar('\n'); | |
86 | BN_print_fp(stdout, b4); | |
87 | putchar('\n'); | |
88 | BN_print_fp(stdout, b5); | |
89 | putchar('\n'); | |
90 | } | |
91 | ||
92 | done: | |
93 | OPENSSL_assert(success); | |
94 | ||
95 | return 0; | |
96 | } | |
ad4da7fb KR |
97 | |
98 | void FuzzerCleanup(void) | |
99 | { | |
7d22ccee KR |
100 | BN_free(b1); |
101 | BN_free(b2); | |
102 | BN_free(b3); | |
103 | BN_free(b4); | |
104 | BN_free(b5); | |
105 | BN_CTX_free(ctx); | |
ad4da7fb | 106 | } |