questions on {ERASE,WRITE,UPDATE} BINARY commands

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

questions on {ERASE,WRITE,UPDATE} BINARY commands

Peter Marschall
Hi,

I have questions on the {ERASE,WRITE,UPDATE} BINARY commands for which I do
not find the answer in the spec (ISO-7816-4).

Here they are:
* What's the exact difference between WRITE BINARY & UPDATE BINARY?
  My understanding of the spec is that WRITE BINARY can extend a file's size,
  while UPDATE BINARY can only update data elements that are already within
  the file (i.e. in the range [0 .. file_size-1]).
  Is my understanding correct or did I misunderstand the specscompletely?

* Is it to be considered an error if UPDATE BINARY
   a) uses an idx >= existing_file_size ?
   b) wants to update 0 data elements (i.e. count = 0) ?
   c) idx + count >= existing_file_size?

* Similar for ERASE BINARY
   a) Can it set data elements to logical erased state beyond the file size?
        i.e. idx + count >= existing_file_size
   b) Is it an error to erase 0 data alements
       i.e. count = 0
   c) If idx + count >= file_size, does the file get zapped (=shortened)
       to idx data elements?
 
Thanks in advance
Peter

--
Peter Marschall
[hidden email]
_______________________________________________
opensc-devel mailing list
[hidden email]
http://www.opensc-project.org/mailman/listinfo/opensc-devel
Reply | Threaded
Open this post in threaded view
|

Re: questions on {ERASE, WRITE, UPDATE} BINARY commands

Martin Paljak-4
Hello,

On Thu, Jun 7, 2012 at 10:24 PM, Peter Marschall <[hidden email]> wrote:
> Here they are:
> * What's the exact difference between WRITE BINARY & UPDATE BINARY?
>  My understanding of the spec is that WRITE BINARY can extend a file's size,
>  while UPDATE BINARY can only update data elements that are already within
>  the file (i.e. in the range [0 .. file_size-1]).
>  Is my understanding correct or did I misunderstand the specscompletely?
AFAIU either can change file size (which can be done though 7816-9).
UPDATE will *set* the bits as given in the command, whereas WRITE can
allow some bit-fiddling.

Why the question? If there would be a card that implements both, I
think you would want to use UPDATE, at least in the context of OpenSC,
unless it is *not* supported and WRITE is supported.

What exactly is the context?

> * Is it to be considered an error if UPDATE BINARY
>   a) uses an idx >= existing_file_size ?
Probably. '6B00' (offset outside the EF)
>   b) wants to update 0 data elements (i.e. count = 0) ?
IMHO should not, but implementations might vary, of course.
>   c) idx + count >= existing_file_size?
Probably. '6B00' (offset outside the EF)


> * Similar for ERASE BINARY
>   a) Can it set data elements to logical erased state beyond the file size?
>        i.e. idx + count >= existing_file_size
>   b) Is it an error to erase 0 data alements
>       i.e. count = 0
>   c) If idx + count >= file_size, does the file get zapped (=shortened)
>       to idx data elements?
Ditto.

Martin
_______________________________________________
opensc-devel mailing list
[hidden email]
http://www.opensc-project.org/mailman/listinfo/opensc-devel
Reply | Threaded
Open this post in threaded view
|

Re: questions on {ERASE, WRITE, UPDATE} BINARY commands

Martin Paljak-4
On Thu, Jun 7, 2012 at 10:35 PM, Martin Paljak <[hidden email]> wrote:

> Hello,
>
> On Thu, Jun 7, 2012 at 10:24 PM, Peter Marschall <[hidden email]> wrote:
>> Here they are:
>> * What's the exact difference between WRITE BINARY & UPDATE BINARY?
>>  My understanding of the spec is that WRITE BINARY can extend a file's size,
>>  while UPDATE BINARY can only update data elements that are already within
>>  the file (i.e. in the range [0 .. file_size-1]).
>>  Is my understanding correct or did I misunderstand the specscompletely?
> AFAIU either can change file size (which can be done though 7816-9).
Correction, can NOT change file size.
_______________________________________________
opensc-devel mailing list
[hidden email]
http://www.opensc-project.org/mailman/listinfo/opensc-devel
Reply | Threaded
Open this post in threaded view
|

Re: questions on {ERASE, WRITE, UPDATE} BINARY commands

Peter Marschall
In reply to this post by Martin Paljak-4
Hi Martin,

thanks for the quick reply.

On Thursday, 7. June 2012, Martin Paljak wrote:

