From patchwork Fri May 29 06:58:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stijn Devriendt X-Patchwork-Id: 1300466 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:4864:20::438; helo=mail-wr1-x438.google.com; envelope-from=swupdate+bncbclnzfwotyirbkhfyl3akgqeowuzjsq@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20161025 header.b=DLxxW1cp; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=mDko3ntE; dkim-atps=neutral Received: from mail-wr1-x438.google.com (mail-wr1-x438.google.com [IPv6:2a00:1450:4864:20::438]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49YFlx69chz9sSn for ; Fri, 29 May 2020 16:58:52 +1000 (AEST) Received: by mail-wr1-x438.google.com with SMTP id c14sf643164wrw.11 for ; Thu, 28 May 2020 23:58:52 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1590735528; cv=pass; d=google.com; s=arc-20160816; b=Fz6JNRKy5qMfRkijd9/U/10ARs7XETmfJtUum8yT7hNUAtW07J71NKsuAX1PsZ1w6h qPKctMTqcKSRAu/2J4NC57tIHP6jCpGaYItGADq4PfmMZjrxCSswTqAsYBOdJVvR/OZd F/A4Hb3uPpXKc7I06jNLAbK0cnIDNPD7z1++/8+MMaGipx02rXhh8t5XsauSBT8gwzUM tBHsaZ8x/529aMwt1DKHAYfVsPpuuRgvTVwIZO94w9tv8jMtcDTjsnEqo3RGqVYeM47m dWEt0lQ4eieEr1rSRdF+/wpC6VBEBkbgkhzFe1VPMDPcweDr9JSBKpzzx34DEB2RHgqF TEIg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:mime-version:sender:dkim-signature :dkim-signature; bh=xmlQWaXGigmfFeNKIdk+xgv5+QheXnxIc2yqokoBTLI=; b=LDG1p1ltInJM7YY5tS/mrycnwH20shehnGLVhPWc7ytxicnA2iRPbwm8PwjeOVz116 Q8GPymrfqNMPe2Lz52bQIyKumXqxJQrX3K3sLG3d3/dDhuzFWqNFRAiTwHVvPcXA5s32 1k7NECQTCaUBgF3il5kN24l0ZuAgE+R35RuZvg0P5L2HNYmciATux+45p5alv6j4sXyr af5LDVfK6HJcx1HdQBu7FudLXm24K2jaGj4Najt3Mq+/K7opgXKtW5JO1LTbYfIlxl00 NiHiTmrze6vqvPKbbu5/5uivtwlEmV4mRcHOET5DE524+aMu71UbHDWkf5KS3b8uECup 6r8g== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=H1rSCaho; spf=pass (google.com: domain of highguy@gmail.com designates 2a00:1450:4864:20::541 as permitted sender) smtp.mailfrom=highguy@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=xmlQWaXGigmfFeNKIdk+xgv5+QheXnxIc2yqokoBTLI=; b=DLxxW1cpyUP5zyIx3vlpCL6rh1HHuo9mJyG42wXtmeWmAofmYTV2QoMgfzfI5jtJYP 6LkWiGZZqI+Ird7H4ulo5AlSBtgTvmM4wkn4GMOsT2h4kxffYOlShuPYnhftR9y/Jj8T P7tDrUhSyVfAfFkYdy3RnfGdhC0Ct11tEAU3AY22DkZCVW4k9zHwOdPmmZZqHRQlpdPu O8oQUTo4S4w5dp4XJ2o9NMaZEiYswIUvTuI0vcnLFLqddThskMMyozZQPZVQkB6bgXFU 1YyR+XrbS7DeRnMGjxpFMzOOrpzRCYXXQTtzcRbKIbch1OKFthekwTNHCO2tEiHnIHHS KL6A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=xmlQWaXGigmfFeNKIdk+xgv5+QheXnxIc2yqokoBTLI=; b=mDko3ntELFIkGUMW2VjjRTamTQWRD+qwwBb1Zi7Ri8BIH+JoXPUZge4YexIodNY+8o r1XP2Ns/f1ljL7jysyAcf4fh6t1I6/hEQ0sr64CMD2uaOkrY8jXJCGQ2Ua+v/fXeB4n7 fC5TWABPXyL6WLso8ZNjLpI69gBSrrY3tuy1+l2KlC/ppmyvEi9n8YZBEezaQbBNq+Tz XRE+q3BIfaiVqYPo9CLaukGpoFTN2fwl+yJrG2J/t7tYDweK+BOwHU+v8w7RnL19kG4E D4MFtVsvfSydt+hhcQ/m+ndldJTzy4vDr77YfOCKSOUUtkOL2vGsHLX9aK2v7HPoqGOC +QBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:mime-version:from:to:cc:subject:date :message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=xmlQWaXGigmfFeNKIdk+xgv5+QheXnxIc2yqokoBTLI=; b=IOiiEFFAj/EjjpTQk5MpKSLxy0BsUwZfbtmnW+6r+LNPzi78pYWL8tNfpqvAEnRMjD uDIgWfC+8taD01usHzxxRz5Pco2tep6mhOmW/D4YU5Z9AHs6I+/qvVCfm3dWYX6t9xRj XEEVWG7pRbRSYmRyC8j0wPRtrp2wGgg4kMnihbRf9LxfQaQiPmjD+OPSP6FrpGX151+Y CbMChRD+a5pKZkW9dGRiYKMcfYtcKTnGxKWq+PHxVjR0gR+Rt0NOfofGGIdrB8dCLd8+ dzGRzZCKIv3siYQnod482jlebBh5QDRiW61eKimExPifts4YqrIUDWuvthKbEE9+Z4cS j76Q== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM532sgbYMDnbIbN/I8okMZfYHOkLgbA2zO+Tcro/DZrhv2/AGWGlU icKQIRSKBTFg+ovQvO9NI6g= X-Google-Smtp-Source: ABdhPJzr6KjuWXMkp9kg2vgLdEDpFuxOPGk91RXd6xgFFaeTpkM2sB7GJTB1XhkZ10CgulKBOJBwhA== X-Received: by 2002:adf:f205:: with SMTP id p5mr7718794wro.302.1590735528827; Thu, 28 May 2020 23:58:48 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: swupdate@googlegroups.com Received: by 2002:a1c:96c1:: with SMTP id y184ls2406368wmd.1.gmail; Thu, 28 May 2020 23:58:48 -0700 (PDT) X-Received: by 2002:a7b:c5d7:: with SMTP id n23mr7586087wmk.185.1590735528133; Thu, 28 May 2020 23:58:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590735528; cv=none; d=google.com; s=arc-20160816; b=Kch3NxvzaMnFeL0UL+6BjRyx+IBPl0ACgRR/Sh326YNVzFctVEpUlnoOZia3p5uIT3 NyfSQrdY4CPxK7dvaDtq71gAGuuiQqZYz3AQxjXgA9Ml/2093vuivAOrfh+vAqbdyubY FYdl5M8uDHcLlI3ReXNNjbCAjWAW2BbUZgVfV9z5f1m2n78oW+P9PVFG8p06NTN5IE5W BWpplgUUrx99vgt/T6R+WfO9pprrD+qSLwKSlwJBUDK/JeK/BRkOHGUQnwqBPAKDnL7E X4eLTBlVTqK8rr6BxlphcpB2SYel/0pZBPEx3spX6rsWq10iZWbEfCt0n60+k9G4Qb0i NM8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+Fw/RLa0qqZxrvLbFBIrrReftaOGOsQy0sSZgCq8MCM=; b=Jj6t+12hpgrEKrEKCMwQCPwpkt5207MCUKO7FZbNFyCMyZPvoH+v3KVwo9kbBYdO2g BAicjfzEfjsODAZkAsqX7HBX6sc3izXDujQ9lBwWi1kN6FHDIRaGSCykHCc10gHDL4wE IcOKNfTFa4IrRMmKLPL44cjb9X+t4kXEdZbKwMzwW1bnstKiC8Q1g42mmt3QB8xi01+U hTizMBXzMg3m0FycjCYoq+IQctoHSUJQj/8YdFha1DUQ125mg2k7Or0/KCwP9Kd3YURG vpT8h0eXFPebUcdzhWML8lGrM5AnsWecWpdkDlIQB9KfB9eqvLSZQQFaYnqfGb7Pe8pv nJrA== ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=H1rSCaho; spf=pass (google.com: domain of highguy@gmail.com designates 2a00:1450:4864:20::541 as permitted sender) smtp.mailfrom=highguy@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-ed1-x541.google.com (mail-ed1-x541.google.com. [2a00:1450:4864:20::541]) by gmr-mx.google.com with ESMTPS id m20si377935wmc.0.2020.05.28.23.58.48 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 28 May 2020 23:58:48 -0700 (PDT) Received-SPF: pass (google.com: domain of highguy@gmail.com designates 2a00:1450:4864:20::541 as permitted sender) client-ip=2a00:1450:4864:20::541; Received: by mail-ed1-x541.google.com with SMTP id e10so929115edq.0 for ; Thu, 28 May 2020 23:58:48 -0700 (PDT) X-Received: by 2002:a50:d65c:: with SMTP id c28mr6553954edj.21.1590735527902; Thu, 28 May 2020 23:58:47 -0700 (PDT) Received: from sde1.telecom-it.be (ptr-dv4l9au8mfcs6krkuj6.18120a2.ip6.access.telenet.be. [2a02:1811:ce13:ba00:485e:9916:929:292]) by smtp.googlemail.com with ESMTPSA id u20sm6051738edy.80.2020.05.28.23.58.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 May 2020 23:58:47 -0700 (PDT) From: Stijn Devriendt To: HIGHGuY@gmail.com, sbabic@denx.de Cc: swupdate@googlegroups.com, Stijn Devriendt Subject: [swupdate] [PATCH 2/3] ascii_to_bin: rework interface to accept destination length Date: Fri, 29 May 2020 08:58:30 +0200 Message-Id: <20200529065831.9062-2-sde@unmatched.eu> X-Mailer: git-send-email 2.17.1 In-Reply-To: <0543dba8-ff25-e539-606c-d638e6f41cef@denx.de> References: <0543dba8-ff25-e539-606c-d638e6f41cef@denx.de> X-Original-Sender: HIGHGuY@gmail.com X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=H1rSCaho; spf=pass (google.com: domain of highguy@gmail.com designates 2a00:1450:4864:20::541 as permitted sender) smtp.mailfrom=highguy@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , This also shows that the ivt buffer in copyfile was incorrectly sized. Signed-off-by: Stijn Devriendt --- core/cpio_utils.c | 4 ++-- core/util.c | 23 +++++++++++++---------- include/util.h | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/core/cpio_utils.c b/core/cpio_utils.c index da84057..9afb699 100644 --- a/core/cpio_utils.c +++ b/core/cpio_utils.c @@ -389,7 +389,7 @@ int copyfile(int fdin, void *out, unsigned int nbytes, unsigned long *offs, unsi unsigned int md_len = 0; unsigned char *aes_key = NULL; unsigned char *ivt = NULL; - unsigned char ivtbuf[32]; + unsigned char ivtbuf[16]; struct InputState input_state = { .fdin = fdin, @@ -449,7 +449,7 @@ int copyfile(int fdin, void *out, unsigned int nbytes, unsigned long *offs, unsi if (encrypted) { aes_key = get_aes_key(); - if (imgivt && strlen(imgivt) && !ascii_to_bin(ivtbuf, imgivt, sizeof(ivtbuf))) { + if (imgivt && strlen(imgivt) && !ascii_to_bin(ivtbuf, sizeof(ivtbuf), imgivt)) { ivt = ivtbuf; } else ivt = get_aes_ivt(); diff --git a/core/util.c b/core/util.c index bd7ef6e..88dd8ac 100644 --- a/core/util.c +++ b/core/util.c @@ -54,24 +54,27 @@ static char* TMPDIRSCRIPT = NULL; * Convert a hash as hexa string into a sequence of bytes * hash must be an array of 32 bytes as specified by SHA256 */ -int ascii_to_bin(unsigned char *dest, const char *src, size_t srclen) +int ascii_to_bin(unsigned char *dest, size_t dstlen, const char *src) { unsigned int i; unsigned int val; + size_t srclen; if (src == NULL) { return 0; } + srclen = strlen(src); + if (srclen % 2) return -EINVAL; - if (strlen(src) == srclen) { - for (i = 0; i < srclen; i+= 2) { - val = from_ascii(&src[i], 2, LG_16); - dest[i / 2] = val; + if (srclen == 2 * dstlen) { + for (i = 0; i < dstlen; i++) { + val = from_ascii(&src[i*2], 2, LG_16); + dest[i] = val; } } else - return -1; + return -EINVAL; return 0; } @@ -463,7 +466,7 @@ from_ascii (char const *where, size_t digs, unsigned logbase) int ascii_to_hash(unsigned char *hash, const char *s) { - return ascii_to_bin(hash, s, 64); + return ascii_to_bin(hash, SHA256_HASH_LENGTH, s); } void hash_to_ascii(const unsigned char *hash, char *str) @@ -572,8 +575,8 @@ int set_aes_key(const char *key, const char *ivt) return -ENOMEM; } - ret = ascii_to_bin(aes_key->key, key, sizeof(aes_key->key) * 2) | - ascii_to_bin(aes_key->ivt, ivt, sizeof(aes_key->ivt) * 2); + ret = ascii_to_bin(aes_key->key, sizeof(aes_key->key), key) | + ascii_to_bin(aes_key->ivt, sizeof(aes_key->ivt), ivt); if (ret) { return -EINVAL; @@ -589,7 +592,7 @@ int set_aes_ivt(const char *ivt) if (!aes_key) return -EFAULT; - ret = ascii_to_bin(aes_key->ivt, ivt, sizeof(aes_key->ivt) * 2); + ret = ascii_to_bin(aes_key->ivt, sizeof(aes_key->ivt), ivt); if (ret) { return -EINVAL; diff --git a/include/util.h b/include/util.h index 68df96a..2f83c8a 100644 --- a/include/util.h +++ b/include/util.h @@ -140,7 +140,7 @@ typedef void (*notifier) (RECOVERY_STATUS status, int error, int level, const ch uintmax_t from_ascii (char const *where, size_t digs, unsigned logbase); int ascii_to_hash(unsigned char *hash, const char *s); -int ascii_to_bin(unsigned char *dest, const char *src, size_t srclen); +int ascii_to_bin(unsigned char *dest, size_t dstlen, const char *src); void hash_to_ascii(const unsigned char *hash, char *s); int IsValidHash(const unsigned char *hash);