1305153Sjkim/* 2305153Sjkim * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. 3299389Sjkim * 4299389Sjkim * Licensed under the Apache License 2.0 (the "License"); 595967Speter * you may not use this file except in compliance with the License. 6238405Sjkim * You may obtain a copy of the License at 7238405Sjkim * https://www.openssl.org/source/license.html 8238405Sjkim * or in the file LICENSE in the source distribution. 995967Speter */ 10238405Sjkim 1195967Speter/* 1295967Speter * Confirm that a^b mod c agrees when calculated cleverly vs naively, for 1395967Speter * random a, b and c. 1495967Speter */ 15238405Sjkim 16238405Sjkim#include <stdio.h> 17127326Smarkm#include <openssl/bn.h> 18238405Sjkim#include <openssl/err.h> 19238405Sjkim#include "fuzzer.h" 2095967Speter 21238405Sjkim 22238405Sjkimint FuzzerInitialize(int *argc, char ***argv) 23238405Sjkim{ 24238405Sjkim OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); 25238405Sjkim ERR_clear_error(); 26238405Sjkim 27238405Sjkim return 1; 28238405Sjkim} 29238405Sjkim 30238405Sjkimint FuzzerTestOneInput(const uint8_t *buf, size_t len) 31238405Sjkim{ 32238405Sjkim int success = 0; 33238405Sjkim size_t l1 = 0, l2 = 0, l3 = 0; 34238405Sjkim int s1 = 0, s3 = 0; 35238405Sjkim BN_CTX *ctx; 36238405Sjkim BIGNUM *b1; 37238405Sjkim BIGNUM *b2; 38238405Sjkim BIGNUM *b3; 39238405Sjkim BIGNUM *b4; 40238405Sjkim BIGNUM *b5; 41238405Sjkim 42238405Sjkim b1 = BN_new(); 43238405Sjkim b2 = BN_new(); 44238405Sjkim b3 = BN_new(); 45238405Sjkim b4 = BN_new(); 46238405Sjkim b5 = BN_new(); 47238405Sjkim ctx = BN_CTX_new(); 48238405Sjkim 49238405Sjkim /* Divide the input into three parts, using the values of the first two 50238405Sjkim * bytes to choose lengths, which generate b1, b2 and b3. Use three bits 51238405Sjkim * of the third byte to choose signs for the three numbers. 52238405Sjkim */ 53238405Sjkim if (len > 2) { 54238405Sjkim len -= 3; 55238405Sjkim l1 = (buf[0] * len) / 255; 56238405Sjkim ++buf; 57238405Sjkim l2 = (buf[0] * (len - l1)) / 255; 58238405Sjkim ++buf; 59238405Sjkim l3 = len - l1 - l2; 60238405Sjkim 61238405Sjkim s1 = buf[0] & 1; 62238405Sjkim s3 = buf[0] & 4; 63238405Sjkim ++buf; 64238405Sjkim } 65238405Sjkim OPENSSL_assert(BN_bin2bn(buf, l1, b1) == b1); 66238405Sjkim BN_set_negative(b1, s1); 67238405Sjkim OPENSSL_assert(BN_bin2bn(buf + l1, l2, b2) == b2); 68238405Sjkim OPENSSL_assert(BN_bin2bn(buf + l1 + l2, l3, b3) == b3); 69238405Sjkim BN_set_negative(b3, s3); 70238405Sjkim 71238405Sjkim /* mod 0 is undefined */ 72238405Sjkim if (BN_is_zero(b3)) { 73238405Sjkim success = 1; 74238405Sjkim goto done; 75238405Sjkim } 76238405Sjkim 77238405Sjkim OPENSSL_assert(BN_mod_exp(b4, b1, b2, b3, ctx)); 78238405Sjkim OPENSSL_assert(BN_mod_exp_simple(b5, b1, b2, b3, ctx)); 79238405Sjkim 80238405Sjkim success = BN_cmp(b4, b5) == 0; 81238405Sjkim if (!success) { 82238405Sjkim BN_print_fp(stdout, b1); 83238405Sjkim putchar('\n'); 84238405Sjkim BN_print_fp(stdout, b2); 85238405Sjkim putchar('\n'); 86238405Sjkim BN_print_fp(stdout, b3); 87238405Sjkim putchar('\n'); 88238405Sjkim BN_print_fp(stdout, b4); 89238405Sjkim putchar('\n'); 90238405Sjkim BN_print_fp(stdout, b5); 91238405Sjkim putchar('\n'); 92238405Sjkim } 93238405Sjkim 94238405Sjkim done: 95238405Sjkim OPENSSL_assert(success); 96238405Sjkim BN_free(b1); 97238405Sjkim BN_free(b2); 98238405Sjkim BN_free(b3); 99238405Sjkim BN_free(b4); 100238405Sjkim BN_free(b5); 101238405Sjkim BN_CTX_free(ctx); 102238405Sjkim ERR_clear_error(); 103238405Sjkim 104238405Sjkim return 0; 10595967Speter} 106238405Sjkim 107238405Sjkimvoid FuzzerCleanup(void) 108238405Sjkim{ 109238405Sjkim} 110238405Sjkim