From patchwork Tue May 26 09:01:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stijn Devriendt X-Patchwork-Id: 1297818 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::340; helo=mail-wm1-x340.google.com; envelope-from=swupdate+bncbclnzfwotyirbyfvwp3akgqeligadii@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=Aa++G4fm; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=CeccMVqZ; dkim-atps=neutral Received: from mail-wm1-x340.google.com (mail-wm1-x340.google.com [IPv6:2a00:1450:4864:20::340]) (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 49WScg6Mkmz9sSs for ; Tue, 26 May 2020 19:01:23 +1000 (AEST) Received: by mail-wm1-x340.google.com with SMTP id p24sf587032wmc.1 for ; Tue, 26 May 2020 02:01:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1590483680; cv=pass; d=google.com; s=arc-20160816; b=aOLvBR3XT5SJbv/7c4Ya9iVwuhyDzczblBVVJqZWHS4jVOJtO/nQi7bJNphHKNicdD YUUP/rISYqgvlzsktjUE60D+hFC0HXlYeoiY3wlDa7daB57B2qwoWabJ9IJ/ehWKLNPF cuy6yKqajh7I35VxT/2jye1+lzQnk+jzTh8S2lTRdAeRlntxHtTtJjXeoCgv/IoGV5x0 hbpzLlJxKHIJkLenGVuscqYI7URuiV8W1g04GEtDLQAd6kvqjv48mvItwcdIQhSL/R3Z rVK8kLsvykVi1se4tnzqgfjp2fWkaRYNoT8f+QsCIvJcYEFHKiWk7SecOjKBqedfXELU BlGw== 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=9UxyUnCEQaSXo+luJclAULeCwznPih+QsIeZnaNUwkQ=; b=BpKobkRnx4tRZLINmmyS3kr/ctb1agI4aw7mTGxtgOeziivhN4pnubg/Aa9qH7c+bO Yboj9xlAWIq5OIrq1UEereFPNlWqdPvZ2DOYgUO5vSPQREm2opteTYWDIROXEgkb6Qso j4kxrjpw/ZkVYFyh6k8rbZbLVXGmrrI0XiRsnuhxR/fb37nxAX/NiNO1zCHaejQHfLI5 kyLD4IIhpEr/l3kfg5CjIIo6Ha4dsbMJaNjZYNjhTTaYV6RxfyRkNtCUuDaLKkM1LKsT Yb1WtWGPYAX+hG6MhslySLS9r+GkPqNWQhmv5fsPDLP9P5xSV6LW866ueTnDJ9xp/k6u iZcA== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=GNusjS8Z; 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=9UxyUnCEQaSXo+luJclAULeCwznPih+QsIeZnaNUwkQ=; b=Aa++G4fm+5yBhBbFYJS3Psxf7zxa4L8qel3Qra5ERl6eKyePoyZzvYFfQEUYqMdvSc V19IVSXYrwwNgf3YWhnINI/joLuA4HjRawyyNBm9et8IOmzL6Lrq4fJkPVId8vOGp/Cg 4XOpNpij+u0xyBosjVvKwGTXaCOIZCk/YuzCPd1ZqOMDMCPsqUrTpEaEvxAKUZwPisXm ppIoJOjZMvoL0PIXpzabYzTwSKVEW3jvhEQAkUP7gRJ+In3scVSAE6s+9LmkF91VG6Fm ez8vPYlHraOzH9bcmXmdNSmMhOa7APh5U6D2eEGexSOO9AQUl3ipvBU5WH9tua76FYNk TNYg== 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=9UxyUnCEQaSXo+luJclAULeCwznPih+QsIeZnaNUwkQ=; b=CeccMVqZO4p91zT7SlMUJYmsZdElHh0x946QPnn9w8gOiiAXYQHLy+0tpuxnVWpA/z 5QqV9EqKot3gD9F8vBmQZmjKvPmQSq8XrPq06HI/n2F/ogTkl/rLy+S5lgWbBjJ4pI2Q KagBL0PsrJbtlUltqw9FFQaVcOjW2nagPWsyopWhDqawKdmv72nxbFKPytd303Z+8N7P CiJhIUf4WK6WqpfMMKqEDa4s+YEysOl4q8WA3m2a8AyLnH/fH8E4CMaP7EG30p1tGu3b 13lA8cZxj4JxP/EcpZ4My2v6nzLhuSTKv7U7idaFBSH9GbwvWcS8rHfrTD09wP8FYw6e Wlig== 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=9UxyUnCEQaSXo+luJclAULeCwznPih+QsIeZnaNUwkQ=; b=pBnYHGpVJlj3k3ut3WcIew1AAlLRRApJq37Zewr9v6wQBlFzvCT1m7FlITB/8aDCaf kahcaAKnmx0QbVR+wB+gUEREGktZ5L7nZd5lHTAsv71WMY2H31+0sdS2Tda/srtCHNhP Ie9komZxsnfrgr2MnHDp6dfcwfhgPfoZOLNrtyRVuqOP8NyghPrp+QErjBMJ5GwsMNsh Oj7YpJAsV2c7Z9gFQ4FpKUcXuqDOCfprQJJMj5nrIfW0TYfArFZ5PQH7noG3KR5t+rn6 U6vMmsJPIjWOblakbIzMcmK7fb/a7isW5WYzRyYwoYAJNdtsL3clnqZX8b4cdUdfcmO7 Q+oQ== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM530X0I02OwqzEmHmzgbBmfCik916mGiJimMyZFJUGI8UdU/F7ZPe zHH1J401FjA9IB9FbLjWAeM= X-Google-Smtp-Source: ABdhPJwkvbZzSB9aQFNasTc4k4AbhEh89ZMUUQLSNgZFrecd5ejQNsE4BbYAbYDbfeuFdHHUcjEaLQ== X-Received: by 2002:adf:9795:: with SMTP id s21mr12446713wrb.166.1590483680808; Tue, 26 May 2020 02:01:20 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: swupdate@googlegroups.com Received: by 2002:adf:82ce:: with SMTP id 72ls13184099wrc.1.gmail; Tue, 26 May 2020 02:01:20 -0700 (PDT) X-Received: by 2002:a5d:4d0d:: with SMTP id z13mr4601601wrt.220.1590483680290; Tue, 26 May 2020 02:01:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590483680; cv=none; d=google.com; s=arc-20160816; b=T5pQbZkmYd7KklVh/j5SW3trKOlj/xOlZkBuMUGgDgS0yuzvAYYopmXTkxtYTZmhUc 8DnsPC6WjwQPpEjPOiiCtM8qic9KbJ/LwRxWVs4q2KGLsutuz5baBk0bEB3zNX598DcN ar9gJANjNphdZ7OqatDmu4WU7uyDPi6s2bSejKoYcePhtQqn91/YFMQ3pBc/1iaR8MMr bTh4SePjR6yR2afwBYo6TqJFgyKgq/gKq48SS96Rv4xgzhktX6B5W567JJwwUfKTmjL7 zMd7PRE9RfOb/2Q+FkCsngPCDt+UpfPfHsz2k7YEFry5NV+ZBsnp2E5XgPo7Hi799u+u fhrw== 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=zlSwwd27XNC0nw6tuvElhIHqdeYxqpTx1iK4IGwaBsc=; b=r6AV4NRXBx54SUafTB3UWxz2NJVrN1XPSBvPU0och1kYQckxtUbZsWOOJj3zpUCNx9 zEiA8iR2TQhNJMjViI1DOhbZLL4XfjWiAWLefREGpBb0G3Pgf7Dv1kVr23bFSnrxNxR8 ZbprBYDFXnGqq5WxoVGt3G5nBATribamHEatgupFmiDJRrnAtZ5a+D1PKcFrFlFVpMo4 CfEzFEoYbrsY5OPYH/GY7o+gDv5T9cA+Uh0UrW1uBJ9lE3KBvwF2beeV1LERNRIKhMsj oWR0XtCQLMjgVlPMd6oLhH69jYV37/38i+07Lofyx4n9c4Jc3kLaaWWrL7vaDYikspZZ 63QQ== ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=GNusjS8Z; 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 s137si548255wme.2.2020.05.26.02.01.20 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 26 May 2020 02:01:20 -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 bs4so17001510edb.6 for ; Tue, 26 May 2020 02:01:20 -0700 (PDT) X-Received: by 2002:a50:ec83:: with SMTP id e3mr19541834edr.349.1590483680075; Tue, 26 May 2020 02:01:20 -0700 (PDT) Received: from sde1.telecom-it.be (ptr-dv4l9atc2l38asy60vl.18120a2.ip6.access.telenet.be. [2a02:1811:ce13:ba00:ce9:39e6:1495:3bf1]) by smtp.googlemail.com with ESMTPSA id 13sm11834875ejh.65.2020.05.26.02.01.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 May 2020 02:01:19 -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: Tue, 26 May 2020 11:01:02 +0200 Message-Id: <20200526090103.6489-2-sde@unmatched.eu> X-Mailer: git-send-email 2.17.1 In-Reply-To: <062DA3C1-232F-47EC-9437-8E45185A1E66@gmail.com> References: <062DA3C1-232F-47EC-9437-8E45185A1E66@gmail.com> 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=GNusjS8Z; 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. Acked-by: Stefano Babic --- 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);