> On Thu, Jun 7, 2012 at 10:24 PM, Peter Marschall <[hidden email]> wrote:
> > Here they are:
> > * What's the exact difference between WRITE BINARY & UPDATE BINARY?
> >  My understanding of the spec is that WRITE BINARY can extend a file's
> > size, while UPDATE BINARY can only update data elements that are already
> > within the file (i.e. in the range [0 .. file_size-1]).
> >  Is my understanding correct or did I misunderstand the specscompletely?
>
> AFAIU either can change file size (which can be done though 7816-9).
> UPDATE will *set* the bits as given in the command, whereas WRITE can
> allow some bit-fiddling.
>
> Why the question? If there would be a card that implements both, I
> think you would want to use UPDATE, at least in the context of OpenSC,
> unless it is *not* supported and WRITE is supported.
>
> What exactly is the context?
The wish to implement them correctly for the OpenPGP card.

>
> > * Is it to be considered an error if UPDATE BINARY
> >   a) uses an idx >= existing_file_size ?
>
> Probably. '6B00' (offset outside the EF)
>
> >   b) wants to update 0 data elements (i.e. count = 0) ?
>
> IMHO should not, but implementations might vary, of course.
>
> >   c) idx + count >= existing_file_size?
>
> Probably. '6B00' (offset outside the EF)
>
> > * Similar for ERASE BINARY
> >   a) Can it set data elements to logical erased state beyond the file
> > size? i.e. idx + count >= existing_file_size
> >   b) Is it an error to erase 0 data alements
> >       i.e. count = 0
> >   c) If idx + count >= file_size, does the file get zapped (=shortened)
> >       to idx data elements?
>
> Ditto.
>
> Martin


--
Peter Marschall
[hidden email]
_______________________________________________
opensc-devel mailing list
[hidden email]
http://www.opensc-project.org/mailman/listinfo/opensc-devel
Reply | Threaded
Open this post in threaded view
|

Re: questions on {ERASE, WRITE, UPDATE} BINARY commands

Martin Paljak-4
On Thu, Jun 7, 2012 at 10:44 PM, Peter Marschall <[hidden email]> wrote:
>> Why the question? If there would be a card that implements both, I
>> think you would want to use UPDATE, at least in the context of OpenSC,
>> unless it is *not* supported and WRITE is supported.
>>
>> What exactly is the context?
> The wish to implement them correctly for the OpenPGP card.

I don't see references to UPDATE/WRITE/ERASE BINARY in OpenPGP 2.0.1
spec, only PUT DATA?

See also this e-mail:

http://lists.gnupg.org/pipermail/gnupg-devel/2011-May/026079.html


Martin
_______________________________________________
opensc-devel mailing list
[hidden email]
http://www.opensc-project.org/mailman/listinfo/opensc-devel
Reply | Threaded
Open this post in threaded view
|

Re: questions on {ERASE, WRITE, UPDATE} BINARY commands

Peter Marschall
In reply to this post by Martin Paljak-4
Hi,

thanks for the quick reply/correction.

On Thursday, 7. June 2012, Martin Paljak wrote:
> On Thu, Jun 7, 2012 at 10:35 PM, Martin Paljak <[hidden email]>
wrote:

> > Hello,
> >
> > On Thu, Jun 7, 2012 at 10:24 PM, Peter Marschall <[hidden email]> wrote:
> >> Here they are:
> >> * What's the exact difference between WRITE BINARY & UPDATE BINARY?
> >>  My understanding of the spec is that WRITE BINARY can extend a file's
> >> size, while UPDATE BINARY can only update data elements that are
> >> already within the file (i.e. in the range [0 .. file_size-1]).
> >>  Is my understanding correct or did I misunderstand the specscompletely?
> >
> > AFAIU either can change file size (which can be done though 7816-9).
>
> Correction, can NOT change file size.

Does that mean that none of them can change the number of data elements that
are in the file ?

This seems to contradict the sentence in ISO 7816-4 7.2.4 WRITE BINARY which
states:
"- the write-once of the bits given in the command data field (the command
shall be aborted if thestring of data units is not in the logical erased
state)"

To me that sentence sounds like WRITE BINARY is an operation that
A) can only be used on data that is logically reset,
    (i.e. once WRITE_BINARY was performed, it cannot be used on the same data
    any more without a preceding ERASE BINARY of that region)
B) can extend the number of data units in the file
    (this is what I sloppily called existing_file_size in my< previous mail)

In the other hand, ISO 7816-4 7.2.4 UPDATE BINARY says:
"the command initialtes the update of the bits already present in an EF ..."

This is what I interpret as "can only update existing data units in the file,
but not create more.

Am I completely wrong?
Are there "interpretation helpers" for the spec available somewhere?


While I am at it:
Would you mind to pull Pull Request #53
        https://github.com/OpenSC/OpenSC/pull/53
into the staging branch of github's open/opensc?
(It is a little bit frustrating to not get any feedback at all for a PullReq
;-)

Thanks
PEter

