Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:72118 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 32261 invoked from network); 3 Feb 2014 20:57:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Feb 2014 20:57:59 -0000 Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.182 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.217.182 mail-lb0-f182.google.com Received: from [209.85.217.182] ([209.85.217.182:59925] helo=mail-lb0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 13/12-35654-5D200F25 for ; Mon, 03 Feb 2014 15:57:58 -0500 Received: by mail-lb0-f182.google.com with SMTP id w7so5901466lbi.27 for ; Mon, 03 Feb 2014 12:57:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=di9urE0QZUKWCL+lSVSNT52w3SJiRaowLHZelE8ub3A=; b=pUSPl4eQFEGVS7QLw6oUOlCVSPtEurkCuI97wVNq9npeKxJhm7gN8zL0r5oh4DFgjs 5ljUFDFykk9w6i3H+F7lYOzLo1Jp2wo+GCb4y02J2d3mOOsJYUbNVhw/D8yp6kUvGjQ6 nNuGBJyXZUYFK1W7zhgW79FHekRIx41aOrEewzItTCby8Hd2fQycWInTI5/HHu4R1SPN 3CqHLzHcLQBGCx5Cj8Zxzqxv9kTPUSFRmE0KUYY5h91PcgZs+PQY+gZXIJzDe7VE23kP QyycKgG2YEahuLRRh5Zu7gCYMAzqAC0ogVQwFRVEmvQX/7kLnszYhlwm6YpWllhhCJcS Gmlg== X-Received: by 10.152.219.97 with SMTP id pn1mr25916701lac.9.1391461074508; Mon, 03 Feb 2014 12:57:54 -0800 (PST) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.112.199.37 with HTTP; Mon, 3 Feb 2014 12:57:13 -0800 (PST) In-Reply-To: References: <9E3AA302-1EC1-4497-996F-716555CAAB64@rouvenwessling.de> Date: Tue, 4 Feb 2014 05:57:13 +0900 X-Google-Sender-Auth: cdbJwYOfZFtnPmJQ7DoC8_bAWBw Message-ID: To: Nikita Popov Cc: =?UTF-8?Q?Rouven_We=C3=9Fling?= , PHP internals Content-Type: multipart/alternative; boundary=001a1137f67c109ed204f186c70c Subject: Re: [PHP-DEV] [VOTE] Timing attack safe string comparison function From: yohgaki@ohgaki.net (Yasuo Ohgaki) --001a1137f67c109ed204f186c70c Content-Type: text/plain; charset=UTF-8 Hi Nikita, On Tue, Feb 4, 2014 at 2:10 AM, Nikita Popov wrote: > > Hi internals, > > > > as I've received no further feedback I've opened the voting on "Timing > > attack safe string comparison function": > > > > - https://wiki.php.net/rfc/timing_attack > > > > Voting ends on 2014/02/09 11:00PM UTC > > > > Best regards > > Rouven > > > > Did your code already get reviewed by someone with understanding of the > issue? From a quick glance, two potential issues: > * You are using MAX, i.e. an if-then-else branch. I'm pretty sure that the > if and else branches will have different instruction counts in that case. > Simple alternative would be something fixed like mod_len = known_len+1 or > known_len&1. > * You leak information on mod_len / known_len, because you will have > different cache access patterns for comparing always the same 10 memory > positions and 10000 different ones, at least I'd assume so. > I don't know how you can prevent the latter issue, and if it is possible at > all. Personally I'd just drop the length magic and explicitly document it > to be safe for equal-length strings only. In any case you should have this > reviewed by someone with more than just a cursory understanding of the > matter. Length leak is known issue and we may improve these. There was discussion for this. For the sake of completeness, we may address issues now or later. To be honest, I think length leak must be avoided especially for shorter strings. It would be better to iterate at least 100 times regardless of input. Perhaps, something like + /** + * If known_string has a length of 0 we set the length to 1, + * this will cause us to compare all bytes of userString with the null byte which fails + */ + mod_len = MAX(known_len, 1); len = MAX(known_len, 127); + + /* This is security sensitive code. Do not optimize this for speed. */ + result = known_len - user_len; + for (j = 0; j < user_len; j++) { for (j = 0; j < len; j++) { + result |= known_str[j % mod_len] ^ user_str[j]; result |= known_str[j % known_len] ^ user_str[j % user_len]; + } Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net --001a1137f67c109ed204f186c70c--