--
Peter Marschall
[hidden email]
_______________________________________________
opensc-devel mailing list
[hidden email]
http://www.opensc-project.org/mailman/listinfo/opensc-devel
Reply | Threaded
Open this post in threaded view
|

Re: questions on {ERASE, WRITE, UPDATE} BINARY commands

Andreas Schwier (ML)
Hi Peter,

ERASE and WRITE are left-overs from the old smart card days. Most - if
not all - cards and applications today only implement UPDATE BINARY.

There is also no common understanding that UPDATE BINARY must not extend
the length of an EF. Some implementations maintain a maximum EF and a
current EF size. The maximum size is typically set in CREATE FILE,
whereas the current EF size depends on the amount of data written to the
EF. An EF may start with no data contained and and UPDATE BINARY command
with P1|P2 = Length of EF (or zero based offset after last byte ;-)
appends the amount of data provided in the C-Data of the APDU. Usually
gaps are not allowed, so an offset beyond end-of-file + 1 gives
SW1/SW2=6B00.

Other implementations allocate the full EF size at creation, so you can
immediately read from the EF, even though no data has been written yet.

Hope this helps,

Andreas

Am 07.06.2012 22:01, schrieb Peter Marschall:

> Hi,
>
> thanks for the quick reply/correction.
>
> On Thursday, 7. June 2012, Martin Paljak wrote:
>> On Thu, Jun 7, 2012 at 10:35 PM, Martin Paljak <[hidden email]>
> wrote:
>>> Hello,
>>>
>>> On Thu, Jun 7, 2012 at 10:24 PM, Peter Marschall <[hidden email]> wrote:
>>>> Here they are:
>>>> * What's the exact difference between WRITE BINARY & UPDATE BINARY?
>>>>  My understanding of the spec is that WRITE BINARY can extend a file's
>>>> size, while UPDATE BINARY can only update data elements that are
>>>> already within the file (i.e. in the range [0 .. file_size-1]).
>>>>  Is my understanding correct or did I misunderstand the specscompletely?
>>> AFAIU either can change file size (which can be done though 7816-9).
>> Correction, can NOT change file size.
> Does that mean that none of them can change the number of data elements that
> are in the file ?
>
> This seems to contradict the sentence in ISO 7816-4 7.2.4 WRITE BINARY which
> states:
> "- the write-once of the bits given in the command data field (the command
> shall be aborted if thestring of data units is not in the logical erased
> state)"
>
> To me that sentence sounds like WRITE BINARY is an operation that
> A) can only be used on data that is logically reset,
>     (i.e. once WRITE_BINARY was performed, it cannot be used on the same data
>     any more without a preceding ERASE BINARY of that region)
> B) can extend the number of data units in the file
>     (this is what I sloppily called existing_file_size in my< previous mail)
>
> In the other hand, ISO 7816-4 7.2.4 UPDATE BINARY says:
> "the command initialtes the update of the bits already present in an EF ..."
>
> This is what I interpret as "can only update existing data units in the file,
> but not create more.
>
> Am I completely wrong?
> Are there "interpretation helpers" for the spec available somewhere?
>
>
> While I am at it:
> Would you mind to pull Pull Request #53
> https://github.com/OpenSC/OpenSC/pull/53
> into the staging branch of github's open/opensc?
> (It is a little bit frustrating to not get any feedback at all for a PullReq
> ;-)
>
> Thanks
> PEter
>


--

    ---------    CardContact Software & System Consulting
   |.##> <##.|   Andreas Schwier
   |#       #|   Schülerweg 38
   |#       #|   32429 Minden, Germany
   |'##> <##'|   Phone +49 171 8334920
    ---------    http://www.cardcontact.de
                 http://www.tscons.de
                 http://www.openscdp.org


_______________________________________________
opensc-devel mailing list
[hidden email]
http://www.opensc-project.org/mailman/listinfo/opensc-devel
Reply | Threaded
Open this post in threaded view
|

Re: questions on {ERASE, WRITE, UPDATE} BINARY commands

Peter Marschall
Hi Andreas,

thanks a ton.
That explains it very well and answers all my questions.

On Friday, 8. June 2012, Andreas Schwier (ML) wrote:

> Hi Peter,
>
> ERASE and WRITE are left-overs from the old smart card days. Most - if
> not all - cards and applications today only implement UPDATE BINARY.
>
> There is also no common understanding that UPDATE BINARY must not extend
> the length of an EF. Some implementations maintain a maximum EF and a
> current EF size. The maximum size is typically set in CREATE FILE,
> whereas the current EF size depends on the amount of data written to the
> EF. An EF may start with no data contained and and UPDATE BINARY command
> with P1|P2 = Length of EF (or zero based offset after last byte ;-)
> appends the amount of data provided in the C-Data of the APDU. Usually
> gaps are not allowed, so an offset beyond end-of-file + 1 gives
> SW1/SW2=6B00.
>
> Other implementations allocate the full EF size at creation, so you can
> immediately read from the EF, even though no data has been written yet.
>
> Hope this helps,
>
> Andreas
>
> Am 07.06.2012 22:01, schrieb Peter Marschall:
> > Hi,
> >
> > thanks for the quick reply/correction.
> >
> > On Thursday, 7. June 2012, Martin Paljak wrote:
> >> On Thu, Jun 7, 2012 at 10:35 PM, Martin Paljak <[hidden email]>
> >
> > wrote:
> >>> Hello,
> >>>
> >>> On Thu, Jun 7, 2012 at 10:24 PM, Peter Marschall <[hidden email]> wrote:
> >>>> Here they are:
> >>>> * What's the exact difference between WRITE BINARY & UPDATE BINARY?
> >>>>
> >>>>  My understanding of the spec is that WRITE BINARY can extend a file's
> >>>>
> >>>> size, while UPDATE BINARY can only update data elements that are
> >>>> already within the file (i.e. in the range [0 .. file_size-1]).
> >>>>
> >>>>  Is my understanding correct or did I misunderstand the
> >>>>  specscompletely?
> >>>
> >>> AFAIU either can change file size (which can be done though 7816-9).
> >>
> >> Correction, can NOT change file size.
> >
> > Does that mean that none of them can change the number of data elements
> > that are in the file ?
> >
> > This seems to contradict the sentence in ISO 7816-4 7.2.4 WRITE BINARY
> > which states:
> > "- the write-once of the bits given in the command data field (the
> > command shall be aborted if thestring of data units is not in the
> > logical erased state)"
> >
> > To me that sentence sounds like WRITE BINARY is an operation that
> > A) can only be used on data that is logically reset,
> >
> >     (i.e. once WRITE_BINARY was performed, it cannot be used on the same
> >     data any more without a preceding ERASE BINARY of that region)
> >
> > B) can extend the number of data units in the file
> >
> >     (this is what I sloppily called existing_file_size in my< previous
> >     mail)
> >
> > In the other hand, ISO 7816-4 7.2.4 UPDATE BINARY says:
> > "the command initialtes the update of the bits already present in an EF
> > ..."
> >
> > This is what I interpret as "can only update existing data units in the
> > file, but not create more.
> >
> > Am I completely wrong?
> > Are there "interpretation helpers" for the spec available somewhere?
> >
> >
> > While I am at it:
> > Would you mind to pull Pull Request #53
> >
> > https://github.com/OpenSC/OpenSC/pull/53
> >
> > into the staging branch of github's open/opensc?
> > (It is a little bit frustrating to not get any feedback at all for a
> > PullReq ;-)
> >
> > Thanks
> > PEter


--
Peter Marschall
[hidden email]
_______________________________________________
opensc-devel mailing list
[hidden email]
http://www.opensc-project.org/mailman/listinfo/opensc-devel
Reply | Threaded
Open this post in threaded view
|

Re: questions on {ERASE,WRITE,UPDATE} BINARY commands

DARc
In reply to this post by Peter Marschall
Hi,
Maybe it will help somebody even if topic is a bit old :)

Differences of those commands are also related to so called logical erased state of EF binary file.
Logical erased state is defined by card OS designer and can be ‘00’ or ‘FF’.
The whole card or just particular EF can have stated proper logic of WriteBinary which is :

- the write-once of the bits given in the command data field (the command shall be aborted if the string of
data units is not in the logical erased state);
- the logical-OR of the bits already present in the card with the bits given in the command data field (the
logical erased state of the bits of the file is zero);
- the logical-AND of the bits already present in the card with the bits given in the command data field (the
logical erased state of the bits of the file is one).

Logical erased state is notified to the public in ATR historical byte (second software function table of Card Capabilities) for the card and/or at EF level in FCI tag 82 – second byte -> “data coding byte” (also in any parent file up to MF).

Lack of data coding byte or ATR definition lead to default logic for WriteBinary which is OR and EF file logical erase state is all ‘00’.

Depending on those declaration you someone can figure out what value has “empty binary file”: 00 or FF, as this can be its “logical erased state”.

Finally differences in relation to UpdateBinary:
- UpdateBinary simply SET each bit of any byte addressed to the new value,
- WriteBinarry does OR/AND with current value of each bit of information in EF binary file – binary results of writing the same data can be finally different depending on logic assigned to card/EF file,
- if write-once is used WriteBiarry can end with error state if initial values WERE NOT in logical erased state.

And please notice that in par EraseBinary fills EF binary file with either ‘00’ of ‘FF’ depending of chosen convention.

Hope this helps you or somebody else :)